Differences From Artifact [6c61d557752efef5]:
- File
polemy/parse.d
- 2010-11-24 11:20:42 - part of checkin [153a14cec0] on branch trunk - if-then-else without {}s. some cosmetic changes (user: kinaba) [annotate]
To 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]
322 322 el = E(0);
323 323 }
324 324 return new App(pos, new Var(pos,"if"), cond, new Fun(thenPos,[],th), new Fun(elsePos,[],el));
325 325 }
326 326
327 327 AST parsePatternMatch(LexPosition pos)
328 328 {
329 - // case "(" pmExpr ")" CASES
329 + // case pmExpr CASES
330 330 //==>
331 331 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFirst())
332 - eat("(", "after case");
333 332 AST pmExpr = E(0);
334 - eat(")", "after case");
335 333 string pmVar = freshVarName();
336 334 string pmTryFirst = freshVarName();
337 335 AST pmBody = parsePatternMatchCases(pmVar, pmTryFirst,
338 336 new App(pos, new Var(pos, pmTryFirst)));
339 337 return new Let(pos, pmVar, [], pmExpr, pmBody);
340 338 }
341 339
342 340 AST parsePatternMatchCases(string pmVar, string tryThisBranchVar, AST thenDoThis)
343 341 {
344 - // when( pat ) { cBody }
342 + // when pat: cBody
345 343 //==>
346 344 // ... let failBranchVar = ... in
347 345 // let tryThisBranchVar = fun(){ if(test){cBody}else{failBranchVar()} } in thenDoThis
348 346 if( tryEat("when") )
349 347 {
350 348 auto pos = currentPosition();
351 349 string failBranchVar = freshVarName();
352 350
353 - eat("(", "after when");
354 351 auto pr = parsePattern();
355 - eat(")", "after when");
356 - eat("{", "after pattern");
357 - AST cBody = Body();
352 + eat(":", "after when pattern");
353 + AST cBody = E(0);
358 354 AST judgement = new App(pos, new Var(pos, "if"),
359 355 ppTest(pmVar, pr), new Fun(pos,[],ppBind(pmVar, pr, cBody)),
360 356 new Var(pos, failBranchVar));
361 - eat("}", "after pattern clause");
362 357 return parsePatternMatchCases(pmVar, failBranchVar,
363 358 new Let(pos, tryThisBranchVar, [],
364 359 new Fun(pos,[],judgement), thenDoThis)
365 360 );
366 361 }
367 362 else
368 363 {
................................................................................
659 654 assert_eq(parseString(`x{y:1}`), call(var(".="),var("x"),strl("y"),intl(1)));
660 655 }
661 656
662 657 unittest
663 658 {
664 659 assert_nothrow(parseString(`
665 660 case( 1 )
666 - when(x){1}
661 + when(x): 1
667 662 `));
668 663 assert_nothrow(parseString(`
669 - case( 1 )
670 - when({aaaa:_}){1}
664 + case 1
665 + when {aaaa:_}: 1
671 666 `));
672 667 assert_nothrow(parseString(`
673 - case( 1 )
674 - when({aaaa:@value(x)}){1}
675 - when({aaaa:{bbb:_}, ccc:123}){1}
668 + case 1
669 + when {aaaa:@value(x)}: 1
670 + when {aaaa:{bbb:_}, ccc:123}: 1
676 671 `));
677 672 }
678 673
679 674 unittest
680 675 {
681 676 // test for omitting { .. }
682 677 assert_nothrow(parseString(`
683 678 if(1) 2 else 3
684 679 `));
685 680 assert_nothrow(parseString(`
686 681 if(1) x{y:z} else 3
687 682 `));
688 683 }