mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 17:08:03 +00:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
f617061dab
@ -24,16 +24,16 @@ public class ASTGenerator {
|
|||||||
if (ctx.meth() != null) {
|
if (ctx.meth() != null) {
|
||||||
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
|
meths = ctx.meth().stream().map(ASTGenerator::generateMethod).toList();
|
||||||
}
|
}
|
||||||
return new Class(ctx.id().getText(), declarations, meths, constructors);
|
return new Class(ctx.id().IDENTIFIER().getText(), declarations, meths, constructors);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) {
|
public static Declaration generateFieldVariable(DecafParser.FieldContext ctx) {
|
||||||
Type type = ASTGenerator.getType(ctx.type());
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
return new Declaration(ctx.id().getText(), type);
|
return new Declaration(ctx.id().IDENTIFIER().getText(), type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Parameter generateParameter(DecafParser.ParamContext ctx) {
|
public static Parameter generateParameter(DecafParser.ParamContext ctx) {
|
||||||
return new Parameter(ctx.id().getText(), getType(ctx.type()));
|
return new Parameter(ctx.id().IDENTIFIER().getText(), getType(ctx.type()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Method generateMethod(DecafParser.MethContext ctx) {
|
public static Method generateMethod(DecafParser.MethContext ctx) {
|
||||||
@ -42,7 +42,7 @@ public class ASTGenerator {
|
|||||||
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());
|
||||||
return new Method(getType(ctx.returntype()), ctx.id().getText(), params, block);
|
return new Method(getType(ctx.returntype()), ctx.id().IDENTIFIER().getText(), params, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
|
public static Constructor generateConstructor(DecafParser.ConstructorContext ctx) {
|
||||||
|
@ -15,6 +15,26 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public class Compiler {
|
public class Compiler {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
generateAST("""
|
||||||
|
public class ClassWithConstructorWithParameters {
|
||||||
|
int x;
|
||||||
|
public ClassWithConstructorWithParameters(int startValue, int repetitions) {
|
||||||
|
this.x = startValue;
|
||||||
|
while (repetitions > 0) {
|
||||||
|
int innerRepetitions;
|
||||||
|
innerRepetitions = this.x;
|
||||||
|
while (innerRepetitions > 0) {
|
||||||
|
this.x = this.x * this.x;
|
||||||
|
innerRepetitions -= 1;
|
||||||
|
}
|
||||||
|
repetitions -= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
public static Class generateAST(String fromSource) {
|
public static Class generateAST(String fromSource) {
|
||||||
CharStream input = CharStreams.fromString(fromSource);
|
CharStream input = CharStreams.fromString(fromSource);
|
||||||
DecafLexer lexer = new DecafLexer(input);
|
DecafLexer lexer = new DecafLexer(input);
|
||||||
@ -63,7 +83,7 @@ public class Compiler {
|
|||||||
CodeGenUtils.writeClassfile(bytes, classname);
|
CodeGenUtils.writeClassfile(bytes, classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
// public static void main(String[] args) {
|
||||||
generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java", "ClassWithConstructor");
|
// generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java", "ClassWithConstructor");
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
List<DecafParser.RecipientContext> recipientList = ctx.fieldVarAccess().recipient();
|
List<DecafParser.RecipientContext> recipientList = ctx.fieldVarAccess().recipient();
|
||||||
recipient = generateRecursiveOwnerChain(recipientList, null);
|
recipient = generateRecursiveOwnerChain(recipientList, null);
|
||||||
}
|
}
|
||||||
return new FieldVarAccess(isField, recipient, ctx.getText());
|
return new FieldVarAccess(isField, recipient, ctx.fieldVarAccess().id().IDENTIFIER().getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Expression generateConstant(DecafParser.LiteralContext ctx) {
|
public static Expression generateConstant(DecafParser.LiteralContext ctx) {
|
||||||
@ -97,7 +97,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
Expression astExpr = expr.accept(this);
|
Expression astExpr = expr.accept(this);
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
|
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -118,7 +118,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
DecafParser.RecipientContext ctx = ctxList.get(0);
|
DecafParser.RecipientContext ctx = ctxList.get(0);
|
||||||
ctxList.remove(0);
|
ctxList.remove(0);
|
||||||
if (ctx.id() != null) {
|
if (ctx.id() != null) {
|
||||||
return new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().getText());
|
return new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().IDENTIFIER().getText());
|
||||||
}
|
}
|
||||||
if (ctx.methName() != null) {
|
if (ctx.methName() != null) {
|
||||||
List<Expression> args = new ArrayList<>();
|
List<Expression> args = new ArrayList<>();
|
||||||
@ -127,7 +127,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
|
|||||||
Expression astExpr = expr.accept(new ExpressionGenerator());
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
return new MethodCall(new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().getText()), args);
|
return new MethodCall(new FieldVarAccess(false, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().IDENTIFIER().getText()), args);
|
||||||
|
|
||||||
}
|
}
|
||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
|
@ -75,7 +75,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
private FieldVarAccess generateField(DecafParser.FieldVarAccessContext fieldIdContext) {
|
private FieldVarAccess generateField(DecafParser.FieldVarAccessContext fieldIdContext) {
|
||||||
return new FieldVarAccess(fieldIdContext.THIS() != null,
|
return new FieldVarAccess(fieldIdContext.THIS() != null,
|
||||||
ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(),
|
ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(),
|
||||||
null), fieldIdContext.id().getText());
|
null), fieldIdContext.id().IDENTIFIER().getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
//StatementExpression
|
//StatementExpression
|
||||||
@ -92,7 +92,7 @@ public class StatementGenerator extends DecafBaseVisitor<Statement> {
|
|||||||
Expression astExpr = expr.accept(new ExpressionGenerator());
|
Expression astExpr = expr.accept(new ExpressionGenerator());
|
||||||
args.add(astExpr);
|
args.add(astExpr);
|
||||||
}
|
}
|
||||||
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().getText()), args);
|
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,6 +11,6 @@ public class VariableGenerator extends DecafBaseVisitor<Declaration> {
|
|||||||
@Override
|
@Override
|
||||||
public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) {
|
public Declaration visitLocalVar(DecafParser.LocalVarContext ctx) {
|
||||||
Type type = ASTGenerator.getType(ctx.type());
|
Type type = ASTGenerator.getType(ctx.type());
|
||||||
return new Declaration(ctx.id().getText(), type);
|
return new Declaration(ctx.id().IDENTIFIER().getText(), type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user