From 9f8f21c7bf63f30b87bc0bebb75347b469416c8b Mon Sep 17 00:00:00 2001 From: arookas Date: Mon, 28 Dec 2015 23:27:17 -0500 Subject: [PATCH] Changed how system builtins are initialized. --- ssc/ast/nodes.system.cs | 23 +++++++++------------- ssc/context.cs | 42 +++++++++++++++++++++-------------------- ssc/symbol table.cs | 4 ++++ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/ssc/ast/nodes.system.cs b/ssc/ast/nodes.system.cs index 39dca48..2d73887 100644 --- a/ssc/ast/nodes.system.cs +++ b/ssc/ast/nodes.system.cs @@ -4,8 +4,7 @@ : base(location) { } public override void Compile(sunContext context) { - var builtinInfo = context.ResolveSystemBuiltin("yield"); - context.Text.WriteFUNC(builtinInfo.Index, 0); + context.Text.WriteFUNC((int)sunSystemBuiltins.Yield, 0); context.Text.WritePOP(); } } @@ -15,9 +14,7 @@ : base(location) { } public override void Compile(sunContext context) { - var builtinInfo = context.ResolveSystemBuiltin("exit"); - context.Text.WriteFUNC(builtinInfo.Index, 0); - context.Text.WritePOP(); + context.Text.WriteFUNC((int)sunSystemBuiltins.Exit, 0); context.Text.WritePOP(); } } @@ -27,8 +24,7 @@ : base(location) { } public override void Compile(sunContext context) { - var builtinInfo = context.ResolveSystemBuiltin("lock"); - context.Text.WriteFUNC(builtinInfo.Index, 0); + context.Text.WriteFUNC((int)sunSystemBuiltins.Lock, 0); context.Text.WritePOP(); } } @@ -38,8 +34,7 @@ : base(location) { } public override void Compile(sunContext context) { - var builtinInfo = context.ResolveSystemBuiltin("unlock"); - context.Text.WriteFUNC(builtinInfo.Index, 0); + context.Text.WriteFUNC((int)sunSystemBuiltins.Unlock, 0); context.Text.WritePOP(); } } @@ -50,9 +45,9 @@ protected sunCast(sunSourceLocation location) : base(location) { } - protected void Compile(sunContext context, sunBuiltinSymbol symbol) { + protected void Compile(sunContext context, int index) { Argument.Compile(context); - context.Text.WriteFUNC(symbol.Index, 1); + context.Text.WriteFUNC(index, 1); } sunExpressionFlags sunTerm.GetExpressionFlags(sunContext context) { @@ -65,7 +60,7 @@ : base(location) { } public override void Compile(sunContext context) { - Compile(context, context.ResolveSystemBuiltin("int")); + Compile(context, (int)sunSystemBuiltins.Int); } } @@ -74,7 +69,7 @@ : base(location) { } public override void Compile(sunContext context) { - Compile(context, context.ResolveSystemBuiltin("float")); + Compile(context, (int)sunSystemBuiltins.Float); } } @@ -83,7 +78,7 @@ : base(location) { } public override void Compile(sunContext context) { - Compile(context, context.ResolveSystemBuiltin("typeof")); + Compile(context, (int)sunSystemBuiltins.Typeof); } } } diff --git a/ssc/context.cs b/ssc/context.cs index 58deb30..a3fbedf 100644 --- a/ssc/context.cs +++ b/ssc/context.cs @@ -55,15 +55,16 @@ namespace arookas { // begin text block mTextOffset = (uint)mWriter.Position; mWriter.PushAnchor(); // match code offsets and writer offsets + + // add system builtins (must be same order as constants) + AddSystemBuiltin("yield"); + AddSystemBuiltin("exit"); + AddSystemBuiltin("lock"); + AddSystemBuiltin("unlock"); + AddSystemBuiltin("int"); + AddSystemBuiltin("float"); + AddSystemBuiltin("typeof"); - // add system builtins - DeclareSystemBuiltin("yield", false); - DeclareSystemBuiltin("exit", false); - DeclareSystemBuiltin("lock", false); - DeclareSystemBuiltin("unlock", false); - DeclareSystemBuiltin("int", false, "x"); - DeclareSystemBuiltin("float", false, "x"); - DeclareSystemBuiltin("typeof", false, "x"); } public void Close() { if (!mOpen) { @@ -136,18 +137,6 @@ namespace arookas { return symbol; } - public sunBuiltinSymbol DeclareSystemBuiltin(string name, bool variadic, params string[] parameters) { - var symbol = SymbolTable.Builtins.FirstOrDefault(i => i.Name == name); - if (symbol == null) { - symbol = new sunBuiltinSymbol(name, new sunParameterInfo(parameters, variadic), SymbolTable.Count); - SymbolTable.Add(symbol); - } - return symbol; - } - public sunBuiltinSymbol ResolveSystemBuiltin(string name) { - return SymbolTable.Builtins.FirstOrDefault(i => i.Name == name); - } - // storables public sunVariableSymbol DeclareVariable(sunIdentifier node) { #if SSC_PACK_VARS @@ -230,6 +219,9 @@ namespace arookas { return null; } + void AddSystemBuiltin(string name) { + SymbolTable.Add(new sunBuiltinSymbol(name, SymbolTable.Count)); + } void WriteHeader() { mWriter.WriteString("SPCB"); mWriter.Write32(mTextOffset); @@ -240,4 +232,14 @@ namespace arookas { mWriter.WriteS32(SymbolTable.VariableCount); } } + + enum sunSystemBuiltins { + Yield, + Exit, + Lock, + Unlock, + Int, + Float, + Typeof, + } } diff --git a/ssc/symbol table.cs b/ssc/symbol table.cs index a41cb51..eb724ff 100644 --- a/ssc/symbol table.cs +++ b/ssc/symbol table.cs @@ -90,6 +90,10 @@ namespace arookas { public override sunSymbolType Type { get { return sunSymbolType.Builtin; } } public override uint Data { get { return (uint)Index; } } + public sunBuiltinSymbol(string name, int index) + : base(name, null) { + Index = index; + } public sunBuiltinSymbol(string name, sunParameterInfo parameters, int index) : base(name, parameters) { Index = index;