Artifact Content
Not logged in

Artifact eb7f695558db6c0f6c75cbbd0256a325eb977788


     1  /**
     2   * Authors: k.inaba
     3   * License: NYSL 0.9982 http://www.kmonos.net/nysl/
     4   *
     5   * Runtime data structures for Polemy programming language.
     6   */
     7  module polemy.runtime;
     8  import polemy._common;
     9  import polemy.lex : LexPosition;
    10  import std.stdio;
    11  
    12  class PolemyRuntimeException : Exception
    13  {
    14  	this(string msg) { super(msg); }
    15  }
    16  
    17  abstract class Value
    18  {
    19  }
    20  
    21  class UndefinedValue : Value
    22  {
    23  	mixin SimpleConstructor;
    24  	mixin SimpleCompare;
    25  }
    26  
    27  class IntValue : Value
    28  {
    29  	BigInt data;
    30  	mixin SimpleConstructor;
    31  	mixin SimpleCompare;
    32  }
    33  
    34  class StrValue : Value
    35  {
    36  	string data;
    37  	mixin SimpleConstructor;
    38  	mixin SimpleCompare;
    39  }
    40  
    41  class FunValue : Value
    42  {
    43  	Value delegate(immutable LexPosition pos, Value[]) data;
    44  	mixin SimpleConstructor;
    45  	Value call(immutable LexPosition pos, Value[] args) { return data(pos,args); }
    46  }
    47  import std.stdio;
    48  class Context
    49  {
    50  	Context       parent;
    51  	Value[string] table;
    52  	this(Context parent = null) { this.parent = parent; }
    53  
    54  	void add(string i, Value v)
    55  	{
    56  		table[i] = v;
    57  	}
    58  	
    59  	Value opIndex(string i)
    60  	{
    61  		if( i in table )
    62  			return table[i];
    63  		if( parent is null )
    64  			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
    65  		return parent[i];
    66  	}
    67  
    68  	void opIndexAssign(Value v, string i)
    69  	{
    70  		if( i in table )
    71  			return table[i] = v;
    72  		if( parent is null )
    73  			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
    74  		return parent[i] = v;
    75  	}
    76  }