method parameters
This commit is contained in:
parent
ab19751146
commit
9d86ab3a9f
@ -27,9 +27,21 @@ public class Compiler {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception{
|
public static void main(String[] args) throws Exception{
|
||||||
|
|
||||||
Path filePath = Paths.get("NichtHaskell/src/main/java/Input.java");
|
Path filePath = Paths.get("src/main/java/Input.java");
|
||||||
|
|
||||||
|
|
||||||
|
// todo remove this debug info
|
||||||
|
Path absolutePath = filePath.toAbsolutePath();
|
||||||
|
System.out.println("Full path: " + absolutePath);
|
||||||
|
|
||||||
|
String content;
|
||||||
|
try {
|
||||||
|
content = Files.readString(filePath);
|
||||||
|
}catch (java.nio.file.NoSuchFileException e){
|
||||||
|
System.out.println("File not found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
String content = Files.readString(filePath);
|
|
||||||
|
|
||||||
System.out.println("--- print content ---");
|
System.out.println("--- print content ---");
|
||||||
System.out.println(content);
|
System.out.println(content);
|
||||||
@ -91,16 +103,7 @@ public class Compiler {
|
|||||||
// System.out.println(refType.name);
|
// System.out.println(refType.name);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.returnType = "int";
|
|
||||||
List<IStatement> statementsList = abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.statements;
|
|
||||||
statementsList.remove(0);
|
|
||||||
statementsList.add(new LocalVarDecl("int", "localInt"));
|
|
||||||
statementsList.add(new LocalVarDecl("bool", "localBool"));
|
|
||||||
statementsList.add(new LocalVarDecl("char", "localChar"));
|
|
||||||
statementsList.add(new AssignStatementExpression("=", new LocalVarIdentifier("localInt"), new UnaryExpression("", new IntDatatype())));
|
|
||||||
statementsList.add(new AssignStatementExpression("=", new InstVarExpression(abstractSyntaxTree.classes.get(1), "instVarBool"), new UnaryExpression("instVarBool", new BoolDatatype())));
|
|
||||||
|
|
||||||
abstractSyntaxTree.classes.get(1).methodDecls.get(0).codeBlock.statements.add(new ReturnStatement(new UnaryExpression("", new IntDatatype())));
|
|
||||||
abstractSyntaxTree.typeCheck();
|
abstractSyntaxTree.typeCheck();
|
||||||
|
|
||||||
abstractSyntaxTree.codeGen();
|
abstractSyntaxTree.codeGen();
|
||||||
|
@ -6,6 +6,10 @@ class Example {
|
|||||||
}
|
}
|
||||||
class Example2 {
|
class Example2 {
|
||||||
boolean instVarBool;
|
boolean instVarBool;
|
||||||
int m(int n){return 1;}
|
int m(int n){
|
||||||
|
while(instVarBool){
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ public class MethodDecl implements Node {
|
|||||||
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", descriptor, false);
|
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", descriptor, false);
|
||||||
|
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
codeBlock.codeGen(mv, localVars);
|
//codeBlock.codeGen(mv, localVars);
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
|
|
||||||
//automatically computed max stack and max locals
|
//automatically computed max stack and max locals
|
||||||
@ -87,7 +87,7 @@ public class MethodDecl implements Node {
|
|||||||
MethodVisitor mv = cw.visitMethod(access, name, "([Ljava/lang/String;)V", null, null);
|
MethodVisitor mv = cw.visitMethod(access, name, "([Ljava/lang/String;)V", null, null);
|
||||||
|
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
codeBlock.codeGen(mv, localVars);
|
//codeBlock.codeGen(mv, localVars);
|
||||||
mv.visitInsn(Opcodes.RETURN);
|
mv.visitInsn(Opcodes.RETURN);
|
||||||
|
|
||||||
mv.visitMaxs(0, 0);
|
mv.visitMaxs(0, 0);
|
||||||
@ -97,7 +97,7 @@ public class MethodDecl implements Node {
|
|||||||
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(methodContext), null, null);
|
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(methodContext), null, null);
|
||||||
|
|
||||||
mv.visitCode();
|
mv.visitCode();
|
||||||
codeBlock.codeGen(mv, localVars);
|
//codeBlock.codeGen(mv, localVars);
|
||||||
|
|
||||||
// We have to check the return type to get the return opcode
|
// We have to check the return type to get the return opcode
|
||||||
// For methods which return an actual value, the return opcode is created in the method body to ensure the
|
// For methods which return an actual value, the return opcode is created in the method body to ensure the
|
||||||
|
@ -36,6 +36,30 @@ public class BlockStatement extends AbstractType implements IStatement {
|
|||||||
for (IStatement statement : statements) {
|
for (IStatement statement : statements) {
|
||||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
|
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
|
||||||
|
|
||||||
|
if (typeOfCurrentStatement.type.contains(",")) {
|
||||||
|
// else if has 2 returns, all code paths must retrun a value.
|
||||||
|
String[] substrings = typeOfCurrentStatement.type.split(",");
|
||||||
|
|
||||||
|
String firstType = substrings[0];
|
||||||
|
String secondType = substrings[1];
|
||||||
|
|
||||||
|
if(!firstType.equals(this.returnType) || !firstType.equals(this.returnType)){
|
||||||
|
if(!firstType.equals("void")){
|
||||||
|
throw new Exception("TypeCeck Exception: if paths return wrong type");
|
||||||
|
}
|
||||||
|
if(!secondType.equals("void")){
|
||||||
|
throw new Exception("TypeCeck Exception: else paths return wrong type");
|
||||||
|
}
|
||||||
|
boolean firstIsVoid = firstType.equals("void");
|
||||||
|
|
||||||
|
if(!firstIsVoid){
|
||||||
|
typeOfCurrentStatement.type = firstType;
|
||||||
|
}else{
|
||||||
|
typeOfCurrentStatement.type = secondType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!typeOfCurrentStatement.type.equals(this.returnType)) {
|
if (!typeOfCurrentStatement.type.equals(this.returnType)) {
|
||||||
if (!typeOfCurrentStatement.type.equals("void"))
|
if (!typeOfCurrentStatement.type.equals("void"))
|
||||||
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
|
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
|
||||||
|
@ -9,6 +9,7 @@ import org.objectweb.asm.*;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class IfElseStatement extends AbstractType implements IStatement{
|
public class IfElseStatement extends AbstractType implements IStatement{
|
||||||
IExpression condition;
|
IExpression condition;
|
||||||
@ -26,11 +27,11 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
|||||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||||
TypeCheckResult result = new TypeCheckResult();
|
TypeCheckResult result = new TypeCheckResult();
|
||||||
|
|
||||||
//TypeCheckResult conditionType = condition.typeCheck();
|
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
|
||||||
|
|
||||||
// if (!conditionType.equals("bool")) {
|
if (!conditionType.equals("bool")) {
|
||||||
// throw new IllegalArgumentException("should be boolean");
|
throw new IllegalArgumentException("should be boolean");
|
||||||
// }
|
}
|
||||||
|
|
||||||
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
|
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
|
||||||
TypeCheckResult elseStatementType = elseStatement.typeCheck(methodContext, typeContext, localVars);
|
TypeCheckResult elseStatementType = elseStatement.typeCheck(methodContext, typeContext, localVars);
|
||||||
@ -38,11 +39,17 @@ public class IfElseStatement extends AbstractType implements IStatement{
|
|||||||
if (!ifStatementType.equals(elseStatementType)) {
|
if (!ifStatementType.equals(elseStatementType)) {
|
||||||
throw new IllegalArgumentException("if and else have different types");
|
throw new IllegalArgumentException("if and else have different types");
|
||||||
}
|
}
|
||||||
|
if(ifStatementType.type != "void" && elseStatementType.type != "void"){
|
||||||
result.type = elseStatementType.type;
|
if(Objects.equals(ifStatementType.type, elseStatementType.type)){
|
||||||
|
throw new Exception("TypeCeck Exception: If and else return different not-void types");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.type = ifStatementType.type + "," + elseStatementType.type;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||||
|
|
||||||
|
@ -25,11 +25,11 @@ public class IfStatement extends AbstractType implements IStatement{
|
|||||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||||
TypeCheckResult result = new TypeCheckResult();
|
TypeCheckResult result = new TypeCheckResult();
|
||||||
|
|
||||||
//TypeCheckResult conditionType = condition.typeCheck();
|
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
|
||||||
|
|
||||||
// if (!conditionType.equals("bool")) {
|
if (!conditionType.equals("bool")) {
|
||||||
// throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
|
throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
|
||||||
// }
|
}
|
||||||
|
|
||||||
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
|
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
|
||||||
result.type = ifStatementType.type;
|
result.type = ifStatementType.type;
|
||||||
|
@ -23,15 +23,18 @@ public class WhileStatement extends AbstractType implements IStatement {
|
|||||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||||
TypeCheckResult result = new TypeCheckResult();
|
TypeCheckResult result = new TypeCheckResult();
|
||||||
|
|
||||||
// TypeCheckResult conditionType = condition.typeCheck();
|
// check condition
|
||||||
//
|
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
|
||||||
// if (!conditionType.equals("bool")) {
|
if (!conditionType.equals("bool")) {
|
||||||
// throw new IllegalArgumentException("Expected boolean");
|
throw new IllegalArgumentException("Expected boolean");
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// check code block
|
||||||
TypeCheckResult statementType = statement.typeCheck(methodContext, typeContext, localVars);
|
TypeCheckResult statementType = statement.typeCheck(methodContext, typeContext, localVars);
|
||||||
|
|
||||||
|
// set result
|
||||||
result.type = statementType.type;
|
result.type = statementType.type;
|
||||||
|
setTypeCheckResult(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ import org.objectweb.asm.*;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class AssignStatementExpression extends AbstractType implements IExpression, IStatement {
|
public class AssignStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||||
public String operator;
|
public String operator;
|
||||||
@ -51,12 +52,10 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||||
if (left instanceof VarRefExpression varRef) {
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeCheckResult typeCheck() throws Exception {
|
public TypeCheckResult typeCheck() throws Exception {
|
||||||
return null;
|
return null;
|
||||||
@ -117,3 +116,4 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
@ -51,6 +51,11 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) 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
|
||||||
// I need the methodContext here to get the method descriptor
|
// I need the methodContext here to get the method descriptor
|
||||||
@ -66,10 +71,10 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
|||||||
for (MethodDecl methodDecl : methodDecls) {
|
for (MethodDecl methodDecl : methodDecls) {
|
||||||
if (methodDecl.name.equals(methodName)) {
|
if (methodDecl.name.equals(methodName)) {
|
||||||
//Get the method descriptor
|
//Get the method descriptor
|
||||||
descriptor = methodDecl.getMethodDescriptor(methodContext);
|
// descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
|
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Load this onto the stack
|
// Load this onto the stack
|
||||||
@ -86,9 +91,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
|
|||||||
for (MethodDecl methodDecl : methodDecls) {
|
for (MethodDecl methodDecl : methodDecls) {
|
||||||
if (methodDecl.name.equals(methodName)) {
|
if (methodDecl.name.equals(methodName)) {
|
||||||
//Get the method descriptor
|
//Get the method descriptor
|
||||||
descriptor = methodDecl.getMethodDescriptor(methodContext);
|
// descriptor = methodDecl.getMethodDescriptor(methodContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
|
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import abstractSyntaxTree.Statement.IStatement;
|
|||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
|
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
|
||||||
@ -19,12 +20,14 @@ public class NewStatementExpression extends AbstractType implements IExpression,
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
|
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void codeGen(MethodVisitor mv) throws Exception {
|
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
|
||||||
throw new Exception("CodeGen not implemented for NewStatementExpression");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user