gettypecheckresult
This commit is contained in:
parent
5592a6cfe1
commit
ec38d8049b
@ -1,17 +1,21 @@
|
||||
class ExampleEmpty {
|
||||
|
||||
}
|
||||
|
||||
class Example {
|
||||
int i;
|
||||
boolean b;
|
||||
char c;
|
||||
void callM(){
|
||||
Example2 example2 = new Example2();
|
||||
example2.m(1);
|
||||
}
|
||||
|
||||
}
|
||||
class Example2 {
|
||||
boolean instVarBool;
|
||||
int m(int n){
|
||||
boolean localBool;
|
||||
localBool = true;
|
||||
if(localBool){
|
||||
return n;
|
||||
}
|
||||
return -1;
|
||||
boolean a = this.instVarBool;
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
@ -15,8 +15,8 @@ import java.util.Objects;
|
||||
|
||||
public class RefType extends AbstractType implements Node {
|
||||
|
||||
//Class Name
|
||||
public String name;
|
||||
|
||||
public String name; // Class Name
|
||||
public List<FieldDecl> fieldDecls;
|
||||
public List<MethodDecl> methodDecls;
|
||||
boolean hasMain;
|
||||
|
@ -24,4 +24,9 @@ public class BoolDatatype extends AbstractType implements IDatatype{
|
||||
mv.visitInsn(Opcodes.ICONST_0); // 0 for false
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -24,4 +24,9 @@ public class CharDatatype extends AbstractType implements IDatatype{
|
||||
|
||||
mv.visitLdcInsn((int)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,8 @@ public interface IDatatype {
|
||||
// visit method for code generation
|
||||
|
||||
void codeGen(MethodVisitor mv) throws Exception;
|
||||
|
||||
TypeCheckResult getTypeCheckResult();
|
||||
}
|
||||
|
||||
//TODO: Check if we need to differentiate between primitive types and reference types --> for example in "=="
|
@ -31,4 +31,9 @@ public class IntDatatype extends AbstractType implements IDatatype{
|
||||
else
|
||||
mv.visitLdcInsn(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -175,4 +175,9 @@ public class BinaryExpression extends AbstractType implements IExpression{
|
||||
|
||||
mv.visitLabel(expressionEnd);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
@ -15,4 +15,6 @@ public interface IExpression extends Node {
|
||||
|
||||
// visit method for code generation
|
||||
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
|
||||
|
||||
TypeCheckResult getTypeCheckResult();
|
||||
}
|
||||
|
@ -63,4 +63,9 @@ public class InstVarExpression implements IExpression{
|
||||
// Load the variable onto the stack
|
||||
mv.visitFieldInsn(Opcodes.GETFIELD, classRef.name, fieldName, fieldDescriptor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -71,4 +71,9 @@ public class LocalVarIdentifier implements IExpression{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -70,4 +70,9 @@ public class UnaryExpression extends AbstractType implements IExpression{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -86,4 +86,9 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
statement.codeGen(mv, blockLocalVars, typeContext);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
@ -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 {
|
||||
//An empty statement does not generate any code
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
|
||||
TypeCheckResult getTypeCheckResult();
|
||||
}
|
||||
|
@ -70,4 +70,9 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
||||
mv.visitLabel(statementEnd); //End of the if-else statement
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -17,7 +17,7 @@ public class LocalVarDecl implements IStatement{
|
||||
}
|
||||
@Override
|
||||
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);
|
||||
|
||||
@ -49,4 +49,9 @@ public class LocalVarDecl implements IStatement{
|
||||
mv.visitVarInsn(Opcodes.ASTORE, index);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -53,4 +53,9 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
mv.visitInsn(Opcodes.RETURN);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
||||
|
@ -59,4 +59,9 @@ public class WhileStatement extends AbstractType implements IStatement {
|
||||
|
||||
mv.visitLabel(conditionFalse);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
}
|
@ -66,10 +66,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
for (MethodDecl methodDecl : methodDecls) {
|
||||
if (methodDecl.name.equals(methodName)) {
|
||||
//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 {
|
||||
// Load this onto the stack
|
||||
@ -85,10 +85,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
for (MethodDecl methodDecl : methodDecls) {
|
||||
if (methodDecl.name.equals(methodName)) {
|
||||
//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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package abstractSyntaxTree.StatementExpression;
|
||||
|
||||
import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckHelper;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
@ -8,6 +9,7 @@ import abstractSyntaxTree.Statement.IStatement;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@ -22,7 +24,13 @@ public class NewStatementExpression extends AbstractType 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 {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user