diff options
| author | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
|---|---|---|
| committer | Elizabeth Alexander Hunt <me@liz.coffee> | 2026-07-02 11:55:17 -0700 |
| commit | 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 (patch) | |
| tree | ed97e39ec77c5231ffd2c394493e68d00ddac5a4 /Homework/cs5300/compiler/submit | |
| download | misc-undergrad-main.tar.gz misc-undergrad-main.zip | |
Diffstat (limited to 'Homework/cs5300/compiler/submit')
31 files changed, 1199 insertions, 0 deletions
diff --git a/Homework/cs5300/compiler/submit/ASTVisitor.java b/Homework/cs5300/compiler/submit/ASTVisitor.java new file mode 100644 index 0000000..cbd794f --- /dev/null +++ b/Homework/cs5300/compiler/submit/ASTVisitor.java @@ -0,0 +1,276 @@ +package submit; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.antlr.v4.runtime.tree.TerminalNode; +import parser.CminusBaseVisitor; +import parser.CminusParser; +import submit.ast.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +public class ASTVisitor extends CminusBaseVisitor<Node> { + private final Logger LOGGER; + private SymbolTable symbolTable; + + public ASTVisitor(Logger LOGGER) { + this.LOGGER = LOGGER; + } + + private VarType getVarType(CminusParser.TypeSpecifierContext ctx) { + final String t = ctx.getText(); + return (t.equals("int")) ? VarType.INT : (t.equals("bool")) ? VarType.BOOL : VarType.CHAR; + } + + @Override public Node visitProgram(CminusParser.ProgramContext ctx) { + symbolTable = new SymbolTable(); + List<Declaration> decls = new ArrayList<>(); + for (CminusParser.DeclarationContext d : ctx.declaration()) { + decls.add((Declaration) visitDeclaration(d)); + } + return new Program(decls); + } + + @Override public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + List<String> ids = new ArrayList<>(); + List<Integer> arraySizes = new ArrayList<>(); + for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) { + String id = v.ID().getText(); + ids.add(id); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + if (v.NUMCONST() != null) { + arraySizes.add(Integer.parseInt(v.NUMCONST().getText())); + } else { + arraySizes.add(-1); + } + } + final boolean isStatic = false; + return new VarDeclaration(type, ids, arraySizes, isStatic); + } + + @Override public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { + VarType returnType = null; + if (ctx.typeSpecifier() != null) { + returnType = getVarType(ctx.typeSpecifier()); + } + String id = ctx.ID().getText(); + List<Param> params = new ArrayList<>(); + for (CminusParser.ParamContext p : ctx.param()) { + params.add((Param) visitParam(p)); + } + Statement statement = (Statement) visitStatement(ctx.statement()); + symbolTable.addSymbol(id, new SymbolInfo(id, returnType, true)); + return new FunDeclaration(returnType, id, params, statement); + } + + @Override public Node visitParam(CminusParser.ParamContext ctx) { + VarType type = getVarType(ctx.typeSpecifier()); + String id = ctx.paramId().ID().getText(); + symbolTable.addSymbol(id, new SymbolInfo(id, type, false)); + return new Param(type, id, ctx.paramId().children.size() > 1); + } + + @Override public Node visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { + symbolTable = symbolTable.createChild(); + List<Statement> statements = new ArrayList<>(); + for (CminusParser.VarDeclarationContext d : ctx.varDeclaration()) { + statements.add((VarDeclaration) visitVarDeclaration(d)); + } + for (CminusParser.StatementContext d : ctx.statement()) { + statements.add((Statement) visitStatement(d)); + } + symbolTable = symbolTable.getParent(); + return new CompoundStatement(statements); + } + + @Override public Node visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { + if (ctx.expression() == null) { + return Statement.empty(); + } + return new ExpressionStatement((Expression) visitExpression(ctx.expression())); + } + + @Override public Node visitIfStmt(CminusParser.IfStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement trueStatement = (Statement) visitStatement(ctx.statement(0)); + Statement falseStatement = null; + if (ctx.statement().size() > 1) { + falseStatement = (Statement) visitStatement(ctx.statement(1)); + } + return new If(expression, trueStatement, falseStatement); + } + + @Override public Node visitWhileStmt(CminusParser.WhileStmtContext ctx) { + Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression()); + Statement statement = (Statement) visitStatement(ctx.statement()); + return new While(expression, statement); + } + + @Override public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) { + if (ctx.expression() != null) { + return new Return((Expression) visitExpression(ctx.expression())); + } + return new Return(null); + } + + @Override public Node visitBreakStmt(CminusParser.BreakStmtContext ctx) { + return new Break(); + } + + @Override public Node visitExpression(CminusParser.ExpressionContext ctx) { + final Node ret; + CminusParser.MutableContext mutable = ctx.mutable(); + CminusParser.ExpressionContext expression = ctx.expression(); + if (mutable != null) { + // Assignment + ParseTree operator = ctx.getChild(1); + Mutable lhs = (Mutable) visitMutable(mutable);//new Mutable(mutable.ID().getText(), (Expression) visitExpression(mutable.expression())); + Expression rhs = null; + if (expression != null) { + rhs = (Expression) visitExpression(expression); + } + ret = new Assignment(lhs, operator.getText(), rhs); + } else { + ret = visitSimpleExpression(ctx.simpleExpression()); + } + return ret; + } + + @Override public Node visitOrExpression(CminusParser.OrExpressionContext ctx) { + List<Node> ands = new ArrayList<>(); + for (CminusParser.AndExpressionContext and : ctx.andExpression()) { + ands.add(visitAndExpression(and)); + } + if (ands.size() == 1) { + return ands.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)ands.get(0), "||", (Expression)ands.get(1)); + for (int i = 2; i < ands.size(); ++i) { + op = new BinaryOperator(op, "||", (Expression) ands.get(i)); + } + return op; + } + + @Override public Node visitAndExpression(CminusParser.AndExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.UnaryRelExpressionContext un : ctx.unaryRelExpression()) { + uns.add(visitUnaryRelExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)uns.get(0), "&&", (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, "&&", (Expression) uns.get(i)); + } + return op; + } + + @Override public Node visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { + Expression e = (Expression)visitRelExpression(ctx.relExpression()); + for (TerminalNode n : ctx.BANG()) { + e = new UnaryOperator("!", e); + } + return e; + } + + @Override public Node visitRelExpression(CminusParser.RelExpressionContext ctx) { + List<Node> uns = new ArrayList<>(); + for (CminusParser.SumExpressionContext un : ctx.sumExpression()) { + uns.add(visitSumExpression(un)); + } + if (uns.size() == 1) { + return uns.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)uns.get(0), ctx.relop(0).getText(), (Expression)uns.get(1)); + for (int i = 2; i < uns.size(); ++i) { + op = new BinaryOperator(op, ctx.relop(i-1).getText(), (Expression) uns.get(i)); + } + return op; + } + + @Override public Node visitSumExpression(CminusParser.SumExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.TermExpressionContext e : ctx.termExpression()) { + es.add(visitTermExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)es.get(0), ctx.sumop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.sumop(i-1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override public Node visitTermExpression(CminusParser.TermExpressionContext ctx) { + List<Node> es = new ArrayList<>(); + for (CminusParser.UnaryExpressionContext e : ctx.unaryExpression()) { + es.add(visitUnaryExpression(e)); + } + if (es.size() == 1) { + return es.get(0); + } + BinaryOperator op = new BinaryOperator((Expression)es.get(0), ctx.mulop(0).getText(), (Expression)es.get(1)); + for (int i = 2; i < es.size(); ++i) { + op = new BinaryOperator(op, ctx.mulop(i-1).getText(), (Expression) es.get(i)); + } + return op; + } + + @Override public Node visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { + Node ret = visitFactor(ctx.factor()); + for (int i = ctx.unaryop().size()-1; i >= 0; i--) { + ret = new UnaryOperator(ctx.unaryop(i).getText(), (Expression)ret); + } + return ret; + } + + @Override public Node visitMutable(CminusParser.MutableContext ctx) { + Expression e = null; + if (ctx.expression() != null) { + e = (Expression) visitExpression(ctx.expression()); + } + String id = ctx.ID().getText(); + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Mutable(id, e); + } + + @Override public Node visitImmutable(CminusParser.ImmutableContext ctx) { + if (ctx.expression() != null) { + return new ParenExpression((Expression) visitExpression(ctx.expression())); + } + return visitChildren(ctx); + } + + @Override public Node visitCall(CminusParser.CallContext ctx) { + final String id = ctx.ID().getText(); + final List<Expression> args = new ArrayList<>(); + for (CminusParser.ExpressionContext e : ctx.expression()) { + args.add((Expression) visitExpression(e)); + } + if (symbolTable.find(id) == null) { + LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id); + } + return new Call(id, args); + } + + @Override public Node visitConstant(CminusParser.ConstantContext ctx) { + final Node node; + if (ctx.NUMCONST() != null) { + node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText())); + } else if (ctx.CHARCONST() != null) { + node = new CharConstant(ctx.CHARCONST().getText().charAt(0)); + } else if (ctx.STRINGCONST() != null) { + node = new StringConstant(ctx.STRINGCONST().getText()); + } else { + node = new BoolConstant(ctx.getText().equals("true")); + } + return node; + } +} diff --git a/Homework/cs5300/compiler/submit/SymbolInfo.java b/Homework/cs5300/compiler/submit/SymbolInfo.java new file mode 100644 index 0000000..19c3f9b --- /dev/null +++ b/Homework/cs5300/compiler/submit/SymbolInfo.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit; + +import submit.ast.VarType; + +/** + * + * @author edwajohn + */ +public class SymbolInfo { + + private final String id; + // In the case of a function, type is the return type + private final VarType type; + private final boolean function; + + public SymbolInfo(String id, VarType type, boolean function) { + this.id = id; + this.type = type; + this.function = function; + } + + @Override + public String toString() { + return "<" + id + ", " + type + '>'; + } + +} diff --git a/Homework/cs5300/compiler/submit/SymbolTable.java b/Homework/cs5300/compiler/submit/SymbolTable.java new file mode 100644 index 0000000..7ca6b27 --- /dev/null +++ b/Homework/cs5300/compiler/submit/SymbolTable.java @@ -0,0 +1,63 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/* + * Code formatter project + * CS 4481 + */ +/** + * + */ +public class SymbolTable { + + private final HashMap<String, SymbolInfo> table; + private SymbolTable parent; + private final List<SymbolTable> children; + + public SymbolTable() { + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + } + + public void addSymbol(String id, SymbolInfo symbol) { + table.put(id, symbol); + } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { + return parent; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Assignment.java b/Homework/cs5300/compiler/submit/ast/Assignment.java new file mode 100644 index 0000000..9edc536 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Assignment.java @@ -0,0 +1,34 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Assignment implements Expression, Node { + + private final Mutable mutable; + private final AssignmentType type; + private final Expression rhs; + + public Assignment(Mutable mutable, String assign, Expression rhs) { + this.mutable = mutable; + this.type = AssignmentType.fromString(assign); + this.rhs = rhs; + } + + public void toCminus(StringBuilder builder, final String prefix) { + mutable.toCminus(builder, prefix); + if (rhs != null) { + builder.append(" ").append(type.toString()).append(" "); + rhs.toCminus(builder, prefix); + } else { + builder.append(type.toString()); + + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/AssignmentType.java b/Homework/cs5300/compiler/submit/ast/AssignmentType.java new file mode 100644 index 0000000..9dd2bdf --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/AssignmentType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum AssignmentType { + + EQUALS("="), PLUS("+="), MINUS("-="), TIMES("*="), DIVIDE("/="), INC("++"), DEC("--"); + + private final String value; + + private AssignmentType(String value) { + this.value = value; + } + + public static AssignmentType fromString(String s) { + for (AssignmentType at : AssignmentType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in AssignType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BinaryOperator.java b/Homework/cs5300/compiler/submit/ast/BinaryOperator.java new file mode 100644 index 0000000..747b3ba --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BinaryOperator.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BinaryOperator implements Expression { + + private final Expression lhs, rhs; + private final BinaryOperatorType type; + + public BinaryOperator(Expression lhs, BinaryOperatorType type, Expression rhs) { + this.lhs = lhs; + this.type = type; + this.rhs = rhs; + } + + public BinaryOperator(Expression lhs, String type, Expression rhs) { + this.lhs = lhs; + this.type = BinaryOperatorType.fromString(type); + this.rhs = rhs; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + lhs.toCminus(builder, prefix); + builder.append(" ").append(type).append(" "); + rhs.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java b/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java new file mode 100644 index 0000000..37235c8 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BinaryOperatorType.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum BinaryOperatorType { + + OR("||"), AND("&&"), + LE("<="), LT("<"), GT(">"), GE(">="), EQ("=="), NE("!="), + PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"), MOD("%"); + + private final String value; + + private BinaryOperatorType(String value) { + this.value = value; + } + + public static BinaryOperatorType fromString(String s) { + for (BinaryOperatorType at : BinaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in OperatorType.fromString(): " + s); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/BoolConstant.java b/Homework/cs5300/compiler/submit/ast/BoolConstant.java new file mode 100644 index 0000000..963bebe --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/BoolConstant.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class BoolConstant implements Expression { + + private final boolean value; + + public BoolConstant(boolean value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (value) { + builder.append("true"); + } else { + builder.append("false"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Break.java b/Homework/cs5300/compiler/submit/ast/Break.java new file mode 100644 index 0000000..006d4d3 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Break.java @@ -0,0 +1,18 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Break implements Statement { + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("break;\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Call.java b/Homework/cs5300/compiler/submit/ast/Call.java new file mode 100644 index 0000000..700799b --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Call.java @@ -0,0 +1,37 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Call implements Expression { + + private final String id; + private final List<Expression> args; + + public Call(String id, List<Expression> args) { + this.id = id; + this.args = new ArrayList<>(args); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id).append("("); + for (Expression arg : args) { + arg.toCminus(builder, prefix); + builder.append(", "); + } + if (!args.isEmpty()) { + builder.setLength(builder.length() - 2); + } + builder.append(")"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/CharConstant.java b/Homework/cs5300/compiler/submit/ast/CharConstant.java new file mode 100644 index 0000000..8f41fac --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/CharConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class CharConstant implements Expression { + + private final char value; + + public CharConstant(char value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("'").append(value).append("'"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/CompoundStatement.java b/Homework/cs5300/compiler/submit/ast/CompoundStatement.java new file mode 100644 index 0000000..f745a26 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/CompoundStatement.java @@ -0,0 +1,30 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.List; + +/** + * + * @author edwajohn + */ +public class CompoundStatement implements Statement { + + private final List<Statement> statements; + + public CompoundStatement(List<Statement> statements) { + this.statements = statements; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("{\n"); + for (Statement s : statements) { + s.toCminus(builder, prefix + " "); + } + builder.append(prefix).append("}\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Declaration.java b/Homework/cs5300/compiler/submit/ast/Declaration.java new file mode 100644 index 0000000..c70e856 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Declaration.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Declaration extends Statement, Node { + +} diff --git a/Homework/cs5300/compiler/submit/ast/Expression.java b/Homework/cs5300/compiler/submit/ast/Expression.java new file mode 100644 index 0000000..910f6f0 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Expression.java @@ -0,0 +1,13 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public interface Expression extends Node { + +} diff --git a/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java b/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java new file mode 100644 index 0000000..c0a647a --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/ExpressionStatement.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ExpressionStatement implements Statement { + + private final Expression expression; + + public ExpressionStatement(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + expression.toCminus(builder, prefix); + builder.append(";\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/FunDeclaration.java b/Homework/cs5300/compiler/submit/ast/FunDeclaration.java new file mode 100644 index 0000000..39ced42 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/FunDeclaration.java @@ -0,0 +1,46 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class FunDeclaration implements Declaration, Node { + + private final VarType returnType; + private final String id; + private final ArrayList<Param> params; + private final Statement statement; + + public FunDeclaration(VarType returnType, String id, List<Param> params, + Statement statement) { + this.returnType = returnType; + this.id = id; + this.params = new ArrayList<>(params); + this.statement = statement; + } + + public void toCminus(StringBuilder builder, final String prefix) { + String rt = (returnType != null) ? returnType.toString() : "void"; + builder.append("\n").append(rt).append(" "); + builder.append(id); + builder.append("("); + + for (Param param : params) { + param.toCminus(builder, prefix); + builder.append(", "); + } + if (!params.isEmpty()) { + builder.delete(builder.length() - 2, builder.length()); + } + builder.append(")\n"); + statement.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/If.java b/Homework/cs5300/compiler/submit/ast/If.java new file mode 100644 index 0000000..ee62e30 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/If.java @@ -0,0 +1,44 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class If implements Statement { + + private final Expression expression; + private final Statement trueStatement; + private final Statement falseStatement; + + public If(Expression expression, Statement trueStatement, Statement falseStatement) { + this.expression = expression; + this.trueStatement = trueStatement; + this.falseStatement = falseStatement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("if ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (trueStatement instanceof CompoundStatement) { + trueStatement.toCminus(builder, prefix); + } else { + trueStatement.toCminus(builder, prefix + " "); + } + if (falseStatement != null) { + builder.append(prefix).append("else\n"); +// falseStatement.toCminus(builder, prefix); + if (falseStatement instanceof CompoundStatement) { + falseStatement.toCminus(builder, prefix); + } else { + falseStatement.toCminus(builder, prefix + " "); + } + } +// builder.append(prefix).append("}"); + } +} diff --git a/Homework/cs5300/compiler/submit/ast/Mutable.java b/Homework/cs5300/compiler/submit/ast/Mutable.java new file mode 100644 index 0000000..c7ef089 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Mutable.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Mutable implements Expression, Node { + + private final String id; + private final Expression index; + + public Mutable(String id, Expression index) { + this.id = id; + this.index = index; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(id); + if (index != null) { + builder.append("["); + index.toCminus(builder, prefix); + builder.append("]"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Node.java b/Homework/cs5300/compiler/submit/ast/Node.java new file mode 100644 index 0000000..455bb3f --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Node.java @@ -0,0 +1,5 @@ +package submit.ast; + +public interface Node { + void toCminus(StringBuilder builder, final String prefix); +} diff --git a/Homework/cs5300/compiler/submit/ast/NumConstant.java b/Homework/cs5300/compiler/submit/ast/NumConstant.java new file mode 100644 index 0000000..991d7d9 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/NumConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class NumConstant implements Expression, Node { + + private final int value; + + public NumConstant(int value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(Integer.toString(value)); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Param.java b/Homework/cs5300/compiler/submit/ast/Param.java new file mode 100644 index 0000000..215bdb6 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Param.java @@ -0,0 +1,43 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Param implements Node { + + private final VarType type; + private final String id; + private final boolean array; + + public Param(VarType type, String id, boolean array) { + this.type = type; + this.id = id; + this.array = array; + } + + public VarType getType() { + return type; + } + + public String getId() { + return id; + } + + public boolean isArray() { + return array; + } + + public void toCminus(StringBuilder builder, final String prefix) { + if (isArray()) { + builder.append(type).append(" ").append(id).append("[]"); + } else { + builder.append(type).append(" ").append(id); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/ParenExpression.java b/Homework/cs5300/compiler/submit/ast/ParenExpression.java new file mode 100644 index 0000000..63dce8d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/ParenExpression.java @@ -0,0 +1,26 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class ParenExpression implements Expression { + + private final Expression expression; + + public ParenExpression(Expression expression) { + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append("("); + expression.toCminus(builder, prefix); + builder.append(")"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Program.java b/Homework/cs5300/compiler/submit/ast/Program.java new file mode 100644 index 0000000..0a8b44d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Program.java @@ -0,0 +1,36 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class Program implements Node { + + private ArrayList<Declaration> declarations; + + public Program(List<Declaration> declarations) { + this.declarations = new ArrayList<>(declarations); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + toCminus(builder, ""); + return builder.toString(); + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + for (Declaration declaration : declarations) { + declaration.toCminus(builder, ""); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Return.java b/Homework/cs5300/compiler/submit/ast/Return.java new file mode 100644 index 0000000..c1f9d4d --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Return.java @@ -0,0 +1,31 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class Return implements Statement { + + private final Expression expr; + + public Return(Expression expr) { + this.expr = expr; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix); + if (expr == null) { + builder.append("return;\n"); + } else { + builder.append("return "); + expr.toCminus(builder, prefix); + builder.append(";\n"); + } + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/Statement.java b/Homework/cs5300/compiler/submit/ast/Statement.java new file mode 100644 index 0000000..2e0581e --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/Statement.java @@ -0,0 +1,16 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; + +/** + * + * @author edwajohn + */ +public interface Statement extends Node { + public static CompoundStatement empty() { return new CompoundStatement(new ArrayList<>()); } + +} diff --git a/Homework/cs5300/compiler/submit/ast/StringConstant.java b/Homework/cs5300/compiler/submit/ast/StringConstant.java new file mode 100644 index 0000000..cac6bee --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/StringConstant.java @@ -0,0 +1,23 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class StringConstant implements Expression { + + private final String value; + + public StringConstant(String value) { + this.value = value; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append("\"").append(value).append("\""); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/UnaryOperator.java b/Homework/cs5300/compiler/submit/ast/UnaryOperator.java new file mode 100644 index 0000000..47d69af --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/UnaryOperator.java @@ -0,0 +1,27 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class UnaryOperator implements Expression { + + private final UnaryOperatorType type; + private final Expression expression; + + public UnaryOperator(String type, Expression expression) { + this.type = UnaryOperatorType.fromString(type); + this.expression = expression; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(type); + expression.toCminus(builder, prefix); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java b/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java new file mode 100644 index 0000000..828ee5a --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/UnaryOperatorType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum UnaryOperatorType { + + NOT("!"), NEG("-"), DEREF("*"), QUESTION("?"); + + private final String value; + + private UnaryOperatorType(String value) { + this.value = value; + } + + public static UnaryOperatorType fromString(String s) { + for (UnaryOperatorType at : UnaryOperatorType.values()) { + if (at.value.equals(s)) { + return at; + } + } + throw new RuntimeException("Illegal string in UnaryOperatorType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/VarDeclaration.java b/Homework/cs5300/compiler/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..9006619 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/VarDeclaration.java @@ -0,0 +1,47 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author edwajohn + */ +public class VarDeclaration implements Declaration, Node { + + private final VarType type; + private final List<String> ids; + private final List<Integer> arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List<String> ids, List<Integer> arraySizes, boolean isStatic) { + this.type = type; + this.ids = new ArrayList<>(ids); + this.arraySizes = arraySizes; + this.isStatic = isStatic; + } + + public void toCminus(StringBuilder builder, final String prefix) { + builder.append(prefix); + if (isStatic) { + builder.append("static "); + } + builder.append(type).append(" "); + for (int i = 0; i < ids.size(); ++i) { + final String id = ids.get(i); + final int arraySize = arraySizes.get(i); + if (arraySize >= 0) { + builder.append(id).append("[").append(arraySize).append("]").append(", "); + } else { + builder.append(id).append(", "); + } + } + builder.delete(builder.length() - 2, builder.length()); + builder.append(";\n"); + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/VarType.java b/Homework/cs5300/compiler/submit/ast/VarType.java new file mode 100644 index 0000000..ba28946 --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/VarType.java @@ -0,0 +1,35 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public enum VarType { + + INT("int"), BOOL("bool"), CHAR("char"); + + private final String value; + + private VarType(String value) { + this.value = value; + } + + public static VarType fromString(String s) { + for (VarType vt : VarType.values()) { + if (vt.value.equals(s)) { + return vt; + } + } + throw new RuntimeException("Illegal string in VarType.fromString()"); + } + + @Override + public String toString() { + return value; + } + +} diff --git a/Homework/cs5300/compiler/submit/ast/While.java b/Homework/cs5300/compiler/submit/ast/While.java new file mode 100644 index 0000000..9dcbb5b --- /dev/null +++ b/Homework/cs5300/compiler/submit/ast/While.java @@ -0,0 +1,33 @@ +/* + * Code formatter project + * CS 4481 + */ +package submit.ast; + +/** + * + * @author edwajohn + */ +public class While implements Statement { + + private final Expression expression; + private final Statement statement; + + public While(Expression expression, Statement statement) { + this.expression = expression; + this.statement = statement; + } + + @Override + public void toCminus(StringBuilder builder, String prefix) { + builder.append(prefix).append("while ("); + expression.toCminus(builder, prefix); + builder.append(")\n"); + if (statement instanceof CompoundStatement) { + statement.toCminus(builder, prefix); + } else { + statement.toCminus(builder, prefix + " "); + } + + } +} |
