Removed the parameter localvariable in typeCheck

This commit is contained in:
ahmad 2024-05-12 13:07:39 +02:00
parent 827b5551f4
commit 048aff5996
24 changed files with 206 additions and 209 deletions

View File

@ -9,45 +9,45 @@ import java.util.Map;
public class TypedExpressionHelp {
public static TypedExpression convertExpression(Map<String, Type> localVar, TypedClass clas, Expression expression) {
public static TypedExpression convertExpression( TypedClass clas, Expression expression) {
if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(localVar, clas, boolLiteral);
typedBoolLiteral.typeCheck(localVar, clas);
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( clas, boolLiteral);
typedBoolLiteral.typeCheck( clas);
return typedBoolLiteral;
}
else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral(localVar, clas, charLiteral);
typedCharLiteral.typeCheck(localVar, clas);
TypedCharLiteral typedCharLiteral = new TypedCharLiteral( clas, charLiteral);
typedCharLiteral.typeCheck( clas);
return typedCharLiteral;
}
else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral(localVar, clas, intLiteral);
typedIntLiteral.typeCheck(localVar, clas);
TypedIntLiteral typedIntLiteral = new TypedIntLiteral( clas, intLiteral);
typedIntLiteral.typeCheck( clas);
return typedIntLiteral;
}
else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary(localVar, clas, binary);
typedBinary.typeCheck(localVar, clas);
TypedBinary typedBinary = new TypedBinary( clas, binary);
typedBinary.typeCheck( clas);
return typedBinary;
}
else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(localVar, clas, fieldVarAccess);
typedFieldVarAccess.typeCheck(localVar, clas);
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( clas, fieldVarAccess);
typedFieldVarAccess.typeCheck( clas);
return typedFieldVarAccess;
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
typedMethodCall.typeCheck(localVar, clas);
TypedMethodCall typedMethodCall = new TypedMethodCall( clas, methodCall);
typedMethodCall.typeCheck( clas);
return typedMethodCall;
}
else if (expression instanceof New newStmt) {
TypedNew typedNew = new TypedNew(localVar, clas, newStmt);
typedNew.typeCheck(localVar, clas);
TypedNew typedNew = new TypedNew( clas, newStmt);
typedNew.typeCheck( clas);
return typedNew;
}
else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary(localVar, clas, unary);
typedUnary.typeCheck(localVar, clas);
TypedUnary typedUnary = new TypedUnary( clas, unary);
typedUnary.typeCheck( clas);
return typedUnary;
} else {
return null;

View File

@ -1,10 +1,7 @@
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, TypedClass clas);
Type typeCheck(TypedClass clas);
}

View File

