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