gettypecheckresult

This commit is contained in:
Krauß, Josefine 2024-05-31 12:59:58 +02:00
parent 5592a6cfe1
commit ec38d8049b
22 changed files with 106 additions and 14 deletions

View File

@ -1,17 +1,21 @@
class ExampleEmpty {
}
class Example { class Example {
int i; int i;
boolean b; boolean b;
char c; char c;
void callM(){
Example2 example2 = new Example2();
example2.m(1);
}
} }
class Example2 { class Example2 {
boolean instVarBool; boolean instVarBool;
int m(int n){ int m(int n){
boolean localBool; boolean a = this.instVarBool;
localBool = true; return n;
if(localBool){
return n;
}
return -1;
} }
} }

View File

@ -15,8 +15,8 @@ import java.util.Objects;
public class RefType extends AbstractType implements Node { public class RefType extends AbstractType implements Node {
//Class Name
public String name; public String name; // Class Name
public List<FieldDecl> fieldDecls; public List<FieldDecl> fieldDecls;
public List<MethodDecl> methodDecls; public List<MethodDecl> methodDecls;
boolean hasMain; boolean hasMain;

View File

@ -24,4 +24,9 @@ public class BoolDatatype extends AbstractType implements IDatatype{
mv.visitInsn(Opcodes.ICONST_0); // 0 for false mv.visitInsn(Opcodes.ICONST_0); // 0 for false
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -24,4 +24,9 @@ public class CharDatatype extends AbstractType implements IDatatype{
mv.visitLdcInsn((int)value); mv.visitLdcInsn((int)value);
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -10,6 +10,8 @@ public interface IDatatype {
// visit method for code generation // visit method for code generation
void codeGen(MethodVisitor mv) throws Exception; void codeGen(MethodVisitor mv) throws Exception;
TypeCheckResult getTypeCheckResult();
} }
//TODO: Check if we need to differentiate between primitive types and reference types --> for example in "==" //TODO: Check if we need to differentiate between primitive types and reference types --> for example in "=="

View File

@ -31,4 +31,9 @@ public class IntDatatype extends AbstractType implements IDatatype{
else else
mv.visitLdcInsn(value); mv.visitLdcInsn(value);
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -175,4 +175,9 @@ public class BinaryExpression extends AbstractType implements IExpression{
mv.visitLabel(expressionEnd); mv.visitLabel(expressionEnd);
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -15,4 +15,6 @@ public interface IExpression extends Node {
// visit method for code generation // visit method for code generation
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception; void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
TypeCheckResult getTypeCheckResult();
} }

View File

@ -63,4 +63,9 @@ public class InstVarExpression implements IExpression{
// Load the variable onto the stack // Load the variable onto the stack
mv.visitFieldInsn(Opcodes.GETFIELD, classRef.name, fieldName, fieldDescriptor); mv.visitFieldInsn(Opcodes.GETFIELD, classRef.name, fieldName, fieldDescriptor);
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -24,4 +24,9 @@ public class IntConstantExpression extends AbstractType implements IExpression{
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -71,4 +71,9 @@ public class LocalVarIdentifier implements IExpression{
break; break;
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -70,4 +70,9 @@ public class UnaryExpression extends AbstractType implements IExpression{
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -86,4 +86,9 @@ public class BlockStatement extends AbstractType implements IStatement {
statement.codeGen(mv, blockLocalVars, typeContext); statement.codeGen(mv, blockLocalVars, typeContext);
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -22,4 +22,9 @@ public class EmptyStatement extends AbstractType implements IStatement{
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
//An empty statement does not generate any code //An empty statement does not generate any code
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -14,4 +14,5 @@ public interface IStatement extends Node {
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception; TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception;
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception; void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
TypeCheckResult getTypeCheckResult();
} }

View File

@ -70,4 +70,9 @@ public class IfElseStatement extends AbstractType implements IStatement{
mv.visitLabel(statementEnd); //End of the if-else statement mv.visitLabel(statementEnd); //End of the if-else statement
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -50,6 +50,11 @@ public class IfStatement extends AbstractType implements IStatement{
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -17,7 +17,7 @@ public class LocalVarDecl implements IStatement{
} }
@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 {
TypeCheckHelper.typeExists(this.type, new ArrayList<>(methodContext.keySet())); TypeCheckHelper.typeExists(this.type, new ArrayList<>(typeContext.keySet()));
localVars.put(this.identifier, this.type); localVars.put(this.identifier, this.type);
@ -49,4 +49,9 @@ public class LocalVarDecl implements IStatement{
mv.visitVarInsn(Opcodes.ASTORE, index); mv.visitVarInsn(Opcodes.ASTORE, index);
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -53,4 +53,9 @@ public class ReturnStatement extends AbstractType implements IStatement{
mv.visitInsn(Opcodes.RETURN); mv.visitInsn(Opcodes.RETURN);
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -59,4 +59,9 @@ public class WhileStatement extends AbstractType implements IStatement {
mv.visitLabel(conditionFalse); mv.visitLabel(conditionFalse);
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -66,10 +66,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
for (MethodDecl methodDecl : methodDecls) { for (MethodDecl methodDecl : methodDecls) {
if (methodDecl.name.equals(methodName)) { if (methodDecl.name.equals(methodName)) {
//Get the method descriptor //Get the method descriptor
descriptor = methodDecl.getMethodDescriptor(methodContext); // descriptor = methodDecl.getMethodDescriptor(methodContext);
} }
} }
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false); // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
} else { } else {
// Load this onto the stack // Load this onto the stack
@ -85,10 +85,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
for (MethodDecl methodDecl : methodDecls) { for (MethodDecl methodDecl : methodDecls) {
if (methodDecl.name.equals(methodName)) { if (methodDecl.name.equals(methodName)) {
//Get the method descriptor //Get the method descriptor
descriptor = methodDecl.getMethodDescriptor(methodContext); // descriptor = methodDecl.getMethodDescriptor(methodContext);
} }
} }
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false); // mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
} }
} }
} }

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.StatementExpression; package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType; import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression; import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
@ -8,6 +9,7 @@ import abstractSyntaxTree.Statement.IStatement;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -22,7 +24,13 @@ public class NewStatementExpression 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(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null; if(!TypeCheckHelper.typeExists(className, new ArrayList<>(typeContext.keySet()))){
throw new Exception("TypeCheck Exception: An instance of " + className + " is created, but the type does not exist.");
}
TypeCheckResult result = new TypeCheckResult();
result.type = className;
setTypeCheckResult(result);
return result;
} }
@Override @Override