Compare commits

...

3 Commits

Author SHA1 Message Date
Krauß, Josefine
35fba57efa Merge remote-tracking branch 'origin/master'
# Conflicts:
#	src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java
#	src/main/java/abstractSyntaxTree/Statement/IfStatement.java
#	src/main/java/abstractSyntaxTree/Statement/WhileStatement.java
2024-05-14 15:19:26 +02:00
Krauß, Josefine
6148b46063 parameterlist, typecheck 2024-05-14 15:18:18 +02:00
Krauß, Josefine
a59950e186 parameterlist, typecheck 2024-05-14 15:15:58 +02:00
15 changed files with 89 additions and 68 deletions

View File

@@ -91,6 +91,16 @@ public class Compiler {
// 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.codeGen();

View File

@@ -6,8 +6,6 @@ class Example {
}
class Example2 {
boolean instVarBool;
void m(int i){
return;
}
int m(int n){return 1;}
}

View File

@@ -14,7 +14,7 @@ import java.util.List;
import java.util.Map;
import java.util.Stack;
public class MethodDecl implements IClass, Node {
public class MethodDecl implements Node {
//Class Name
public String classThatContainsMethod;

View File

@@ -3,6 +3,7 @@ package abstractSyntaxTree.Class;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
@@ -29,7 +30,7 @@ public class RefType extends AbstractType implements Node {
this.hasMain = hasMain;
}
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext,
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext,
HashMap<String, HashMap<String, String>> typeContext) throws Exception {
TypeCheckResult result = new TypeCheckResult();
@@ -83,7 +84,7 @@ public class RefType extends AbstractType implements Node {
}
for (MethodDecl method : methodDecls) {
method.codeGen(cw);
// method.codeGen(cw);
}
cw.visitEnd();
}

View File

@@ -3,9 +3,11 @@ package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult;
import TypeCheck.TypeCheckHelper;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.*;
import java.beans.Expression;
import java.util.HashMap;
import java.util.Objects;
public class BinaryExpression extends AbstractType implements IExpression{
@@ -37,7 +39,7 @@ public class BinaryExpression extends AbstractType implements IExpression{
case "<=":
case ">=":
case "!=":
result.type = helper.upperBound(leftType.type, rightType.type);
result.type = helper.upperBound(leftType.type, rightType.type);
break;
case "-":
@@ -58,6 +60,11 @@ public class BinaryExpression extends AbstractType implements IExpression{
}
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
return null;
}
@Override
public void codeGen(MethodVisitor mv) throws Exception {
// Label for the jump instruction
@@ -86,11 +93,11 @@ public class BinaryExpression extends AbstractType implements IExpression{
mv.visitJumpInsn(Opcodes.IFNE, operationTrue);
break;
case "==":
// Keep in mind that only primitive types are allowed in this case (at this time)
case "==":
// Keep in mind that only primitive types are allowed in this case (at this time)
left.codeGen(mv);
right.codeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression
break;
@@ -167,4 +174,4 @@ public class BinaryExpression extends AbstractType implements IExpression{
mv.visitLabel(expressionEnd);
}
}
}

View File

@@ -1,11 +1,15 @@
package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
public interface IExpression {
// typeCheck method
TypeCheckResult typeCheck() throws Exception;
//TypeCheckResult typeCheck() throws Exception;
TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception;
// visit method for code generation
void codeGen(MethodVisitor mv) throws Exception;

View File

@@ -2,9 +2,12 @@ package abstractSyntaxTree.Expression;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
public class InstVarExpression implements IExpression{
//TODO: We have to decide upon more parameters and where they come from, for
@@ -12,12 +15,16 @@ public class InstVarExpression implements IExpression{
private RefType classRef;
private String fieldName;
/* public InstVarExpression(RefType classRef, String fieldName){
public InstVarExpression(RefType classRef, String fieldName){
this.classRef = classRef;
this.fieldName = fieldName;
}*/
}
@Override
public TypeCheckResult typeCheck() 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 {
//todo
return null;
}

View File

@@ -20,7 +20,7 @@ import java.util.jar.JarOutputStream;
public class Program implements Node {
public List<RefType> classes;
public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
public HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext; // (class, (returntype, (identifier, parameterList)))
public HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext; // (class, (returntype, (identifier, parameter)))
public Program(List<RefType> classes){
this.classes = classes;
@@ -41,11 +41,11 @@ public class Program implements Node {
typeContext.put(oneClass.name, classVars);
// build method context
HashMap<String, List<String>> methodIdentifierAndParameter = new HashMap<>();
HashMap<String, HashMap<String, List<String >>> returnTypeAndMethod = new HashMap<>();
HashMap<String, ParameterList> methodIdentifierAndParameter = new HashMap<>();
HashMap<String, HashMap<String, ParameterList>> returnTypeAndMethod = new HashMap<>();
for (MethodDecl methodDecl : oneClass.methodDecls){
methodIdentifierAndParameter.put(methodDecl.name, (List<String>) methodDecl.parameters);
methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters);
returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter);
}
methodContext.put(oneClass.name, returnTypeAndMethod);
@@ -66,7 +66,7 @@ public class Program implements Node {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, oneClass.name, null, "java/lang/Object", null);
oneClass.codeGen(cw, methodContext);
// oneClass.codeGen(cw);
cw.visitEnd();
byte[] bytecode = cw.toByteArray();
@@ -102,4 +102,4 @@ public class Program implements Node {
}
*/
}
}
}

View File

@@ -25,11 +25,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 {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
//TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("should be boolean");
}
// if (!conditionType.equals("bool")) {
// throw new IllegalArgumentException("should be boolean");
// }
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
TypeCheckResult elseStatementType = elseStatement.typeCheck(methodContext, typeContext, localVars);

View File

@@ -24,11 +24,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 {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
//TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
}
// if (!conditionType.equals("bool")) {
// throw new Exception("TypeCheck Exception: Condition of If-Statement should be bool.");
// }
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
result.type = ifStatementType.type;

View File

@@ -22,11 +22,11 @@ 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 {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("Expected boolean");
}
// TypeCheckResult conditionType = condition.typeCheck();
//
// if (!conditionType.equals("bool")) {
// throw new IllegalArgumentException("Expected boolean");
// }
TypeCheckResult statementType = statement.typeCheck(methodContext, typeContext, localVars);
@@ -48,6 +48,7 @@ public class WhileStatement extends AbstractType implements IStatement {
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
statement.codeGen(mv, blockLocalVars);
//statement.codeGen(mv);
//TODO: If the block ends with a return statement, we might have to pop it from the stack
// So the next iteration starts with a clean stack
mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop

View File

@@ -29,23 +29,23 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
TypeCheckHelper helper = new TypeCheckHelper();
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult leftType = left.typeCheck();
if(leftType == null){ //left expression is the identifier of a var
leftType = new TypeCheckResult();
if (left instanceof LocalVarIdentifier) {
LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left;
String identifier = localVarIdentifier.getIdentifier();
leftType.type = localVars.get(identifier);
}
// not local var
}
TypeCheckResult rightType = right.typeCheck();
TypeCheckResult leftType;
String upperbound = helper.upperBound(leftType.type, rightType.type);
if (Objects.equals(upperbound, leftType.type)) {
result.type = leftType.type;
if (left instanceof LocalVarIdentifier) {
leftType = new TypeCheckResult();
LocalVarIdentifier localVarIdentifier = (LocalVarIdentifier) left;
String identifier = localVarIdentifier.getIdentifier();
leftType.type = localVars.get(identifier);
}else{
//leftType = left.typeCheck();
}
setTypeCheckResult(result);
// TypeCheckResult rightType = right.typeCheck();
//
// String upperbound = helper.upperBound(leftType.type, rightType.type);
// if (Objects.equals(upperbound, leftType.type)) {
// result.type = leftType.type;
// }
// setTypeCheckResult(result);
return result;
}
@@ -57,7 +57,6 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
}
@Override
public TypeCheckResult typeCheck() throws Exception {
return null;
}
@@ -67,14 +66,14 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
left.codeGen(mv);
right.codeGen(mv);
if (left instanceof VarRefExpression varRef) {
//TODO: Implement the handling of a variable reference --> I need a list of local variables
// for that to determine if the variable is a local or field variable
} else if (left instanceof InstVarExpression instVar) {
mv.visitInsn(Opcodes.DUP_X1);
// We now again need the owner (class reference), name (of the Field in the owner) and type of the field
//mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
}
// if (left instanceof VarRefExpression varRef) {
// //TODO: Implement the handling of a variable reference --> I need a list of local variables
// // for that to determine if the variable is a local or field variable
// } else if (left instanceof InstVarExpression instVar) {
// mv.visitInsn(Opcodes.DUP_X1);
//
// // We now again need the owner (class reference), name (of the Field in the owner) and type of the field
// //mv.visitFieldInsn(Opcodes.PUTFIELD, instVar.className, instVar.varName, instVar.type);
// }
}
}

View File

@@ -25,7 +25,6 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
this.arguments = arguments;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
@@ -61,7 +60,7 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
public void codeGen(MethodVisitor mv) throws Exception {
//Generate Bytecode for the receiver
if(classThatHasTheMethodIfNotThis != null){
classThatHasTheMethodIfNotThis.codeGen(new ClassWriter(ClassWriter.COMPUTE_FRAMES));
// classThatHasTheMethodIfNotThis.codeGen(new ClassWriter(ClassWriter.COMPUTE_FRAMES));
} else {
mv.visitVarInsn(Opcodes.ALOAD, 0);
}

View File

@@ -11,11 +11,6 @@ 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

View File

@@ -170,7 +170,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
@Override
public Node visitAssign(DecafParser.AssignContext ctx) {
return new AssignStatementExpression();
return new AssignStatementExpression("", null, null);
}
@Override