Change the parameter classes to one class in TypedNode

This commit is contained in:
ahmad 2024-05-09 19:59:40 +02:00
parent 735781a63c
commit cb4c637ce4
24 changed files with 180 additions and 171 deletions

View File

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

View File

@ -9,7 +9,7 @@ import java.util.Map;
public class TypedExpressionUtil { 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) { if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
typedBoolLiteral.setValue(boolLiteral.value()); typedBoolLiteral.setValue(boolLiteral.value());
@ -17,23 +17,23 @@ public class TypedExpressionUtil {
} }
else if (expression instanceof CharLiteral charLiteral) { else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral() TypedCharLiteral typedCharLiteral = new TypedCharLiteral()
.convertToCharLiteral(localVar, classes, charLiteral); .convertToCharLiteral(localVar, clas, charLiteral);
return typedCharLiteral; return typedCharLiteral;
} }
else if (expression instanceof IntLiteral intLiteral) { else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral() TypedIntLiteral typedIntLiteral = new TypedIntLiteral()
.convertToTypedIntLiteral(localVar, classes, intLiteral); .convertToTypedIntLiteral(localVar, clas, intLiteral);
return typedIntLiteral; return typedIntLiteral;
} }
else if (expression instanceof Binary binary) { else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary() TypedBinary typedBinary = new TypedBinary()
.convertToTypedBinary(localVar, classes, binary); .convertToTypedBinary(localVar, clas, binary);
typedBinary.typeCheck(localVar, classes); typedBinary.typeCheck(localVar, clas);
return typedBinary; return typedBinary;
} }
else if (expression instanceof FieldVarAccess fieldVarAccess) { else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess() TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess()
.convertToTypedFieldVarAccess(localVar, classes, fieldVarAccess); .convertToTypedFieldVarAccess(localVar, clas, fieldVarAccess);
return typedFieldVarAccess; return typedFieldVarAccess;
} }
else if (expression instanceof MethodCall methodCall) { else if (expression instanceof MethodCall methodCall) {
@ -44,7 +44,7 @@ public class TypedExpressionUtil {
typedFieldVarAccess.setField(methodCall.recipient().field()); typedFieldVarAccess.setField(methodCall.recipient().field());
typedMethodCall.setRecipient(typedFieldVarAccess); typedMethodCall.setRecipient(typedFieldVarAccess);
for (var arg : methodCall.args()) { for (var arg : methodCall.args()) {
typedMethodCall.getArgs().add(getKindOfExpression(localVar, classes, arg)); typedMethodCall.getArgs().add(getKindOfExpression(localVar, clas, arg));
} }
return typedMethodCall; return typedMethodCall;
} }
@ -52,14 +52,14 @@ public class TypedExpressionUtil {
TypedNew typedNew = new TypedNew(); TypedNew typedNew = new TypedNew();
typedNew.setType(newStmt.type()); typedNew.setType(newStmt.type());
for (var arg : newStmt.args()) { for (var arg : newStmt.args()) {
typedNew.getArgs().add(getKindOfExpression(localVar, classes, arg)); typedNew.getArgs().add(getKindOfExpression(localVar, clas, arg));
} }
return typedNew; return typedNew;
} }
else if (expression instanceof Unary unary) { else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary(); TypedUnary typedUnary = new TypedUnary();
typedUnary.setOp(unary.op()); typedUnary.setOp(unary.op());
typedUnary.setRight(getKindOfExpression(localVar, classes, unary.right())); typedUnary.setRight(getKindOfExpression(localVar, clas, unary.right()));
return typedUnary; return typedUnary;
} else { } else {
return null; return null;

View File

@ -20,19 +20,19 @@ public class TypedAssignment implements TypedStatement {
private TypedExpression value; private TypedExpression value;
private Type type; 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(); varName = untyped.location().id();
value = getKindOfExpression(localVar, classes, untyped.value()); value = getKindOfExpression(localVar, clas, untyped.value());
return this; return this;
} }
@Override @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)) { if (!localVar.containsKey(varName)) {
throw new RuntimeException("Variable not declared"); throw new RuntimeException("Variable not declared");
} }
Type typeLeft = localVar.get(varName); Type typeLeft = localVar.get(varName);
Type typeRight = value.typeCheck(localVar, classes); Type typeRight = value.typeCheck(localVar, clas);
if (typeLeft.equals(typeRight) ) { if (typeLeft.equals(typeRight) ) {
type = typeLeft; type = typeLeft;
@ -42,11 +42,11 @@ public class TypedAssignment implements TypedStatement {
} }
@Override @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; Assignment untyped = (Assignment) unTypedAST;
TypedAssignment typedAssignment = new TypedAssignment(); TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setVarName(untyped.location().id()); 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; return typedAssignment;
} }

