inzterface changes typecheck
This commit is contained in:
parent
c3a9b9a7d6
commit
87cde5e048
@ -1,9 +1,5 @@
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.BlockStatement;
|
||||
import gen.DecafLexer;
|
||||
import gen.DecafParser;
|
||||
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.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;
|
||||
|
||||
public class Compiler {
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
|
||||
// get file
|
||||
// String filePath = "EmptyClass.java";
|
||||
// String content = new String(Files.readAllBytes(Paths.get(filePath)));
|
||||
// CharArrayReader charStream = new CharArrayReader(content.toCharArray());
|
||||
Path filePath = Paths.get("NichtHaskell/src/main/java/Input.java");
|
||||
|
||||
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);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
|
||||
@ -46,41 +46,40 @@ public class Compiler {
|
||||
|
||||
ParseTree tree = parser.program();
|
||||
|
||||
System.out.println("--- print tree ---");
|
||||
System.out.println(tree);
|
||||
|
||||
ASTGenerator generator = new ASTGenerator();
|
||||
Program abstractSyntaxTree =(Program) generator.visit(tree);
|
||||
|
||||
|
||||
System.out.println("--- AST generator ---");
|
||||
System.out.println("Parsed " + abstractSyntaxTree.classes.size() + " classes with names:");
|
||||
for (RefType refType : abstractSyntaxTree.classes) {
|
||||
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);
|
||||
}
|
||||
//
|
||||
// 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.typeCheck();
|
||||
|
||||
|
@ -10,6 +10,7 @@ import java.util.List;
|
||||
public interface IClass {
|
||||
// visit method for code generation
|
||||
|
||||
|
||||
void codeGen(ClassWriter cw) throws Exception;
|
||||
|
||||
}
|
||||
|
@ -42,7 +42,8 @@ public class Program implements Node {
|
||||
HashMap<String, List<String>> methodIdentifierAndParameter = new HashMap<>();
|
||||
HashMap<String, HashMap<String, List<String >>> returnTypeAndMethod = new HashMap<>();
|
||||
for (MethodDecl methodDecl : oneClass.methodDecls){
|
||||
methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters);
|
||||
|
||||
methodIdentifierAndParameter.put(methodDecl.name, (List<String>) methodDecl.parameters);
|
||||
returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter);
|
||||
}
|
||||
methodContext.put(oneClass.name, returnTypeAndMethod);
|
||||
|
@ -25,6 +25,11 @@ public class BlockStatement extends AbstractType implements IStatement{
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
@ -35,13 +40,14 @@ public class BlockStatement extends AbstractType implements IStatement{
|
||||
|
||||
TypeCheckResult blockType = new TypeCheckResult();
|
||||
for (IStatement statement : statements) {
|
||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck();
|
||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
|
||||
|
||||
if (!typeOfCurrentStatement.equals(this.returnType) && !blockType.equals("void")) {
|
||||
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 ignore unreadchable statements?
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -15,9 +15,16 @@ public class EmptyStatement extends AbstractType implements IStatement{
|
||||
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
|
||||
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
|
||||
|
@ -13,7 +13,11 @@ public interface IStatement extends Node {
|
||||
|
||||
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;
|
||||
|
||||
|
||||
void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception;
|
||||
}
|
||||
|
@ -40,6 +40,11 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
|
@ -24,8 +24,8 @@ public class IfStatement extends AbstractType implements IStatement{
|
||||
|
||||
TypeCheckResult conditionType = condition.typeCheck();
|
||||
|
||||
if (!conditionType.equals("boolean")) {
|
||||
throw new IllegalArgumentException("should be boolean");
|
||||
if (!conditionType.equals("bool")) {
|
||||
throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
|
||||
}
|
||||
|
||||
TypeCheckResult ifStatementType = ifStatement.typeCheck();
|
||||
@ -33,6 +33,11 @@ public class IfStatement extends AbstractType implements IStatement{
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
|
@ -29,6 +29,11 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
|
@ -33,6 +33,11 @@ public class WhileStatement extends AbstractType implements IStatement {
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
|
@ -34,11 +34,21 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
left.codeGen(mv);
|
||||
|
@ -44,11 +44,21 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
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
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
|
@ -15,11 +15,21 @@ public class NewStatementExpression extends AbstractType implements IExpression,
|
||||
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
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
throw new Exception("CodeGen not implemented for NewStatementExpression");
|
||||
|
Loading…
Reference in New Issue
Block a user