From b2536564bcdb65b25b40ce99e644f391c507a64d Mon Sep 17 00:00:00 2001 From: bill Date: Sat, 20 Feb 2021 23:24:11 +0800 Subject: [PATCH] Cleanup --- src/main/cup/chocopy/pa1/ChocoPy.cup | 21 ++++++++++----------- src/main/jflex/chocopy/pa1/ChocoPy.jflex | 3 --- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/main/cup/chocopy/pa1/ChocoPy.cup b/src/main/cup/chocopy/pa1/ChocoPy.cup index bb26a36..15340a3 100644 --- a/src/main/cup/chocopy/pa1/ChocoPy.cup +++ b/src/main/cup/chocopy/pa1/ChocoPy.cup @@ -316,11 +316,11 @@ class_body_defs ::= class_body_defs:defs var_def:vd {: RESULT = combine(de /* fun_def */ fun_def ::= DEF:def identifier:id LPAR typed_vars:params RPAR ret_type:rt COLON:col NEWLINE INDENT fun_body_decs:fbd stmt_list:sl DEDENT - {: TypeAnnotation _rt = rt;if(rt.getLocation()[0] == -2) _rt = new ClassType( colxright, colxright, "");RESULT = new FuncDef(defxleft, getRight(sl), id, params, _rt, fbd, sl); :} + {: TypeAnnotation _rt = rt;if((rt instanceof ClassType) && ((ClassType)rt).className == "") _rt = new ClassType( colxright, colxright, "");RESULT = new FuncDef(defxleft, getRight(sl), id, params, _rt, fbd, sl); :} ; ret_type ::= ARROW type:t {: RESULT= t; :} - | {: RESULT= new ClassType(new ComplexSymbolFactory.Location(-2,-1), new ComplexSymbolFactory.Location(-1,-1),""); :} + | {: RESULT= new ClassType(null, null,""); :} ; typed_vars ::= typed_var:tv {: RESULT= single(tv); :} @@ -427,11 +427,11 @@ pexpr ::= identifier:id {: RESULT = id; :} | index_expr:i {: RESULT = i; :} | member_expr:m LPAR expr_list:l RPAR:rpar {: RESULT = new MethodCallExpr(mxleft, rparxright, m, l); :} | identifier:id LPAR expr_list:l RPAR:rpar {: RESULT = new CallExpr(idxleft, rparxright, id, l); :} - | pexpr:p1 PLUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} - | pexpr:p1 MINUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} - | pexpr:p1 MUL:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} - | pexpr:p1 DIV:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} - | pexpr:p1 MOD:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} + | pexpr:p1 PLUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} + | pexpr:p1 MINUS:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} + | pexpr:p1 MUL:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} + | pexpr:p1 DIV:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} + | pexpr:p1 MOD:bo pexpr:p2 {: RESULT = new BinaryExpr(p1xleft, p2xright, p1, bo, p2); :} | MINUS:m pexpr:p {: RESULT = new UnaryExpr(mxleft, pxright, m, p); :} ; @@ -440,17 +440,16 @@ expr_list ::= expr:e {: RESULT = single(e); | {: RESULT = new ArrayList(); :} ; - -/* bin_op */ +/* bin_op */ //We may still be able to use bin_op, so I left it here. bin_op ::= PLUS:a {: RESULT = new StringLiteral(axleft, axright, "+"); :} | MINUS:a {: RESULT = new StringLiteral(axleft, axright, "-"); :} | MUL:a {: RESULT = new StringLiteral(axleft, axright, "*"); :} - | DIV:a {: RESULT = new StringLiteral(axleft, axright, "/"); :} + | DIV:a {: RESULT = new StringLiteral(axleft, axright, "//"); :} //Section 2.6.3 in chocopy language reference | MOD:a {: RESULT = new StringLiteral(axleft, axright, "%"); :} ; -/* comp_op */ +/* comp_op */ //this might also need some change in order not to break left associativity comp_op ::= EQUAL:a {: RESULT = new StringLiteral(axleft, axright, "=="); :} | NEQ:a {: RESULT = new StringLiteral(axleft, axright, "!="); :} | LEQ:a {: RESULT = new StringLiteral(axleft, axright, "<="); :} diff --git a/src/main/jflex/chocopy/pa1/ChocoPy.jflex b/src/main/jflex/chocopy/pa1/ChocoPy.jflex index 8699d5c..43a7f03 100644 --- a/src/main/jflex/chocopy/pa1/ChocoPy.jflex +++ b/src/main/jflex/chocopy/pa1/ChocoPy.jflex @@ -46,7 +46,6 @@ import java.util.ArrayList; /** Return a terminal symbol of syntactic category TYPE and semantic * value VALUE at the current source location. */ private Symbol symbol(int type, Object value) { - //System.out.println(yytext() + ChocoPyTokens.terminalNames[type]); return symbolFactory.newSymbol(ChocoPyTokens.terminalNames[type], type, new ComplexSymbolFactory.Location(yyline + 1, yycolumn + 1), new ComplexSymbolFactory.Location(yyline + 1,yycolumn + yylength()), @@ -136,7 +135,6 @@ if True: AFTER state. */ pop(); - //System.out.println("dedent"); return symbol(ChocoPyTokens.DEDENT, currIndent); } /*Otherwise, we will start dealing with the rest @@ -149,7 +147,6 @@ if True: current level should have, start a new level which will have `currIndent' indents. */ - //System.out.println("indent"); push(currIndent); return symbol(ChocoPyTokens.INDENT, currIndent);