diff --git a/src/main/java/Compiler.java b/src/main/java/Compiler.java index d3c6153..28e65da 100644 --- a/src/main/java/Compiler.java +++ b/src/main/java/Compiler.java @@ -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 fieldDecls = new ArrayList<>(); - - FieldDecl fieldDecl = new FieldDecl("int", "i"); - fieldDecls.add(fieldDecl); - -// FieldDecl fieldDecl2 = new FieldDecl("char", "i"); -// fieldDecls.add(fieldDecl2); - - List 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 fieldDecls = new ArrayList<>(); +// +// FieldDecl fieldDecl = new FieldDecl("int", "i"); +// fieldDecls.add(fieldDecl); +// +//// FieldDecl fieldDecl2 = new FieldDecl("char", "i"); +//// fieldDecls.add(fieldDecl2); +// +// List 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(); diff --git a/src/main/java/abstractSyntaxTree/Class/IClass.java b/src/main/java/abstractSyntaxTree/Class/IClass.java index dae625a..e71c991 100644 --- a/src/main/java/abstractSyntaxTree/Class/IClass.java +++ b/src/main/java/abstractSyntaxTree/Class/IClass.java @@ -10,6 +10,7 @@ import java.util.List; public interface IClass { // visit method for code generation + void codeGen(ClassWriter cw) throws Exception; } diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index e51d87a..ed2750e 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -42,7 +42,8 @@ public class Program implements Node { HashMap> methodIdentifierAndParameter = new HashMap<>(); HashMap>> returnTypeAndMethod = new HashMap<>(); for (MethodDecl methodDecl : oneClass.methodDecls){ - methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters); + + methodIdentifierAndParameter.put(methodDecl.name, (List) methodDecl.parameters); returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); } methodContext.put(oneClass.name, returnTypeAndMethod); diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index a57b370..039abbb 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -25,6 +25,11 @@ public class BlockStatement extends AbstractType implements IStatement{ return null; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> 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; } diff --git a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java index 70a82b5..774e0b7 100644 --- a/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/EmptyStatement.java @@ -15,9 +15,16 @@ public class EmptyStatement extends AbstractType implements IStatement{ return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return typeCheck(methodContext, typeContext); + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { - return null; + TypeCheckResult result = new TypeCheckResult(); + result.type = "void"; + return result; } @Override diff --git a/src/main/java/abstractSyntaxTree/Statement/IStatement.java b/src/main/java/abstractSyntaxTree/Statement/IStatement.java index 43665a3..85918b5 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IStatement.java @@ -13,7 +13,11 @@ public interface IStatement extends Node { TypeCheckResult typeCheck() throws Exception; + TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception; + + TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception; + void codeGen(MethodVisitor mv, HashMap localVars) throws Exception; } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index 7ad152c..b28086b 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -40,6 +40,11 @@ public class IfElseStatement extends AbstractType implements IStatement{ return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index 0e63d40..b51fe74 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -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>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index 683813d..8e324d7 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -29,6 +29,11 @@ public class ReturnStatement extends AbstractType implements IStatement{ return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index 2e67c3b..b312be3 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -33,6 +33,11 @@ public class WhileStatement extends AbstractType implements IStatement { return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java index 281dcc8..8d96ae6 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/AssignStatementExpression.java @@ -34,11 +34,21 @@ public class AssignStatementExpression extends AbstractType implements IExpressi return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; } + @Override + public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + + } + @Override public void codeGen(MethodVisitor mv) throws Exception { left.codeGen(mv); diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index 1129f47..1e6598a 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -44,11 +44,21 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr return result; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; } + @Override + public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + + } + //Errors occur due to the change in parameter in the RefType class @Override public void codeGen(MethodVisitor mv) throws Exception { diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index 7350a9b..63b7743 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -15,11 +15,21 @@ public class NewStatementExpression extends AbstractType implements IExpression, return null; } + @Override + public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { + return null; + } + @Override public TypeCheckResult typeCheck(HashMap>>> methodContext, HashMap> typeContext) throws Exception { return null; } + @Override + public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + + } + @Override public void codeGen(MethodVisitor mv) throws Exception { throw new Exception("CodeGen not implemented for NewStatementExpression");