View File

@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.Binary; import de.maishai.ast.records.Binary;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
@ -20,34 +21,34 @@ public class TypedBinary implements TypedExpression {
private TypedExpression right; private TypedExpression right;
private Type type; private Type type;
public TypedBinary convertToTypedBinary(Map<String, Type> localVar, Map<String, TypedClass> classes, Binary unTypedBinary) { public TypedBinary convertToTypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
left = getKindOfExpression(localVar, classes, unTypedBinary.left()); left = getKindOfExpression(localVar, clas, unTypedBinary.left());
right = getKindOfExpression(localVar, classes, unTypedBinary.right()); right = getKindOfExpression(localVar, clas, unTypedBinary.right());
op = unTypedBinary.op(); op = unTypedBinary.op();
return this; return this;
} }
@Override @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 (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
if (left.typeCheck(localVar, classes) == Type.INT && if (left.typeCheck(localVar, clas) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) { right.typeCheck(localVar, clas) == Type.INT) {
type = Type.INT; type = Type.INT;
return Type.INT; return Type.INT;
} else { } else {
throw new RuntimeException("Type mismatch in " + op); 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) { } 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 && if (left.typeCheck(localVar, clas) == Type.INT &&
right.typeCheck(localVar, classes) == Type.INT) { right.typeCheck(localVar, clas) == Type.INT) {
type = Type.BOOL; type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} else { } else {
throw new RuntimeException("Type mismatch in " + op); throw new RuntimeException("Type mismatch in " + op);
} }
} else if (op == Operator.AND || op == Operator.OR) { } else if (op == Operator.AND || op == Operator.OR) {
if (left.typeCheck(localVar, classes) == Type.BOOL && if (left.typeCheck(localVar, clas) == Type.BOOL &&
right.typeCheck(localVar, classes) == Type.BOOL) { right.typeCheck(localVar, clas) == Type.BOOL) {
type = Type.BOOL; type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} else { } else {
@ -60,13 +61,13 @@ public class TypedBinary implements TypedExpression {
} }
@Override @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; Binary binary = (Binary) unTypedAST;
TypedBinary typedBinary = new TypedBinary(); 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.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; return typedBinary;
} }

View File

