1 /**
2 * Authors: k.inaba
3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
4 *
5 * Evaluator for Polemy programming language.
6 */
7 module polemy.eval;
8 import polemy._common;
9 import polemy.lex : LexPosition;
10 import polemy.ast;
11 import polemy.parse;
12 import polemy.value;
13 import std.typecons;
14 import std.stdio;
15
16 Table createGlobalContext()
17 {
18 auto ctx = new Table;
19 // [TODO] autogenerate these typechecks
20 ctx.set("+", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
21 if( args.length != 2 )
22 throw new RuntimeException(pos, "+ takes two arguments!!");
23 if( auto x = cast(IntValue)args[0] )
24 if( auto y = cast(IntValue)args[1] )
25 return new IntValue(x.data+y.data);
26 throw new RuntimeException(pos, "cannot add non-integers");
27 }));
28 ctx.set("-", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
29 if( args.length != 2 )
30 throw new RuntimeException(pos, "- takes two arguments!!");
31 if( auto x = cast(IntValue)args[0] )
32 if( auto y = cast(IntValue)args[1] )
33 return new IntValue(x.data-y.data);
34 throw new RuntimeException(pos, "cannot subtract non-integers");
35 }));
36 ctx.set("*", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
37 if( args.length != 2 )
38 throw new RuntimeException(pos, "* takes two arguments!!");
39 if( auto x = cast(IntValue)args[0] )
40 if( auto y = cast(IntValue)args[1] )
41 return new IntValue(x.data*y.data);
42 throw new RuntimeException(pos, "cannot multiply non-integers");
43 }));
44 ctx.set("/", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
45 if( args.length != 2 )
46 throw new RuntimeException(pos, "/ takes two arguments!!");
47 if( auto x = cast(IntValue)args[0] )
48 if( auto y = cast(IntValue)args[1] )
49 return new IntValue(x.data/y.data);
50 throw new RuntimeException(pos, "cannot divide non-integers");
51 }));
52 ctx.set("<", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
53 if( args.length != 2 )
54 throw new RuntimeException(pos, "< takes two arguments!!");
55 if( auto x = cast(IntValue)args[0] )
56 if( auto y = cast(IntValue)args[1] )
57 return new IntValue(BigInt(to!int(x.data < y.data)));
58 throw new RuntimeException(pos, "cannot compare non-integers");
59 }));
60 ctx.set(">", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
61 if( args.length != 2 )
62 throw new RuntimeException(pos, "> takes two arguments!!");
63 if( auto x = cast(IntValue)args[0] )
64 if( auto y = cast(IntValue)args[1] )
65 return new IntValue(BigInt(to!int(x.data>y.data)));
66 throw new RuntimeException(pos, "cannot compare non-integers");
67 }));
68 ctx.set("print", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
69 foreach(a; args)
70 write(a);
71 writeln("");
72 return new IntValue(BigInt(178));
73 }));
74 ctx.set("if", "@val", new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
75 if( args.length != 3 )
76 throw new RuntimeException(pos, "if takes three arguments!!");
77 if( auto x = cast(IntValue)args[0] )
78 if( auto ft = cast(FunValue)args[1] )
79 if( auto fe = cast(FunValue)args[2] )
80 return (x.data == 0 ? fe : ft).call(pos,[]);
81 throw new RuntimeException(pos, "type mismatch in if");
82 }));
83 return ctx;
84 }
85
86 /// Entry point of this module
87
88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn)
89 {
90 return eval( polemy.parse.parseString(str, fn_ln_cn) );
91 }
92
93 Tuple!(Value,"val",Table,"ctx") evalFile(S, T...)(S filenae, T ln_cn)
94 {
95 return eval( polemy.parse.parseFile(filename, ln_cn) );
96 }
97
98 Tuple!(Value,"val",Table,"ctx") eval(AST e)
99 {
100 Table ctx = createGlobalContext();
101 return typeof(return)(eval(e, ctx), ctx);
102 }
103
104 Value eval(AST _e, Table ctx, bool splitCtx = true)
105 {
106 if( auto e = cast(StrLiteral)_e )
107 {
108 return new StrValue(e.data);
109 }
110 else
111 if( auto e = cast(IntLiteral)_e )
112 {
113 return new IntValue(e.data);
114 }
115 else
116 if( auto e = cast(VarExpression)_e )
117 {
118 return ctx.get(e.var, "@val", e.pos);
119 }
120 else
121 if( auto e = cast(LetExpression)_e )
122 {
123 // for letrec, we need this, but should avoid overwriting????
124 // ctx.set(e.var, "@val", new UndefinedValue, e.pos);
125 Value v = eval(e.init, ctx, true);
126 ctx.set(e.var, "@val", v, e.pos);
127 return eval(e.expr, ctx);
128 }
129 else
130 if( auto e = cast(FuncallExpression)_e )
131 {
132 Value _f = eval(e.fun, ctx);
133 if( auto f = cast(FunValue)_f ) {
134 Value[] args;
135 foreach(a; e.args)
136 args ~= eval(a, ctx);
137 return f.call(e.pos, args);
138 } else
139 throw new RuntimeException(e.pos, "Non-funcion is applied");
140 }
141 else
142 if( auto e = cast(FunLiteral)_e )
143 {
144 return new FunValue(delegate Value(immutable LexPosition pos, Value[] args){
145 if( e.params.length != args.length )
146 throw new RuntimeException(e.pos, sprintf!"Argument Number Mismatch (%d required but %d given)"
147 (e.params.length, args.length));
148 Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet);
149 foreach(i,p; e.params)
150 ctxNeo.set(p, "@val", args[i]);
151 return eval(e.funbody, ctxNeo);
152 });
153 }
154 throw new RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression %s"(typeid(_e)));
155 }
156
157 unittest
158 {
159 auto r = assert_nothrow( evalString(`var x = 21; x + x*x;`) );
160 assert_eq( r.val, new IntValue(BigInt(21+21*21)) );
161 assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21)) );
162 assert_nothrow( r.ctx.get("x","@val") );
163 assert_throw!RuntimeException( r.ctx.get("y","@val") );
164 }
165 unittest
166 {
167 auto r = assert_nothrow( evalString(`var x = 21; var x = x + x*x;`) );
168 assert_eq( r.val, new IntValue(BigInt(21+21*21)) );
169 assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21+21*21)) );
170 assert_nothrow( r.ctx.get("x","@val") );
171 assert_throw!RuntimeException( r.ctx.get("y","@val") );
172 }
173 unittest
174 {
175 assert_nothrow( evalString(`print("Hello, world!");`) );
176 assert_nothrow( evalString(`print(fun(){});`) );
177 }
178 unittest
179 {
180 assert_nothrow( evalString(`var fac = fun(x){
181 1;
182 };
183 print(fac(3));`));
184 assert_nothrow( evalString(`var fac = fun(x){
185 if(x)
186 { x*fac(x-1); }
187 else
188 { 1; };
189 };
190 print(fac(10));`));
191 assert_nothrow( evalString(`var fib = fun(x){
192 if(x<2)
193 { 1; }
194 else
195 { fib(x-1) + fib(x-2); };
196 };
197 print(fib(10));`));
198 }