Check-in [68546f3e9f]
Not logged in
Overview
SHA1 Hash:68546f3e9fb24a3d2d621ba158741b290dba1987
Date: 2010-11-10 00:19:20
User: kinaba
Comment:several samples
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | manifest
Tags And Properties
Changes

Modified polemy/parse.d from [a8c1fd55863166fb] to [3e7ec41f1027e8ba].

63 63 if( tryEat("(") ) { 64 64 lex = saved; 65 65 goto asExpression; 66 66 } 67 67 } 68 68 immutable LexPosition varpos = (lex.empty ? null : lex.front.pos); 69 69 string var = eatId("after "~kwd,true); 70 - eat("=", "after "~kwd); 71 - kwd = (kwd[0]=='@' ? kwd : ""); // "let, var, def ==> neutral layer" 72 - auto e = E(0); 73 - if( tryEat(";") && !lex.empty && (lex.front.quoted || !["}",")","]"].canFind(lex.front.str)) ) 74 - return new LetExpression(pos, var, kwd, e, Body()); 75 - else 76 - return new LetExpression(pos, var, kwd, e, new VarExpression(varpos, var)); 70 + // [TODO] refactor. only auto e = ... differ 71 + if(tryEat("(")) { 72 + kwd = (kwd[0]=='@' ? kwd : ""); // "let, var, def ==> neutral layer" 73 + auto e = parseLambdaAfterOpenParen(varpos); 74 + if( tryEat(";") && !lex.empty && (lex.front.quoted || !["}",")","]"].canFind(lex.front.str)) ) 75 + return new LetExpression(pos, var, kwd, e, Body()); 76 + else 77 + return new LetExpression(pos, var, kwd, e, new VarExpression(varpos, var)); 78 + } else { 79 + eat("=", "after "~kwd); 80 + kwd = (kwd[0]=='@' ? kwd : ""); // "let, var, def ==> neutral layer" 81 + auto e = E(0); 82 + if( tryEat(";") && !lex.empty && (lex.front.quoted || !["}",")","]"].canFind(lex.front.str)) ) 83 + return new LetExpression(pos, var, kwd, e, Body()); 84 + else 85 + return new LetExpression(pos, var, kwd, e, new VarExpression(varpos, var)); 86 + } 77 87 } 78 88 else 79 89 { 80 90 asExpression: 81 91 auto e = E(0); 82 92 if( tryEat(";") && !lex.empty && (lex.front.quoted || (lex.front.str!="}" && lex.front.str!=")")) ) 83 93 return new LetExpression(pos, "_", "", e, Body()); ................................................................................ 200 210 new FunLiteral(thenPos, [], th), 201 211 new FunLiteral(elsePos, [], el) 202 212 ); 203 213 } 204 214 if( tryEat("fun") || tryEat("\u03BB") ) 205 215 { 206 216 eat("(", "after fun"); 207 - string[] params; 208 - while( !tryEat(")") ) 209 - { 210 - params ~= eatId("for function parameter"); 211 - if( !tryEat(",") ) { 212 - eat(")", "after function parameters"); 213 - break; 214 - } 215 - } 216 - eat("{", "after function parameters"); 217 - auto funbody = Body(); 218 - eat("}", "after function body"); 219 - return new FunLiteral(pos, params, funbody); 217 + return parseLambdaAfterOpenParen(pos); 220 218 } 221 219 scope(exit) lex.popFront; 222 220 return new VarExpression(pos, lex.front.str); 223 221 } 222 + 223 + AST parseLambdaAfterOpenParen(immutable LexPosition pos) 224 + { 225 + string[] params; 226 + while( !tryEat(")") ) 227 + { 228 + params ~= eatId("for function parameter"); 229 + if( !tryEat(",") ) { 230 + eat(")", "after function parameters"); 231 + break; 232 + } 233 + } 234 + eat("{", "after function parameters"); 235 + auto funbody = Body(); 236 + eat("}", "after function body"); 237 + return new FunLiteral(pos, params, funbody); 238 + } 224 239 225 240 private: 226 241 Lexer lex; 227 242 this(Lexer lex) { this.lex = lex; } 228 243 229 244 void eat(string kwd, lazy string msg) 230 245 { ................................................................................ 328 343 assert_throw!ParseException(parseString(`1+2}`)); 329 344 assert_throw!UnexpectedEOF(parseString(`let "x"`)); 330 345 assert_throw!UnexpectedEOF(parseString(`var`)); 331 346 assert_throw!ParseException(parseString(`@val x ==`)); 332 347 assert_throw!ParseException(parseString(`if(){1}`)); 333 348 assert_throw!UnexpectedEOF(parseString(`f(`)); 334 349 } 350 + 351 +unittest 352 +{ 353 + mixin EasyAST; 354 + assert_eq(parseString(`def foo(x) { x+1 }; foo`), 355 + let("foo", "", 356 + fun(["x"], call(var("+"), var("x"), intl(1))), 357 + var("foo")) 358 + ); 359 +}

Added sample/fib.pmy version [8a19e45bd42355a9]

1 +def fib(x) 2 +{ 3 + if( x < 2 ) { 1 } 4 + else { fib(x-1) + fib(x-2) } 5 +}; 6 + 7 +let upto = λ(n, f){ 8 + if( n > 0 ){ upto(n-1,f) }; 9 + f(n) 10 +}; 11 + 12 +var compose = fun(f,g){ fun(x){f(g(x))} }; 13 +var "<<" = compose; 14 + 15 +upto(16, print<<fib);

Added sample/helloworld.pmy version [5a53203a60db5c54]

1 +print("Hello, World!")

Added sample/plusminus.pmy version [5278c04aa82ef1ff]

1 +@s "+" = fun(x, y) {@v( 2 + @s(x) - @s(y) 3 +)}; 4 + 5 +print( 1 + 2 ); 6 +print( @s(1 + 2) );