Merge remote-tracking branch 'origin/master'

This commit is contained in:
Jochen Seyfried 2024-05-31 13:53:19 +02:00
commit bc984a89e1
6 changed files with 17 additions and 9 deletions

View File

@ -104,6 +104,7 @@ public class Compiler {
// } // }
abstractSyntaxTree.classes.get(2).methodDecls.get(0).codeBlock.returnType = "int";
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheck();
abstractSyntaxTree.codeGen(); abstractSyntaxTree.codeGen();

View File

@ -6,16 +6,13 @@ class Example {
int i; int i;
boolean b; boolean b;
char c; char c;
void callM(){
Example2 example2 = new Example2();
example2.m(1);
}
} }
class Example2 { class Example2 {
boolean instVarBool; boolean instVarBool;
int m(int n){ int m(int n){
boolean a = this.instVarBool; int localA;
int localB;
return n; return n;
} }
} }

View File

@ -35,6 +35,10 @@ public class MethodDecl implements Node {
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 Exception {
// jede methode als block statement aufrufen und jede neue locale varibale in localvars schreiben // jede methode als block statement aufrufen und jede neue locale varibale in localvars schreiben
List<Parameter> parametersList = parameters.parameterList;
for(Parameter parameter : parametersList){
localVars.put(parameter.identifier, parameter.type);
}
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
codeBlock.typeCheck(methodContext, typeContext, localVars); codeBlock.typeCheck(methodContext, typeContext, localVars);

View File

@ -29,7 +29,7 @@ public class LocalVarIdentifier implements IExpression{
if (localVars.containsKey(identifier)) { if (localVars.containsKey(identifier)) {
result.type = localVars.get(identifier); result.type = localVars.get(identifier);
} else { } else {
throw new Exception("TypeCheck Exception: local var does not exist"); throw new Exception("TypeCheck Exception: Local var " + identifier + " does not exist.");
} }
return result; return result;
} }

View File

@ -36,10 +36,16 @@ public class BlockStatement extends AbstractType implements IStatement {
} }
for (IStatement statement : statements) { for (IStatement statement : statements) {
// todo remove later if there are no null statement any more // todo remove later if there are no null statement any more
if (statement != null) { if (statement != null) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
if (typeOfCurrentStatement.type.contains(",")) { if (typeOfCurrentStatement.type.contains(",")) {
// else if has 2 returns, all code paths must retrun a value. // else if has 2 returns, all code paths must retrun a value.
String[] substrings = typeOfCurrentStatement.type.split(","); String[] substrings = typeOfCurrentStatement.type.split(",");
@ -49,10 +55,10 @@ public class BlockStatement extends AbstractType implements IStatement {
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) { if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
if (!firstType.equals("void")) { if (!firstType.equals("void")) {
throw new Exception("TypeCeck Exception: if paths return wrong type"); throw new Exception("TypeCheck Exception: if paths return wrong type");
} }
if (!secondType.equals("void")) { if (!secondType.equals("void")) {
throw new Exception("TypeCeck Exception: else paths return wrong type"); throw new Exception("TypeCheck Exception: else paths return wrong type");
} }
boolean firstIsVoid = firstType.equals("void"); boolean firstIsVoid = firstType.equals("void");

View File

@ -68,7 +68,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
public Node visitConstuctorDecl(DecafParser.ConstuctorDeclContext ctx) { public Node visitConstuctorDecl(DecafParser.ConstuctorDeclContext ctx) {
String name = ctx.Identifier().getText(); String name = ctx.Identifier().getText();
ParameterList parameterList = (ParameterList) visit(ctx.parameterList()); ParameterList parameterList = (ParameterList) visit(ctx.parameterList());
return new MethodDecl("", "", name, parameterList, new BlockStatement(new ArrayList<>(), "void")); return new MethodDecl("", null, name, parameterList, new BlockStatement(new ArrayList<>(), "void"));
} }
@Override @Override