Differences From Artifact [232a7f3469a1ed88]:
- File
polemy/eval.d
- 2010-11-13 04:58:01 - part of checkin [3a2762fca5] on branch trunk - _isXXX (user: kinaba) [annotate]
To Artifact [2bd159f2e71889e8]:
- File
polemy/eval.d
- 2010-11-13 05:38:21 - part of checkin [078444a806] on branch trunk - additional primitives for doing typechecking (user: kinaba) [annotate]
28 28 if( T.length != args.length )
29 29 throw genex!RuntimeException(pos, "argument number mismatch!");
30 30 T typed_args;
31 31 foreach(i, Ti; T)
32 32 {
33 33 typed_args[i] = cast(Ti) args[i];
34 34 if( typed_args[i] is null )
35 - throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d"(i));
35 + throw genex!RuntimeException(pos, sprintf!"type mismatch on the argument %d"(i+1));
36 36 }
37 37 try {
38 38 return dg(typed_args);
39 39 } catch( RuntimeException e ) {
40 40 throw e.pos is null ? new RuntimeException(pos, e.msg, e.file, e.line) : e;
41 41 }
42 42 });
................................................................................
47 47 {
48 48 auto ctx = new Table;
49 49 ctx.set("+", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data + rhs.data);} ));
50 50 ctx.set("-", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data - rhs.data);} ));
51 51 ctx.set("*", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data * rhs.data);} ));
52 52 ctx.set("/", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data / rhs.data);} ));
53 53 ctx.set("%", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(lhs.data % rhs.data);} ));
54 - ctx.set("<", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data < rhs.data ? 1: 0));} ));
55 - ctx.set(">", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data > rhs.data ? 1: 0));} ));
56 - ctx.set("<=", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data <= rhs.data ? 1: 0));} ));
57 - ctx.set(">=", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data >= rhs.data ? 1: 0));} ));
58 - ctx.set("==", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data == rhs.data ? 1: 0));} ));
59 - ctx.set("!=", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt(lhs.data != rhs.data ? 1: 0));} ));
54 + ctx.set("||", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) || (rhs.data!=0) ? 1:0));} ));
55 + ctx.set("&&", "@v", native( (IntValue lhs, IntValue rhs){return new IntValue(BigInt((lhs.data!=0) && (rhs.data!=0) ? 1:0));} ));
56 + ctx.set("<", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs < rhs ? 1: 0));} ));
57 + ctx.set(">", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs > rhs ? 1: 0));} ));
58 + ctx.set("<=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs <= rhs ? 1: 0));} ));
59 + ctx.set(">=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs >= rhs ? 1: 0));} ));
60 + ctx.set("==", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs == rhs ? 1: 0));} ));
61 + ctx.set("!=", "@v", native( (Value lhs, Value rhs){return new IntValue(BigInt(lhs != rhs ? 1: 0));} ));
60 62 ctx.set("print", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
61 63 foreach(a; args)
62 64 write(a);
63 65 writeln("");
64 66 return new IntValue(BigInt(178));
65 67 }));
66 68 ctx.set("if", "@v", new FunValue(delegate Value(immutable LexPosition pos, Layer lay, Value[] args){
................................................................................
121 123 if( lay == "@v" )
122 124 return v;
123 125 else // rise
124 126 return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v", [v]);
125 127 },
126 128 (VarExpression e)
127 129 {
130 + if( lay == "@v" )
131 + return ctx.get(e.var, lay, e.pos);
128 132 try {
129 133 return ctx.get(e.var, lay, e.pos);
130 - } catch( RuntimeException ) {
134 + } catch( Throwable ) { // [TODO] more precise...
131 135 // rise from @v
132 136 return (cast(FunValue)ctx.get(lay, "(system)", e.pos)).call(e.pos, "@v",
133 137 [ctx.get(e.var, "@v", e.pos)]
134 138 );
135 139 }
136 140 },
137 141 (LayeredExpression e)
................................................................................
201 205 assert_eq( evalString(`let x=1; let y=(let x=2;fun(){x}); y()`).val, new IntValue(BigInt(2)) );
202 206 }
203 207 unittest
204 208 {
205 209 assert_eq( evalString(`@a x=1; @b x=2; @a(x)`).val, new IntValue(BigInt(1)) );
206 210 assert_eq( evalString(`@a x=1; @b x=2; @b(x)`).val, new IntValue(BigInt(2)) );
207 211 assert_eq( evalString(`let x=1; let _ = (@a x=2;2); x`).val, new IntValue(BigInt(1)) );
208 - assert_throw!Error( evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) );
212 + assert_throw!Throwable( evalString(`let x=1; let _ = (@a x=2;2); @a(x)`) );
209 213 }
210 214
211 215 unittest
212 216 {
213 217 assert_eq( evalString(`var fac = fun(x){
214 218 if(x)
215 219 { x*fac(x-1); }