@ -19,7 +19,7 @@ public class TypedBlock implements TypedNode {
private List<TypedStatement> stmts; 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) { if (unTypedBlock == null) {
return null; return null;
@ -34,7 +34,7 @@ public class TypedBlock implements TypedNode {
} else { } else {
for (Declaration var : unTypedBlock.localVariables()) { for (Declaration var : unTypedBlock.localVariables()) {
TypedLocalVariable typedVar = new TypedLocalVariable() TypedLocalVariable typedVar = new TypedLocalVariable()
.convertToTypedLocalVariable(localVar, classes, var); .convertToTypedLocalVariable(localVar, clas, var);
typedBlock.getVars().add(typedVar); typedBlock.getVars().add(typedVar);
} }
} }
@ -46,41 +46,41 @@ public class TypedBlock implements TypedNode {
for (var stmt : unTypedBlock.stmts()) { for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) { if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment() TypedAssignment typedAssignment = new TypedAssignment()
.convertToTypedAssignment(localVar, classes, assignment); .convertToTypedAssignment(localVar, clas, assignment);
typedAssignment.typeCheck(localVar, classes); typedAssignment.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedAssignment); typedBlock.getStmts().add(typedAssignment);
continue; continue;
} }
if (stmt instanceof For forStmt) { if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, classes, forStmt); TypedFor typedFor = new TypedFor().convertToTypedFor(localVar, clas, forStmt);
typedFor.typeCheck(localVar, classes); typedFor.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedFor); typedBlock.getStmts().add(typedFor);
continue; continue;
} }
if (stmt instanceof IfElse ifElse) { if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse() TypedIfElse typedIfElse = new TypedIfElse()
.convertToTypedIfElse(localVar, classes, ifElse); .convertToTypedIfElse(localVar, clas, ifElse);
typedIfElse.typeCheck(localVar, classes); typedIfElse.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedIfElse); typedBlock.getStmts().add(typedIfElse);
continue; continue;
} }
if (stmt instanceof While whileStmt) { if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile() TypedWhile typedWhile = new TypedWhile()
.convertToTypedWhile(localVar, classes, whileStmt); .convertToTypedWhile(localVar, clas, whileStmt);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedWhile); typedBlock.getStmts().add(typedWhile);
continue; continue;
} }
if (stmt instanceof DoWhile doWhile) { if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile() TypedDoWhile typedDoWhile = new TypedDoWhile()
.convertToTypedDoWhile(localVar, classes, doWhile); .convertToTypedDoWhile(localVar, clas, doWhile);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedDoWhile); typedBlock.getStmts().add(typedDoWhile);
continue; continue;
} }
if (stmt instanceof Return returnStmt) { if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn() TypedReturn typedReturn = new TypedReturn()
.convertToTypedReturn(localVar, classes, returnStmt); .convertToTypedReturn(localVar, clas, returnStmt);
//TODO: check the type //TODO: check the type
typedBlock.getStmts().add(typedReturn); typedBlock.getStmts().add(typedReturn);
continue; continue;
@ -93,7 +93,7 @@ public class TypedBlock implements TypedNode {
if (stmt instanceof Break) { if (stmt instanceof Break) {
TypedBreak typedBreak = new TypedBreak(); TypedBreak typedBreak = new TypedBreak();
typedBreak.typeCheck(localVar, classes); typedBreak.typeCheck(localVar, clas);
typedBlock.getStmts().add(typedBreak); typedBlock.getStmts().add(typedBreak);
continue; continue;
} }
@ -109,19 +109,19 @@ public class TypedBlock implements TypedNode {
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (vars == null) { if (vars == null) {
vars = List.of(); vars = List.of();
} else { } else {
for (TypedLocalVariable var : vars) { for (TypedLocalVariable var : vars) {
var.typeCheck(localVar, classes); var.typeCheck(localVar, clas);
} }
} }
if (stmts == null) { if (stmts == null) {
stmts = List.of(); stmts = List.of();
} else { } else {
for (TypedStatement stmt : stmts) { for (TypedStatement stmt : stmts) {
stmt.typeCheck(localVar, classes); stmt.typeCheck(localVar, clas);
} }
} }
@ -129,7 +129,7 @@ public class TypedBlock implements TypedNode {
} }
@Override @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; Block untyped = (Block) unTypedAST;
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedBlock.setStmts(new ArrayList<>()); typedBlock.setStmts(new ArrayList<>());

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.BoolLiteral; import de.maishai.ast.records.BoolLiteral;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
@ -16,19 +17,19 @@ public class TypedBoolLiteral implements TypedExpression {
private Boolean value; private Boolean value;
private Type type; 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(); value = unTypedBoolLiteral.value();
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
type = Type.BOOL; type = Type.BOOL;
return Type.BOOL; return Type.BOOL;
} }
@Override @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; BoolLiteral untyped = (BoolLiteral) unTypedAST;
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral();
typedBoolLiteral.setValue(untyped.value()); typedBoolLiteral.setValue(untyped.value());

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Break; import de.maishai.ast.records.Break;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.TypedStatement; import de.maishai.typedast.TypedStatement;
@ -17,13 +18,13 @@ public class TypedBreak implements TypedStatement {
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
type = Type.VOID; type = Type.VOID;
return type; return type;
} }
@Override @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; Break untyped = (Break) unTypedAST;
if(untyped == null) { if(untyped == null) {
throw new RuntimeException("Break not found"); throw new RuntimeException("Break not found");

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.CharLiteral; import de.maishai.ast.records.CharLiteral;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
@ -12,19 +13,19 @@ import java.util.Map;
@Data @Data
public class TypedCharLiteral implements TypedExpression { public class TypedCharLiteral implements TypedExpression {
private char value; 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(); value = unTypedCharLiteral.value();
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
return Type.CHAR; return Type.CHAR;
} }
private Type type;
@Override @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; CharLiteral untyped = (CharLiteral) unTypedAST;
TypedCharLiteral typedCharLiteral = new TypedCharLiteral(); TypedCharLiteral typedCharLiteral = new TypedCharLiteral();
typedCharLiteral.setValue(untyped.value()); typedCharLiteral.setValue(untyped.value());

View File

@ -1,10 +1,8 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class; 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.CodeGenUtils;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -29,33 +27,33 @@ public class TypedClass implements TypedNode {
private List<TypedConstructor> typedConstructors; private List<TypedConstructor> typedConstructors;
@Override @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)) { if (clas == null) {
throw new RuntimeException("Class " + className + " already exists"); throw new RuntimeException("Class not found");
} }
classes.put(className, this);
for (TypedField field : typedFields) { for (TypedField field : typedFields) {
field.typeCheck(localVar, classes); field.typeCheck(localVar, clas);
} }
for(TypedConstructor constructor : typedConstructors) { for (TypedConstructor constructor : typedConstructors) {
constructor.typeCheck(localVar, classes); constructor.typeCheck(localVar, clas);
} }
for (TypedMethod typedMethod : typedMethods) { for (TypedMethod typedMethod : typedMethods) {
typedMethod.typeCheck(localVar, classes); typedMethod.typeCheck(localVar, clas);
} }
return Type.REFERENCE(className); return Type.REFERENCE(className);
} }
public TypedNode startConversion(de.maishai.ast.records.Class c) { public TypedNode startConversion(de.maishai.ast.records.Class c) {
Map<String, Type> local = new HashMap<>(); Map<String, Type> local = new HashMap<>();
Map<String, TypedClass> classMap = new HashMap<>(); TypedClass classMap = new TypedClass();
return convertToTypedAST(local, classMap, c); return convertToTypedAST(local, classMap, c);
} }
@Override @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(); TypedClass typedClass = new TypedClass();
Class c = (Class) unTypedAST; Class c = (Class) unTypedAST;
typedClass.setClassName(c.classname()); typedClass.setClassName(c.classname());
@ -64,19 +62,19 @@ public class TypedClass implements TypedNode {
typedClass.setTypedConstructors(new ArrayList<>()); typedClass.setTypedConstructors(new ArrayList<>());
for (Declaration field : c.fieldDeclarations()) { for (Declaration field : c.fieldDeclarations()) {
TypedField typedField = new TypedField(); 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()) { for (Method method : c.methods()) {
TypedMethod typedMethod = new TypedMethod(); 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()) { for (Constructor constructor : c.constructors()) {
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, classes, constructor)); typedClass.getTypedConstructors().add(typedConstructor.constructorToTypedConstructor(localVar, clas, constructor));
} }
return typedClass; return typedClass;
@ -111,7 +109,7 @@ public class TypedClass implements TypedNode {
c.typedFields = List.of(f1, f2); c.typedFields = List.of(f1, f2);
//Constructors //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); c.typedConstructors = List.of(constructor);
//Methods //Methods

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Constructor; import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Parameter; import de.maishai.ast.records.Parameter;
import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
@ -26,7 +27,7 @@ public class TypedConstructor implements TypedNode {
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; 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 typedConstructor = new TypedConstructor();
typedConstructor.setName(unTypedConstructor.className()); typedConstructor.setName(unTypedConstructor.className());
typedConstructor.setTypedParameters(new ArrayList<>()); typedConstructor.setTypedParameters(new ArrayList<>());
@ -44,27 +45,27 @@ public class TypedConstructor implements TypedNode {
} }
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes ,unTypedConstructor.block())); typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas ,unTypedConstructor.block()));
return typedConstructor; return typedConstructor;
} }
@Override @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)) { if (localVar.containsKey(name)) {
throw new RuntimeException("constructor already declared"); throw new RuntimeException("constructor already declared");
} }
localVar.put(name, Type.VOID); localVar.put(name, Type.VOID);
if (typedParameters != null) { if (typedParameters != null) {
for (TypedParameter param : typedParameters) { for (TypedParameter param : typedParameters) {
param.typeCheck(localVar, classes); param.typeCheck(localVar, clas);
} }
} }
typedBlock.typeCheck(localVar, classes); typedBlock.typeCheck(localVar, clas);
return Type.VOID; return Type.VOID;
} }
@Override @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; Constructor untyped = (Constructor) unTypedAST;
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setName(untyped.className()); typedConstructor.setName(untyped.className());
@ -83,7 +84,7 @@ public class TypedConstructor implements TypedNode {
} }
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); typedConstructor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
return typedConstructor; return typedConstructor;
} }

