Check-in [7465fcdd7f]
Not logged in
Overview
SHA1 Hash:7465fcdd7f2f7407f2e88bc6fde115bfaff3660b
Date: 2010-11-09 23:59:36
User: kinaba
Comment:layered function invocation
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified main.d from [b29be17d733aa309] to [667427a12e5e3b09].

26 26 { 27 27 scope(failure) 28 28 { buf = ""; lineno = nextlineno; } 29 29 30 30 buf ~= s; 31 31 nextlineno ++; 32 32 try 33 - { lastVal = eval(parseString(buf, "<REPL>", lineno), ctx); } 33 + { lastVal = eval(parseString(buf, "<REPL>", lineno), ctx, false, "@v"); } 34 34 catch( UnexpectedEOF ) 35 35 { return false; } // wait 36 36 buf = ""; 37 37 lineno = nextlineno; 38 38 return true; 39 39 } 40 40

Modified polemy/eval.d from [62b3543f769fc9c9] to [050eab1743a45f8b].

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

Modified polemy/lex.d from [1725bdb3bf054565] to [caf474d107a1f8cb].

159 159 readWhile!isSpace(); 160 160 this.current = (current is null ? readNext() : current); 161 161 } 162 162 163 163 public static { 164 164 bool isSpace (dchar c) { return std.ctype.isspace(c)!=0; } 165 165 bool isSymbol (dchar c) { return 0x21<=c && c<=0x7f && !std.ctype.isalnum(c) && c!='_' && c!='\''; } 166 - bool isSSymbol (dchar c) { return !find("()[]{};", c).empty; } 166 + bool isSSymbol (dchar c) { return "()[]{};@".canFind(c); } 167 167 bool isMSymbol (dchar c) { return isSymbol(c) && !isSSymbol(c) && c!='"' && c!='#'; } 168 168 bool isLetter (dchar c) { return !isSpace(c) && !isSymbol(c); } 169 169 } 170 170 171 171 string readQuoted(const LexPosition pos){char[] buf; return readQuoted(pos,buf);} 172 172 string readQuoted(const LexPosition pos, ref char[] buf) 173 173 { ................................................................................ 269 269 assert_eq( ts[4].pos.lineno, 2 ); 270 270 assert_eq( ts[4].pos.column, 6 ); 271 271 assert_eq( ts[4].str, ":-" ); 272 272 273 273 assert_eq( ts[5].pos.lineno, 2 ); 274 274 assert_eq( ts[5].pos.column, 8 ); 275 275 assert_eq( ts[5].str, "(" ); 276 - assert_eq( ts[6].str, "@@" ); 277 - assert_eq( ts[7].str, ";" ); // paren and simicolons are split 276 + assert_eq( ts[6].str, "@" ); 277 + assert_eq( ts[7].str, "@" ); 278 + assert_eq( ts[8].str, ";" ); // paren and simicolons, atmarks are split 278 279 279 - assert_eq( ts.length, 8 ); 280 + assert_eq( ts.length, 9 ); 280 281 } 281 282 282 283 unittest 283 284 { 284 285 // !! be sure to run the unittest on the root of the source directory 285 286 auto lexf = lexerFromFile("polemy/lex.d"); 286 287 lexf = find!`a.str == "module"`(lexf); ................................................................................ 364 365 365 366 unittest 366 367 { 367 368 auto lex = lexerFromString(`=""`); 368 369 assert_eq(lex.front.str, "="); lex.popFront; 369 370 assert_eq(lex.front.str, ""); lex.popFront; 370 371 assert( lex.empty ); 372 + assert_eq( lexerFromString(`-@`).front.str, "-" ); 371 373 } 372 374 373 375 /// Forward range for reader character by character, 374 376 /// keeping track of position information and caring \r\n -> \n conversion. 375 377 376 378 private 377 379 struct PositionedReader(CharSeq)

Modified polemy/value.d from [737774791c82ade1] to [01c119756ed23ca5].

8 8 import polemy._common; 9 9 import polemy.lex; 10 10 11 11 /// Raised when something went wrong in runtime 12 12 13 13 class RuntimeException : Exception 14 14 { 15 - const LexPosition pos; 16 - 17 - this( const LexPosition pos, string msg, string file=null, size_t line=0, Throwable next=null ) 18 - { super(sprintf!"[%s] %s"(pos, msg), file, line, next); this.pos = pos; } 15 + mixin ExceptionWithPosition; 19 16 } 20 17 21 18 /// Runtime values of Polemy 22 19 23 20 abstract class Value 24 21 { 25 22 } 26 23 27 24 class IntValue : Value 28 25 { 29 26 BigInt data; 30 27 31 - mixin SimpleConstructor; 32 - mixin SimpleCompare; 28 + mixin SimpleClass; 33 29 override string toString() const { return std.bigint.toDecimalString(cast(BigInt)data); } 34 30 } 35 31 36 32 class StrValue : Value 37 33 { 38 34 string data; 39 35 40 - mixin SimpleConstructor; 41 - mixin SimpleCompare; 36 + mixin SimpleClass; 42 37 override string toString() const { return data; } 43 38 } 44 39 45 40 class FunValue : Value 46 41 { 47 - Value delegate(immutable LexPosition pos, Value[]) data; 42 + Value delegate(immutable LexPosition pos, string lay, Value[]) data; 48 43 49 44 mixin SimpleConstructor; 50 45 alias data call; 51 46 override string toString() const { return sprintf!"(function:%s:%s)"(data.ptr,data.funcptr); } 52 47 } 53 48 54 49 /// Layer ID ................................................................................ 103 98 unittest 104 99 { 105 100 Table c0 = new Table; 106 101 Table c01 = new Table(c0, Table.Kind.NotPropagateSet); 107 102 Table c012 = new Table(c01, Table.Kind.PropagateSet); 108 103 Table c013 = new Table(c01, Table.Kind.PropagateSet); 109 104 110 - assert_nothrow( c012.set("x", "@val", new IntValue(BigInt(12))) ); 111 - assert_throw!RuntimeException( c013.get("x", "@val") ); 112 - assert_nothrow( c013.set("x", "@val", new IntValue(BigInt(13))) ); 113 - assert_eq( c013.get("x", "@val"), new IntValue(BigInt(13)) ); 114 - assert_eq( c012.get("x", "@val"), new IntValue(BigInt(12)) ); 115 - assert_throw!RuntimeException( c01.get("x", "@val") ); 105 + assert_nothrow( c012.set("x", "@v", new IntValue(BigInt(12))) ); 106 + assert_throw!RuntimeException( c013.get("x", "@v") ); 107 + assert_nothrow( c013.set("x", "@v", new IntValue(BigInt(13))) ); 108 + assert_eq( c013.get("x", "@v"), new IntValue(BigInt(13)) ); 109 + assert_eq( c012.get("x", "@v"), new IntValue(BigInt(12)) ); 110 + assert_throw!RuntimeException( c01.get("x", "@v") ); 111 + 112 + assert_nothrow( c01.set("y", "@v", new IntValue(BigInt(1))) ); 113 + assert_eq( c013.get("y", "@v"), new IntValue(BigInt(1)) ); 114 + assert_eq( c012.get("y", "@v"), new IntValue(BigInt(1)) ); 115 + assert_eq( c01.get("y", "@v"), new IntValue(BigInt(1)) ); 116 116 117 - assert_nothrow( c01.set("y", "@val", new IntValue(BigInt(1))) ); 118 - assert_eq( c013.get("y", "@val"), new IntValue(BigInt(1)) ); 119 - assert_eq( c012.get("y", "@val"), new IntValue(BigInt(1)) ); 120 - assert_eq( c01.get("y", "@val"), new IntValue(BigInt(1)) ); 117 + assert_nothrow( c0.set("z", "@v", new IntValue(BigInt(0))) ); 118 + assert_eq( c013.get("z", "@v"), new IntValue(BigInt(0)) ); 119 + assert_eq( c012.get("z", "@v"), new IntValue(BigInt(0)) ); 120 + assert_eq( c01.get("z", "@v"), new IntValue(BigInt(0)) ); 121 + assert_eq( c0.get("z", "@v"), new IntValue(BigInt(0)) ); 121 122 122 - assert_nothrow( c0.set("z", "@val", new IntValue(BigInt(0))) ); 123 - assert_eq( c013.get("z", "@val"), new IntValue(BigInt(0)) ); 124 - assert_eq( c012.get("z", "@val"), new IntValue(BigInt(0)) ); 125 - assert_eq( c01.get("z", "@val"), new IntValue(BigInt(0)) ); 126 - assert_eq( c0.get("z", "@val"), new IntValue(BigInt(0)) ); 123 + assert_nothrow( c012.set("y", "@v", new IntValue(BigInt(444))) ); 124 + assert_eq( c013.get("y", "@v"), new IntValue(BigInt(444)) ); 125 + assert_eq( c012.get("y", "@v"), new IntValue(BigInt(444)) ); 126 + assert_eq( c01.get("y", "@v"), new IntValue(BigInt(444)) ); 127 127 128 - assert_nothrow( c012.set("y", "@val", new IntValue(BigInt(444))) ); 129 - assert_eq( c013.get("y", "@val"), new IntValue(BigInt(444)) ); 130 - assert_eq( c012.get("y", "@val"), new IntValue(BigInt(444)) ); 131 - assert_eq( c01.get("y", "@val"), new IntValue(BigInt(444)) ); 132 - 133 - assert_nothrow( c012.set("z", "@val", new IntValue(BigInt(555))) ); 134 - assert_eq( c013.get("z", "@val"), new IntValue(BigInt(0)) ); 135 - assert_eq( c012.get("z", "@val"), new IntValue(BigInt(555)) ); 136 - assert_eq( c01.get("z", "@val"), new IntValue(BigInt(0)) ); 137 - assert_eq( c0.get("z", "@val"), new IntValue(BigInt(0)) ); 128 + assert_nothrow( c012.set("z", "@v", new IntValue(BigInt(555))) ); 129 + assert_eq( c013.get("z", "@v"), new IntValue(BigInt(0)) ); 130 + assert_eq( c012.get("z", "@v"), new IntValue(BigInt(555)) ); 131 + assert_eq( c01.get("z", "@v"), new IntValue(BigInt(0)) ); 132 + assert_eq( c0.get("z", "@v"), new IntValue(BigInt(0)) ); 138 133 139 134 // [TODO] define the semantics and test @layers 140 135 }

Modified tricks/test.d from [09cc57fcd7c969ca] to [7c50b0e91b8919b8].

6 6 */ 7 7 module tricks.test; 8 8 import std.conv : to; 9 9 import core.exception; 10 10 11 11 /// Unittest helper that asserts an expression must throw something 12 12 13 -void assert_throw(ExceptionType, T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="") 13 +void assert_throw(ExceptionType=Throwable, 14 + T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="") 14 15 { 15 - try 16 - { t(); } 17 - catch(ExceptionType) 18 - { return; } 19 - catch(Throwable e) 20 - { onAssertErrorMsg(fn, ln, msg.length ? msg : "bad exception\n >> "~e.toString()); } 16 + static if( is(ExceptionType == Throwable) ) 17 + try 18 + { t(); } 19 + catch(ExceptionType) 20 + { return; } 21 + else 22 + try 23 + { t(); } 24 + catch(ExceptionType) 25 + { return; } 26 + catch(Throwable e) 27 + { onAssertErrorMsg(fn, ln, msg.length ? msg : "bad exception\n >> "~e.toString()); } 21 28 onAssertErrorMsg(fn, ln, msg.length ? msg : "not thrown"); 22 29 } 23 30 24 31 /// Unittest helper that asserts an expression must not throw anything 25 32 26 33 auto assert_nothrow(T, string fn=__FILE__, size_t ln=__LINE__)(lazy T t, string msg="") 27 34 {