refactoring

This commit is contained in:
Krauß, Josefine 2024-07-04 11:57:49 +02:00
parent 920fa2fa48
commit 0533ddbc5d
11 changed files with 6 additions and 38 deletions

View File

@ -18,9 +18,9 @@ import java.util.Objects;
public class FieldDecl extends AbstractType implements Node {
public String type; // from parser
public String identifier;// from parser
public IExpression expression; // value of the field
public String type;
public String identifier;
public IExpression expression;
public FieldDecl(String type, String identifier, IExpression expression){
this.type = type;
@ -44,8 +44,6 @@ public class FieldDecl extends AbstractType implements Node {
setTypeCheckResult(result);
return result;
//write field table
}
public void codeGen(ClassWriter cw) {

View File

@ -35,7 +35,6 @@ public class MethodDecl implements Node {
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException {
// jede methode als block statement aufrufen und jede neue locale varibale in localvars schreiben
List<Parameter> parametersList = parameters.parameterList;
for(Parameter parameter : parametersList){
localVars.put(parameter.identifier, parameter.type);

View File

@ -68,7 +68,6 @@ public class RefType extends AbstractType implements Node {
// type check each method
for (MethodDecl methodDecl : methodDecls) {
// methodDecl.classThatContainsMethod = this.name;
methodDecl.typeCheck(methodContext, typeContext);
}

View File

@ -5,14 +5,9 @@ import TypeCheck.TypeCheckResult;
import org.objectweb.asm.MethodVisitor;
public interface IDatatype {
// typeCheck method
TypeCheckResult typeCheck() throws TypeCheckException;
// visit method for code generation
void codeGen(MethodVisitor mv) throws Exception;
TypeCheckResult getTypeCheckResult();
}
//TODO: Check if we need to differentiate between primitive types and reference types --> for example in "=="

View File

@ -58,9 +58,6 @@ public class BinaryExpression extends AbstractType implements IExpression{
result.type = "int";
}
break;
//case "&" ist für logisches und auf bit level
//case "|" ist für logisches oder auf bit level
}
setTypeCheckResult(result);

View File

@ -10,11 +10,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
public interface IExpression extends Node {
// typeCheck method
//TypeCheckResult typeCheck() throws Exception;
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException;
// visit method for code generation
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception;
TypeCheckResult getTypeCheckResult();

View File

@ -87,7 +87,6 @@ public class Program implements Node {
typeContext.put(oneClass.name, classVars);
// build method context
HashMap<String, HashMap<String, ParameterList>> identifierAndMethod = new HashMap<>();
for (MethodDecl methodDecl : oneClass.methodDecls){
if(methodDecl.returnType == null) continue;
@ -98,8 +97,8 @@ public class Program implements Node {
methodContext.put(oneClass.name, identifierAndMethod);
}
int mainCounter = 0;
// check if main exists
int mainCounter = 0;
for(RefType oneClass : classes){
if(oneClass.hasMain)
mainCounter++;

View File

@ -17,13 +17,10 @@ import java.util.List;
import java.util.Objects;
public class BlockStatement extends AbstractType implements IStatement {
//We will need a parameter which holds the symbol table
HashMap<String, String> localVars;
public String returnType; // "not" --> not void
public String returnType;
public List<IStatement> statements;
public String thisClass;
// do we need expression, statementexpression
public BlockStatement(List<IStatement> statements, String returnType) {
this.statements = statements;

View File

@ -14,8 +14,6 @@ import java.util.Objects;
public class IfStatement extends AbstractType implements IStatement{
public IExpression condition;
//Do we need a block statement here?
IStatement ifStatement;
public String thisClass;

View File

@ -34,7 +34,6 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
String classToSearchMethodIn = thisClass;
//method is called on something that is not this ???
if (this.receiver != null) {
if (!receiver.thisExpression) {
classToSearchMethodIn = localVars.get(receiver.identifier);
@ -51,11 +50,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
receiver.instVarExpression.thisClass = this.thisClass;
String typeOfSubreceiver = receiver.instVarExpression.typeCheck(methodContext, typeContext, localVars).type;
String lastField = receiver.instVarExpression.fieldName;
currentType = typeOfSubreceiver;
//currentType = typeContext.get(receiver.instVarExpression.getTypeCheckResult().type).get(mostLeftField);
} else {
currentType = classToSearchMethodIn;
}
@ -63,16 +58,11 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
currentType = thisClass;
}
//if classToSearchMethodIn does not contain method, throw exception. go through list and check each
for (int i = 0; i < receivingMethods.size(); i++) {
currentType = (String) methodContext.get(currentType).get(receivingMethods.get(i).methodName).keySet().toArray()[0];
if (currentType == null)
throw new TypeCheckException("The method " + methodName + " was not found in " + classToSearchMethodIn + ".");
receivingMethods.get(i).thisClass = this.thisClass;
// currentType = return type
// ThisClass = class von methode
receivingMethods.get(i).checkParameters(methodContext, typeContext, localVars);
}
currentType = (String) methodContext.get(currentType).get(methodName).keySet().toArray()[0];

View File

@ -21,7 +21,7 @@ public class NewStatementExpression extends AbstractType implements IExpression,
public String thisClass;
private String className;
private List<IExpression> arguments; //These need to have a TypeCheckResult
private List<IExpression> arguments;
public NewStatementExpression(String className, List<IExpression> arguments) {
this.className = className;