Differences From 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]
To Artifact [38516f68b159d6bd]:
- File
polemy/parse.d
- 2010-11-24 13:22:04 - part of checkin [f9c31f3cd8] on branch trunk - Fixed the null dereference bug when directly wrote "case 1 when 2: 3" in REPL. It was due to null LexPosition in the AST. Now AST.pos !is null is an invariant of AST. (user: kinaba) [annotate]
248 } 248 }
249 249
250 AST BaseExpression() 250 AST BaseExpression()
251 { 251 {
252 if( lex.empty ) 252 if( lex.empty )
253 throw genex!UnexpectedEOF(currentPosition(), "Reached EO 253 throw genex!UnexpectedEOF(currentPosition(), "Reached EO
254 254
255 auto pos = lex.front.pos; | 255 auto pos = currentPosition();
256 if( lex.front.quoted ) 256 if( lex.front.quoted )
257 { 257 {
258 scope(exit) lex.popFront; 258 scope(exit) lex.popFront;
259 return new Str(pos, lex.front.str); 259 return new Str(pos, lex.front.str);
260 } 260 }
261 if( isNumber(lex.front.str) ) 261 if( isNumber(lex.front.str) )
262 { 262 {
................................................................................................................................................................................
321 { 321 {
322 // case pmExpr CASES 322 // case pmExpr CASES
323 //==> 323 //==>
324 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir 324 // let pmVar = pmExpr in (... let pmTryFirst = ... in pmTryFir
325 AST pmExpr = E(0); 325 AST pmExpr = E(0);
326 string pmVar = freshVarName(); 326 string pmVar = freshVarName();
327 string pmTryFirst = freshVarName(); 327 string pmTryFirst = freshVarName();
328 AST pmBody = parsePatternMatchCases(pmVar, pmTryFirst, | 328 AST pmBody = parsePatternMatchCases(pos, pmVar, pmTryFirst,
329 new App(pos, new Var(pos, pmTryFirst))); 329 new App(pos, new Var(pos, pmTryFirst)));
330 return new Let(pos, pmVar, [], pmExpr, pmBody); 330 return new Let(pos, pmVar, [], pmExpr, pmBody);
331 } 331 }
332 332
333 AST parsePatternMatchCases(string pmVar, string tryThisBranchVar, AST th | 333 AST parsePatternMatchCases(LexPosition casePos, string pmVar, string try
334 { 334 {
335 // when pat: cBody 335 // when pat: cBody
336 //==> 336 //==>
337 // ... let failBranchVar = ... in 337 // ... let failBranchVar = ... in
338 // let tryThisBranchVar = fun(){ if(test){cBody}else{failBran 338 // let tryThisBranchVar = fun(){ if(test){cBody}else{failBran
339 if( tryEat("when") ) 339 if( tryEat("when") )
340 { 340 {
................................................................................................................................................................................
343 343
344 auto pr = parsePattern(); 344 auto pr = parsePattern();
345 eat(":", "after when pattern"); 345 eat(":", "after when pattern");
346 AST cBody = E(0); 346 AST cBody = E(0);
347 AST judgement = new App(pos, new Var(pos, "if"), 347 AST judgement = new App(pos, new Var(pos, "if"),
348 ppTest(pmVar, pr), new Fun(pos,[],ppBind(pmVar, 348 ppTest(pmVar, pr), new Fun(pos,[],ppBind(pmVar,
349 new Var(pos, failBranchVar)); 349 new Var(pos, failBranchVar));
350 return parsePatternMatchCases(pmVar, failBranchVar, | 350 return parsePatternMatchCases(casePos, pmVar, failBranch
351 new Let(pos, tryThisBranchVar, [], 351 new Let(pos, tryThisBranchVar, [],
352 new Fun(pos,[],judgement), thenDoThis) 352 new Fun(pos,[],judgement), thenDoThis)
353 ); 353 );
354 } 354 }
355 else 355 else
356 { 356 {
357 auto pos = currentPosition(); <
358 AST doNothing = new Fun(pos,[], | 357 AST doNothing = new Fun(casePos,[],
359 new Str(pos, sprintf!"(pattern match failure:%s) | 358 new Str(casePos, sprintf!"(pattern match failure
360 return new Let(currentPosition(), tryThisBranchVar, [], | 359 return new Let(casePos, tryThisBranchVar, [], doNothing,
361 } 360 }
362 } 361 }
363 362
364 // hageshiku tenuki 363 // hageshiku tenuki
365 abstract class SinglePattern 364 abstract class SinglePattern
366 { 365 {
367 string[] path; 366 string[] path;
................................................................................................................................................................................
555 AST doNothingExpression() 554 AST doNothingExpression()
556 { 555 {
557 return new Str(currentPosition(), "(empty function body)"); 556 return new Str(currentPosition(), "(empty function body)");
558 } 557 }
559 558
560 LexPosition currentPosition() 559 LexPosition currentPosition()
561 { 560 {
562 return lex.empty ? null : lex.front.pos; | 561 return lex.empty ? new LexPosition("EOF",0,0) : lex.front.pos;
563 } 562 }
564 } 563 }
565 564
566 unittest 565 unittest
567 { 566 {
568 mixin EasyAST; 567 mixin EasyAST;
569 568