View File

@ -16,29 +16,29 @@ public class TypedDoWhile implements TypedStatement {
public TypedDoWhile() { public TypedDoWhile() {
} }
public TypedDoWhile convertToTypedDoWhile(Map<String, Type> localVar, Map<String, TypedClass> classes, DoWhile unTypedDoWhile) { public TypedDoWhile convertToTypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedDoWhile.block()); typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedDoWhile.block());
cond = getKindOfExpression(localVar, classes, unTypedDoWhile.cond()); cond = getKindOfExpression(localVar, clas, unTypedDoWhile.cond());
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (cond.typeCheck(localVar, classes) != Type.BOOL) { if (cond.typeCheck(localVar, clas) != Type.BOOL) {
throw new RuntimeException("Condition must be boolean"); throw new RuntimeException("Condition must be boolean");
} }
typedBlock.typeCheck(localVar, classes); typedBlock.typeCheck(localVar, clas);
return Type.VOID; return Type.VOID;
} }
@Override @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; DoWhile untyped = (DoWhile) unTypedAST;
TypedDoWhile typedDoWhile = new TypedDoWhile(); TypedDoWhile typedDoWhile = new TypedDoWhile();
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); typedDoWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, classes, untyped.cond())); typedDoWhile.setCond((TypedExpression) cond.convertToTypedAST(localVar, clas, untyped.cond()));
return typedDoWhile; return typedDoWhile;
} }

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Node;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -18,7 +19,7 @@ public class TypedField implements TypedNode {
private String varName; private String varName;
private Type type; 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())){ if(localVar.containsKey(declaration.name())){
throw new RuntimeException("Variable " + declaration.name() + " already declared"); throw new RuntimeException("Variable " + declaration.name() + " already declared");
} }
@ -29,7 +30,7 @@ public class TypedField implements TypedNode {
} }
@Override @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)) { if (localVar.containsKey(varName)) {
throw new RuntimeException("Variable " + varName + " already declared"); throw new RuntimeException("Variable " + varName + " already declared");
} }
@ -38,7 +39,7 @@ public class TypedField implements TypedNode {
} }
@Override @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; Declaration untypedDeclaration = (Declaration) unTypedAST;
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();

