Differences From Artifact [6af5a0863b67aa85]:
- File
polemy/runtime.d
- 2010-11-23 10:37:54 - part of checkin [5e924caac9] on branch trunk - added AST-rewriting macro sample. (user: kinaba) [annotate]
To Artifact [641d49d7fd647519]:
- File
polemy/runtime.d
- 2010-11-23 13:55:15 - part of checkin [2134cd44cc] on branch trunk - further clean-up for polemy2d (user: kinaba) [annotate]
1 +
1 2 /**
2 3 * Authors: k.inaba
3 4 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
4 5 *
5 6 * Runtime library for Polemy programming language.
6 7 */
7 8 module polemy.runtime;
8 9 import polemy._common;
9 10 import polemy.layer;
11 +import polemy.failure;
10 12 import polemy.value;
11 13 import polemy.eval;
12 14 import std.stdio;
13 15
14 16 /// enroll the native implementations of primitive functions
15 17
16 18 void enrollRuntimeLibrary( Evaluator e )
17 19 {
18 - e.addPrimitive("+", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} );
19 - e.addPrimitive("-", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} );
20 - e.addPrimitive("*", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} );
21 - e.addPrimitive("/", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} );
22 - e.addPrimitive("%", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} );
23 - e.addPrimitive("~", ValueLayer, (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} );
24 - e.addPrimitive("||", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} );
25 - e.addPrimitive("&&", ValueLayer, (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} );
20 + // arithmetic operations
21 + e.addPrimitive("+", ValueLayer,
22 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} );
23 + e.addPrimitive("-", ValueLayer,
24 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} );
25 + e.addPrimitive("*", ValueLayer,
26 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} );
27 + e.addPrimitive("/", ValueLayer,
28 + (IntValue lhs, IntValue rhs){
29 + if( rhs.data == 0 )
30 + throw genex!RuntimeException("division by 0");
31 + return new IntValue(lhs.data / rhs.data);
32 + });
33 + e.addPrimitive("%", ValueLayer,
34 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} );
35 + e.addPrimitive("||", ValueLayer,
36 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 || rhs.data!=0);} );
37 + e.addPrimitive("&&", ValueLayer,
38 + (IntValue lhs, IntValue rhs){return new IntValue(lhs.data!=0 && rhs.data!=0);} );
39 + // string operation(s)
40 + e.addPrimitive("~", ValueLayer,
41 + (Value lhs, Value rhs){return new StrValue(lhs.toString ~ rhs.toString);} );
42 + // comparison
26 43 e.addPrimitive("<", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs < rhs);} );
27 44 e.addPrimitive(">", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs > rhs);} );
28 45 e.addPrimitive("<=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs <= rhs);} );
29 46 e.addPrimitive(">=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs >= rhs);} );
30 47 e.addPrimitive("==", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs == rhs);} );
31 48 e.addPrimitive("!=", ValueLayer, (Value lhs, Value rhs){return new IntValue(lhs != rhs);} );
32 - e.addPrimitive("print", ValueLayer, (Value a){
33 - writeln(a);
34 - return new IntValue(0);
35 - });
49 + // control flow
36 50 e.addPrimitive("if", ValueLayer, (IntValue x, FunValue ft, FunValue fe){
37 51 auto toRun = (x.data==0 ? fe : ft);
38 - // [TODO] fill positional information
39 52 return toRun.invoke(ValueLayer, toRun.definitionContext(), null);
40 53 });
41 - e.addPrimitive("_isint", ValueLayer, (Value v){return new IntValue(cast(IntValue)v !is null);} );
42 - e.addPrimitive("_isstr", ValueLayer, (Value v){return new IntValue(cast(StrValue)v !is null);} );
43 - e.addPrimitive("_isfun", ValueLayer, (Value v){return new IntValue(cast(FunValue)v !is null);} );
44 - e.addPrimitive("_isundefined", ValueLayer, (Value v){return new IntValue(cast(UndefinedValue)v !is null);} );
45 - e.addPrimitive("_istable", ValueLayer, (Value v){return new IntValue(cast(Table)v !is null);} );
54 + // type test
55 + e.addPrimitive("_isint", ValueLayer,
56 + (Value v){return new IntValue(cast(IntValue)v !is null);} );
57 + e.addPrimitive("_isstr", ValueLayer,
58 + (Value v){return new IntValue(cast(StrValue)v !is null);} );
59 + e.addPrimitive("_isfun", ValueLayer,
60 + (Value v){return new IntValue(cast(FunValue)v !is null);} );
61 + e.addPrimitive("_isundefined", ValueLayer,
62 + (Value v){return new IntValue(cast(UndefinedValue)v !is null);} );
63 + e.addPrimitive("_istable", ValueLayer,
64 + (Value v){return new IntValue(cast(Table)v !is null);} );
65 + // table
46 66 e.addPrimitive(".", ValueLayer, (Table t, StrValue s){
47 67 return (t.has(s.data, ValueLayer) ? t.get(s.data, ValueLayer) : new UndefinedValue);
48 68 });
49 69 e.addPrimitive(".?", ValueLayer, (Table t, StrValue s){
50 70 return new IntValue(t.has(s.data, ValueLayer));
51 71 });
52 72 e.addPrimitive(".=", ValueLayer, (Table t, StrValue s, Value v){
................................................................................
53 73 auto t2 = new Table(t, Table.Kind.NotPropagateSet);
54 74 t2.set(s.data, ValueLayer, v);
55 75 return t2;
56 76 });
57 77 e.addPrimitive("{}", ValueLayer, (){
58 78 return new Table;
59 79 });
80 + // IO
81 + e.addPrimitive("print", ValueLayer, (Value a){ writeln(a); return new IntValue(0); });
60 82 }