diff --git a/sources/zweic/Parser.scala b/sources/zweic/Parser.scala index 7793d52..3596627 100755 --- a/sources/zweic/Parser.scala +++ b/sources/zweic/Parser.scala @@ -318,7 +318,7 @@ if ( token == ADD || token == SUB ) { return Binop(sumOp(), left, sumExpression()); } else { - if ( token == OR ) { + if (check(OR)) { return Unop(Operators.NOT, Binop(Operators.AND, Unop(Operators.NOT, left), @@ -378,9 +378,12 @@ case NULLFACTOR => nextToken; rval = NullLit(); case READINT => nextToken; rval = ReadInt(); case READCHAR => nextToken; rval = ReadChar(); - case LPAREN => nextToken; rval = expression(); accept(RPAREN); + case LPAREN => + nextToken; rval = expression(); + rval.brackets = true; accept(RPAREN); case LACCOLADE => rval = block(); - case NEW => nextToken; accept(IDENT); rval = New(Name(chars), params()); + case NEW => nextToken; accept(IDENT); + rval = New(Name(chars), params()); case _ => error("Identifier, Number, String, 'true', 'false', 'this', 'null', 'readInt', 'readChar', '(Expression)', '{Block}' or 'new'"); } diff --git a/sources/zweic/Printer.scala b/sources/zweic/Printer.scala index f0d9aaf..f76db90 100755 --- a/sources/zweic/Printer.scala +++ b/sources/zweic/Printer.scala @@ -27,32 +27,34 @@ */ private var level = 0; - def joinprint(args:List[Tree], sep:String):Printer = args match { + def joinprint(args:List[Tree], sep: String):Printer = args match { case h :: Nil => print(h); case h :: t => - print(h).print(sep); + print(h).print(sep); joinprint(t, sep); case Nil => print(""); } - //TODO: do this by passing a function as argument. - def joinprintln(args:List[Tree]):Printer = args match { + + def joinprint(args:List[Tree]):Printer = args match { case h :: Nil => print(h); case h :: t => - print(h); - println; - joinprintln(t); + print(h).println; + joinprint(t); case Nil => print(""); } - /** * Prints a tree node. */ - def print(tree: Tree): Printer = tree match { + def print(tree: Tree): Printer = { + if (tree.isInstanceOf[Expr] && tree.asInstanceOf[Expr].brackets) + print("("); + + tree match { case Program(decls, main) => for (val d <- decls) { print(d).println; @@ -70,7 +72,7 @@ case _ => null; } print(" {").indent.println; - joinprintln(members); + joinprint(members); undent.println.print("}").println; case FieldDecl(formal) => @@ -103,7 +105,7 @@ case While(cond, stats) => print("while (").print(cond).print(") {"); - indent.println.joinprintln(stats).undent.println.print("}"); + indent.println.joinprint(stats).undent.println.print("}"); case Var(varname, vartype, init) => print(vartype).print(" ").print(varname).print(" = "). @@ -177,8 +179,13 @@ case x => error("unexpected error in tree"); - } - + }; + + if (tree.isInstanceOf[Expr] && tree.asInstanceOf[Expr].brackets) + print(")"); + + print(""); + } /** * Print abstract syntax trees separated by commas. diff --git a/sources/zweic/Tree.scala b/sources/zweic/Tree.scala index 758024d..cb83d99 100755 --- a/sources/zweic/Tree.scala +++ b/sources/zweic/Tree.scala @@ -104,7 +104,9 @@ // Tree nodes for expressions /* A common superclass for expressions */ -abstract class Expr extends Tree; +abstract class Expr extends Tree { + var brackets:Boolean = false; +}; /* E = Ident name */ case class Ident(name: Name) extends Expr;