1
Fork 0

Working compiler.

master
Joshua Potter 2014-04-26 13:23:14 -04:00
parent 2f673d60b7
commit 8390199866
80 changed files with 4009 additions and 3488 deletions

View File

@ -1,14 +1,19 @@
MiniJava Compiler
=================
[Version 1.0.0 - 04/15/2014]
[Version 1.0.0 - 04/26/2014]
The following is a LL(1) compiler for a subset of Java, denoted MiniJava. As of now, it appears to work for the most
part, passing the regression tests performed after each segment.
The following is a complete MiniJava Compiler implementation, according to the specifications in the docs folder.
Noted bugs:
* Changing a field of an object in an array causes crashes
This is not really a surprise, considering the Contextual Analysis aspect of the project
is in fairly rough shape.
As of now, I do not plan on implementing more things to this, unless there is some potential for extra credit.
Other Features to include in future releases
--------------------------------------------
* Static Field Initialization
* Keyword 'null'
* Support class constructors
* 'for' loop
* String type and string literals
* Code generation for conditional operations && and ||
* Overloaded methods
* Inheritance of fields and methods
* Dynamic Method Invocation
* Support for instanceof/super

Binary file not shown.

BIN
docs/pa-final.pdf Normal file

Binary file not shown.

BIN
docs/pa1-updated.pdf Normal file

Binary file not shown.

View File

@ -151,7 +151,6 @@ public class Test
/* run code */
System.out.println("Running code ... ");
Interpreter.debug(objectCodeFileName, asmCodeFileName);
// Interpreter.interpret(objectCodeFileName);
System.out.println("*** mJAM execution completed");
}

View File

