-
Notifications
You must be signed in to change notification settings - Fork 1
/
IRPrintInstruction.java
29 lines (25 loc) · 1.04 KB
/
IRPrintInstruction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class IRPrintInstruction implements IRInstruction {
TempVar expr;
Boolean newLine;
public IRPrintInstruction(TempVar expr, Boolean newLine) {
this.expr = expr;
this.newLine = newLine;
}
public String toString() {
String newLineStr = newLine ? "LN" : "";
return String.format("PRINT%s%s %s;", newLineStr, expr.type.toString(), expr.toString());
}
@Override
public String toBytecodeString() {
String newLineStr = newLine ? "ln" : "";
StringBuilder sb = new StringBuilder();
sb.append("getstatic java/lang/System/out Ljava/io/PrintStream;");
if (expr.type instanceof StringType) {
sb.append(String.format("\n aload %d", expr.number));
} else {
sb.append(String.format("\n %sload %d", expr.type.toString().toLowerCase(), expr.number));
}
sb.append(String.format("\n invokevirtual java/io/PrintStream/print%s(%s)V", newLineStr, expr.type.toSignString()));
return sb.toString();
}
}