Removing packed variables mode
I halfway did it in dc223e5
. Here's the other half. Also some minor
cleanup/refactoring as per usual.
This commit is contained in:
parent
30c3f6f19c
commit
b147006ffe
1 changed files with 42 additions and 32 deletions
74
ssc/scope.cs
74
ssc/scope.cs
|
@ -4,47 +4,57 @@ using System.Linq;
|
||||||
|
|
||||||
namespace arookas {
|
namespace arookas {
|
||||||
class sunScopeStack : IEnumerable<sunScope> {
|
class sunScopeStack : IEnumerable<sunScope> {
|
||||||
List<sunScope> Stack { get; set; }
|
List<sunScope> mStack;
|
||||||
|
int mGlobals, mLocals;
|
||||||
|
|
||||||
public int Count { get { return Stack.Count; } }
|
public int Count {
|
||||||
#if SSC_PACK_VARS
|
get { return mStack.Count; }
|
||||||
int GlobalCount { get { return Stack.Where(i => i.Type == sunScopeType.Script).Sum(i => i.VariableCount); } }
|
}
|
||||||
int LocalCount { get { return Stack.Where(i => i.Type == sunScopeType.Function).Sum(i => i.VariableCount); } }
|
|
||||||
#else
|
|
||||||
int GlobalCount { get; set; }
|
|
||||||
int LocalCount { get; set; }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public sunScope Root { get { return this.FirstOrDefault(i => i.Type == Top.Type); } }
|
public sunScope Root {
|
||||||
public sunScope Script { get { return this.FirstOrDefault(i => i.Type == sunScopeType.Script); } }
|
get { return this.FirstOrDefault(i => i.Type == Top.Type); }
|
||||||
public sunScope Function { get { return this.FirstOrDefault(i => i.Type == sunScopeType.Function); } }
|
}
|
||||||
public sunScope Top { get { return this[Count - 1]; } }
|
public sunScope Script {
|
||||||
|
get { return this.FirstOrDefault(i => i.Type == sunScopeType.Script); }
|
||||||
|
}
|
||||||
|
public sunScope Function {
|
||||||
|
get { return this.FirstOrDefault(i => i.Type == sunScopeType.Function); }
|
||||||
|
}
|
||||||
|
public sunScope Top {
|
||||||
|
get { return this[Count - 1]; }
|
||||||
|
}
|
||||||
|
|
||||||
public sunScope this[int index] { get { return Stack[index]; } }
|
public sunScope this[int index] {
|
||||||
|
get { return mStack[index]; }
|
||||||
|
}
|
||||||
|
|
||||||
public sunScopeStack() {
|
public sunScopeStack() {
|
||||||
Stack = new List<sunScope>(8);
|
mStack = new List<sunScope>(8);
|
||||||
Push(sunScopeType.Script); // push global scope
|
Push(sunScopeType.Script); // push global scope
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Push() { Push(Top.Type); }
|
public void Push() { Push(Top.Type); }
|
||||||
public void Push(sunScopeType type) {
|
public void Push(sunScopeType type) {
|
||||||
Stack.Add(new sunScope(type));
|
mStack.Add(new sunScope(type));
|
||||||
}
|
}
|
||||||
public void Pop() {
|
public void Pop() {
|
||||||
if (Count > 1) {
|
if (Count > 1) {
|
||||||
Stack.RemoveAt(Count - 1);
|
mStack.RemoveAt(Count - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Clear() {
|
public void Clear() {
|
||||||
Stack.Clear();
|
mStack.Clear();
|
||||||
Push(sunScopeType.Script); // add global scope
|
Push(sunScopeType.Script); // add global scope
|
||||||
|
mGlobals = 0;
|
||||||
|
mLocals = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if !SSC_PACK_VARS
|
public void ResetGlobalCount() {
|
||||||
public void ResetGlobalCount() { GlobalCount = 0; }
|
mGlobals = 0;
|
||||||
public void ResetLocalCount() { LocalCount = 0; }
|
}
|
||||||
#endif
|
public void ResetLocalCount() {
|
||||||
|
mLocals = 0;
|
||||||
|
}
|
||||||
|
|
||||||
public sunVariableSymbol DeclareVariable(string name) {
|
public sunVariableSymbol DeclareVariable(string name) {
|
||||||
switch (Top.Type) {
|
switch (Top.Type) {
|
||||||
|
@ -57,26 +67,26 @@ namespace arookas {
|
||||||
return Top.DeclareConstant(name, expression);
|
return Top.DeclareConstant(name, expression);
|
||||||
}
|
}
|
||||||
sunVariableSymbol DeclareGlobal(string name) {
|
sunVariableSymbol DeclareGlobal(string name) {
|
||||||
var symbol = Top.DeclareVariable(name, 0, GlobalCount);
|
var symbol = Top.DeclareVariable(name, 0, mGlobals);
|
||||||
#if !SSC_PACK_VARS
|
|
||||||
if (symbol != null) {
|
if (symbol != null) {
|
||||||
++GlobalCount;
|
++mGlobals;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
sunVariableSymbol DeclareLocal(string name) {
|
sunVariableSymbol DeclareLocal(string name) {
|
||||||
var symbol = Top.DeclareVariable(name, 1, LocalCount);
|
var symbol = Top.DeclareVariable(name, 1, mLocals);
|
||||||
#if !SSC_PACK_VARS
|
|
||||||
if (symbol != null) {
|
if (symbol != null) {
|
||||||
++LocalCount;
|
++mLocals;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerator<sunScope> GetEnumerator() { return Stack.GetEnumerator(); }
|
public IEnumerator<sunScope> GetEnumerator() {
|
||||||
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
|
return mStack.GetEnumerator();
|
||||||
|
}
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() {
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class sunScope {
|
class sunScope {
|
||||||
|
|
Loading…
Reference in a new issue