This commit is contained in:
Julian Murek 2024-06-20 13:28:31 +02:00
commit e7d4a83a1d
33 changed files with 167 additions and 135 deletions

View File

@ -6,6 +6,7 @@ import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Expression.UnaryExpression;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.IfElseStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
@ -78,36 +79,6 @@ public class Compiler {
System.out.println(refType.name);
}
//
// List<FieldDecl> fieldDecls = new ArrayList<>();
//
// FieldDecl fieldDecl = new FieldDecl("int", "i");
// fieldDecls.add(fieldDecl);
//
//// FieldDecl fieldDecl2 = new FieldDecl("char", "i");
//// fieldDecls.add(fieldDecl2);
//
// List<MethodDecl> methodDecls = new ArrayList<>();
//
//
// MethodDecl methodDecl = new MethodDecl("ClassA", "int", "m", new ParameterList(new ArrayList<>()), new BlockStatement(new ArrayList<>(), "void"));
// methodDecls.add(methodDecl);
//
//// MethodDecl methodDecl2 = new MethodDecl("ClassA", "int", "m", new ArrayList<>(), new ArrayList<>());
//// methodDecls.add(methodDecl2);
//
// abstractSyntaxTree.classes.add(new RefType("MyClass", fieldDecls, methodDecls, false));
//
// System.out.println("Parsed " + abstractSyntaxTree.classes.size() + " classes with identifiers/names:");
// for (RefType refType : abstractSyntaxTree.classes) {
// System.out.println(refType.name);
// }
//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.codeGen();

View File

