Artifact Content
Not logged in

Artifact ac6802f569d2081c8702ef49f001bc8c6a1c47bc


     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.value;
     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  	override string toString() const { return "(undefined)"; }
    26  }
    27  
    28  class IntValue : Value
    29  {
    30  	BigInt data;
    31  	mixin SimpleConstructor;
    32  	mixin SimpleCompare;
    33  	override string toString() const {
    34  		return std.bigint.toDecimalString(cast(BigInt)data);
    35  	}
    36  }
    37  
    38  class StrValue : Value
    39  {
    40  	string data;
    41  	mixin SimpleConstructor;
    42  	mixin SimpleCompare;
    43  	override string toString() const { return data; }
    44  }
    45  
    46  class FunValue : Value
    47  {
    48  	Value delegate(immutable LexPosition pos, Value[]) data;
    49  	mixin SimpleConstructor;
    50  	Value call(immutable LexPosition pos, Value[] args) { return data(pos,args); }
    51  	override string toString() const { return sprintf!"(function:%s:%s)"(data.ptr,data.funcptr); }
    52  }
    53  import std.stdio;
    54  class Context
    55  {
    56  	Context       parent;
    57  	Value[string] table;
    58  	this(Context parent = null) { this.parent = parent; }
    59  
    60  	void add(string i, Value v)
    61  	{
    62  		table[i] = v;
    63  	}
    64  	
    65  	Value opIndex(string i)
    66  	{
    67  		if( i in table )
    68  			return table[i];
    69  		if( parent is null )
    70  			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
    71  		return parent[i];
    72  	}
    73  
    74  	void opIndexAssign(Value v, string i)
    75  	{
    76  		if( i in table )
    77  			return table[i] = v;
    78  		if( parent is null )
    79  			throw new PolemyRuntimeException(sprintf!"variable %s not found"(i));
    80  		return parent[i] = v;
    81  	}
    82  }