Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created November 17, 2012 12:33
Show Gist options
  • Save joseanpg/4095604 to your computer and use it in GitHub Desktop.
Save joseanpg/4095604 to your computer and use it in GitHub Desktop.
Understanding cool-tree.java
{- This is mine -}
data Symbol = Symbol { id :: Int
, str :: String
}
data Program = Program { classes :: Classes}
data Class_ = Class_ { name :: Symbol
, parent :: Symbol
, features :: Features
, filename :: Symbol
}
data Feature = Method { name :: Symbol
, formals :: Formals
, return_type :: Symbol
, expr :: Expression
}
| Attr { name :: Symbol
, type_decl :: Symbol
, init :: Expression
}
data Formal = Formal { name :: Symbol
, type_decl: Symbol
}
data Case = Branch { name :: Symbol
, type_decl :: Symbol
, expr :: Expression
}
data Expression = Assign { name :: Symbol
, expr :: Expression
}
| Static_dispatch { expr :: Expression
, type_name :: Symbol
, name :: Symbol
, actual :: Expressions
| Dispatch { expr :: Expression
, name :: Symbol
, actual :: Expressions
| Cond { pred :: Expression
, then_exp :: Expression
, else_exp :: Expression
| Loop { pred :: Expression
, body :: Expression
}
| Typcase { expr :: Expression
, cases :: Cases
}
| Block { body :: Expressions }
| Let { identifier :: Symbol
, type_decl :: Symbol
, init :: Expression
, body :: Expression
}
| Plus { e1 :: Expression
, e2 :: Expression
}
| Sub { e1 :: Expression
, e2 :: Expression
}
| Mul { e1 :: Expression
, e2: Expression
}
| Divide { e1 :: Expression
, e2: Expression
}
| Neg { e1 :: Expression }
| Lt { e1 :: Expression
, e2 :: Expression
}
| Eq { e1 :: Expression
, e2 :: Expression
}
| Leq { e1 :: Expression
, e2 :: Expression
}
| Comp { e1 :: Expression}
| Int_const { token :: Symbol }
| Bool_const { val :: Boolean }
| String_const { token :: Symbol }
| New_ { type_name :: Symbol }
| Isvoid { e1 :: Expression }
| No_expr
| Object { name :: Symbol }
type Classes = [Class_]
type Features = [Feature]
type Formals = [Formal]
type Expressions = [Expression]
type Cases = [Case]

/* Copyright (c) 2000 The Regents of the University of California. All rights reserved.

Permission to use, copy, modify, and distribute this software for any purpose, without fee, and without written agreement is hereby granted, provided that the above copyright notice and the following two paragraphs appear in all copies of this software.

IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */

abstract class ListNode extends TreeNode {
private Vector elements;
public abstract Class getElementClass();
protected ListNode(int lineNumber, Vector elements) {
super(lineNumber);
this.elements = elements;
}
protected ListNode(int lineNumber) {
super(lineNumber);
elements = new Vector();
}
protected Vector copyElements() {
Vector cp = new Vector();
for (int i = 0; i < elements.size(); i++) {
cp.addElement(((TreeNode)elements.elementAt(i)).copy());
}
return cp;
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n));
out.print("list\n");
for (int i = 0; i < getLength(); i++) {
getNth(i).dump(out, n + 2);
}
out.print(Utilities.pad(n));
out.print("(end_of_list)\n");
}
public TreeNode getNth(int n) { return (TreeNode)elements.elementAt(n);}
public int getLength() { return elements.size();}
public Enumeration getElements() { return elements.elements();}
public void addElement(TreeNode node) { elements.addElement(node);}
public String toString() { return elements.toString();}
}
class Classes extends ListNode {
public final static Class elementClass = Class_.class;
protected Classes(int lineNumber, Vector elements) {super(lineNumber, elements);}
public Class getElementClass() {return elementClass;}
public Classes(int lineNumber) {super(lineNumber);}
public Classes appendElement(TreeNode elem) {addElement(elem);return this;}
public TreeNode copy() {return new Classes(lineNumber, copyElements());}
}
class Features extends ListNode {
public final static Class elementClass = Feature.class;
protected Features(int lineNumber, Vector elements) {super(lineNumber, elements);}
public Class getElementClass() {return elementClass;}
public Features(int lineNumber) {super(lineNumber);}
public Features appendElement(TreeNode elem) {addElement(elem);return this;}
public TreeNode copy() {return new Features(lineNumber, copyElements());}
}
class Formals extends ListNode {
public final static Class elementClass = Formal.class;
protected Formals(int lineNumber, Vector elements) { super(lineNumber, elements);}
public Class getElementClass() { return elementClass;}
public Formals(int lineNumber) {super(lineNumber);}
public Formals appendElement(TreeNode elem) {addElement(elem);return this;}
public TreeNode copy() { return new Formals(lineNumber, copyElements());}
}
class Expressions extends ListNode {
public final static Class elementClass = Expression.class;
protected Expressions(int lineNumber, Vector elements) {super(lineNumber, elements);}
public Class getElementClass() {return elementClass;}
public Expressions(int lineNumber) {super(lineNumber);}
public Expressions appendElement(TreeNode elem) {addElement(elem);return this;}
public TreeNode copy() { return new Expressions(lineNumber, copyElements());}
}
class Cases extends ListNode {
public final static Class elementClass = Case.class;
protected Cases(int lineNumber, Vector elements) { super(lineNumber, elements);}
public Class getElementClass() {return elementClass;}
public Cases(int lineNumber) {super(lineNumber);}
public Cases appendElement(TreeNode elem) {addElement(elem);return this;}
public TreeNode copy() { return new Cases(lineNumber, copyElements());}
}
abstract class TreeNode {
protected int lineNumber;
protected TreeNode(int lineNumber) {
this.lineNumber = lineNumber;
}
public abstract TreeNode copy();
public abstract void dump(PrintStream out, int n);
public TreeNode set(TreeNode other) {
this.lineNumber = other.lineNumber;
return this;
}
public int getLineNumber() {return lineNumber;}
protected Boolean copy_Boolean(Boolean b) { return new Boolean(b.booleanValue());}
protected AbstractSymbol copy_AbstractSymbol(AbstractSymbol sym) { return sym;}
protected void dump_Boolean(PrintStream out, int n, Boolean b) {
out.print(Utilities.pad(n));
out.println(b.booleanValue() ? "1" : "0");
}
protected void dump_AbstractSymbol(PrintStream out, int n, AbstractSymbol sym) {
out.print(Utilities.pad(n));
out.println(sym.getString());
}
protected void dump_line(PrintStream out, int n) {
out.println(Utilities.pad(n) + "#" + lineNumber);
}
}
abstract class Program extends TreeNode {
protected Program(int lineNumber) {super(lineNumber);}
public abstract void dump_with_types(PrintStream out, int n);
public abstract void semant();
}
class programc extends Program {
protected Classes classes;
public programc(int lineNumber, Classes a1) {
super(lineNumber);
classes = a1;
}
public TreeNode copy() {
return new programc(lineNumber, (Classes)classes.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "programc\n");
classes.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_program");
for (Enumeration e = classes.getElements(); e.hasMoreElements(); ) {
// sm: changed 'n + 1' to 'n + 2' to match changes elsewhere
((Class_)e.nextElement()).dump_with_types(out, n + 2);
}
}
public void semant() {
ClassTable classTable = new ClassTable(classes);
for (Enumeration e = classes.getElements(); e.hasMoreElements(); ) {
class_c c = (class_c) e.nextElement();
System.out.println(c.getName()+":"+c.getParent());
}
if (classTable.errors()) {
System.err.println("Compilation halted due to static semantic errors.");
System.exit(1);
}
}
}
//////////////////////////////////////////////////////////////////////////////
/** This class may be used to contain the semantic information such as
* the inheritance graph. You may use it or not as you like: it is only
* here to provide a container for the supplied methods. */
class ClassTable {
public ClassTable(Classes cls) {
semantErrors = 0;
errorStream = System.err;
/* fill this in */
}
public PrintStream semantError(class_c c) { return semantError(c.getFilename(), c);}
public PrintStream semantError(AbstractSymbol filename, TreeNode t) {
errorStream.print(filename + ":" + t.getLineNumber() + ": ");
return semantError();
}
public PrintStream semantError() {
semantErrors++;
return errorStream;
}
public boolean errors() {
return semantErrors != 0;
}
//private members
private int semantErrors;
private PrintStream errorStream;
private void installBasicClasses() {
AbstractSymbol filename
= AbstractTable.stringtable.addString("<basic class>");
// The following demonstrates how to create dummy parse trees to
// refer to basic Cool classes. There's no need for method
// bodies -- these are already built into the runtime system.
// IMPORTANT: The results of the following expressions are
// stored in local variables. You will want to do something
// with those variables at the end of this method to make this
// code meaningful.
// The Object class has no parent class. Its methods are
// cool_abort() : Object aborts the program
// type_name() : Str returns a string representation
// of class name
// copy() : SELF_TYPE returns a copy of the object
class_c Object_class =
new class_c(0,
TreeConstants.Object_,
TreeConstants.No_class,
new Features(0)
.appendElement(new method(0,
TreeConstants.cool_abort,
new Formals(0),
TreeConstants.Object_,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.type_name,
new Formals(0),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.copy,
new Formals(0),
TreeConstants.SELF_TYPE,
new no_expr(0))),
filename);
// The IO class inherits from Object. Its methods are
// out_string(Str) : SELF_TYPE writes a string to the output
// out_int(Int) : SELF_TYPE " an int " " "
// in_string() : Str reads a string from the input
// in_int() : Int " an int " " "
class_c IO_class =
new class_c(0,
TreeConstants.IO,
TreeConstants.Object_,
new Features(0)
.appendElement(new method(0,
TreeConstants.out_string,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Str)),
TreeConstants.SELF_TYPE,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.out_int,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Int)),
TreeConstants.SELF_TYPE,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.in_string,
new Formals(0),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.in_int,
new Formals(0),
TreeConstants.Int,
new no_expr(0))),
filename);
// The Int class has no methods and only a single attribute, the
// "val" for the integer.
class_c Int_class =
new class_c(0,
TreeConstants.Int,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.prim_slot,
new no_expr(0))),
filename);
// Bool also has only the "val" slot.
class_c Bool_class =
new class_c(0,
TreeConstants.Bool,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.prim_slot,
new no_expr(0))),
filename);
// The class Str has a number of slots and operations:
// val the length of the string
// str_field the string itself
// length() : Int returns length of the string
// concat(arg: Str) : Str performs string concatenation
// substr(arg: Int, arg2: Int): Str substring selection
class_c Str_class =
new class_c(0,
TreeConstants.Str,
TreeConstants.Object_,
new Features(0)
.appendElement(new attr(0,
TreeConstants.val,
TreeConstants.Int,
new no_expr(0)))
.appendElement(new attr(0,
TreeConstants.str_field,
TreeConstants.prim_slot,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.length,
new Formals(0),
TreeConstants.Int,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.concat,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Str)),
TreeConstants.Str,
new no_expr(0)))
.appendElement(new method(0,
TreeConstants.substr,
new Formals(0)
.appendElement(new formalc(0,
TreeConstants.arg,
TreeConstants.Int))
.appendElement(new formalc(0,
TreeConstants.arg2,
TreeConstants.Int)),
TreeConstants.Str,
new no_expr(0))),
filename);
/* Do somethind with Object_class, IO_class, Int_class, Bool_class, and Str_class here */
}
}
abstract class Class_ extends TreeNode {
protected Class_(int lineNumber) {super(lineNumber);}
public abstract void dump_with_types(PrintStream out, int n);
}
class class_c extends Class_ {
protected AbstractSymbol name;
protected AbstractSymbol parent;
protected Features features;
protected AbstractSymbol filename;
public class_c(int lineNumber, AbstractSymbol a1
, AbstractSymbol a2
, Features a3
, AbstractSymbol a4) {
super(lineNumber);
name = a1;
parent = a2;
features = a3;
filename = a4;
}
public TreeNode copy() {
return new class_c(lineNumber, copy_AbstractSymbol(name)
, copy_AbstractSymbol(parent)
, (Features)features.copy()
, copy_AbstractSymbol(filename));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "class_c\n");
dump_AbstractSymbol(out, n+2, name);
dump_AbstractSymbol(out, n+2, parent);
features.dump(out, n+2);
dump_AbstractSymbol(out, n+2, filename);
}
public AbstractSymbol getFilename() { return filename; }
public AbstractSymbol getName() { return name; }
public AbstractSymbol getParent() { return parent; }
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_class");
dump_AbstractSymbol(out, n + 2, name);
dump_AbstractSymbol(out, n + 2, parent);
out.print(Utilities.pad(n + 2) + "\"");
Utilities.printEscapedString(out, filename.getString());
out.println("\"\n" + Utilities.pad(n + 2) + "(");
for (Enumeration e = features.getElements(); e.hasMoreElements();) {
((Feature)e.nextElement()).dump_with_types(out, n + 2);
}
out.println(Utilities.pad(n + 2) + ")");
}
}
abstract class Feature extends TreeNode {
protected Feature(int lineNumber) {super(lineNumber);}
public abstract void dump_with_types(PrintStream out, int n);
}
class method extends Feature {
protected AbstractSymbol name;
protected Formals formals;
protected AbstractSymbol return_type;
protected Expression expr;
public method(int lineNumber, AbstractSymbol a1, Formals a2, AbstractSymbol a3, Expression a4) {
super(lineNumber);
name = a1;
formals = a2;
return_type = a3;
expr = a4;
}
public TreeNode copy() {
return new method(lineNumber, copy_AbstractSymbol(name), (Formals)formals.copy(), copy_AbstractSymbol(return_type), (Expression)expr.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "method\n");
dump_AbstractSymbol(out, n+2, name);
formals.dump(out, n+2);
dump_AbstractSymbol(out, n+2, return_type);
expr.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_method");
dump_AbstractSymbol(out, n + 2, name);
for (Enumeration e = formals.getElements(); e.hasMoreElements();) {
((Formal)e.nextElement()).dump_with_types(out, n + 2);
}
dump_AbstractSymbol(out, n + 2, return_type);
expr.dump_with_types(out, n + 2);
}
}
class attr extends Feature {
protected AbstractSymbol name;
protected AbstractSymbol type_decl;
protected Expression init;
public attr(int lineNumber, AbstractSymbol a1, AbstractSymbol a2, Expression a3) {
super(lineNumber);
name = a1;
type_decl = a2;
init = a3;
}
public TreeNode copy() {
return new attr(lineNumber, copy_AbstractSymbol(name), copy_AbstractSymbol(type_decl), (Expression)init.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "attr\n");
dump_AbstractSymbol(out, n+2, name);
dump_AbstractSymbol(out, n+2, type_decl);
init.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_attr");
dump_AbstractSymbol(out, n + 2, name);
dump_AbstractSymbol(out, n + 2, type_decl);
init.dump_with_types(out, n + 2);
}
}
abstract class Formal extends TreeNode {
protected Formal(int lineNumber) {super(lineNumber);}
public abstract void dump_with_types(PrintStream out, int n);
}
class formalc extends Formal {
protected AbstractSymbol name;
protected AbstractSymbol type_decl;
public formalc(int lineNumber, AbstractSymbol a1, AbstractSymbol a2) {
super(lineNumber);
name = a1;
type_decl = a2;
}
public TreeNode copy() {
return new formalc(lineNumber, copy_AbstractSymbol(name), copy_AbstractSymbol(type_decl));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "formalc\n");
dump_AbstractSymbol(out, n+2, name);
dump_AbstractSymbol(out, n+2, type_decl);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_formal");
dump_AbstractSymbol(out, n + 2, name);
dump_AbstractSymbol(out, n + 2, type_decl);
}
}
abstract class Case extends TreeNode {
protected Case(int lineNumber) {super(lineNumber);}
public abstract void dump_with_types(PrintStream out, int n);
}
class branch extends Case {
protected AbstractSymbol name;
protected AbstractSymbol type_decl;
protected Expression expr;
public branch(int lineNumber, AbstractSymbol a1, AbstractSymbol a2, Expression a3) {
super(lineNumber);
name = a1;
type_decl = a2;
expr = a3;
}
public TreeNode copy() {
return new branch(lineNumber, copy_AbstractSymbol(name), copy_AbstractSymbol(type_decl), (Expression)expr.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "branch\n");
dump_AbstractSymbol(out, n+2, name);
dump_AbstractSymbol(out, n+2, type_decl);
expr.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_branch");
dump_AbstractSymbol(out, n + 2, name);
dump_AbstractSymbol(out, n + 2, type_decl);
expr.dump_with_types(out, n + 2);
}
}
abstract class Expression extends TreeNode {
protected Expression(int lineNumber) {super(lineNumber);}
private AbstractSymbol type = null;
public AbstractSymbol get_type() { return type; }
public Expression set_type(AbstractSymbol s) { type = s; return this; }
public abstract void dump_with_types(PrintStream out, int n);
public void dump_type(PrintStream out, int n) {
if (type != null) { out.println(Utilities.pad(n) + ": " + type.getString()); }
else { out.println(Utilities.pad(n) + ": _no_type"); }
}
}
class assign extends Expression {
protected AbstractSymbol name;
protected Expression expr;
public assign(int lineNumber, AbstractSymbol a1, Expression a2) {
super(lineNumber);
name = a1;
expr = a2;
}
public TreeNode copy() {
return new assign(lineNumber, copy_AbstractSymbol(name), (Expression)expr.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "assign\n");
dump_AbstractSymbol(out, n+2, name);
expr.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_assign");
dump_AbstractSymbol(out, n + 2, name);
expr.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class static_dispatch extends Expression {
protected Expression expr;
protected AbstractSymbol type_name;
protected AbstractSymbol name;
protected Expressions actual;
public static_dispatch(int lineNumber, Expression a1, AbstractSymbol a2, AbstractSymbol a3, Expressions a4) {
super(lineNumber);
expr = a1;
type_name = a2;
name = a3;
actual = a4;
}
public TreeNode copy() {
return new static_dispatch(lineNumber, (Expression)expr.copy(), copy_AbstractSymbol(type_name), copy_AbstractSymbol(name), (Expressions)actual.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "static_dispatch\n");
expr.dump(out, n+2);
dump_AbstractSymbol(out, n+2, type_name);
dump_AbstractSymbol(out, n+2, name);
actual.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_static_dispatch");
expr.dump_with_types(out, n + 2);
dump_AbstractSymbol(out, n + 2, type_name);
dump_AbstractSymbol(out, n + 2, name);
out.println(Utilities.pad(n + 2) + "(");
for (Enumeration e = actual.getElements(); e.hasMoreElements();) {
((Expression)e.nextElement()).dump_with_types(out, n + 2);
}
out.println(Utilities.pad(n + 2) + ")");
dump_type(out, n);
}
}
class dispatch extends Expression {
protected Expression expr;
protected AbstractSymbol name;
protected Expressions actual;
public dispatch(int lineNumber, Expression a1, AbstractSymbol a2, Expressions a3) {
super(lineNumber);
expr = a1;
name = a2;
actual = a3;
}
public TreeNode copy() {
return new dispatch(lineNumber, (Expression)expr.copy(), copy_AbstractSymbol(name), (Expressions)actual.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "dispatch\n");
expr.dump(out, n+2);
dump_AbstractSymbol(out, n+2, name);
actual.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_dispatch");
expr.dump_with_types(out, n + 2);
dump_AbstractSymbol(out, n + 2, name);
out.println(Utilities.pad(n + 2) + "(");
for (Enumeration e = actual.getElements(); e.hasMoreElements();) {
((Expression)e.nextElement()).dump_with_types(out, n + 2);
}
out.println(Utilities.pad(n + 2) + ")");
dump_type(out, n);
}
}
class cond extends Expression {
protected Expression pred;
protected Expression then_exp;
protected Expression else_exp;
public cond(int lineNumber, Expression a1, Expression a2, Expression a3) {
super(lineNumber);
pred = a1;
then_exp = a2;
else_exp = a3;
}
public TreeNode copy() {
return new cond(lineNumber, (Expression)pred.copy(), (Expression)then_exp.copy(), (Expression)else_exp.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "cond\n");
pred.dump(out, n+2);
then_exp.dump(out, n+2);
else_exp.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_cond");
pred.dump_with_types(out, n + 2);
then_exp.dump_with_types(out, n + 2);
else_exp.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class loop extends Expression {
protected Expression pred;
protected Expression body;
public loop(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
pred = a1;
body = a2;
}
public TreeNode copy() {
return new loop(lineNumber, (Expression)pred.copy(), (Expression)body.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "loop\n");
pred.dump(out, n+2);
body.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_loop");
pred.dump_with_types(out, n + 2);
body.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class typcase extends Expression {
protected Expression expr;
protected Cases cases;
public typcase(int lineNumber, Expression a1, Cases a2) {
super(lineNumber);
expr = a1;
cases = a2;
}
public TreeNode copy() {
return new typcase(lineNumber, (Expression)expr.copy(), (Cases)cases.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "typcase\n");
expr.dump(out, n+2);
cases.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_typcase");
expr.dump_with_types(out, n + 2);
for (Enumeration e = cases.getElements(); e.hasMoreElements();) {
((Case)e.nextElement()).dump_with_types(out, n + 2);
}
dump_type(out, n);
}
}
class block extends Expression {
protected Expressions body;
public block(int lineNumber, Expressions a1) {
super(lineNumber);
body = a1;
}
public TreeNode copy() {
return new block(lineNumber, (Expressions)body.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "block\n");
body.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_block");
for (Enumeration e = body.getElements(); e.hasMoreElements();) {
((Expression)e.nextElement()).dump_with_types(out, n + 2);
}
dump_type(out, n);
}
}
class let extends Expression {
protected AbstractSymbol identifier;
protected AbstractSymbol type_decl;
protected Expression init;
protected Expression body;
public let(int lineNumber, AbstractSymbol a1, AbstractSymbol a2, Expression a3, Expression a4) {
super(lineNumber);
identifier = a1;
type_decl = a2;
init = a3;
body = a4;
}
public TreeNode copy() {
return new let(lineNumber, copy_AbstractSymbol(identifier), copy_AbstractSymbol(type_decl), (Expression)init.copy(), (Expression)body.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "let\n");
dump_AbstractSymbol(out, n+2, identifier);
dump_AbstractSymbol(out, n+2, type_decl);
init.dump(out, n+2);
body.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_let");
dump_AbstractSymbol(out, n + 2, identifier);
dump_AbstractSymbol(out, n + 2, type_decl);
init.dump_with_types(out, n + 2);
body.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class plus extends Expression {
protected Expression e1;
protected Expression e2;
public plus(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new plus(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "plus\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_plus");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class sub extends Expression {
protected Expression e1;
protected Expression e2;
public sub(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new sub(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "sub\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_sub");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class mul extends Expression {
protected Expression e1;
protected Expression e2;
public mul(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new mul(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "mul\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_mul");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class divide extends Expression {
protected Expression e1;
protected Expression e2;
public divide(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new divide(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "divide\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_divide");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class neg extends Expression {
protected Expression e1;
public neg(int lineNumber, Expression a1) {
super(lineNumber);
e1 = a1;
}
public TreeNode copy() {
return new neg(lineNumber, (Expression)e1.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "neg\n");
e1.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_neg");
e1.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class lt extends Expression {
protected Expression e1;
protected Expression e2;
public lt(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new lt(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "lt\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_lt");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class eq extends Expression {
protected Expression e1;
protected Expression e2;
public eq(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new eq(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "eq\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_eq");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class leq extends Expression {
protected Expression e1;
protected Expression e2;
public leq(int lineNumber, Expression a1, Expression a2) {
super(lineNumber);
e1 = a1;
e2 = a2;
}
public TreeNode copy() {
return new leq(lineNumber, (Expression)e1.copy(), (Expression)e2.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "leq\n");
e1.dump(out, n+2);
e2.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_leq");
e1.dump_with_types(out, n + 2);
e2.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class comp extends Expression {
protected Expression e1;
public comp(int lineNumber, Expression a1) {
super(lineNumber);
e1 = a1;
}
public TreeNode copy() {
return new comp(lineNumber, (Expression)e1.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "comp\n");
e1.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_comp");
e1.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class int_const extends Expression {
protected AbstractSymbol token;
public int_const(int lineNumber, AbstractSymbol a1) {
super(lineNumber);
token = a1;
}
public TreeNode copy() {
return new int_const(lineNumber, copy_AbstractSymbol(token));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "int_const\n");
dump_AbstractSymbol(out, n+2, token);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_int");
dump_AbstractSymbol(out, n + 2, token);
dump_type(out, n);
}
}
class bool_const extends Expression {
protected Boolean val;
public bool_const(int lineNumber, Boolean a1) {
super(lineNumber);
val = a1;
}
public TreeNode copy() {
return new bool_const(lineNumber, copy_Boolean(val));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "bool_const\n");
dump_Boolean(out, n+2, val);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_bool");
dump_Boolean(out, n + 2, val);
dump_type(out, n);
}
}
class string_const extends Expression {
protected AbstractSymbol token;
public string_const(int lineNumber, AbstractSymbol a1) {
super(lineNumber);
token = a1;
}
public TreeNode copy() {
return new string_const(lineNumber, copy_AbstractSymbol(token));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "string_const\n");
dump_AbstractSymbol(out, n+2, token);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_string");
out.print(Utilities.pad(n + 2) + "\"");
Utilities.printEscapedString(out, token.getString());
out.println("\"");
dump_type(out, n);
}
}
class new_ extends Expression {
protected AbstractSymbol type_name;
public new_(int lineNumber, AbstractSymbol a1) {
super(lineNumber);
type_name = a1;
}
public TreeNode copy() {
return new new_(lineNumber, copy_AbstractSymbol(type_name));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "new_\n");
dump_AbstractSymbol(out, n+2, type_name);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_new");
dump_AbstractSymbol(out, n + 2, type_name);
dump_type(out, n);
}
}
class isvoid extends Expression {
protected Expression e1;
public isvoid(int lineNumber, Expression a1) {
super(lineNumber);
e1 = a1;
}
public TreeNode copy() {
return new isvoid(lineNumber, (Expression)e1.copy());
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "isvoid\n");
e1.dump(out, n+2);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_isvoid");
e1.dump_with_types(out, n + 2);
dump_type(out, n);
}
}
class no_expr extends Expression {
public no_expr(int lineNumber) {
super(lineNumber);
}
public TreeNode copy() {
return new no_expr(lineNumber);
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "no_expr\n");
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_no_expr");
dump_type(out, n);
}
}
class object extends Expression {
protected AbstractSymbol name;
public object(int lineNumber, AbstractSymbol a1) {
super(lineNumber);
name = a1;
}
public TreeNode copy() {
return new object(lineNumber, copy_AbstractSymbol(name));
}
public void dump(PrintStream out, int n) {
out.print(Utilities.pad(n) + "object\n");
dump_AbstractSymbol(out, n+2, name);
}
public void dump_with_types(PrintStream out, int n) {
dump_line(out, n);
out.println(Utilities.pad(n) + "_object");
dump_AbstractSymbol(out, n + 2, name);
dump_type(out, n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment