From 08658fc8c4715fcf2e0f245888992f067ad36056 Mon Sep 17 00:00:00 2001 From: ahmad Date: Thu, 9 May 2024 21:11:10 +0200 Subject: [PATCH] Make the code better to read --- .../typedast/Util/TypedExpressionUtil.java | 19 ++---- .../typedast/typedclass/TypedAssignment.java | 12 ++-- .../typedast/typedclass/TypedBinary.java | 9 ++- .../typedast/typedclass/TypedBlock.java | 59 +++++++++---------- .../typedast/typedclass/TypedBoolLiteral.java | 13 +++- .../typedast/typedclass/TypedBreak.java | 4 +- .../typedast/typedclass/TypedCharLiteral.java | 11 ++-- .../typedast/typedclass/TypedClass.java | 37 +++++++++--- .../typedast/typedclass/TypedConstructor.java | 21 ++++--- .../typedast/typedclass/TypedDoWhile.java | 13 ++-- .../typedast/typedclass/TypedField.java | 7 ++- .../typedclass/TypedFieldVarAccess.java | 13 ++-- .../maishai/typedast/typedclass/TypedFor.java | 18 +++--- .../typedast/typedclass/TypedIfElse.java | 18 +++--- .../typedast/typedclass/TypedIntLiteral.java | 7 ++- .../typedclass/TypedLocalVariable.java | 7 ++- .../typedast/typedclass/TypedMethod.java | 29 +++++---- .../typedast/typedclass/TypedReturn.java | 11 +++- .../typedast/typedclass/TypedWhile.java | 15 +++-- 19 files changed, 194 insertions(+), 129 deletions(-) diff --git a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java index 11622f9..d879dad 100644 --- a/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java +++ b/src/main/java/de/maishai/typedast/Util/TypedExpressionUtil.java @@ -11,30 +11,21 @@ public class TypedExpressionUtil { public static TypedExpression getKindOfExpression(Map localVar, TypedClass clas, Expression expression) { if (expression instanceof BoolLiteral boolLiteral) { - TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); - typedBoolLiteral.setValue(boolLiteral.value()); - return typedBoolLiteral; + return new TypedBoolLiteral(boolLiteral.value()); } else if (expression instanceof CharLiteral charLiteral) { - TypedCharLiteral typedCharLiteral = new TypedCharLiteral() - .convertToCharLiteral(localVar, clas, charLiteral); - return typedCharLiteral; + return new TypedCharLiteral(localVar, clas, charLiteral); } else if (expression instanceof IntLiteral intLiteral) { - TypedIntLiteral typedIntLiteral = new TypedIntLiteral() - .convertToTypedIntLiteral(localVar, clas, intLiteral); - return typedIntLiteral; + return new TypedIntLiteral(localVar, clas, intLiteral); } else if (expression instanceof Binary binary) { - TypedBinary typedBinary = new TypedBinary() - .convertToTypedBinary(localVar, clas, binary); + TypedBinary typedBinary = new TypedBinary(localVar, clas, binary); typedBinary.typeCheck(localVar, clas); return typedBinary; } else if (expression instanceof FieldVarAccess fieldVarAccess) { - TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess() - .convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess); - return typedFieldVarAccess; + return new TypedFieldVarAccess(localVar, clas, fieldVarAccess); } else if (expression instanceof MethodCall methodCall) { TypedMethodCall typedMethodCall = new TypedMethodCall(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index efb8900..345328c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -20,10 +20,12 @@ public class TypedAssignment implements TypedStatement { private TypedExpression value; private Type type; - public TypedAssignment convertToTypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { + public TypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { + convertToTypedAssignment(localVar, clas, untyped); + } + public void convertToTypedAssignment(Map localVar, TypedClass clas, Assignment untyped) { varName = untyped.location().id(); value = getKindOfExpression(localVar, clas, untyped.value()); - return this; } @Override @@ -43,11 +45,11 @@ public class TypedAssignment implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) { - Assignment untyped = (Assignment) unTypedAST; + /* Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); typedAssignment.setVarName(untyped.location().id()); - typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value())); - return typedAssignment; + typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value()));*/ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index e2b6b0a..767461a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -8,6 +8,7 @@ import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; import java.util.Map; @@ -15,17 +16,21 @@ import java.util.Map; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; @Data +@NoArgsConstructor public class TypedBinary implements TypedExpression { private TypedExpression left; private Operator op; private TypedExpression right; private Type type; - public TypedBinary convertToTypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { + public TypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { + convertToTypedBinary(localVar, clas, unTypedBinary); + } + + public void convertToTypedBinary(Map localVar, TypedClass clas, Binary unTypedBinary) { left = getKindOfExpression(localVar, clas, unTypedBinary.left()); right = getKindOfExpression(localVar, clas, unTypedBinary.right()); op = unTypedBinary.op(); - return this; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 38f3241..e1e1b8e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -19,70 +19,70 @@ public class TypedBlock implements TypedNode { private List stmts; - public TypedBlock blockToTypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { + public TypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { + blockToTypedBlock(localVar, clas, unTypedBlock); + } + + public void blockToTypedBlock(Map localVar, TypedClass clas, Block unTypedBlock) { if (unTypedBlock == null) { - return null; + vars = List.of(); + stmts = List.of(); + return; } - TypedBlock typedBlock = new TypedBlock(); - typedBlock.setStmts(new ArrayList<>()); - typedBlock.setVars(new ArrayList<>()); + + stmts = new ArrayList<>(); + vars = new ArrayList<>(); if (unTypedBlock.localVariables().isEmpty()) { - typedBlock.setVars(List.of()); + vars = List.of(); } else { for (Declaration var : unTypedBlock.localVariables()) { - TypedLocalVariable typedVar = new TypedLocalVariable() - .convertToTypedLocalVariable(localVar, clas, var); - typedBlock.getVars().add(typedVar); + TypedLocalVariable typedVar = new TypedLocalVariable(localVar, clas, var); + vars.add(typedVar); } } if (unTypedBlock.stmts().isEmpty()) { - typedBlock.setStmts(List.of()); + stmts = List.of(); } else { for (var stmt : unTypedBlock.stmts()) { if (stmt instanceof Assignment assignment) { - TypedAssignment typedAssignment = new TypedAssignment() - .convertToTypedAssignment(localVar, clas, assignment); + TypedAssignment typedAssignment = new TypedAssignment(localVar, clas, assignment); typedAssignment.typeCheck(localVar, clas); - typedBlock.getStmts().add(typedAssignment); + stmts.add(typedAssignment); continue; } if (stmt instanceof For forStmt) { - TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, clas, forStmt); + TypedFor typedFor = new TypedFor(localVar, clas, forStmt); typedFor.typeCheck(localVar, clas); - typedBlock.getStmts().add(typedFor); + stmts.add(typedFor); continue; } if (stmt instanceof IfElse ifElse) { - TypedIfElse typedIfElse = new TypedIfElse() - .convertToTypedIfElse(localVar, clas, ifElse); + TypedIfElse typedIfElse = new TypedIfElse(localVar, clas, ifElse); typedIfElse.typeCheck(localVar, clas); - typedBlock.getStmts().add(typedIfElse); + stmts.add(typedIfElse); continue; } if (stmt instanceof While whileStmt) { - TypedWhile typedWhile = new TypedWhile() - .convertToTypedWhile(localVar, clas, whileStmt); + TypedWhile typedWhile = new TypedWhile(localVar, clas, whileStmt); //TODO: check the type - typedBlock.getStmts().add(typedWhile); + stmts.add(typedWhile); continue; } if (stmt instanceof DoWhile doWhile) { - TypedDoWhile typedDoWhile = new TypedDoWhile() - .convertToTypedDoWhile(localVar, clas, doWhile); + TypedDoWhile typedDoWhile = new TypedDoWhile(localVar, clas, doWhile); //TODO: check the type - typedBlock.getStmts().add(typedDoWhile); + stmts.add(typedDoWhile); continue; } if (stmt instanceof Return returnStmt) { - TypedReturn typedReturn = new TypedReturn() - .convertToTypedReturn(localVar, clas, returnStmt); + TypedReturn typedReturn = new TypedReturn(localVar, clas, returnStmt); //TODO: check the type - typedBlock.getStmts().add(typedReturn); + stmts.add(typedReturn); continue; } if (stmt instanceof New newStmt) { @@ -94,7 +94,7 @@ public class TypedBlock implements TypedNode { if (stmt instanceof Break) { TypedBreak typedBreak = new TypedBreak(); typedBreak.typeCheck(localVar, clas); - typedBlock.getStmts().add(typedBreak); + stmts.add(typedBreak); continue; } @@ -104,8 +104,7 @@ public class TypedBlock implements TypedNode { } } } - System.out.println("typedBlock: " + typedBlock.toString()); - return typedBlock; + System.out.println("typedBlock: " + this.toString()); } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index 1899c6a..ad00581 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -7,19 +7,28 @@ import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import java.util.Map; @Data +@NoArgsConstructor public class TypedBoolLiteral implements TypedExpression { private Boolean value; private Type type; - public TypedBoolLiteral convertToTypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { + public TypedBoolLiteral(Boolean value) { + this.value = value; + } + + public TypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { + convertToTypedBoolLiteral(localVar, clas, unTypedBoolLiteral); + } + + public void convertToTypedBoolLiteral(Map localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) { value = unTypedBoolLiteral.value(); - return this; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java index ba2fcc0..e209d33 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java @@ -12,9 +12,9 @@ import org.objectweb.asm.MethodVisitor; import java.util.Map; @Data public class TypedBreak implements TypedStatement { - private Type type; + private Type type = Type.VOID; - public TypedBreak convertToTypedBreak(Break unTypedBreak) { + public TypedBreak convertToTypedBreak(Map localVar, TypedClass clas, Break unTypedBreak) { return this; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index 1af20aa..2b2a5bd 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -15,9 +15,11 @@ public class TypedCharLiteral implements TypedExpression { private char value; private Type type = Type.CHAR; - public TypedCharLiteral convertToCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { + public TypedCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { + convertToCharLiteral(localVar, clas, unTypedCharLiteral); + } + public void convertToCharLiteral(Map localVar, TypedClass clas, CharLiteral unTypedCharLiteral) { value = unTypedCharLiteral.value(); - return this; } @Override public Type typeCheck(Map localVar, TypedClass clas) { @@ -26,10 +28,11 @@ public class TypedCharLiteral implements TypedExpression { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - CharLiteral untyped = (CharLiteral) unTypedAST; + /* CharLiteral untyped = (CharLiteral) unTypedAST; TypedCharLiteral typedCharLiteral = new TypedCharLiteral(); typedCharLiteral.setValue(untyped.value()); - return typedCharLiteral; + return typedCharLiteral; */ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 885fdda..ab3bacd 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -26,6 +26,30 @@ public class TypedClass implements TypedNode { private List typedMethods; private List typedConstructors; + public TypedClass(Map localVar, TypedClass clas, Node node) { + convertToTypedClass(localVar, clas, node); + } + + public void convertToTypedClass(Map localVar, TypedClass clas, Node node) { + Class c = (Class) node; + className = c.classname(); + typedFields = new ArrayList<>(); + typedMethods = new ArrayList<>(); + typedConstructors = new ArrayList<>(); + + for (Declaration field : c.fieldDeclarations()) { + typedFields.add(new TypedField(localVar, clas, field)); + } + + for (Method method : c.methods()) { + typedMethods.add(new TypedMethod(localVar, clas, method)); + } + + for (Constructor constructor : c.constructors()) { + typedConstructors.add(new TypedConstructor(localVar, clas, constructor)); + } + } + @Override public Type typeCheck(Map localVar, TypedClass clas) { @@ -45,16 +69,16 @@ public class TypedClass implements TypedNode { return Type.REFERENCE(className); } - public TypedNode startConversion(de.maishai.ast.records.Class c) { + public TypedNode startConversion(Class c) { Map local = new HashMap<>(); TypedClass classMap = new TypedClass(); - return convertToTypedAST(local, classMap, c); + return new TypedClass(local, classMap, c); } @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - TypedClass typedClass = new TypedClass(); + /* TypedClass typedClass = new TypedClass(); Class c = (Class) unTypedAST; typedClass.setClassName(c.classname()); typedClass.setTypedFields(new ArrayList<>()); @@ -68,16 +92,15 @@ public class TypedClass implements TypedNode { for (Method method : c.methods()) { - TypedMethod typedMethod = new TypedMethod(); - typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, clas, method)); + typedClass.getTypedMethods().add(new TypedMethod(localVar, clas, method)); } for (Constructor constructor : c.constructors()) { TypedConstructor typedConstructor = new TypedConstructor(); typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor)); } - - return typedClass; + */ + return null; } public byte[] codeGen() { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 7d71a9f..1034974 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -27,26 +27,26 @@ public class TypedConstructor implements TypedNode { private List typedParameters; private TypedBlock typedBlock; - public TypedConstructor constructorToTypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { - TypedConstructor typedConstructor = new TypedConstructor(); - typedConstructor.setName(unTypedConstructor.className()); - typedConstructor.setTypedParameters(new ArrayList<>()); + public TypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { + constructorToTypedConstructor(localVar, clas, unTypedConstructor); + } + public void constructorToTypedConstructor(Map localVar, TypedClass clas, Constructor unTypedConstructor) { + name = unTypedConstructor.className(); + typedParameters = new ArrayList<>(); if (unTypedConstructor.params().isEmpty()) { - typedConstructor.setTypedParameters(List.of()); + typedParameters = List.of(); } else { for (Parameter param : unTypedConstructor.params()) { TypedParameter typedParam = new TypedParameter(); typedParam.setParaName(param.name()); typedParam.setType(param.type()); - typedConstructor.getTypedParameters().add(typedParam); + typedParameters.add(typedParam); } } - TypedBlock typedBlock = new TypedBlock(); - typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas ,unTypedConstructor.block())); - return typedConstructor; + this.typedBlock = new TypedBlock(localVar, clas ,unTypedConstructor.block()); } @Override @@ -83,8 +83,7 @@ public class TypedConstructor implements TypedNode { } } - TypedBlock typedBlock = new TypedBlock(); - typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); + typedConstructor.setTypedBlock(new TypedBlock(localVar, clas, untyped.block())); return typedConstructor; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 4b69d04..04c51a6 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -14,12 +14,12 @@ public class TypedDoWhile implements TypedStatement { private TypedBlock typedBlock; private TypedExpression cond; - public TypedDoWhile() { + public TypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { + convertToTypedDoWhile(localVar, clas, unTypedDoWhile); } - public TypedDoWhile convertToTypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { - typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedDoWhile.block()); + public void convertToTypedDoWhile(Map localVar, TypedClass clas, DoWhile unTypedDoWhile) { + typedBlock = new TypedBlock(localVar, clas, unTypedDoWhile.block()); cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond()); - return this; } @Override @@ -33,14 +33,15 @@ public class TypedDoWhile implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - DoWhile untyped = (DoWhile) unTypedAST; + /* DoWhile untyped = (DoWhile) unTypedAST; TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedBlock typedBlock = new TypedBlock(); typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); - return typedDoWhile; + return typedDoWhile; */ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 4732fd5..2455533 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -19,14 +19,17 @@ public class TypedField implements TypedNode { private String varName; private Type type; - public TypedField convertToTypedField(Map localVar, TypedClass clas, Declaration declaration){ + public TypedField(Map localVar, TypedClass clas, Declaration declaration){ + convertToTypedField(localVar, clas, declaration); + } + + public void convertToTypedField(Map localVar, TypedClass clas, Declaration declaration){ if(localVar.containsKey(declaration.name())){ throw new RuntimeException("Variable " + declaration.name() + " already declared"); } varName = declaration.name(); type = declaration.type(); localVar.put(this.varName, this.type); - return this; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java index 07d0cf5..915037d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -6,6 +6,7 @@ import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; import java.util.Map; @@ -13,18 +14,21 @@ import java.util.Map; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; @Data +@NoArgsConstructor public class TypedFieldVarAccess implements TypedExpression { private Boolean field; private TypedExpression recursiveOwnerChain; private String name; private Type type; - public TypedFieldVarAccess convertToTypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { + public TypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { + convertToTypedFieldVarAccess(localVar, clas, unTypedFieldVarAccess); + } + public void convertToTypedFieldVarAccess(Map localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { field = unTypedFieldVarAccess.field(); recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); name = unTypedFieldVarAccess.id(); type = localVar.get(unTypedFieldVarAccess.id()); - return this; } @Override @@ -43,13 +47,14 @@ public class TypedFieldVarAccess implements TypedExpression { } @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; + /* FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(); typedFieldVarAccess.setField(untypedFieldVarAccess.field()); typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, clas, untypedFieldVarAccess.recursiveOwnerChain())); typedFieldVarAccess.setName(untypedFieldVarAccess.id()); - return typedFieldVarAccess; + return typedFieldVarAccess; */ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java index e92a19c..da6a941 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -20,12 +20,14 @@ public class TypedFor implements TypedStatement { private TypedAssignment inc; private TypedBlock typedBlock; - public TypedFor convertToTypedFor(Map localVar,TypedClass clas, For unTypedFor) { - assign = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.assign()); + public TypedFor(Map localVar, TypedClass clas, For unTypedFor) { + convertToTypedFor(localVar, clas, unTypedFor); + } + public void convertToTypedFor(Map localVar,TypedClass clas, For unTypedFor) { + assign = new TypedAssignment(localVar, clas, unTypedFor.assign()); cond = getKindOfExpression(localVar, clas, unTypedFor.cond()); - inc = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.inc()); - typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedFor.block()); - return this; + inc = new TypedAssignment(localVar, clas, unTypedFor.inc()); + typedBlock = new TypedBlock(localVar, clas, unTypedFor.block()); } @Override @@ -40,7 +42,7 @@ public class TypedFor implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - For untyped = (For) unTypedAST; + /*For untyped = (For) unTypedAST; TypedFor typedFor = new TypedFor(); TypedAssignment assign = new TypedAssignment(); typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign())); @@ -50,8 +52,8 @@ public class TypedFor implements TypedStatement { typedFor.setCond(null); TypedBlock typedBlock = new TypedBlock(); typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); - - return typedFor; +*/ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 94a638e..a9a3fdc 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -12,16 +13,19 @@ import java.util.Map; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; @Data +@NoArgsConstructor public class TypedIfElse implements TypedStatement { private TypedExpression typedCon; private TypedBlock ifTypedBlock; private TypedBlock elseTypedBlock; - public TypedIfElse convertToTypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { - ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.ifBlock()); - elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.elseBlock()); + public TypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { + convertToTypedIfElse(localVar, clas, unTypedIfElse); + } + public void convertToTypedIfElse(Map localVar, TypedClass clas, IfElse unTypedIfElse) { + ifTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.ifBlock()); + elseTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.elseBlock()); typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond()); - return this; } @Override @@ -43,10 +47,8 @@ public class TypedIfElse implements TypedStatement { TypedIfElse typedIfElse = new TypedIfElse(); typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, clas, ifElse.cond())); - TypedBlock ifTypedBlock = new TypedBlock(); - TypedBlock elseTypedBlock = new TypedBlock(); - typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, clas, ifElse.ifBlock())); - typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, clas, ifElse.elseBlock())); + typedIfElse.setIfTypedBlock(new TypedBlock(localVar, clas, ifElse.ifBlock())); + typedIfElse.setElseTypedBlock(new TypedBlock(localVar, clas, ifElse.elseBlock())); return typedIfElse; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index 2427c1a..9a7640c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -18,9 +18,12 @@ import java.util.Map; public class TypedIntLiteral implements TypedExpression { private Integer value; - public TypedIntLiteral convertToTypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { + public TypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { + convertToTypedIntLiteral(localVar, clas, unTypedIntLiteral); + } + + public void convertToTypedIntLiteral(Map localVar, TypedClass clas, IntLiteral unTypedIntLiteral) { value = unTypedIntLiteral.value(); - return this; } private Type type = Type.INT; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index f731aa1..a06902a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -17,14 +17,17 @@ import java.util.Map; public final class TypedLocalVariable implements TypedNode { private String name; private Type type; - public TypedLocalVariable convertToTypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration){ + + public TypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration){ + convertToTypedLocalVariable(localVar, clas, declaration); + } + public void convertToTypedLocalVariable(Map localVar, TypedClass clas, Declaration declaration){ if(localVar.containsKey(declaration.name())){ throw new RuntimeException("Variable " + declaration.name() + " already declared"); } this.setName(declaration.name()); this.setType(declaration.type()); localVar.put(this.name, this.type); - return this; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index fb9ab95..f1814f0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -5,6 +5,7 @@ import de.maishai.ast.records.Node; import de.maishai.ast.records.Parameter; import de.maishai.typedast.*; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -14,31 +15,34 @@ import java.util.List; import java.util.Map; @Data +@NoArgsConstructor public class TypedMethod implements TypedNode { private String name; private Type returnType; private List typedParameters; private TypedBlock typedBlock; - public TypedMethod convertToTypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { - TypedMethod typedMethod = new TypedMethod(); - typedMethod.setName(unTypedMethod.methodName()); - typedMethod.setTypedParameters(new ArrayList<>()); - typedMethod.setReturnType(unTypedMethod.type()); + public TypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { + convertToTypedMethod(localVar, clas, unTypedMethod); + } + + public void convertToTypedMethod(Map localVar, TypedClass clas, Method unTypedMethod) { + + name = unTypedMethod.methodName(); + returnType = unTypedMethod.type(); + typedParameters = new ArrayList<>(); if(unTypedMethod.params().isEmpty()){ - typedMethod.setTypedParameters(List.of()); + typedParameters = List.of(); }else { for (Parameter parameter : unTypedMethod.params()) { TypedParameter typedParameter = new TypedParameter(); typedParameter.setParaName(parameter.name()); typedParameter.setType(parameter.type()); - typedMethod.getTypedParameters().add(typedParameter); + typedParameters.add(typedParameter); } } - TypedBlock block = new TypedBlock(); - typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, clas, unTypedMethod.block())); - return typedMethod; + typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block()); } @Override public Type typeCheck(Map localVar, TypedClass clas) { @@ -54,7 +58,7 @@ public class TypedMethod implements TypedNode { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - Method untypedMethod = (Method) unTypedAST; + /* Method untypedMethod = (Method) unTypedAST; TypedMethod typedMethod = new TypedMethod(); typedMethod.setTypedParameters(new ArrayList<>()); @@ -71,7 +75,8 @@ public class TypedMethod implements TypedNode { TypedBlock typedBlock = new TypedBlock(); typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block())); - return typedMethod; + return typedMethod; */ + return null; } public void codeGen(ClassWriter cw) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 1a9453f..07c0d0e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -4,6 +4,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; import lombok.Data; +import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -12,9 +13,14 @@ import java.util.Map; import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression; @Data +@NoArgsConstructor public class TypedReturn implements TypedStatement { private TypedExpression ret; + + public TypedReturn(Map localVar, TypedClass clas, Return unTypedReturn) { + convertToTypedReturn(localVar, clas, unTypedReturn); + } public TypedReturn convertToTypedReturn(Map localVar, TypedClass clas, Return unTypedReturn) { ret = getKindOfExpression(localVar, clas, unTypedReturn.ret()); return this; @@ -28,10 +34,11 @@ public class TypedReturn implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - Return untyped = (Return) unTypedAST; + /* Return untyped = (Return) unTypedAST; TypedReturn typedReturn = new TypedReturn(); typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret())); - return typedReturn; + return typedReturn; */ + return null; } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index d18dfea..0170a88 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -14,10 +14,13 @@ public class TypedWhile implements TypedStatement { private TypedExpression cond; private TypedBlock typedBlock; - public TypedWhile convertToTypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { + public TypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { + convertToTypedWhile(localVar, clas, unTypedWhile); + } + + public void convertToTypedWhile(Map localVar, TypedClass clas, While unTypedWhile) { cond = getKindOfExpression(localVar, clas, unTypedWhile.cond()); - typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedWhile.block()); - return this; + typedBlock = new TypedBlock(localVar, clas, unTypedWhile.block()); } @Override public Type typeCheck(Map localVar, TypedClass clas) { @@ -30,12 +33,12 @@ public class TypedWhile implements TypedStatement { @Override public TypedNode convertToTypedAST(Map localVar, TypedClass clas, Node unTypedAST) { - While untyped = (While) unTypedAST; + /* While untyped = (While) unTypedAST; TypedWhile typedWhile = new TypedWhile(); typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond())); TypedBlock typedBlock = new TypedBlock(); - typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block())); - return typedWhile; + typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));*/ + return null; }