diff --git a/sources/zweic/Analyzer.scala b/sources/zweic/Analyzer.scala index e13a41d..1133356 100755 --- a/sources/zweic/Analyzer.scala +++ b/sources/zweic/Analyzer.scala @@ -25,7 +25,7 @@ // Analyze a program def analyzeProgram(tree: Program): Unit = tree match { - case Program(classes, main) => + case Program(classes, main) => classes.foreach(analyzeClass); analyzeExpr(emptyVarScope, main); () diff --git a/sources/zweic/Parser.scala b/sources/zweic/Parser.scala index 887985e..0c9e37e 100755 --- a/sources/zweic/Parser.scala +++ b/sources/zweic/Parser.scala @@ -15,7 +15,7 @@ var pushedBack: Option[Triple[Int,String,Token]] = None; - var pos: Int = 0; + //var pos = 0; /** * Reports an error that a specific token was expected but @@ -64,11 +64,11 @@ } pushedBack match { case Some(Triple(p, c, t)) => - token = t; - chars = c; - pos = p; - pushedBack = None; - case _ => return super.nextToken; + token = t; + chars = c; + start = p; + pushedBack = None; + case _ => return super.nextToken; } } @@ -78,13 +78,13 @@ case _ => val savedToken = token; val savedChars = chars; - val savedPos = pos; + val savedPos = start; nextToken; val rval = token; - pushedBack = Some(Triple(pos, chars, token)); + pushedBack = Some(Triple(start, chars, token)); token = savedToken; chars = savedChars; - pos = savedPos; + start = savedPos; return rval; } @@ -103,6 +103,7 @@ * Program = { ClassDecl } Expression */ private def program(): Program = { + val pos = start; var classes:List[ClassDef] = Nil; while (token == CLASS) { classes = classDecl() :: classes; @@ -115,6 +116,7 @@ * ClassDecl = "class" ident ["extends" ident] "{" {Member} "}" */ private def classDecl(): ClassDef = { + val pos = start; accept(CLASS); val name = Name(chars); accept(IDENT); @@ -137,6 +139,7 @@ * "(" [Formal {"," Formal}] ")" Block) */ private def member(): Member = { + val pos = start; val f:Formal = formal(); if (check(SEMICOLON)) { return FieldDecl(f); @@ -160,6 +163,7 @@ * Formal = Type ident */ private def formal(): Formal = { + val pos = start; val typ = type1(); val name = Name(chars); accept(IDENT); @@ -170,6 +174,7 @@ * Block = "{" {Statements} ["return" Expression] "}" */ private def block(): Block = { + val pos = start; accept (LACCOLADE); var s:List[Stat] = Nil; var e:Expr = NullLit().setPos(pos); @@ -186,12 +191,15 @@ /** * Type1 = "Int" | "Null" | ident */ - private def type1(): TypeTree = token match { - case INT => nextToken; return IntType().setPos(pos); - case NULLTYPE => nextToken; return NullType().setPos(pos); - case IDENT => val name=Name(chars); nextToken; - return ClassType(name).setPos(pos); - case _ => error("'Int', 'Null' or identifier token"); return null; + private def type1(): TypeTree = { + val pos = start; + token match { + case INT => nextToken; return IntType().setPos(pos); + case NULLTYPE => nextToken; return NullType().setPos(pos); + case IDENT => val name=Name(chars); nextToken; + return ClassType(name).setPos(pos); + case _ => error("'Int', 'Null' or identifier token"); return null; + } } /** @@ -203,6 +211,7 @@ * | "printChar" "(" Expression ")" ";" */ private def statement(): Stat = { + val pos = start; if (token == IDENT) { peekAhead match { case IDENT => @@ -280,6 +289,7 @@ * Expression = "if" "(" Expression ")" Expression [ "else" Expression ] | CmpExpression */ private def expression(): Expr = { + val pos = start; if ( check(IF) ) { accept(LPAREN); val cond = expression(); @@ -299,6 +309,7 @@ * MyCmpExpression = SumExpression { CompOp SumExpression } */ private def cmpExpression(): Expr = { + val pos = start; var left = sumExpression(); while ( token == EQ || token == NE || token == LT || token == GT || token == LE || token == GE ) { @@ -314,6 +325,7 @@ * MySumExpression = Term { SumOp Term } */ private def sumExpression(): Expr = { + val pos = start; var left = term(); while ( token == ADD || token == SUB || token == OR ) { @@ -337,6 +349,7 @@ * MyTerm = [ NegateOp ] Factor { ProdOp [ NegateOp ] Factor } */ private def term(): Expr = { + val pos = start; var left:Expr = _negFactor(); while ( token == MUL || token == DIV || token == MOD || token == AND ) { @@ -347,7 +360,8 @@ return left; } - private def _negFactor(): Expr = + private def _negFactor(): Expr = { + val pos = start; if ( token == SUB || token == NOT ) { val op = Operators.token2op(token); nextToken; @@ -357,8 +371,10 @@ } else { return factor(); } + } private def _string(str: String): New = { + val pos = start; if ( str.length() > 1 ) { return New(Name("Cons"), IntLit(str.charAt(0).asInstanceOf[Int]).setPos(pos)::_string(str.substring(1))::Nil).setPos(pos); } else { @@ -370,6 +386,7 @@ * Factor = look it up yourself! */ private def factor(): Expr = { + val pos = start; var name = chars; var rval:Expr = null; rval = token match { diff --git a/sources/zweic/Printer.scala b/sources/zweic/Printer.scala index 9afc8d3..ffa9748 100755 --- a/sources/zweic/Printer.scala +++ b/sources/zweic/Printer.scala @@ -61,18 +61,18 @@ case ClassDef(name, extend, members) => print("class ").print(name); extend match { - case Some(e) => print(" extends ").print(e); - case _ => null; + case Some(e) => print(" extends ").print(e); + case _ => null; } print(" {").indent.println; joinprint(members, println.println); undent.println.print("}").println; - case FieldDecl(formal) => + case FieldDecl(formal) => print(formal).print(";"); - case MethodDef(name, args, returntype, expression) => - print(returntype).print(" ").print(name); + case MethodDef(name, args, returntype, expression) => + print(returntype).print(" ").print(name); print(" (").joinprint(args, ", ").print(") "); print(expression); diff --git a/sources/zweic/Scanner.scala b/sources/zweic/Scanner.scala index b374762..51a0f33 100755 --- a/sources/zweic/Scanner.scala +++ b/sources/zweic/Scanner.scala @@ -96,12 +96,11 @@ def nextToken: Unit = { // initialize the position of the current token buf.setLength (0); - start = Position.encode(line, column); // skip comments and whitespace characters while (Character.isWhitespace (ch) || ch == '/') { if (debug == true && ch == '\n'){ - Console.println ("") + Console.println ("") } if (ch == '/'){ nextCh; @@ -112,15 +111,17 @@ } } else { // division + start = Position.encode(line, column); token = DIV; return } } else { - // whitespace + // whitespace nextCh; } } + start = Position.encode(line, column); // read the current token token = readToken; }