add System out

This commit is contained in:
404Simon 2024-05-20 23:06:06 +02:00
parent 09a327bdee
commit d55217e85a
3 changed files with 41 additions and 9 deletions

View File

@ -98,6 +98,12 @@ public class TypedBlock implements TypedNode {
typedMethodCall.typeCheck(typedProgram);
stmts.add(typedMethodCall);
}
if (stmt instanceof Print printStmt) {
TypedPrint typedPrint = new TypedPrint(typedProgram, printStmt);
typedPrint.typeCheck(typedProgram);
stmts.add(typedPrint);
}
}
this.typeCheck(typedProgram);
}

View File

@ -0,0 +1,34 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Print;
import de.maishai.typedast.*;
import org.objectweb.asm.Opcodes;
import java.util.List;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
public class TypedPrint implements TypedStatement {
private TypedExpression value;
private Type type;
public TypedPrint(TypedProgram typedProgram, Print untyped) {
value = convertExpression(typedProgram, untyped.value());
type = Type.VOID;
}
@Override
public void codeGen(MethodContext ctx) {
ctx.getMv().visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
ctx.pushAnonToStack();
value.codeGen(ctx);
ctx.getMv().visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", CodeGenUtils.generateDescriptor(List.of(value.getType()), type), false);
ctx.popStack();
ctx.popStack();
}
@Override
public Type typeCheck(TypedProgram typedProgram) {
return null;
}
}

View File

@ -6,15 +6,7 @@ public class ClassCanBeBytecoded {
public int test(int var1) {
for (int i = 0; i < 12; i = i + 1) {
var1 = this.c.c.c.c.x + this.x;
if (var1 * 3 == 0) {
continue;
}
while (var1 < 10) {
var1 = var1 + 1;
}
}
print(var1);
return var1;
}
}