From 45f834722b1712ab80fb084b7773af15b0831140 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 8 May 2024 15:05:04 +0200 Subject: [PATCH] Implementation for testing --- .../typedast/typedclass/TypedAssignment.java | 11 ++++-- .../typedast/typedclass/TypedBinary.java | 4 +++ .../typedast/typedclass/TypedBlock.java | 29 +++++++++++++++ .../typedast/typedclass/TypedBoolLiteral.java | 3 +- .../typedast/typedclass/TypedClass.java | 24 +++++++++---- .../typedast/typedclass/TypedConstructor.java | 36 +++++++++++++++++++ .../typedast/typedclass/TypedField.java | 21 +++++++++++ .../typedast/typedclass/TypedMethod.java | 29 ++++++++++++++- 8 files changed, 147 insertions(+), 10 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index c185962..9af6c60 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -1,6 +1,5 @@ package de.maishai.typedast.typedclass; -import de.maishai.ast.AssignSign; import de.maishai.ast.records.Assignment; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; @@ -16,7 +15,7 @@ import java.util.Map; public class TypedAssignment implements TypedStatement { private String varName; private TypedExpression value; - + private Type type; @Override public Type typeCheck(Map localVar, Map classes) { if (!localVar.containsKey(varName)) { @@ -27,10 +26,18 @@ public class TypedAssignment implements TypedStatement { Type typeRight = value.typeCheck(localVar, classes); if (typeLeft.equals(typeRight) ) { + type = typeLeft; return typeLeft; } throw new RuntimeException("type of left not equals with type of right"); } + // int y = 5; + // int x = (y + 5 + z); + //TypedAssignment([ + // localVar("x", "int"), + // localVar("y","int") + // ], + // "int") @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index b3c0235..8260f8f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -13,11 +13,13 @@ public class TypedBinary implements TypedExpression { private TypedExpression left; private Operator op; private TypedExpression right; + private Type type; @Override public Type typeCheck(Map localVar, Map classes) { if(op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) { if (left.typeCheck(localVar, classes) == Type.INT && right.typeCheck(localVar, classes) == Type.INT) { + type = Type.INT; return Type.INT; } else { throw new RuntimeException("Type mismatch"); @@ -25,6 +27,7 @@ public class TypedBinary implements TypedExpression { }else if(op == Operator.GT || op == Operator.LT || op == Operator.GE || op == Operator.LE || op == Operator.EQ || op == Operator.NE || op == Operator.AND || op == Operator.OR) { if (left.typeCheck(localVar, classes) == Type.INT && right.typeCheck(localVar, classes) == Type.INT) { + type = Type.BOOL; return Type.BOOL; } else { throw new RuntimeException("Type mismatch"); @@ -32,6 +35,7 @@ public class TypedBinary implements TypedExpression { } else if (op == Operator.AND || op == Operator.OR) { if (left.typeCheck(localVar, classes) == Type.BOOL && right.typeCheck(localVar, classes) == Type.BOOL) { + type = Type.BOOL; return Type.BOOL; } else { throw new RuntimeException("Type mismatch"); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 624f14f..7056db5 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -17,6 +17,35 @@ public class TypedBlock implements TypedNode { private List vars; private List stmts; + + + public TypedBlock unTypedBlockToTypedBlock(Block unTypedBlock) { + TypedBlock typedBlock = new TypedBlock(); + + for (LocalVariable var : unTypedBlock.vars()) { + TypedLocalVariable typedVar = new TypedLocalVariable(); + typedVar.setName(var.name().name()); + switch (var.type()) { + case INT: + typedVar.setType(Type.INT); + break; + case BOOL: + typedVar.setType(Type.BOOL); + break; + case CHAR: + typedVar.setType(Type.CHAR); + break; + case OBJECT: + typedVar.setType(Type.REFERENCE(var.name().name())); + break; + } + typedBlock.getVars().add(typedVar); + } + + typedBlock.getStmts().add((TypedStatement) unTypedBlock.stmts()); + + return typedBlock; + } @Override public Type typeCheck(Map localVar, Map classes) { Type typeOfLastStmt = Type.VOID; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index 92c79ed..ce6d4bc 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -11,9 +11,10 @@ import java.util.Map; public class TypedBoolLiteral implements TypedExpression { private Boolean value; - + private Type type; @Override public Type typeCheck(Map localVar, Map classes) { + type = Type.BOOL; return Type.BOOL; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 6bb3477..1ef81c0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -1,8 +1,11 @@ package de.maishai.typedast.typedclass; +import de.maishai.ast.records.*; +import de.maishai.ast.records.Class; import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.TypedNode; import de.maishai.typedast.Type; +import de.maishai.typedast.TypedStatement; import lombok.Data; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; @@ -50,17 +53,26 @@ public class TypedClass implements TypedNode { @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { TypedClass typedClass = new TypedClass(); + Class c = (Class) unTypedAST; - for (TypedField field : typedFields) { - typedClass.getTypedFields().add((TypedField) field.convertToTypedAST(localVar, classes, unTypedAST)); + for (Field field : c.fields()) { + TypedField typedField = new TypedField(); + typedClass.getTypedFields().add(typedField.unTypedFieldToTypedField(field)); } - for (TypedConstructor constructor : typedConstructors) { - typedClass.getTypedConstructors().add((TypedConstructor) constructor.convertToTypedAST(localVar, classes, unTypedAST)); + for (Constructor constructor : c.constructors()) { + TypedConstructor typedConstructor = new TypedConstructor(); + typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor)); } - for (TypedMethod method : typedMethods) { - typedClass.getTypedMethods().add((TypedMethod) method.convertToTypedAST(localVar, classes, unTypedAST)); + for(Constructor constructor : c.constructors()){ + TypedConstructor typedConstructor = new TypedConstructor(); + typedClass.getTypedConstructors().add(typedConstructor.unTypedContructorToTypedConstructor(constructor)); + } + + for (Method method : c.methods()) { + TypedMethod typedMethod = new TypedMethod(); + typedClass.getTypedMethods().add(typedMethod.unTypedMethodToTypedMethod(method)); } return typedClass; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 97289e2..692344a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -43,6 +43,42 @@ public class TypedConstructor implements TypedNode { return Type.VOID; } + public TypedConstructor unTypedContructorToTypedConstructor(de.maishai.ast.records.Constructor unTypedConstructor) { + TypedConstructor typedConstructor = new TypedConstructor(); + typedConstructor.setName(unTypedConstructor.id().name()); + + if (unTypedConstructor.params().isEmpty()) { + typedConstructor.setTypedParameters(null); + } else { + + for (Parameter param : unTypedConstructor.params()) { + TypedParameter typedParam = new TypedParameter(); + typedParam.setParaName(param.name()); + + switch(param.type()) { + case INT: + typedParam.setType(Type.INT); + break; + case BOOL: + typedParam.setType(Type.BOOL); + break; + case CHAR: + typedParam.setType(Type.CHAR); + break; + case OBJECT: + typedParam.setType(Type.REFERENCE(param.type().name())); + break; + } + typedConstructor.getTypedParameters().add(typedParam); + } + + } + Block block = unTypedConstructor.block(); + TypedBlock typedBlock = new TypedBlock(); + typedConstructor.setTypedBlock(typedBlock.unTypedBlockToTypedBlock(block)); + return typedConstructor; + } + @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Constructor untyped = (Constructor) unTypedAST; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 93e3c7c..2017916 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -19,6 +19,27 @@ public class TypedField implements TypedNode { private String varName; private Type type; + + public TypedField unTypedFieldToTypedField(Field field){ + TypedField typedField = new TypedField(); + switch(field.type()) { + case INT: + typedField.setType(Type.INT); + break; + case BOOL: + typedField.setType(Type.BOOL); + break; + case CHAR: + typedField.setType(Type.CHAR); + break; + case OBJECT: + typedField.setType(Type.REFERENCE(typedField.varName)); + break; + } + typedField.setVarName(field.name().name()); + return typedField; + } + @Override public Type typeCheck(Map localVar, Map classes) { if (localVar.containsKey(varName)) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 167258d..3750282 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -19,9 +19,36 @@ public class TypedMethod implements TypedNode { private List typedParameters; private TypedBlock typedBlock; + public TypedMethod unTypedMethodToTypedMethod(Method unTypedMethod) { + TypedMethod typedMethod = new TypedMethod(); + typedMethod.setName(unTypedMethod.id().name()); + + for(Parameter parameter : unTypedMethod.params()){ + TypedParameter typedParameter = new TypedParameter(); + typedParameter.setParaName(parameter.name()); + switch(parameter.type()) { + case INT: + typedParameter.setType(Type.INT); + break; + case BOOL: + typedParameter.setType(Type.BOOL); + break; + case CHAR: + typedParameter.setType(Type.CHAR); + break; + case OBJECT: + typedParameter.setType(Type.REFERENCE(parameter.name())); + break; + } + typedParameters.add(typedParameter); + } + TypedBlock block = new TypedBlock(); + typedMethod.setTypedBlock(block.unTypedBlockToTypedBlock(unTypedMethod.block())); + return typedMethod; + } @Override public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(name)) { + if(localVar.containsKey(name)) { throw new RuntimeException("Method " + name + " already exists"); } localVar.put(name, returnType);