Changed how system builtins are initialized.

This commit is contained in:
arookas 2015-12-28 23:27:17 -05:00
parent ed4822ee29
commit 9f8f21c7bf
3 changed files with 35 additions and 34 deletions

View file

@ -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);
}
}
}

View file

@ -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,
}
}

View file

@ -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;