4198578702 2010-11-07 kinaba: /** 4198578702 2010-11-07 kinaba: * Authors: k.inaba 4198578702 2010-11-07 kinaba: * License: NYSL 0.9982 http://www.kmonos.net/nysl/ 4198578702 2010-11-07 kinaba: * 4198578702 2010-11-07 kinaba: * Runtime data structures for Polemy programming language. 423f308350 2010-11-07 kinaba: */ 4198578702 2010-11-07 kinaba: module polemy.runtime; 4198578702 2010-11-07 kinaba: import polemy._common; 0569f7b8c2 2010-11-07 kinaba: import polemy.lex : LexPosition; 0569f7b8c2 2010-11-07 kinaba: import std.stdio; 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: class PolemyRuntimeException : Exception 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: this(string msg) { super(msg); } 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: abstract class Value 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 0569f7b8c2 2010-11-07 kinaba: class UndefinedValue : Value 0569f7b8c2 2010-11-07 kinaba: { 0569f7b8c2 2010-11-07 kinaba: mixin SimpleConstructor; 0569f7b8c2 2010-11-07 kinaba: mixin SimpleCompare; 820e7198cc 2010-11-07 kinaba: override string toString() const { return "(undefined)"; } 0569f7b8c2 2010-11-07 kinaba: } 0569f7b8c2 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: class IntValue : Value 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: BigInt data; 423f308350 2010-11-07 kinaba: mixin SimpleConstructor; 423f308350 2010-11-07 kinaba: mixin SimpleCompare; 820e7198cc 2010-11-07 kinaba: override string toString() const { 820e7198cc 2010-11-07 kinaba: const(char)[] cs; data.toString((const(char)[] s){cs=s;}, null); 820e7198cc 2010-11-07 kinaba: return to!string(cs); 820e7198cc 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: class StrValue : Value 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: string data; 423f308350 2010-11-07 kinaba: mixin SimpleConstructor; 423f308350 2010-11-07 kinaba: mixin SimpleCompare; 820e7198cc 2010-11-07 kinaba: override string toString() const { return data; } 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 0569f7b8c2 2010-11-07 kinaba: class FunValue : Value 5d4cb856d8 2010-11-07 kinaba: { 0569f7b8c2 2010-11-07 kinaba: Value delegate(immutable LexPosition pos, Value[]) data; 5d4cb856d8 2010-11-07 kinaba: mixin SimpleConstructor; 0569f7b8c2 2010-11-07 kinaba: Value call(immutable LexPosition pos, Value[] args) { return data(pos,args); } 820e7198cc 2010-11-07 kinaba: override string toString() const { return sprintf!"(function:%s:%s)"(data.ptr,data.funcptr); } 5d4cb856d8 2010-11-07 kinaba: } 0569f7b8c2 2010-11-07 kinaba: import std.stdio; 423f308350 2010-11-07 kinaba: class Context 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: Context parent; 423f308350 2010-11-07 kinaba: Value[string] table; 423f308350 2010-11-07 kinaba: this(Context parent = null) { this.parent = parent; } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: void add(string i, Value v) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: table[i] = v; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: Value opIndex(string i) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( i in table ) 423f308350 2010-11-07 kinaba: return table[i]; 423f308350 2010-11-07 kinaba: if( parent is null ) 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"variable %s not found"(i)); 423f308350 2010-11-07 kinaba: return parent[i]; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: 423f308350 2010-11-07 kinaba: void opIndexAssign(Value v, string i) 423f308350 2010-11-07 kinaba: { 423f308350 2010-11-07 kinaba: if( i in table ) 423f308350 2010-11-07 kinaba: return table[i] = v; 423f308350 2010-11-07 kinaba: if( parent is null ) 423f308350 2010-11-07 kinaba: throw new PolemyRuntimeException(sprintf!"variable %s not found"(i)); 423f308350 2010-11-07 kinaba: return parent[i] = v; 423f308350 2010-11-07 kinaba: } 423f308350 2010-11-07 kinaba: }