@ -21,18 +21,18 @@ public class TypedAssignment implements TypedStatement {
// private TypedExpression recursiveOwnerChain;
private Type type;
public TypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
convertToTypedAssignment(localVar, clas, untyped);
public TypedAssignment(TypedClass clas, Assignment untyped) {
convertToTypedAssignment(clas, untyped);
}
public void convertToTypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) {
public void convertToTypedAssignment(TypedClass clas, Assignment untyped) {
varName = untyped.location().id();
value = convertExpression(localVar, clas, untyped.value());
// recursiveOwnerChain = convertExpression(localVar, clas, untyped.location().recursiveOwnerChain());
value = convertExpression(clas, untyped.value());
// recursiveOwnerChain = convertExpression( clas, untyped.location().recursiveOwnerChain());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
Type typeLeft = null;
if (clas.isThereField(varName)) {
@ -54,7 +54,7 @@ public class TypedAssignment implements TypedStatement {
}
}
Type typeRight = value.typeCheck(localVar, clas);
Type typeRight = value.typeCheck(clas);
if (typeLeft.equals(typeRight)) {
type = typeLeft;

View File

@ -21,20 +21,20 @@ public class TypedBinary implements TypedExpression {
private TypedExpression right;
private Type type;
public TypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
convertToTypedBinary(localVar, clas, unTypedBinary);
public TypedBinary(TypedClass clas, Binary unTypedBinary) {
convertToTypedBinary(clas, unTypedBinary);
}
public void convertToTypedBinary(Map<String, Type> localVar, TypedClass clas, Binary unTypedBinary) {
left = convertExpression(localVar, clas, unTypedBinary.left());
right = convertExpression(localVar, clas, unTypedBinary.right());
public void convertToTypedBinary(TypedClass clas, Binary unTypedBinary) {
left = convertExpression(clas, unTypedBinary.left());
right = convertExpression(clas, unTypedBinary.right());
op = unTypedBinary.op();
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
Type leftType = left.typeCheck(localVar, clas);
Type rightType = right.typeCheck(localVar, clas);
public Type typeCheck(TypedClass clas) {
Type leftType = left.typeCheck(clas);
Type rightType = right.typeCheck(clas);
if (op == Operator.ADD || op == Operator.SUB || op == Operator.MUL) {
if (leftType == Type.INT && rightType == Type.INT) {

View File

@ -23,8 +23,8 @@ public class TypedBlock implements TypedNode {
private Type type;
public TypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
convertToTypedBlock(localVar, clas, unTypedBlock);
public TypedBlock(TypedClass clas, Block unTypedBlock) {
convertToTypedBlock(clas, unTypedBlock);
}
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
@ -32,56 +32,56 @@ public class TypedBlock implements TypedNode {
this.stmts = stmts;
}
public void convertToTypedBlock(Map<String, Type> localVar, TypedClass clas, Block unTypedBlock) {
public void convertToTypedBlock(TypedClass clas, Block unTypedBlock) {
if (unTypedBlock == null) {
return;
}
for (Declaration var : unTypedBlock.localVariables()) {
vars.add(new TypedLocalVariable(localVar, clas, var));
vars.add(new TypedLocalVariable(clas, var));
}
for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment(localVar, clas, assignment);
typedAssignment.typeCheck(localVar, clas);
TypedAssignment typedAssignment = new TypedAssignment(clas, assignment);
typedAssignment.typeCheck(clas);
stmts.add(typedAssignment);
continue;
}
if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor(localVar, clas, forStmt);
typedFor.typeCheck(localVar, clas);
TypedFor typedFor = new TypedFor(clas, forStmt);
typedFor.typeCheck(clas);
stmts.add(typedFor);
continue;
}
if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse(localVar, clas, ifElse);
typedIfElse.typeCheck(localVar, clas);
TypedIfElse typedIfElse = new TypedIfElse(clas, ifElse);
typedIfElse.typeCheck(clas);
stmts.add(typedIfElse);
continue;
}
if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile(localVar, clas, whileStmt);
typedWhile.typeCheck(localVar, clas);
TypedWhile typedWhile = new TypedWhile(clas, whileStmt);
typedWhile.typeCheck(clas);
stmts.add(typedWhile);
continue;
}
if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile(localVar, clas, doWhile);
typedDoWhile.typeCheck(localVar, clas);
TypedDoWhile typedDoWhile = new TypedDoWhile(clas, doWhile);
typedDoWhile.typeCheck(clas);
stmts.add(typedDoWhile);
continue;
}
if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn(localVar, clas, returnStmt);
typedReturn.typeCheck(localVar, clas);
TypedReturn typedReturn = new TypedReturn(clas, returnStmt);
typedReturn.typeCheck(clas);
stmts.add(typedReturn);
continue;
}
if (stmt instanceof New newStmt) {
TypedNew typedNew = new TypedNew(localVar, clas, newStmt);
typedNew.typeCheck(localVar, clas);
TypedNew typedNew = new TypedNew(clas, newStmt);
typedNew.typeCheck(clas);
stmts.add(typedNew);
continue;
}
@ -92,20 +92,20 @@ public class TypedBlock implements TypedNode {
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall);
typedMethodCall.typeCheck(localVar, clas);
TypedMethodCall typedMethodCall = new TypedMethodCall(clas, methodCall);
typedMethodCall.typeCheck(clas);
stmts.add(typedMethodCall);
}
}
this.typeCheck(localVar, clas);
this.typeCheck(clas);
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
for (TypedStatement stmt : stmts) {
stmt.typeCheck(localVar, clas);
stmt.typeCheck(clas);
}
//TODO: Type von Return zurückgeben

View File

@ -21,17 +21,17 @@ public class TypedBoolLiteral implements TypedExpression {
private Type type;
public TypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(localVar, clas, unTypedBoolLiteral);
public TypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(clas, unTypedBoolLiteral);
}
public void convertToTypedBoolLiteral(Map<String, Type> localVar, TypedClass clas, BoolLiteral unTypedBoolLiteral) {
public void convertToTypedBoolLiteral(TypedClass clas, BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value();
type = Type.BOOL;
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
return type;
}

View File

@ -15,11 +15,12 @@ import java.util.Map;
public class TypedBreak implements TypedStatement {
private Type type = Type.VOID;
public TypedBreak convertToTypedBreak(Map<String, Type> localVar, TypedClass clas, Break unTypedBreak) {
public TypedBreak convertToTypedBreak(TypedClass clas, Break unTypedBreak) {
return this;
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
return type;
}

View File

@ -16,17 +16,17 @@ public class TypedCharLiteral implements TypedExpression {
private char value;
private Type type;
public TypedCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(localVar, clas, unTypedCharLiteral);
public TypedCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(clas, unTypedCharLiteral);
}
public void convertToCharLiteral(Map<String, Type> localVar, TypedClass clas, CharLiteral unTypedCharLiteral) {
public void convertToCharLiteral(TypedClass clas, CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value();
type = Type.CHAR;
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
return type;
}

View File

@ -29,8 +29,8 @@ public class TypedClass implements TypedNode {
private TypedConstructor currentConstructor;
private Type type;
public TypedClass(Map<String, Type> localVar, Class c) {
convertToTypedClass(localVar, c);
public TypedClass(Class c) {
convertToTypedClass(c);
}
public void enterCurrentMethod(TypedMethod method) {
@ -81,28 +81,28 @@ public class TypedClass implements TypedNode {
return false;
}
public void convertToTypedClass(Map<String, Type> localVar, Class c) {
public void convertToTypedClass(Class c) {
className = c.classname();
type = Type.REFERENCE(className);
for (Declaration declaration : c.fieldDeclarations()) {
typedDeclarations.add(new TypedDeclaration(localVar, this, declaration));
typedDeclarations.add(new TypedDeclaration(this, declaration));
}
// Am Anfang werden die Konstruktoren und Methoden in die Listen eingefügt
// Methoden können verwendet werden, bevor deren Blöcke ausgeführt werden
for (Constructor constructor : c.constructors()) {
typedConstructors.add(new TypedConstructor(localVar, this, constructor));
typedConstructors.add(new TypedConstructor(this, constructor));
}
for (Method method : c.methods()) {
typedMethods.add(new TypedMethod(localVar, this, method));
typedMethods.add(new TypedMethod(this, method));
}
// Hier werden die Blöcke der Konstruktoren und Methoden ausgeführt
int i = 0;
for (Constructor constructor : c.constructors()) {
enterCurrentConstructor(typedConstructors.get(i));
typedConstructors.get(i).convertToBlock(localVar, this, constructor);
typedConstructors.get(i).convertToBlock(this, constructor);
exitCurrentConstructor();
i++;
}
@ -110,7 +110,7 @@ public class TypedClass implements TypedNode {
int j = 0;
for (Method method : c.methods()) {
enterCurrentMethod(typedMethods.get(j));
typedMethods.get(j).convertToTypedBlock(localVar, this, method);
typedMethods.get(j).convertToTypedBlock(this, method);
exitCurrentMethod();
j++;
}
@ -118,14 +118,14 @@ public class TypedClass implements TypedNode {
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
return type;
}
public TypedNode startConversion(Class c) {
Map<String, Type> local = new HashMap<>();
return new TypedClass(local, c);
return new TypedClass(c);
}
public boolean isParameterWitNameInMethod(String parameterName) {

View File

@ -34,11 +34,11 @@ public class TypedConstructor implements TypedNode {
this.typedBlock = typedBlock;
}
public TypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
convertToTypedConstructor(localVar, clas, unTypedConstructor);
public TypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
convertToTypedConstructor(clas, unTypedConstructor);
}
public boolean isLocalVariablePresent(String localVarName) {
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
@ -49,25 +49,26 @@ public class TypedConstructor implements TypedNode {
public boolean isLocalVariableInConstructor(String localVarName) {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
}
public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}
public void convertToTypedConstructor(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
public void convertToTypedConstructor(TypedClass clas, Constructor unTypedConstructor) {
name = unTypedConstructor.className();
for (Parameter param : unTypedConstructor.params()) {
typedParameters.add(new TypedParameter(localVar, clas, param));
typedParameters.add(new TypedParameter(clas, param));
}
type = Type.VOID;
}
public void convertToBlock(Map<String, Type> localVar, TypedClass clas, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(localVar, clas, unTypedConstructor.block());
public void convertToBlock(TypedClass clas, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(clas, unTypedConstructor.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
//TODO: check if return is there
return type;
}

View File

@ -22,21 +22,18 @@ public final class TypedDeclaration implements TypedNode {
private String name;
private Type type;
public TypedDeclaration(Map<String, Type> localVar, TypedClass clas, Declaration declaration) {
convertToTypedDeclaration(localVar, clas, declaration);
public TypedDeclaration(TypedClass clas, Declaration declaration) {
convertToTypedDeclaration(clas, declaration);
}
public void convertToTypedDeclaration(Map<String, Type> localVar, TypedClass clas, Declaration declaration) {
if (localVar.containsKey(declaration.name())) {
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
public void convertToTypedDeclaration(TypedClass clas, Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(localVar, clas);
typeCheck(clas);
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (clas.isThereField(name)) {
throw new RuntimeException("Field " + name + " already declared");
}

View File

@ -15,21 +15,21 @@ public class TypedDoWhile implements TypedStatement {
private TypedExpression cond;
private Type type;
public TypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
convertToTypedDoWhile(localVar, clas, unTypedDoWhile);
public TypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
convertToTypedDoWhile(clas, unTypedDoWhile);
}
public void convertToTypedDoWhile(Map<String, Type> localVar, TypedClass clas, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock(localVar, clas, unTypedDoWhile.block());
cond = convertExpression(localVar, clas, unTypedDoWhile.cond());
public void convertToTypedDoWhile(TypedClass clas, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock(clas, unTypedDoWhile.block());
cond = convertExpression(clas, unTypedDoWhile.cond());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (cond.typeCheck(localVar, clas) != Type.BOOL) {
public Type typeCheck(TypedClass clas) {
if (cond.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("Condition must be boolean");
}
typedBlock.typeCheck(localVar, clas);
typedBlock.typeCheck(clas);
this.type = Type.VOID;
return Type.VOID;
}

View File

@ -20,18 +20,18 @@ public class TypedFieldVarAccess implements TypedExpression {
private String name;
private Type type;
public TypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(localVar, clas, unTypedFieldVarAccess);
public TypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(clas, unTypedFieldVarAccess);
}
public void convertToTypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
public void convertToTypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
field = unTypedFieldVarAccess.field();
recursiveOwnerChain = convertExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain());
recursiveOwnerChain = convertExpression(clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id();
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (field) {
if (clas.isThereField(name)) {
type = clas.getFieldType(name);
@ -52,11 +52,11 @@ public class TypedFieldVarAccess implements TypedExpression {
type = clas.getParameterTypeInCurrentMethod(name);
return clas.getParameterTypeInCurrentMethod(name);
}
}
}/*
if (localVar.containsKey(name)) {
type = localVar.get(name);
return localVar.get(name);
}
}*/
}
throw new RuntimeException("Variable " + name + " not declared ");

View File

@ -22,25 +22,25 @@ public class TypedFor implements TypedStatement {
//TODO: type of block in for loop
private Type type;
public TypedFor(Map<String, Type> localVar, TypedClass clas, For unTypedFor) {
convertToTypedFor(localVar, clas, unTypedFor);
public TypedFor(TypedClass clas, For unTypedFor) {
convertToTypedFor(clas, unTypedFor);
}
public void convertToTypedFor(Map<String, Type> localVar, TypedClass clas, For unTypedFor) {
assign = new TypedAssignment(localVar, clas, unTypedFor.assign());
cond = convertExpression(localVar, clas, unTypedFor.cond());
inc = new TypedAssignment(localVar, clas, unTypedFor.inc());
typedBlock = new TypedBlock(localVar, clas, unTypedFor.block());
public void convertToTypedFor(TypedClass clas, For unTypedFor) {
assign = new TypedAssignment(clas, unTypedFor.assign());
cond = convertExpression(clas, unTypedFor.cond());
inc = new TypedAssignment(clas, unTypedFor.inc());
typedBlock = new TypedBlock(clas, unTypedFor.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
assign.typeCheck(localVar, clas);
if (!cond.typeCheck(localVar, clas).equals(Type.BOOL)) {
public Type typeCheck(TypedClass clas) {
assign.typeCheck(clas);
if (!cond.typeCheck(clas).equals(Type.BOOL)) {
throw new RuntimeException("Condition must be a boolean");
}
inc.typeCheck(localVar, clas);
type = typedBlock.typeCheck(localVar, clas);
inc.typeCheck(clas);
type = typedBlock.typeCheck(clas);
return type;
}

View File

@ -21,29 +21,29 @@ public class TypedIfElse implements TypedStatement {
//TODO: add Type
private Type type;
public TypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
convertToTypedIfElse(localVar, clas, unTypedIfElse);
public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
convertToTypedIfElse(clas, unTypedIfElse);
}
public void convertToTypedIfElse(Map<String, Type> localVar, TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock(localVar, clas, unTypedIfElse.elseBlock());
typedCon = convertExpression(localVar, clas, unTypedIfElse.cond());
public void convertToTypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock(clas, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock(clas, unTypedIfElse.elseBlock());
typedCon = convertExpression(clas, unTypedIfElse.cond());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (typedCon.typeCheck(localVar, clas) != Type.BOOL) {
if (typedCon.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("If condition must be a boolean");
}
if (ifTypedBlock.typeCheck(localVar, clas) != Type.VOID) {
if (ifTypedBlock.typeCheck(clas) != Type.VOID) {
throw new RuntimeException("If block must be of type void");
}
//TODO: catch all cases of return when one method has a return type
if (ifTypedBlock.typeCheck(localVar, clas) == elseTypedBlock.typeCheck(localVar, clas)) {
type = ifTypedBlock.typeCheck(localVar, clas);
if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) {
type = ifTypedBlock.typeCheck(clas);
}
return type;

View File

@ -20,17 +20,17 @@ public class TypedIntLiteral implements TypedExpression {
private Type type;
public TypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(localVar, clas, unTypedIntLiteral);
public TypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(clas, unTypedIntLiteral);
}
public void convertToTypedIntLiteral(Map<String, Type> localVar, TypedClass clas, IntLiteral unTypedIntLiteral) {
public void convertToTypedIntLiteral(TypedClass clas, IntLiteral unTypedIntLiteral) {
value = unTypedIntLiteral.value();
type = Type.INT;
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
return type;
}

View File

@ -23,34 +23,31 @@ public final class TypedLocalVariable implements TypedNode {
private String name;
private Type type;
public TypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration) {
convertToTypedLocalVariable(localVar, clas, declaration);
public TypedLocalVariable(TypedClass clas, Declaration declaration) {
convertToTypedLocalVariable(clas, declaration);
}
public void convertToTypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration) {
if (localVar.containsKey(declaration.name())) {
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
public void convertToTypedLocalVariable(TypedClass clas, Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(localVar, clas);
typeCheck(clas);
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()){
if(clas.getCurrentMethod().isLocalVariableInMethod(name)){
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentMethod().getLocalVariables().add(this);
return type;
}
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()){
if(clas.getCurrentConstructor().isLocalVariableInConstructor(name)){
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentConstructor().getLocalVariables().add(this);
return type;
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
if (clas.getCurrentMethod().isLocalVariableInMethod(name)) {
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentMethod().getLocalVariables().add(this);
return type;
}
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
if (clas.getCurrentConstructor().isLocalVariableInConstructor(name)) {
throw new RuntimeException("Variable " + name + " already declared");
}
clas.getCurrentConstructor().getLocalVariables().add(this);
return type;
}
throw new RuntimeException("not found method or constructor in class");

View File

@ -23,16 +23,16 @@ public class TypedMethod implements TypedNode {
private TypedBlock typedBlock;
private List<TypedLocalVariable> localVariables = new ArrayList<>();
public TypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
convertToTypedMethod(localVar, clas, unTypedMethod);
public TypedMethod(TypedClass clas, Method unTypedMethod) {
convertToTypedMethod(clas, unTypedMethod);
}
public void convertToTypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
public void convertToTypedMethod(TypedClass clas, Method unTypedMethod) {
name = unTypedMethod.methodName();
returnType = unTypedMethod.type();
for (var parameter : unTypedMethod.params()) {
typedParameters.add(new TypedParameter(localVar, clas, parameter));
typedParameters.add(new TypedParameter(clas, parameter));
}
clas.getTypedMethods().stream().filter(method -> method.equals(this)).findFirst().ifPresentOrElse(
@ -41,15 +41,17 @@ public class TypedMethod implements TypedNode {
}, () -> {
});
localVar.put(name, returnType);
//localVar.put(name, returnType);
}
public void convertToTypedBlock(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) {
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block());
public void convertToTypedBlock(TypedClass clas, Method unTypedMethod) {
typedBlock = new TypedBlock(clas, unTypedMethod.block());
}
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
public boolean isParameterPresent(String parameterName) {
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
@ -57,20 +59,21 @@ public class TypedMethod implements TypedNode {
public boolean isLocalVariableInMethod(String localVarName) {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
}
public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (localVar.containsKey(name)) {
public Type typeCheck(TypedClass clas) {
/* if (localVar.containsKey(name)) {
throw new RuntimeException("Method " + name + " already exists");
}
localVar.put(name, returnType);
for (TypedParameter typedParameter : typedParameters) {
typedParameter.typeCheck(localVar, clas);
}
typedParameter.typeCheck( clas);
}*/
return returnType;
}

View File

@ -19,19 +19,19 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
private List<TypedExpression> args;
private Type type;
public TypedMethodCall(Map<String, Type> localVar, TypedClass clas, MethodCall unTypedMethodCall) {
convertToTypedMethodCall(localVar, clas, unTypedMethodCall);
public TypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
convertToTypedMethodCall(clas, unTypedMethodCall);
}
public void convertToTypedMethodCall(Map<String, Type> localVar, TypedClass clas, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(localVar, clas, unTypedMethodCall.recipient());
public void convertToTypedMethodCall(TypedClass clas, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(clas, unTypedMethodCall.recipient());
for (Expression arg : unTypedMethodCall.args()) {
args.add(convertExpression(localVar, clas, arg));
args.add(convertExpression(clas, arg));
}
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
//TODO: implement this
return null;
}

View File

@ -17,28 +17,29 @@ public class TypedNew implements TypedExpression, TypedStatement {
private Type type;
private List<TypedExpression> args = new ArrayList<>();
public TypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
convertToTypedNew(localVar, clas, unTypedNew);
public TypedNew(TypedClass clas, New unTypedNew) {
convertToTypedNew(clas, unTypedNew);
}
public void convertToTypedNew(Map<String, Type> localVar, TypedClass clas, New unTypedNew) {
public void convertToTypedNew(TypedClass clas, New unTypedNew) {
type = unTypedNew.type();
for (Expression arg : unTypedNew.args()) {
args.add(convertExpression(localVar, clas, arg));
args.add(convertExpression(clas, arg));
}
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
for(var constructor : clas.getTypedConstructors()){
if(constructor.getTypedParameters().size() == args.size()){
public Type typeCheck(TypedClass clas) {
for (var constructor : clas.getTypedConstructors()) {
if (constructor.getTypedParameters().size() == args.size()) {
boolean valid = true;
for(int i = 0; i < args.size(); i++){
if(!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(localVar, clas))){
for (int i = 0; i < args.size(); i++) {
if (!constructor.getTypedParameters().get(i).getType().equals(args.get(i).typeCheck(clas))) {
valid = false;
break;
}
}
if(valid){
if (valid) {
return Type.REFERENCE(clas.getClassName());
}
}

View File

@ -14,11 +14,11 @@ public class TypedParameter implements TypedNode {
private String paraName;
private Type type;
public TypedParameter(Map<String, Type> localVar, TypedClass clas, Parameter unTypedParameter) {
convertToTypedParameter(localVar, clas, unTypedParameter);
public TypedParameter(TypedClass clas, Parameter unTypedParameter) {
convertToTypedParameter(clas, unTypedParameter);
}
public void convertToTypedParameter(Map<String, Type> localVar, TypedClass clas, Parameter unTypedParameter) {
public void convertToTypedParameter(TypedClass clas, Parameter unTypedParameter) {
paraName = unTypedParameter.name();
type = unTypedParameter.type();
}
@ -34,7 +34,7 @@ public class TypedParameter implements TypedNode {
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent()) {
if (clas.isParameterWitNameInMethod(paraName)) {

View File

@ -18,17 +18,17 @@ public class TypedReturn implements TypedStatement {
private TypedExpression ret;
private Type type;
public TypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
convertToTypedReturn(localVar, clas, unTypedReturn);
public TypedReturn(TypedClass clas, Return unTypedReturn) {
convertToTypedReturn(clas, unTypedReturn);
}
public void convertToTypedReturn(Map<String, Type> localVar, TypedClass clas, Return unTypedReturn) {
ret = convertExpression(localVar, clas, unTypedReturn.ret());
public void convertToTypedReturn(TypedClass clas, Return unTypedReturn) {
ret = convertExpression(clas, unTypedReturn.ret());
type = ret.getType();
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent()) {
if (clas.getCurrentMethod().getReturnType().getKind() != this.type.getKind()) {
StringBuilder exp = new StringBuilder();

View File

@ -19,27 +19,27 @@ public class TypedUnary implements TypedExpression {
private TypedExpression right;
private Type type;
public TypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) {
convertToTypedUnary(localVar, clas, unTypedUnary);
public TypedUnary(TypedClass clas, Unary unTypedUnary) {
convertToTypedUnary(clas, unTypedUnary);
}
public void convertToTypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) {
public void convertToTypedUnary(TypedClass clas, Unary unTypedUnary) {
op = unTypedUnary.op();
right = convertExpression(localVar, clas, unTypedUnary.right());
right = convertExpression(clas, unTypedUnary.right());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
public Type typeCheck(TypedClass clas) {
if (op == UnaryOperator.NOT) {
if (right.typeCheck(localVar, clas) != Type.BOOL) {
if (right.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("Not operator must be applied to boolean");
}
return Type.BOOL;
}
if (op == UnaryOperator.SUB) {
if (right.typeCheck(localVar, clas) != Type.INT) {
if (right.typeCheck(clas) != Type.INT) {
throw new RuntimeException("Minus operator must be applied to int");
}
return Type.INT;

View File

@ -15,21 +15,21 @@ public class TypedWhile implements TypedStatement {
private TypedBlock typedBlock;
private Type type;
public TypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
convertToTypedWhile(localVar, clas, unTypedWhile);
public TypedWhile(TypedClass clas, While unTypedWhile) {
convertToTypedWhile(clas, unTypedWhile);
}
public void convertToTypedWhile(Map<String, Type> localVar, TypedClass clas, While unTypedWhile) {
cond = convertExpression(localVar, clas, unTypedWhile.cond());
typedBlock = new TypedBlock(localVar, clas, unTypedWhile.block());
public void convertToTypedWhile(TypedClass clas, While unTypedWhile) {
cond = convertExpression(clas, unTypedWhile.cond());
typedBlock = new TypedBlock(clas, unTypedWhile.block());
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (cond.typeCheck(localVar, clas) != Type.BOOL) {
public Type typeCheck(TypedClass clas) {
if (cond.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("While condition must be a boolean");
}
type = typedBlock.typeCheck(localVar, clas);
type = typedBlock.typeCheck(clas);
return type;
}