inzterface changes typecheck

This commit is contained in:
Krauß, Josefine 2024-05-14 11:33:17 +02:00
parent c3a9b9a7d6
commit 87cde5e048
13 changed files with 109 additions and 41 deletions

View File

@ -1,9 +1,5 @@
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program; import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import gen.DecafLexer; import gen.DecafLexer;
import gen.DecafParser; import gen.DecafParser;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
@ -12,19 +8,23 @@ import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.ParseTree;
import java.util.ArrayList; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List; import java.util.List;
public class Compiler { public class Compiler {
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
// get file Path filePath = Paths.get("NichtHaskell/src/main/java/Input.java");
// String filePath = "EmptyClass.java";
// String content = new String(Files.readAllBytes(Paths.get(filePath)));
// CharArrayReader charStream = new CharArrayReader(content.toCharArray());
CharStream codeCharStream = CharStreams.fromString("class Example { int i; }"); String content = Files.readString(filePath);
System.out.println("--- print content ---");
System.out.println(content);
CharStream codeCharStream = CharStreams.fromString(content);
DecafLexer lexer = new DecafLexer(codeCharStream); DecafLexer lexer = new DecafLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
@ -46,41 +46,40 @@ public class Compiler {
ParseTree tree = parser.program(); ParseTree tree = parser.program();
System.out.println("--- print tree ---");
System.out.println(tree);
ASTGenerator generator = new ASTGenerator(); ASTGenerator generator = new ASTGenerator();
Program abstractSyntaxTree =(Program) generator.visit(tree); Program abstractSyntaxTree =(Program) generator.visit(tree);
System.out.println("--- AST generator ---");
System.out.println("Parsed " + abstractSyntaxTree.classes.size() + " classes with names:"); System.out.println("Parsed " + abstractSyntaxTree.classes.size() + " classes with names:");
for (RefType refType : abstractSyntaxTree.classes) { for (RefType refType : abstractSyntaxTree.classes) {
System.out.println(refType.name); System.out.println(refType.name);
} }
//
List<FieldDecl> fieldDecls = new ArrayList<>(); // List<FieldDecl> fieldDecls = new ArrayList<>();
//
FieldDecl fieldDecl = new FieldDecl("int", "i"); // FieldDecl fieldDecl = new FieldDecl("int", "i");
fieldDecls.add(fieldDecl); // fieldDecls.add(fieldDecl);
//
// FieldDecl fieldDecl2 = new FieldDecl("char", "i"); //// FieldDecl fieldDecl2 = new FieldDecl("char", "i");
// fieldDecls.add(fieldDecl2); //// fieldDecls.add(fieldDecl2);
//
List<MethodDecl> methodDecls = new ArrayList<>(); // 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 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); //// MethodDecl methodDecl2 = new MethodDecl("ClassA", "int", "m", new ArrayList<>(), new ArrayList<>());
//// methodDecls.add(methodDecl2);
abstractSyntaxTree.classes.add(new RefType("MyClass", fieldDecls, methodDecls, false)); //
// 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("Parsed " + abstractSyntaxTree.classes.size() + " classes with identifiers/names:");
System.out.println(refType.name); // for (RefType refType : abstractSyntaxTree.classes) {
} // System.out.println(refType.name);
// }
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheck();

View File

@ -10,6 +10,7 @@ import java.util.List;
public interface IClass { public interface IClass {
// visit method for code generation // visit method for code generation
void codeGen(ClassWriter cw) throws Exception; void codeGen(ClassWriter cw) throws Exception;
} }

View File

@ -42,7 +42,8 @@ public class Program implements Node {
HashMap<String, List<String>> methodIdentifierAndParameter = new HashMap<>(); HashMap<String, List<String>> methodIdentifierAndParameter = new HashMap<>();
HashMap<String, HashMap<String, List<String >>> returnTypeAndMethod = new HashMap<>(); HashMap<String, HashMap<String, List<String >>> returnTypeAndMethod = new HashMap<>();
for (MethodDecl methodDecl : oneClass.methodDecls){ for (MethodDecl methodDecl : oneClass.methodDecls){
methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters);
methodIdentifierAndParameter.put(methodDecl.name, (List<String>) methodDecl.parameters);
returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter);
} }
methodContext.put(oneClass.name, returnTypeAndMethod); methodContext.put(oneClass.name, returnTypeAndMethod);

View File

@ -25,6 +25,11 @@ public class BlockStatement extends AbstractType implements IStatement{
return null; return null;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
@ -35,13 +40,14 @@ public class BlockStatement extends AbstractType implements IStatement{
TypeCheckResult blockType = new TypeCheckResult(); TypeCheckResult blockType = new TypeCheckResult();
for (IStatement statement : statements) { for (IStatement statement : statements) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(); TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if (!typeOfCurrentStatement.equals(this.returnType) && !blockType.equals("void")) { if (!typeOfCurrentStatement.equals(this.returnType) && !blockType.equals("void")) {
throw new IllegalArgumentException("Block returns type that it should not return"); throw new IllegalArgumentException("Block returns type that it should not return");
} }
} }
// todo check if the block returns the needed return type in every case // todo check if the block returns the needed return type in every case
// todo ignore unreadchable statements?
return result; return result;
} }

View File

@ -15,9 +15,16 @@ public class EmptyStatement extends AbstractType implements IStatement{
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return typeCheck(methodContext, typeContext);
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; TypeCheckResult result = new TypeCheckResult();
result.type = "void";
return result;
} }
@Override @Override

View File

@ -13,7 +13,11 @@ public interface IStatement extends Node {
TypeCheckResult typeCheck() throws Exception; TypeCheckResult typeCheck() throws Exception;
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception;
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception; TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception;
void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception; void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception;
} }

View File

@ -40,6 +40,11 @@ public class IfElseStatement extends AbstractType implements IStatement{
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;

View File

@ -24,8 +24,8 @@ public class IfStatement extends AbstractType implements IStatement{
TypeCheckResult conditionType = condition.typeCheck(); TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("boolean")) { if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("should be boolean"); throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
} }
TypeCheckResult ifStatementType = ifStatement.typeCheck(); TypeCheckResult ifStatementType = ifStatement.typeCheck();
@ -33,6 +33,11 @@ public class IfStatement extends AbstractType implements IStatement{
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;

View File

@ -29,6 +29,11 @@ public class ReturnStatement extends AbstractType implements IStatement{
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;

View File

@ -33,6 +33,11 @@ public class WhileStatement extends AbstractType implements IStatement {
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;

View File

@ -34,11 +34,21 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;
} }
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
}
@Override @Override
public void codeGen(MethodVisitor mv) throws Exception { public void codeGen(MethodVisitor mv) throws Exception {
left.codeGen(mv); left.codeGen(mv);

View File

@ -44,11 +44,21 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
return result; return result;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;
} }
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
}
//Errors occur due to the change in parameter in the RefType class //Errors occur due to the change in parameter in the RefType class
@Override @Override
public void codeGen(MethodVisitor mv) throws Exception { public void codeGen(MethodVisitor mv) throws Exception {

View File

@ -15,11 +15,21 @@ public class NewStatementExpression extends AbstractType implements IExpression,
return null; return null;
} }
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception { public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
return null; return null;
} }
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
}
@Override @Override
public void codeGen(MethodVisitor mv) throws Exception { public void codeGen(MethodVisitor mv) throws Exception {
throw new Exception("CodeGen not implemented for NewStatementExpression"); throw new Exception("CodeGen not implemented for NewStatementExpression");