Made variable-packing optional.

This commit is contained in:
arookas 2015-12-28 02:47:49 -05:00
parent 63740ea381
commit 1b514eecff
3 changed files with 50 additions and 5 deletions

View file

@ -180,16 +180,36 @@ namespace arookas
// storables
public sunVariableSymbol DeclareVariable(sunIdentifier node)
{
// assert variable is not already declared in current scope
#if SSC_RESOLVEVAR_NEST
if (Scopes.Top.GetIsDeclared(node.Value))
#else
if (Scopes.Any(i => i.GetIsDeclared(node.Value)))
#endif
{
throw new sunRedeclaredVariableException(node);
}
return Scopes.DeclareVariable(node.Value);
var symbol = Scopes.DeclareVariable(node.Value);
if (Scopes.Top.Type == sunScopeType.Script) // global-scope variables are added to the symbol table
{
#if SSC_RESOLVEVAR_NEST
// only add the variable symbol if there isn't one with this index already
if (!SymbolTable.Variables.Any(i => i.Index == symbol.Index))
{
SymbolTable.Add(new sunVariableSymbol(String.Format("global{0}", symbol.Index), symbol.Display, symbol.Index));
}
#else
SymbolTable.Add(symbol);
#endif
}
return symbol;
}
public sunConstantSymbol DeclareConstant(sunIdentifier node, sunExpression expression)
{
#if SSC_RESOLVEVAR_NEST
if (Scopes.Top.GetIsDeclared(node.Value))
#else
if (Scopes.Any(i => i.GetIsDeclared(node.Value)))
#endif
{
throw new sunRedeclaredVariableException(node);
}

View file

@ -9,8 +9,13 @@ namespace arookas
List<sunScope> Stack { get; set; }
public int Count { get { return Stack.Count; } }
#if SSC_PACK_VARS
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 Script { get { return this.FirstOrDefault(i => i.Type == sunScopeType.Script); } }
@ -42,6 +47,11 @@ namespace arookas
Push(sunScopeType.Script); // add global scope
}
#if !SSC_PACK_VARS
public void ResetGlobalCount() { GlobalCount = 0; }
public void ResetLocalCount() { LocalCount = 0; }
#endif
public sunVariableSymbol DeclareVariable(string name)
{
switch (Top.Type)
@ -57,11 +67,25 @@ namespace arookas
}
sunVariableSymbol DeclareGlobal(string name)
{
return Top.DeclareVariable(name, 0, GlobalCount);
var symbol = Top.DeclareVariable(name, 0, GlobalCount);
#if !SSC_PACK_VARS
if (symbol != null)
{
++GlobalCount;
}
#endif
return symbol;
}
sunVariableSymbol DeclareLocal(string name)
{
return Top.DeclareVariable(name, 1, LocalCount);
var symbol = Top.DeclareVariable(name, 1, LocalCount);
#if !SSC_PACK_VARS
if (symbol != null)
{
++LocalCount;
}
#endif
return symbol;
}
public IEnumerator<sunScope> GetEnumerator() { return Stack.GetEnumerator(); }

View file

@ -140,9 +140,10 @@ namespace arookas
{
Offset = context.Text.Offset;
context.Scopes.Push(sunScopeType.Function);
context.Scopes.ResetLocalCount();
foreach (var parameter in Parameters)
{
context.Scopes.DeclareVariable(parameter);
context.Scopes.DeclareVariable(parameter); // since there is no AST node for these, they won't affect MaxLocalCount
}
context.Text.StoreDisplay(1);
context.Text.DeclareLocal(Body.MaxLocalCount);