Artifact Content
Not logged in

Artifact b0c768679677d7d54d5210dee1ce2c9cb4d5f746


module polemy.runtime;
import polemy._common;
/*
 * Author:  k.inaba
 * License: NYSL 0.9982 (http://www.kmonos.net/nysl/
 *   Runtime data structures for the polemy programming language
 */

class PolemyRuntimeException : Exception
{
	this(string msg) { super(msg); }
}

abstract class Value
{
}

class IntValue : Value
{
	BigInt data;
	mixin SimpleConstructor;
	mixin SimpleCompare;
}

class StrValue : Value
{
	string data;
	mixin SimpleConstructor;
	mixin SimpleCompare;
}

class Context
{
	Context       parent;
	Value[string] table;
	this(Context parent = null) { this.parent = parent; }

	void add(string i, Value v)
	{
		table[i] = v;
	}
	
	Value opIndex(string i)
	{
		if( i in table )
			return table[i];
		if( parent is null )
			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
		return parent[i];
	}

	void opIndexAssign(Value v, string i)
	{
		if( i in table )
			return table[i] = v;
		if( parent is null )
			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
		return parent[i] = v;
	}
}