@ -1,11 +1,21 @@
class Example1 {
int i;
Example e;
int m(int n){
Example e = new Example();
e.m(1);
this.e = new Example();
this.e.m(2);
this.m(2);
return 0;
}
}
class Example {
int i;
boolean b;
char c;
int m(int n){
int localA;
localA = 5;
this.i = 3;
return localA;
int m(int a){
return 0;
}
}

View File

@ -0,0 +1,7 @@
package TypeCheck;
public class TypeCheckException extends Exception {
public TypeCheckException(String message) {
super(message);
}
}

View File

@ -4,23 +4,19 @@ import java.util.List;
import java.util.Objects;
public class TypeCheckHelper {
public String upperBound(String type1, String type2) throws Exception{
if(Objects.equals(type1, "boolean"))
type1 = "bool";
if (Objects.equals(type2, "boolean"))
type2 = "bool";
boolean type1Primitiv = Objects.equals(type1, "bool") || Objects.equals(type1, "int") || Objects.equals(type1, "char");
boolean type2Primitiv = Objects.equals(type2, "bool") || Objects.equals(type2, "int") || Objects.equals(type2, "char");
public String upperBound(String type1, String type2) throws TypeCheckException{
boolean type1Primitiv = Objects.equals(type1, "boolean") || Objects.equals(type1, "int") || Objects.equals(type1, "char");
boolean type2Primitiv = Objects.equals(type2, "boolean") || Objects.equals(type2, "int") || Objects.equals(type2, "char");
String result;
if(type1Primitiv && type2Primitiv){
if(Objects.equals(type1, type2)){
result = type1;
}else{
throw new Exception("no upper bound");
throw new TypeCheckException("There is no upper bound.");
}
}else if(type1Primitiv || type2Primitiv){
throw new Exception("no upper bound");
throw new TypeCheckException("There is no upper bound.");
}else{
result = "class";
}

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Class;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
@ -23,7 +24,7 @@ public class FieldDecl extends AbstractType implements Node {
this.type = type;
this.identifier = identifier;
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, String>> typeContext) throws Exception {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Class;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.Parameter;
@ -33,7 +34,7 @@ public class MethodDecl implements Node {
this.localVars = new LinkedHashMap<>();
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException {
// jede methode als block statement aufrufen und jede neue locale varibale in localvars schreiben
List<Parameter> parametersList = parameters.parameterList;
for(Parameter parameter : parametersList){
@ -43,11 +44,14 @@ public class MethodDecl implements Node {
TypeCheckResult result = new TypeCheckResult();
String CodeBlockType = codeBlock.typeCheck(methodContext, typeContext, localVars).type;
if(!Objects.equals(this.returnType, CodeBlockType))
throw new Exception("TypeCheck Exception: Method retruns wrong type");
throw new TypeCheckException("Method returns " + CodeBlockType + ", but should retrun " + this.returnType + ". ");
result.type = codeBlock.returnType;
return result;
}
//TODO: Es wird kein Bytecode für Standardkonstruktoren für die Klassen generiert
//TODO: Stack computing schlägt fehl --> Reihenfolge aufruf? --> Sobald if-else / if / while drin sind? --> vllt auch schon bei MethodenDecl
//Need to get the returnType of the method if it is an object
// methodContext (class, (returnType, (identifier, parameter)))
public void codeGen(ClassWriter cw, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Class;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Node;
@ -34,14 +35,14 @@ public class RefType extends AbstractType implements Node {
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext,
HashMap<String, HashMap<String, String>> typeContext) throws Exception {
HashMap<String, HashMap<String, String>> typeContext) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
// check if field with the same identifier is defined more than once
List<String> discoveredFieldIdentifiers = new ArrayList<>();
for (FieldDecl fieldDecl : fieldDecls) {
if(discoveredFieldIdentifiers.contains(fieldDecl.identifier)){
throw new Exception("A field with the identifier " + fieldDecl.identifier + " was defined more than once in the same class.");
throw new TypeCheckException("A field with the identifier " + fieldDecl.identifier + " was defined more than once in the class " + this.name + ". ");
}
discoveredFieldIdentifiers.add(fieldDecl.identifier);
}
@ -58,7 +59,7 @@ public class RefType extends AbstractType implements Node {
if (discoveredMethods.containsKey(methodDecl.returnType)) {
if(discoveredMethods.get(methodDecl.returnType).equals(methodDecl.name)){
throw new Exception("A method with the name " + methodDecl.name + " and return type " + methodDecl.returnType + " was defined more than once in the same class.");
throw new TypeCheckException("A method with the name " + methodDecl.name + " and return type " + methodDecl.returnType + " was defined more than once in the same class.");
}
}
discoveredMethods.put(methodDecl.returnType, methodDecl.name);

View File

@ -17,10 +17,10 @@ public class BoolDatatype extends AbstractType implements IDatatype{
return (Objects.equals(value, boolDatatype.value));
}
@Override
public TypeCheckResult typeCheck() throws Exception {
public TypeCheckResult typeCheck() {
TypeCheckResult result = new TypeCheckResult();
result.type = "bool";
result.type = "boolean";
setTypeCheckResult(result);
return result;

View File

@ -10,7 +10,7 @@ public class CharDatatype extends AbstractType implements IDatatype{
char value;
@Override
public TypeCheckResult typeCheck() throws Exception {
public TypeCheckResult typeCheck() {
TypeCheckResult result = new TypeCheckResult();
result.type = "char";

View File

@ -1,11 +1,12 @@
package abstractSyntaxTree.Datatype;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import org.objectweb.asm.MethodVisitor;
public interface IDatatype {
// typeCheck method
TypeCheckResult typeCheck() throws Exception;
TypeCheckResult typeCheck() throws TypeCheckException;
// visit method for code generation

View File

@ -10,7 +10,7 @@ import java.util.Objects;
public class IntDatatype extends AbstractType implements IDatatype{
int value;
@Override
public TypeCheckResult typeCheck() throws Exception {
public TypeCheckResult typeCheck() {
TypeCheckResult result = new TypeCheckResult();
result.type = "int";
@ -19,9 +19,6 @@ public class IntDatatype extends AbstractType implements IDatatype{
return result;
}
// visit method for code generation
@Override
public void codeGen(MethodVisitor mv) throws Exception {

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.TypeCheckHelper;
import TypeCheck.AbstractType;
@ -24,7 +25,7 @@ public class BinaryExpression 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckHelper helper = new TypeCheckHelper();
TypeCheckResult result = new TypeCheckResult();
@ -163,7 +164,7 @@ public class BinaryExpression extends AbstractType implements IExpression{
break;
default:
throw new Exception("Unknown operator: " + operator);
throw new TypeCheckException("The operator " + operator + " is not known.");
}
mv.visitLabel(operationFalse);

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
@ -16,9 +17,9 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi
}
@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) {
TypeCheckResult result = new TypeCheckResult();
result.type = "bool";
result.type = "boolean";
setTypeCheckResult(result);
return result;
}

View File

@ -16,7 +16,7 @@ public class CharConstantExpression 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) {
TypeCheckResult result = new TypeCheckResult();
result.type = "char";
setTypeCheckResult(result);

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList;
@ -11,7 +12,7 @@ import java.util.LinkedHashMap;
public interface IExpression extends Node {
// typeCheck method
//TypeCheckResult typeCheck() throws Exception;
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 TypeCheckException;
// visit method for code generation
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
@ -30,10 +31,11 @@ public class InstVarExpression 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 {
//todo ///////////////////
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
String varType = typeContext.get(classRef.name).get(fieldName);
if (varType == null) {
throw new TypeCheckException("Field " + fieldName + " was not found in class " + classRef.name + ".");
}
TypeCheckResult result = new TypeCheckResult();
result.type = varType;
return result;

View File

@ -18,7 +18,7 @@ public class IntConstantExpression 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) {
TypeCheckResult result = new TypeCheckResult();
result.type = "int";
setTypeCheckResult(result);

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import org.objectweb.asm.*;
@ -25,12 +26,12 @@ public class LocalVarIdentifier 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
if (localVars.containsKey(identifier)) {
result.type = localVars.get(identifier);
} else {
throw new Exception("TypeCheck Exception: Local var " + identifier + " does not exist.");
throw new TypeCheckException("Local var " + identifier + " does not exist.");
}
setTypeCheckResult(result);
return result;

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.Expression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Datatype.IDatatype;
@ -20,7 +21,7 @@ public class UnaryExpression extends AbstractType implements IExpression{
this.operand = operand;
}
@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 TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult operandTypeCheckResult = operand.typeCheck();

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
@ -27,7 +28,7 @@ public class Program implements Node {
}
public TypeCheckResult typeCheck() throws Exception{
public TypeCheckResult typeCheck() throws TypeCheckException {
this.typeContext = new HashMap<>();
this.methodContext = new HashMap<>();

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Class.FieldDecl;
@ -28,7 +29,7 @@ public class BlockStatement extends AbstractType 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 {
HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
this.localVars = localVars;
@ -54,10 +55,10 @@ public class BlockStatement extends AbstractType implements IStatement {
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
if (!firstType.equals("void") && this.returnType != null) {
throw new Exception("TypeCheck Exception: if paths return wrong type");
throw new TypeCheckException("The if-Path returns the wrong type.");
}
if (!secondType.equals("void") && this.returnType != null) {
throw new Exception("TypeCheck Exception: else paths return wrong type");
throw new TypeCheckException("The else-path returns the wrong type.");
}
boolean firstIsVoid = firstType.equals("void");
@ -78,11 +79,9 @@ public class BlockStatement extends AbstractType implements IStatement {
this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType))
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
throw new TypeCheckException("At least some statements of the block returns the wrong type or missing return statement.");
}
result.type = this.returnType;
// todo check if the block returns the needed return type in every case
// todo ignore unreachable statements?
setTypeCheckResult(result);
return result;
}

View File

@ -13,7 +13,7 @@ import java.util.Objects;
public class EmptyStatement extends AbstractType 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) {
TypeCheckResult result = new TypeCheckResult();
result.type = "void";
return result;

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList;
@ -11,7 +12,7 @@ import java.util.List;
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 TypeCheckException;
void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
TypeCheckResult getTypeCheckResult();

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
@ -12,7 +13,7 @@ import java.util.List;
import java.util.Objects;
public class IfElseStatement extends AbstractType implements IStatement{
IExpression condition;
public IExpression condition;
IStatement ifStatement;
IStatement elseStatement;
@ -24,13 +25,13 @@ public class IfElseStatement extends AbstractType 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
if (!conditionType.type.equals("bool")) {
throw new IllegalArgumentException("should be boolean");
if (!conditionType.type.equals("boolean")) {
throw new TypeCheckException("The condition of a if statement is " + conditionType.type + ", but should be boolean.");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
@ -38,7 +39,7 @@ public class IfElseStatement extends AbstractType implements IStatement{
if (!ifStatementType.type.equals(elseStatementType.type)) {
if(ifStatementType.type != "void" && elseStatementType.type != "void")
throw new IllegalArgumentException("TypeCeck Exception: if and else have different types");
throw new TypeCheckException("If- and else-path return different not-void types.");
}
result.type = ifStatementType.type + "," + elseStatementType.type;

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
@ -12,7 +13,7 @@ import java.util.List;
import java.util.Objects;
public class IfStatement extends AbstractType implements IStatement{
IExpression condition;
public IExpression condition;
//Do we need a block statement here?
IStatement ifStatement;
@ -23,13 +24,13 @@ public class IfStatement extends AbstractType 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
if (!conditionType.equals("bool")) {
throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
if (!conditionType.type.equals("boolean")) {
throw new TypeCheckException("Condition of If-Statement should is " + conditionType.type + ", but should be boolean.");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);

View File

@ -1,8 +1,10 @@
package abstractSyntaxTree.Statement;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -12,12 +14,14 @@ import java.util.*;
public class LocalVarDecl extends AbstractType implements IStatement{
String type;
String identifier;
public LocalVarDecl(String type, String identifier) {
IExpression expression;
public LocalVarDecl(String type, String identifier, IExpression expression) {
this.type = type;
this.identifier = identifier;
this.expression = expression;
}
@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 TypeCheckException {
TypeCheckHelper.typeExists(this.type, new ArrayList<>(methodContext.keySet()));
localVars.put(this.identifier, this.type);

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
@ -19,7 +20,7 @@ public class ReturnStatement extends AbstractType 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
if (expression == null) {
@ -45,7 +46,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
//TODO: Resolve how do we get the type of the expression
String type = expression.getTypeCheckResult().type;
if (type.equals("int") || type.equals("bool") || type.equals("char")) {
if (type.equals("int") || type.equals("boolean") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN);
} else {
mv.visitInsn(Opcodes.ARETURN);

View File

@ -1,5 +1,6 @@
package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
@ -21,12 +22,12 @@ public class WhileStatement extends AbstractType 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
TypeCheckResult result = new TypeCheckResult();
// check condition
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
if (!conditionType.equals("bool")) {
if (!conditionType.type.equals("boolean")) {
throw new IllegalArgumentException("Expected boolean");
}

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
@ -28,7 +29,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
}
@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 TypeCheckException {
TypeCheckHelper helper = new TypeCheckHelper();
TypeCheckResult result = new TypeCheckResult();
@ -48,7 +49,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
if(Objects.equals(leftType.type, "boolean"))
leftType.type = "bool";
if (!Objects.equals(upperbound, leftType.type)) {
throw new Exception("TypeCheck Exception: upper bound of assignment is not left type");
throw new TypeCheckException("The upper bound of assignment is not the left type.");
}
result.type = "void";
setTypeCheckResult(result);

View File

@ -1,12 +1,14 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.IStatement;
import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -28,29 +30,34 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
this.arguments = arguments;
}
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
RefType searchMethodHere;
if(classThatHasTheMethodIfNotThis == null){
searchMethodHere = thisClass;
} else {
searchMethodHere = classThatHasTheMethodIfNotThis;
}
List<MethodDecl> methods = searchMethodHere.methodDecls;
if(!methods.contains(methodName)){
throw new Exception("method not found");
}
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;
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
//todo i really dont get this. waiting for tuesday meeting
throw new TypeCheckException("NOT IMPLEMENTED");
// TypeCheckResult result = new TypeCheckResult();
// String receivingField;
// String classContainingMethod;
//
// if(receiver.instVarExpression != null){
// receivingField = receiver.instVarExpression.fieldName;
// }else if(receiver.thisExpression) {
//
// }
// RefType searchMethodHere;
// if(classThatHasTheMethodIfNotThis == null){
// searchMethodHere = thisClass;
// } else {
// searchMethodHere = classThatHasTheMethodIfNotThis;
// }
//
// List<MethodDecl> methods = searchMethodHere.methodDecls;
//
// if(!methods.contains(methodName)){
// throw new TypeCheckException("The method " + methodName + " is called, but does not exist.");
// }
//
// return result;
}
//Errors occur due to the change in parameter in the RefType class
@ -60,6 +67,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
//Generate Bytecode for the receiver
if (classThatHasTheMethodIfNotThis != null) {
//TODO: classThatHasTheMethodIfNotThis must be an object --> instance of the class not the class itself
// This is not finished
// Need to call codeGen so it pushes the instance onto the stack, which will be popped of
//classThatHasTheMethodIfNotThis.codeGen();
@ -87,7 +95,7 @@ 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);

View File

@ -1,6 +1,7 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
@ -26,14 +27,16 @@ 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 {
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws TypeCheckException {
//todo testen wenn in ast vorhanden
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.");
throw new TypeCheckException("An instance of " + className + " is created, but the type does not exist.");
}
TypeCheckResult result = new TypeCheckResult();
result.type = className;
setTypeCheckResult(result);
return result;
}
@Override

View File

@ -1,7 +1,16 @@
package abstractSyntaxTree.StatementExpression;
import TypeCheck.TypeCheckException;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.IStatement;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.LinkedHashMap;
public class Receiver implements Node {
boolean thisExpression;
@ -24,6 +33,7 @@ public class Receiver implements Node {
public Receiver(String identifier) {
this.identifier = identifier;
}
}

View File

@ -34,7 +34,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
boolean hasMain;
if(ctx.MainMethodDecl() != null) {
hasMain = true;
MethodDecl mainMethod = new MethodDecl(name, "void", "main", new ParameterList(new ArrayList<>()), new BlockStatement(new ArrayList<>(), "void"));
MethodDecl mainMethod = new MethodDecl(name, "void", "main", new ParameterList(new ArrayList<>()), new BlockStatement(new ArrayList<>(), null));
} else {
hasMain = false;
}
@ -60,13 +60,21 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override
public Node visitLocalVarDecl(DecafParser.LocalVarDeclContext ctx) {
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText());
if (ctx.expression() != null) {
IExpression expression = (IExpression) visit(ctx.expression());
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), expression);
} else {
return new LocalVarDecl(ctx.type().getText(), ctx.Identifier().getText(), null);
}
}
@Override
public Node visitConstuctorDecl(DecafParser.ConstuctorDeclContext ctx) {
String name = ctx.Identifier().getText();
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
ParameterList parameterList = new ParameterList(new ArrayList<>());
if (ctx.parameterList() != null) {
parameterList = (ParameterList) visit(ctx.parameterList());
}
BlockStatement block = (BlockStatement) visitBlock(ctx.block());
return new MethodDecl("", null, name, parameterList, block);
}
@ -76,7 +84,10 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
String type;
String name = ctx.Identifier().getText();
BlockStatement block = (BlockStatement) visitBlock(ctx.block());
ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
ParameterList parameterList = new ParameterList(new ArrayList<>());
if (ctx.parameterList() != null) {
parameterList = (ParameterList) visit(ctx.parameterList());
}
if (ctx.Void() != null) {
type = "void";
} else {
@ -371,13 +382,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
return null;
}
public String getReturnType(List<IStatement> statements){
for(IStatement stmt: statements) {
if(stmt instanceof ReturnStatement) {
return "not";
}
}
return "void";
public String getReturnType(List<IStatement> statements) {
return null;
}
}