View File

@ -19,16 +19,16 @@ public class TypedFieldVarAccess implements TypedExpression {
private String name; private String name;
private Type type; 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(); field = unTypedFieldVarAccess.field();
recursiveOwnerChain = getKindOfExpression(localVar, classes, unTypedFieldVarAccess.recursiveOwnerChain()); recursiveOwnerChain = getKindOfExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id(); name = unTypedFieldVarAccess.id();
type = localVar.get(unTypedFieldVarAccess.id()); type = localVar.get(unTypedFieldVarAccess.id());
return this; return this;
} }
@Override @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); Type type = localVar.get(name);
if (type == null) { if (type == null) {
@ -42,11 +42,11 @@ public class TypedFieldVarAccess implements TypedExpression {
return type; return type;
} }
@Override @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; FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST;
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(); TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess();
typedFieldVarAccess.setField(untypedFieldVarAccess.field()); 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()); typedFieldVarAccess.setName(untypedFieldVarAccess.id());
return typedFieldVarAccess; return typedFieldVarAccess;

View File

@ -20,36 +20,36 @@ public class TypedFor implements TypedStatement {
private TypedAssignment inc; private TypedAssignment inc;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedFor convertToTypedFor(Map<String, Type> localVar, Map<String, TypedClass> classes, For unTypedFor) { public TypedFor convertToTypedFor(Map<String, Type> localVar,TypedClass clas, For unTypedFor) {
assign = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.assign()); assign = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.assign());
cond = getKindOfExpression(localVar, classes, unTypedFor.cond()); cond = getKindOfExpression(localVar, clas, unTypedFor.cond());
inc = new TypedAssignment().convertToTypedAssignment(localVar, classes, unTypedFor.inc()); inc = new TypedAssignment().convertToTypedAssignment(localVar, clas, unTypedFor.inc());
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedFor.block()); typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedFor.block());
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
assign.typeCheck(localVar, classes); assign.typeCheck(localVar, clas);
if (!cond.typeCheck(localVar, classes).equals(Type.BOOL)) { if (!cond.typeCheck(localVar, clas).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, clas);
return typedBlock.typeCheck(localVar, classes); return typedBlock.typeCheck(localVar, clas);
} }
@Override @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; For untyped = (For) unTypedAST;
TypedFor typedFor = new TypedFor(); TypedFor typedFor = new TypedFor();
TypedAssignment assign = new TypedAssignment(); TypedAssignment assign = new TypedAssignment();
typedFor.setAssign(assign.convertToTypedAssignment(localVar,classes,untyped.assign())); typedFor.setAssign(assign.convertToTypedAssignment(localVar,clas,untyped.assign()));
TypedAssignment lnc = new TypedAssignment(); TypedAssignment lnc = new TypedAssignment();
typedFor.setInc(lnc.convertToTypedAssignment(localVar,classes,untyped.inc())); typedFor.setInc(lnc.convertToTypedAssignment(localVar,clas,untyped.inc()));
typedFor.setCond(null); typedFor.setCond(null);
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); typedFor.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
return typedFor; return typedFor;
} }

