Compare commits

...

2 Commits

Author SHA1 Message Date
Krauß, Josefine
5fa6664850 Merge branch 'master' of https://gitea.hb.dhbw-stuttgart.de/i22022/NichtHaskell
# Conflicts:
#	src/main/java/abstractSyntaxTree/Expression/LocalVarIdentifier.java
#	src/main/java/abstractSyntaxTree/Expression/UnaryExpression.java
#	src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java
#	src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java
2024-05-31 09:42:21 +02:00
Krauß, Josefine
08d13a0df7 method parameters 2024-05-31 09:39:17 +02:00
8 changed files with 56 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -19,7 +19,7 @@ public class UnaryExpression extends AbstractType implements IExpression{
this.operand = operand;
}
@Override
public TypeCheckResult typeCheck() 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 {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult operandTypeCheckResult = operand.typeCheck();

View File

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

View File

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

View File

@ -38,18 +38,26 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
String identifier = localVarIdentifier.getIdentifier();
leftType.type = localVars.get(identifier);
}else{
//leftType = left.typeCheck();
leftType = left.typeCheck(methodContext, typeContext, localVars);
}
// TypeCheckResult rightType = right.typeCheck();
//
// String upperbound = helper.upperBound(leftType.type, rightType.type);
// if (Objects.equals(upperbound, leftType.type)) {
// result.type = leftType.type;
// }
// setTypeCheckResult(result);
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);
String upperbound = helper.upperBound(leftType.type, rightType.type);
if (Objects.equals(upperbound, leftType.type)) {
result.type = leftType.type;
}
setTypeCheckResult(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 {
return null;
}
@ -76,7 +84,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
}
counter++;
}
if (index == -1){
throw new Exception("Variable " + varName + " not found");
}
@ -91,29 +99,21 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
mv.visitVarInsn(Opcodes.ASTORE, index);
break;
}
} else if (left instanceof InstVarExpression){
instVar = (InstVarExpression) left;
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
String fieldType = typeContext.get(instVar.classRef.name).get(instVar.fieldName);
String fieldDescriptor;
switch (fieldType) {
case "int":
fieldDescriptor = "I";
break;
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);
}
// if (left instanceof VarRefExpression varRef) {
// //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
// } else if (left instanceof InstVarExpression instVar) {
// mv.visitInsn(Opcodes.DUP_X1);
//
// // We now again need the owner (class reference), name (of the Field in the owner) and type of the field
// //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
// }
}
}