Renamed symbol table classes.

This commit is contained in:
arookas 2015-12-21 04:49:22 -05:00
parent 5accab46db
commit 9ace3833cb
8 changed files with 58 additions and 63 deletions

View file

@ -175,7 +175,7 @@ namespace arookas
} }
public abstract void Compile(sunContext context, sunVariableInfo variable); public abstract void Compile(sunContext context, sunVariableSymbol variable);
} }
class sunIncrement : sunAugment class sunIncrement : sunAugment
@ -186,7 +186,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variable) public override void Compile(sunContext context, sunVariableSymbol variable)
{ {
context.Text.IncVariable(variable); context.Text.IncVariable(variable);
} }
@ -200,7 +200,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variable) public override void Compile(sunContext context, sunVariableSymbol variable)
{ {
context.Text.DecVariable(variable); context.Text.DecVariable(variable);
} }

View file

@ -291,7 +291,7 @@ namespace arookas
} }
public virtual void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public virtual void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
expression.Compile(context); expression.Compile(context);
context.Text.StoreVariable(variableInfo); context.Text.StoreVariable(variableInfo);
@ -306,7 +306,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -323,7 +323,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -340,7 +340,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -357,7 +357,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -374,7 +374,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -391,7 +391,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -408,7 +408,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -425,7 +425,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);
@ -442,7 +442,7 @@ namespace arookas
} }
public override void Compile(sunContext context, sunVariableInfo variableInfo, sunExpression expression) public override void Compile(sunContext context, sunVariableSymbol variableInfo, sunExpression expression)
{ {
context.Text.PushVariable(variableInfo); context.Text.PushVariable(variableInfo);
expression.Compile(context); expression.Compile(context);

View file

@ -12,7 +12,7 @@
public override void Compile(sunContext context) public override void Compile(sunContext context)
{ {
sunVariableInfo variableInfo; sunVariableSymbol variableInfo;
sunConstInfo constInfo; sunConstInfo constInfo;
context.ResolveVariableOrConstant(Variable, out variableInfo, out constInfo); context.ResolveVariableOrConstant(Variable, out variableInfo, out constInfo);
if (variableInfo != null) if (variableInfo != null)

View file

@ -105,34 +105,34 @@ namespace arookas
} }
// builtins // builtins
public sunBuiltinInfo DeclareBuiltin(sunBuiltinDeclaration node) public sunBuiltinSymbol DeclareBuiltin(sunBuiltinDeclaration node)
{ {
var symbolInfo = SymbolTable.Callables.FirstOrDefault(f => f.Name == node.Builtin.Value); var symbolInfo = SymbolTable.Callables.FirstOrDefault(f => f.Name == node.Builtin.Value);
if (symbolInfo != null) if (symbolInfo != null)
{ {
throw new sunRedeclaredBuiltinException(node); throw new sunRedeclaredBuiltinException(node);
} }
var builtinInfo = new sunBuiltinInfo(node.Builtin.Value, node.Parameters.ParameterInfo, SymbolTable.Count); var builtinInfo = new sunBuiltinSymbol(node.Builtin.Value, node.Parameters.ParameterInfo, SymbolTable.Count);
SymbolTable.Add(builtinInfo); SymbolTable.Add(builtinInfo);
return builtinInfo; return builtinInfo;
} }
public sunBuiltinInfo DeclareSystemBuiltin(string name, bool variadic, params string[] parameters) public sunBuiltinSymbol DeclareSystemBuiltin(string name, bool variadic, params string[] parameters)
{ {
var builtinInfo = SymbolTable.Builtins.FirstOrDefault(f => f.Name == name); var builtinInfo = SymbolTable.Builtins.FirstOrDefault(f => f.Name == name);
if (builtinInfo == null) if (builtinInfo == null)
{ {
builtinInfo = new sunBuiltinInfo(name, new sunParameterInfo(parameters, variadic), SymbolTable.Count); builtinInfo = new sunBuiltinSymbol(name, new sunParameterInfo(parameters, variadic), SymbolTable.Count);
SymbolTable.Add(builtinInfo); SymbolTable.Add(builtinInfo);
} }
return builtinInfo; return builtinInfo;
} }
public sunBuiltinInfo ResolveSystemBuiltin(string name) public sunBuiltinSymbol ResolveSystemBuiltin(string name)
{ {
return SymbolTable.Builtins.FirstOrDefault(f => f.Name == name); return SymbolTable.Builtins.FirstOrDefault(f => f.Name == name);
} }
// functions // functions
public sunFunctionInfo DefineFunction(sunFunctionDefinition node) public sunFunctionSymbol DefineFunction(sunFunctionDefinition node)
{ {
if (node.Parameters.IsVariadic) if (node.Parameters.IsVariadic)
{ {
@ -143,11 +143,11 @@ namespace arookas
{ {
throw new sunRedefinedFunctionException(node); throw new sunRedefinedFunctionException(node);
} }
var functionInfo = new sunFunctionInfo(node.Function.Value, node.Parameters.ParameterInfo, node.Body); var functionInfo = new sunFunctionSymbol(node.Function.Value, node.Parameters.ParameterInfo, node.Body);
SymbolTable.Add(functionInfo); SymbolTable.Add(functionInfo);
return functionInfo; return functionInfo;
} }
public sunCallableSymbolInfo ResolveCallable(sunFunctionCall node) public sunCallableSymbol ResolveCallable(sunFunctionCall node)
{ {
var symbolInfo = SymbolTable.Callables.FirstOrDefault(f => f.Name == node.Function.Value); var symbolInfo = SymbolTable.Callables.FirstOrDefault(f => f.Name == node.Function.Value);
if (symbolInfo == null) if (symbolInfo == null)
@ -158,7 +158,7 @@ namespace arookas
} }
// variables // variables
public sunVariableInfo DeclareVariable(sunIdentifier node) public sunVariableSymbol DeclareVariable(sunIdentifier node)
{ {
// assert variable is not already declared in current scope // assert variable is not already declared in current scope
if (Scopes.Top.GetIsVariableDeclared(node.Value)) if (Scopes.Top.GetIsVariableDeclared(node.Value))
@ -166,14 +166,9 @@ namespace arookas
throw new sunRedeclaredVariableException(node); throw new sunRedeclaredVariableException(node);
} }
var variableInfo = Scopes.DeclareVariable(node.Value); var variableInfo = Scopes.DeclareVariable(node.Value);
if (Scopes.Top.Type == sunScopeType.Script)
{
// script variables are added to the symbol table
SymbolTable.Add(variableInfo);
}
return variableInfo; return variableInfo;
} }
public sunVariableInfo ResolveVariable(sunIdentifier node) public sunVariableSymbol ResolveVariable(sunIdentifier node)
{ {
// walk the stack backwards to resolve to the variable's latest declaration // walk the stack backwards to resolve to the variable's latest declaration
for (int i = Scopes.Count - 1; i >= 0; --i) for (int i = Scopes.Count - 1; i >= 0; --i)
@ -187,7 +182,7 @@ namespace arookas
throw new sunUndeclaredVariableException(node); throw new sunUndeclaredVariableException(node);
} }
public sunVariableInfo DeclareParameter(string name) { return Scopes.DeclareVariable(name); } public sunVariableSymbol DeclareParameter(string name) { return Scopes.DeclareVariable(name); }
// constants // constants
public sunConstInfo DeclareConstant(sunIdentifier node, sunExpression expression) public sunConstInfo DeclareConstant(sunIdentifier node, sunExpression expression)
@ -201,7 +196,7 @@ namespace arookas
} }
public sunConstInfo ResolveConstant(sunIdentifier node) public sunConstInfo ResolveConstant(sunIdentifier node)
{ {
// walk the stack backwards to resolve to the variable's latest declaration // walk the stack backwards to resolve to the constant's latest declaration
for (int i = Scopes.Count - 1; i >= 0; --i) for (int i = Scopes.Count - 1; i >= 0; --i)
{ {
var constInfo = Scopes[i].ResolveConstant(node.Value); var constInfo = Scopes[i].ResolveConstant(node.Value);
@ -213,7 +208,7 @@ namespace arookas
throw new sunUndeclaredVariableException(node); throw new sunUndeclaredVariableException(node);
} }
public void ResolveVariableOrConstant(sunIdentifier node, out sunVariableInfo variableInfo, out sunConstInfo constInfo) public void ResolveVariableOrConstant(sunIdentifier node, out sunVariableSymbol variableInfo, out sunConstInfo constInfo)
{ {
try try
{ {

View file

@ -201,7 +201,7 @@ namespace arookas
} }
class sunArgumentCountException : sunNodeException<sunFunctionCall> class sunArgumentCountException : sunNodeException<sunFunctionCall>
{ {
public sunCallableSymbolInfo CalledSymbol { get; private set; } public sunCallableSymbol CalledSymbol { get; private set; }
public int ArgumentMinimum { get { return CalledSymbol.Parameters.Minimum; } } public int ArgumentMinimum { get { return CalledSymbol.Parameters.Minimum; } }
public int ArgumentCount { get { return Node.Arguments.Count; } } public int ArgumentCount { get { return Node.Arguments.Count; } }
@ -227,7 +227,7 @@ namespace arookas
} }
} }
public sunArgumentCountException(sunFunctionCall node, sunCallableSymbolInfo calledSymbol) public sunArgumentCountException(sunFunctionCall node, sunCallableSymbol calledSymbol)
: base(node) : base(node)
{ {
if (calledSymbol == null) if (calledSymbol == null)

View file

@ -39,7 +39,7 @@ namespace arookas
Push(sunScopeType.Script); // add global scope Push(sunScopeType.Script); // add global scope
} }
public sunVariableInfo DeclareVariable(string name) public sunVariableSymbol DeclareVariable(string name)
{ {
switch (Top.Type) switch (Top.Type)
{ {
@ -48,7 +48,7 @@ namespace arookas
} }
return null; return null;
} }
sunVariableInfo DeclareGlobal(string name) sunVariableSymbol DeclareGlobal(string name)
{ {
var variableInfo = Top.DeclareVariable(name, 0, GlobalCount); var variableInfo = Top.DeclareVariable(name, 0, GlobalCount);
if (variableInfo == null) if (variableInfo == null)
@ -57,7 +57,7 @@ namespace arookas
} }
return variableInfo; return variableInfo;
} }
sunVariableInfo DeclareLocal(string name) sunVariableSymbol DeclareLocal(string name)
{ {
var variableInfo = Top.DeclareVariable(name, 1, LocalCount); var variableInfo = Top.DeclareVariable(name, 1, LocalCount);
if (variableInfo == null) if (variableInfo == null)
@ -73,7 +73,7 @@ namespace arookas
class sunScope class sunScope
{ {
List<sunVariableInfo> variables = new List<sunVariableInfo>(10); List<sunVariableSymbol> variables = new List<sunVariableSymbol>(10);
List<sunConstInfo> constants = new List<sunConstInfo>(10); List<sunConstInfo> constants = new List<sunConstInfo>(10);
public sunScopeType Type { get; private set; } public sunScopeType Type { get; private set; }
@ -86,17 +86,17 @@ namespace arookas
public int ConstantCount { get { return constants.Count; } } public int ConstantCount { get { return constants.Count; } }
public bool GetIsVariableDeclared(string name) { return variables.Any(v => v.Name == name); } public bool GetIsVariableDeclared(string name) { return variables.Any(v => v.Name == name); }
public sunVariableInfo DeclareVariable(string name, int display, int index) public sunVariableSymbol DeclareVariable(string name, int display, int index)
{ {
if (GetIsVariableDeclared(name) || GetIsConstantDeclared(name)) if (GetIsVariableDeclared(name) || GetIsConstantDeclared(name))
{ {
return null; return null;
} }
var variableInfo = new sunVariableInfo(name, display, index); var variableInfo = new sunVariableSymbol(name, display, index);
variables.Add(variableInfo); variables.Add(variableInfo);
return variableInfo; return variableInfo;
} }
public sunVariableInfo ResolveVariable(string name) { return variables.FirstOrDefault(v => v.Name == name); } public sunVariableSymbol ResolveVariable(string name) { return variables.FirstOrDefault(v => v.Name == name); }
public bool GetIsConstantDeclared(string name) { return constants.Any(c => c.Name == name); } public bool GetIsConstantDeclared(string name) { return constants.Any(c => c.Name == name); }
public sunConstInfo DeclareConstant(string name, sunExpression expression) public sunConstInfo DeclareConstant(string name, sunExpression expression)

View file

@ -7,21 +7,21 @@ using System.Linq;
namespace arookas namespace arookas
{ {
class sunSymbolTable : IEnumerable<sunSymbolInfo> class sunSymbolTable : IEnumerable<sunSymbol>
{ {
List<sunSymbolInfo> symbols = new List<sunSymbolInfo>(10); List<sunSymbol> symbols = new List<sunSymbol>(10);
public int Count { get { return symbols.Count; } } public int Count { get { return symbols.Count; } }
public int BuiltinCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Builtin); } } public int BuiltinCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Builtin); } }
public int FunctionCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Function); } } public int FunctionCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Function); } }
public int VariableCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Variable); } } public int VariableCount { get { return symbols.Count(sym => sym.Type == sunSymbolType.Variable); } }
public IEnumerable<sunCallableSymbolInfo> Callables { get { return symbols.OfType<sunCallableSymbolInfo>(); } } public IEnumerable<sunCallableSymbol> Callables { get { return symbols.OfType<sunCallableSymbol>(); } }
public IEnumerable<sunBuiltinInfo> Builtins { get { return symbols.Where(sym => sym.Type == sunSymbolType.Builtin).Cast<sunBuiltinInfo>(); } } public IEnumerable<sunBuiltinSymbol> Builtins { get { return symbols.Where(sym => sym.Type == sunSymbolType.Builtin).Cast<sunBuiltinSymbol>(); } }
public IEnumerable<sunFunctionInfo> Functions { get { return symbols.Where(sym => sym.Type == sunSymbolType.Function).Cast<sunFunctionInfo>(); } } public IEnumerable<sunFunctionSymbol> Functions { get { return symbols.Where(sym => sym.Type == sunSymbolType.Function).Cast<sunFunctionSymbol>(); } }
public IEnumerable<sunVariableInfo> Variables { get { return symbols.Where(sym => sym.Type == sunSymbolType.Variable).Cast<sunVariableInfo>(); } } public IEnumerable<sunVariableSymbol> Variables { get { return symbols.Where(sym => sym.Type == sunSymbolType.Variable).Cast<sunVariableSymbol>(); } }
public void Add(sunSymbolInfo symbol) { symbols.Add(symbol); } public void Add(sunSymbol symbol) { symbols.Add(symbol); }
public void Clear() { symbols.Clear(); } public void Clear() { symbols.Clear(); }
public void Write(aBinaryWriter writer) public void Write(aBinaryWriter writer)
@ -45,11 +45,11 @@ namespace arookas
} }
} }
public IEnumerator<sunSymbolInfo> GetEnumerator() { return symbols.GetEnumerator(); } public IEnumerator<sunSymbol> GetEnumerator() { return symbols.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
} }
abstract class sunSymbolInfo abstract class sunSymbol
{ {
public string Name { get; private set; } public string Name { get; private set; }
@ -57,20 +57,20 @@ namespace arookas
public abstract sunSymbolType Type { get; } public abstract sunSymbolType Type { get; }
public abstract uint Data { get; } public abstract uint Data { get; }
protected sunSymbolInfo(string name) protected sunSymbol(string name)
{ {
Name = name; Name = name;
} }
} }
abstract class sunCallableSymbolInfo : sunSymbolInfo abstract class sunCallableSymbol : sunSymbol
{ {
public sunParameterInfo Parameters { get; private set; } public sunParameterInfo Parameters { get; private set; }
protected List<sunPoint> CallSites { get; private set; } protected List<sunPoint> CallSites { get; private set; }
public bool HasCallSites { get { return CallSites.Count > 0; } } public bool HasCallSites { get { return CallSites.Count > 0; } }
protected sunCallableSymbolInfo(string name, sunParameterInfo parameterInfo) protected sunCallableSymbol(string name, sunParameterInfo parameterInfo)
: base(name) : base(name)
{ {
Parameters = parameterInfo; Parameters = parameterInfo;
@ -83,7 +83,7 @@ namespace arookas
public abstract void Compile(sunContext context); public abstract void Compile(sunContext context);
} }
class sunBuiltinInfo : sunCallableSymbolInfo class sunBuiltinSymbol : sunCallableSymbol
{ {
public int Index { get; private set; } public int Index { get; private set; }
@ -91,7 +91,7 @@ namespace arookas
public override sunSymbolType Type { get { return sunSymbolType.Builtin; } } public override sunSymbolType Type { get { return sunSymbolType.Builtin; } }
public override uint Data { get { return (uint)Index; } } public override uint Data { get { return (uint)Index; } }
public sunBuiltinInfo(string name, sunParameterInfo parameters, int index) public sunBuiltinSymbol(string name, sunParameterInfo parameters, int index)
: base(name, parameters) : base(name, parameters)
{ {
Index = index; Index = index;
@ -111,7 +111,7 @@ namespace arookas
} }
} }
class sunFunctionInfo : sunCallableSymbolInfo class sunFunctionSymbol : sunCallableSymbol
{ {
sunNode Body { get; set; } sunNode Body { get; set; }
public uint Offset { get; private set; } public uint Offset { get; private set; }
@ -120,7 +120,7 @@ namespace arookas
public override sunSymbolType Type { get { return sunSymbolType.Function; } } public override sunSymbolType Type { get { return sunSymbolType.Function; } }
public override uint Data { get { return (uint)Offset; } } public override uint Data { get { return (uint)Offset; } }
public sunFunctionInfo(string name, sunParameterInfo parameters, sunNode body) public sunFunctionSymbol(string name, sunParameterInfo parameters, sunNode body)
: base(name, parameters) : base(name, parameters)
{ {
Body = body; Body = body;
@ -213,7 +213,7 @@ namespace arookas
IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
} }
class sunVariableInfo : sunSymbolInfo class sunVariableSymbol : sunSymbol
{ {
public int Display { get; private set; } public int Display { get; private set; }
public int Index { get; private set; } public int Index { get; private set; }
@ -222,7 +222,7 @@ namespace arookas
public override sunSymbolType Type { get { return sunSymbolType.Variable; } } public override sunSymbolType Type { get { return sunSymbolType.Variable; } }
public override uint Data { get { return (uint)Index; } } public override uint Data { get { return (uint)Index; } }
public sunVariableInfo(string name, int display, int index) public sunVariableSymbol(string name, int display, int index)
: base(name) : base(name)
{ {
Display = display; Display = display;

View file

@ -51,7 +51,7 @@ namespace arookas
writer.Write8(0x03); writer.Write8(0x03);
writer.WriteS32(value); writer.WriteS32(value);
} }
public void PushVariable(sunVariableInfo variableInfo) public void PushVariable(sunVariableSymbol variableInfo)
{ {
PushVariable(variableInfo.Display, variableInfo.Index); PushVariable(variableInfo.Display, variableInfo.Index);
} }
@ -67,11 +67,11 @@ namespace arookas
writer.Write8(0x05); writer.Write8(0x05);
} }
public void IncVariable(sunVariableInfo variableInfo) public void IncVariable(sunVariableSymbol variableInfo)
{ {
IncVariable(variableInfo.Display, variableInfo.Index); IncVariable(variableInfo.Display, variableInfo.Index);
} }
public void DecVariable(sunVariableInfo variableInfo) public void DecVariable(sunVariableSymbol variableInfo)
{ {
DecVariable(variableInfo.Display, variableInfo.Index); DecVariable(variableInfo.Display, variableInfo.Index);
} }
@ -94,7 +94,7 @@ namespace arookas
public void Div() { writer.Write8(0x0B); } public void Div() { writer.Write8(0x0B); }
public void Mod() { writer.Write8(0x0C); } public void Mod() { writer.Write8(0x0C); }
public void StoreVariable(sunVariableInfo variableInfo) public void StoreVariable(sunVariableSymbol variableInfo)
{ {
StoreVariable(variableInfo.Display, variableInfo.Index); StoreVariable(variableInfo.Display, variableInfo.Index);
} }