Added local-scope for functions & script variables
Also moved name-mangling logic to its own function. Next, adding actual support in the language.
This commit is contained in:
parent
32b716a3b9
commit
c0e747d2bd
1 changed files with 57 additions and 10 deletions
|
@ -8,6 +8,8 @@ using System.Text;
|
||||||
namespace arookas {
|
namespace arookas {
|
||||||
class sunContext {
|
class sunContext {
|
||||||
Stack<sunNameLabel> mNameStack;
|
Stack<sunNameLabel> mNameStack;
|
||||||
|
Stack<long> mLocalStack;
|
||||||
|
long mLocal;
|
||||||
|
|
||||||
public sunDataTable DataTable { get; private set; }
|
public sunDataTable DataTable { get; private set; }
|
||||||
public sunSymbolTable SymbolTable { get; private set; }
|
public sunSymbolTable SymbolTable { get; private set; }
|
||||||
|
@ -31,7 +33,8 @@ namespace arookas {
|
||||||
SymbolTable = new sunSymbolTable();
|
SymbolTable = new sunSymbolTable();
|
||||||
Scopes = new sunScopeStack();
|
Scopes = new sunScopeStack();
|
||||||
Loops = new sunLoopStack();
|
Loops = new sunLoopStack();
|
||||||
mNameStack = new Stack<sunNameLabel>(5);
|
mNameStack = new Stack<sunNameLabel>(10);
|
||||||
|
mLocalStack = new Stack<long>(10);
|
||||||
AddSystemSymbols();
|
AddSystemSymbols();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +44,8 @@ namespace arookas {
|
||||||
Scopes.Clear();
|
Scopes.Clear();
|
||||||
Loops.Clear();
|
Loops.Clear();
|
||||||
mNameStack.Clear();
|
mNameStack.Clear();
|
||||||
|
mLocalStack.Clear();
|
||||||
|
mLocal = 0;
|
||||||
|
|
||||||
// reinstall system symbols
|
// reinstall system symbols
|
||||||
AddSystemSymbols();
|
AddSystemSymbols();
|
||||||
|
@ -67,11 +72,17 @@ namespace arookas {
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
public sunCallableSymbol ResolveCallable(sunFunctionCall node) {
|
public sunCallableSymbol ResolveCallable(sunFunctionCall node) {
|
||||||
var symbol = SymbolTable.Callables.FirstOrDefault(i => i.Name == node.Function.Value);
|
var global = node.Function.Value;
|
||||||
if (symbol == null) {
|
var local = MangleSymbolName(global, false, true);
|
||||||
throw new sunUndefinedFunctionException(node);
|
var symbol = SymbolTable.Callables.FirstOrDefault(i => i.Name == local);
|
||||||
|
if (symbol != null) {
|
||||||
|
return symbol;
|
||||||
}
|
}
|
||||||
return symbol;
|
symbol = SymbolTable.Callables.FirstOrDefault(i => i.Name == global);
|
||||||
|
if (symbol != null) {
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
|
throw new sunUndefinedFunctionException(node);
|
||||||
}
|
}
|
||||||
public sunCallableSymbol MustResolveCallable(sunFunctionCall node) {
|
public sunCallableSymbol MustResolveCallable(sunFunctionCall node) {
|
||||||
var symbol = ResolveCallable(node);
|
var symbol = ResolveCallable(node);
|
||||||
|
@ -118,16 +129,26 @@ namespace arookas {
|
||||||
return Scopes.DeclareConstant(node.Value, expression);
|
return Scopes.DeclareConstant(node.Value, expression);
|
||||||
}
|
}
|
||||||
public sunStorableSymbol ResolveStorable(sunIdentifier node) {
|
public sunStorableSymbol ResolveStorable(sunIdentifier node) {
|
||||||
|
var global = node.Value;
|
||||||
|
var local = MangleSymbolName(global, false, true);
|
||||||
for (int i = Scopes.Count - 1; i >= 0; --i) {
|
for (int i = Scopes.Count - 1; i >= 0; --i) {
|
||||||
var symbol = Scopes[i].ResolveStorable(node.Value);
|
var symbol = Scopes[i].ResolveStorable(local);
|
||||||
|
if (symbol != null) {
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
|
symbol = Scopes[i].ResolveStorable(global);
|
||||||
if (symbol != null) {
|
if (symbol != null) {
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
public sunVariableSymbol ResolveVariable(sunIdentifier node) { return ResolveStorable(node) as sunVariableSymbol; }
|
public sunVariableSymbol ResolveVariable(sunIdentifier node) {
|
||||||
public sunConstantSymbol ResolveConstant(sunIdentifier node) { return ResolveStorable(node) as sunConstantSymbol; }
|
return ResolveStorable(node) as sunVariableSymbol;
|
||||||
|
}
|
||||||
|
public sunConstantSymbol ResolveConstant(sunIdentifier node) {
|
||||||
|
return ResolveStorable(node) as sunConstantSymbol;
|
||||||
|
}
|
||||||
public sunStorableSymbol MustResolveStorable(sunIdentifier node) {
|
public sunStorableSymbol MustResolveStorable(sunIdentifier node) {
|
||||||
var symbol = ResolveStorable(node);
|
var symbol = ResolveStorable(node);
|
||||||
if (symbol == null) {
|
if (symbol == null) {
|
||||||
|
@ -164,6 +185,16 @@ namespace arookas {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// locals
|
||||||
|
public void PushLocal() {
|
||||||
|
mLocalStack.Push(mLocal++);
|
||||||
|
}
|
||||||
|
public void PopLocal() {
|
||||||
|
if (mLocalStack.Count > 0) {
|
||||||
|
mLocalStack.Pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// system symbols
|
// system symbols
|
||||||
void AddSystemSymbols() {
|
void AddSystemSymbols() {
|
||||||
// add system builtins
|
// add system builtins
|
||||||
|
@ -176,7 +207,7 @@ namespace arookas {
|
||||||
Typeof = AddSystemBuiltin("typeof");
|
Typeof = AddSystemBuiltin("typeof");
|
||||||
|
|
||||||
// add system variables
|
// add system variables
|
||||||
Switch = AddSystemVariable("$switch"); // storage for switch statements
|
Switch = AddSystemVariable("switch"); // storage for switch statements
|
||||||
}
|
}
|
||||||
sunCallableSymbol AddSystemBuiltin(string name) {
|
sunCallableSymbol AddSystemBuiltin(string name) {
|
||||||
var symbol = new sunBuiltinSymbol(name, SymbolTable.Count);
|
var symbol = new sunBuiltinSymbol(name, SymbolTable.Count);
|
||||||
|
@ -184,9 +215,25 @@ namespace arookas {
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
sunStorableSymbol AddSystemVariable(string name) {
|
sunStorableSymbol AddSystemVariable(string name) {
|
||||||
var symbol = Scopes.DeclareVariable(name);
|
var symbol = Scopes.DeclareVariable(MangleSymbolName(name, true, false));
|
||||||
SymbolTable.Add(symbol);
|
SymbolTable.Add(symbol);
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// static util
|
||||||
|
string MangleSymbolName(string basename, bool system, bool local) {
|
||||||
|
var prefix = "";
|
||||||
|
var suffix = "";
|
||||||
|
if (system) {
|
||||||
|
prefix = "$";
|
||||||
|
}
|
||||||
|
if (local) {
|
||||||
|
suffix = String.Format("@{0}", mLocal);
|
||||||
|
}
|
||||||
|
if (prefix == "" && suffix == "") {
|
||||||
|
return basename;
|
||||||
|
}
|
||||||
|
return String.Concat(prefix, basename, suffix);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue