diff --git a/src/main/java/de/maishai/typedast/Node.java b/src/main/java/de/maishai/typedast/Node.java index a5a4bc7..f9ad199 100644 --- a/src/main/java/de/maishai/typedast/Node.java +++ b/src/main/java/de/maishai/typedast/Node.java @@ -1,10 +1,10 @@ package de.maishai.typedast; -import de.maishai.typedast.typedclass.Class; +import de.maishai.typedast.typedclass.TypedClass; import java.util.Map; public interface Node { - Type typeCheck(Map localVar, Map classes); - Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST); + Type typeCheck(Map localVar, Map classes); + Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST); } diff --git a/src/main/java/de/maishai/typedast/typedclass/Method.java b/src/main/java/de/maishai/typedast/typedclass/Method.java deleted file mode 100644 index db7d566..0000000 --- a/src/main/java/de/maishai/typedast/typedclass/Method.java +++ /dev/null @@ -1,35 +0,0 @@ -package de.maishai.typedast.typedclass; - -import de.maishai.typedast.Node; -import de.maishai.typedast.Type; -import lombok.Data; - -import java.util.List; -import java.util.Map; - -@Data -public class Method implements Node { - private Id id; - private Type returnType; - private List parameters; - private Block block; - - @Override - public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(id.getName())) { - throw new RuntimeException("Method " + id.getName() + " already exists"); - } - localVar.put(id.getName(), returnType); - for(Parameter parameter: parameters) { - parameter.typeCheck(localVar, classes); - } - return block.typeCheck(localVar, classes); - } - - @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - return null; - } - - -} diff --git a/src/main/java/de/maishai/typedast/typedclass/Assignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java similarity index 82% rename from src/main/java/de/maishai/typedast/typedclass/Assignment.java rename to src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index c56f7d9..f42e35c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Assignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -9,13 +9,13 @@ import lombok.Data; import java.util.Map; @Data -public class Assignment implements Statement { - private Id loc; +public class TypedAssignment implements Statement { + private TypedId loc; private AssignSign assignSign; private Expression value; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(!localVar.containsKey(loc.getName())) { throw new RuntimeException("Variable not declared"); } @@ -28,7 +28,7 @@ public class Assignment implements Statement { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Binary.java b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java similarity index 87% rename from src/main/java/de/maishai/typedast/typedclass/Binary.java rename to src/main/java/de/maishai/typedast/typedclass/TypedBinary.java index a2f1d36..6548ec9 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Binary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBinary.java @@ -8,12 +8,12 @@ import lombok.Data; import java.util.Map; @Data -public class Binary implements Expression { +public class TypedBinary implements Expression { private Expression left; private Operator op; private Expression right; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(left.typeCheck(localVar, classes) == Type.INT && right.typeCheck(localVar, classes) == Type.INT) { @@ -31,7 +31,7 @@ public class Binary implements Expression { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Block.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java similarity index 76% rename from src/main/java/de/maishai/typedast/typedclass/Block.java rename to src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 55b4048..25b9c9d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Block.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -8,13 +8,13 @@ import lombok.Data; import java.util.List; import java.util.Map; @Data -public class Block implements Node { - private List vars; +public class TypedBlock implements Node { + private List vars; private List stmts; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { Type typeOfLastStmt = Type.VOID; - for(LocalVariable var : vars){ + for(TypedLocalVariable var : vars){ var.typeCheck(localVar, classes); } for(Statement stmt : stmts){ @@ -26,7 +26,7 @@ public class Block implements Node { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/BoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java similarity index 50% rename from src/main/java/de/maishai/typedast/typedclass/BoolLiteral.java rename to src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index a3c5450..c0e7b54 100644 --- a/src/main/java/de/maishai/typedast/typedclass/BoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -7,18 +7,21 @@ import lombok.Data; import java.util.Map; @Data -public class BoolLiteral implements Expression { +public class TypedBoolLiteral implements Expression { private Boolean value; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.BOOL; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - return null; + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + de.maishai.ast.records.BoolLiteral untyped = (de.maishai.ast.records.BoolLiteral) unTypedAST; + TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); + typedBoolLiteral.setValue(untyped.value()); + return typedBoolLiteral; } - // implemented the methods of typeCheck for each class in + } diff --git a/src/main/java/de/maishai/typedast/typedclass/ReturnVoid.java b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java similarity index 73% rename from src/main/java/de/maishai/typedast/typedclass/ReturnVoid.java rename to src/main/java/de/maishai/typedast/typedclass/TypedBreak.java index 79fef85..3f48365 100644 --- a/src/main/java/de/maishai/typedast/typedclass/ReturnVoid.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBreak.java @@ -7,15 +7,14 @@ import lombok.Data; import java.util.Map; @Data -public class ReturnVoid implements Statement { - +public class TypedBreak implements Statement { @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/CharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java similarity index 74% rename from src/main/java/de/maishai/typedast/typedclass/CharLiteral.java rename to src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index ec75bef..d370ae7 100644 --- a/src/main/java/de/maishai/typedast/typedclass/CharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -7,15 +7,15 @@ import lombok.Data; import java.util.Map; @Data -public class CharLiteral implements Expression { +public class TypedCharLiteral implements Expression { private char value; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.CHAR; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Class.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java similarity index 54% rename from src/main/java/de/maishai/typedast/typedclass/Class.java rename to src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 3f97809..7b5ad66 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Class.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -12,38 +12,38 @@ import java.util.List; import java.util.Map; @Data -public class Class implements Node { - private Id id; - private List fields; - private List methods; +public class TypedClass implements Node { + private TypedId typedId; + private List typedFields; + private List typedMethods; @Override - public Type typeCheck(Map localVar, Map classes) { - if(classes.containsKey(id.getName())) { - throw new RuntimeException("Class " + id.getName() + " already exists"); + public Type typeCheck(Map localVar, Map classes) { + if(classes.containsKey(typedId.getName())) { + throw new RuntimeException("Class " + typedId.getName() + " already exists"); } - classes.put(id.getName(), this); - for(Field field: fields) { + classes.put(typedId.getName(), this); + for(TypedField field: typedFields) { field.typeCheck(localVar, classes); } - for(Method method: methods) { - method.typeCheck(localVar, classes); + for(TypedMethod typedMethod : typedMethods) { + typedMethod.typeCheck(localVar, classes); } - return Type.REFERENCE(id.getName()); + return Type.REFERENCE(typedId.getName()); } public Node startConversion(de.maishai.ast.records.Class c) { Map local = new HashMap<>(); - Map classMap = new HashMap<>(); + Map classMap = new HashMap<>(); return convertToTypedAST(local, classMap, c); } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - Class typedClass = new Class(); + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + TypedClass typedClass = new TypedClass(); - for (Field field: fields) { + for (TypedField field: typedFields) { } return typedClass; @@ -51,14 +51,14 @@ public class Class implements Node { public void codeGen() { ClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, id.getName(), null, "java/lang/Object", null); - for(Field field : fields) { + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, typedId.getName(), null, "java/lang/Object", null); + for(TypedField field : typedFields) { //field.codeGen(cw); } - for(Method m: methods) { + for(TypedMethod m: typedMethods) { //m.codeGen(cw); } - writeClassfile(cw, id.getName()); + writeClassfile(cw, typedId.getName()); } public static void writeClassfile(ClassWriter cw, String name) { @@ -74,10 +74,10 @@ public class Class implements Node { public static void main(String[] args) { - Class c = new Class(); - c.fields = new ArrayList<>(); - c.methods = new ArrayList<>(); - c.id.setName("Test"); + TypedClass c = new TypedClass(); + c.typedFields = new ArrayList<>(); + c.typedMethods = new ArrayList<>(); + c.typedId.setName("Test"); c.codeGen(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Constructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java similarity index 54% rename from src/main/java/de/maishai/typedast/typedclass/Constructor.java rename to src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index cffef42..3914358 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Constructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -7,29 +7,29 @@ import lombok.Data; import java.util.List; import java.util.Map; @Data -public class Constructor implements Node { +public class TypedConstructor implements Node { - private Id id; - private List params; - private Block block; + private TypedId typedId; + private List typedParameters; + private TypedBlock typedBlock; //constructor: PUBLIC? id '(' params? ')' block; @Override - public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(id.getName())) { + public Type typeCheck(Map localVar, Map classes) { + if(localVar.containsKey(typedId.getName())) { throw new RuntimeException("constructor already declared"); } - localVar.put(id.getName(), Type.VOID); - if(params != null){ - for(Parameter param : params){ + localVar.put(typedId.getName(), Type.VOID); + if(typedParameters != null){ + for(TypedParameter param : typedParameters){ param.typeCheck(localVar, classes); } } - block.typeCheck(localVar, classes); + typedBlock.typeCheck(localVar, classes); return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Continue.java b/src/main/java/de/maishai/typedast/typedclass/TypedContinue.java similarity index 73% rename from src/main/java/de/maishai/typedast/typedclass/Continue.java rename to src/main/java/de/maishai/typedast/typedclass/TypedContinue.java index c6a6d6b..62e9a7c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Continue.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedContinue.java @@ -7,15 +7,15 @@ import lombok.Data; import java.util.Map; @Data -public class Continue implements Statement { +public class TypedContinue implements Statement { @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/DoWhile.java b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java similarity index 72% rename from src/main/java/de/maishai/typedast/typedclass/DoWhile.java rename to src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java index 5854ca5..a3d908f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/DoWhile.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDoWhile.java @@ -8,20 +8,20 @@ import lombok.Data; import java.util.Map; @Data -public class DoWhile implements Statement { - private Block block; +public class TypedDoWhile implements Statement { + private TypedBlock typedBlock; private Expression cond; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(cond.typeCheck(localVar, classes) != Type.BOOL){ throw new RuntimeException("Condition must be boolean"); } - block.typeCheck(localVar, classes); + typedBlock.typeCheck(localVar, classes); return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Field.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java similarity index 52% rename from src/main/java/de/maishai/typedast/typedclass/Field.java rename to src/main/java/de/maishai/typedast/typedclass/TypedField.java index c08b03a..cd238dc 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Field.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -7,21 +7,21 @@ import lombok.Data; import java.util.Map; @Data -public class Field implements Node { - private Id id; +public class TypedField implements Node { + private TypedId typedId; private Type type; @Override - public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(id.getName())){ - throw new RuntimeException("Variable " + id.getName() + " already declared"); + public Type typeCheck(Map localVar, Map classes) { + if(localVar.containsKey(typedId.getName())){ + throw new RuntimeException("Variable " + typedId.getName() + " already declared"); } - localVar.put(id.getName(), type); + localVar.put(typedId.getName(), type); return type; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } diff --git a/src/main/java/de/maishai/typedast/typedclass/FieldId.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java similarity index 77% rename from src/main/java/de/maishai/typedast/typedclass/FieldId.java rename to src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java index 0213808..685f6cb 100644 --- a/src/main/java/de/maishai/typedast/typedclass/FieldId.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java @@ -7,12 +7,12 @@ import lombok.Data; import java.util.Map; @Data -public class FieldId implements Node { +public class TypedFieldId implements Node { private Boolean field; private Expression recipient; - private Id id; + private TypedId typedId; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { //TODO: Implement typeCheck for FieldId if(field){ return null; @@ -22,7 +22,7 @@ public class FieldId implements Node { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/For.java b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java similarity index 76% rename from src/main/java/de/maishai/typedast/typedclass/For.java rename to src/main/java/de/maishai/typedast/typedclass/TypedFor.java index 2e4d688..afb852a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/For.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFor.java @@ -9,23 +9,23 @@ import lombok.Data; import java.util.Map; @Data -public class For implements Statement { +public class TypedFor implements Statement { private Expression assign; private Expression cond; private Expression inc; - private Block block; + private TypedBlock typedBlock; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { assign.typeCheck(localVar, classes); if(!cond.typeCheck(localVar, classes).equals(Type.BOOL)) { throw new RuntimeException("Condition must be a boolean"); } inc.typeCheck(localVar, classes); - return block.typeCheck(localVar, classes); + return typedBlock.typeCheck(localVar, classes); } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Id.java b/src/main/java/de/maishai/typedast/typedclass/TypedId.java similarity index 80% rename from src/main/java/de/maishai/typedast/typedclass/Id.java rename to src/main/java/de/maishai/typedast/typedclass/TypedId.java index 9f0ae23..0fa3d9a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Id.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedId.java @@ -8,11 +8,11 @@ import lombok.Data; import java.util.Map; @Data -public class Id implements Expression { +public class TypedId implements Expression { private String name; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(!localVar.containsKey(name)){ throw new RuntimeException("Variable " + name + " not declared or not found"); } @@ -21,7 +21,7 @@ public class Id implements Expression { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/IfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java similarity index 74% rename from src/main/java/de/maishai/typedast/typedclass/IfElse.java rename to src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 8a76ce0..bbf59e4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/IfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -9,26 +9,26 @@ import lombok.Data; import java.util.Map; @Data -public class IfElse implements Statement { +public class TypedIfElse implements Statement { private Expression cond; - private Block ifBlock; - private Block elseBlock; + private TypedBlock ifTypedBlock; + private TypedBlock elseTypedBlock; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { // if(cond, Statement1, Statement2) // condition must be a boolean if(cond.typeCheck(localVar, classes) != Type.BOOL){ throw new RuntimeException("If condition must be a boolean"); } //Statement1 and Statement2 must be of type void - if(ifBlock.typeCheck(localVar, classes) != Type.VOID){ + if(ifTypedBlock.typeCheck(localVar, classes) != Type.VOID){ throw new RuntimeException("If block must be of type void"); } return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/IntLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java similarity index 74% rename from src/main/java/de/maishai/typedast/typedclass/IntLiteral.java rename to src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java index 47ebb37..a5a4307 100644 --- a/src/main/java/de/maishai/typedast/typedclass/IntLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIntLiteral.java @@ -8,15 +8,15 @@ import lombok.Data; import java.util.Map; @Data -public class IntLiteral implements Expression { +public class TypedIntLiteral implements Expression { private Integer value; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.INT; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/LocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java similarity index 52% rename from src/main/java/de/maishai/typedast/typedclass/LocalVariable.java rename to src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index b85577a..32c4c41 100644 --- a/src/main/java/de/maishai/typedast/typedclass/LocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -6,21 +6,21 @@ import lombok.Data; import java.util.Map; @Data -public class LocalVariable implements Node { - private Id name; +public class TypedLocalVariable implements Node { + private TypedId typedId; private Type type; @Override - public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(name.getName())) { - throw new RuntimeException("Variable " + name.getName() + " already exists"); + public Type typeCheck(Map localVar, Map classes) { + if(localVar.containsKey(typedId.getName())) { + throw new RuntimeException("Variable " + typedId.getName() + " already exists"); } - localVar.put(name.getName(), type); + localVar.put(typedId.getName(), type); return type; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/MainMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java similarity index 59% rename from src/main/java/de/maishai/typedast/typedclass/MainMethod.java rename to src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java index 90016ca..74c21b3 100644 --- a/src/main/java/de/maishai/typedast/typedclass/MainMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMainMethod.java @@ -7,15 +7,15 @@ import lombok.Data; import java.util.Map; @Data -public class MainMethod implements Node { - private Block block; +public class TypedMainMethod implements Node { + private TypedBlock typedBlock; @Override - public Type typeCheck(Map localVar, Map classes) { - return block.typeCheck(localVar, classes); + public Type typeCheck(Map localVar, Map classes) { + return typedBlock.typeCheck(localVar, classes); } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java new file mode 100644 index 0000000..7761285 --- /dev/null +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -0,0 +1,35 @@ +package de.maishai.typedast.typedclass; + +import de.maishai.typedast.Node; +import de.maishai.typedast.Type; +import lombok.Data; + +import java.util.List; +import java.util.Map; + +@Data +public class TypedMethod implements Node { + private TypedId typedId; + private Type returnType; + private List typedParameters; + private TypedBlock typedBlock; + + @Override + public Type typeCheck(Map localVar, Map classes) { + if(localVar.containsKey(typedId.getName())) { + throw new RuntimeException("Method " + typedId.getName() + " already exists"); + } + localVar.put(typedId.getName(), returnType); + for(TypedParameter typedParameter : typedParameters) { + typedParameter.typeCheck(localVar, classes); + } + return typedBlock.typeCheck(localVar, classes); + } + + @Override + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + return null; + } + + +} diff --git a/src/main/java/de/maishai/typedast/typedclass/MethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java similarity index 76% rename from src/main/java/de/maishai/typedast/typedclass/MethodCall.java rename to src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 9e91f90..5d07de0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/MethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -10,20 +10,20 @@ import java.util.List; import java.util.Map; @Data -public class MethodCall implements Expression, Statement { +public class TypedMethodCall implements Expression, Statement { private Boolean field; private Expression recipient; - private Id id; + private TypedId typedId; private List args; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { //TODO: Implement typeCheck for MethodCall return null; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/New.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java similarity index 76% rename from src/main/java/de/maishai/typedast/typedclass/New.java rename to src/main/java/de/maishai/typedast/typedclass/TypedNew.java index 375b0e6..651f707 100644 --- a/src/main/java/de/maishai/typedast/typedclass/New.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -10,24 +10,24 @@ import java.util.List; import java.util.Map; @Data -public class New implements Expression, Statement { +public class TypedNew implements Expression, Statement { private Type type; private List args; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { // new A(1, 2, 3) if (!classes.containsKey(this.type.getReference())) { throw new RuntimeException("Class " + this.type.getReference() + " not found"); } - Class klasse = classes.get(this.type.getReference()); - if (klasse.getFields().size() != args.size()) { + TypedClass klasse = classes.get(this.type.getReference()); + if (klasse.getTypedFields().size() != args.size()) { throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference()); } // compare the types of the arguments for (int i = 0; i < args.size(); i++) { Type type = args.get(i).typeCheck(localVar, classes); - if (!type.equals(klasse.getFields().get(i).getType())) { + if (!type.equals(klasse.getTypedFields().get(i).getType())) { throw new RuntimeException("False type for argument " + i + " in class " + type.getKind()); } } @@ -35,7 +35,7 @@ public class New implements Expression, Statement { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Parameter.java b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java similarity index 52% rename from src/main/java/de/maishai/typedast/typedclass/Parameter.java rename to src/main/java/de/maishai/typedast/typedclass/TypedParameter.java index 4829a2b..b2eb013 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Parameter.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java @@ -7,21 +7,21 @@ import lombok.Data; import java.util.Map; @Data -public class Parameter implements Node { - private Id id; +public class TypedParameter implements Node { + private TypedId typedId; private Type type; @Override - public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(id.getName())) { - throw new RuntimeException("Parameter " + id.getName() + " already exists"); + public Type typeCheck(Map localVar, Map classes) { + if(localVar.containsKey(typedId.getName())) { + throw new RuntimeException("Parameter " + typedId.getName() + " already exists"); } - localVar.put(id.getName(), type); + localVar.put(typedId.getName(), type); return type; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } diff --git a/src/main/java/de/maishai/typedast/typedclass/Program.java b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java similarity index 66% rename from src/main/java/de/maishai/typedast/typedclass/Program.java rename to src/main/java/de/maishai/typedast/typedclass/TypedProgram.java index a530a5b..fe3516e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Program.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java @@ -8,18 +8,18 @@ import java.util.List; import java.util.Map; @Data -public class Program implements Node { - private List classes; +public class TypedProgram implements Node { + private List typedClasses; @Override - public Type typeCheck(Map localVar, Map classes) { - for(Class c : this.classes){ + public Type typeCheck(Map localVar, Map classes) { + for(TypedClass c : this.typedClasses){ c.typeCheck(localVar, classes); } return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Return.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java similarity index 77% rename from src/main/java/de/maishai/typedast/typedclass/Return.java rename to src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 4782a3c..60edd27 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Return.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -9,16 +9,16 @@ import lombok.Data; import java.util.Map; @Data -public class Return implements Statement { +public class TypedReturn implements Statement { private Expression ret; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return ret.typeCheck(localVar, classes); } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Break.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java similarity index 73% rename from src/main/java/de/maishai/typedast/typedclass/Break.java rename to src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java index 993a045..95a96b0 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Break.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturnVoid.java @@ -7,14 +7,15 @@ import lombok.Data; import java.util.Map; @Data -public class Break implements Statement { +public class TypedReturnVoid implements Statement { + @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { return Type.VOID; } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/Unary.java b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java similarity index 87% rename from src/main/java/de/maishai/typedast/typedclass/Unary.java rename to src/main/java/de/maishai/typedast/typedclass/TypedUnary.java index 314275d..03b2a0f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/Unary.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedUnary.java @@ -8,11 +8,11 @@ import lombok.Data; import java.util.Map; @Data -public class Unary implements Expression { +public class TypedUnary implements Expression { private UnaryOperator op; private Expression right; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(op == UnaryOperator.NOT){ if(right.typeCheck(localVar, classes) != Type.BOOL){ @@ -31,7 +31,7 @@ public class Unary implements Expression { } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } } diff --git a/src/main/java/de/maishai/typedast/typedclass/While.java b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java similarity index 71% rename from src/main/java/de/maishai/typedast/typedclass/While.java rename to src/main/java/de/maishai/typedast/typedclass/TypedWhile.java index 759adcf..3bceb8a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/While.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedWhile.java @@ -9,19 +9,19 @@ import lombok.Data; import java.util.Map; @Data -public class While implements Statement { +public class TypedWhile implements Statement { private Expression cond; - private Block block; + private TypedBlock typedBlock; @Override - public Type typeCheck(Map localVar, Map classes) { + public Type typeCheck(Map localVar, Map classes) { if(cond.typeCheck(localVar, classes) != Type.BOOL){ throw new RuntimeException("While condition must be a boolean"); } - return block.typeCheck(localVar, classes); + return typedBlock.typeCheck(localVar, classes); } @Override - public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + public Node convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { return null; } }