Compare commits

...

2 Commits

Author SHA1 Message Date
Jochen Seyfried
0fcea195de Updated If and WhileStatement 2024-05-14 14:54:33 +02:00
Jochen Seyfried
f2260c7a73 Deleted ICLass and updated the MethodDecl codeGen 2024-05-14 14:51:59 +02:00
4 changed files with 14 additions and 4 deletions

View File

@ -45,17 +45,19 @@ public class IfElseStatement extends AbstractType implements IStatement{
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
Label conditionFalse = new Label();
Label statementEnd = new Label();
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock
ifStatement.codeGen(mv, blockLocalVars); //If the condition is true, execute the ifBlock
mv.visitJumpInsn(Opcodes.GOTO, statementEnd); //Jump to the end of the if-else statement
mv.visitLabel(conditionFalse);
elseStatement.codeGen(mv); //If the condition is false, execute the elseBlock
elseStatement.codeGen(mv, blockLocalVars); //If the condition is false, execute the elseBlock
mv.visitLabel(statementEnd); //End of the if-else statement

View File

@ -38,12 +38,14 @@ public class IfStatement extends AbstractType implements IStatement{
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
Label conditionFalse = new Label();
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.codeGen(mv);
ifStatement.codeGen(mv, blockLocalVars);
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
}

View File

@ -36,6 +36,9 @@ public class WhileStatement extends AbstractType implements IStatement {
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
Label conditionFalse = new Label();
Label LoopStart = new Label();
@ -44,7 +47,7 @@ public class WhileStatement extends AbstractType implements IStatement {
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
statement.codeGen(mv);
statement.codeGen(mv, blockLocalVars);
//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

@ -51,6 +51,9 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
if (left instanceof VarRefExpression varRef) {
}
}