Diff
Not logged in

Differences From Artifact [62b3543f769fc9c9]:

To Artifact [050eab1743a45f8b]:


13 import std.typecons; 13 import std.typecons; 14 import std.stdio; 14 import std.stdio; 15 15 16 Table createGlobalContext() 16 Table createGlobalContext() 17 { 17 { 18 auto ctx = new Table; 18 auto ctx = new Table; 19 // [TODO] autogenerate these typechecks 19 // [TODO] autogenerate these typechecks 20 ctx.set("+", "@val", new FunValue(delegate Value(immutable LexPosition p | 20 ctx.set("+", "@v", new FunValue(delegate Value(immutable LexPosition pos 21 if( args.length != 2 ) 21 if( args.length != 2 ) 22 throw new RuntimeException(pos, "+ takes two arguments!! | 22 throw genex!RuntimeException(pos, "+ takes two arguments 23 if( auto x = cast(IntValue)args[0] ) 23 if( auto x = cast(IntValue)args[0] ) 24 if( auto y = cast(IntValue)args[1] ) 24 if( auto y = cast(IntValue)args[1] ) 25 return new IntValue(x.data+y.data); 25 return new IntValue(x.data+y.data); 26 throw new RuntimeException(pos, "cannot add non-integers"); | 26 throw genex!RuntimeException(pos, "cannot add non-integers"); 27 })); 27 })); 28 ctx.set("-", "@val", new FunValue(delegate Value(immutable LexPosition p | 28 ctx.set("-", "@v", new FunValue(delegate Value(immutable LexPosition pos 29 if( args.length != 2 ) 29 if( args.length != 2 ) 30 throw new RuntimeException(pos, "- takes two arguments!! | 30 throw genex!RuntimeException(pos, "- takes two arguments 31 if( auto x = cast(IntValue)args[0] ) 31 if( auto x = cast(IntValue)args[0] ) 32 if( auto y = cast(IntValue)args[1] ) 32 if( auto y = cast(IntValue)args[1] ) 33 return new IntValue(x.data-y.data); 33 return new IntValue(x.data-y.data); 34 throw new RuntimeException(pos, "cannot subtract non-integers"); | 34 throw genex!RuntimeException(pos, "cannot subtract non-integers" 35 })); 35 })); 36 ctx.set("*", "@val", new FunValue(delegate Value(immutable LexPosition p | 36 ctx.set("*", "@v", new FunValue(delegate Value(immutable LexPosition pos 37 if( args.length != 2 ) 37 if( args.length != 2 ) 38 throw new RuntimeException(pos, "* takes two arguments!! | 38 throw genex!RuntimeException(pos, "* takes two arguments 39 if( auto x = cast(IntValue)args[0] ) 39 if( auto x = cast(IntValue)args[0] ) 40 if( auto y = cast(IntValue)args[1] ) 40 if( auto y = cast(IntValue)args[1] ) 41 return new IntValue(x.data*y.data); 41 return new IntValue(x.data*y.data); 42 throw new RuntimeException(pos, "cannot multiply non-integers"); | 42 throw genex!RuntimeException(pos, "cannot multiply non-integers" 43 })); 43 })); 44 ctx.set("/", "@val", new FunValue(delegate Value(immutable LexPosition p | 44 ctx.set("/", "@v", new FunValue(delegate Value(immutable LexPosition pos 45 if( args.length != 2 ) 45 if( args.length != 2 ) 46 throw new RuntimeException(pos, "/ takes two arguments!! | 46 throw genex!RuntimeException(pos, "/ takes two arguments 47 if( auto x = cast(IntValue)args[0] ) 47 if( auto x = cast(IntValue)args[0] ) 48 if( auto y = cast(IntValue)args[1] ) 48 if( auto y = cast(IntValue)args[1] ) 49 return new IntValue(x.data/y.data); 49 return new IntValue(x.data/y.data); 50 throw new RuntimeException(pos, "cannot divide non-integers"); | 50 throw genex!RuntimeException(pos, "cannot divide non-integers"); 51 })); 51 })); 52 ctx.set("<", "@val", new FunValue(delegate Value(immutable LexPosition p | 52 ctx.set("<", "@v", new FunValue(delegate Value(immutable LexPosition pos 53 if( args.length != 2 ) 53 if( args.length != 2 ) 54 throw new RuntimeException(pos, "< takes two arguments!! | 54 throw genex!RuntimeException(pos, "< takes two arguments 55 if( auto x = cast(IntValue)args[0] ) 55 if( auto x = cast(IntValue)args[0] ) 56 if( auto y = cast(IntValue)args[1] ) 56 if( auto y = cast(IntValue)args[1] ) 57 return new IntValue(BigInt(to!int(x.data < y.dat 57 return new IntValue(BigInt(to!int(x.data < y.dat 58 throw new RuntimeException(pos, "cannot compare non-integers"); | 58 throw genex!RuntimeException(pos, "cannot compare non-integers") 59 })); 59 })); 60 ctx.set(">", "@val", new FunValue(delegate Value(immutable LexPosition p | 60 ctx.set(">", "@v", new FunValue(delegate Value(immutable LexPosition pos 61 if( args.length != 2 ) 61 if( args.length != 2 ) 62 throw new RuntimeException(pos, "> takes two arguments!! | 62 throw genex!RuntimeException(pos, "> takes two arguments 63 if( auto x = cast(IntValue)args[0] ) 63 if( auto x = cast(IntValue)args[0] ) 64 if( auto y = cast(IntValue)args[1] ) 64 if( auto y = cast(IntValue)args[1] ) 65 return new IntValue(BigInt(to!int(x.data>y.data) 65 return new IntValue(BigInt(to!int(x.data>y.data) 66 throw new RuntimeException(pos, "cannot compare non-integers"); | 66 throw genex!RuntimeException(pos, "cannot compare non-integers") 67 })); 67 })); 68 ctx.set("print", "@val", new FunValue(delegate Value(immutable LexPositi | 68 ctx.set("print", "@v", new FunValue(delegate Value(immutable LexPosition 69 foreach(a; args) 69 foreach(a; args) 70 write(a); 70 write(a); 71 writeln(""); 71 writeln(""); 72 return new IntValue(BigInt(178)); 72 return new IntValue(BigInt(178)); 73 })); 73 })); 74 ctx.set("if", "@val", new FunValue(delegate Value(immutable LexPosition | 74 ctx.set("if", "@v", new FunValue(delegate Value(immutable LexPosition po 75 if( args.length != 3 ) 75 if( args.length != 3 ) 76 throw new RuntimeException(pos, "if takes three argument | 76 throw genex!RuntimeException(pos, "if takes three argume 77 if( auto x = cast(IntValue)args[0] ) 77 if( auto x = cast(IntValue)args[0] ) 78 if( auto ft = cast(FunValue)args[1] ) 78 if( auto ft = cast(FunValue)args[1] ) 79 if( auto fe = cast(FunValue)args[2] ) 79 if( auto fe = cast(FunValue)args[2] ) 80 return (x.data == 0 ? fe : ft).call(pos,[]); | 80 return (x.data == 0 ? fe : ft).call(pos,lay,[]); 81 throw new RuntimeException(pos, "type mismatch in if"); | 81 throw genex!RuntimeException(pos, "type mismatch in if"); 82 })); 82 })); 83 return ctx; 83 return ctx; 84 } 84 } 85 85 86 /// Entry point of this module 86 /// Entry point of this module 87 87 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) 88 Tuple!(Value,"val",Table,"ctx") evalString(S,T...)(S str, T fn_ln_cn) ................................................................................................................................................................................ 94 { 94 { 95 return eval( polemy.parse.parseFile(filename, ln_cn) ); 95 return eval( polemy.parse.parseFile(filename, ln_cn) ); 96 } 96 } 97 97 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 98 Tuple!(Value,"val",Table,"ctx") eval(AST e) 99 { 99 { 100 Table ctx = createGlobalContext(); 100 Table ctx = createGlobalContext(); 101 return typeof(return)(eval(e, ctx), ctx); | 101 return typeof(return)(eval(e, ctx, false, "@v"), ctx); 102 } 102 } 103 103 104 Value eval(AST _e, Table ctx, bool splitCtx = false, Layer lay="@val") | 104 Value eval(AST _e, Table ctx, bool splitCtx, Layer lay) 105 { 105 { 106 if( auto e = cast(StrLiteral)_e ) 106 if( auto e = cast(StrLiteral)_e ) 107 { 107 { 108 return new StrValue(e.data); 108 return new StrValue(e.data); 109 } 109 } 110 else 110 else 111 if( auto e = cast(IntLiteral)_e ) 111 if( auto e = cast(IntLiteral)_e ) ................................................................................................................................................................................ 122 { 122 { 123 return eval(e.expr, ctx, false, e.lay); 123 return eval(e.expr, ctx, false, e.lay); 124 } 124 } 125 else 125 else 126 if( auto e = cast(LetExpression)_e ) 126 if( auto e = cast(LetExpression)_e ) 127 { 127 { 128 // for letrec, we need this, but should avoid overwriting???? 128 // for letrec, we need this, but should avoid overwriting???? 129 // ctx.set(e.var, "@val", new UndefinedValue, e.pos); | 129 // ctx.set(e.var, "@v", new UndefinedValue, e.pos); 130 Value v = eval(e.init, ctx, true); | 130 Value v = eval(e.init, ctx, true, lay); 131 if(splitCtx) 131 if(splitCtx) 132 ctx = new Table(ctx, Table.Kind.NotPropagateSet); 132 ctx = new Table(ctx, Table.Kind.NotPropagateSet); 133 ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos); 133 ctx.set(e.var, (e.layer.length ? e.layer : lay), v, e.pos); 134 return eval(e.expr, ctx); | 134 return eval(e.expr, ctx, false, lay); 135 } 135 } 136 else 136 else 137 if( auto e = cast(FuncallExpression)_e ) 137 if( auto e = cast(FuncallExpression)_e ) 138 { 138 { 139 Value _f = eval(e.fun, ctx); | 139 Value _f = eval(e.fun, ctx, true, lay); 140 if( auto f = cast(FunValue)_f ) { 140 if( auto f = cast(FunValue)_f ) { 141 Value[] args; 141 Value[] args; 142 foreach(a; e.args) 142 foreach(a; e.args) 143 args ~= eval(a, ctx); | 143 args ~= eval(a, ctx, true, lay); 144 return f.call(e.pos, args); | 144 return f.call(e.pos, lay, args); 145 } else 145 } else 146 throw new RuntimeException(e.pos, "Non-funcion is applie | 146 throw genex!RuntimeException(e.pos, "Non-funcion is appl 147 } 147 } 148 else 148 else 149 if( auto e = cast(FunLiteral)_e ) 149 if( auto e = cast(FunLiteral)_e ) 150 { 150 { 151 return new FunValue(delegate Value(immutable LexPosition pos, Va | 151 return new FunValue(delegate Value(immutable LexPosition pos, st 152 if( e.params.length != args.length ) 152 if( e.params.length != args.length ) 153 throw new RuntimeException(e.pos, sprintf!"Argum | 153 throw genex!RuntimeException(e.pos, sprintf!"Arg 154 (e.params.length, args.length)); 154 (e.params.length, args.length)); 155 Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet 155 Table ctxNeo = new Table(ctx, Table.Kind.NotPropagateSet 156 foreach(i,p; e.params) 156 foreach(i,p; e.params) 157 ctxNeo.set(p, "@val", args[i]); | 157 ctxNeo.set(p, lay, args[i]); 158 return eval(e.funbody, ctxNeo); | 158 return eval(e.funbody, ctxNeo, true, lay); 159 }); 159 }); 160 } 160 } 161 throw new RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression % | 161 throw genex!RuntimeException(_e.pos, sprintf!"Unknown Kind of Expression 162 } 162 } 163 163 164 unittest 164 unittest 165 { 165 { 166 auto r = assert_nothrow( evalString(`var x = 21; x + x*x;`) ); 166 auto r = assert_nothrow( evalString(`var x = 21; x + x*x;`) ); 167 assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 167 assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 168 assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21)) ); | 168 assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21)) ); 169 assert_nothrow( r.ctx.get("x","@val") ); | 169 assert_nothrow( r.ctx.get("x","@v") ); 170 assert_throw!RuntimeException( r.ctx.get("y","@val") ); | 170 assert_throw!RuntimeException( r.ctx.get("y","@v") ); 171 } 171 } 172 unittest 172 unittest 173 { 173 { 174 auto r = assert_nothrow( evalString(`var x = 21; var x = x + x*x;`) ); 174 auto r = assert_nothrow( evalString(`var x = 21; var x = x + x*x;`) ); 175 assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 175 assert_eq( r.val, new IntValue(BigInt(21+21*21)) ); 176 assert_eq( r.ctx.get("x","@val"), new IntValue(BigInt(21+21*21)) ); | 176 assert_eq( r.ctx.get("x","@v"), new IntValue(BigInt(21+21*21)) ); 177 assert_nothrow( r.ctx.get("x","@val") ); | 177 assert_nothrow( r.ctx.get("x","@v") ); 178 assert_throw!RuntimeException( r.ctx.get("y","@val") ); | 178 assert_throw!RuntimeException( r.ctx.get("y","@v") ); 179 } 179 } 180 unittest 180 unittest 181 { 181 { 182 assert_eq( evalString(`let x=1; let y=(let x=2); x`).val, new IntValue(B 182 assert_eq( evalString(`let x=1; let y=(let x=2); x`).val, new IntValue(B 183 assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new 183 assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new 184 } 184 } 185 unittest 185 unittest ................................................................................................................................................................................ 203 if(x<2) 203 if(x<2) 204 { 1; } 204 { 1; } 205 else 205 else 206 { fib(x-1) + fib(x-2); }; 206 { fib(x-1) + fib(x-2); }; 207 }; 207 }; 208 fib(10);`).val, new IntValue(BigInt(89))); 208 fib(10);`).val, new IntValue(BigInt(89))); 209 } 209 } > 210 > 211 unittest > 212 { > 213 assert_throw!Throwable( evalString(`@s "+"=fun(x,y){x-y};@s(1+2)`) ); > 214 assert_eq( evalString(`@s "+"=fun(x,y){x-y};1+2`).val, new IntValue(BigI > 215 assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};1+2`).val, new I > 216 assert_eq( evalString(`@s "+"=fun(x,y){@v(@s(x)-@s(y))};@s(1+2)`).val, n > 217 }