From 8e80cc2985780f6193b62cc284e9f1137fb6631f Mon Sep 17 00:00:00 2001 From: Andreas Stadelmeier Date: Wed, 30 Mar 2022 03:17:55 +0200 Subject: [PATCH] Add TargetExpression --- .../target/generate/ASTToTargetAST.java | 31 ++-- .../generate/StatementToTargetExpression.java | 156 ++++++++++++++++++ .../target/tree/TargetMethod.java | 4 +- .../target/tree/expression/TargetAssign.java | 4 + .../target/tree/expression/TargetBlock.java | 6 + .../tree/expression/TargetExpression.java | 7 + .../expression/TargetLambdaExpression.java | 8 + .../target/tree/expression/TargetNew.java | 8 + .../target/tree/expression/TargetReturn.java | 4 + .../target/tree/expression/TargetSuper.java | 4 + .../target/tree/expression/TargetThis.java | 4 + .../target/tree/type/TargetGenericType.java | 4 + .../target/tree/type/TargetType.java | 2 +- src/test/java/targetast/ASTToTargetTest.java | 5 + 14 files changed, 232 insertions(+), 15 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java create mode 100644 src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java create mode 100644 src/test/java/targetast/ASTToTargetTest.java diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 14896b83..63248f92 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -4,14 +4,11 @@ import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Method; +import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.type.*; -import de.dhbwstuttgart.target.tree.TargetClass; -import de.dhbwstuttgart.target.tree.TargetConstructor; -import de.dhbwstuttgart.target.tree.TargetField; -import de.dhbwstuttgart.target.tree.TargetMethod; -import de.dhbwstuttgart.target.tree.type.TargetRefType; -import de.dhbwstuttgart.target.tree.type.TargetSuperWildcard; -import de.dhbwstuttgart.target.tree.type.TargetType; +import de.dhbwstuttgart.target.tree.*; +import de.dhbwstuttgart.target.tree.expression.TargetBlock; +import de.dhbwstuttgart.target.tree.type.*; import java.util.ArrayList; import java.util.List; @@ -31,14 +28,24 @@ public class ASTToTargetAST { } private TargetMethod convert(Method input, Map sigma) { - return null; + List params = input.getParameterList().getFormalparalist().stream() + .map(param -> new MethodParameter(convert(param.getType(), sigma), param.getName())).collect(Collectors.toList()); + return new TargetMethod(input.name, params, convert(input.block)); + } + + private TargetBlock convert(Block block) { + return new TargetBlock(block.statements.stream().map(e -> { + StatementToTargetExpression converter = new StatementToTargetExpression(); + e.accept(converter); + return converter.result; + }).collect(Collectors.toList())); } private TargetField convert(Field input, Map sigma) { return null; } - private TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map sigma) { + private TargetType convert(RefTypeOrTPHOrWildcardOrGeneric input, Map sigma) { return input.acceptTV(new TypeVisitor<>() { @Override public TargetType visit(RefType refType) { @@ -53,17 +60,17 @@ public class ASTToTargetAST { @Override public TargetType visit(TypePlaceholder typePlaceholder) { - return convert(sigma.get(typePlaceholder), sigma); + return sigma.get(typePlaceholder); } @Override public TargetType visit(ExtendsWildcardType extendsWildcardType) { - return null; + return new TargetExtendsWildcard(convert(extendsWildcardType.getInnerType(), sigma)); } @Override public TargetType visit(GenericRefType genericRefType) { - return null; + return new TargetGenericType(genericRefType.getParsedName()); } }); } diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java new file mode 100644 index 00000000..cf083f3d --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -0,0 +1,156 @@ +package de.dhbwstuttgart.target.generate; + +import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; +import de.dhbwstuttgart.syntaxtree.ASTVisitor; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.statement.*; +import de.dhbwstuttgart.target.tree.expression.TargetExpression; + +public class StatementToTargetExpression implements StatementVisitor { + public TargetExpression result; + + @Override + public void visit(ArgumentList argumentList) { + + } + + @Override + public void visit(LambdaExpression lambdaExpression) { + + } + + @Override + public void visit(Assign assign) { + + } + + @Override + public void visit(BinaryExpr binary) { + + } + + @Override + public void visit(Block block) { + + } + + @Override + public void visit(CastExpr castExpr) { + + } + + @Override + public void visit(EmptyStmt emptyStmt) { + + } + + @Override + public void visit(FieldVar fieldVar) { + + } + + @Override + public void visit(ForStmt forStmt) { + + } + + @Override + public void visit(IfStmt ifStmt) { + + } + + @Override + public void visit(InstanceOf instanceOf) { + + } + + @Override + public void visit(LocalVar localVar) { + + } + + @Override + public void visit(LocalVarDecl localVarDecl) { + + } + + @Override + public void visit(MethodCall methodCall) { + + } + + @Override + public void visit(NewClass methodCall) { + + } + + @Override + public void visit(NewArray newArray) { + + } + + @Override + public void visit(Return aReturn) { + + } + + @Override + public void visit(ReturnVoid aReturn) { + + } + + @Override + public void visit(StaticClassName staticClassName) { + + } + + @Override + public void visit(Super aSuper) { + + } + + @Override + public void visit(This aThis) { + + } + + @Override + public void visit(WhileStmt whileStmt) { + + } + + @Override + public void visit(DoStmt whileStmt) { + + } + + @Override + public void visit(AssignToField assignLeftSide) { + + } + + @Override + public void visit(AssignToLocal assignLeftSide) { + + } + + @Override + public void visit(SuperCall superCall) { + + } + + @Override + public void visit(ExpressionReceiver expressionReceiver) { + + } + + @Override + public void visit(UnaryExpr unaryExpr) { + + } + + @Override + public void visit(Literal literal) { + + } +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java index 910c12a0..6538b7a5 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/TargetMethod.java @@ -1,9 +1,9 @@ package de.dhbwstuttgart.target.tree; -import de.dhbwstuttgart.target.tree.type.TargetType; +import de.dhbwstuttgart.target.tree.expression.TargetBlock; import java.util.List; -public record TargetMethod(String name, List parameterTypes) { +public record TargetMethod(String name, List parameterTypes, TargetBlock block) { } diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java new file mode 100644 index 00000000..c603db44 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetAssign.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.target.tree.expression; + +public record TargetAssign(TargetExpression leftSide, TargetExpression rightSide) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java new file mode 100644 index 00000000..eed51adc --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetBlock.java @@ -0,0 +1,6 @@ +package de.dhbwstuttgart.target.tree.expression; + +import java.util.List; + +public record TargetBlock(List statememts) 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 new file mode 100644 index 00000000..82d33daa --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetExpression.java @@ -0,0 +1,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 { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java new file mode 100644 index 00000000..815ef036 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetLambdaExpression.java @@ -0,0 +1,8 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.MethodParameter; + +import java.util.List; + +public record TargetLambdaExpression(List params, TargetExpression block) 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 new file mode 100644 index 00000000..7e78cc57 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java @@ -0,0 +1,8 @@ +package de.dhbwstuttgart.target.tree.expression; + +import de.dhbwstuttgart.target.tree.type.TargetType; + +import java.util.List; + +public record TargetNew(TargetType instantiatedType, 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 new file mode 100644 index 00000000..1c86dbc6 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetReturn.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.target.tree.expression; + +public record TargetReturn(TargetExpression expression) implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java new file mode 100644 index 00000000..40cd73b8 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetSuper.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.target.tree.expression; + +public record TargetSuper() implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java new file mode 100644 index 00000000..9bbc5891 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetThis.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.target.tree.expression; + +public record TargetThis() implements TargetExpression { +} diff --git a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java new file mode 100644 index 00000000..fce6b7ee --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetGenericType.java @@ -0,0 +1,4 @@ +package de.dhbwstuttgart.target.tree.type; + +public record TargetGenericType(String name) implements TargetType { +} 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 849df99c..eed6dcab 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/type/TargetType.java @@ -1,5 +1,5 @@ package de.dhbwstuttgart.target.tree.type; public sealed interface TargetType - permits TargetExtendsWildcard, TargetFunNType, TargetRefType, TargetSuperWildcard { + permits TargetExtendsWildcard, TargetFunNType, TargetGenericType, TargetRefType, TargetSuperWildcard { } diff --git a/src/test/java/targetast/ASTToTargetTest.java b/src/test/java/targetast/ASTToTargetTest.java new file mode 100644 index 00000000..64d3368f --- /dev/null +++ b/src/test/java/targetast/ASTToTargetTest.java @@ -0,0 +1,5 @@ +package targetast; + +public class ASTToTargetTest { + +}