instvar typecheck and getTypeCheckresults

This commit is contained in:
Krauß, Josefine 2024-06-20 08:00:23 +02:00
parent 112c5f2c1b
commit 6915a97432
22 changed files with 63 additions and 50 deletions

View File

@ -105,6 +105,9 @@ public class Compiler {
//abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.returnType = "char"; //abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.returnType = "char";
AssignStatementExpression a = (AssignStatementExpression) abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.statements.get(2);
InstVarExpression b = (InstVarExpression) a.left;
b.classRef = abstractSyntaxTree.classes.get(0);
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheck();
abstractSyntaxTree.codeGen(); abstractSyntaxTree.codeGen();

View File

@ -1,29 +1,11 @@
class ExampleEmpty {
}
class Example { class Example {
int i; int i;
boolean b; boolean b;
char c; char c;
}
class Example2 {
boolean instVarBool;
int m(int n){ int m(int n){
int localA; int localA;
int localB; localA = 5;
return n; this.i = 3;
return localA;
} }
} }
class Example3 {
char isNice;
char m(int n) {
int test = 12;
this.m(test);
boolean l;
l = false;
return 't';
}
}

View File

@ -37,6 +37,6 @@ public class BoolDatatype extends AbstractType implements IDatatype{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -37,6 +37,6 @@ public class CharDatatype extends AbstractType implements IDatatype{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -44,6 +44,6 @@ public class IntDatatype extends AbstractType implements IDatatype{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -186,9 +186,8 @@ public class BinaryExpression extends AbstractType implements IExpression{
&& Objects.equals(right, binaryExpression.right) && Objects.equals(right, binaryExpression.right)
); );
} }
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -27,4 +27,8 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi
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 super.getTypeCheckResult();
}
} }

View File

@ -27,4 +27,9 @@ public class CharConstantExpression 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 super.getTypeCheckResult();
}
} }

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Expression; package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
@ -14,7 +15,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class InstVarExpression implements IExpression{ public class InstVarExpression extends AbstractType implements IExpression{
public RefType classRef; public RefType classRef;
@ -85,6 +86,6 @@ public class InstVarExpression implements IExpression{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -19,7 +19,10 @@ public class IntConstantExpression 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; TypeCheckResult result = new TypeCheckResult();
result.type = "int";
setTypeCheckResult(result);
return result;
} }
@Override @Override
@ -38,6 +41,6 @@ public class IntConstantExpression extends AbstractType implements IExpression{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Expression; package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import org.objectweb.asm.*; import org.objectweb.asm.*;
@ -12,7 +13,7 @@ import org.objectweb.asm.MethodVisitor;
import java.util.HashMap; import java.util.HashMap;
import java.util.Objects; import java.util.Objects;
public class LocalVarIdentifier implements IExpression{ public class LocalVarIdentifier extends AbstractType implements IExpression{
String identifier; String identifier;
public LocalVarIdentifier(String identifier){ public LocalVarIdentifier(String identifier){
@ -31,6 +32,7 @@ public class LocalVarIdentifier implements IExpression{
} else { } else {
throw new Exception("TypeCheck Exception: Local var " + identifier + " does not exist."); throw new Exception("TypeCheck Exception: Local var " + identifier + " does not exist.");
} }
setTypeCheckResult(result);
return result; return result;
} }
@ -73,6 +75,11 @@ public class LocalVarIdentifier implements IExpression{
} }
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return super.getTypeCheckResult();
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -81,9 +88,4 @@ 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

@ -83,6 +83,6 @@ public class UnaryExpression extends AbstractType implements IExpression{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -74,7 +74,7 @@ public class BlockStatement extends AbstractType implements IStatement {
continue; continue;
// set return of block if not known yet // set return of block if not known yet
if(this.returnType == null) if(this.returnType == null|| this.returnType == "not")
this.returnType = typeOfCurrentStatement.type; this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType)) if (!typeOfCurrentStatement.type.equals(this.returnType))
@ -113,6 +113,6 @@ public class BlockStatement extends AbstractType implements IStatement {
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -31,6 +31,6 @@ public class EmptyStatement extends AbstractType implements IStatement{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -81,6 +81,6 @@ public class IfElseStatement extends AbstractType implements IStatement{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -64,7 +64,7 @@ public class IfStatement extends AbstractType implements IStatement{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement; package abstractSyntaxTree.Statement;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper; import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Parameter.ParameterList; import abstractSyntaxTree.Parameter.ParameterList;
@ -8,7 +9,7 @@ import org.objectweb.asm.Opcodes;
import java.util.*; import java.util.*;
public class LocalVarDecl implements IStatement{ public class LocalVarDecl extends AbstractType implements IStatement{
String type; String type;
String identifier; String identifier;
public LocalVarDecl(String type, String identifier) { public LocalVarDecl(String type, String identifier) {
@ -62,6 +63,6 @@ public class LocalVarDecl implements IStatement{
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -65,8 +65,8 @@ public class ReturnStatement extends AbstractType implements IStatement{
); );
} }
// @Override @Override
// public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
// return this. return super.getTypeCheckResult();
// } }
} }

View File

@ -73,6 +73,6 @@ public class WhileStatement extends AbstractType implements IStatement {
@Override @Override
public TypeCheckResult getTypeCheckResult() { public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); return super.getTypeCheckResult();
} }
} }

View File

@ -143,4 +143,8 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
&& Objects.equals(right, assignStatementExpression.right) && Objects.equals(right, assignStatementExpression.right)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return super.getTypeCheckResult();
}
} }

View File

@ -103,4 +103,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
&& Objects.equals(arguments, methodCallStatementExpression.arguments) && Objects.equals(arguments, methodCallStatementExpression.arguments)
); );
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return super.getTypeCheckResult();
}
} }

View File

@ -84,4 +84,8 @@ public class NewStatementExpression extends AbstractType implements IExpression,
descriptor.append(")V"); // Constructors always return void descriptor.append(")V"); // Constructors always return void
return descriptor.toString(); return descriptor.toString();
} }
@Override
public TypeCheckResult getTypeCheckResult() {
return super.getTypeCheckResult();
}
} }