Fixed: correctly resolve storable symbols

This commit is contained in:
arookas 2016-02-01 14:03:56 -05:00
parent 6f1dec212b
commit dc223e5c26

View file

@ -98,26 +98,14 @@ namespace arookas {
return DeclareVariable(node.Name, node.Modifiers); return DeclareVariable(node.Name, node.Modifiers);
} }
sunVariableSymbol DeclareVariable(sunIdentifier node, sunSymbolModifiers modifiers) { sunVariableSymbol DeclareVariable(sunIdentifier node, sunSymbolModifiers modifiers) {
var name = MangleSymbolName(node.Value, false, (modifiers & sunSymbolModifiers.Local) != 0); var local = (modifiers & sunSymbolModifiers.Local) != 0;
#if SSC_PACK_VARS var name = MangleSymbolName(node.Value, false, local);
if (Scopes.Top.GetIsDeclared(name)) {
throw new sunRedeclaredVariableException(node);
}
#else
if (Scopes.Any(i => i.GetIsDeclared(name))) { if (Scopes.Any(i => i.GetIsDeclared(name))) {
throw new sunRedeclaredVariableException(node); throw new sunRedeclaredVariableException(node);
} }
#endif
var symbol = Scopes.DeclareVariable(name); var symbol = Scopes.DeclareVariable(name);
if (Scopes.Top.Type == sunScopeType.Script) { // global-scope variables are added to the symbol table if (Scopes.Top.Type == sunScopeType.Script) { // global-scope variables are added to the symbol table
#if SSC_PACK_VARS
// 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); SymbolTable.Add(symbol);
#endif
} }
return symbol; return symbol;
} }
@ -125,27 +113,29 @@ namespace arookas {
return DeclareConstant(node.Name, node.Expression, node.Modifiers); return DeclareConstant(node.Name, node.Expression, node.Modifiers);
} }
sunConstantSymbol DeclareConstant(sunIdentifier node, sunExpression expression, sunSymbolModifiers modifiers) { sunConstantSymbol DeclareConstant(sunIdentifier node, sunExpression expression, sunSymbolModifiers modifiers) {
var name = MangleSymbolName(node.Value, false, (modifiers & sunSymbolModifiers.Local) != 0); var local = (modifiers & sunSymbolModifiers.Local) != 0;
#if SSC_PACK_VARS var name = MangleSymbolName(node.Value, false, local);
if (Scopes.Top.GetIsDeclared(name)) {
throw new sunRedeclaredVariableException(node);
}
#else
if (Scopes.Any(i => i.GetIsDeclared(name))) { if (Scopes.Any(i => i.GetIsDeclared(name))) {
throw new sunRedeclaredVariableException(node); throw new sunRedeclaredVariableException(node);
} }
#endif
return Scopes.DeclareConstant(name, expression); return Scopes.DeclareConstant(name, expression);
} }
public sunStorableSymbol ResolveStorable(sunIdentifier node) { public sunStorableSymbol ResolveStorable(sunIdentifier node) {
var global = node.Value; var global = node.Value;
var local = MangleSymbolName(global, false, true); var local = MangleSymbolName(global, false, true);
var symbol = ResolveStorable(local);
if (symbol != null) {
return symbol;
}
symbol = ResolveStorable(global);
if (symbol != null) {
return symbol;
}
return null;
}
sunStorableSymbol ResolveStorable(string name) {
for (int i = Scopes.Count - 1; i >= 0; --i) { for (int i = Scopes.Count - 1; i >= 0; --i) {
var symbol = Scopes[i].ResolveStorable(local); var symbol = Scopes[i].ResolveStorable(name);
if (symbol != null) {
return symbol;
}
symbol = Scopes[i].ResolveStorable(global);
if (symbol != null) { if (symbol != null) {
return symbol; return symbol;
} }