| |
---|
| | case h :: t => |
---|
| | print(h).print(sep); |
---|
| | joinprint(t, sep); |
---|
| | case Nil => |
---|
| | print("---"); |
---|
| | } |
---|
| | print(""); |
---|
| | } |
---|
| | //TODO: do this by passing a function as argument. |
---|
| | def joinprintln(args:List[Tree]):Printer = args match { |
---|
| | case h :: Nil => |
---|
| | print(h); |
---|
| | case h :: t => |
---|
| | print(h); |
---|
| | println; |
---|
| | joinprintln(t); |
---|
| | case Nil => |
---|
| | print(""); |
---|
| | } |
---|
| | |
---|
| | |
---|
| | /** |
---|
| | * Prints a tree node. |
---|
| | */ |
---|
| | def print(tree: Tree): Printer = tree match { |
---|
| | case Program(decls, main) => |
---|
| | for (val d <- decls) { |
---|
| | print(d).println; |
---|
| | } |
---|
| | print(main).println |
---|
| | |
---|
| | //############################################################################ |
---|
| | // Tree nodes for definitions |
---|
| | print(main).println; |
---|
| | |
---|
| | // |
---|
| | // Tree nodes for definitions |
---|
| | // |
---|
| | |
---|
| | case ClassDef(name, extend, members) => |
---|
| | print("class ").print(name); |
---|
| | extend match { |
---|
| | case Some(e) => print(" extends ").print(e); |
---|
| | case _ => null; |
---|
| | } |
---|
| | print(" {") |
---|
| | indent; println; |
---|
| | for (val m <- members) { |
---|
| | print(m).println; |
---|
| | } |
---|
| | undent; |
---|
| | print("}").println; |
---|
| | print(" {").indent.println; |
---|
| | joinprintln(members); |
---|
| | undent.println.print("}").println; |
---|
| | |
---|
| | case FieldDecl(formal) => |
---|
| | print(formal); |
---|
| | |
---|
| | case MethodDef(name, args, returntype, expression) => |
---|
| | print(returntype).print(" ").print(name).print(" ("); |
---|
| | joinprint(args, ", "); |
---|
| | print(") {").println; |
---|
| | indent; |
---|
| | print(expression).println; |
---|
| | undent; |
---|
| | print("}") |
---|
| | print(returntype).print(" ").print(name); |
---|
| | print(" (").joinprint(args, ", ").print(") "); |
---|
| | print(expression); |
---|
| | |
---|
| | case Formal(name, typ) => |
---|
| | print(typ).print(" ").print(name); |
---|
| | |
---|
| | //############################################################################ |
---|
| | // Tree nodes for types |
---|
| | // |
---|
| | // Tree nodes for types |
---|
| | // |
---|
| | |
---|
| | case IntType() => |
---|
| | print("Int"); |
---|
| | |
---|
| |
---|
| | |
---|
| | case ClassType(name) => |
---|
| | print(name); |
---|
| | |
---|
| | //############################################################################ |
---|
| | // Tree nodes for statements |
---|
| | // |
---|
| | // Tree nodes for statements |
---|
| | // |
---|
| | |
---|
| | case While(cond, stats) => |
---|
| | print("while (").print(cond).print(") {"); |
---|
| | indent; println; |
---|
| | for (val s <- stats) { |
---|
| | print(s).println; |
---|
| | } |
---|
| | undent; |
---|
| | println; |
---|
| | print("}"); |
---|
| | indent.println.joinprintln(stats).undent.println.print("}"); |
---|
| | |
---|
| | case Var(varname, vartype, init) => |
---|
| | print(vartype).print(" ").print(varname).print(" = ").print(init); |
---|
| | print(vartype).print(" ").print(varname).print(" = "). |
---|
| | print(init).print(";"); |
---|
| | |
---|
| | case Set(name, expr) => |
---|
| | print(name).print(" = ").print(expr); |
---|
| | print(name).print(" = ").print(expr).print(";"); |
---|
| | |
---|
| | case Do(expr) => |
---|
| | print(expr); |
---|
| | print(expr).print(";"); |
---|
| | |
---|
| | case PrintInt(expr) => |
---|
| | print("PrintInt(").print(expr).print(")"); |
---|
| | print("printInt(").print(expr).print(")").print(";"); |
---|
| | |
---|
| | case PrintChar(expr) => |
---|
| | print("PrintChar(").print(expr).print(")"); |
---|
| | |
---|
| | //############################################################################ |
---|
| | // Tree nodes for expressions |
---|
| | print("printChar(").print(expr).print(")").print(";"); |
---|
| | |
---|
| | // |
---|
| | // Tree nodes for expressions |
---|
| | // |
---|
| | |
---|
| | case Ident(name) => |
---|
| | print(name) |
---|
| | print(name); |
---|
| | |
---|
| | case New(name, args) => |
---|
| | print("new ").print(name).print("("); |
---|
| | joinprint(args, ", "); |
---|
| |
---|
| | case Unop(op, expr) => |
---|
| | print(op.toString()).print(expr); |
---|
| | |
---|
| | case Binop(op, left, right) => |
---|
| | print(left).print(op.toString()).print(right); |
---|
| | print(left).print(" "); |
---|
| | print(op.toString()); |
---|
| | print(" ").print(right); |
---|
| | |
---|
| | case ReadInt() => |
---|
| | print("ReadInt"); |
---|
| | print("readInt"); |
---|
| | |
---|
| | case ReadChar() => |
---|
| | print("ReadChar"); |
---|
| | print("readChar"); |
---|
| | |
---|
| | case If(cond, stata, statb) => |
---|
| | print("if (").print(cond).print(") {"); |
---|
| | indent; println; |
---|
| | print(stata) |
---|
| | undent; println; |
---|
| | print("} else {") |
---|
| | indent; println; |
---|
| | print(statb) |
---|
| | undent; println; |
---|
| | print("}").println; |
---|
| | print("if (").print(cond).print(") "); |
---|
| | indent.print(stata).undent.println; |
---|
| | print("else ").indent; |
---|
| | print(statb).undent; |
---|
| | |
---|
| | case Block(stats, expression) => |
---|
| | print("{") |
---|
| | indent; println; |
---|
| | for(val s <- stats) { |
---|
| | print("{").indent.println; |
---|
| | for (val s <- stats) { |
---|
| | print(s).println; |
---|
| | } |
---|
| | print("return ").print(expression) |
---|
| | undent; println; |
---|
| | print("}").println; |
---|
| | } |
---|
| | print("return ").print(expression); |
---|
| | undent.println.print("}"); |
---|
| | |
---|
| | case x => |
---|
| | this |
---|
| | |
---|
| | |
---|
| | error("unexpected error in tree"); |
---|
| | } |
---|
| | |
---|
| | |
---|
| | /** |
---|
| |
---|
| | |