Changed the Name of classes

This commit is contained in:
ahmad 2024-05-04 17:19:04 +02:00
parent f129277236
commit cabadd7594
29 changed files with 184 additions and 181 deletions

View File

@ -1,10 +1,10 @@
package de.maishai.typedast; package de.maishai.typedast;
import de.maishai.typedast.typedclass.Class; import de.maishai.typedast.typedclass.TypedClass;
import java.util.Map; import java.util.Map;
public interface Node { public interface Node {
Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes); Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes);
Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST); Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST);
} }

View File

@ -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<Parameter> parameters;
private Block block;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> 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<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) {
return null;
}
}

View File

@ -9,13 +9,13 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Assignment implements Statement { public class TypedAssignment implements Statement {
private Id loc; private TypedId loc;
private AssignSign assignSign; private AssignSign assignSign;
private Expression value; private Expression value;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(!localVar.containsKey(loc.getName())) { if(!localVar.containsKey(loc.getName())) {
throw new RuntimeException("Variable not declared"); throw new RuntimeException("Variable not declared");
} }
@ -28,7 +28,7 @@ public class Assignment implements Statement {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,12 +8,12 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Binary implements Expression { public class TypedBinary implements Expression {
private Expression left; private Expression left;
private Operator op; private Operator op;
private Expression right; private Expression right;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(left.typeCheck(localVar, classes) == Type.INT && if(left.typeCheck(localVar, classes) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) { right.typeCheck(localVar, classes) == Type.INT) {
@ -31,7 +31,7 @@ public class Binary implements Expression {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,13 +8,13 @@ import lombok.Data;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class Block implements Node { public class TypedBlock implements Node {
private List<LocalVariable> vars; private List<TypedLocalVariable> vars;
private List<Statement> stmts; private List<Statement> stmts;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
Type typeOfLastStmt = Type.VOID; Type typeOfLastStmt = Type.VOID;
for(LocalVariable var : vars){ for(TypedLocalVariable var : vars){
var.typeCheck(localVar, classes); var.typeCheck(localVar, classes);
} }
for(Statement stmt : stmts){ for(Statement stmt : stmts){
@ -26,7 +26,7 @@ public class Block implements Node {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,18 +7,21 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class BoolLiteral implements Expression { public class TypedBoolLiteral implements Expression {
private Boolean value; private Boolean value;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.BOOL; return Type.BOOL;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; 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
} }

View File

@ -7,15 +7,14 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class ReturnVoid implements Statement { public class TypedBreak implements Statement {
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,15 +7,15 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class CharLiteral implements Expression { public class TypedCharLiteral implements Expression {
private char value; private char value;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.CHAR; return Type.CHAR;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -12,38 +12,38 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class Class implements Node { public class TypedClass implements Node {
private Id id; private TypedId typedId;
private List<Field> fields; private List<TypedField> typedFields;
private List<Method> methods; private List<TypedMethod> typedMethods;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(classes.containsKey(id.getName())) { if(classes.containsKey(typedId.getName())) {
throw new RuntimeException("Class " + id.getName() + " already exists"); throw new RuntimeException("Class " + typedId.getName() + " already exists");
} }
classes.put(id.getName(), this); classes.put(typedId.getName(), this);
for(Field field: fields) { for(TypedField field: typedFields) {
field.typeCheck(localVar, classes); field.typeCheck(localVar, classes);
} }
for(Method method: methods) { for(TypedMethod typedMethod : typedMethods) {
method.typeCheck(localVar, classes); typedMethod.typeCheck(localVar, classes);
} }
return Type.REFERENCE(id.getName()); return Type.REFERENCE(typedId.getName());
} }
public Node startConversion(de.maishai.ast.records.Class c) { public Node startConversion(de.maishai.ast.records.Class c) {
Map<String, Type> local = new HashMap<>(); Map<String, Type> local = new HashMap<>();
Map<String, Class> classMap = new HashMap<>(); Map<String, TypedClass> classMap = new HashMap<>();
return convertToTypedAST(local, classMap, c); return convertToTypedAST(local, classMap, c);
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Class typedClass = new Class(); TypedClass typedClass = new TypedClass();
for (Field field: fields) { for (TypedField field: typedFields) {
} }
return typedClass; return typedClass;
@ -51,14 +51,14 @@ public class Class implements Node {
public void codeGen() { public void codeGen() {
ClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); 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); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, typedId.getName(), null, "java/lang/Object", null);
for(Field field : fields) { for(TypedField field : typedFields) {
//field.codeGen(cw); //field.codeGen(cw);
} }
for(Method m: methods) { for(TypedMethod m: typedMethods) {
//m.codeGen(cw); //m.codeGen(cw);
} }
writeClassfile(cw, id.getName()); writeClassfile(cw, typedId.getName());
} }
public static void writeClassfile(ClassWriter cw, String name) { public static void writeClassfile(ClassWriter cw, String name) {
@ -74,10 +74,10 @@ public class Class implements Node {
public static void main(String[] args) { public static void main(String[] args) {
Class c = new Class(); TypedClass c = new TypedClass();
c.fields = new ArrayList<>(); c.typedFields = new ArrayList<>();
c.methods = new ArrayList<>(); c.typedMethods = new ArrayList<>();
c.id.setName("Test"); c.typedId.setName("Test");
c.codeGen(); c.codeGen();
} }
} }

View File

@ -7,29 +7,29 @@ import lombok.Data;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class Constructor implements Node { public class TypedConstructor implements Node {
private Id id; private TypedId typedId;
private List<Parameter> params; private List<TypedParameter> typedParameters;
private Block block; private TypedBlock typedBlock;
//constructor: PUBLIC? id '(' params? ')' block; //constructor: PUBLIC? id '(' params? ')' block;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(id.getName())) { if(localVar.containsKey(typedId.getName())) {
throw new RuntimeException("constructor already declared"); throw new RuntimeException("constructor already declared");
} }
localVar.put(id.getName(), Type.VOID); localVar.put(typedId.getName(), Type.VOID);
if(params != null){ if(typedParameters != null){
for(Parameter param : params){ for(TypedParameter param : typedParameters){
param.typeCheck(localVar, classes); param.typeCheck(localVar, classes);
} }
} }
block.typeCheck(localVar, classes); typedBlock.typeCheck(localVar, classes);
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,15 +7,15 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Continue implements Statement { public class TypedContinue implements Statement {
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,20 +8,20 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class DoWhile implements Statement { public class TypedDoWhile implements Statement {
private Block block; private TypedBlock typedBlock;
private Expression cond; private Expression cond;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(cond.typeCheck(localVar, classes) != Type.BOOL){ if(cond.typeCheck(localVar, classes) != Type.BOOL){
throw new RuntimeException("Condition must be boolean"); throw new RuntimeException("Condition must be boolean");
} }
block.typeCheck(localVar, classes); typedBlock.typeCheck(localVar, classes);
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,21 +7,21 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Field implements Node { public class TypedField implements Node {
private Id id; private TypedId typedId;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(id.getName())){ if(localVar.containsKey(typedId.getName())){
throw new RuntimeException("Variable " + id.getName() + " already declared"); throw new RuntimeException("Variable " + typedId.getName() + " already declared");
} }
localVar.put(id.getName(), type); localVar.put(typedId.getName(), type);
return type; return type;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }

View File

@ -7,12 +7,12 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class FieldId implements Node { public class TypedFieldId implements Node {
private Boolean field; private Boolean field;
private Expression recipient; private Expression recipient;
private Id id; private TypedId typedId;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
//TODO: Implement typeCheck for FieldId //TODO: Implement typeCheck for FieldId
if(field){ if(field){
return null; return null;
@ -22,7 +22,7 @@ public class FieldId implements Node {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -9,23 +9,23 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class For implements Statement { public class TypedFor implements Statement {
private Expression assign; private Expression assign;
private Expression cond; private Expression cond;
private Expression inc; private Expression inc;
private Block block; private TypedBlock typedBlock;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
assign.typeCheck(localVar, classes); assign.typeCheck(localVar, classes);
if(!cond.typeCheck(localVar, classes).equals(Type.BOOL)) { if(!cond.typeCheck(localVar, classes).equals(Type.BOOL)) {
throw new RuntimeException("Condition must be a boolean"); throw new RuntimeException("Condition must be a boolean");
} }
inc.typeCheck(localVar, classes); inc.typeCheck(localVar, classes);
return block.typeCheck(localVar, classes); return typedBlock.typeCheck(localVar, classes);
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,11 +8,11 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Id implements Expression { public class TypedId implements Expression {
private String name; private String name;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(!localVar.containsKey(name)){ if(!localVar.containsKey(name)){
throw new RuntimeException("Variable " + name + " not declared or not found"); throw new RuntimeException("Variable " + name + " not declared or not found");
} }
@ -21,7 +21,7 @@ public class Id implements Expression {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -9,26 +9,26 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class IfElse implements Statement { public class TypedIfElse implements Statement {
private Expression cond; private Expression cond;
private Block ifBlock; private TypedBlock ifTypedBlock;
private Block elseBlock; private TypedBlock elseTypedBlock;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
// if(cond, Statement1, Statement2) // if(cond, Statement1, Statement2)
// condition must be a boolean // condition must be a boolean
if(cond.typeCheck(localVar, classes) != Type.BOOL){ if(cond.typeCheck(localVar, classes) != Type.BOOL){
throw new RuntimeException("If condition must be a boolean"); throw new RuntimeException("If condition must be a boolean");
} }
//Statement1 and Statement2 must be of type void //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"); throw new RuntimeException("If block must be of type void");
} }
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,15 +8,15 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class IntLiteral implements Expression { public class TypedIntLiteral implements Expression {
private Integer value; private Integer value;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.INT; return Type.INT;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -6,21 +6,21 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class LocalVariable implements Node { public class TypedLocalVariable implements Node {
private Id name; private TypedId typedId;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(name.getName())) { if(localVar.containsKey(typedId.getName())) {
throw new RuntimeException("Variable " + name.getName() + " already exists"); throw new RuntimeException("Variable " + typedId.getName() + " already exists");
} }
localVar.put(name.getName(), type); localVar.put(typedId.getName(), type);
return type; return type;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,15 +7,15 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class MainMethod implements Node { public class TypedMainMethod implements Node {
private Block block; private TypedBlock typedBlock;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return block.typeCheck(localVar, classes); return typedBlock.typeCheck(localVar, classes);
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -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<TypedParameter> typedParameters;
private TypedBlock typedBlock;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> 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<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null;
}
}

View File

@ -10,20 +10,20 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class MethodCall implements Expression, Statement { public class TypedMethodCall implements Expression, Statement {
private Boolean field; private Boolean field;
private Expression recipient; private Expression recipient;
private Id id; private TypedId typedId;
private List<Expression> args; private List<Expression> args;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
//TODO: Implement typeCheck for MethodCall //TODO: Implement typeCheck for MethodCall
return null; return null;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -10,24 +10,24 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class New implements Expression, Statement { public class TypedNew implements Expression, Statement {
private Type type; private Type type;
private List<Expression> args; private List<Expression> args;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
// new A(1, 2, 3) // new A(1, 2, 3)
if (!classes.containsKey(this.type.getReference())) { if (!classes.containsKey(this.type.getReference())) {
throw new RuntimeException("Class " + this.type.getReference() + " not found"); throw new RuntimeException("Class " + this.type.getReference() + " not found");
} }
Class klasse = classes.get(this.type.getReference()); TypedClass klasse = classes.get(this.type.getReference());
if (klasse.getFields().size() != args.size()) { if (klasse.getTypedFields().size() != args.size()) {
throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference()); throw new RuntimeException("number of arguments is invalid for class" + this.type.getReference());
} }
// compare the types of the arguments // compare the types of the arguments
for (int i = 0; i < args.size(); i++) { for (int i = 0; i < args.size(); i++) {
Type type = args.get(i).typeCheck(localVar, classes); 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()); throw new RuntimeException("False type for argument " + i + " in class " + type.getKind());
} }
} }
@ -35,7 +35,7 @@ public class New implements Expression, Statement {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,21 +7,21 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Parameter implements Node { public class TypedParameter implements Node {
private Id id; private TypedId typedId;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(id.getName())) { if(localVar.containsKey(typedId.getName())) {
throw new RuntimeException("Parameter " + id.getName() + " already exists"); throw new RuntimeException("Parameter " + typedId.getName() + " already exists");
} }
localVar.put(id.getName(), type); localVar.put(typedId.getName(), type);
return type; return type;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }

View File

@ -8,18 +8,18 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Data @Data
public class Program implements Node { public class TypedProgram implements Node {
private List<Class> classes; private List<TypedClass> typedClasses;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
for(Class c : this.classes){ for(TypedClass c : this.typedClasses){
c.typeCheck(localVar, classes); c.typeCheck(localVar, classes);
} }
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -9,16 +9,16 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Return implements Statement { public class TypedReturn implements Statement {
private Expression ret; private Expression ret;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return ret.typeCheck(localVar, classes); return ret.typeCheck(localVar, classes);
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -7,14 +7,15 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Break implements Statement { public class TypedReturnVoid implements Statement {
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
return Type.VOID; return Type.VOID;
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -8,11 +8,11 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class Unary implements Expression { public class TypedUnary implements Expression {
private UnaryOperator op; private UnaryOperator op;
private Expression right; private Expression right;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(op == UnaryOperator.NOT){ if(op == UnaryOperator.NOT){
if(right.typeCheck(localVar, classes) != Type.BOOL){ if(right.typeCheck(localVar, classes) != Type.BOOL){
@ -31,7 +31,7 @@ public class Unary implements Expression {
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }

View File

@ -9,19 +9,19 @@ import lombok.Data;
import java.util.Map; import java.util.Map;
@Data @Data
public class While implements Statement { public class TypedWhile implements Statement {
private Expression cond; private Expression cond;
private Block block; private TypedBlock typedBlock;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, Class> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(cond.typeCheck(localVar, classes) != Type.BOOL){ if(cond.typeCheck(localVar, classes) != Type.BOOL){
throw new RuntimeException("While condition must be a boolean"); throw new RuntimeException("While condition must be a boolean");
} }
return block.typeCheck(localVar, classes); return typedBlock.typeCheck(localVar, classes);
} }
@Override @Override
public Node convertToTypedAST(Map<String, Type> localVar, Map<String, Class> classes, de.maishai.ast.records.Node unTypedAST) { public Node convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
return null; return null;
} }
} }