interface changes and some typecheck for code block. also some minor changes in blockstatement structure
This commit is contained in:
parent
371755f390
commit
4f2599d053
4
.idea/compiler.xml
generated
4
.idea/compiler.xml
generated
@ -6,10 +6,8 @@
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="NichtHaskell" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
<bytecodeTargetLevel>
|
||||
<module name="NichtHaskell" target="20" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
</project>
|
@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<module version="4">
|
||||
<component name="AdditionalModuleElements">
|
||||
<content url="file://$MODULE_DIR$" dumb="true">
|
||||
<sourceFolder url="file://$MODULE_DIR$/Source" isTestSource="false" />
|
||||
|
@ -2,6 +2,7 @@ import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.BlockStatement;
|
||||
import gen.DecafLexer;
|
||||
import gen.DecafParser;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
@ -51,8 +52,26 @@ public class Compiler {
|
||||
System.out.println(tree);
|
||||
|
||||
|
||||
|
||||
Program abstractSyntaxTree = new Program(new ArrayList<>());
|
||||
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 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));
|
||||
|
||||
|
||||
|
||||
abstractSyntaxTree.typeCheck();
|
||||
|
||||
|
@ -2,11 +2,13 @@ package abstractSyntaxTree.Class;
|
||||
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Program;
|
||||
import abstractSyntaxTree.Statement.BlockStatement;
|
||||
import abstractSyntaxTree.Statement.IStatement;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class MethodDecl implements IClass {
|
||||
|
||||
@ -14,11 +16,11 @@ public class MethodDecl implements IClass {
|
||||
public String name;
|
||||
public List<String> parameters;
|
||||
public String returnType;
|
||||
public List<IStatement> codeBlock;
|
||||
public BlockStatement codeBlock;
|
||||
|
||||
// save localvars
|
||||
public Map<String, String> localVars;
|
||||
|
||||
public MethodDecl(String classThatContainsMethod, String returnType, String name, List<String> parameters, List<IStatement> codeBlock){
|
||||
public MethodDecl(String classThatContainsMethod, String returnType, String name, List<String> parameters, BlockStatement codeBlock){
|
||||
this.classThatContainsMethod = classThatContainsMethod;
|
||||
this.returnType = returnType;
|
||||
this.name = name;
|
||||
@ -27,8 +29,9 @@ public class MethodDecl implements IClass {
|
||||
}
|
||||
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
// write localvars
|
||||
// jede methode als block statement aufrufen
|
||||
// jede methode als block statement aufrufen und jede neue locale varibale in localvars schreiben
|
||||
|
||||
codeBlock.typeCheck(methodContext, typeContext);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -27,29 +27,35 @@ public class Program {
|
||||
|
||||
|
||||
public TypeCheckResult typeCheck() throws Exception{
|
||||
this.typeContext = new HashMap<>();
|
||||
this.methodContext = new HashMap<>();
|
||||
|
||||
for(RefType oneClass : classes){
|
||||
|
||||
// build type context
|
||||
this.typeContext = new HashMap<>();
|
||||
|
||||
HashMap<String, String> classVars = new HashMap<>();
|
||||
for (FieldDecl fieldDecl: oneClass.fieldDecls){
|
||||
classVars.put(fieldDecl.type, fieldDecl.identifier);
|
||||
}
|
||||
typeContext.put(oneClass.name, classVars);
|
||||
|
||||
// build method context
|
||||
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);
|
||||
returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter);
|
||||
}
|
||||
this.methodContext = new HashMap<>();
|
||||
methodContext.put(oneClass.name, returnTypeAndMethod);
|
||||
}
|
||||
|
||||
// typecheck each class
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
for(RefType oneClass : classes){
|
||||
oneClass.typeCheck(methodContext, typeContext);
|
||||
}
|
||||
return null;
|
||||
result.type = "program";
|
||||
return result;
|
||||
}
|
||||
|
||||
public void codeGen() throws Exception{
|
||||
|
@ -11,32 +11,37 @@ public class BlockStatement extends AbstractType implements IStatement{
|
||||
|
||||
//We will need a parameter which holds the symbol table
|
||||
HashMap<String, String > localVars;
|
||||
HashMap<String, String > typeIndentifierTable; // from program
|
||||
String returnType;
|
||||
List<IStatement> statements;
|
||||
// do we need expression, statementexpression
|
||||
|
||||
public BlockStatement(List<IStatement> statements, HashMap<String, String> localVars, HashMap<String, String> typeIndentifierTable){
|
||||
|
||||
public BlockStatement(List<IStatement> statements, String returnType){
|
||||
this.statements = statements;
|
||||
this.returnType = returnType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() 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();
|
||||
|
||||
if(statements.size() == 0){
|
||||
result.type = "void";
|
||||
}
|
||||
|
||||
TypeCheckResult blockType = null;
|
||||
TypeCheckResult blockType = new TypeCheckResult();
|
||||
for (IStatement statement : statements) {
|
||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck();
|
||||
|
||||
if (blockType == null) {
|
||||
blockType = typeOfCurrentStatement;
|
||||
} else if (!typeOfCurrentStatement.equals(blockType) && !blockType.equals("void")) {
|
||||
throw new IllegalArgumentException("different statement types");
|
||||
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
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,9 @@ import TypeCheck.TypeCheckResult;
|
||||
import TypeCheck.AbstractType;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class EmptyStatement extends AbstractType implements IStatement{
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() throws Exception {
|
||||
@ -12,6 +15,11 @@ 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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
//An empty statement does not generate any code
|
||||
|
@ -3,11 +3,16 @@ package abstractSyntaxTree.Statement;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public interface IStatement {
|
||||
|
||||
|
||||
|
||||
TypeCheckResult typeCheck() 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) throws Exception;
|
||||
}
|
||||
|
@ -5,6 +5,9 @@ import TypeCheck.AbstractType;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class IfElseStatement extends AbstractType implements IStatement{
|
||||
IExpression condition;
|
||||
IStatement ifStatement;
|
||||
@ -37,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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
|
||||
|
@ -5,6 +5,9 @@ import TypeCheck.AbstractType;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class IfStatement extends AbstractType implements IStatement{
|
||||
IExpression condition;
|
||||
|
||||
@ -30,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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
|
||||
|
@ -5,6 +5,9 @@ import TypeCheck.AbstractType;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class ReturnStatement extends AbstractType implements IStatement{
|
||||
IExpression expression;
|
||||
|
||||
@ -26,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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
//TODO: We do not differentiate between primitive types and reference types
|
||||
// This is a problem at "BinaryExpression" and here because we need to know the type to return
|
||||
|
@ -5,6 +5,9 @@ import TypeCheck.AbstractType;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class WhileStatement extends AbstractType implements IStatement {
|
||||
IExpression condition;
|
||||
IStatement statement;
|
||||
@ -30,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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
Label conditionFalse = new Label();
|
||||
|
@ -9,6 +9,8 @@ import abstractSyntaxTree.Expression.VarRefExpression;
|
||||
import abstractSyntaxTree.Statement.IStatement;
|
||||
import org.objectweb.asm.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AssignStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
@ -32,6 +34,11 @@ 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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
left.codeGen(mv);
|
||||
|
@ -10,6 +10,7 @@ import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class MethodCallStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
@ -43,6 +44,11 @@ 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) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
//Errors occur due to the change in parameter in the RefType class
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv) throws Exception {
|
||||
|
@ -6,12 +6,20 @@ import abstractSyntaxTree.Expression.IExpression;
|
||||
import abstractSyntaxTree.Statement.IStatement;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||
@Override
|
||||
public TypeCheckResult typeCheck() 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) throws Exception {
|
||||
throw new Exception("CodeGen not implemented for NewStatementExpression");
|
||||
|
Loading…
Reference in New Issue
Block a user