mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 16:18:02 +00:00
Change the parameter classes to one class in TypedNode
This commit is contained in:
parent
735781a63c
commit
cb4c637ce4
@ -1,10 +1,11 @@
|
||||
package de.maishai.typedast;
|
||||
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.typedclass.TypedClass;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface TypedNode {
|
||||
Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes);
|
||||
TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST);
|
||||
Type typeCheck(Map<String, Type> localVar, TypedClass clas);
|
||||
TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.util.Map;
|
||||
|
||||
public class TypedExpressionUtil {
|
||||
|
||||
public static TypedExpression getKindOfExpression(Map<String, Type> localVar, Map<String, TypedClass> classes, Expression expression) {
|
||||
public static TypedExpression getKindOfExpression(Map<String, Type> localVar, TypedClass clas, Expression expression) {
|
||||
if (expression instanceof BoolLiteral boolLiteral) {
|
||||
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
|
||||
typedBoolLiteral.setValue(boolLiteral.value());
|
||||
@ -17,23 +17,23 @@ public class TypedExpressionUtil {
|
||||
}
|
||||
else if (expression instanceof CharLiteral charLiteral) {
|
||||
TypedCharLiteral typedCharLiteral = new TypedCharLiteral()
|
||||
.convertToCharLiteral(localVar, classes, charLiteral);
|
||||
.convertToCharLiteral(localVar, clas, charLiteral);
|
||||
return typedCharLiteral;
|
||||
}
|
||||
else if (expression instanceof IntLiteral intLiteral) {
|
||||
TypedIntLiteral typedIntLiteral = new TypedIntLiteral()
|
||||
.convertToTypedIntLiteral(localVar, classes, intLiteral);
|
||||
.convertToTypedIntLiteral(localVar, clas, intLiteral);
|
||||
return typedIntLiteral;
|
||||
}
|
||||
else if (expression instanceof Binary binary) {
|
||||
TypedBinary typedBinary = new TypedBinary()
|
||||
.convertToTypedBinary(localVar, classes, binary);
|
||||
typedBinary.typeCheck(localVar, classes);
|
||||
.convertToTypedBinary(localVar, clas, binary);
|
||||
typedBinary.typeCheck(localVar, clas);
|
||||
return typedBinary;
|
||||
}
|
||||
else if (expression instanceof FieldVarAccess fieldVarAccess) {
|
||||
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess()
|
||||
.convertToTypedFieldVarAccess(localVar, classes, fieldVarAccess);
|
||||
.convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess);
|
||||
return typedFieldVarAccess;
|
||||
}
|
||||
else if (expression instanceof MethodCall methodCall) {
|
||||
@ -44,7 +44,7 @@ public class TypedExpressionUtil {
|
||||
typedFieldVarAccess.setField(methodCall.recipient().field());
|
||||
typedMethodCall.setRecipient(typedFieldVarAccess);
|
||||
for (var arg : methodCall.args()) {
|
||||
typedMethodCall.getArgs().add(getKindOfExpression(localVar, classes, arg));
|
||||
typedMethodCall.getArgs().add(getKindOfExpression(localVar, clas, arg));
|
||||
}
|
||||
return typedMethodCall;
|
||||
}
|
||||
@ -52,14 +52,14 @@ public class TypedExpressionUtil {
|
||||
TypedNew typedNew = new TypedNew();
|
||||
typedNew.setType(newStmt.type());
|
||||
for (var arg : newStmt.args()) {
|
||||
typedNew.getArgs().add(getKindOfExpression(localVar, classes, arg));
|
||||
typedNew.getArgs().add(getKindOfExpression(localVar, clas, arg));
|
||||
}
|
||||
return typedNew;
|
||||
}
|
||||
else if (expression instanceof Unary unary) {
|
||||
TypedUnary typedUnary = new TypedUnary();
|
||||
typedUnary.setOp(unary.op());
|
||||
typedUnary.setRight(getKindOfExpression(localVar, classes, unary.right()));
|
||||
typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right()));
|
||||
return typedUnary;
|
||||
} else {
|
||||
return null;
|
||||
|
@ -20,19 +20,19 @@ public class TypedAssignment implements TypedStatement {
|
||||
private TypedExpression value;
|
||||
private Type type;
|
||||
|
||||
public TypedAssignment convertToTypedAssignment(Map<String, Type> localVar, Map<String, TypedClass> classes, Assignment untyped) {
|
||||
public TypedAssignment convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
|
||||
varName = untyped.location().id();
|
||||
value = getKindOfExpression(localVar, classes, untyped.value());
|
||||
value = getKindOfExpression(localVar, clas, untyped.value());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (!localVar.containsKey(varName)) {
|
||||
throw new RuntimeException("Variable not declared");
|
||||
}
|
||||
Type typeLeft = localVar.get(varName);
|
||||
Type typeRight = value.typeCheck(localVar, classes);
|
||||
Type typeRight = value.typeCheck(localVar, clas);
|
||||
|
||||
if (typeLeft.equals(typeRight) ) {
|
||||
type = typeLeft;
|
||||
@ -42,11 +42,11 @@ public class TypedAssignment implements TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
|
||||
Assignment untyped = (Assignment) unTypedAST;
|
||||
TypedAssignment typedAssignment = new TypedAssignment();
|
||||
typedAssignment.setVarName(untyped.location().id());
|
||||
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
|
||||
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, clas, untyped.value()));
|
||||
return typedAssignment;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.Operator;
|
||||
import de.maishai.ast.records.Binary;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
@ -20,34 +21,34 @@ public class TypedBinary implements TypedExpression {
|
||||
private TypedExpression right;
|
||||
private Type type;
|
||||
|
||||
public TypedBinary convertToTypedBinary(Map<String, Type> localVar, Map<String, TypedClass> classes, Binary unTypedBinary) {
|
||||
left = getKindOfExpression(localVar, classes, unTypedBinary.left());
|
||||
right = getKindOfExpression(localVar, classes, unTypedBinary.right());
|
||||
public TypedBinary convertToTypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
|
||||
left = getKindOfExpression(localVar, clas, unTypedBinary.left());
|
||||
right = getKindOfExpression(localVar, clas, unTypedBinary.right());
|
||||
op = unTypedBinary.op();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
|
||||
if (left.typeCheck(localVar, classes) == Type.INT &&
|
||||
right.typeCheck(localVar, classes) == Type.INT) {
|
||||
if (left.typeCheck(localVar, clas) == Type.INT &&
|
||||
right.typeCheck(localVar, clas) == Type.INT) {
|
||||
type = Type.INT;
|
||||
return Type.INT;
|
||||
} else {
|
||||
throw new RuntimeException("Type mismatch in " + op);
|
||||
}
|
||||
} 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) {
|
||||
if (left.typeCheck(localVar, clas) == Type.INT &&
|
||||
right.typeCheck(localVar, clas) == Type.INT) {
|
||||
type = Type.BOOL;
|
||||
return Type.BOOL;
|
||||
} else {
|
||||
throw new RuntimeException("Type mismatch in " + op);
|
||||
}
|
||||
} else if (op == Operator.AND || op == Operator.OR) {
|
||||
if (left.typeCheck(localVar, classes) == Type.BOOL &&
|
||||
right.typeCheck(localVar, classes) == Type.BOOL) {
|
||||
if (left.typeCheck(localVar, clas) == Type.BOOL &&
|
||||
right.typeCheck(localVar, clas) == Type.BOOL) {
|
||||
type = Type.BOOL;
|
||||
return Type.BOOL;
|
||||
} else {
|
||||
@ -60,13 +61,13 @@ public class TypedBinary implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Binary binary = (Binary) unTypedAST;
|
||||
|
||||
TypedBinary typedBinary = new TypedBinary();
|
||||
typedBinary.setLeft((TypedExpression) this.left.convertToTypedAST(localVar, classes, binary.left()));
|
||||
typedBinary.setLeft((TypedExpression) this.left.convertToTypedAST(localVar, clas, binary.left()));
|
||||
typedBinary.setOp(binary.op());
|
||||
typedBinary.setRight((TypedExpression) this.right.convertToTypedAST(localVar, classes, binary.right()));
|
||||
typedBinary.setRight((TypedExpression) this.right.convertToTypedAST(localVar, clas, binary.right()));
|
||||
|
||||
return typedBinary;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ public class TypedBlock implements TypedNode {
|
||||
private List<TypedStatement> stmts;
|
||||
|
||||
|
||||
public TypedBlock blockToTypedBlock(Map<String, Type> localVar, Map<String, TypedClass> classes, Block unTypedBlock) {
|
||||
public TypedBlock blockToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
|
||||
|
||||
if (unTypedBlock == null) {
|
||||
return null;
|
||||
@ -34,7 +34,7 @@ public class TypedBlock implements TypedNode {
|
||||
} else {
|
||||
for (Declaration var : unTypedBlock.localVariables()) {
|
||||
TypedLocalVariable typedVar = new TypedLocalVariable()
|
||||
.convertToTypedLocalVariable(localVar, classes, var);
|
||||
.convertToTypedLocalVariable(localVar, clas, var);
|
||||
typedBlock.getVars().add(typedVar);
|
||||
}
|
||||
}
|
||||
@ -46,41 +46,41 @@ public class TypedBlock implements TypedNode {
|
||||
for (var stmt : unTypedBlock.stmts()) {
|
||||
if (stmt instanceof Assignment assignment) {
|
||||
TypedAssignment typedAssignment = new TypedAssignment()
|
||||
.convertToTypedAssignment(localVar, classes, assignment);
|
||||
typedAssignment.typeCheck(localVar, classes);
|
||||
.convertToTypedAssignment(localVar, clas, assignment);
|
||||
typedAssignment.typeCheck(localVar, clas);
|
||||
typedBlock.getStmts().add(typedAssignment);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof For forStmt) {
|
||||
TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, classes, forStmt);
|
||||
typedFor.typeCheck(localVar, classes);
|
||||
TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, clas, forStmt);
|
||||
typedFor.typeCheck(localVar, clas);
|
||||
typedBlock.getStmts().add(typedFor);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof IfElse ifElse) {
|
||||
TypedIfElse typedIfElse = new TypedIfElse()
|
||||
.convertToTypedIfElse(localVar, classes, ifElse);
|
||||
typedIfElse.typeCheck(localVar, classes);
|
||||
.convertToTypedIfElse(localVar, clas, ifElse);
|
||||
typedIfElse.typeCheck(localVar, clas);
|
||||
typedBlock.getStmts().add(typedIfElse);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof While whileStmt) {
|
||||
TypedWhile typedWhile = new TypedWhile()
|
||||
.convertToTypedWhile(localVar, classes, whileStmt);
|
||||
.convertToTypedWhile(localVar, clas, whileStmt);
|
||||
//TODO: check the type
|
||||
typedBlock.getStmts().add(typedWhile);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof DoWhile doWhile) {
|
||||
TypedDoWhile typedDoWhile = new TypedDoWhile()
|
||||
.convertToTypedDoWhile(localVar, classes, doWhile);
|
||||
.convertToTypedDoWhile(localVar, clas, doWhile);
|
||||
//TODO: check the type
|
||||
typedBlock.getStmts().add(typedDoWhile);
|
||||
continue;
|
||||
}
|
||||
if (stmt instanceof Return returnStmt) {
|
||||
TypedReturn typedReturn = new TypedReturn()
|
||||
.convertToTypedReturn(localVar, classes, returnStmt);
|
||||
.convertToTypedReturn(localVar, clas, returnStmt);
|
||||
//TODO: check the type
|
||||
typedBlock.getStmts().add(typedReturn);
|
||||
continue;
|
||||
@ -93,7 +93,7 @@ public class TypedBlock implements TypedNode {
|
||||
|
||||
if (stmt instanceof Break) {
|
||||
TypedBreak typedBreak = new TypedBreak();
|
||||
typedBreak.typeCheck(localVar, classes);
|
||||
typedBreak.typeCheck(localVar, clas);
|
||||
typedBlock.getStmts().add(typedBreak);
|
||||
continue;
|
||||
}
|
||||
@ -109,19 +109,19 @@ public class TypedBlock implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (vars == null) {
|
||||
vars = List.of();
|
||||
} else {
|
||||
for (TypedLocalVariable var : vars) {
|
||||
var.typeCheck(localVar, classes);
|
||||
var.typeCheck(localVar, clas);
|
||||
}
|
||||
}
|
||||
if (stmts == null) {
|
||||
stmts = List.of();
|
||||
} else {
|
||||
for (TypedStatement stmt : stmts) {
|
||||
stmt.typeCheck(localVar, classes);
|
||||
stmt.typeCheck(localVar, clas);
|
||||
}
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ public class TypedBlock implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Block untyped = (Block) unTypedAST;
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedBlock.setStmts(new ArrayList<>());
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.BoolLiteral;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
@ -16,19 +17,19 @@ public class TypedBoolLiteral implements TypedExpression {
|
||||
private Boolean value;
|
||||
private Type type;
|
||||
|
||||
public TypedBoolLiteral convertToTypedBoolLiteral(Map<String, Type> localVar, Map<String, TypedClass> classes,BoolLiteral unTypedBoolLiteral) {
|
||||
public TypedBoolLiteral convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
|
||||
value = unTypedBoolLiteral.value();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
type = Type.BOOL;
|
||||
return Type.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
BoolLiteral untyped = (BoolLiteral) unTypedAST;
|
||||
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
|
||||
typedBoolLiteral.setValue(untyped.value());
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Break;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.TypedStatement;
|
||||
@ -17,13 +18,13 @@ public class TypedBreak implements TypedStatement {
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
type = Type.VOID;
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Break untyped = (Break) unTypedAST;
|
||||
if(untyped == null) {
|
||||
throw new RuntimeException("Break not found");
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.CharLiteral;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
@ -12,19 +13,19 @@ import java.util.Map;
|
||||
@Data
|
||||
public class TypedCharLiteral implements TypedExpression {
|
||||
private char value;
|
||||
private Type type = Type.CHAR;
|
||||
|
||||
public TypedCharLiteral convertToCharLiteral(Map<String, Type> localVar, Map<String, TypedClass> classes, CharLiteral unTypedCharLiteral) {
|
||||
public TypedCharLiteral convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
|
||||
value = unTypedCharLiteral.value();
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
return Type.CHAR;
|
||||
}
|
||||
private Type type;
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
CharLiteral untyped = (CharLiteral) unTypedAST;
|
||||
TypedCharLiteral typedCharLiteral = new TypedCharLiteral();
|
||||
typedCharLiteral.setValue(untyped.value());
|
||||
|
@ -1,10 +1,8 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.Operator;
|
||||
import de.maishai.ast.records.*;
|
||||
import de.maishai.ast.records.Class;
|
||||
import de.maishai.ast.records.Constructor;
|
||||
import de.maishai.ast.records.Declaration;
|
||||
import de.maishai.ast.records.Method;
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
@ -29,33 +27,33 @@ public class TypedClass implements TypedNode {
|
||||
private List<TypedConstructor> typedConstructors;
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
|
||||
if (classes.containsKey(className)) {
|
||||
throw new RuntimeException("Class " + className + " already exists");
|
||||
if (clas == null) {
|
||||
throw new RuntimeException("Class not found");
|
||||
}
|
||||
classes.put(className, this);
|
||||
|
||||
for (TypedField field : typedFields) {
|
||||
field.typeCheck(localVar, classes);
|
||||
field.typeCheck(localVar, clas);
|
||||
}
|
||||
for(TypedConstructor constructor : typedConstructors) {
|
||||
constructor.typeCheck(localVar, classes);
|
||||
for (TypedConstructor constructor : typedConstructors) {
|
||||
constructor.typeCheck(localVar, clas);
|
||||
}
|
||||
for (TypedMethod typedMethod : typedMethods) {
|
||||
typedMethod.typeCheck(localVar, classes);
|
||||
typedMethod.typeCheck(localVar, clas);
|
||||
}
|
||||
return Type.REFERENCE(className);
|
||||
}
|
||||
|
||||
public TypedNode startConversion(de.maishai.ast.records.Class c) {
|
||||
Map<String, Type> local = new HashMap<>();
|
||||
Map<String, TypedClass> classMap = new HashMap<>();
|
||||
TypedClass classMap = new TypedClass();
|
||||
|
||||
return convertToTypedAST(local, classMap, c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
TypedClass typedClass = new TypedClass();
|
||||
Class c = (Class) unTypedAST;
|
||||
typedClass.setClassName(c.classname());
|
||||
@ -65,18 +63,18 @@ public class TypedClass implements TypedNode {
|
||||
|
||||
for (Declaration field : c.fieldDeclarations()) {
|
||||
TypedField typedField = new TypedField();
|
||||
typedClass.getTypedFields().add(typedField.convertToTypedField(localVar, classes, field));
|
||||
typedClass.getTypedFields().add(typedField.convertToTypedField(localVar, clas, field));
|
||||
}
|
||||
|
||||
|
||||
for (Method method : c.methods()) {
|
||||
TypedMethod typedMethod = new TypedMethod();
|
||||
typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, classes,method));
|
||||
typedClass.getTypedMethods().add(typedMethod.convertToTypedMethod(localVar, clas, method));
|
||||
}
|
||||
|
||||
for (Constructor constructor : c.constructors()) {
|
||||
TypedConstructor typedConstructor = new TypedConstructor();
|
||||
typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, classes, constructor));
|
||||
typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor));
|
||||
}
|
||||
|
||||
return typedClass;
|
||||
@ -111,7 +109,7 @@ public class TypedClass implements TypedNode {
|
||||
c.typedFields = List.of(f1, f2);
|
||||
|
||||
//Constructors
|
||||
TypedConstructor constructor = new TypedConstructor( "SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>()));
|
||||
TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>()));
|
||||
c.typedConstructors = List.of(constructor);
|
||||
|
||||
//Methods
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Constructor;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.ast.records.Parameter;
|
||||
import de.maishai.typedast.CodeGenUtils;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
@ -26,7 +27,7 @@ public class TypedConstructor implements TypedNode {
|
||||
private List<TypedParameter> typedParameters;
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
public TypedConstructor constructorToTypedConstructor(Map<String, Type> localVar, Map<String, TypedClass> classes, Constructor unTypedConstructor) {
|
||||
public TypedConstructor constructorToTypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
|
||||
TypedConstructor typedConstructor = new TypedConstructor();
|
||||
typedConstructor.setName(unTypedConstructor.className());
|
||||
typedConstructor.setTypedParameters(new ArrayList<>());
|
||||
@ -44,27 +45,27 @@ public class TypedConstructor implements TypedNode {
|
||||
|
||||
}
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes ,unTypedConstructor.block()));
|
||||
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas ,unTypedConstructor.block()));
|
||||
return typedConstructor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (localVar.containsKey(name)) {
|
||||
throw new RuntimeException("constructor already declared");
|
||||
}
|
||||
localVar.put(name, Type.VOID);
|
||||
if (typedParameters != null) {
|
||||
for (TypedParameter param : typedParameters) {
|
||||
param.typeCheck(localVar, classes);
|
||||
param.typeCheck(localVar, clas);
|
||||
}
|
||||
}
|
||||
typedBlock.typeCheck(localVar, classes);
|
||||
typedBlock.typeCheck(localVar, clas);
|
||||
return Type.VOID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Constructor untyped = (Constructor) unTypedAST;
|
||||
TypedConstructor typedConstructor = new TypedConstructor();
|
||||
typedConstructor.setName(untyped.className());
|
||||
@ -83,7 +84,7 @@ public class TypedConstructor implements TypedNode {
|
||||
|
||||
}
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block()));
|
||||
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
|
||||
return typedConstructor;
|
||||
}
|
||||
|
||||
|
@ -16,29 +16,29 @@ public class TypedDoWhile implements TypedStatement {
|
||||
|
||||
public TypedDoWhile() {
|
||||
}
|
||||
public TypedDoWhile convertToTypedDoWhile(Map<String, Type> localVar, Map<String, TypedClass> classes, DoWhile unTypedDoWhile) {
|
||||
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedDoWhile.block());
|
||||
cond = getKindOfExpression(localVar, classes, unTypedDoWhile.cond());
|
||||
public TypedDoWhile convertToTypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
|
||||
typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedDoWhile.block());
|
||||
cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
if (cond.typeCheck(localVar, classes) != Type.BOOL) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (cond.typeCheck(localVar, clas) != Type.BOOL) {
|
||||
throw new RuntimeException("Condition must be boolean");
|
||||
}
|
||||
typedBlock.typeCheck(localVar, classes);
|
||||
typedBlock.typeCheck(localVar, clas);
|
||||
return Type.VOID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
DoWhile untyped = (DoWhile) unTypedAST;
|
||||
|
||||
TypedDoWhile typedDoWhile = new TypedDoWhile();
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block()));
|
||||
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
|
||||
typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
|
||||
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond()));
|
||||
|
||||
return typedDoWhile;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Declaration;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
import lombok.AllArgsConstructor;
|
||||
@ -18,7 +19,7 @@ public class TypedField implements TypedNode {
|
||||
private String varName;
|
||||
private Type type;
|
||||
|
||||
public TypedField convertToTypedField(Map<String, Type> localVar, Map<String, TypedClass> classes, Declaration declaration){
|
||||
public TypedField convertToTypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
|
||||
if(localVar.containsKey(declaration.name())){
|
||||
throw new RuntimeException("Variable " + declaration.name() + " already declared");
|
||||
}
|
||||
@ -29,7 +30,7 @@ public class TypedField implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (localVar.containsKey(varName)) {
|
||||
throw new RuntimeException("Variable " + varName + " already declared");
|
||||
}
|
||||
@ -38,7 +39,7 @@ public class TypedField implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Declaration untypedDeclaration = (Declaration) unTypedAST;
|
||||
TypedField typedField = new TypedField();
|
||||
|
||||
|
@ -19,16 +19,16 @@ public class TypedFieldVarAccess implements TypedExpression {
|
||||
private String name;
|
||||
private Type type;
|
||||
|
||||
public TypedFieldVarAccess convertToTypedFieldVarAccess(Map<String, Type> localVar, Map<String, TypedClass> classes, FieldVarAccess unTypedFieldVarAccess) {
|
||||
public TypedFieldVarAccess convertToTypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
|
||||
field = unTypedFieldVarAccess.field();
|
||||
recursiveOwnerChain = getKindOfExpression(localVar, classes, unTypedFieldVarAccess.recursiveOwnerChain());
|
||||
recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain());
|
||||
name = unTypedFieldVarAccess.id();
|
||||
type = localVar.get(unTypedFieldVarAccess.id());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
Type type = localVar.get(name);
|
||||
|
||||
if (type == null) {
|
||||
@ -42,11 +42,11 @@ public class TypedFieldVarAccess implements TypedExpression {
|
||||
return type;
|
||||
}
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST;
|
||||
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess();
|
||||
typedFieldVarAccess.setField(untypedFieldVarAccess.field());
|
||||
typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, classes, untypedFieldVarAccess.recursiveOwnerChain()));
|
||||
typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, clas, untypedFieldVarAccess.recursiveOwnerChain()));
|
||||
typedFieldVarAccess.setName(untypedFieldVarAccess.id());
|
||||
|
||||
return typedFieldVarAccess;
|
||||
|
@ -20,36 +20,36 @@ public class TypedFor implements TypedStatement {
|
||||
private TypedAssignment inc;
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
public TypedFor convertToTypedFor(Map<String, Type> localVar, Map<String, TypedClass> classes, For unTypedFor) {
|
||||
assign = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.assign());
|
||||
cond = getKindOfExpression(localVar, classes, unTypedFor.cond());
|
||||
inc = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.inc());
|
||||
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedFor.block());
|
||||
public TypedFor convertToTypedFor(Map<String, Type> localVar,TypedClass clas, For unTypedFor) {
|
||||
assign = new TypedAssignment().convertToTypedAssignment(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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
assign.typeCheck(localVar, classes);
|
||||
if (!cond.typeCheck(localVar, classes).equals(Type.BOOL)) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
assign.typeCheck(localVar, clas);
|
||||
if (!cond.typeCheck(localVar, clas).equals(Type.BOOL)) {
|
||||
throw new RuntimeException("Condition must be a boolean");
|
||||
}
|
||||
inc.typeCheck(localVar, classes);
|
||||
return typedBlock.typeCheck(localVar, classes);
|
||||
inc.typeCheck(localVar, clas);
|
||||
return typedBlock.typeCheck(localVar, clas);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
For untyped = (For) unTypedAST;
|
||||
TypedFor typedFor = new TypedFor();
|
||||
TypedAssignment assign = new TypedAssignment();
|
||||
typedFor.setAssign(assign.convertToTypedAssignment(localVar,classes,untyped.assign()));
|
||||
typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign()));
|
||||
TypedAssignment lnc = new TypedAssignment();
|
||||
typedFor.setInc(lnc.convertToTypedAssignment(localVar,classes,untyped.inc()));
|
||||
typedFor.setInc(lnc.convertToTypedAssignment(localVar,clas,untyped.inc()));
|
||||
|
||||
typedFor.setCond(null);
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block()));
|
||||
typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
|
||||
|
||||
return typedFor;
|
||||
}
|
||||
|
@ -17,36 +17,36 @@ public class TypedIfElse implements TypedStatement {
|
||||
private TypedBlock ifTypedBlock;
|
||||
private TypedBlock elseTypedBlock;
|
||||
|
||||
public TypedIfElse convertToTypedIfElse(Map<String, Type> localVar, Map<String, TypedClass> classes, IfElse unTypedIfElse) {
|
||||
ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.ifBlock());
|
||||
elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.elseBlock());
|
||||
typedCon = getKindOfExpression(localVar, classes, unTypedIfElse.cond());
|
||||
public TypedIfElse convertToTypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
|
||||
ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.ifBlock());
|
||||
elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.elseBlock());
|
||||
typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
|
||||
if (typedCon.typeCheck(localVar, classes) != Type.BOOL) {
|
||||
if (typedCon.typeCheck(localVar, clas) != Type.BOOL) {
|
||||
throw new RuntimeException("If condition must be a boolean");
|
||||
}
|
||||
if (ifTypedBlock.typeCheck(localVar, classes) != Type.VOID) {
|
||||
if (ifTypedBlock.typeCheck(localVar, clas) != Type.VOID) {
|
||||
throw new RuntimeException("If block must be of type void");
|
||||
}
|
||||
return Type.VOID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
IfElse ifElse = (IfElse) unTypedAST;
|
||||
|
||||
TypedIfElse typedIfElse = new TypedIfElse();
|
||||
typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, classes, ifElse.cond()));
|
||||
typedIfElse.setTypedCon((TypedExpression) typedCon.convertToTypedAST(localVar, clas, ifElse.cond()));
|
||||
|
||||
TypedBlock ifTypedBlock = new TypedBlock();
|
||||
TypedBlock elseTypedBlock = new TypedBlock();
|
||||
typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, classes, ifElse.ifBlock()));
|
||||
typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, classes, ifElse.elseBlock()));
|
||||
typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, clas, ifElse.ifBlock()));
|
||||
typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, clas, ifElse.elseBlock()));
|
||||
|
||||
return typedIfElse;
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ import java.util.Map;
|
||||
public class TypedIntLiteral implements TypedExpression {
|
||||
private Integer value;
|
||||
|
||||
public TypedIntLiteral convertToTypedIntLiteral(Map<String, Type> localVar, Map<String, TypedClass> classes, IntLiteral unTypedIntLiteral) {
|
||||
public TypedIntLiteral convertToTypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) {
|
||||
value = unTypedIntLiteral.value();
|
||||
return this;
|
||||
}
|
||||
@ -28,12 +28,12 @@ public class TypedIntLiteral implements TypedExpression {
|
||||
this.value = value;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
return Type.INT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
|
||||
IntLiteral untyped = (IntLiteral) unTypedAST;
|
||||
return new TypedIntLiteral(untyped.value());
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Declaration;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
@ -16,7 +17,7 @@ import java.util.Map;
|
||||
public final class TypedLocalVariable implements TypedNode {
|
||||
private String name;
|
||||
private Type type;
|
||||
public TypedLocalVariable convertToTypedLocalVariable(Map<String, Type> localVar, Map<String, TypedClass> classes, Declaration declaration){
|
||||
public TypedLocalVariable convertToTypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
|
||||
if(localVar.containsKey(declaration.name())){
|
||||
throw new RuntimeException("Variable " + declaration.name() + " already declared");
|
||||
}
|
||||
@ -27,7 +28,7 @@ public final class TypedLocalVariable implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
Type type = localVar.get(name);
|
||||
if(type == this.type) {
|
||||
return type;
|
||||
@ -36,7 +37,7 @@ public final class TypedLocalVariable implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Declaration untypedLocalVariable = (Declaration) unTypedAST;
|
||||
TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Method;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.ast.records.Parameter;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
@ -19,10 +20,11 @@ public class TypedMethod implements TypedNode {
|
||||
private List<TypedParameter> typedParameters;
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
public TypedMethod convertToTypedMethod(Map<String, Type> localVar, Map<String, TypedClass> classes, Method unTypedMethod) {
|
||||
public TypedMethod convertToTypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
|
||||
TypedMethod typedMethod = new TypedMethod();
|
||||
typedMethod.setName(unTypedMethod.methodName());
|
||||
typedMethod.setTypedParameters(new ArrayList<>());
|
||||
typedMethod.setReturnType(unTypedMethod.type());
|
||||
|
||||
if(unTypedMethod.params().isEmpty()){
|
||||
typedMethod.setTypedParameters(List.of());
|
||||
@ -31,27 +33,27 @@ public class TypedMethod implements TypedNode {
|
||||
TypedParameter typedParameter = new TypedParameter();
|
||||
typedParameter.setParaName(parameter.name());
|
||||
typedParameter.setType(parameter.type());
|
||||
typedParameters.add(typedParameter);
|
||||
typedMethod.getTypedParameters().add(typedParameter);
|
||||
}
|
||||
}
|
||||
TypedBlock block = new TypedBlock();
|
||||
typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, classes, unTypedMethod.block()));
|
||||
typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, clas, unTypedMethod.block()));
|
||||
return typedMethod;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if(localVar.containsKey(name)) {
|
||||
throw new RuntimeException("Method " + name + " already exists");
|
||||
}
|
||||
localVar.put(name, returnType);
|
||||
for(TypedParameter typedParameter : typedParameters) {
|
||||
typedParameter.typeCheck(localVar, classes);
|
||||
typedParameter.typeCheck(localVar, clas);
|
||||
}
|
||||
return typedBlock.typeCheck(localVar, classes);
|
||||
return typedBlock.typeCheck(localVar, clas);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Method untypedMethod = (Method) unTypedAST;
|
||||
TypedMethod typedMethod = new TypedMethod();
|
||||
|
||||
@ -67,7 +69,7 @@ public class TypedMethod implements TypedNode {
|
||||
}
|
||||
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes,untypedMethod.block()));
|
||||
typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block()));
|
||||
|
||||
return typedMethod;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.MethodCall;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
@ -20,13 +21,13 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
//TODO: Implement typeCheck for MethodCall
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
//TODO: Implement this
|
||||
return null;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.New;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.typedast.*;
|
||||
import lombok.Data;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
@ -18,20 +19,17 @@ public class TypedNew implements TypedExpression, TypedStatement {
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
// new A(1, 2, 3)
|
||||
if (!classes.containsKey(this.type.getReference())) {
|
||||
throw new RuntimeException("Class " + this.type.getReference() + " not found");
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if(clas == null){
|
||||
throw new RuntimeException("Class not found");
|
||||
}
|
||||
|
||||
TypedClass klasse = classes.get(this.type.getReference());
|
||||
if (klasse.getTypedFields().size() != args.size()) {
|
||||
if (clas.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.getTypedFields().get(i).getType())) {
|
||||
Type type = args.get(i).typeCheck(localVar, clas);
|
||||
if (!type.equals(clas.getTypedFields().get(i).getType())) {
|
||||
throw new RuntimeException("False type for argument " + i + " in class " + type.getKind());
|
||||
}
|
||||
}
|
||||
@ -39,7 +37,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
|
||||
New untyped = (New) unTypedAST;
|
||||
TypedNew typedNew = new TypedNew();
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.ast.records.Parameter;
|
||||
import de.maishai.typedast.TypedNode;
|
||||
import de.maishai.typedast.Type;
|
||||
@ -23,7 +24,7 @@ public class TypedParameter implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if (localVar.containsKey(paraName)) {
|
||||
throw new RuntimeException("Parameter " + paraName + " already exists");
|
||||
}
|
||||
@ -32,7 +33,7 @@ public class TypedParameter implements TypedNode {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
|
||||
Parameter untyped = (Parameter) unTypedAST;
|
||||
TypedParameter typedParameter = new TypedParameter();
|
||||
typedParameter.setParaName(untyped.name());
|
||||
|
@ -15,22 +15,22 @@ import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
|
||||
public class TypedReturn implements TypedStatement {
|
||||
private TypedExpression ret;
|
||||
|
||||
public TypedReturn convertToTypedReturn(Map<String, Type> localVar, Map<String, TypedClass> classes, Return unTypedReturn) {
|
||||
ret = getKindOfExpression(localVar, classes, unTypedReturn.ret());
|
||||
public TypedReturn convertToTypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
|
||||
ret = getKindOfExpression(localVar, clas, unTypedReturn.ret());
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
//TODO: Implement typeCheck for Return
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
Return untyped = (Return) unTypedAST;
|
||||
TypedReturn typedReturn = new TypedReturn();
|
||||
typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, classes, untyped.ret()));
|
||||
typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret()));
|
||||
return typedReturn;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.maishai.typedast.typedclass;
|
||||
|
||||
import de.maishai.ast.UnaryOperator;
|
||||
import de.maishai.ast.records.Node;
|
||||
import de.maishai.ast.records.Unary;
|
||||
import de.maishai.typedast.MethodContext;
|
||||
import de.maishai.typedast.TypedExpression;
|
||||
@ -20,23 +21,23 @@ public class TypedUnary implements TypedExpression {
|
||||
private TypedExpression right;
|
||||
private Type type;
|
||||
|
||||
public TypedUnary convertToTypedUnary(Map<String, Type> localVar, Map<String, TypedClass> classes, Unary unTypedUnary) {
|
||||
public TypedUnary convertToTypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) {
|
||||
op = unTypedUnary.op();
|
||||
right = getKindOfExpression(localVar, classes, unTypedUnary.right());
|
||||
right = getKindOfExpression(localVar, clas, unTypedUnary.right());
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
|
||||
if(op == UnaryOperator.NOT){
|
||||
if(right.typeCheck(localVar, classes) != Type.BOOL){
|
||||
if(right.typeCheck(localVar, clas) != Type.BOOL){
|
||||
throw new RuntimeException("Not operator must be applied to boolean");
|
||||
}
|
||||
return Type.BOOL;
|
||||
}
|
||||
|
||||
if(op == UnaryOperator.SUB){
|
||||
if(right.typeCheck(localVar, classes) != Type.INT){
|
||||
if(right.typeCheck(localVar, clas) != Type.INT){
|
||||
throw new RuntimeException("Minus operator must be applied to int");
|
||||
}
|
||||
return Type.INT;
|
||||
@ -45,11 +46,11 @@ public class TypedUnary implements TypedExpression {
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, de.maishai.ast.records.Node unTypedAST) {
|
||||
Unary untyped = (Unary) unTypedAST;
|
||||
TypedUnary typedUnary = new TypedUnary();
|
||||
typedUnary.setOp(untyped.op());
|
||||
typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, classes, untyped.right()));
|
||||
typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, clas, untyped.right()));
|
||||
return typedUnary;
|
||||
}
|
||||
|
||||
|
@ -14,27 +14,27 @@ public class TypedWhile implements TypedStatement {
|
||||
private TypedExpression cond;
|
||||
private TypedBlock typedBlock;
|
||||
|
||||
public TypedWhile convertToTypedWhile(Map<String, Type> localVar, Map<String, TypedClass> classes, While unTypedWhile) {
|
||||
cond = getKindOfExpression(localVar, classes, unTypedWhile.cond());
|
||||
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedWhile.block());
|
||||
public TypedWhile convertToTypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
|
||||
cond = getKindOfExpression(localVar, clas, unTypedWhile.cond());
|
||||
typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedWhile.block());
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
|
||||
if(cond.typeCheck(localVar, classes) != Type.BOOL){
|
||||
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
|
||||
if(cond.typeCheck(localVar, clas) != Type.BOOL){
|
||||
throw new RuntimeException("While condition must be a boolean");
|
||||
}
|
||||
typedBlock.typeCheck(localVar, classes);
|
||||
typedBlock.typeCheck(localVar, clas);
|
||||
return Type.BOOL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
|
||||
public TypedNode convertToTypedAST(Map<String, Type> localVar, TypedClass clas, Node unTypedAST) {
|
||||
While untyped = (While) unTypedAST;
|
||||
TypedWhile typedWhile = new TypedWhile();
|
||||
typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond()));
|
||||
typedWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond()));
|
||||
TypedBlock typedBlock = new TypedBlock();
|
||||
typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block()));
|
||||
typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
|
||||
return typedWhile;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user