@ -1,27 +1,27 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class AST {
public AST(SourcePosition posn) {
this.posn = posn;
}
public String toString() {
String fullClassName = this.getClass().getName();
String cn = fullClassName.substring(1 + fullClassName.lastIndexOf('.'));
if (ASTDisplay.showPosition)
cn = cn + " " + posn.toString();
return cn;
}
public abstract <A, R> R visit(Visitor<A, R> v, A o);
public SourcePosition posn;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class AST {
public AST(SourcePosition posn) {
this.posn = posn;
}
public String toString() {
String fullClassName = this.getClass().getName();
String cn = fullClassName.substring(1 + fullClassName.lastIndexOf('.'));
if (ASTDisplay.showPosition)
cn = cn + " " + posn.toString();
return cn;
}
public abstract <A, R> R visit(Visitor<A, R> v, A o);
public SourcePosition posn;
}

View File

@ -1,358 +1,358 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
/*
* Display AST in text form
* In-order traversal of AST, visiting each node with a method of the form
*
* public Object visitXXX( XXX astnode, String arg)
*
* where arg is a prefix string (indentation) to precede display of ast node
* and a null Object is returned as the result.
*
* implements Visitor<argtype,resulttype>
*/
public class ASTDisplay implements Visitor<String, Object> {
public static boolean showPosition = false;
/**
* print text representation of AST to stdout
*
* @param ast
* root node of AST
*/
public void showTree(AST ast) {
System.out.println("======= AST Display =========================");
ast.visit(this, "");
System.out.println("=============================================");
}
// methods to format output
/**
* display arbitrary text for a node
*
* @param prefix
* spacing to indicate depth in AST
* @param text
* preformatted node display
*/
private void show(String prefix, String text) {
System.out.println(prefix + text);
}
/**
* display AST node by name
*
* @param prefix
* spacing to indicate depth in AST
* @param node
* AST node, will be shown by name
*/
private void show(String prefix, AST node) {
System.out.println(prefix + node.toString());
}
/**
* quote a string
*
* @param text
* string to quote
*/
private String quote(String text) {
return ("\"" + text + "\"");
}
/**
* increase depth in AST
*
* @param prefix
* current spacing to indicate depth in AST
* @return new spacing
*/
private String indent(String prefix) {
return prefix + " ";
}
// /////////////////////////////////////////////////////////////////////////////
//
// PACKAGE
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitPackage(Package prog, String arg) {
show(arg, prog);
ClassDeclList cl = prog.classDeclList;
show(arg, " ClassDeclList [" + cl.size() + "]");
String pfx = arg + " . ";
for (ClassDecl c : prog.classDeclList) {
c.visit(this, pfx);
}
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// DECLARATIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitClassDecl(ClassDecl clas, String arg) {
show(arg, clas);
show(indent(arg), quote(clas.name) + " classname");
show(arg, " FieldDeclList [" + clas.fieldDeclList.size() + "]");
String pfx = arg + " . ";
for (FieldDecl f : clas.fieldDeclList)
f.visit(this, pfx);
show(arg, " MethodDeclList [" + clas.methodDeclList.size() + "]");
for (MethodDecl m : clas.methodDeclList)
m.visit(this, pfx);
return null;
}
public Object visitFieldDecl(FieldDecl f, String arg) {
show(arg, "(" + (f.isPrivate ? "private" : "public")
+ (f.isStatic ? " static) " : ") ") + f.toString());
f.type.visit(this, indent(arg));
show(indent(arg), quote(f.name) + " fieldname");
return null;
}
public Object visitMethodDecl(MethodDecl m, String arg) {
show(arg, "(" + (m.isPrivate ? "private" : "public")
+ (m.isStatic ? " static) " : ") ") + m.toString());
m.type.visit(this, indent(arg));
show(indent(arg), quote(m.name) + " methodname");
ParameterDeclList pdl = m.parameterDeclList;
show(arg, " ParameterDeclList [" + pdl.size() + "]");
String pfx = ((String) arg) + " . ";
for (ParameterDecl pd : pdl) {
pd.visit(this, pfx);
}
StatementList sl = m.statementList;
show(arg, " StmtList [" + sl.size() + "]");
for (Statement s : sl) {
s.visit(this, pfx);
}
if (m.returnExp != null) {
m.returnExp.visit(this, indent(arg));
}
return null;
}
public Object visitParameterDecl(ParameterDecl pd, String arg) {
show(arg, pd);
pd.type.visit(this, indent(arg));
show(indent(arg), quote(pd.name) + "parametername ");
return null;
}
public Object visitVarDecl(VarDecl vd, String arg) {
show(arg, vd);
vd.type.visit(this, indent(arg));
show(indent(arg), quote(vd.name) + " varname");
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// TYPES
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitBaseType(BaseType type, String arg) {
show(arg, type.typeKind + " " + type.toString());
return null;
}
public Object visitClassType(ClassType type, String arg) {
show(arg, type);
show(indent(arg), quote(type.className.spelling) + " classname");
return null;
}
public Object visitArrayType(ArrayType type, String arg) {
show(arg, type);
type.eltType.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// STATEMENTS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitBlockStmt(BlockStmt stmt, String arg) {
show(arg, stmt);
StatementList sl = stmt.sl;
show(arg, " StatementList [" + sl.size() + "]");
String pfx = arg + " . ";
for (Statement s : sl) {
s.visit(this, pfx);
}
return null;
}
public Object visitVardeclStmt(VarDeclStmt stmt, String arg) {
show(arg, stmt);
stmt.varDecl.visit(this, indent(arg));
stmt.initExp.visit(this, indent(arg));
return null;
}
public Object visitAssignStmt(AssignStmt stmt, String arg) {
show(arg, stmt);
stmt.ref.visit(this, indent(arg));
stmt.val.visit(this, indent(arg));
return null;
}
public Object visitCallStmt(CallStmt stmt, String arg) {
show(arg, stmt);
stmt.methodRef.visit(this, indent(arg));
ExprList al = stmt.argList;
show(arg, " ExprList [" + al.size() + "]");
String pfx = arg + " . ";
for (Expression e : al) {
e.visit(this, pfx);
}
return null;
}
public Object visitIfStmt(IfStmt stmt, String arg) {
show(arg, stmt);
stmt.cond.visit(this, indent(arg));
stmt.thenStmt.visit(this, indent(arg));
if (stmt.elseStmt != null)
stmt.elseStmt.visit(this, indent(arg));
return null;
}
public Object visitWhileStmt(WhileStmt stmt, String arg) {
show(arg, stmt);
stmt.cond.visit(this, indent(arg));
stmt.body.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// EXPRESSIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitUnaryExpr(UnaryExpr expr, String arg) {
show(arg, expr);
expr.operator.visit(this, indent(arg));
expr.expr.visit(this, indent(indent(arg)));
return null;
}
public Object visitBinaryExpr(BinaryExpr expr, String arg) {
show(arg, expr);
expr.operator.visit(this, indent(arg));
expr.left.visit(this, indent(indent(arg)));
expr.right.visit(this, indent(indent(arg)));
return null;
}
public Object visitRefExpr(RefExpr expr, String arg) {
show(arg, expr);
expr.ref.visit(this, indent(arg));
return null;
}
public Object visitCallExpr(CallExpr expr, String arg) {
show(arg, expr);
expr.functionRef.visit(this, indent(arg));
ExprList al = expr.argList;
show(arg, " ExprList + [" + al.size() + "]");
String pfx = arg + " . ";
for (Expression e : al) {
e.visit(this, pfx);
}
return null;
}
public Object visitLiteralExpr(LiteralExpr expr, String arg) {
show(arg, expr);
expr.literal.visit(this, indent(arg));
return null;
}
public Object visitNewArrayExpr(NewArrayExpr expr, String arg) {
show(arg, expr);
expr.eltType.visit(this, indent(arg));
expr.sizeExpr.visit(this, indent(arg));
return null;
}
public Object visitNewObjectExpr(NewObjectExpr expr, String arg) {
show(arg, expr);
expr.classtype.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// REFERENCES
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitQualifiedRef(QualifiedRef qr, String arg) {
show(arg, qr);
qr.id.visit(this, indent(arg));
qr.ref.visit(this, indent(arg));
return null;
}
public Object visitIndexedRef(IndexedRef ir, String arg) {
show(arg, ir);
ir.indexExpr.visit(this, indent(arg));
ir.ref.visit(this, indent(arg));
return null;
}
public Object visitIdRef(IdRef ref, String arg) {
show(arg, ref);
ref.id.visit(this, indent(arg));
return null;
}
public Object visitThisRef(ThisRef ref, String arg) {
show(arg, ref);
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// TERMINALS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitIdentifier(Identifier id, String arg) {
show(arg, quote(id.spelling) + " " + id.toString());
return null;
}
public Object visitOperator(Operator op, String arg) {
show(arg, quote(op.spelling) + " " + op.toString());
return null;
}
public Object visitIntLiteral(IntLiteral num, String arg) {
show(arg, quote(num.spelling) + " " + num.toString());
return null;
}
public Object visitBooleanLiteral(BooleanLiteral bool, String arg) {
show(arg, quote(bool.spelling) + " " + bool.toString());
return null;
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
/*
* Display AST in text form
* In-order traversal of AST, visiting each node with a method of the form
*
* public Object visitXXX( XXX astnode, String arg)
*
* where arg is a prefix string (indentation) to precede display of ast node
* and a null Object is returned as the result.
*
* implements Visitor<argtype,resulttype>
*/
public class ASTDisplay implements Visitor<String, Object> {
public static boolean showPosition = false;
/**
* print text representation of AST to stdout
*
* @param ast
* root node of AST
*/
public void showTree(AST ast) {
System.out.println("======= AST Display =========================");
ast.visit(this, "");
System.out.println("=============================================");
}
// methods to format output
/**
* display arbitrary text for a node
*
* @param prefix
* spacing to indicate depth in AST
* @param text
* preformatted node display
*/
private void show(String prefix, String text) {
System.out.println(prefix + text);
}
/**
* display AST node by name
*
* @param prefix
* spacing to indicate depth in AST
* @param node
* AST node, will be shown by name
*/
private void show(String prefix, AST node) {
System.out.println(prefix + node.toString());
}
/**
* quote a string
*
* @param text
* string to quote
*/
private String quote(String text) {
return ("\"" + text + "\"");
}
/**
* increase depth in AST
*
* @param prefix
* current spacing to indicate depth in AST
* @return new spacing
*/
private String indent(String prefix) {
return prefix + " ";
}
// /////////////////////////////////////////////////////////////////////////////
//
// PACKAGE
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitPackage(Package prog, String arg) {
show(arg, prog);
ClassDeclList cl = prog.classDeclList;
show(arg, " ClassDeclList [" + cl.size() + "]");
String pfx = arg + " . ";
for (ClassDecl c : prog.classDeclList) {
c.visit(this, pfx);
}
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// DECLARATIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitClassDecl(ClassDecl clas, String arg) {
show(arg, clas);
show(indent(arg), quote(clas.name) + " classname");
show(arg, " FieldDeclList [" + clas.fieldDeclList.size() + "]");
String pfx = arg + " . ";
for (FieldDecl f : clas.fieldDeclList)
f.visit(this, pfx);
show(arg, " MethodDeclList [" + clas.methodDeclList.size() + "]");
for (MethodDecl m : clas.methodDeclList)
m.visit(this, pfx);
return null;
}
public Object visitFieldDecl(FieldDecl f, String arg) {
show(arg, "(" + (f.isPrivate ? "private" : "public")
+ (f.isStatic ? " static) " : ") ") + f.toString());
f.type.visit(this, indent(arg));
show(indent(arg), quote(f.name) + " fieldname");
return null;
}
public Object visitMethodDecl(MethodDecl m, String arg) {
show(arg, "(" + (m.isPrivate ? "private" : "public")
+ (m.isStatic ? " static) " : ") ") + m.toString());
m.type.visit(this, indent(arg));
show(indent(arg), quote(m.name) + " methodname");
ParameterDeclList pdl = m.parameterDeclList;
show(arg, " ParameterDeclList [" + pdl.size() + "]");
String pfx = ((String) arg) + " . ";
for (ParameterDecl pd : pdl) {
pd.visit(this, pfx);
}
StatementList sl = m.statementList;
show(arg, " StmtList [" + sl.size() + "]");
for (Statement s : sl) {
s.visit(this, pfx);
}
if (m.returnExp != null) {
m.returnExp.visit(this, indent(arg));
}
return null;
}
public Object visitParameterDecl(ParameterDecl pd, String arg) {
show(arg, pd);
pd.type.visit(this, indent(arg));
show(indent(arg), quote(pd.name) + "parametername ");
return null;
}
public Object visitVarDecl(VarDecl vd, String arg) {
show(arg, vd);
vd.type.visit(this, indent(arg));
show(indent(arg), quote(vd.name) + " varname");
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// TYPES
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitBaseType(BaseType type, String arg) {
show(arg, type.typeKind + " " + type.toString());
return null;
}
public Object visitClassType(ClassType type, String arg) {
show(arg, type);
show(indent(arg), quote(type.className.spelling) + " classname");
return null;
}
public Object visitArrayType(ArrayType type, String arg) {
show(arg, type);
type.eltType.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// STATEMENTS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitBlockStmt(BlockStmt stmt, String arg) {
show(arg, stmt);
StatementList sl = stmt.sl;
show(arg, " StatementList [" + sl.size() + "]");
String pfx = arg + " . ";
for (Statement s : sl) {
s.visit(this, pfx);
}
return null;
}
public Object visitVardeclStmt(VarDeclStmt stmt, String arg) {
show(arg, stmt);
stmt.varDecl.visit(this, indent(arg));
stmt.initExp.visit(this, indent(arg));
return null;
}
public Object visitAssignStmt(AssignStmt stmt, String arg) {
show(arg, stmt);
stmt.ref.visit(this, indent(arg));
stmt.val.visit(this, indent(arg));
return null;
}
public Object visitCallStmt(CallStmt stmt, String arg) {
show(arg, stmt);
stmt.methodRef.visit(this, indent(arg));
ExprList al = stmt.argList;
show(arg, " ExprList [" + al.size() + "]");
String pfx = arg + " . ";
for (Expression e : al) {
e.visit(this, pfx);
}
return null;
}
public Object visitIfStmt(IfStmt stmt, String arg) {
show(arg, stmt);
stmt.cond.visit(this, indent(arg));
stmt.thenStmt.visit(this, indent(arg));
if (stmt.elseStmt != null)
stmt.elseStmt.visit(this, indent(arg));
return null;
}
public Object visitWhileStmt(WhileStmt stmt, String arg) {
show(arg, stmt);
stmt.cond.visit(this, indent(arg));
stmt.body.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// EXPRESSIONS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitUnaryExpr(UnaryExpr expr, String arg) {
show(arg, expr);
expr.operator.visit(this, indent(arg));
expr.expr.visit(this, indent(indent(arg)));
return null;
}
public Object visitBinaryExpr(BinaryExpr expr, String arg) {
show(arg, expr);
expr.operator.visit(this, indent(arg));
expr.left.visit(this, indent(indent(arg)));
expr.right.visit(this, indent(indent(arg)));
return null;
}
public Object visitRefExpr(RefExpr expr, String arg) {
show(arg, expr);
expr.ref.visit(this, indent(arg));
return null;
}
public Object visitCallExpr(CallExpr expr, String arg) {
show(arg, expr);
expr.functionRef.visit(this, indent(arg));
ExprList al = expr.argList;
show(arg, " ExprList + [" + al.size() + "]");
String pfx = arg + " . ";
for (Expression e : al) {
e.visit(this, pfx);
}
return null;
}
public Object visitLiteralExpr(LiteralExpr expr, String arg) {
show(arg, expr);
expr.literal.visit(this, indent(arg));
return null;
}
public Object visitNewArrayExpr(NewArrayExpr expr, String arg) {
show(arg, expr);
expr.eltType.visit(this, indent(arg));
expr.sizeExpr.visit(this, indent(arg));
return null;
}
public Object visitNewObjectExpr(NewObjectExpr expr, String arg) {
show(arg, expr);
expr.classtype.visit(this, indent(arg));
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// REFERENCES
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitQualifiedRef(QualifiedRef qr, String arg) {
show(arg, qr);
qr.id.visit(this, indent(arg));
qr.ref.visit(this, indent(arg));
return null;
}
public Object visitIndexedRef(IndexedRef ir, String arg) {
show(arg, ir);
ir.indexExpr.visit(this, indent(arg));
ir.ref.visit(this, indent(arg));
return null;
}
public Object visitIdRef(IdRef ref, String arg) {
show(arg, ref);
ref.id.visit(this, indent(arg));
return null;
}
public Object visitThisRef(ThisRef ref, String arg) {
show(arg, ref);
return null;
}
// /////////////////////////////////////////////////////////////////////////////
//
// TERMINALS
//
// /////////////////////////////////////////////////////////////////////////////
public Object visitIdentifier(Identifier id, String arg) {
show(arg, quote(id.spelling) + " " + id.toString());
return null;
}
public Object visitOperator(Operator op, String arg) {
show(arg, quote(op.spelling) + " " + op.toString());
return null;
}
public Object visitIntLiteral(IntLiteral num, String arg) {
show(arg, quote(num.spelling) + " " + num.toString());
return null;
}
public Object visitBooleanLiteral(BooleanLiteral bool, String arg) {
show(arg, quote(bool.spelling) + " " + bool.toString());
return null;
}
}

View File

@ -1,23 +1,27 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ArrayType extends Type {
public ArrayType(Type eltType, SourcePosition posn) {
super(TypeKind.ARRAY, posn);
this.eltType = eltType;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitArrayType(this, o);
}
public Type eltType;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ArrayType extends Type {
public ArrayType(Type eltType, SourcePosition posn) {
super(TypeKind.ARRAY, posn);
this.eltType = eltType;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitArrayType(this, o);
}
public String toString() {
return eltType + " Array";
}
public Type eltType;
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class AssignStmt extends Statement {
public AssignStmt(Reference r, Expression e, SourcePosition posn) {
super(posn);
ref = r;
val = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitAssignStmt(this, o);
}
public Reference ref;
public Expression val;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class AssignStmt extends Statement {
public AssignStmt(Reference r, Expression e, SourcePosition posn) {
super(posn);
ref = r;
val = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitAssignStmt(this, o);
}
public Reference ref;
public Expression val;
}

View File

@ -1,18 +1,22 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BaseType extends Type {
public BaseType(TypeKind t, SourcePosition posn) {
super(t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBaseType(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BaseType extends Type {
public BaseType(TypeKind t, SourcePosition posn) {
super(t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBaseType(this, o);
}
public String toString() {
return typeKind.toString();
}
}

View File

@ -1,26 +1,26 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BinaryExpr extends Expression {
public BinaryExpr(Operator o, Expression e1, Expression e2,
SourcePosition posn) {
super(posn);
operator = o;
left = e1;
right = e2;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBinaryExpr(this, o);
}
public Operator operator;
public Expression left;
public Expression right;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BinaryExpr extends Expression {
public BinaryExpr(Operator o, Expression e1, Expression e2,
SourcePosition posn) {
super(posn);
operator = o;
left = e1;
right = e2;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBinaryExpr(this, o);
}
public Operator operator;
public Expression left;
public Expression right;
}

View File

@ -1,21 +1,21 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BlockStmt extends Statement {
public BlockStmt(StatementList sl, SourcePosition posn) {
super(posn);
this.sl = sl;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBlockStmt(this, o);
}
public StatementList sl;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BlockStmt extends Statement {
public BlockStmt(StatementList sl, SourcePosition posn) {
super(posn);
this.sl = sl;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBlockStmt(this, o);
}
public StatementList sl;
}

View File

@ -1,19 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BooleanLiteral extends Literal {
public BooleanLiteral(String spelling, SourcePosition posn) {
super(spelling, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBooleanLiteral(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class BooleanLiteral extends Literal {
public BooleanLiteral(String spelling, SourcePosition posn) {
super(spelling, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitBooleanLiteral(this, o);
}
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallExpr extends Expression {
public CallExpr(Reference f, ExprList el, SourcePosition posn) {
super(posn);
functionRef = f;
argList = el;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitCallExpr(this, o);
}
public Reference functionRef;
public ExprList argList;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallExpr extends Expression {
public CallExpr(Reference f, ExprList el, SourcePosition posn) {
super(posn);
functionRef = f;
argList = el;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitCallExpr(this, o);
}
public Reference functionRef;
public ExprList argList;
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallStmt extends Statement {
public CallStmt(Reference m, ExprList el, SourcePosition posn) {
super(posn);
methodRef = m;
argList = el;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitCallStmt(this, o);
}
public Reference methodRef;
public ExprList argList;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class CallStmt extends Statement {
public CallStmt(Reference m, ExprList el, SourcePosition posn) {
super(posn);
methodRef = m;
argList = el;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitCallStmt(this, o);
}
public Reference methodRef;
public ExprList argList;
}

View File

@ -1,27 +1,25 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassDecl extends Declaration {
public ClassDecl(String cn, FieldDeclList fdl, MethodDeclList mdl, SourcePosition posn) {
super(cn, null, posn);
fieldDeclList = fdl;
methodDeclList = mdl;
Identifier ident = new Identifier(cn, posn);
type = new ClassType(ident, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitClassDecl(this, o);
}
public FieldDeclList fieldDeclList;
public MethodDeclList methodDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassDecl extends Declaration {
public ClassDecl(String cn, FieldDeclList fdl, MethodDeclList mdl,
SourcePosition posn) {
super(cn, null, posn);
fieldDeclList = fdl;
methodDeclList = mdl;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitClassDecl(this, o);
}
public FieldDeclList fieldDeclList;
public MethodDeclList methodDeclList;
}

View File

@ -1,32 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ClassDeclList implements Iterable<ClassDecl> {
public ClassDeclList() {
classDeclList = new ArrayList<ClassDecl>();
}
public void add(ClassDecl cd) {
classDeclList.add(cd);
}
public ClassDecl get(int i) {
return classDeclList.get(i);
}
public int size() {
return classDeclList.size();
}
public Iterator<ClassDecl> iterator() {
return classDeclList.iterator();
}
private List<ClassDecl> classDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ClassDeclList implements Iterable<ClassDecl> {
public ClassDeclList() {
classDeclList = new ArrayList<ClassDecl>();
}
public void add(ClassDecl cd) {
classDeclList.add(cd);
}
public ClassDecl get(int i) {
return classDeclList.get(i);
}
public int size() {
return classDeclList.size();
}
public Iterator<ClassDecl> iterator() {
return classDeclList.iterator();
}
private List<ClassDecl> classDeclList;
}

View File

@ -1,21 +1,25 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassType extends Type {
public ClassType(Identifier cn, SourcePosition posn) {
super(TypeKind.CLASS, posn);
className = cn;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitClassType(this, o);
}
public Identifier className;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ClassType extends Type {
public ClassType(Identifier cn, SourcePosition posn) {
super(TypeKind.CLASS, posn);
className = cn;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitClassType(this, o);
}
public String toString() {
return "Class " + className.spelling;
}
public Identifier className;
}

View File

@ -1,32 +1,24 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.CodeGenerator.RuntimeEntity;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Declaration extends AST {
public Declaration(String name, Type type, SourcePosition posn) {
super(posn);
this.name = name;
this.type = type;
}
@Override
public String toString() {
if (posn != null) {
return this.name + "(Line: " + posn.line + ", Column: " + posn.col
+ ")";
} else {
return super.toString();
}
}
public RuntimeEntity entity;
public String name;
public Type type;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.CodeGenerator.RuntimeEntity;
import miniJava.ContextualAnalyzer.IdTable;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Declaration extends AST {
public Declaration(String name, Type type, SourcePosition posn) {
super(posn);
this.name = name;
this.type = type;
}
public RuntimeEntity entity;
public IdTable table;
public String name;
public Type type;
}

View File

@ -2,6 +2,10 @@ package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
/**
* The following is a custom class, included for use with method and field
* declarations.
*/
public class Declarators {
public Declarators(boolean isPrivate, boolean isStatic, Type mt,
@ -16,4 +20,4 @@ public class Declarators {
public boolean isStatic;
public Type mt;
public SourcePosition posn;
}
}

View File

@ -1,32 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ExprList implements Iterable<Expression> {
public ExprList() {
elist = new ArrayList<Expression>();
}
public void add(Expression e) {
elist.add(e);
}
public Expression get(int i) {
return elist.get(i);
}
public int size() {
return elist.size();
}
public Iterator<Expression> iterator() {
return elist.iterator();
}
private List<Expression> elist;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ExprList implements Iterable<Expression> {
public ExprList() {
elist = new ArrayList<Expression>();
}
public void add(Expression e) {
elist.add(e);
}
public Expression get(int i) {
return elist.get(i);
}
public int size() {
return elist.size();
}
public Iterator<Expression> iterator() {
return elist.iterator();
}
private List<Expression> elist;
}

View File

@ -1,16 +1,16 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Expression extends AST {
public Expression(SourcePosition posn) {
super(posn);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Expression extends AST {
public Expression(SourcePosition posn) {
super(posn);
}
}

View File

@ -1,24 +1,28 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class FieldDecl extends MemberDecl {
public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name,
SourcePosition posn) {
super(isPrivate, isStatic, t, name, posn);
}
public FieldDecl(MemberDecl md, SourcePosition posn) {
super(md, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitFieldDecl(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class FieldDecl extends MemberDecl {
public FieldDecl(boolean isPrivate, boolean isStatic, Type t, String name,
SourcePosition posn) {
super(isPrivate, isStatic, t, name, posn);
}
public FieldDecl(MemberDecl md, SourcePosition posn) {
super(md, posn);
}
public FieldDecl(Declarators d, String name) {
super(d.isPrivate, d.isStatic, d.mt, name, d.posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitFieldDecl(this, o);
}
}

View File

@ -1,37 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class FieldDeclList implements Iterable<FieldDecl> {
public FieldDeclList() {
fieldDeclList = new ArrayList<FieldDecl>();
}
public FieldDeclList(FieldDecl f) {
fieldDeclList = new ArrayList<FieldDecl>();
fieldDeclList.add(f);
}
public void add(FieldDecl cd) {
fieldDeclList.add(cd);
}
public FieldDecl get(int i) {
return fieldDeclList.get(i);
}
public int size() {
return fieldDeclList.size();
}
public Iterator<FieldDecl> iterator() {
return fieldDeclList.iterator();
}
private List<FieldDecl> fieldDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class FieldDeclList implements Iterable<FieldDecl> {
public FieldDeclList() {
fieldDeclList = new ArrayList<FieldDecl>();
}
public void add(FieldDecl cd) {
fieldDeclList.add(cd);
}
public FieldDecl get(int i) {
return fieldDeclList.get(i);
}
public int size() {
return fieldDeclList.size();
}
public Iterator<FieldDecl> iterator() {
return fieldDeclList.iterator();
}
private List<FieldDecl> fieldDeclList;
}

View File

@ -1,22 +1,22 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IdRef extends Reference {
public IdRef(Identifier id, SourcePosition posn) {
super(posn);
this.id = id;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIdRef(this, o);
}
public Identifier id;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IdRef extends Reference {
public IdRef(Identifier id, SourcePosition posn) {
super(posn);
this.id = id;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIdRef(this, o);
}
public Identifier id;
}

View File

@ -1,21 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Identifier extends Terminal {
public Identifier(String s, SourcePosition posn) {
super(s, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIdentifier(this, o);
}
public Declaration decl = null;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.CodeGenerator.RuntimeEntity;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Identifier extends Terminal {
public Identifier(String s, SourcePosition posn) {
super(s, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIdentifier(this, o);
}
public Declaration decl;
public RuntimeEntity entity;
}

View File

@ -1,32 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IfStmt extends Statement {
public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn) {
super(posn);
cond = b;
thenStmt = t;
elseStmt = e;
}
public IfStmt(Expression b, Statement t, SourcePosition posn) {
super(posn);
cond = b;
thenStmt = t;
elseStmt = null;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIfStmt(this, o);
}
public Expression cond;
public Statement thenStmt;
public Statement elseStmt;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IfStmt extends Statement {
public IfStmt(Expression b, Statement t, Statement e, SourcePosition posn) {
super(posn);
cond = b;
thenStmt = t;
elseStmt = e;
}
public IfStmt(Expression b, Statement t, SourcePosition posn) {
super(posn);
cond = b;
thenStmt = t;
elseStmt = null;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIfStmt(this, o);
}
public Expression cond;
public Statement thenStmt;
public Statement elseStmt;
}

View File

@ -1,24 +1,24 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IndexedRef extends Reference {
public IndexedRef(Reference ref, Expression expr, SourcePosition posn) {
super(posn);
this.ref = ref;
this.indexExpr = expr;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIndexedRef(this, o);
}
public Reference ref;
public Expression indexExpr;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IndexedRef extends Reference {
public IndexedRef(Reference ref, Expression expr, SourcePosition posn) {
super(posn);
this.ref = ref;
this.indexExpr = expr;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIndexedRef(this, o);
}
public Reference ref;
public Expression indexExpr;
}

View File

@ -1,19 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IntLiteral extends Literal {
public IntLiteral(String s, SourcePosition posn) {
super(s, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIntLiteral(this, o);
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class IntLiteral extends Literal {
public IntLiteral(String s, SourcePosition posn) {
super(s, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitIntLiteral(this, o);
}
}

View File

@ -1,15 +1,15 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Literal extends Terminal {
public Literal(String spelling, SourcePosition posn) {
super(spelling, posn);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Literal extends Terminal {
public Literal(String spelling, SourcePosition posn) {
super(spelling, posn);
}
}

View File

@ -1,21 +1,21 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class LiteralExpr extends Expression {
public LiteralExpr(Literal c, SourcePosition posn) {
super(posn);
literal = c;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitLiteralExpr(this, o);
}
public Literal literal;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class LiteralExpr extends Expression {
public LiteralExpr(Literal c, SourcePosition posn) {
super(posn);
literal = c;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitLiteralExpr(this, o);
}
public Literal literal;
}

View File

@ -1,16 +1,16 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class LocalDecl extends Declaration {
public LocalDecl(String name, Type t, SourcePosition posn) {
super(name, t, posn);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class LocalDecl extends Declaration {
public LocalDecl(String name, Type t, SourcePosition posn) {
super(name, t, posn);
}
}

View File

@ -1,27 +1,27 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class MemberDecl extends Declaration {
public MemberDecl(boolean isPrivate, boolean isStatic, Type mt,
String name, SourcePosition posn) {
super(name, mt, posn);
this.isPrivate = isPrivate;
this.isStatic = isStatic;
}
public MemberDecl(MemberDecl md, SourcePosition posn) {
super(md.name, md.type, posn);
this.isPrivate = md.isPrivate;
this.isStatic = md.isStatic;
}
public boolean isPrivate;
public boolean isStatic;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class MemberDecl extends Declaration {
public MemberDecl(boolean isPrivate, boolean isStatic, Type mt,
String name, SourcePosition posn) {
super(name, mt, posn);
this.isPrivate = isPrivate;
this.isStatic = isStatic;
}
public MemberDecl(MemberDecl md, SourcePosition posn) {
super(md.name, md.type, posn);
this.isPrivate = md.isPrivate;
this.isStatic = md.isStatic;
}
public boolean isPrivate;
public boolean isStatic;
}

View File

@ -1,27 +1,27 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class MethodDecl extends MemberDecl {
public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl,
Expression e, SourcePosition posn) {
super(md, posn);
parameterDeclList = pl;
statementList = sl;
returnExp = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitMethodDecl(this, o);
}
public ParameterDeclList parameterDeclList;
public StatementList statementList;
public Expression returnExp;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class MethodDecl extends MemberDecl {
public MethodDecl(MemberDecl md, ParameterDeclList pl, StatementList sl,
Expression e, SourcePosition posn) {
super(md, posn);
parameterDeclList = pl;
statementList = sl;
returnExp = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitMethodDecl(this, o);
}
public ParameterDeclList parameterDeclList;
public StatementList statementList;
public Expression returnExp;
}

View File

@ -1,37 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class MethodDeclList implements Iterable<MethodDecl> {
public MethodDeclList() {
methodDeclList = new ArrayList<MethodDecl>();
}
public MethodDeclList(MethodDecl m) {
methodDeclList = new ArrayList<MethodDecl>();
methodDeclList.add(m);
}
public void add(MethodDecl cd) {
methodDeclList.add(cd);
}
public MethodDecl get(int i) {
return methodDeclList.get(i);
}
public int size() {
return methodDeclList.size();
}
public Iterator<MethodDecl> iterator() {
return methodDeclList.iterator();
}
private List<MethodDecl> methodDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class MethodDeclList implements Iterable<MethodDecl> {
public MethodDeclList() {
methodDeclList = new ArrayList<MethodDecl>();
}
public void add(MethodDecl cd) {
methodDeclList.add(cd);
}
public MethodDecl get(int i) {
return methodDeclList.get(i);
}
public int size() {
return methodDeclList.size();
}
public Iterator<MethodDecl> iterator() {
return methodDeclList.iterator();
}
private List<MethodDecl> methodDeclList;
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewArrayExpr extends NewExpr {
public NewArrayExpr(Type et, Expression e, SourcePosition posn) {
super(posn);
eltType = et;
sizeExpr = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitNewArrayExpr(this, o);
}
public Type eltType;
public Expression sizeExpr;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewArrayExpr extends NewExpr {
public NewArrayExpr(Type et, Expression e, SourcePosition posn) {
super(posn);
eltType = et;
sizeExpr = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitNewArrayExpr(this, o);
}
public Type eltType;
public Expression sizeExpr;
}

View File

@ -1,15 +1,15 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class NewExpr extends Expression {
public NewExpr(SourcePosition posn) {
super(posn);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class NewExpr extends Expression {
public NewExpr(SourcePosition posn) {
super(posn);
}
}

View File

@ -1,21 +1,21 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewObjectExpr extends NewExpr {
public NewObjectExpr(ClassType ct, SourcePosition posn) {
super(posn);
classtype = ct;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitNewObjectExpr(this, o);
}
public ClassType classtype;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class NewObjectExpr extends NewExpr {
public NewObjectExpr(ClassType ct, SourcePosition posn) {
super(posn);
classtype = ct;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitNewObjectExpr(this, o);
}
public ClassType classtype;
}

View File

@ -1,23 +1,22 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
import miniJava.SyntacticAnalyzer.Token;
public class Operator extends Terminal {
public Operator(Token t, SourcePosition posn) {
super(t.spelling, posn);
token = t;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitOperator(this, o);
}
public Token token;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
import miniJava.SyntacticAnalyzer.Token;
public class Operator extends Terminal {
public Operator(Token t, SourcePosition posn) {
super(t.spelling, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitOperator(this, o);
}
public Token token;
}

View File

@ -1,22 +1,22 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Package extends AST {
public Package(ClassDeclList cdl, SourcePosition posn) {
super(posn);
classDeclList = cdl;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitPackage(this, o);
}
public ClassDeclList classDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class Package extends AST {
public Package(ClassDeclList cdl, SourcePosition posn) {
super(posn);
classDeclList = cdl;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitPackage(this, o);
}
public ClassDeclList classDeclList;
}

View File

@ -1,19 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ParameterDecl extends LocalDecl {
public ParameterDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitParameterDecl(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ParameterDecl extends LocalDecl {
public ParameterDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitParameterDecl(this, o);
}
}

View File

@ -1,37 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ParameterDeclList implements Iterable<ParameterDecl> {
public ParameterDeclList() {
parameterDeclList = new ArrayList<ParameterDecl>();
}
public ParameterDeclList(ParameterDecl p) {
parameterDeclList = new ArrayList<ParameterDecl>();
parameterDeclList.add(p);
}
public void add(ParameterDecl s) {
parameterDeclList.add(s);
}
public ParameterDecl get(int i) {
return parameterDeclList.get(i);
}
public int size() {
return parameterDeclList.size();
}
public Iterator<ParameterDecl> iterator() {
return parameterDeclList.iterator();
}
private List<ParameterDecl> parameterDeclList;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class ParameterDeclList implements Iterable<ParameterDecl> {
public ParameterDeclList() {
parameterDeclList = new ArrayList<ParameterDecl>();
}
public void add(ParameterDecl s) {
parameterDeclList.add(s);
}
public ParameterDecl get(int i) {
return parameterDeclList.get(i);
}
public int size() {
return parameterDeclList.size();
}
public Iterator<ParameterDecl> iterator() {
return parameterDeclList.iterator();
}
private List<ParameterDecl> parameterDeclList;
}

View File

@ -1,25 +1,25 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class QualifiedRef extends Reference {
public QualifiedRef(Reference ref, Identifier id, SourcePosition posn) {
super(posn);
this.ref = ref;
this.id = id;
}
@Override
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitQualifiedRef(this, o);
}
public Reference ref;
public Identifier id;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class QualifiedRef extends Reference {
public QualifiedRef(Reference ref, Identifier id, SourcePosition posn) {
super(posn);
this.ref = ref;
this.id = id;
}
@Override
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitQualifiedRef(this, o);
}
public Reference ref;
public Identifier id;
}

View File

@ -1,21 +1,21 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class RefExpr extends Expression {
public RefExpr(Reference r, SourcePosition posn) {
super(posn);
ref = r;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitRefExpr(this, o);
}
public Reference ref;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class RefExpr extends Expression {
public RefExpr(Reference r, SourcePosition posn) {
super(posn);
ref = r;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitRefExpr(this, o);
}
public Reference ref;
}

View File

@ -1,18 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.CodeGenerator.RuntimeEntity;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Reference extends AST {
public Reference(SourcePosition posn) {
super(posn);
}
public Declaration decl;
public RuntimeEntity entity;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.CodeGenerator.RuntimeEntity;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Reference extends AST {
public Reference(SourcePosition posn) {
super(posn);
}
public String spelling;
public Declaration decl;
public RuntimeEntity entity;
}

View File

@ -1,16 +1,16 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Statement extends AST {
public Statement(SourcePosition posn) {
super(posn);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public abstract class Statement extends AST {
public Statement(SourcePosition posn) {
super(posn);
}
}

View File

@ -1,32 +1,32 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class StatementList implements Iterable<Statement> {
public StatementList() {
slist = new ArrayList<Statement>();
}
public void add(Statement s) {
slist.add(s);
}
public Statement get(int i) {
return slist.get(i);
}
public int size() {
return slist.size();
}
public Iterator<Statement> iterator() {
return slist.iterator();
}
private List<Statement> slist;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import java.util.*;
public class StatementList implements Iterable<Statement> {
public StatementList() {
slist = new ArrayList<Statement>();
}
public void add(Statement s) {
slist.add(s);
}
public Statement get(int i) {
return slist.get(i);
}
public int size() {
return slist.size();
}
public Iterator<Statement> iterator() {
return slist.iterator();
}
private List<Statement> slist;
}

View File

@ -1,18 +1,18 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Terminal extends AST {
public Terminal(String s, SourcePosition posn) {
super(posn);
spelling = s;
}
public String spelling;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Terminal extends AST {
public Terminal(String s, SourcePosition posn) {
super(posn);
spelling = s;
}
public String spelling;
}

View File

@ -1,21 +1,21 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ThisRef extends Reference {
public ThisRef(SourcePosition posn) {
super(posn);
}
@Override
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitThisRef(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class ThisRef extends Reference {
public ThisRef(SourcePosition posn) {
super(posn);
}
@Override
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitThisRef(this, o);
}
}

View File

@ -1,19 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Type extends AST {
public Type(TypeKind typ, SourcePosition posn) {
super(posn);
typeKind = typ;
}
public TypeKind typeKind;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
abstract public class Type extends AST {
public Type(TypeKind typ, SourcePosition posn) {
super(posn);
typeKind = typ;
}
public TypeKind typeKind;
}

View File

@ -1,10 +1,10 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
public enum TypeKind {
VOID, INT, BOOLEAN, CLASS, ARRAY, UNSUPPORTED, ERROR, EQUALS, RELATIONAL;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
public enum TypeKind {
VOID, INT, BOOLEAN, CLASS, ARRAY, UNSUPPORTED, ERROR;
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class UnaryExpr extends Expression {
public UnaryExpr(Operator o, Expression e, SourcePosition posn) {
super(posn);
operator = o;
expr = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitUnaryExpr(this, o);
}
public Operator operator;
public Expression expr;
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class UnaryExpr extends Expression {
public UnaryExpr(Operator o, Expression e, SourcePosition posn) {
super(posn);
operator = o;
expr = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitUnaryExpr(this, o);
}
public Operator operator;
public Expression expr;
}

View File

@ -1,19 +1,19 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDecl extends LocalDecl {
public VarDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitVarDecl(this, o);
}
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDecl extends LocalDecl {
public VarDecl(Type t, String name, SourcePosition posn) {
super(name, t, posn);
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitVarDecl(this, o);
}
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDeclStmt extends Statement {
public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn) {
super(posn);
varDecl = vd;
initExp = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitVardeclStmt(this, o);
}
public VarDecl varDecl;
public Expression initExp;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class VarDeclStmt extends Statement {
public VarDeclStmt(VarDecl vd, Expression e, SourcePosition posn) {
super(posn);
varDecl = vd;
initExp = e;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitVardeclStmt(this, o);
}
public VarDecl varDecl;
public Expression initExp;
}

View File

@ -1,80 +1,80 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
/**
* An implementation of the Visitor interface provides a method visitX for each
* non-abstract AST class X.
*/
public interface Visitor<ArgType, ResultType> {
// Package
public ResultType visitPackage(Package prog, ArgType arg);
// Declarations
public ResultType visitClassDecl(ClassDecl cd, ArgType arg);
public ResultType visitFieldDecl(FieldDecl fd, ArgType arg);
public ResultType visitMethodDecl(MethodDecl md, ArgType arg);
public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg);
public ResultType visitVarDecl(VarDecl decl, ArgType arg);
// Types
public ResultType visitBaseType(BaseType type, ArgType arg);
public ResultType visitClassType(ClassType type, ArgType arg);
public ResultType visitArrayType(ArrayType type, ArgType arg);
// Statements
public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg);
public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg);
public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg);
public ResultType visitCallStmt(CallStmt stmt, ArgType arg);
public ResultType visitIfStmt(IfStmt stmt, ArgType arg);
public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg);
// Expressions
public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg);
public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg);
public ResultType visitRefExpr(RefExpr expr, ArgType arg);
public ResultType visitCallExpr(CallExpr expr, ArgType arg);
public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg);
public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg);
public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg);
// References
public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg);
public ResultType visitIndexedRef(IndexedRef ref, ArgType arg);
public ResultType visitIdRef(IdRef ref, ArgType arg);
public ResultType visitThisRef(ThisRef ref, ArgType arg);
// Terminals
public ResultType visitIdentifier(Identifier id, ArgType arg);
public ResultType visitOperator(Operator op, ArgType arg);
public ResultType visitIntLiteral(IntLiteral num, ArgType arg);
public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg);
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
/**
* An implementation of the Visitor interface provides a method visitX for each
* non-abstract AST class X.
*/
public interface Visitor<ArgType, ResultType> {
// Package
public ResultType visitPackage(Package prog, ArgType arg);
// Declarations
public ResultType visitClassDecl(ClassDecl cd, ArgType arg);
public ResultType visitFieldDecl(FieldDecl fd, ArgType arg);
public ResultType visitMethodDecl(MethodDecl md, ArgType arg);
public ResultType visitParameterDecl(ParameterDecl pd, ArgType arg);
public ResultType visitVarDecl(VarDecl decl, ArgType arg);
// Types
public ResultType visitBaseType(BaseType type, ArgType arg);
public ResultType visitClassType(ClassType type, ArgType arg);
public ResultType visitArrayType(ArrayType type, ArgType arg);
// Statements
public ResultType visitBlockStmt(BlockStmt stmt, ArgType arg);
public ResultType visitVardeclStmt(VarDeclStmt stmt, ArgType arg);
public ResultType visitAssignStmt(AssignStmt stmt, ArgType arg);
public ResultType visitCallStmt(CallStmt stmt, ArgType arg);
public ResultType visitIfStmt(IfStmt stmt, ArgType arg);
public ResultType visitWhileStmt(WhileStmt stmt, ArgType arg);
// Expressions
public ResultType visitUnaryExpr(UnaryExpr expr, ArgType arg);
public ResultType visitBinaryExpr(BinaryExpr expr, ArgType arg);
public ResultType visitRefExpr(RefExpr expr, ArgType arg);
public ResultType visitCallExpr(CallExpr expr, ArgType arg);
public ResultType visitLiteralExpr(LiteralExpr expr, ArgType arg);
public ResultType visitNewObjectExpr(NewObjectExpr expr, ArgType arg);
public ResultType visitNewArrayExpr(NewArrayExpr expr, ArgType arg);
// References
public ResultType visitQualifiedRef(QualifiedRef ref, ArgType arg);
public ResultType visitIndexedRef(IndexedRef ref, ArgType arg);
public ResultType visitIdRef(IdRef ref, ArgType arg);
public ResultType visitThisRef(ThisRef ref, ArgType arg);
// Terminals
public ResultType visitIdentifier(Identifier id, ArgType arg);
public ResultType visitOperator(Operator op, ArgType arg);
public ResultType visitIntLiteral(IntLiteral num, ArgType arg);
public ResultType visitBooleanLiteral(BooleanLiteral bool, ArgType arg);
}

View File

@ -1,23 +1,23 @@
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class WhileStmt extends Statement {
public WhileStmt(Expression b, Statement s, SourcePosition posn) {
super(posn);
cond = b;
body = s;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitWhileStmt(this, o);
}
public Expression cond;
public Statement body;
}
/**
* miniJava Abstract Syntax Tree classes
* @author prins
* @version COMP 520 (v2.2)
*/
package miniJava.AbstractSyntaxTrees;
import miniJava.SyntacticAnalyzer.SourcePosition;
public class WhileStmt extends Statement {
public WhileStmt(Expression b, Statement s, SourcePosition posn) {
super(posn);
cond = b;
body = s;
}
public <A, R> R visit(Visitor<A, R> v, A o) {
return v.visitWhileStmt(this, o);
}
public Expression cond;
public Statement body;
}

View File

@ -0,0 +1,55 @@
package miniJava.CodeGenerator;
import mJAM.Machine;
import miniJava.AbstractSyntaxTrees.*;
public class Code {
public boolean addr;
public Declaration decl;
/**
* If addr true, returns the address of the declaration.
* Otherwise, uses the size of the declaration in its place.
* @param op
* @param decl
* @param addr
*/
public Code(Declaration decl, boolean addr) {
this.decl = decl;
this.addr = addr;
}
/**
*
* @param index
*/
public void modify(int instr) {
// Setup size
switch(decl.type.typeKind) {
case ARRAY:
case CLASS:
Machine.code[instr].n = Machine.addressSize;
case INT:
Machine.code[instr].n = Machine.integerSize;
case BOOLEAN:
Machine.code[instr].n = Machine.booleanSize;
case VOID:
Machine.code[instr].n = 0;
default:
Machine.code[instr].n = -1;
}
// Setup displacement
if(addr) {
Machine.code[instr].d += decl.entity.addr;
} else {
Machine.code[instr].d += decl.entity.size;
}
// Setup register
Machine.code[instr].r = decl.entity.reg.ordinal();
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,52 +1,19 @@
package miniJava.CodeGenerator;
import mJAM.Machine;
import mJAM.Machine.Op;
import mJAM.Machine.Prim;
import mJAM.Machine.Reg;
public class RuntimeEntity {
public Reg reg;
public int size;
public int addr;
public int size = -1; // Size of type
public int addr = -1; // Position relative to register
public int instr = -1; // Instruction relative to CB
public Reg register = Reg.ZR; // Register to offset by
RuntimeEntity parent = null;
// For use with nested elements
public boolean indirect = false;
public RuntimeEntity parent = null;
public RuntimeEntity(int size, int addr, Reg register) {
this(size, addr, register, -1);
}
public RuntimeEntity(int size, int addr, Reg register, int instr) {
public RuntimeEntity(int size, int addr, Reg reg) {
this.reg = reg;
this.size = size;
this.addr = addr;
this.register = register;
this.instr = instr;
}
// Load entity into memory (if this, should call LOADA)
public void load() {
if(parent != null) {
parent.load();
Machine.emit(Op.LOADL, addr);
Machine.emit(Prim.fieldref);
} else {
if(indirect) {
Machine.emit(Op.LOADA, size, register, addr);
} else {
Machine.emit(Op.LOAD, size, register, addr);
}
}
}
public void call() {
if(parent != null) {
parent.load();
} else {
Machine.emit(Op.LOADA, size, Reg.OB, 0);
}
}
}

View File

@ -1,13 +1,21 @@
package miniJava;
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import mJAM.*;
import miniJava.SyntacticAnalyzer.*;
import mJAM.Disassembler;
import mJAM.Interpreter;
import mJAM.ObjectFile;
import miniJava.SyntacticAnalyzer.Scanner;
import miniJava.SyntacticAnalyzer.Parser;
import miniJava.AbstractSyntaxTrees.Package;
import miniJava.ContextualAnalyzer.Analyzer;
import miniJava.AbstractSyntaxTrees.ASTDisplay;
import miniJava.CodeGenerator.Encoder;
import miniJava.Exceptions.*;
import miniJava.ContextualAnalyzer.IdTable;
import miniJava.ContextualAnalyzer.Analyzer;
import miniJava.ContextualAnalyzer.Reporter;
public class Compiler {
@ -26,15 +34,22 @@ public class Compiler {
Scanner scanner = new Scanner(new BufferedReader(input));
Parser parser = new Parser(scanner);
Package p = parser.parse();
// Identification/Type Checking
Analyzer analyzer = new Analyzer();
analyzer.visitPackage(p, null);
int analyzed = analyzer.validate();
// Begin Compilation to mJAM
if(analyzed == 0) {
// Display
ASTDisplay display = new ASTDisplay();
display.showTree(p);
// Contextual Analyzer
IdTable table = new IdTable();
Analyzer analyzer = new Analyzer();
analyzer.visitPackage(p, table);
// Compilation
if(Reporter.error) {
System.exit(rc);
} else {
// Build mJAM assembly
Encoder encoder = new Encoder();
encoder.visitPackage(p, null);
@ -43,23 +58,36 @@ public class Compiler {
String objectFileName = args[0].substring(0, pos) + ".mJAM";
ObjectFile objF = new ObjectFile(objectFileName);
if(objF.write()) {
System.out.println("***Object File Failed.");
Reporter.emit("Object File Failed.");
}
// create asm file using disassembler
String asmCodeFileName = "test.asm";
System.out.print("Writing assembly file ... ");
Disassembler d = new Disassembler(objectFileName);
if (d.disassemble()) {
System.out.println("FAILED!");
return;
}
else
System.out.println("SUCCEEDED");
// run
System.out.println("Running code ... ");
Interpreter.debug(objectFileName, asmCodeFileName);
System.out.println("*** mJAM execution completed");
}
System.exit(analyzed);
System.exit(0);
} catch (FileNotFoundException e) {
System.out.println("***" + e.getMessage());
Reporter.emit(e.getMessage());
} catch (IOException e) {
System.out.println("***" + e.getMessage());
} catch (ScanningException e) {
System.out.println("***" + e.getMessage());
} catch (ParsingException e) {
System.out.println("***" + e.getMessage());
Reporter.emit(e.getMessage());
}
System.exit(rc);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,127 @@
package miniJava.ContextualAnalyzer;
import java.util.HashMap;
import java.util.ArrayList;
import miniJava.Compiler;
import miniJava.AbstractSyntaxTrees.*;
/**
*
*/
public class IdTable {
private IdTable parent;
private ArrayList<HashMap<String, Declaration>> scope;
// /////////////////////////////////////////////////////////////////////////////
//
// CONSTRUCTORS
//
// /////////////////////////////////////////////////////////////////////////////
/**
*
*/
public IdTable() {
this(null);
}
/**
*
* @param parent
*/
public IdTable(IdTable parent) {
this.parent = parent;
this.scope = new ArrayList<>();
push();
}
// /////////////////////////////////////////////////////////////////////////////
//
// ACTIVE SCOPE
//
// /////////////////////////////////////////////////////////////////////////////
/**
*
*/
public void pop() {
int last = scope.size() - 1;
scope.remove(last);
}
/**
*
*/
public void push() {
HashMap<String, Declaration> nested = new HashMap<>();
scope.add(nested);
}
/**
*
*/
public void add(Declaration decl) {
for(int i = 0; i < scope.size(); i++) {
HashMap<String, Declaration> nest = scope.get(i);
if(nest.containsKey(decl.name)) {
Declaration prev = nest.get(decl.name);
if(decl instanceof ClassDecl) {
Reporter.report(decl, prev, "Class");
} else if(decl instanceof FieldDecl) {
Reporter.report(decl, prev, "Field");
} else if(decl instanceof MethodDecl) {
Reporter.report(decl, prev, "Method");
} else if(decl instanceof ParameterDecl) {
Reporter.report(decl, prev, "Parameter");
} else if(decl instanceof VarDecl) {
Reporter.report(decl, prev, "Variable");
}
System.exit(Compiler.rc);
}
}
scope.get(scope.size()-1).put(decl.name, decl);
}
// /////////////////////////////////////////////////////////////////////////////
//
// GETTERS
//
// /////////////////////////////////////////////////////////////////////////////
/**
*
* @param name
*/
public Declaration getDeclaration(String name) {
IdTable current = this;
while (current != null) {
Declaration decl = current.getDeclarationAtScope(name);
if (decl == null) current = current.parent;
else return decl;
}
return null;
}
/**
*
* @param name
*/
public Declaration getDeclarationAtScope(String name) {
for (int i = scope.size() - 1; i >= 0; i--) {
HashMap<String, Declaration> nest = scope.get(i);
if (nest.containsKey(name)) return nest.get(name);
}
return null;
}
}

View File

@ -1,221 +0,0 @@
package miniJava.ContextualAnalyzer;
import java.util.*;
import miniJava.AbstractSyntaxTrees.*;
public class IdentificationTable {
private IdentificationTable parent;
private HashMap<String, IdentificationTable> scope;
private ArrayList<HashMap<String, Declaration>> table;
/**
*
*/
public IdentificationTable() {
this(null);
}
/**
*
* @param parent
*/
public IdentificationTable(IdentificationTable parent) {
this.parent = parent;
this.scope = new HashMap<String, IdentificationTable>();
this.table = new ArrayList<HashMap<String, Declaration>>();
this.table.add(new HashMap<String, Declaration>());
}
/**
* Adds another level for variables to be stored at
*/
public void popLevel() {
table.remove(table.size() - 1);
}
/**
* Removes all variables declared at the current level
*/
public void pushLevel() {
table.add(new HashMap<String, Declaration>());
}
/**
* This method will only ever be called with class/method declarations.
*
* @param decl
* @return
*/
public IdentificationTable openScope(Declaration decl) {
Declaration current = getDeclarationAtScope(decl.name);
if (scope.containsKey(decl.name) || current != null) {
Reporter.report(ErrorType.REDEFINITION, decl, current);
return null;
} else {
table.get(table.size() - 1).put(decl.name, decl);
scope.put(decl.name, new IdentificationTable(this));
return scope.get(decl.name);
}
}
/**
* Return nested scope corresponding to declaration (or null if
* non-existant).
*
* @param decl
* @return
*/
public IdentificationTable getScope(Declaration decl) {
if (scope.containsKey(decl.name)) {
return scope.get(decl.name);
}
return null;
}
/**
* Iterates through all parents and tries to find the specified declaration
* by name.
*
* @param name
* @return
*/
public Declaration getDeclaration(String name) {
IdentificationTable current = this;
while (current != null) {
Declaration decl = current.getDeclarationAtScope(name);
if (decl == null) current = current.parent;
else return decl;
}
return null;
}
/**
* Iterates through levels (from higher to lower) for declaration, returning
* none if it does not exist.
*
* @param name
* @return
*/
public Declaration getDeclarationAtScope(String name) {
for (int i = table.size() - 1; i >= 0; i--) {
HashMap<String, Declaration> level = table.get(i);
if (level.containsKey(name)) return level.get(name);
}
return null;
}
/**
* Add declaration to current table's table member.
*
* @param name
*/
public void setDeclarationAtScope(Declaration decl) {
for (int i = 0; i < table.size(); i++) {
HashMap<String, Declaration> level = table.get(i);
if (level.containsKey(decl.name)) {
Declaration defined = level.get(decl.name);
Reporter.report(ErrorType.REDEFINITION, decl, defined);
return;
}
}
table.get(table.size() - 1).put(decl.name, decl);
}
/**
* Checks whether the specified class has been declared.
*
* @param t
* @return
*/
public boolean classExists(Type t) {
if (t.typeKind == TypeKind.CLASS) {
ClassType ct = (ClassType) t;
return getDeclaration(ct.className.spelling) != null;
}
return true;
}
/**
* Determines whether two types match.
*
* @param t1
* @param t2
* @return
*/
public static boolean match(Type t1, Type t2) {
return IdentificationTable.match(t1, t2, false);
}
/**
* Determines whether two type match, reporting an error if they do not.
*
* @param t1
* @param t2
* @param report
* @return
*/
public static boolean match(Type t1, Type t2, boolean report) {
if (t1.typeKind != t2.typeKind) {
if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
return false;
}
// Check Class Types match
else if (t1.typeKind == TypeKind.CLASS) {
ClassType c1 = (ClassType) t1;
ClassType c2 = (ClassType) t2;
if (!c1.className.spelling.equals(c2.className.spelling)) {
if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
return false;
}
}
// Check array types match
else if (t1.typeKind == TypeKind.ARRAY) {
ArrayType a1 = (ArrayType) t1;
ArrayType a2 = (ArrayType) t2;
if (!IdentificationTable.match(a1.eltType, a2.eltType)) {
if (report) Reporter.report(ErrorType.TYPE_MISMATCH, t1, t2);
return false;
}
}
return true;
}
/**
* Determines if the passed method is a valid entry point for the
* compilation phase.
*
* @param md
* @return
*/
public static boolean isMainMethod(MethodDecl md) {
// Check Declaration
if (!md.isPrivate && md.isStatic && md.type.typeKind == TypeKind.VOID
&& md.name.equals("main") && md.parameterDeclList.size() == 1) {
// Check Parameter Declaration
ParameterDecl pd = md.parameterDeclList.get(0);
if (pd.type.typeKind != TypeKind.ARRAY) return false;
ArrayType at = (ArrayType) pd.type;
if (at.eltType.typeKind != TypeKind.CLASS) return false;
ClassType ct = (ClassType) at.eltType;
return ct.className.spelling.equals("String");
}
return false;
}
}

View File

@ -1,205 +1,27 @@
package miniJava.ContextualAnalyzer;
import miniJava.AbstractSyntaxTrees.*;
enum ErrorType {
THIS,
LENGTH,
VOID_TYPE,
CLASS_IDENTIFER,
VARDECL_USED,
NONFUNCTION_CALL,
FUNCTION_ASSIGNMENT,
UNDEFINED,
STATIC,
VISIBILITY,
NO_RETURN,
TYPE_MISMATCH,
REDEFINITION,
MAIN_UNDECLARED,
INVALID_PARAM_COUNT,
MULTIPLE_MAIN,
UNDECLARED_TYPE,
SINGLE_VARCOND,
INVALID_INDEX
}
import miniJava.AbstractSyntaxTrees.Declaration;
public class Reporter {
public static boolean error = false;
/**
* Convenience function for getting type names.
*
* @param t
* @return
*/
private static String getTypeName(Type t) {
if (t instanceof ClassType) {
ClassType ct = (ClassType) t;
return ct.className.spelling;
} else if (t instanceof ArrayType) {
ArrayType at = (ArrayType) t;
return getTypeName(at.eltType);
}
return t.typeKind.toString();
}
/**
* Convenience method for formatting error message.
*
* @param message
*/
private static void emit(String message) {
public static void emit(String message) {
error = true;
System.out.println("***" + message);
}
/**
* Convenience function for managing all error types.
*
* @param type
* @param a1
* @param a2
* Redefinitions
* @param d1
* @param d2
*/
public static void report(ErrorType type, AST a1, AST a2) {
switch (type) {
// Cannot access 'this' in a static method
case THIS: {
MethodDecl md = (MethodDecl) a2;
emit("Cannot reference 'this' " + a1.posn + " in static method '" + md.name + "' " + md.posn);
break;
}
// Array types have the single field 'length'
case LENGTH: {
emit("Array types have only a single field 'length' (at " + a1.posn + ").");
break;
}
// Can't use a class as an identifier solely
case CLASS_IDENTIFER: {
emit("Cannot use class identifier outside of a qualified reference at " + a1.posn);
break;
}
// Cannot have a parameter of type void
case VOID_TYPE: {
emit("Cannot have a parameter of type void at " + a1.posn);
break;
}
// Attempting to call a non function as a function
case NONFUNCTION_CALL: {
emit("Not a valid function call at " + a1.posn);
break;
}
// Cannot assign a value to a function
case FUNCTION_ASSIGNMENT: {
emit("Cannot assign a value to a function at " + a1.posn);
break;
}
// Tried accessing a non-static member from a static method
case STATIC: {
MemberDecl md = (MemberDecl) a1;
Identifier ident = (Identifier) a2;
emit("'" + md.name + "' " + md.posn + " is an instance member and cannot be accessed at " + ident.posn);
break;
}
// Tried accessing a private member of a different class
case VISIBILITY: {
MemberDecl md = (MemberDecl) a1;
Identifier ident = (Identifier) a2;
emit("'" + md.name + "' " + md.posn + " is a private member and cannot be accessed at " + ident.posn);
break;
}
// Non-void function does not have a return statement
case NO_RETURN: {
MethodDecl md = (MethodDecl) a1;
emit("'" + md.name + "' " + md.posn + " must have a return statement");
break;
}
// The passed types are not the same
case TYPE_MISMATCH: {
String name1 = getTypeName((Type) a1);
String name2 = getTypeName((Type) a2);
if(a1 instanceof ArrayType) name1 += " Array";
if(a2 instanceof ArrayType) name2 += " Array";
emit("Expected type '" + name1 + "' but got '" + name2 + "' " + a2.posn);
break;
}
// Attempting to redeclare a variable
case REDEFINITION: {
emit("Variable at " + a1.posn + " already declared earlier at " + a2.posn);
break;
}
// Identifier could not be found
case UNDEFINED: {
Identifier ident = (Identifier) a1;
emit("Identifier '" + ident.spelling + "' " + ident.posn + " is undeclared.");
break;
}
// A public static void main(String[] args) method was not declared
case MAIN_UNDECLARED: {
emit("A main function was not declared");
break;
}
// Parameter counts of an expression/statement do not match declaration
case INVALID_PARAM_COUNT: {
MethodDecl md = (MethodDecl) a2;
emit("Call to '" + md.name + "' " + a2.posn + " has an invalid parameter count at " + a1.posn);
break;
}
// A public static void main(String[] args) was declared more than once
case MULTIPLE_MAIN: {
emit("Main function at " + a1.posn + " already declared previously at " + a2.posn);
break;
}
// A reference has been made to a non-existant type
case UNDECLARED_TYPE: {
if(a1 instanceof Type) {
String typeName = getTypeName((Type) a1);
emit("'" + typeName + "' " + a1.posn + " has not been declared previously");
} else {
emit("Identifier at " + a1.posn + " could not be identified");
}
break;
}
// A Variable Declaration Statement was made as the only statement of a condition
case SINGLE_VARCOND: {
emit("Conditional statment cannot be followed by a variable declaration statement exclusively " + a1.posn);
break;
}
// An indexed expression must be of an int type
case INVALID_INDEX: {
emit("Index expression is not of type int " + a1.posn);
break;
}
// A variable declaration identifier was used in a var decl statement
case VARDECL_USED: {
emit("Identifier at " + a1.posn + " cannot refer to the variable declaration at " + a2.posn);
break;
}
}
error = true;
public static void report(Declaration d1, Declaration d2, String prefix) {
emit(prefix + " at " + d1.posn + " previously defined at " + d2.posn);
}
}

View File

@ -1,10 +0,0 @@
package miniJava.Exceptions;
public class IdentificationException extends Exception {
private static final long serialVersionUID = 1L;
public IdentificationException() {
super("Identification Error!");
}
}

View File

@ -1,17 +0,0 @@
package miniJava.Exceptions;
import miniJava.SyntacticAnalyzer.Token;
public class ParsingException extends Exception {
private static final long serialVersionUID = 1L;
public ParsingException() {
super("Unidentified parsing error!");
}
public ParsingException(Token t) {
super("Parsing error with " + t.spelling + " at " + t.posn.toString());
}
}

View File

@ -1,11 +0,0 @@
package miniJava.Exceptions;
public class ScanningException extends Exception {
private static final long serialVersionUID = 1L;
public ScanningException(int col, int line) {
super("Scanning error at Column: " + col + ", Line: " + line);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,16 @@
package miniJava.SyntacticAnalyzer;
import java.io.IOException;
/**
*
*/
public class ParsingException extends IOException {
private static final long serialVersionUID = 1L;
public ParsingException(SourcePosition posn) {
super("Parsing error at " + posn);
}
}

View File

@ -1,313 +1,306 @@
package miniJava.SyntacticAnalyzer;
import java.io.*;
import miniJava.Exceptions.*;
public class Scanner {
private int col = 1;
private int line = 1;
private boolean predefined;
private BufferedReader input;
public Scanner(BufferedReader input) {
this.input = input;
}
public Scanner(String input) {
StringReader reader = new StringReader(input);
this.input = new BufferedReader(reader);
}
/**
*
* @param input
*/
public Scanner(BufferedReader input) {
this(input, false);
}
/**
*
* @param input
* @param predefined
*/
public Scanner(String input, boolean predefined) {
this(new BufferedReader(new StringReader(input)), predefined);
}
/**
*
* @param input
* @param predefined
*/
public Scanner(BufferedReader input, boolean predefined) {
this.input = input;
this.predefined = predefined;
}
// /////////////////////////////////////////////////////////////////////////////
//
// Scanning
//
// /////////////////////////////////////////////////////////////////////////////
/**
* Scans in input, returning next token.
*
* @return
* @throws IOException
*/
public Token scan() throws ScanningException {
String attr = "";
public Token scan() throws IOException {
Token token = null;
String spelling = "";
while (token == null) {
// Check for EOF
int c = read();
if (c == -1)
return new Token("", Token.TYPE.EOT);
SourcePosition posn = new SourcePosition(col, line);
if(c == -1) {
token = new Token("", Token.TYPE.EOT, posn);
} else {
spelling += (char) c;
switch(c) {
// Setup
attr += (char) c;
switch (c) {
// Operators
case '*':
token = new Token(attr, Token.TYPE.BINOP);
break;
case '+':
if (peek('+'))
throw new ScanningException(col, line);
token = new Token(attr, Token.TYPE.BINOP);
break;
case '-':
if (peek('-'))
throw new ScanningException(col, line);
token = new Token(attr, Token.TYPE.BINOP);
break;
// Check for comment
case '/':
if (peek('*')) {
read();
readComment();
attr = "";
} else if (peek('/')) {
readLine();
attr = "";
} else
token = new Token(attr, Token.TYPE.BINOP);
break;
// Check for c or c=
case '>':
case '<':
if (peek('='))
attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP);
break;
// Check for ! or !=
case '!':
if (!peek('='))
token = new Token(attr, Token.TYPE.UNOP);
else {
attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP);
}
break;
// Check for && or ||
case '&':
case '|':
if (!peek((char) c))
throw new ScanningException(col, line);
else {
attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP);
}
break;
// Other Operators
case '=':
if (!peek('='))
token = new Token(attr, Token.TYPE.EQUALS);
else {
attr += (char) read();
token = new Token(attr, Token.TYPE.BINOP);
}
break;
case '.':
token = new Token(attr, Token.TYPE.PERIOD);
break;
case ',':
token = new Token(attr, Token.TYPE.COMMA);
break;
case '[':
token = new Token(attr, Token.TYPE.LSQUARE);
break;
case ']':
token = new Token(attr, Token.TYPE.RSQUARE);
break;
case '{':
token = new Token(attr, Token.TYPE.LBRACKET);
break;
case '}':
token = new Token(attr, Token.TYPE.RBRACKET);
break;
case '(':
token = new Token(attr, Token.TYPE.LPAREN);
break;
case ')':
token = new Token(attr, Token.TYPE.RPAREN);
break;
case ';':
token = new Token(attr, Token.TYPE.SEMICOLON);
break;
default:
// Identifier or Keyword
if (isAlpha((char) c)) {
for (char n = peek(); isAlpha(n) || isDigit(n);) {
attr += (char) read();
n = peek();
// Operators
case '*':
case '+':
case '-': {
if(peek(c)) throw new ScanningException(posn);
token = new Token(spelling, Token.TYPE.BINOP, posn);
break;
}
// Comment
case '/': {
if(peek('*')) {
read();
readMultiLineComment();
spelling = "";
} else if(peek('/')) {
readSingleLineComment();
spelling = "";
} else {
token = new Token(spelling, Token.TYPE.BINOP, posn);
}
break;
}
// Relational
case '>':
case '<': {
if (peek('=')) spelling += (char) read();
token = new Token(spelling, Token.TYPE.BINOP, posn);
break;
}
// Negation
case '!': {
if(peek('=')) {
spelling += (char) read();
token = new Token(spelling, Token.TYPE.BINOP, posn);
} else {
token = new Token(spelling, Token.TYPE.UNOP, posn);
}
break;
}
// Logical
case '&':
case '|': {
if(!peek(c)) {
throw new ScanningException(posn);
} else {
spelling += (char) read();
token = new Token(spelling, Token.TYPE.BINOP, posn);
}
break;
}
// Other Operators
case '=': {
if(peek('=')) {
spelling += (char) read();
token = new Token(spelling, Token.TYPE.BINOP, posn);
} else {
token = new Token(spelling, Token.TYPE.EQUALS, posn);
}
break;
}
// Miscellaneous
case '.':
case ',':
case '[':
case ']':
case '{':
case '}':
case '(':
case ')':
case ';': {
token = new Token(spelling, Token.symbols.get(c), posn);
break;
}
default: {
// Identifier or keyword
if(isAlpha(c)) {
int next = peek();
while(isAlpha(next) || isDigit(next) || next == '_') {
spelling += (char) read();
next = peek();
}
if(Token.keywords.containsKey(spelling)) {
token = new Token(spelling, Token.keywords.get(spelling), posn);
} else {
token = new Token(spelling, Token.TYPE.ID, posn);
}
}
// Number
else if(isDigit(c)) {
int next = peek();
while(isDigit(next)) {
spelling += (char) read();
next = peek();
}
if (Token.keywords.containsKey(attr)) {
token = new Token(attr, Token.keywords.get(attr));
} else {
token = new Token(attr, Token.TYPE.ID);
token = new Token(spelling, Token.TYPE.NUM, posn);
}
// Whitespace
else if(isWhitespace(c)) {
spelling = "";
}
// Unrecognized Character
else {
throw new ScanningException(posn);
}
}
}
// Number
else if (isDigit((char) c)) {
for (char n = peek(); isDigit(n);) {
attr += (char) read();
n = peek();
}
token = new Token(attr, Token.TYPE.NUM);
}
// Whitespace
else if (isWhitespace((char) c)) {
attr = "";
}
// Unrecognized Character
else
throw new ScanningException(col, line);
;
break;
}
}
token.posn = new SourcePosition(line, col - token.spelling.length());
return token;
}
// /////////////////////////////////////////////////////////////////////////////
//
// Convenience Methods
//
// /////////////////////////////////////////////////////////////////////////////
/**
* Looks at next character in stream without consuming.
*
* @return
* @throws IOException
*/
private char peek() throws ScanningException {
try {
input.mark(1);
int next = input.read();
input.reset();
return next == -1 ? '\0' : (char) next;
} catch (IOException e) {
throw new ScanningException(col, line);
}
}
/**
* Returns whether passed character is next in stream.
*
* @param c
* @return
* @throws IOException
*/
private boolean peek(char c) throws ScanningException {
try {
input.mark(1);
int next = input.read();
input.reset();
return c == next;
} catch (IOException e) {
throw new ScanningException(col, line);
}
}
/**
* Alternative reading that keeps track of position.
*
* @return
* @throws IOException
*/
private int read() throws ScanningException {
try {
int next = input.read();
if (next != '\n' && next != '\r')
col += 1;
else {
col = 1;
line += 1;
if (peek('\r') || peek('\n'))
next = input.read();
}
return next;
} catch (IOException e) {
throw new ScanningException(col, line);
}
}
/**
* Consumes input until an end of comment has been reached.
*
* @throws IOException
*/
private void readComment() throws ScanningException {
char prev = '\0', current = '\0';
while (prev != '*' || current != '/') {
prev = current;
int next = read();
if (next == -1)
throw new ScanningException(col, line);
else
current = (char) next;
}
}
/**
* Consumes input until the end of line is reached
*
* @throws IOException
*/
private void readLine() throws ScanningException {
for (int n = 0; n != '\n' && n != '\r' && n != -1; n = read()) {
}
}
/**
* Tells whether character is alphabetical.
*
* @param c
* @return
*/
private boolean isAlpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
private boolean isAlpha(int c) {
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| (predefined && c == '_');
}
/**
* Tells whether character is numerical.
*
* @param c
* @return
*/
private boolean isDigit(char c) {
private boolean isDigit(int c) {
return c >= '0' && c <= '9';
}
/**
* Tells wheter character is whitespace.
*
* @param c
* @return
*/
private boolean isWhitespace(char c) {
private boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
/**
*
* @return
* @throws IOException
*/
private int peek() throws IOException {
input.mark(1);
int next = input.read();
input.reset();
return next;
}
/**
*
* @param c
* @return
* @throws IOException
*/
private boolean peek(int c) throws IOException {
input.mark(1);
int next = input.read();
input.reset();
return c == next;
}
/**
*
* @return
* @throws IOException
*/
private int read() throws IOException {
int next = input.read();
if(next == '\n' || next == '\r') {
col = 1;
line += 1;
} else {
col += 1;
}
return next;
}
/**
*
* @throws IOException
*/
private void readSingleLineComment() throws IOException {
col = 1;
line += 1;
input.readLine();
}
/**
*
* @throws IOException
*/
private void readMultiLineComment() throws IOException {
int prev = '\0';
int current = '\0';
while(prev != '*' || current != '/') {
prev = current;
current = read();
// Unterminated
if(current == -1) {
SourcePosition posn = new SourcePosition(line, col);
throw new ScanningException(posn);
}
}
}
}

View File

@ -0,0 +1,16 @@
package miniJava.SyntacticAnalyzer;
import java.io.IOException;
/**
*
*/
public class ScanningException extends IOException {
private static final long serialVersionUID = 1L;
public ScanningException(SourcePosition posn) {
super("Scanning error at " + posn);
}
}

View File

@ -1,11 +1,14 @@
package miniJava.SyntacticAnalyzer;
/**
*
*/
public class SourcePosition {
public final int col;
public final int line;
public SourcePosition(int line, int col) {
public SourcePosition(int col, int line) {
this.col = col;
this.line = line;
}

View File

@ -2,26 +2,51 @@ package miniJava.SyntacticAnalyzer;
import java.util.HashMap;
/**
*
*/
public class Token {
public enum TYPE {
// Possible Terminals
ID, NUM, UNOP, BINOP,
// Terminals
ID,
NUM,
UNOP,
BINOP,
EQUALS,
PERIOD,
COMMA,
LPAREN,
RPAREN,
LSQUARE,
RSQUARE,
LBRACKET,
RBRACKET,
SEMICOLON,
// Keywords
IF, ELSE, NEW, INT, VOID, THIS, TRUE, FALSE, CLASS, WHILE, RETURN, BOOLEAN,
// Declarators
STATIC, PUBLIC, PRIVATE,
// Other Terminals
EQUALS, PERIOD, COMMA, LPAREN, RPAREN, LSQUARE, RSQUARE, LBRACKET, RBRACKET, SEMICOLON,
IF,
ELSE,
NEW,
INT,
VOID,
THIS,
TRUE,
FALSE,
CLASS,
WHILE,
RETURN,
BOOLEAN,
STATIC,
PUBLIC,
PRIVATE,
// End of Token Stream
EOT
};
// Pair words with enumeration
public final static HashMap<String, TYPE> keywords;
static {
keywords = new HashMap<String, TYPE>();
@ -41,14 +66,29 @@ public class Token {
keywords.put("false", TYPE.FALSE);
keywords.put("new", TYPE.NEW);
}
// Pair symbols with enumeration
public final static HashMap<Integer, TYPE> symbols;
static {
symbols = new HashMap<Integer, TYPE>();
symbols.put((int) '.', TYPE.PERIOD);
symbols.put((int) ',', TYPE.COMMA);
symbols.put((int) '[', TYPE.LSQUARE);
symbols.put((int) ']', TYPE.RSQUARE);
symbols.put((int) '{', TYPE.LBRACKET);
symbols.put((int) '}', TYPE.RBRACKET);
symbols.put((int) '(', TYPE.LPAREN);
symbols.put((int) ')', TYPE.RPAREN);
symbols.put((int) ';', TYPE.SEMICOLON);
}
public final TYPE type;
public SourcePosition posn;
public final String spelling;
public final SourcePosition posn;
public Token(String spelling, TYPE type) {
public Token(String spelling, TYPE type, SourcePosition posn) {
this.type = type;
this.posn = null;
this.posn = posn;
this.spelling = spelling;
}
}

View File

@ -0,0 +1,71 @@
package tester;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/* Automated regression tester for Checkpoint 1 tests
* Created by Max Beckman-Harned
* Put your tests in "tests/pa1_tests" folder in your Eclipse workspace directory
*/
public class Checkpoint1 {
static ExecutorService threadPool = Executors.newCachedThreadPool();
public static void main(String[] args) throws IOException, InterruptedException {
File testDir = new File(System.getProperty("java.class.path")
+ "/../tests/pa1_tests");
int failures = 0;
for (File x : testDir.listFiles()) {
int returnCode = runTest(x);
if (x.getName().indexOf("pass") != -1) {
if (returnCode == 0)
System.out.println(x.getName() + " passed successfully!");
else {
failures++;
System.err.println(x.getName()
+ " failed but should have passed!");
}
} else {
if (returnCode == 4)
System.out.println(x.getName() + " failed successfully!");
else {
System.err.println(x.getName() + " did not fail properly!");
failures++;
}
}
}
System.out.println(failures + " failures in all.");
}
private static int runTest(File x) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "miniJava.Compiler", x.getPath()).directory(new File(System.getProperty("java.class.path")));
Process p = pb.start();
threadPool.execute(new ProcessOutputter(p.getInputStream(), false));
p.waitFor();
return p.exitValue();
}
static class ProcessOutputter implements Runnable {
private Scanner processOutput;
private boolean output;
public ProcessOutputter(InputStream _processStream, boolean _output) {
processOutput = new Scanner(_processStream);
output = _output;
}
@Override
public void run() {
while(processOutput.hasNextLine()) {
String line = processOutput.nextLine();
if (output)
System.out.println(line);
}
}
}
}

View File

@ -0,0 +1,98 @@
package tester;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.Scanner;
/* Automated regression tester for Checkpoint 2 tests
* Created by Max Beckman-Harned
* Put your tests in "tests/pa2_tests" folder in your Eclipse workspace directory
* If you preface your error messages / exceptions with ERROR or *** then they will be displayed if they appear during processing
*/
public class Checkpoint2 {
private static class ReturnInfo {
int returnCode;
String ast;
public ReturnInfo(int _returnCode, String _ast) {
returnCode = _returnCode;
ast = _ast;
}
}
public static void main(String[] args) throws IOException, InterruptedException {
File testDir = new File(System.getProperty("java.class.path")
+ "/../tests/pa2_tests");
int failures = 0;
for (File x : testDir.listFiles()) {
if (x.getName().endsWith("out") || x.getName().startsWith("."))
continue;
ReturnInfo info = runTest(x);
int returnCode = info.returnCode;
String ast = info.ast;
if (x.getName().indexOf("pass") != -1) {
if (returnCode == 0) {
String actualAST = getAST(new FileInputStream(x.getPath() + ".out"));
if (actualAST.equals(ast))
System.out.println(x.getName() + " parsed successfully and has a correct AST!");
else {
System.err.println(x.getName() + " parsed successfully but has an incorrect AST!");
failures++;
}
}
else {
failures++;
System.err.println(x.getName()
+ " failed to be parsed!");
}
} else {
if (returnCode == 4)
System.out.println(x.getName() + " failed successfully!");
else {
System.err.println(x.getName() + " did not fail properly!");
failures++;
}
}
}
System.out.println(failures + " failures in all.");
}
private static ReturnInfo runTest(File x) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "miniJava.Compiler", x.getPath()).directory(new File(System.getProperty("java.class.path")));
pb.redirectErrorStream(true);
Process p = pb.start();
String ast = getAST(p.getInputStream());
p.waitFor();
int exitValue = p.exitValue();
return new ReturnInfo(exitValue, ast);
}
public static String getAST(InputStream stream) {
Scanner scan = new Scanner(stream);
String ast = null;
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.equals("======= AST Display =========================")) {
line = scan.nextLine();
while(scan.hasNext() && !line.equals("=============================================")) {
ast += line + "\n";
line = scan.nextLine();
}
}
if (line.startsWith("*** "))
System.out.println(line);
if (line.startsWith("ERROR")) {
System.out.println(line);
while(scan.hasNext())
System.out.println(scan.next());
}
}
scan.close();
return ast;
}
}

View File

@ -0,0 +1,73 @@
package tester;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
/* Automated regression tester for Checkpoint 3 tests
* Created by Max Beckman-Harned
* Put your tests in "tests/pa3_tests" folder in your Eclipse workspace directory
* If you preface your error messages / exceptions with ERROR or *** then they will be displayed if they appear during processing
*/
public class Checkpoint3 {
public static void main(String[] args) throws IOException, InterruptedException {
File testDir = new File(System.getProperty("java.class.path")
+ "/../tests/pa3_tests");
int failures = 0;
for (File x : testDir.listFiles()) {
if (x.getName().endsWith("out") || x.getName().startsWith(".") || x.getName().endsWith("mJAM") || x.getName().endsWith("asm"))
continue;
int returnCode = runTest(x);
if (x.getName().indexOf("pass") != -1) {
if (returnCode == 0) {
System.out.println(x.getName() + " processed successfully!");
}
else {
failures++;
System.err.println(x.getName()
+ " failed to be processed!");
}
} else {
if (returnCode == 4)
System.out.println(x.getName() + " failed successfully!");
else {
System.err.println(x.getName() + " did not fail properly!");
failures++;
}
}
}
System.out.println(failures + " failures in all.");
}
private static int runTest(File x) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "miniJava.Compiler", x.getPath()).directory(new File(System.getProperty("java.class.path")));
pb.redirectErrorStream(true);
Process p = pb.start();
processStream(p.getInputStream());
p.waitFor();
int exitValue = p.exitValue();
return exitValue;
}
public static void processStream(InputStream stream) {
Scanner scan = new Scanner(stream);
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.startsWith("*** "))
System.out.println(line);
if (line.startsWith("ERROR")) {
System.out.println(line);
//while(scan.hasNext())
//System.out.println(scan.next());
}
}
scan.close();
}
}

112
src/tester/Checkpoint4.java Normal file
View File

@ -0,0 +1,112 @@
package tester;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
/* Automated regression tester for Checkpoint 4 tests
* Created by Max Beckman-Harned
* Put your tests in "tests/pa4_tests" folder in your Eclipse workspace directory
* If you preface your compiler error messages / exceptions with ERROR or *** then they will be displayed if they appear during processing
*/
public class Checkpoint4 {
public static void main(String[] args) throws IOException, InterruptedException {
File testDir = new File(System.getProperty("java.class.path")
+ "/../tests/pa4_tests");
int failures = 0;
for (File x : testDir.listFiles()) {
if (x.getName().startsWith(".") || x.getName().endsWith("mJAM") || x.getName().endsWith("asm"))
continue;
int returnCode = runTest(x);
if (x.getName().indexOf("pass") != -1) {
if (returnCode == 0) {
try {
int val = executeTest(x);
int expected = Integer.parseInt(x.getName().substring(5,7));
if (val == expected)
System.out.println(x.getName() + " ran successfully!");
else {
failures++;
System.err.println(x.getName() + " compiled but did not run successfully--got output " + val);
}
}
catch(Exception ex) {
failures++;
System.err.println(x.getName() + " did not output correctly.");
}
}
else {
failures++;
System.err.println(x.getName()
+ " failed to be processed!");
}
} else {
if (returnCode == 4)
System.out.println(x.getName() + " failed successfully!");
else {
System.err.println(x.getName() + " did not fail properly!");
failures++;
}
}
}
System.out.println(failures + " failures in all.");
}
private static int runTest(File x) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "miniJava.Compiler", x.getPath()).directory(new File(System.getProperty("java.class.path")));
pb.redirectErrorStream(true);
Process p = pb.start();
processStream(p.getInputStream());
p.waitFor();
int exitValue = p.exitValue();
return exitValue;
}
private static int executeTest(File x) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder("java", "mJAM.Interpreter", x.getPath().replace(".java", ".mJAM")).directory(new File(System.getProperty("java.class.path")));
Process process = pb.start();
Scanner scan = new Scanner(process.getInputStream());
int num = -1;
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.startsWith(">>> ")) {
num = Integer.parseInt(line.substring(4));
System.out.println("Result = " + num);
break;
}
}
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.startsWith("*** ")) {
System.out.println(line);
break;
}
}
scan.close();
return num;
}
public static void processStream(InputStream stream) {
Scanner scan = new Scanner(stream);
while (scan.hasNextLine()) {
String line = scan.nextLine();
if (line.startsWith("*** "))
System.out.println(line);
if (line.startsWith("ERROR")) {
System.out.println(line);
//while(scan.hasNext())
//System.out.println(scan.next());
}
}
scan.close();
}
}