Compare commits

..

No commits in common. "5fa6664850abb734c7fb4b64fc6e9dacea902a99" and "d6fb82580830654e3ee5fbc848bbd50df40ce86c" have entirely different histories.

8 changed files with 44 additions and 56 deletions

View File

@ -27,15 +27,7 @@ public class FieldDecl extends AbstractType implements Node {
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
List<String> typesList= new ArrayList<>(); TypeCheckHelper.typeExists(this.type, typeContext.keySet().stream().toList());
for (HashMap<String, String> innerMap : typeContext.values()) {
for (String value : innerMap.values()) {
typesList.add(value);
}
}
TypeCheckHelper.typeExists(this.type, typesList);
result.type = this.type; result.type = this.type;
setTypeCheckResult(result); setTypeCheckResult(result);

View File

@ -24,12 +24,12 @@ public class BinaryExpression extends AbstractType implements IExpression{
} }
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { public TypeCheckResult typeCheck() throws Exception {
TypeCheckHelper helper = new TypeCheckHelper(); TypeCheckHelper helper = new TypeCheckHelper();
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
TypeCheckResult leftType = left.typeCheck(methodContext, typeContext, localVars); TypeCheckResult leftType = left.typeCheck();
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); TypeCheckResult rightType = right.typeCheck();
switch (operator) { switch (operator) {
@ -66,6 +66,12 @@ public class BinaryExpression extends AbstractType implements IExpression{
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception { public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
// Label for the jump instruction // Label for the jump instruction

View File

@ -26,11 +26,8 @@ public class InstVarExpression implements IExpression{
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
//todo /////////////////// //todo
String varType = typeContext.get(classRef.name).get(fieldName); return null;
TypeCheckResult result = new TypeCheckResult();
result.type = varType;
return result;
} }
@Override @Override

View File

@ -6,10 +6,6 @@ import org.objectweb.asm.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
public class LocalVarIdentifier implements IExpression{ public class LocalVarIdentifier implements IExpression{
@ -23,7 +19,7 @@ public class LocalVarIdentifier implements IExpression{
} }
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { public TypeCheckResult typeCheck() throws Exception {
return null; return null;
} }

View File

@ -19,7 +19,7 @@ public class UnaryExpression extends AbstractType implements IExpression{
this.operand = operand; this.operand = operand;
} }
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
TypeCheckResult operandTypeCheckResult = operand.typeCheck(); TypeCheckResult operandTypeCheckResult = operand.typeCheck();

View File

@ -36,9 +36,7 @@ public class Program implements Node {
// build type context // build type context
HashMap<String, String> classVars = new HashMap<>(); HashMap<String, String> classVars = new HashMap<>();
for (FieldDecl fieldDecl: oneClass.fieldDecls){ for (FieldDecl fieldDecl: oneClass.fieldDecls){
if(fieldDecl.type == "boolean") classVars.put(fieldDecl.type, fieldDecl.identifier);
fieldDecl.type = "bool";
classVars.put(fieldDecl.identifier, fieldDecl.type);
} }
typeContext.put(oneClass.name, classVars); typeContext.put(oneClass.name, classVars);

View File

@ -7,7 +7,6 @@ import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.*; import org.objectweb.asm.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
public class ReturnStatement extends AbstractType implements IStatement{ public class ReturnStatement extends AbstractType implements IStatement{
IExpression expression; IExpression expression;
@ -23,7 +22,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
if (expression == null) { if (expression == null) {
result.type = "void"; result.type = "void";
} else { } else {
TypeCheckResult typedExpression = expression.typeCheck(methodContext, typeContext, localVars); TypeCheckResult typedExpression = expression.typeCheck();
result.type = typedExpression.type; result.type = typedExpression.type;
} }
@ -40,7 +39,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
expression.codeGen(mv); expression.codeGen(mv);
//Get the Type of the expression //Get the Type of the expression
//TODO: Resolve how do we get the type of the expression //TODO: Resolve how do we get the type of the expression
String type = expression.typeCheck(null, null, null).type; String type = expression.typeCheck(??).type;
if (type.equals("int") || type.equals("bool") || type.equals("char")) { if (type.equals("int") || type.equals("bool") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN); mv.visitInsn(Opcodes.IRETURN);

View File

@ -38,26 +38,18 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
String identifier = localVarIdentifier.getIdentifier(); String identifier = localVarIdentifier.getIdentifier();
leftType.type = localVars.get(identifier); leftType.type = localVars.get(identifier);
}else{ }else{
leftType = left.typeCheck(methodContext, typeContext, localVars); //leftType = left.typeCheck();
} }
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); // TypeCheckResult rightType = right.typeCheck();
//
String upperbound = helper.upperBound(leftType.type, rightType.type); // String upperbound = helper.upperBound(leftType.type, rightType.type);
if (Objects.equals(upperbound, leftType.type)) { // if (Objects.equals(upperbound, leftType.type)) {
result.type = leftType.type; // result.type = leftType.type;
} // }
setTypeCheckResult(result); // setTypeCheckResult(result);
return result; return result;
} }
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
if (left instanceof VarRefExpression varRef) {
}
}
public TypeCheckResult typeCheck() throws Exception { public TypeCheckResult typeCheck() throws Exception {
return null; return null;
} }
@ -84,7 +76,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
} }
counter++; counter++;
} }
if (index == -1){ if (index == -1){
throw new Exception("Variable " + varName + " not found"); throw new Exception("Variable " + varName + " not found");
} }
@ -99,21 +91,29 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
mv.visitVarInsn(Opcodes.ASTORE, index); mv.visitVarInsn(Opcodes.ASTORE, index);
break; break;
} }
} else if (left instanceof InstVarExpression){ } else if (left instanceof InstVarExpression){
instVar = (InstVarExpression) left; instVar = (InstVarExpression) left;
// Load "this" onto the stack // Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitVarInsn(Opcodes.ALOAD, 0);
// if (left instanceof VarRefExpression varRef) { String fieldType = typeContext.get(instVar.classRef.name).get(instVar.fieldName);
// //TODO: Implement the handling of a variable reference --> I need a list of local variables
// // for that to determine if the variable is a local or field variable String fieldDescriptor;
// } else if (left instanceof InstVarExpression instVar) { switch (fieldType) {
// mv.visitInsn(Opcodes.DUP_X1); case "int":
// fieldDescriptor = "I";
// // We now again need the owner (class reference), name (of the Field in the owner) and type of the field break;
// //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type); case "boolean":
// } fieldDescriptor = "Z";
break;
default:
//TODO: We need the fully qualified name of the class here in field type
fieldDescriptor = "L" + fieldType + ";";
break;
}
mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.classRef.name, instVar.fieldName, fieldDescriptor);
}
} }
} }