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 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) { if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(localVar, clas, boolLiteral); TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( clas, boolLiteral);
typedBoolLiteral.typeCheck(localVar, clas); typedBoolLiteral.typeCheck( clas);
return typedBoolLiteral; return typedBoolLiteral;
} }
else if (expression instanceof CharLiteral charLiteral) { else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral(localVar, clas, charLiteral); TypedCharLiteral typedCharLiteral = new TypedCharLiteral( clas, charLiteral);
typedCharLiteral.typeCheck(localVar, clas); typedCharLiteral.typeCheck( clas);
return typedCharLiteral; return typedCharLiteral;
} }
else if (expression instanceof IntLiteral intLiteral) { else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral(localVar, clas, intLiteral); TypedIntLiteral typedIntLiteral = new TypedIntLiteral( clas, intLiteral);
typedIntLiteral.typeCheck(localVar, clas); typedIntLiteral.typeCheck( clas);
return typedIntLiteral; return typedIntLiteral;
} }
else if (expression instanceof Binary binary) { else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary(localVar, clas, binary); TypedBinary typedBinary = new TypedBinary( clas, binary);
typedBinary.typeCheck(localVar, clas); typedBinary.typeCheck( clas);
return typedBinary; return typedBinary;
} }
else if (expression instanceof FieldVarAccess fieldVarAccess) { else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(localVar, clas, fieldVarAccess); TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( clas, fieldVarAccess);
typedFieldVarAccess.typeCheck(localVar, clas); typedFieldVarAccess.typeCheck( clas);
return typedFieldVarAccess; return typedFieldVarAccess;
} }
else if (expression instanceof MethodCall methodCall) { else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(localVar, clas, methodCall); TypedMethodCall typedMethodCall = new TypedMethodCall( clas, methodCall);
typedMethodCall.typeCheck(localVar, clas); typedMethodCall.typeCheck( clas);
return typedMethodCall; return typedMethodCall;
} }
else if (expression instanceof New newStmt) { else if (expression instanceof New newStmt) {
TypedNew typedNew = new TypedNew(localVar, clas, newStmt); TypedNew typedNew = new TypedNew( clas, newStmt);
typedNew.typeCheck(localVar, clas); typedNew.typeCheck( clas);
return typedNew; return typedNew;
} }
else if (expression instanceof Unary unary) { else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary(localVar, clas, unary); TypedUnary typedUnary = new TypedUnary( clas, unary);
typedUnary.typeCheck(localVar, clas); typedUnary.typeCheck( clas);
return typedUnary; return typedUnary;
} else { } else {
return null; return null;

View File

@ -1,10 +1,7 @@
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;
public interface TypedNode { 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 TypedExpression recursiveOwnerChain;
private Type type; private Type type;
public TypedAssignment(Map<String, Type> localVar, TypedClass clas, Assignment untyped) { public TypedAssignment(TypedClass clas, Assignment untyped) {
convertToTypedAssignment(localVar, clas, 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(); varName = untyped.location().id();
value = convertExpression(localVar, clas, untyped.value()); value = convertExpression(clas, untyped.value());
// recursiveOwnerChain = convertExpression(localVar, clas, untyped.location().recursiveOwnerChain()); // recursiveOwnerChain = convertExpression( clas, untyped.location().recursiveOwnerChain());
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(TypedClass clas) {
Type typeLeft = null; Type typeLeft = null;
if (clas.isThereField(varName)) { 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)) { if (typeLeft.equals(typeRight)) {
type = typeLeft; type = typeLeft;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,18 +20,18 @@ public class TypedFieldVarAccess implements TypedExpression {
private String name; private String name;
private Type type; private Type type;
public TypedFieldVarAccess(Map<String, Type> localVar, TypedClass clas, FieldVarAccess unTypedFieldVarAccess) { public TypedFieldVarAccess(TypedClass clas, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(localVar, clas, 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(); field = unTypedFieldVarAccess.field();
recursiveOwnerChain = convertExpression(localVar, clas, unTypedFieldVarAccess.recursiveOwnerChain()); recursiveOwnerChain = convertExpression(clas, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id(); name = unTypedFieldVarAccess.id();
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(TypedClass clas) {
if (field) { if (field) {
if (clas.isThereField(name)) { if (clas.isThereField(name)) {
type = clas.getFieldType(name); type = clas.getFieldType(name);
@ -52,11 +52,11 @@ public class TypedFieldVarAccess implements TypedExpression {
type = clas.getParameterTypeInCurrentMethod(name); type = clas.getParameterTypeInCurrentMethod(name);
return clas.getParameterTypeInCurrentMethod(name); return clas.getParameterTypeInCurrentMethod(name);
} }
} }/*
if (localVar.containsKey(name)) { if (localVar.containsKey(name)) {
type = localVar.get(name); type = localVar.get(name);
return localVar.get(name); return localVar.get(name);
} }*/
} }
throw new RuntimeException("Variable " + name + " not declared "); 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 //TODO: type of block in for loop
private Type type; private Type type;
public TypedFor(Map<String, Type> localVar, TypedClass clas, For unTypedFor) { public TypedFor(TypedClass clas, For unTypedFor) {
convertToTypedFor(localVar, clas, unTypedFor); convertToTypedFor(clas, unTypedFor);
} }
public void convertToTypedFor(Map<String, Type> localVar, TypedClass clas, For unTypedFor) { public void convertToTypedFor(TypedClass clas, For unTypedFor) {
assign = new TypedAssignment(localVar, clas, unTypedFor.assign()); assign = new TypedAssignment(clas, unTypedFor.assign());
cond = convertExpression(localVar, clas, unTypedFor.cond()); cond = convertExpression(clas, unTypedFor.cond());
inc = new TypedAssignment(localVar, clas, unTypedFor.inc()); inc = new TypedAssignment(clas, unTypedFor.inc());
typedBlock = new TypedBlock(localVar, clas, unTypedFor.block()); typedBlock = new TypedBlock(clas, unTypedFor.block());
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(TypedClass clas) {
assign.typeCheck(localVar, clas); assign.typeCheck(clas);
if (!cond.typeCheck(localVar, clas).equals(Type.BOOL)) { if (!cond.typeCheck(clas).equals(Type.BOOL)) {
throw new RuntimeException("Condition must be a boolean"); throw new RuntimeException("Condition must be a boolean");
} }
inc.typeCheck(localVar, clas); inc.typeCheck(clas);
type = typedBlock.typeCheck(localVar, clas); type = typedBlock.typeCheck(clas);
return type; return type;
} }

View File

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

View File

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

View File

@ -23,34 +23,31 @@ public final class TypedLocalVariable implements TypedNode {
private String name; private String name;
private Type type; private Type type;
public TypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration) { public TypedLocalVariable(TypedClass clas, Declaration declaration) {
convertToTypedLocalVariable(localVar, clas, declaration); convertToTypedLocalVariable(clas, declaration);
} }
public void convertToTypedLocalVariable(Map<String, Type> localVar, TypedClass clas, Declaration declaration) { public void convertToTypedLocalVariable(TypedClass clas, Declaration declaration) {
if (localVar.containsKey(declaration.name())) {
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
name = declaration.name(); name = declaration.name();
type = declaration.type(); type = declaration.type();
typeCheck(localVar, clas); typeCheck(clas);
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(TypedClass clas) {
if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()){ if (clas.isCurrentMethodPresent() && !clas.isCurrentConstructorPresent()) {
if(clas.getCurrentMethod().isLocalVariableInMethod(name)){ if (clas.getCurrentMethod().isLocalVariableInMethod(name)) {
throw new RuntimeException("Variable " + name + " already declared"); throw new RuntimeException("Variable " + name + " already declared");
} }
clas.getCurrentMethod().getLocalVariables().add(this); clas.getCurrentMethod().getLocalVariables().add(this);
return type; return type;
} }
if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()){ if (!clas.isCurrentMethodPresent() && clas.isCurrentConstructorPresent()) {
if(clas.getCurrentConstructor().isLocalVariableInConstructor(name)){ if (clas.getCurrentConstructor().isLocalVariableInConstructor(name)) {
throw new RuntimeException("Variable " + name + " already declared"); throw new RuntimeException("Variable " + name + " already declared");
} }
clas.getCurrentConstructor().getLocalVariables().add(this); clas.getCurrentConstructor().getLocalVariables().add(this);
return type; return type;
} }
throw new RuntimeException("not found method or constructor in class"); 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 TypedBlock typedBlock;
private List<TypedLocalVariable> localVariables = new ArrayList<>(); private List<TypedLocalVariable> localVariables = new ArrayList<>();
public TypedMethod(Map<String, Type> localVar, TypedClass clas, Method unTypedMethod) { public TypedMethod(TypedClass clas, Method unTypedMethod) {
convertToTypedMethod(localVar, clas, 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(); name = unTypedMethod.methodName();
returnType = unTypedMethod.type(); returnType = unTypedMethod.type();
for (var parameter : unTypedMethod.params()) { 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( 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) { public void convertToTypedBlock(TypedClass clas, Method unTypedMethod) {
typedBlock = new TypedBlock(localVar, clas, unTypedMethod.block()); typedBlock = new TypedBlock(clas, unTypedMethod.block());
} }
public boolean isLocalVariablePresent(String localVarName) { public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName)); return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
} }
public boolean isParameterPresent(String parameterName) { public boolean isParameterPresent(String parameterName) {
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
} }
@ -57,20 +59,21 @@ public class TypedMethod implements TypedNode {
public boolean isLocalVariableInMethod(String localVarName) { public boolean isLocalVariableInMethod(String localVarName) {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
} }
public Type getLocalVariableType(String localVarName) { public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(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, clas); typedParameter.typeCheck( clas);
} }*/
return returnType; return returnType;
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -19,27 +19,27 @@ public class TypedUnary implements TypedExpression {
private TypedExpression right; private TypedExpression right;
private Type type; private Type type;
public TypedUnary(Map<String, Type> localVar, TypedClass clas, Unary unTypedUnary) { public TypedUnary(TypedClass clas, Unary unTypedUnary) {
convertToTypedUnary(localVar, clas, 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(); op = unTypedUnary.op();
right = convertExpression(localVar, clas, unTypedUnary.right()); right = convertExpression(clas, unTypedUnary.right());
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(TypedClass clas) {
if (op == UnaryOperator.NOT) { 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"); 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, clas) != Type.INT) { if (right.typeCheck(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;

View File

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