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 class FieldDecl extends AbstractType implements Node {
public String type; // from parser public String type;
public String identifier;// from parser public String identifier;
public IExpression expression; // value of the field public IExpression expression;
public FieldDecl(String type, String identifier, IExpression expression){ public FieldDecl(String type, String identifier, IExpression expression){
this.type = type; this.type = type;
@ -44,8 +44,6 @@ public class FieldDecl extends AbstractType implements Node {
setTypeCheckResult(result); setTypeCheckResult(result);
return result; return result;
//write field table
} }
public void codeGen(ClassWriter cw) { 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 { 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; List<Parameter> parametersList = parameters.parameterList;
for(Parameter parameter : parametersList){ for(Parameter parameter : parametersList){
localVars.put(parameter.identifier, parameter.type); localVars.put(parameter.identifier, parameter.type);

View File

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

View File

@ -5,14 +5,9 @@ import TypeCheck.TypeCheckResult;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
public interface IDatatype { public interface IDatatype {
// typeCheck method
TypeCheckResult typeCheck() throws TypeCheckException; TypeCheckResult typeCheck() throws TypeCheckException;
// visit method for code generation
void codeGen(MethodVisitor mv) throws Exception; void codeGen(MethodVisitor mv) throws Exception;
TypeCheckResult getTypeCheckResult(); 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"; result.type = "int";
} }
break; break;
//case "&" ist für logisches und auf bit level
//case "|" ist für logisches oder auf bit level
} }
setTypeCheckResult(result); setTypeCheckResult(result);

View File

@ -10,11 +10,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
public interface IExpression extends Node { 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; 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; 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(); TypeCheckResult getTypeCheckResult();

View File

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

View File

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

View File

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

View File

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