Artifact eb7f695558db6c0f6c75cbbd0256a325eb977788
- File
polemy/runtime.d
-
2010-11-07 14:34:29
- part of checkin
[0569f7b8c2]
on branch trunk
- - Added function literal evaluator (i.e., closure).
- Workaround for d2stacktrace's infinite-loop bug.
(when std.demangle.demangle use exception inside it,
it will go into an infinite loop. to avoid this,
I choose to unset TraceHandler during stacktrace generation.
This is far from the complete solution, but at least it should
work as expected under single-thread environment...)
(user:
kinaba)
[annotate]
/**
* Authors: k.inaba
* License: NYSL 0.9982 http://www.kmonos.net/nysl/
*
* Runtime data structures for Polemy programming language.
*/
module polemy.runtime;
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;
}
class IntValue : Value
{
BigInt data;
mixin SimpleConstructor;
mixin SimpleCompare;
}
class StrValue : Value
{
string data;
mixin SimpleConstructor;
mixin SimpleCompare;
}
class FunValue : Value
{
Value delegate(immutable LexPosition pos, Value[]) data;
mixin SimpleConstructor;
Value call(immutable LexPosition pos, Value[] args) { return data(pos,args); }
}
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;
}
}