View File

@ -17,36 +17,36 @@ public class TypedIfElse implements TypedStatement {
private TypedBlock ifTypedBlock; private TypedBlock ifTypedBlock;
private TypedBlock elseTypedBlock; private TypedBlock elseTypedBlock;
public TypedIfElse convertToTypedIfElse(Map<String, Type> localVar, Map<String, TypedClass> classes, IfElse unTypedIfElse) { public TypedIfElse convertToTypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.ifBlock()); ifTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedIfElse.elseBlock()); elseTypedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedIfElse.elseBlock());
typedCon = getKindOfExpression(localVar, classes, unTypedIfElse.cond()); typedCon = getKindOfExpression(localVar, clas, unTypedIfElse.cond());
return this; return this;
} }
@Override @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"); 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"); throw new RuntimeException("If block must be of type void");
} }
return Type.VOID; return Type.VOID;
} }
@Override @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; IfElse ifElse = (IfElse) unTypedAST;
TypedIfElse typedIfElse = new TypedIfElse(); 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 ifTypedBlock = new TypedBlock();
TypedBlock elseTypedBlock = new TypedBlock(); TypedBlock elseTypedBlock = new TypedBlock();
typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, classes, ifElse.ifBlock())); typedIfElse.setIfTypedBlock(ifTypedBlock.blockToTypedBlock(localVar, clas, ifElse.ifBlock()));
typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, classes, ifElse.elseBlock())); typedIfElse.setElseTypedBlock(elseTypedBlock.blockToTypedBlock(localVar, clas, ifElse.elseBlock()));
return typedIfElse; return typedIfElse;
} }

View File

