From 590a4f04aed0eb9aab7c0ab2fae25f5584668ad9 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Tue, 3 May 2022 21:37:14 +0200 Subject: [PATCH] Implement target t AST --- pom.xml | 3 +- .../target/bytecode/CodeGenException.java | 7 ++ .../target/bytecode/Codegen.java | 83 +++++++++++++++++-- .../target/tree/TargetConstructor.java | 4 +- .../target/tree/TargetField.java | 5 ++ .../target/tree/TargetMethod.java | 2 +- .../target/tree/expression/TargetAssign.java | 4 +- .../tree/expression/TargetBinaryOp.java | 40 +++++++++ .../target/tree/expression/TargetBlock.java | 8 +- .../target/tree/expression/TargetBreak.java | 11 +++ .../target/tree/expression/TargetCast.java | 6 ++ .../tree/expression/TargetContinue.java | 11 +++ .../target/tree/expression/TargetDot.java | 6 ++ .../tree/expression/TargetExpression.java | 4 +- .../tree/expression/TargetFieldVar.java | 7 ++ .../target/tree/expression/TargetFor.java | 11 +++ .../target/tree/expression/TargetForEach.java | 11 +++ .../target/tree/expression/TargetIf.java | 10 +++ .../tree/expression/TargetInstanceOf.java | 12 +++ .../expression/TargetLambdaExpression.java | 3 +- .../target/tree/expression/TargetLiteral.java | 35 ++++++++ .../tree/expression/TargetLocalVar.java | 6 ++ .../tree/expression/TargetMethodCall.java | 9 ++ .../target/tree/expression/TargetNew.java | 2 +- .../target/tree/expression/TargetReturn.java | 6 ++ .../target/tree/expression/TargetSuper.java | 4 +- .../target/tree/expression/TargetSwitch.java | 16 ++++ .../target/tree/expression/TargetThis.java | 4 +- .../target/tree/expression/TargetUnaryOp.java | 15 ++++ .../target/tree/expression/TargetVarDecl.java | 11 +++ .../target/tree/expression/TargetWhile.java | 11 +++ .../tree/type/TargetExtendsWildcard.java | 6 +- .../target/tree/type/TargetFunNType.java | 4 + .../target/tree/type/TargetGenericType.java | 4 + .../target/tree/type/TargetRefType.java | 2 +- .../target/tree/type/TargetSuperWildcard.java | 4 + .../target/tree/type/TargetType.java | 15 +++- 37 files changed, 382 insertions(+), 20 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBreak.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetCast.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetContinue.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetDot.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFieldVar.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFor.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetIf.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetInstanceOf.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLocalVar.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetMethodCall.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSwitch.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetUnaryOp.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetVarDecl.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetWhile.java diff --git a/pom.xml b/pom.xml index 105ce877..3f7cc7e3 100644 --- a/pom.xml +++ b/pom.xml @@ -124,7 +124,8 @@ http://maven.apache.org/maven-v4_0_0.xsd"> 17 17 - + --enable-preview + diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java b/src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java new file mode 100644 index 00000000..f517e767 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/bytecode/CodeGenException.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.target.bytecode; + +public class CodeGenException extends RuntimeException { + public CodeGenException(String cause) { + super(cause); + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java index 90a04fb0..fe10d9d6 100755 --- a/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java +++ b/src/main/java/de/dhbwstuttgart/target/bytecode/Codegen.java @@ -5,10 +5,15 @@ import de.dhbwstuttgart.target.tree.TargetConstructor; import de.dhbwstuttgart.target.tree.TargetField; import de.dhbwstuttgart.target.tree.TargetMethod; import de.dhbwstuttgart.target.tree.expression.TargetBlock; +import de.dhbwstuttgart.target.tree.expression.TargetExpression; +import de.dhbwstuttgart.target.tree.expression.TargetVarDecl; import de.dhbwstuttgart.target.tree.type.TargetType; +import javassist.compiler.CodeGen; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; +import java.util.Map; + import static org.objectweb.asm.Opcodes.*; public class Codegen { @@ -20,8 +25,72 @@ public class Codegen { this.cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); } - private void generateBlock(MethodVisitor mv, TargetBlock block) { + private record LocalVar(int index, String name, TargetType type) {} + private class Scope { + Scope parent; + Map locals; + + Scope(Scope parent) { + this.parent = parent; + } + + void add(LocalVar var) { + locals.put(var.name, var); + } + + LocalVar get(String name) { + var local = locals.get(name); + if (local != null ){ + return local; + } + if (parent != null) { + return parent.get(name); + } + throw new CodeGenException("Unknown symbol '" + name + "'"); + } + } + + private class State { + Scope scope = new Scope(null); + int localCounter = 1; + MethodVisitor mv; + + State(MethodVisitor mv) { + this.mv = mv; + } + + void enterScope() { + this.scope = new Scope(this.scope); + } + + void exitScope() { + this.scope = this.scope.parent; + } + + void createVariable(String name, TargetType type) { + scope.add(new LocalVar(localCounter, name, type)); + localCounter += 1; + } + } + + private void generate(State state, TargetExpression expr) { + switch (expr) { + case TargetBlock block: + var localCounter = state.localCounter; + state.enterScope(); + for (var e : block.statements()) { + generate(state, e); + } + state.exitScope(); + state.localCounter = localCounter; + break; + case TargetVarDecl varDecl: + state.createVariable(varDecl.name(), varDecl.varType()); + break; + default: + throw new IllegalStateException("Unexpected value: " + expr); + } } private void generateField(TargetField field) { @@ -29,21 +98,21 @@ public class Codegen { } private void generateConstructor(TargetConstructor constructor) { - MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", constructor.getDescriptor(), null, null); + MethodVisitor mv = cw.visitMethod(constructor.access(), "", constructor.getDescriptor(), null, null); + generate(new State(mv), constructor.block()); + mv.visitMaxs(0, 0); mv.visitEnd(); } private void generateMethod(TargetMethod method) { - // TODO Access modifier - MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.name(), method.getDescriptor(), null, null); - generateBlock(mv, method.block()); + MethodVisitor mv = cw.visitMethod(method.access(), method.name(), method.getDescriptor(), null, null); + generate(new State(mv), method.block()); mv.visitMaxs(0, 0); mv.visitEnd(); } public byte[] generate() { - // TODO Access modifier and signature - cw.visit(V12, ACC_PUBLIC, clazz.qualifiedName(), + cw.visit(V1_8, clazz.modifiers(), clazz.qualifiedName(), null, clazz.superType().toSignature(), clazz.implementingInterfaces().stream().map(TargetType::toSignature).toArray(String[]::new) ); diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java index 94aab071..594d5a20 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetConstructor.java @@ -1,8 +1,10 @@ package de.dhbwstuttgart.target.tree; +import de.dhbwstuttgart.target.tree.expression.TargetBlock; + import java.util.List; -public record TargetConstructor(List parameterTypes) { +public record TargetConstructor(int access, List parameterTypes, TargetBlock block) { public String getDescriptor() { // TODO diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java index c466b159..c9076736 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetField.java @@ -1,11 +1,16 @@ package de.dhbwstuttgart.target.tree; import de.dhbwstuttgart.target.tree.type.TargetType; +import org.objectweb.asm.Opcodes; public record TargetField(int access, TargetType type, String name) { public String getDescriptor() { // TODO return null; } + + public boolean isStatic() { + return (access & Opcodes.ACC_STATIC) != 0; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 9c50f6e7..767f2689 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -4,7 +4,7 @@ import de.dhbwstuttgart.target.tree.expression.TargetBlock; import java.util.List; -public record TargetMethod(String name, List parameterTypes, TargetBlock block) { +public record TargetMethod(int access, String name, List parameterTypes, TargetBlock block) { public String getDescriptor() { // TODO return null; diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java index c603db44..fd4bf51e 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java @@ -1,4 +1,6 @@ package de.dhbwstuttgart.target.tree.expression; -public record TargetAssign(TargetExpression leftSide, TargetExpression rightSide) implements TargetExpression { +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetAssign(TargetType type, TargetExpression leftSide, TargetExpression rightSide) implements TargetExpression { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java new file mode 100644 index 00000000..233157c5 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBinaryOp.java @@ -0,0 +1,40 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.expression.TargetExpression; +import de.dhbwstuttgart.target.tree.type.TargetType; + +public sealed interface TargetBinaryOp extends TargetExpression { + TargetExpression left(); + TargetExpression right(); + + // Artihmetic + record Add(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Sub(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Div(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Mul(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Rem(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + + // Bitwise + record BAnd(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record BOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record XOr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Shl(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Shr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record UShr(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + + // Conditional + record And(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Or(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + + // Comparison + record Equal(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Greater(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record GreaterOrEqual(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record Less(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record LessOrEqual(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + record NotEqual(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} + + record Assign(TargetType type, TargetExpression left, TargetExpression right) implements TargetBinaryOp {} +} + + diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java index eed51adc..87cc3b5e 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java @@ -1,6 +1,12 @@ package de.dhbwstuttgart.target.tree.expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + import java.util.List; -public record TargetBlock(List statememts) implements TargetExpression { +public record TargetBlock(List statements) implements TargetExpression { + @Override + public TargetType type() { + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBreak.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBreak.java new file mode 100644 index 00000000..ae918c44 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBreak.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetBreak() implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetCast.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetCast.java new file mode 100644 index 00000000..f95bc9a8 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetCast.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetCast(TargetType type, TargetExpression expr) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetContinue.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetContinue.java new file mode 100644 index 00000000..42ebc995 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetContinue.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetContinue() implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetDot.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetDot.java new file mode 100644 index 00000000..6eef9857 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetDot.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetDot(TargetType type, boolean isStatic, TargetExpression left, String right) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java index 82d33daa..7c9c85a2 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java @@ -3,5 +3,7 @@ package de.dhbwstuttgart.target.tree.expression; import de.dhbwstuttgart.target.tree.type.*; public sealed interface TargetExpression - permits TargetBlock, TargetLambdaExpression, TargetReturn, TargetThis, TargetSuper, TargetNew, TargetAssign { + permits TargetAssign, TargetBinaryOp, TargetBlock, TargetBreak, TargetCast, TargetContinue, TargetDot, TargetFieldVar, TargetFor, TargetForEach, TargetIf, TargetInstanceOf, TargetLambdaExpression, TargetLiteral, TargetLocalVar, TargetMethodCall, TargetNew, TargetReturn, TargetSuper, TargetSwitch, TargetThis, TargetUnaryOp, TargetVarDecl, TargetWhile { + + TargetType type(); } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFieldVar.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFieldVar.java new file mode 100644 index 00000000..9835fc9f --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFieldVar.java @@ -0,0 +1,7 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.TargetField; +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetFieldVar(TargetType type, TargetField field) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFor.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFor.java new file mode 100644 index 00000000..2ce402ab --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetFor.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetFor(TargetExpression init, TargetExpression termination, TargetExpression increment, TargetExpression body) implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java new file mode 100644 index 00000000..392b84e4 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetForEach.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetForEach(TargetExpression vardecl, TargetExpression list) implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetIf.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetIf.java new file mode 100644 index 00000000..ef94fc0b --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetIf.java @@ -0,0 +1,10 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetIf(TargetExpression expr, TargetExpression if_body, TargetExpression else_body) implements TargetExpression { + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetInstanceOf.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetInstanceOf.java new file mode 100644 index 00000000..79bf75f9 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetInstanceOf.java @@ -0,0 +1,12 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetInstanceOf(Expression left, TargetType right) implements TargetExpression { + + @Override + public TargetType type() { + return TargetType.Boolean; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java index 815ef036..13da5b9c 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java @@ -1,8 +1,9 @@ package de.dhbwstuttgart.target.tree.expression; import de.dhbwstuttgart.target.tree.MethodParameter; +import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; -public record TargetLambdaExpression(List params, TargetExpression block) implements TargetExpression { +public record TargetLambdaExpression(TargetType type, List params, TargetExpression block) implements TargetExpression { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java new file mode 100644 index 00000000..31ccb1f7 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLiteral.java @@ -0,0 +1,35 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public sealed interface TargetLiteral extends TargetExpression { + Object value(); + + record CharLiteral(Integer value) implements TargetLiteral { + @Override + public TargetType type() { + return TargetType.Char; + } + } + + record IntLiteral(Integer value) implements TargetLiteral { + @Override + public TargetType type() { + return TargetType.Integer; + } + } + + record LongLiteral(Long value) implements TargetLiteral { + @Override + public TargetType type() { + return TargetType.Long; + } + } + + record StringLiteral(String value) implements TargetLiteral { + @Override + public TargetType type() { + return TargetType.String; + } + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLocalVar.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLocalVar.java new file mode 100644 index 00000000..e4fa977f --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLocalVar.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetLocalVar(TargetType type, String name) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetMethodCall.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetMethodCall.java new file mode 100644 index 00000000..6a812604 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetMethodCall.java @@ -0,0 +1,9 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.TargetMethod; +import de.dhbwstuttgart.target.tree.type.TargetType; + +import java.util.List; + +public record TargetMethodCall(TargetType type, TargetExpression expr, List args, TargetMethod method) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java index 7e78cc57..6819a744 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java @@ -4,5 +4,5 @@ import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; -public record TargetNew(TargetType instantiatedType, List params) implements TargetExpression { +public record TargetNew(TargetType type, List params) implements TargetExpression { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java index 1c86dbc6..c419cc1c 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java @@ -1,4 +1,10 @@ package de.dhbwstuttgart.target.tree.expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + public record TargetReturn(TargetExpression expression) implements TargetExpression { + @Override + public TargetType type() { + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java index 40cd73b8..ddffe386 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java @@ -1,4 +1,6 @@ package de.dhbwstuttgart.target.tree.expression; -public record TargetSuper() implements TargetExpression { +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetSuper(TargetType type) implements TargetExpression { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSwitch.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSwitch.java new file mode 100644 index 00000000..a6823913 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSwitch.java @@ -0,0 +1,16 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.syntaxtree.statement.Expression; +import de.dhbwstuttgart.target.tree.type.TargetType; + +import java.util.List; + +public record TargetSwitch(Expression expr, List cases, Expression default_) implements TargetExpression { + + @Override + public TargetType type() { + return null; + } + + record Case(Expression value, Expression body) {} +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java index 9bbc5891..fe9b722c 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java @@ -1,4 +1,6 @@ package de.dhbwstuttgart.target.tree.expression; -public record TargetThis() implements TargetExpression { +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetThis(TargetType type) implements TargetExpression { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetUnaryOp.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetUnaryOp.java new file mode 100644 index 00000000..eec72196 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetUnaryOp.java @@ -0,0 +1,15 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public sealed interface TargetUnaryOp extends TargetExpression { + TargetExpression expr(); + + record Negate(TargetType type, TargetExpression expr) implements TargetUnaryOp {} + record Not(TargetType type, TargetExpression expr) implements TargetUnaryOp {} + + record PreIncrement(TargetType type, TargetExpression expr) implements TargetUnaryOp {} + record PostIncrement(TargetType type, TargetExpression expr) implements TargetUnaryOp {} + record PreDecrement(TargetType type, TargetExpression expr) implements TargetUnaryOp {} + record PostDecrement(TargetType type, TargetExpression expr) implements TargetUnaryOp {} +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetVarDecl.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetVarDecl.java new file mode 100644 index 00000000..b0c15225 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetVarDecl.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetVarDecl(String name, TargetType varType) implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetWhile.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetWhile.java new file mode 100644 index 00000000..5f0b8b06 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetWhile.java @@ -0,0 +1,11 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +public record TargetWhile(TargetExpression expr, TargetExpression body) implements TargetExpression { + + @Override + public TargetType type() { + return null; + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetExtendsWildcard.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetExtendsWildcard.java index b30b85e9..4dc033de 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetExtendsWildcard.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetExtendsWildcard.java @@ -2,6 +2,10 @@ package de.dhbwstuttgart.target.tree.type; import java.util.List; -public record TargetExtendsWildcard(TargetType innerType) implements TargetType{ +public record TargetExtendsWildcard(TargetType innerType) implements TargetType { + @Override + public String toSignature() { + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetFunNType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetFunNType.java index c3e86f79..76394836 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetFunNType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetFunNType.java @@ -3,4 +3,8 @@ package de.dhbwstuttgart.target.tree.type; import java.util.List; public record TargetFunNType(int N, List params) implements TargetType { + @Override + public String toSignature() { + return "Fun$$" + N; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java index fce6b7ee..12269632 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java @@ -1,4 +1,8 @@ package de.dhbwstuttgart.target.tree.type; public record TargetGenericType(String name) implements TargetType { + @Override + public String toSignature() { + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java index 33c38f5d..66a2b8ac 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetRefType.java @@ -7,6 +7,6 @@ import java.util.List; public record TargetRefType(String name, List params) implements TargetType { @Override public String toSignature() { - return this.name; + return "L" + this.name.replaceAll("\\.", "/"); } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetSuperWildcard.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetSuperWildcard.java index 421102cb..1414daab 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetSuperWildcard.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetSuperWildcard.java @@ -1,4 +1,8 @@ package de.dhbwstuttgart.target.tree.type; public record TargetSuperWildcard(TargetType innerType) implements TargetType { + @Override + public String toSignature() { + return null; + } } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java index f4d8f7d0..7f78b431 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java @@ -1,7 +1,20 @@ package de.dhbwstuttgart.target.tree.type; +import java.util.List; + public sealed interface TargetType permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard { - public String toSignature(); + // Builtin types + TargetRefType Boolean = new TargetRefType("java.lang.Boolean", List.of()); + TargetRefType Char = new TargetRefType("java.lang.Character", List.of()); + TargetRefType Byte = new TargetRefType("java.lang.Byte", List.of()); + TargetRefType Short = new TargetRefType("java.lang.Short", List.of()); + TargetRefType Integer = new TargetRefType("java.lang.Integer", List.of()); + TargetRefType Long = new TargetRefType("java.lang.Long", List.of()); + TargetRefType Float = new TargetRefType("java.lang.Float", List.of()); + TargetRefType Double = new TargetRefType("java.lang.Double", List.of()); + TargetRefType String = new TargetRefType("java.lang.String", List.of()); + + String toSignature(); }