Merge remote-tracking branch 'origin/master'

This commit is contained in:
StefanZ3 2024-05-31 13:08:26 +02:00
commit d665c0393c
22 changed files with 104 additions and 16 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

@ -35,5 +35,8 @@ public class BoolDatatype extends AbstractType implements IDatatype{
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -34,4 +34,9 @@ public class CharDatatype extends AbstractType implements IDatatype{
CharDatatype charDatatype = (CharDatatype) o; CharDatatype charDatatype = (CharDatatype) o;
return (Objects.equals(value, charDatatype.value)); return (Objects.equals(value, charDatatype.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

@ -41,4 +41,9 @@ public class IntDatatype extends AbstractType implements IDatatype{
IntDatatype intDatatype = (IntDatatype) o; IntDatatype intDatatype = (IntDatatype) o;
return (Objects.equals(value, intDatatype.value)); return (Objects.equals(value, intDatatype.value));
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -3,7 +3,6 @@ package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckHelper;
import TypeCheck.AbstractType; import TypeCheck.AbstractType;
import abstractSyntaxTree.Datatype.IntDatatype;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.*; import org.objectweb.asm.*;
@ -187,4 +186,9 @@ public class BinaryExpression extends AbstractType implements IExpression{
&& Objects.equals(right, binaryExpression.right) && Objects.equals(right, binaryExpression.right)
); );
} }
@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

@ -9,7 +9,6 @@ import org.objectweb.asm.Opcodes;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Objects;
public class InstVarExpression implements IExpression{ public class InstVarExpression implements IExpression{
@ -74,4 +73,9 @@ public class InstVarExpression implements IExpression{
&& Objects.equals(fieldName, instVarExpression.fieldName) && Objects.equals(fieldName, instVarExpression.fieldName)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -35,4 +35,9 @@ public class IntConstantExpression extends AbstractType implements IExpression{
return (Objects.equals(value, intConstantExpression.value) return (Objects.equals(value, intConstantExpression.value)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -81,4 +81,9 @@ public class LocalVarIdentifier implements IExpression{
return (Objects.equals(identifier, localVarIdentifier.identifier) return (Objects.equals(identifier, localVarIdentifier.identifier)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -80,4 +80,9 @@ public class UnaryExpression extends AbstractType implements IExpression{
&& Objects.equals(operand, unaryExpression.operand) && Objects.equals(operand, unaryExpression.operand)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -99,4 +99,9 @@ public class BlockStatement extends AbstractType implements IStatement {
&& Objects.equals(statements, blockStatement.statements) && Objects.equals(statements, blockStatement.statements)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -28,4 +28,9 @@ public class EmptyStatement extends AbstractType implements IStatement{
public boolean equals(Object o) { public boolean equals(Object o) {
return true; return true;
} }
@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

@ -81,4 +81,9 @@ public class IfElseStatement extends AbstractType implements IStatement{
&& Objects.equals(elseStatement, ifElseStatement.elseStatement) && Objects.equals(elseStatement, ifElseStatement.elseStatement)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -61,6 +61,11 @@ public class IfStatement extends AbstractType implements IStatement{
&& Objects.equals(ifStatement, ifStatementObj.ifStatement) && Objects.equals(ifStatement, ifStatementObj.ifStatement)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -59,4 +59,9 @@ public class LocalVarDecl implements IStatement{
&& Objects.equals(identifier, localVarDecl.identifier) && Objects.equals(identifier, localVarDecl.identifier)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -63,4 +63,9 @@ public class ReturnStatement extends AbstractType implements IStatement{
return (Objects.equals(expression, returnStatement.expression) return (Objects.equals(expression, returnStatement.expression)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -70,4 +70,9 @@ public class WhileStatement extends AbstractType implements IStatement {
&& Objects.equals(statement, whileStatement.statement) && Objects.equals(statement, whileStatement.statement)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult();
}
} }

View File

@ -67,10 +67,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
@ -86,10 +86,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