@@ -46,7 +46,7 @@ namespace utils {
4646 constexpr bool isAlternativeOf = isAlternativeOfHelper<Type, Variant>::value;
4747
4848 struct SourceLocation {
49- SourceLocation (int l = 1 , int c = 1 ) : line(l), column(c) {}
49+ SourceLocation (int l = 0 , int c = 0 ) : line(l), column(c) {}
5050
5151 std::string toString () const {
5252 if (line <= 0 || column <= 0 ) {
@@ -75,7 +75,7 @@ namespace utils {
7575 void panic (
7676 const std::string& type,
7777 const std::string& msg,
78- const SourceLocation& sl = SourceLocation(0 , 0 )
78+ const SourceLocation& sl = SourceLocation()
7979 ) {
8080 if (sl.line <= 0 || sl.column <= 0 ) {
8181 throw std::runtime_error (" [" + type + " error] " + msg);
@@ -314,10 +314,16 @@ namespace syntax {
314314 CLASS (const CLASS &) = delete ; \
315315 CLASS &operator =(const CLASS &) = delete
316316
317+ #define BASIC_INFO_PARMDECL \
318+ utils::SourceLocation s = utils::SourceLocation(), \
319+ std::unordered_set<std::string> f = std::unordered_set<std::string>(), \
320+ bool t = false
321+
317322 struct ExprNode {
318323 DELETE_COPY (ExprNode);
319324 virtual ~ExprNode () {}
320- ExprNode (utils::SourceLocation s) : sl(s) {}
325+ ExprNode (BASIC_INFO_PARMDECL)
326+ : sl(s), freeVars(f), tail(t) {}
321327
322328 virtual ExprNode* clone () const = 0;
323329 virtual void traverse (
@@ -342,13 +348,12 @@ namespace syntax {
342348 struct IntegerNode : public ExprNode {
343349 DELETE_COPY (IntegerNode);
344350 virtual ~IntegerNode () {}
345- IntegerNode (utils::SourceLocation s, std::string v) : ExprNode(s), val(std::move(v)) {}
351+ IntegerNode (std::string v, BASIC_INFO_PARMDECL)
352+ : ExprNode(s, f, t), val(std::move(v)) {}
346353
347354 // covariant return type for override
348355 virtual IntegerNode* clone () const override {
349- auto inode = new IntegerNode (sl, val);
350- inode->freeVars = freeVars;
351- inode->tail = tail;
356+ auto inode = new IntegerNode (val, sl, freeVars, tail);
352357 inode->loc = loc;
353358 return inode;
354359 }
@@ -375,13 +380,12 @@ namespace syntax {
375380 struct StringNode : public ExprNode {
376381 DELETE_COPY (StringNode);
377382 virtual ~StringNode () {}
378- StringNode (utils::SourceLocation s, std::string v) : ExprNode(s), val(std::move(v)) {}
383+ StringNode (std::string v, BASIC_INFO_PARMDECL)
384+ : ExprNode(s, f, t), val(std::move(v)) {}
379385
380386 // covariant return type
381387 virtual StringNode* clone () const override {
382- auto snode = new StringNode (sl, val);
383- snode->freeVars = freeVars;
384- snode->tail = tail;
388+ auto snode = new StringNode (val, sl, freeVars, tail);
385389 snode->loc = loc;
386390 return snode;
387391 }
@@ -407,12 +411,11 @@ namespace syntax {
407411 struct VariableNode : public ExprNode {
408412 DELETE_COPY (VariableNode);
409413 virtual ~VariableNode () {}
410- VariableNode (utils::SourceLocation s, std::string n) : ExprNode(s), name(std::move(n)) {}
414+ VariableNode (std::string n, BASIC_INFO_PARMDECL)
415+ : ExprNode(s, f, t), name(std::move(n)) {}
411416
412417 virtual VariableNode* clone () const override {
413- auto vnode = new VariableNode (sl, name);
414- vnode->freeVars = freeVars;
415- vnode->tail = tail;
418+ auto vnode = new VariableNode (name, sl, freeVars, tail);
416419 return vnode;
417420 }
418421 virtual void traverse (
@@ -441,19 +444,16 @@ namespace syntax {
441444 }
442445 delete expr;
443446 }
444- LambdaNode (utils::SourceLocation s, std::vector<VariableNode*> v, ExprNode* e) :
445- ExprNode (s), varList(std::move(v)), expr(e) {
446- }
447+ LambdaNode (std::vector<VariableNode*> v, ExprNode* e, BASIC_INFO_PARMDECL) :
448+ ExprNode (s, f, t), varList(std::move(v)), expr(e) {}
447449
448450 virtual LambdaNode* clone () const override {
449451 std::vector<VariableNode*> newVarList;
450452 for (auto v : varList) {
451453 newVarList.push_back (v->clone ());
452454 }
453455 ExprNode* newExpr = expr->clone ();
454- auto lnode = new LambdaNode (sl, std::move (newVarList), newExpr);
455- lnode->freeVars = freeVars;
456- lnode->tail = tail;
456+ auto lnode = new LambdaNode (std::move (newVarList), newExpr, sl, freeVars, tail);
457457 return lnode;
458458 }
459459 virtual void traverse (
@@ -517,10 +517,9 @@ namespace syntax {
517517 delete expr;
518518 }
519519 LetrecNode (
520- utils::SourceLocation s,
521- std::vector<std::pair<VariableNode*, ExprNode*>> v, ExprNode* e) :
522- ExprNode (s), varExprList(std::move(v)), expr(e) {
523- }
520+ std::vector<std::pair<VariableNode*, ExprNode*>> v, ExprNode* e,
521+ BASIC_INFO_PARMDECL) :
522+ ExprNode (s, f, t), varExprList(std::move(v)), expr(e) {}
524523
525524 virtual LetrecNode* clone () const override {
526525 std::vector<std::pair<VariableNode*, ExprNode*>> newVarExprList;
@@ -529,9 +528,7 @@ namespace syntax {
529528 newVarExprList.push_back (std::make_pair (ve.first ->clone (), ve.second ->clone ()));
530529 }
531530 ExprNode* newExpr = expr->clone ();
532- auto lnode = new LetrecNode (sl, std::move (newVarExprList), newExpr);
533- lnode->freeVars = freeVars;
534- lnode->tail = tail;
531+ auto lnode = new LetrecNode (std::move (newVarExprList), newExpr, sl, freeVars, tail);
535532 return lnode;
536533 }
537534 virtual void traverse (
@@ -600,15 +597,13 @@ namespace syntax {
600597 delete branch1;
601598 delete branch2;
602599 }
603- IfNode (utils::SourceLocation s, ExprNode* c, ExprNode* b1, ExprNode* b2) :
604- ExprNode (s), cond(c), branch1(b1), branch2(b2) {
605- }
600+ IfNode (ExprNode* c, ExprNode* b1, ExprNode* b2, BASIC_INFO_PARMDECL) :
601+ ExprNode (s, f, t), cond(c), branch1(b1), branch2(b2) {}
606602
607603 virtual IfNode* clone () const override {
608604 // the evaluation order of the three clones are irrelevant
609- auto inode = new IfNode (sl, cond->clone (), branch1->clone (), branch2->clone ());
610- inode->freeVars = freeVars;
611- inode->tail = tail;
605+ auto inode = new IfNode (cond->clone (), branch1->clone (), branch2->clone (),
606+ sl, freeVars, tail);
612607 return inode;
613608 }
614609 virtual void traverse (
@@ -659,18 +654,15 @@ namespace syntax {
659654 delete e;
660655 }
661656 }
662- SequenceNode (utils::SourceLocation s, std::vector<ExprNode*> e) :
663- ExprNode (s), exprList(std::move(e)) {
664- }
657+ SequenceNode (std::vector<ExprNode*> e, BASIC_INFO_PARMDECL) :
658+ ExprNode (s, f, t), exprList(std::move(e)) {}
665659
666660 virtual SequenceNode* clone () const override {
667661 std::vector<ExprNode*> newExprList;
668662 for (auto e : exprList) {
669663 newExprList.push_back (e->clone ());
670664 }
671- auto snode = new SequenceNode (sl, std::move (newExprList));
672- snode->freeVars = freeVars;
673- snode->tail = tail;
665+ auto snode = new SequenceNode (std::move (newExprList), sl, freeVars, tail);
674666 return snode;
675667 }
676668 virtual void traverse (
@@ -728,18 +720,16 @@ namespace syntax {
728720 delete a;
729721 }
730722 }
731- IntrinsicCallNode (utils::SourceLocation s, std::string i, std::vector<ExprNode*> a) :
732- ExprNode (s), intrinsic(std::move(i)), argList(std::move(a)) {
733- }
723+ IntrinsicCallNode (std::string i, std::vector<ExprNode*> a, BASIC_INFO_PARMDECL):
724+ ExprNode (s, f, t), intrinsic(std::move(i)), argList(std::move(a)) {}
734725
735726 virtual IntrinsicCallNode* clone () const override {
736727 std::vector<ExprNode*> newArgList;
737728 for (auto a : argList) {
738729 newArgList.push_back (a->clone ());
739730 }
740- auto inode = new IntrinsicCallNode (sl, intrinsic, std::move (newArgList));
741- inode->freeVars = freeVars;
742- inode->tail = tail;
731+ auto inode = new IntrinsicCallNode (
732+ intrinsic, std::move (newArgList), sl, freeVars, tail);
743733 return inode;
744734 }
745735 virtual void traverse (
@@ -794,19 +784,16 @@ namespace syntax {
794784 delete a;
795785 }
796786 }
797- ExprCallNode (utils::SourceLocation s, ExprNode* e, std::vector<ExprNode*> a) :
798- ExprNode (s), expr(e), argList(std::move(a)) {
799- }
787+ ExprCallNode (ExprNode* e, std::vector<ExprNode*> a, BASIC_INFO_PARMDECL) :
788+ ExprNode (s, f, t), expr(e), argList(std::move(a)) {}
800789
801790 virtual ExprCallNode* clone () const override {
802791 ExprNode* newExpr = expr->clone ();
803792 std::vector<ExprNode*> newArgList;
804793 for (auto a : argList) {
805794 newArgList.push_back (a->clone ());
806795 }
807- auto enode = new ExprCallNode (sl, newExpr, std::move (newArgList));
808- enode->freeVars = freeVars;
809- enode->tail = tail;
796+ auto enode = new ExprCallNode (newExpr, std::move (newArgList), sl, freeVars, tail);
810797 return enode;
811798 }
812799 virtual void traverse (
@@ -863,14 +850,12 @@ namespace syntax {
863850 delete var;
864851 delete expr;
865852 }
866- AtNode (utils::SourceLocation s, VariableNode* v, ExprNode* e) :
867- ExprNode (s), var(v), expr(e) {}
853+ AtNode (VariableNode* v, ExprNode* e, BASIC_INFO_PARMDECL ) :
854+ ExprNode (s, f, t ), var(v), expr(e) {}
868855
869856 virtual AtNode* clone () const override {
870857 // the evaluation order of the two clones are irrelevant
871- auto anode = new AtNode (sl, var->clone (), expr->clone ());
872- anode->freeVars = freeVars;
873- anode->tail = tail;
858+ auto anode = new AtNode (var->clone (), expr->clone (), sl, freeVars, tail);
874859 return anode;
875860 }
876861 virtual void traverse (
@@ -907,6 +892,7 @@ namespace syntax {
907892 }
908893 };
909894
895+ #undef BASIC_INFO_PARMDECL
910896#undef DELETE_COPY
911897
912898 ExprNode* parse (std::deque<Token> tokens) {
@@ -954,15 +940,15 @@ namespace syntax {
954940
955941 parseInteger = [&]() -> IntegerNode* {
956942 auto token = consume (isIntegerToken);
957- return new IntegerNode (token.sl , token.text );
943+ return new IntegerNode (token.text , token.sl );
958944 };
959945 parseString = [&]() -> StringNode* { // don't unquote here: AST keeps raw tokens
960946 auto token = consume (isStringToken);
961- return new StringNode (token.sl , token.text );
947+ return new StringNode (token.text , token.sl );
962948 };
963949 parseVariable = [&]() -> VariableNode* {
964950 auto token = consume (isVariableToken);
965- return new VariableNode (token. sl , std::move (token.text ));
951+ return new VariableNode (std::move (token.text ), token. sl );
966952 };
967953 parseLambda = [&]() -> LambdaNode* {
968954 auto start = consume (isTheToken (" lambda" ));
@@ -973,7 +959,7 @@ namespace syntax {
973959 }
974960 consume (isTheToken (" )" ));
975961 auto expr = parseExpr ();
976- return new LambdaNode (start. sl , std::move (varList), expr);
962+ return new LambdaNode (std::move (varList), expr, start. sl );
977963 };
978964 parseLetrec = [&]() -> LetrecNode* {
979965 auto start = consume (isTheToken (" letrec" ));
@@ -987,14 +973,14 @@ namespace syntax {
987973 }
988974 consume (isTheToken (" )" ));
989975 auto expr = parseExpr ();
990- return new LetrecNode (start. sl , std::move (varExprList), expr);
976+ return new LetrecNode (std::move (varExprList), expr, start. sl );
991977 };
992978 parseIf = [&]() -> IfNode* {
993979 auto start = consume (isTheToken (" if" ));
994980 auto cond = parseExpr ();
995981 auto branch1 = parseExpr ();
996982 auto branch2 = parseExpr ();
997- return new IfNode (start. sl , cond, branch1, branch2);
983+ return new IfNode (cond, branch1, branch2, start. sl );
998984 };
999985 parseSequence = [&]() -> SequenceNode* {
1000986 auto start = consume (isTheToken (" {" ));
@@ -1006,7 +992,7 @@ namespace syntax {
1006992 utils::panic (" parser" , " zero-length sequence" , start.sl );
1007993 }
1008994 consume (isTheToken (" }" ));
1009- return new SequenceNode (start. sl , std::move (exprList));
995+ return new SequenceNode (std::move (exprList), start. sl );
1010996 };
1011997 parseIntrinsicCall = [&]() -> IntrinsicCallNode* {
1012998 auto start = consume (isTheToken (" (" ));
@@ -1016,7 +1002,7 @@ namespace syntax {
10161002 argList.push_back (parseExpr ());
10171003 }
10181004 consume (isTheToken (" )" ));
1019- return new IntrinsicCallNode (start. sl , std::move (intrinsic.text ), std::move (argList));
1005+ return new IntrinsicCallNode (std::move (intrinsic.text ), std::move (argList), start. sl );
10201006 };
10211007 parseExprCall = [&]() -> ExprCallNode* {
10221008 auto start = consume (isTheToken (" (" ));
@@ -1026,13 +1012,13 @@ namespace syntax {
10261012 argList.push_back (parseExpr ());
10271013 }
10281014 consume (isTheToken (" )" ));
1029- return new ExprCallNode (start. sl , expr, std::move (argList));
1015+ return new ExprCallNode (expr, std::move (argList), start. sl );
10301016 };
10311017 parseAt = [&]() -> AtNode* {
10321018 auto start = consume (isTheToken (" @" ));
10331019 auto var = parseVariable ();
10341020 auto expr = parseExpr ();
1035- return new AtNode (start. sl , var, expr);
1021+ return new AtNode (var, expr, start. sl );
10361022 };
10371023 parseExpr = [&]() -> ExprNode* {
10381024 if (!tokens.size ()) {
@@ -1146,8 +1132,7 @@ namespace runtime {
11461132 bool f = false ,
11471133 int p = 0 ,
11481134 std::vector<syntax::Location> l = std::vector<syntax::Location>()) :
1149- frame (f), env(std::move(e)), expr(x), pc(p), local(std::move(l)) {
1150- }
1135+ frame (f), env(std::move(e)), expr(x), pc(p), local(std::move(l)) {}
11511136
11521137 // whether this is a frame
11531138 bool frame;
0 commit comments