Skip to content

Commit 3f54988

Browse files
committed
update PA3: use our decaf.lowlevel
1 parent 37d4902 commit 3f54988

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4237
-1082
lines changed

build.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
1515

1616
libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2"
1717

18+
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
19+
libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.9"
20+
1821
// antlr4
1922

2023
enablePlugins(Antlr4Plugin)
@@ -28,3 +31,8 @@ antlr4GenVisitor in Antlr4 := true // default: false
2831
// assembly
2932

3033
assemblyOutputPath in assembly := file("target/decaf.jar")
34+
35+
// java 12
36+
37+
javacOptions in Compile += "--enable-preview"
38+
javacOptions in Compile ++= Seq("-source", "12")
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package decaf.lowlevel;
2+
3+
import decaf.lowlevel.instr.NativeInstr;
4+
import decaf.lowlevel.label.Label;
5+
6+
/**
7+
* Assembly code pretty printer.
8+
*/
9+
public class AsmCodePrinter {
10+
private StringBuilder sb = new StringBuilder();
11+
12+
protected final String INDENTS = " ";
13+
14+
protected final String END_LINE = "\n";
15+
16+
protected final String COMMENT_PROMPT = "#";
17+
18+
/**
19+
* Format print, with indents.
20+
*
21+
* @param fmt format
22+
* @param args arguments
23+
*/
24+
public void print(String fmt, Object... args) {
25+
sb.append(INDENTS);
26+
sb.append(String.format(fmt, args));
27+
}
28+
29+
/**
30+
* Format print a line, with indents.
31+
*
32+
* @param fmt format
33+
* @param args arguments
34+
*/
35+
public void println(String fmt, Object... args) {
36+
sb.append(INDENTS);
37+
sb.append(String.format(fmt, args));
38+
sb.append(END_LINE);
39+
}
40+
41+
/**
42+
* Simply print a newline.
43+
*/
44+
public void println() {
45+
sb.append(END_LINE);
46+
}
47+
48+
/**
49+
* Print a label.
50+
*
51+
* @param label label
52+
*/
53+
public void printLabel(Label label) {
54+
sb.append(label.name);
55+
sb.append(":");
56+
sb.append(END_LINE);
57+
}
58+
59+
/**
60+
* Print a label with comment.
61+
*
62+
* @param label label
63+
* @param comment comment
64+
*/
65+
public void printLabel(Label label, String comment) {
66+
sb.append(label.name);
67+
sb.append(":");
68+
sb.append(" ").append(COMMENT_PROMPT).append(' ').append(comment).append(END_LINE);
69+
}
70+
71+
/**
72+
* Print an instruction.
73+
*
74+
* @param instr instruction
75+
*/
76+
public void printInstr(NativeInstr instr) {
77+
if (instr.isLabel()) {
78+
sb.append(instr.label);
79+
sb.append(":");
80+
} else {
81+
sb.append(INDENTS);
82+
sb.append(instr);
83+
}
84+
85+
sb.append(END_LINE);
86+
}
87+
88+
/**
89+
* Print an instruction, with comment.
90+
*
91+
* @param instr instruction
92+
* @param comment comment
93+
*/
94+
public void printInstr(NativeInstr instr, String comment) {
95+
if (instr.isLabel()) {
96+
sb.append(instr.label);
97+
sb.append(":");
98+
} else {
99+
sb.append(INDENTS);
100+
sb.append(instr);
101+
}
102+
103+
sb.append(" ").append(COMMENT_PROMPT).append(' ').append(comment).append(END_LINE);
104+
}
105+
106+
/**
107+
* Print a comment.
108+
*
109+
* @param comment comment
110+
*/
111+
public void printComment(String comment) {
112+
sb.append(INDENTS);
113+
sb.append(COMMENT_PROMPT).append(' ');
114+
sb.append(comment);
115+
sb.append(END_LINE);
116+
}
117+
118+
/**
119+
* Finish printing. Get the plain text of the assembly code.
120+
*
121+
* @return assembly code
122+
*/
123+
public String close() {
124+
return sb.toString();
125+
}
126+
}

0 commit comments

Comments
 (0)