Made system builtins properties instead of an enum.

This commit is contained in:
arookas 2015-12-31 05:49:29 -05:00
parent 4365a6b894
commit 31adcc5d6c
3 changed files with 36 additions and 37 deletions

View file

@ -37,12 +37,12 @@ namespace arookas {
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
var callableInfo = context.MustResolveCallable(this); var symbol = context.MustResolveCallable(this);
if (!callableInfo.Parameters.ValidateArgumentCount(Arguments.Count)) { if (!symbol.Parameters.ValidateArgumentCount(Arguments.Count)) {
throw new sunArgumentCountException(this, callableInfo); throw new sunArgumentCountException(this, symbol);
} }
Arguments.Compile(context); Arguments.Compile(context);
callableInfo.OpenCallSite(context, Arguments.Count); symbol.OpenCallSite(context, Arguments.Count);
if (IsStatement) { if (IsStatement) {
context.Text.WritePOP(); context.Text.WritePOP();
} }
@ -60,7 +60,7 @@ namespace arookas {
public sunParameterList(sunSourceLocation location) public sunParameterList(sunSourceLocation location)
: base(location) { : base(location) {
int count = this.Count(i => i is sunEllipsis); var count = this.Count(i => i is sunEllipsis);
if (count > 1 || (count > 0 && !(this[Count - 1] is sunEllipsis))) { if (count > 1 || (count > 0 && !(this[Count - 1] is sunEllipsis))) {
throw new sunVariadicParameterListException(this); throw new sunVariadicParameterListException(this);
} }

View file

@ -4,7 +4,7 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
context.Text.WriteFUNC((int)sunSystemBuiltins.Yield, 0); context.Yield.OpenCallSite(context, 0);
context.Text.WritePOP(); context.Text.WritePOP();
} }
} }
@ -14,7 +14,7 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
context.Text.WriteFUNC((int)sunSystemBuiltins.Exit, 0); context.Exit.OpenCallSite(context, 0);
context.Text.WritePOP(); context.Text.WritePOP();
} }
} }
@ -24,7 +24,7 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
context.Text.WriteFUNC((int)sunSystemBuiltins.Lock, 0); context.Lock.OpenCallSite(context, 0);
context.Text.WritePOP(); context.Text.WritePOP();
} }
} }
@ -34,7 +34,7 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
context.Text.WriteFUNC((int)sunSystemBuiltins.Unlock, 0); context.Unlock.OpenCallSite(context, 0);
context.Text.WritePOP(); context.Text.WritePOP();
} }
} }
@ -45,11 +45,6 @@
protected sunCast(sunSourceLocation location) protected sunCast(sunSourceLocation location)
: base(location) { } : base(location) { }
protected void Compile(sunContext context, int index) {
Argument.Compile(context);
context.Text.WriteFUNC(index, 1);
}
sunExpressionFlags sunTerm.GetExpressionFlags(sunContext context) { sunExpressionFlags sunTerm.GetExpressionFlags(sunContext context) {
return sunExpressionFlags.Casts | Argument.Analyze(context); return sunExpressionFlags.Casts | Argument.Analyze(context);
} }
@ -60,7 +55,8 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
Compile(context, (int)sunSystemBuiltins.Int); Argument.Compile(context);
context.Int.OpenCallSite(context, 1);
} }
} }
@ -69,7 +65,8 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
Compile(context, (int)sunSystemBuiltins.Float); Argument.Compile(context);
context.Float.OpenCallSite(context, 1);
} }
} }
@ -78,7 +75,8 @@
: base(location) { } : base(location) { }
public override void Compile(sunContext context) { public override void Compile(sunContext context) {
Compile(context, (int)sunSystemBuiltins.Typeof); Argument.Compile(context);
context.Typeof.OpenCallSite(context, 1);
} }
} }
} }

View file

@ -19,6 +19,15 @@ namespace arookas {
public sunLoopStack Loops { get; private set; } public sunLoopStack Loops { get; private set; }
public sunImportResolver ImportResolver { get; private set; } public sunImportResolver ImportResolver { get; private set; }
// system builtins
public sunCallableSymbol Yield { get; private set; }
public sunCallableSymbol Exit { get; private set; }
public sunCallableSymbol Lock { get; private set; }
public sunCallableSymbol Unlock { get; private set; }
public sunCallableSymbol Int { get; private set; }
public sunCallableSymbol Float { get; private set; }
public sunCallableSymbol Typeof { get; private set; }
public sunContext() { public sunContext() {
DataTable = new sunDataTable(); DataTable = new sunDataTable();
SymbolTable = new sunSymbolTable(); SymbolTable = new sunSymbolTable();
@ -56,14 +65,14 @@ namespace arookas {
mTextOffset = (uint)mWriter.Position; mTextOffset = (uint)mWriter.Position;
mWriter.PushAnchor(); // match code offsets and writer offsets mWriter.PushAnchor(); // match code offsets and writer offsets
// add system builtins (must be same order as constants) // add system builtins
AddSystemBuiltin("yield"); Yield = AddSystemBuiltin("yield");
AddSystemBuiltin("exit"); Exit = AddSystemBuiltin("exit");
AddSystemBuiltin("lock"); Lock = AddSystemBuiltin("lock");
AddSystemBuiltin("unlock"); Unlock = AddSystemBuiltin("unlock");
AddSystemBuiltin("int"); Int = AddSystemBuiltin("int");
AddSystemBuiltin("float"); Float = AddSystemBuiltin("float");
AddSystemBuiltin("typeof"); Typeof = AddSystemBuiltin("typeof");
} }
public void Close() { public void Close() {
@ -219,8 +228,10 @@ namespace arookas {
return null; return null;
} }
void AddSystemBuiltin(string name) { sunCallableSymbol AddSystemBuiltin(string name) {
SymbolTable.Add(new sunBuiltinSymbol(name, SymbolTable.Count)); var symbol = new sunBuiltinSymbol(name, SymbolTable.Count);
SymbolTable.Add(symbol);
return symbol;
} }
void WriteHeader() { void WriteHeader() {
mWriter.WriteString("SPCB"); mWriter.WriteString("SPCB");
@ -232,14 +243,4 @@ namespace arookas {
mWriter.WriteS32(SymbolTable.VariableCount); mWriter.WriteS32(SymbolTable.VariableCount);
} }
} }
enum sunSystemBuiltins {
Yield,
Exit,
Lock,
Unlock,
Int,
Float,
Typeof,
}
} }