diff --git a/sources/zweic/Parser.scala b/sources/zweic/Parser.scala index 0f18abc..3974ef7 100755 --- a/sources/zweic/Parser.scala +++ b/sources/zweic/Parser.scala @@ -95,8 +95,7 @@ def parse: Program = { val result = program(); accept(EOF); - //return result; - return null; + return result; } /* --------------------------------------------------*/ @@ -105,27 +104,32 @@ * Program = { ClassDecl } Expression */ private def program(): Program = { - while (token == CLASS) { - classDecl(); + var classes:List[ClassDef] = Nil; + while (token == CLASS) { + classDecl() :: classes; } - expression(); - return null; + var main = expression(); + return Program(classes.reverse, main); } /** * ClassDecl = "class" ident ["extends" ident] "{" {Member} "}" */ - private def classDecl(): Tree = { + private def classDecl(): ClassDef = { accept(CLASS); + val name = Name(chars); accept(IDENT); + var extend:Option[Name] = None; if (check (EXTENDS)) { + extend = Some(Name(chars)); accept(IDENT); } accept (LACCOLADE); + var members:List[Member] = Nil; while ( !check(RACCOLADE) ) { - member(); + members = member() :: members; } - return null; + return ClassDef (name, extend, members.reverse); } /** @@ -133,57 +137,61 @@ * (";" | * "(" [Formal {"," Formal}] ")" Block) */ - private def member(): Tree = { - formal(); - if (check (SEMICOLON)) { - return null; + private def member(): Member = { + val f:Formal = formal(); + if (check(SEMICOLON)) { + return FieldDecl(f); } else { - accept (LPAREN); + accept(LPAREN); //TODO: is this neccessary? + var par:List[Formal] = Nil; if (token == IDENT || token == INT || token == NULLTYPE) { - formal(); + par = formal() :: par; while (token != RPAREN) { accept (PERIOD); - formal(); + par = formal() :: par; } } accept (RPAREN); - block(); - return null; + val b:Block = block(); + return MethodDef (f.name, par.reverse, f.typ, b); } } /** * Formal = Type ident */ - private def formal(): Tree = { - type1(); + private def formal(): Formal = { + var typ = type1(); + var name = Name(chars); accept(IDENT); - return null; + return Formal(name, typ); } /** * Block = "{" {Statements} ["return" Expression] "}" */ - private def block(): Tree = { + private def block(): Block = { accept (LACCOLADE); + var s:List[Stat] = Nil; + var e:Expr = null; while (token != RETURN && token != RACCOLADE) { - statement(); + s = statement() :: s; } if (check (RETURN)) { - expression(); + e = expression(); } accept(RACCOLADE); - return null; + return Block(s.reverse, e); } /** * Type1 = "Int" | "Null" | ident */ - private def type1(): Tree = token match { - case INT => nextToken; return null; - case NULLTYPE => nextToken; return null; - case IDENT => nextToken; return null; + private def type1(): TypeTree = token match { + case INT => nextToken; return IntType(); + case NULLTYPE => nextToken; return NullType(); + case IDENT => var name=Name(chars); nextToken; return ClassType(name); case _ => error("'Int', 'Null' or identifier token"); return null; } @@ -195,82 +203,92 @@ * | "printInt" "(" Expression ")" ";" * | "printChar" "(" Expression ")" ";" */ - private def statement(): Tree = { + private def statement(): Stat = { if (token == IDENT) { peekAhead match { case IDENT => //type definition and affection - formal(); + val v = formal(); accept(EQUALS); - expression(); + val e = expression(); accept(SEMICOLON); + return Var(v.name, v.typ, e); case EQUALS => //affection + val name = Name(chars); accept(IDENT); accept(EQUALS); - expression(); + val e = expression(); accept(SEMICOLON); + return Set(name, e); case _ => //expression - expression(); - accept(SEMICOLON); + val e = expression(); + accept(SEMICOLON); + return Do(e); } } else { if (check(WHILE)) { //while accept(LPAREN); - expression(); + val cond = expression(); accept(RPAREN); accept(LACCOLADE); + var stats:List[Stat] = Nil; while ( !check(RACCOLADE) ) { - statement(); + stats = statement() :: stats; } + return While(cond, stats.reverse); } else if (token == NULLTYPE || token == INT) { //type definition and affection - formal(); + val v = formal(); accept(EQUALS); - expression(); + val e = expression(); accept(SEMICOLON); + return Var(v.name, v.typ, e); } else if (check(PRINTINT)) { //printInt accept(LPAREN); - expression(); + val e = expression(); accept(RPAREN); accept(SEMICOLON); + return PrintInt(e); } else if (check(PRINTCHAR)) { //printChar accept(LPAREN); - expression(); + val e = expression(); accept(RPAREN); accept(SEMICOLON); + return PrintChar(e); } else { - //expression - expression(); - accept(SEMICOLON); + //expression + val e = expression(); + accept(SEMICOLON); + return Do(e); } } - return null; } /** * Expression = "if" "(" Expression ")" Expression [ "else" Expression ] | CmpExpression */ - private def expression(): Tree = { + private def expression(): Expr = { if ( check(IF) ) { accept(LPAREN); - expression(); + val cond = expression(); accept(RPAREN); - expression(); + val stata = expression(); + var statb:Expr = NullLit(); if ( check(ELSE) ) { - expression(); + statb = expression(); } - return null; + return If(cond, stata, statb); } else { cmpExpression(); return null; @@ -281,49 +299,57 @@ * CmpExpression = [ SumExpression CompOp ] SumExpression * MyCmpExpression = SumExpression [ CompOp SumExpression ] */ - private def cmpExpression(): Tree = { - sumExpression(); + private def cmpExpression(): Expr = { + var left = sumExpression(); if ( token == EQ || token == NE || token == LT || token == GT || token == LE || token == GE ) { - compOp(); - sumExpression(); + val op = compOp(); + val right = sumExpression(); + return Binop(op, left, right); } - return null; + return left; } /** * SumExpression = Term | SumExpression SumOp Term * MySumExpression = Term [ SumOp SumExpression ] */ - private def sumExpression(): Tree = { - term(); + private def sumExpression(): Expr = { + val left = term(); if ( token == ADD || token == SUB || token == OR ) { - sumOp(); - sumExpression(); + val op = sumOp(); + val right = sumExpression(); + return Binop(op, left, right); } - return null; + return left; } /** * Term = [ Term ProdOp ] [ NegateOp ] Factor * MyTerm = [ NegateOp ] Factor [ ProdOp Term ] */ - private def term(): Tree = { + private def term(): Expr = { + var left:Expr; if ( token == SUB || token == NOT ) { + val op = token; nextToken; + val e = factor(); + left = Unop(op, e); + } else { + left = factor(); } - factor(); if ( token == MUL || token == DIV || token == MOD || token == AND ) { - prodOp(); - term(); + val op = prodOp(); + val right = term(); + return Binop(op, left, right); } - return null; + return left; } /** * Factor = look it up yourself! */ - private def factor(): Tree = { + private def factor(): Expr = { token match { case IDENT => nextToken; case NUMBER => nextToken; @@ -353,7 +379,7 @@ /** * Expressions = [ Expression { "," Expression } ] */ - private def expressions(): Tree = { + private def expressions(): List[Expr] = { if ( token == IF || token == SUB || token == NOT || token == IDENT || token == NUMBER || token == STRING || token == TRUE @@ -382,7 +408,7 @@ /** * ProdOp = "*" | "/" | "%" | "&&" */ - private def prodOp(): Tree = token match { + private def prodOp(): Operators = token match { case MUL => nextToken; return null; case DIV => nextToken; return null; case MOD => nextToken; return null; @@ -393,7 +419,7 @@ /** * NegateOp = "-" | "!" */ - private def negateOp(): Tree = token match { + private def negateOp(): Operators = token match { case SUB => nextToken; return null; case NOT => nextToken; return null; case _ => error("'-' or '!' token"); return null; @@ -402,7 +428,7 @@ /** * SumOp = "+" | "-" | "||" */ - private def sumOp(): Tree = token match { + private def sumOp(): Operators = token match { case ADD => nextToken; return null; case SUB => nextToken; return null; case OR => nextToken; return null; @@ -413,7 +439,7 @@ /** * CompOp = "==" | "!" | "<" | ">" | "<=" | ">=" */ - private def compOp(): Tree = token match { + private def compOp(): Operators = token match { case EQ => nextToken; return null; case NE => nextToken; return null; case LT => nextToken; return null; @@ -422,5 +448,4 @@ case GE => nextToken; return null; case _ => error("comparison operator ('==', '!', '<', '>', '<=', '>=')"); return null; } - } diff --git a/sources/zweic/Printer.scala b/sources/zweic/Printer.scala index 4fad2d0..152b966 100755 --- a/sources/zweic/Printer.scala +++ b/sources/zweic/Printer.scala @@ -43,8 +43,7 @@ /** * Print abstract syntax trees separated by commas. */ - private def print(trees: List[Tree]): Printer = trees match { - // ... � compl�ter ... + private def print(trees: List[Tree]): Printer = trees match { case _ => this } diff --git a/sources/zweic/Tree.scala b/sources/zweic/Tree.scala index e320a6f..234698b 100755 --- a/sources/zweic/Tree.scala +++ b/sources/zweic/Tree.scala @@ -47,8 +47,8 @@ /* A common superclass for member definitions */ abstract class Member extends Def; -/* M = FieldDecl name T */ -case class FieldDecl(name: Name, typ: TypeTree) extends Member; +/* M = FieldDecl formal */ +case class FieldDecl(formal: Formal) extends Member; /* D = MethodDecl name { F } T E */ case class MethodDef(name: Name, args: List[Formal],