Differences From Artifact [9751c1054b6569e6]:
- File
polemy/parse.d
- 2010-11-24 11:45:25 - part of checkin [adacc96a55] on branch trunk - changed the syntax of pattern matching (user: kinaba) [annotate]
To Artifact [f6a85fc83dd90cb0]:
- File
polemy/parse.d
- 2010-11-24 12:14:00 - part of checkin [3ae09b8cbf] on branch trunk - changed if-then-else syntax (user: kinaba) [annotate]
297 297 }
298 298 scope(exit) lex.popFront;
299 299 return new Var(pos, lex.front.str);
300 300 }
301 301
302 302 AST parseIfAfterIf(LexPosition pos)
303 303 {
304 - eat("(", "after if");
305 304 auto cond = E(0);
306 - eat(")", "after if condition");
307 - auto thenPos = lex.front.pos;
308 - AST th;
309 - if( tryEat("{") ) {
310 - th = Body();
311 - eat("}", "after if-then body");
312 - } else {
313 - th = E(0);
305 + auto thenPos = currentPosition();
306 + if(!tryEat(":")) {
307 + eat("then", "after if condition");
308 + tryEat(":");
314 309 }
310 + AST th = E(0);
315 311 auto el = doNothingExpression();
316 - auto elsePos = (lex.empty ? LexPosition.dummy : lex.front.pos);
317 - if( tryEat("else") )
318 - if( tryEat("{") ) {
319 - el = Body();
320 - eat("}", "after else body");
321 - } else {
322 - el = E(0);
323 - }
312 + auto elsePos = currentPosition();
313 + if( tryEat("else") ) {
314 + tryEat(":");
315 + el = E(0);
316 + }
324 317 return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el));
325 318 }
326 319
327 320 AST parsePatternMatch(LexPosition pos)
328 321 {
329 322 // case pmExpr CASES
330 323 //==>
................................................................................
584 577 assert_eq(parseString(`1;2;`), let("_","",intl(1),intl(2)));
585 578 assert_eq(parseString(`let x=1 in 2`), let("x","",intl(1),intl(2)));
586 579 assert_eq(parseString(`var x=1;2;`), let("x","",intl(1),intl(2)));
587 580 assert_eq(parseString(`def x=1`), let("x","",intl(1),var("x")));
588 581 assert_eq(parseString(`@val x=1;`), let("x","@val",intl(1),var("x")));
589 582 assert_eq(parseString(`@typ x="#int";`), let("x","@typ",strl("#int"),var("x")));
590 583 assert_eq(parseString(`f(1,2)`), call(var("f"),intl(1),intl(2)));
591 - assert_eq(parseString(`if(1){2}`), call(var("if"),intl(1),fun([],intl(2)),fun([],strl("(empty function body)"))));
592 - assert_eq(parseString(`if(1){2}else{3}`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(3))));
593 - assert_eq(parseString(`if(1){}else{3}()()`),
584 + assert_eq(parseString(`if 1 then 2`), call(var("if"),intl(1),fun([],intl(2)),fun([],strl("(empty function body)"))));
585 + assert_eq(parseString(`if 1 then: 2 else(3)`), call(var("if"),intl(1),fun([],intl(2)),fun([],intl(3))));
586 + assert_eq(parseString(`(if 1 then () else 3)()()`),
594 587 call(call(call(var("if"),intl(1),fun([],strl("(empty function body)")),fun([],intl(3))))));
595 588 assert_eq(parseString(`1+2*3`), call(var("+"),intl(1),call(var("*"),intl(2),intl(3))));
596 589 assert_eq(parseString(`(1+2)*3`), call(var("*"),call(var("+"),intl(1),intl(2)),intl(3)));
597 590 assert_eq(parseString(`1*(2+3)`), call(var("*"),intl(1),call(var("+"),intl(2),intl(3))));
598 591 assert_eq(parseString(`1*2+3`), call(var("+"),call(var("*"),intl(1),intl(2)),intl(3)));
599 592 assert_eq(parseString(`@x(1)`), lay("@x", intl(1)));
600 593 assert_eq(parseString(`fun(x @v @t, y, z @t){}`),
................................................................................
605 598 let y = 200; #comment!!!!!
606 599 x+y
607 600 `),
608 601 let("x", "", intl(100), let("y", "", intl(200), call(var("+"), var("x"), var("y"))))
609 602 );
610 603
611 604 assert_eq(parseString(`
612 - var fac = fun(x){ if(x <= 1) {1} else {x*fac(x-1)} };
605 + var fac = fun(x){ if(x <= 1) then 1 else x*fac(x-1) };
613 606 fac(10)
614 607 `),
615 608 let("fac", "", fun(["x"],
616 609 call(var("if"),
617 610 call(var("<="), var("x"), intl(1)),
618 611 fun([], intl(1)),
619 612 fun([], call(var("*"), var("x"), call(var("fac"),call(var("-"),var("x"),intl(1)))))
................................................................................
666 659 `));
667 660 assert_nothrow(parseString(`
668 661 case 1
669 662 when {aaaa:@value(x)}: 1
670 663 when {aaaa:{bbb:_}, ccc:123}: 1
671 664 `));
672 665 }
673 -
674 -unittest
675 -{
676 - // test for omitting { .. }
677 - assert_nothrow(parseString(`
678 - if(1) 2 else 3
679 - `));
680 - assert_nothrow(parseString(`
681 - if(1) x{y:z} else 3
682 - `));
683 -}