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