@ -18,7 +18,7 @@ import java.util.Map;
public class TypedIntLiteral implements TypedExpression { public class TypedIntLiteral implements TypedExpression {
private Integer value; 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(); value = unTypedIntLiteral.value();
return this; return this;
} }
@ -28,12 +28,12 @@ public class TypedIntLiteral implements TypedExpression {
this.value = value; this.value = value;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
return Type.INT; return Type.INT;
} }
@Override @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; IntLiteral untyped = (IntLiteral) unTypedAST;
return new TypedIntLiteral(untyped.value()); return new TypedIntLiteral(untyped.value());
} }

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Declaration; import de.maishai.ast.records.Declaration;
import de.maishai.ast.records.Node;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
@ -16,7 +17,7 @@ import java.util.Map;
public final class TypedLocalVariable implements TypedNode { public final class TypedLocalVariable implements TypedNode {
private String name; private String name;
private Type type; 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())){ if(localVar.containsKey(declaration.name())){
throw new RuntimeException("Variable " + declaration.name() + " already declared"); throw new RuntimeException("Variable " + declaration.name() + " already declared");
} }
@ -27,7 +28,7 @@ public final class TypedLocalVariable implements TypedNode {
} }
@Override @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); Type type = localVar.get(name);
if(type == this.type) { if(type == this.type) {
return type; return type;
@ -36,7 +37,7 @@ public final class TypedLocalVariable implements TypedNode {
} }
@Override @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; Declaration untypedLocalVariable = (Declaration) unTypedAST;
TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); TypedLocalVariable typedLocalVariable = new TypedLocalVariable();

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Method; import de.maishai.ast.records.Method;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Parameter; import de.maishai.ast.records.Parameter;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
@ -19,10 +20,11 @@ public class TypedMethod implements TypedNode {
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; 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 typedMethod = new TypedMethod();
typedMethod.setName(unTypedMethod.methodName()); typedMethod.setName(unTypedMethod.methodName());
typedMethod.setTypedParameters(new ArrayList<>()); typedMethod.setTypedParameters(new ArrayList<>());
typedMethod.setReturnType(unTypedMethod.type());
if(unTypedMethod.params().isEmpty()){ if(unTypedMethod.params().isEmpty()){
typedMethod.setTypedParameters(List.of()); typedMethod.setTypedParameters(List.of());
@ -31,27 +33,27 @@ public class TypedMethod implements TypedNode {
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(parameter.name()); typedParameter.setParaName(parameter.name());
typedParameter.setType(parameter.type()); typedParameter.setType(parameter.type());
typedParameters.add(typedParameter); typedMethod.getTypedParameters().add(typedParameter);
} }
} }
TypedBlock block = new TypedBlock(); TypedBlock block = new TypedBlock();
typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, classes, unTypedMethod.block())); typedMethod.setTypedBlock(block.blockToTypedBlock(localVar, clas, unTypedMethod.block()));
return typedMethod; return typedMethod;
} }
@Override @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)) { if(localVar.containsKey(name)) {
throw new RuntimeException("Method " + name + " already exists"); throw new RuntimeException("Method " + name + " already exists");
} }
localVar.put(name, returnType); localVar.put(name, returnType);
for(TypedParameter typedParameter : typedParameters) { for(TypedParameter typedParameter : typedParameters) {
typedParameter.typeCheck(localVar, classes); typedParameter.typeCheck(localVar, clas);
} }
return typedBlock.typeCheck(localVar, classes); return typedBlock.typeCheck(localVar, clas);
} }
@Override @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; Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
@ -67,7 +69,7 @@ public class TypedMethod implements TypedNode {
} }
TypedBlock typedBlock = new TypedBlock(); TypedBlock typedBlock = new TypedBlock();
typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes,untypedMethod.block())); typedMethod.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas,untypedMethod.block()));
return typedMethod; return typedMethod;
} }

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.MethodCall; import de.maishai.ast.records.MethodCall;
import de.maishai.ast.records.Node;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -20,13 +21,13 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
} }
@Override @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 //TODO: Implement typeCheck for MethodCall
return null; return null;
} }
@Override @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 //TODO: Implement this
return null; return null;
} }

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.New; import de.maishai.ast.records.New;
import de.maishai.ast.records.Node;
import de.maishai.typedast.*; import de.maishai.typedast.*;
import lombok.Data; import lombok.Data;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@ -18,20 +19,17 @@ public class TypedNew implements TypedExpression, TypedStatement {
return null; return null;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
// new A(1, 2, 3) if(clas == null){
if (!classes.containsKey(this.type.getReference())) { throw new RuntimeException("Class not found");
throw new RuntimeException("Class " + this.type.getReference() + " not found");
} }
TypedClass klasse = classes.get(this.type.getReference()); if (clas.getTypedFields().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
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, clas);
if (!type.equals(klasse.getTypedFields().get(i).getType())) { if (!type.equals(clas.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());
} }
} }
@ -39,7 +37,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
} }
@Override @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; New untyped = (New) unTypedAST;
TypedNew typedNew = new TypedNew(); TypedNew typedNew = new TypedNew();

View File

@ -1,5 +1,6 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Parameter; import de.maishai.ast.records.Parameter;
import de.maishai.typedast.TypedNode; import de.maishai.typedast.TypedNode;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
@ -23,7 +24,7 @@ public class TypedParameter implements TypedNode {
} }
@Override @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)) { if (localVar.containsKey(paraName)) {
throw new RuntimeException("Parameter " + paraName + " already exists"); throw new RuntimeException("Parameter " + paraName + " already exists");
} }
@ -32,7 +33,7 @@ public class TypedParameter implements TypedNode {
} }
@Override @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; Parameter untyped = (Parameter) unTypedAST;
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setParaName(untyped.name()); typedParameter.setParaName(untyped.name());

