mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 17:08:03 +00:00
generator formatting
This commit is contained in:
parent
535c712421
commit
1ff02f86e8
@ -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<Class> 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<Field> 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<Constructor> constructors = new ArrayList<>();
|
||||
if(ctx.constructor() != null){
|
||||
if (ctx.constructor() != null) {
|
||||
constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList();
|
||||
}
|
||||
List<Method> 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<Parameter> 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<Parameter> 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();
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -20,10 +20,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
||||
@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<Expression> {
|
||||
}
|
||||
|
||||
@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<DecafParser.RecipientContext> 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<Expression> {
|
||||
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<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
||||
recipient = generateRecipient(recipientList, null);
|
||||
}
|
||||
Id id = new Id(ctx.methCall().methName().id().getText());
|
||||
List<Expression> 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<Expression> {
|
||||
public Expression visitNew(DecafParser.NewContext ctx) {
|
||||
Type type = ASTGenerator.getType(ctx.type());
|
||||
List<Expression> 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<DecafParser.RecipientContext> ctxList, Expression recipient){
|
||||
if(ctxList.isEmpty()){
|
||||
public static Expression generateRecipient(List<DecafParser.RecipientContext> 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<Expression> 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);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
||||
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<Statement> {
|
||||
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<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
||||
recipient = ExpressionGenerator.generateRecipient(recipientList, null);
|
||||
}
|
||||
Id id = new Id(ctx.methCall().methName().id().getText());
|
||||
List<Expression> 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<Statement> {
|
||||
public Statement visitNew(DecafParser.NewContext ctx) {
|
||||
Type type = ASTGenerator.getType(ctx.type());
|
||||
List<Expression> 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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user