From 1ff02f86e8b293d584d998baa7c10bf391edf9f3 Mon Sep 17 00:00:00 2001 From: laurenz Date: Tue, 7 May 2024 13:02:52 +0200 Subject: [PATCH] generator formatting --- src/main/java/de/maishai/ASTGenerator.java | 32 +++++------ src/main/java/de/maishai/Compiler.java | 5 +- .../java/de/maishai/ExpressionGenerator.java | 54 +++++++++---------- .../java/de/maishai/StatementGenerator.java | 18 +++---- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/main/java/de/maishai/ASTGenerator.java b/src/main/java/de/maishai/ASTGenerator.java index bb84987..1106391 100644 --- a/src/main/java/de/maishai/ASTGenerator.java +++ b/src/main/java/de/maishai/ASTGenerator.java @@ -11,9 +11,9 @@ import java.util.List; public class ASTGenerator { - public static Program generateAST(DecafParser.ProgramContext parseTree){ + public static Program generateAST(DecafParser.ProgramContext parseTree) { List classes = new ArrayList<>(); - for(DecafParser.ClassContext cctx : parseTree.class_()){ + for (DecafParser.ClassContext cctx : parseTree.class_()) { classes.add(generateClass(cctx)); } return new Program(classes); @@ -22,16 +22,16 @@ public class ASTGenerator { public static Class generateClass(DecafParser.ClassContext ctx) { Boolean isPublic = ctx.PUBLIC() != null; List fields = new ArrayList<>(); - if(ctx.field() != null){ + if (ctx.field() != null) { fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList(); } MainMethod mainMethod = generateMainMethod(ctx.mainmeth()); List constructors = new ArrayList<>(); - if(ctx.constructor() != null){ + if (ctx.constructor() != null) { constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList(); } List meths = new ArrayList<>(); - if(ctx.meth() != null){ + if (ctx.meth() != null) { meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList(); } Id classId = new Id(ctx.id().getText()); @@ -51,7 +51,7 @@ public class ASTGenerator { public static Method generateMethod(DecafParser.MethContext ctx) { Boolean isPublic = ctx.PUBLIC() != null; List params = new ArrayList<>(); - if(ctx.params() != null){ + if (ctx.params() != null) { params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); } Block block = new BlockGenerator().visit(ctx.block()); @@ -62,7 +62,7 @@ public class ASTGenerator { public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) { Boolean isPublic = ctx.PUBLIC() != null; List params = new ArrayList<>(); - if(ctx.params() != null){ + if (ctx.params() != null) { params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList(); } Block block = new BlockGenerator().visit(ctx.block()); @@ -75,24 +75,24 @@ public class ASTGenerator { return new MainMethod(block); } - public static Type getType(DecafParser.TypeContext ctx){ - if(ctx.INT() != null) + public static Type getType(DecafParser.TypeContext ctx) { + if (ctx.INT() != null) return Type.INT; - if(ctx.BOOL() != null) + if (ctx.BOOL() != null) return Type.BOOL; - if(ctx.CHAR() != null) + if (ctx.CHAR() != null) return Type.CHAR; throw new RuntimeException(); } - public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx){ - if(ctx.type().INT() != null) + public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) { + if (ctx.type().INT() != null) return ReturnType.INT; - if(ctx.type().BOOL() != null) + if (ctx.type().BOOL() != null) return ReturnType.BOOL; - if(ctx.type().CHAR() != null) + if (ctx.type().CHAR() != null) return ReturnType.CHAR; - if(ctx.VOID() != null) + if (ctx.VOID() != null) return ReturnType.VOID; throw new RuntimeException(); } diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 30cb545..954fb84 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -10,10 +10,9 @@ import org.antlr.v4.runtime.CommonTokenStream; /** * Decaf language Compiler */ -public class Compiler -{ +public class Compiler { - public static Program generateAST(String fromSource){ + public static Program generateAST(String fromSource) { CharStream input = CharStreams.fromString(fromSource); DecafLexer lexer = new DecafLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index c0c6bf5..397ce32 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -20,10 +20,10 @@ public class ExpressionGenerator extends DecafBaseVisitor { @Override public Expression visitUnaryOperation(DecafParser.UnaryOperationContext ctx) { Expression expr = this.visit(ctx.expr()); - if(ctx.unaryOp().NOT() != null){ + if (ctx.unaryOp().NOT() != null) { return new Unary(UnaryOperator.NOT, expr); } - if(ctx.unaryOp().SUB() != null){ + if (ctx.unaryOp().SUB() != null) { return new Unary(UnaryOperator.SUB, expr); } throw new RuntimeException(); @@ -42,45 +42,45 @@ public class ExpressionGenerator extends DecafBaseVisitor { } @Override - public Expression visitIdentifier(DecafParser.IdentifierContext ctx){ + public Expression visitIdentifier(DecafParser.IdentifierContext ctx) { boolean isField = ctx.fieldId().THIS() != null; Expression recipient = null; - if(ctx.fieldId().recipient() != null){ + if (ctx.fieldId().recipient() != null) { List recipientList = ctx.fieldId().recipient(); recipient = generateRecipient(recipientList, null); } return new FieldId(isField, recipient, new Id(ctx.getText())); } - public static Expression generateConstant(DecafParser.LiteralContext ctx){ - if(ctx.NUMBER() != null) + public static Expression generateConstant(DecafParser.LiteralContext ctx) { + if (ctx.NUMBER() != null) return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText())); - if(ctx.BOOLEANLITERAL() != null) + if (ctx.BOOLEANLITERAL() != null) return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText())); - if(ctx.CHARLITERAL() != null) + if (ctx.CHARLITERAL() != null) return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0)); throw new RuntimeException(); } - public static Binary generateBinary(DecafParser.BinaryOperationContext ctx){ + public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) { ExpressionGenerator eGen = new ExpressionGenerator(); return new Binary(eGen.visit(ctx.expr().get(0)) // left side , generateOperator(ctx.binaryOp()) //operator , eGen.visit(ctx.expr().get(1))); //right side } - public static Operator generateOperator(DecafParser.BinaryOpContext ctx){ - if(ctx.ADD() != null)return Operator.ADD; - if(ctx.SUB() != null)return Operator.SUB; - if(ctx.MUL() != null)return Operator.MUL; - if(ctx.GT() != null)return Operator.GT; - if(ctx.LT() != null)return Operator.LT; - if(ctx.GE() != null)return Operator.GE; - if(ctx.LE() != null)return Operator.LE; - if(ctx.EQ() != null)return Operator.EQ; - if(ctx.NE() != null)return Operator.NE; - if(ctx.AND() != null)return Operator.AND; - if(ctx.OR() != null)return Operator.OR; + public static Operator generateOperator(DecafParser.BinaryOpContext ctx) { + if (ctx.ADD() != null) return Operator.ADD; + if (ctx.SUB() != null) return Operator.SUB; + if (ctx.MUL() != null) return Operator.MUL; + if (ctx.GT() != null) return Operator.GT; + if (ctx.LT() != null) return Operator.LT; + if (ctx.GE() != null) return Operator.GE; + if (ctx.LE() != null) return Operator.LE; + if (ctx.EQ() != null) return Operator.EQ; + if (ctx.NE() != null) return Operator.NE; + if (ctx.AND() != null) return Operator.AND; + if (ctx.OR() != null) return Operator.OR; throw new RuntimeException(); } @@ -88,13 +88,13 @@ public class ExpressionGenerator extends DecafBaseVisitor { public Expression visitMethodCall(DecafParser.MethodCallContext ctx) { boolean isField = ctx.methCall().THIS() != null; Expression recipient = null; - if(ctx.methCall().recipient() != null){ + if (ctx.methCall().recipient() != null) { List recipientList = ctx.methCall().recipient(); recipient = generateRecipient(recipientList, null); } Id id = new Id(ctx.methCall().methName().id().getText()); List args = new ArrayList<>(); - for(var expr : ctx.methCall().methName().args().expr()){ + for (var expr : ctx.methCall().methName().args().expr()) { Expression astExpr = expr.accept(this); args.add(astExpr); } @@ -105,22 +105,22 @@ public class ExpressionGenerator extends DecafBaseVisitor { public Expression visitNew(DecafParser.NewContext ctx) { Type type = ASTGenerator.getType(ctx.type()); List args = new ArrayList<>(); - for(var expr : ctx.args().expr()){ + for (var expr : ctx.args().expr()) { Expression astExpr = expr.accept(this); args.add(astExpr); } return new New(type, args); } - public static Expression generateRecipient(List ctxList, Expression recipient){ - if(ctxList.isEmpty()){ + public static Expression generateRecipient(List ctxList, Expression recipient) { + if (ctxList.isEmpty()) { return recipient; } DecafParser.RecipientContext ctx = ctxList.get(0); ctxList.remove(0); Id id = new Id(ctx.methName().id().getText()); List args = new ArrayList<>(); - for(var expr : ctx.methName().args().expr()){ + for (var expr : ctx.methName().args().expr()) { Expression astExpr = expr.accept(new ExpressionGenerator()); args.add(astExpr); } diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index a7494c8..5d1de72 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -16,7 +16,7 @@ public class StatementGenerator extends DecafBaseVisitor { public Statement visitIf(DecafParser.IfContext ctx) { Expression expr = new ExpressionGenerator().visit(ctx.expr()); Block ifBlock = new BlockGenerator().visit(ctx.block(0)); - if(ctx.block().size() == 2){ + if (ctx.block().size() == 2) { Block elseBlock = new BlockGenerator().visit(ctx.block(1)); return new IfElse(expr, ifBlock, elseBlock); } @@ -79,13 +79,13 @@ public class StatementGenerator extends DecafBaseVisitor { public Statement visitMethodCall(DecafParser.MethodCallContext ctx) { boolean isField = ctx.methCall().THIS() != null; Expression recipient = null; - if(ctx.methCall().recipient() != null){ + if (ctx.methCall().recipient() != null) { List recipientList = ctx.methCall().recipient(); recipient = ExpressionGenerator.generateRecipient(recipientList, null); } Id id = new Id(ctx.methCall().methName().id().getText()); List args = new ArrayList<>(); - for(var expr : ctx.methCall().methName().args().expr()){ + for (var expr : ctx.methCall().methName().args().expr()) { Expression astExpr = expr.accept(new ExpressionGenerator()); args.add(astExpr); } @@ -96,21 +96,21 @@ public class StatementGenerator extends DecafBaseVisitor { public Statement visitNew(DecafParser.NewContext ctx) { Type type = ASTGenerator.getType(ctx.type()); List args = new ArrayList<>(); - for(var expr : ctx.args().expr()){ + for (var expr : ctx.args().expr()) { Expression astExpr = expr.accept(new ExpressionGenerator()); args.add(astExpr); } return new New(type, args); } - public static AssignSign getAssignSign(DecafParser.AssignSignContext ctx){ - if(ctx.ASSIGN() != null) + public static AssignSign getAssignSign(DecafParser.AssignSignContext ctx) { + if (ctx.ASSIGN() != null) return AssignSign.ASSIGN; - if(ctx.ADD_ASSIGN() != null) + if (ctx.ADD_ASSIGN() != null) return AssignSign.ADD_ASSIGN; - if(ctx.SUB_ASSIGN() != null) + if (ctx.SUB_ASSIGN() != null) return AssignSign.SUB_ASSIGN; - if(ctx.MUL_ASSIGN() != null) + if (ctx.MUL_ASSIGN() != null) return AssignSign.MUL_ASSIGN; throw new RuntimeException(); }