Artifact Content
Not logged in

Artifact ac6802f569d2081c8702ef49f001bc8c6a1c47bc


/**
 * Authors: k.inaba
 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
 *
 * Runtime data structures for Polemy programming language.
 */
module polemy.value;
import polemy._common;
import polemy.lex : LexPosition;
import std.stdio;

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

abstract class Value
{
}

class UndefinedValue : Value
{
	mixin SimpleConstructor;
	mixin SimpleCompare;
	override string toString() const { return "(undefined)"; }
}

class IntValue : Value
{
	BigInt data;
	mixin SimpleConstructor;
	mixin SimpleCompare;
	override string toString() const {
		return std.bigint.toDecimalString(cast(BigInt)data);
	}
}

class StrValue : Value
{
	string data;
	mixin SimpleConstructor;
	mixin SimpleCompare;
	override string toString() const { return data; }
}

class FunValue : Value
{
	Value delegate(immutable LexPosition pos, Value[]) data;
	mixin SimpleConstructor;
	Value call(immutable LexPosition pos, Value[] args) { return data(pos,args); }
	override string toString() const { return sprintf!"(function:%s:%s)"(data.ptr,data.funcptr); }
}
import std.stdio;
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;
	}
}