View File

@ -15,22 +15,22 @@ import static de.maishai.typedast.Util.TypedExpressionUtil.getKindOfExpression;
public class TypedReturn implements TypedStatement { public class TypedReturn implements TypedStatement {
private TypedExpression ret; private TypedExpression ret;
public TypedReturn convertToTypedReturn(Map<String, Type> localVar, Map<String, TypedClass> classes, Return unTypedReturn) { public TypedReturn convertToTypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
ret = getKindOfExpression(localVar, classes, unTypedReturn.ret()); ret = getKindOfExpression(localVar, clas, unTypedReturn.ret());
return this; return this;
} }
@Override @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 //TODO: Implement typeCheck for Return
return null; return null;
} }
@Override @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; Return untyped = (Return) unTypedAST;
TypedReturn typedReturn = new TypedReturn(); TypedReturn typedReturn = new TypedReturn();
typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, classes, untyped.ret())); typedReturn.setRet((TypedExpression) ret.convertToTypedAST(localVar, clas, untyped.ret()));
return typedReturn; return typedReturn;
} }

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.UnaryOperator; import de.maishai.ast.UnaryOperator;
import de.maishai.ast.records.Node;
import de.maishai.ast.records.Unary; import de.maishai.ast.records.Unary;
import de.maishai.typedast.MethodContext; import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression; import de.maishai.typedast.TypedExpression;
@ -20,23 +21,23 @@ public class TypedUnary implements TypedExpression {
private TypedExpression right; private TypedExpression right;
private Type type; 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(); op = unTypedUnary.op();
right = getKindOfExpression(localVar, classes, unTypedUnary.right()); right = getKindOfExpression(localVar, clas, unTypedUnary.right());
return this; return this;
} }
@Override @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(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"); throw new RuntimeException("Not operator must be applied to boolean");
} }
return Type.BOOL; return Type.BOOL;
} }
if(op == UnaryOperator.SUB){ 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"); throw new RuntimeException("Minus operator must be applied to int");
} }
return Type.INT; return Type.INT;
@ -45,11 +46,11 @@ public class TypedUnary implements TypedExpression {
} }
@Override @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; Unary untyped = (Unary) unTypedAST;
TypedUnary typedUnary = new TypedUnary(); TypedUnary typedUnary = new TypedUnary();
typedUnary.setOp(untyped.op()); typedUnary.setOp(untyped.op());
typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, classes, untyped.right())); typedUnary.setRight((TypedExpression) right.convertToTypedAST(localVar, clas, untyped.right()));
return typedUnary; return typedUnary;
} }

View File

@ -14,27 +14,27 @@ public class TypedWhile implements TypedStatement {
private TypedExpression cond; private TypedExpression cond;
private TypedBlock typedBlock; private TypedBlock typedBlock;
public TypedWhile convertToTypedWhile(Map<String, Type> localVar, Map<String, TypedClass> classes, While unTypedWhile) { public TypedWhile convertToTypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
cond = getKindOfExpression(localVar, classes, unTypedWhile.cond()); cond = getKindOfExpression(localVar, clas, unTypedWhile.cond());
typedBlock = new TypedBlock().blockToTypedBlock(localVar, classes, unTypedWhile.block()); typedBlock = new TypedBlock().blockToTypedBlock(localVar, clas, unTypedWhile.block());
return this; return this;
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if(cond.typeCheck(localVar, classes) != Type.BOOL){ if(cond.typeCheck(localVar, clas) != Type.BOOL){
throw new RuntimeException("While condition must be a boolean"); throw new RuntimeException("While condition must be a boolean");
} }
typedBlock.typeCheck(localVar, classes); typedBlock.typeCheck(localVar, clas);
return Type.BOOL; return Type.BOOL;
} }
@Override @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; While untyped = (While) unTypedAST;
TypedWhile typedWhile = new TypedWhile(); 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(); TypedBlock typedBlock = new TypedBlock();
typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, classes, untyped.block())); typedWhile.setTypedBlock(typedBlock.blockToTypedBlock(localVar, clas, untyped.block()));
return typedWhile; return typedWhile;
} }