mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 16:48: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 class ASTGenerator {
|
||||||
|
|
||||||
public static Program generateAST(DecafParser.ProgramContext parseTree){
|
public static Program generateAST(DecafParser.ProgramContext parseTree) {
|
||||||
List<Class> classes = new ArrayList<>();
|
List<Class> classes = new ArrayList<>();
|
||||||
for(DecafParser.ClassContext cctx : parseTree.class_()){
|
for (DecafParser.ClassContext cctx : parseTree.class_()) {
|
||||||
classes.add(generateClass(cctx));
|
classes.add(generateClass(cctx));
|
||||||
}
|
}
|
||||||
return new Program(classes);
|
return new Program(classes);
|
||||||
@ -22,16 +22,16 @@ public class ASTGenerator {
|
|||||||
public static Class generateClass(DecafParser.ClassContext ctx) {
|
public static Class generateClass(DecafParser.ClassContext ctx) {
|
||||||
Boolean isPublic = ctx.PUBLIC() != null;
|
Boolean isPublic = ctx.PUBLIC() != null;
|
||||||
List<Field> fields = new ArrayList<>();
|
List<Field> fields = new ArrayList<>();
|
||||||
if(ctx.field() != null){
|
if (ctx.field() != null) {
|
||||||
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
|
fields = ctx.field().stream().map(ASTGenerator::generateFieldVariable).toList();
|
||||||
}
|
}
|
||||||
MainMethod mainMethod = generateMainMethod(ctx.mainmeth());
|
MainMethod mainMethod = generateMainMethod(ctx.mainmeth());
|
||||||
List<Constructor> constructors = new ArrayList<>();
|
List<Constructor> constructors = new ArrayList<>();
|
||||||
if(ctx.constructor() != null){
|
if (ctx.constructor() != null) {
|
||||||
constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList();
|
constructors = ctx.constructor().stream().map(ASTGenerator::generateConstructor).toList();
|
||||||
}
|
}
|
||||||
List<Method> meths = new ArrayList<>();
|
List<Method> meths = new ArrayList<>();
|
||||||
if(ctx.meth() != null){
|
if (ctx.meth() != null) {
|
||||||
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
|
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
|
||||||
}
|
}
|
||||||
Id classId = new Id(ctx.id().getText());
|
Id classId = new Id(ctx.id().getText());
|
||||||
@ -51,7 +51,7 @@ public class ASTGenerator {
|
|||||||
public static Method generateMethod(DecafParser.MethContext ctx) {
|
public static Method generateMethod(DecafParser.MethContext ctx) {
|
||||||
Boolean isPublic = ctx.PUBLIC() != null;
|
Boolean isPublic = ctx.PUBLIC() != null;
|
||||||
List<Parameter> params = new ArrayList<>();
|
List<Parameter> params = new ArrayList<>();
|
||||||
if(ctx.params() != null){
|
if (ctx.params() != null) {
|
||||||
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
|
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
|
||||||
}
|
}
|
||||||
Block block = new BlockGenerator().visit(ctx.block());
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
@ -62,7 +62,7 @@ public class ASTGenerator {
|
|||||||
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
|
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
|
||||||
Boolean isPublic = ctx.PUBLIC() != null;
|
Boolean isPublic = ctx.PUBLIC() != null;
|
||||||
List<Parameter> params = new ArrayList<>();
|
List<Parameter> params = new ArrayList<>();
|
||||||
if(ctx.params() != null){
|
if (ctx.params() != null) {
|
||||||
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
|
params = ctx.params().param().stream().map(ASTGenerator::generateParameter).toList();
|
||||||
}
|
}
|
||||||
Block block = new BlockGenerator().visit(ctx.block());
|
Block block = new BlockGenerator().visit(ctx.block());
|
||||||
@ -75,24 +75,24 @@ public class ASTGenerator {
|
|||||||
return new MainMethod(block);
|
return new MainMethod(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Type getType(DecafParser.TypeContext ctx){
|
public static Type getType(DecafParser.TypeContext ctx) {
|
||||||
if(ctx.INT() != null)
|
if (ctx.INT() != null)
|
||||||
return Type.INT;
|
return Type.INT;
|
||||||
if(ctx.BOOL() != null)
|
if (ctx.BOOL() != null)
|
||||||
return Type.BOOL;
|
return Type.BOOL;
|
||||||
if(ctx.CHAR() != null)
|
if (ctx.CHAR() != null)
|
||||||
return Type.CHAR;
|
return Type.CHAR;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx){
|
public static ReturnType getReturnType(DecafParser.ReturntypeContext ctx) {
|
||||||
if(ctx.type().INT() != null)
|
if (ctx.type().INT() != null)
|
||||||
return ReturnType.INT;
|
return ReturnType.INT;
|
||||||
if(ctx.type().BOOL() != null)
|
if (ctx.type().BOOL() != null)
|
||||||
return ReturnType.BOOL;
|
return ReturnType.BOOL;
|
||||||
if(ctx.type().CHAR() != null)
|
if (ctx.type().CHAR() != null)
|
||||||
return ReturnType.CHAR;
|
return ReturnType.CHAR;
|
||||||
if(ctx.VOID() != null)
|
if (ctx.VOID() != null)
|
||||||
return ReturnType.VOID;
|
return ReturnType.VOID;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,9 @@ import org.antlr.v4.runtime.CommonTokenStream;
|
|||||||
/**
|
/**
|
||||||
* Decaf language Compiler
|
* 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);
|
CharStream input = CharStreams.fromString(fromSource);
|
||||||
DecafLexer lexer = new DecafLexer(input);
|
DecafLexer lexer = new DecafLexer(input);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
@ -20,10 +20,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
@Override
|
@Override
|
||||||
public Expression visitUnaryOperation(DecafParser.UnaryOperationContext ctx) {
|
public Expression visitUnaryOperation(DecafParser.UnaryOperationContext ctx) {
|
||||||
Expression expr = this.visit(ctx.expr());
|
Expression expr = this.visit(ctx.expr());
|
||||||
if(ctx.unaryOp().NOT() != null){
|
if (ctx.unaryOp().NOT() != null) {
|
||||||
return new Unary(UnaryOperator.NOT, expr);
|
return new Unary(UnaryOperator.NOT, expr);
|
||||||
}
|
}
|
||||||
if(ctx.unaryOp().SUB() != null){
|
if (ctx.unaryOp().SUB() != null) {
|
||||||
return new Unary(UnaryOperator.SUB, expr);
|
return new Unary(UnaryOperator.SUB, expr);
|
||||||
}
|
}
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
@ -42,45 +42,45 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Expression visitIdentifier(DecafParser.IdentifierContext ctx){
|
public Expression visitIdentifier(DecafParser.IdentifierContext ctx) {
|
||||||
boolean isField = ctx.fieldId().THIS() != null;
|
boolean isField = ctx.fieldId().THIS() != null;
|
||||||
Expression recipient = null;
|
Expression recipient = null;
|
||||||
if(ctx.fieldId().recipient() != null){
|
if (ctx.fieldId().recipient() != null) {
|
||||||
List<DecafParser.RecipientContext> recipientList = ctx.fieldId().recipient();
|
List<DecafParser.RecipientContext> recipientList = ctx.fieldId().recipient();
|
||||||
recipient = generateRecipient(recipientList, null);
|
recipient = generateRecipient(recipientList, null);
|
||||||
}
|
}
|
||||||
return new FieldId(isField, recipient, new Id(ctx.getText()));
|
return new FieldId(isField, recipient, new Id(ctx.getText()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Expression generateConstant(DecafParser.LiteralContext ctx){
|
public static Expression generateConstant(DecafParser.LiteralContext ctx) {
|
||||||
if(ctx.NUMBER() != null)
|
if (ctx.NUMBER() != null)
|
||||||
return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText()));
|
return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText()));
|
||||||
if(ctx.BOOLEANLITERAL() != null)
|
if (ctx.BOOLEANLITERAL() != null)
|
||||||
return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText()));
|
return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText()));
|
||||||
if(ctx.CHARLITERAL() != null)
|
if (ctx.CHARLITERAL() != null)
|
||||||
return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0));
|
return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0));
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx){
|
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) {
|
||||||
ExpressionGenerator eGen = new ExpressionGenerator();
|
ExpressionGenerator eGen = new ExpressionGenerator();
|
||||||
return new Binary(eGen.visit(ctx.expr().get(0)) // left side
|
return new Binary(eGen.visit(ctx.expr().get(0)) // left side
|
||||||
, generateOperator(ctx.binaryOp()) //operator
|
, generateOperator(ctx.binaryOp()) //operator
|
||||||
, eGen.visit(ctx.expr().get(1))); //right side
|
, eGen.visit(ctx.expr().get(1))); //right side
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Operator generateOperator(DecafParser.BinaryOpContext ctx){
|
public static Operator generateOperator(DecafParser.BinaryOpContext ctx) {
|
||||||
if(ctx.ADD() != null)return Operator.ADD;
|
if (ctx.ADD() != null) return Operator.ADD;
|
||||||
if(ctx.SUB() != null)return Operator.SUB;
|
if (ctx.SUB() != null) return Operator.SUB;
|
||||||
if(ctx.MUL() != null)return Operator.MUL;
|
if (ctx.MUL() != null) return Operator.MUL;
|
||||||
if(ctx.GT() != null)return Operator.GT;
|
if (ctx.GT() != null) return Operator.GT;
|
||||||
if(ctx.LT() != null)return Operator.LT;
|
if (ctx.LT() != null) return Operator.LT;
|
||||||
if(ctx.GE() != null)return Operator.GE;
|
if (ctx.GE() != null) return Operator.GE;
|
||||||
if(ctx.LE() != null)return Operator.LE;
|
if (ctx.LE() != null) return Operator.LE;
|
||||||
if(ctx.EQ() != null)return Operator.EQ;
|
if (ctx.EQ() != null) return Operator.EQ;
|
||||||
if(ctx.NE() != null)return Operator.NE;
|
if (ctx.NE() != null) return Operator.NE;
|
||||||
if(ctx.AND() != null)return Operator.AND;
|
if (ctx.AND() != null) return Operator.AND;
|
||||||
if(ctx.OR() != null)return Operator.OR;
|
if (ctx.OR() != null) return Operator.OR;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,13 +88,13 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
public Expression visitMethodCall(DecafParser.MethodCallContext ctx) {
|
public Expression visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||||
boolean isField = ctx.methCall().THIS() != null;
|
boolean isField = ctx.methCall().THIS() != null;
|
||||||
Expression recipient = null;
|
Expression recipient = null;
|
||||||
if(ctx.methCall().recipient() != null){
|
if (ctx.methCall().recipient() != null) {
|
||||||
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
||||||
recipient = generateRecipient(recipientList, null);
|
recipient = generateRecipient(recipientList, null);
|
||||||
}
|
}
|
||||||
Id id = new Id(ctx.methCall().methName().id().getText());
|
Id id = new Id(ctx.methCall().methName().id().getText());
|
||||||
List<Expression> args = new ArrayList<>();
|
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);
|
Expression astExpr = expr.accept(this);
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
@ -105,22 +105,22 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
public Expression visitNew(DecafParser.NewContext ctx) {
|
public Expression visitNew(DecafParser.NewContext ctx) {
|
||||||
Type type = ASTGenerator.getType(ctx.type());
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
List<Expression> args = new ArrayList<>();
|
List<Expression> args = new ArrayList<>();
|
||||||
for(var expr : ctx.args().expr()){
|
for (var expr : ctx.args().expr()) {
|
||||||
Expression astExpr = expr.accept(this);
|
Expression astExpr = expr.accept(this);
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
return new New(type, args);
|
return new New(type, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Expression generateRecipient(List<DecafParser.RecipientContext> ctxList, Expression recipient){
|
public static Expression generateRecipient(List<DecafParser.RecipientContext> ctxList, Expression recipient) {
|
||||||
if(ctxList.isEmpty()){
|
if (ctxList.isEmpty()) {
|
||||||
return recipient;
|
return recipient;
|
||||||
}
|
}
|
||||||
DecafParser.RecipientContext ctx = ctxList.get(0);
|
DecafParser.RecipientContext ctx = ctxList.get(0);
|
||||||
ctxList.remove(0);
|
ctxList.remove(0);
|
||||||
Id id = new Id(ctx.methName().id().getText());
|
Id id = new Id(ctx.methName().id().getText());
|
||||||
List<Expression> args = new ArrayList<>();
|
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());
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
public Statement visitIf(DecafParser.IfContext ctx) {
|
public Statement visitIf(DecafParser.IfContext ctx) {
|
||||||
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
Expression expr = new ExpressionGenerator().visit(ctx.expr());
|
||||||
Block ifBlock = new BlockGenerator().visit(ctx.block(0));
|
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));
|
Block elseBlock = new BlockGenerator().visit(ctx.block(1));
|
||||||
return new IfElse(expr, ifBlock, elseBlock);
|
return new IfElse(expr, ifBlock, elseBlock);
|
||||||
}
|
}
|
||||||
@ -79,13 +79,13 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
public Statement visitMethodCall(DecafParser.MethodCallContext ctx) {
|
public Statement visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||||
boolean isField = ctx.methCall().THIS() != null;
|
boolean isField = ctx.methCall().THIS() != null;
|
||||||
Expression recipient = null;
|
Expression recipient = null;
|
||||||
if(ctx.methCall().recipient() != null){
|
if (ctx.methCall().recipient() != null) {
|
||||||
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
|
||||||
recipient = ExpressionGenerator.generateRecipient(recipientList, null);
|
recipient = ExpressionGenerator.generateRecipient(recipientList, null);
|
||||||
}
|
}
|
||||||
Id id = new Id(ctx.methCall().methName().id().getText());
|
Id id = new Id(ctx.methCall().methName().id().getText());
|
||||||
List<Expression> args = new ArrayList<>();
|
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());
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
@ -96,21 +96,21 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
public Statement visitNew(DecafParser.NewContext ctx) {
|
public Statement visitNew(DecafParser.NewContext ctx) {
|
||||||
Type type = ASTGenerator.getType(ctx.type());
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
List<Expression> args = new ArrayList<>();
|
List<Expression> args = new ArrayList<>();
|
||||||
for(var expr : ctx.args().expr()){
|
for (var expr : ctx.args().expr()) {
|
||||||
Expression astExpr = expr.accept(new ExpressionGenerator());
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
return new New(type, args);
|
return new New(type, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AssignSign getAssignSign(DecafParser.AssignSignContext ctx){
|
public static AssignSign getAssignSign(DecafParser.AssignSignContext ctx) {
|
||||||
if(ctx.ASSIGN() != null)
|
if (ctx.ASSIGN() != null)
|
||||||
return AssignSign.ASSIGN;
|
return AssignSign.ASSIGN;
|
||||||
if(ctx.ADD_ASSIGN() != null)
|
if (ctx.ADD_ASSIGN() != null)
|
||||||
return AssignSign.ADD_ASSIGN;
|
return AssignSign.ADD_ASSIGN;
|
||||||
if(ctx.SUB_ASSIGN() != null)
|
if (ctx.SUB_ASSIGN() != null)
|
||||||
return AssignSign.SUB_ASSIGN;
|
return AssignSign.SUB_ASSIGN;
|
||||||
if(ctx.MUL_ASSIGN() != null)
|
if (ctx.MUL_ASSIGN() != null)
|
||||||
return AssignSign.MUL_ASSIGN;
|
return AssignSign.MUL_ASSIGN;
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user