-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVMWriter.java
111 lines (98 loc) · 2.26 KB
/
VMWriter.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.File;
import java.io.PrintWriter;
/**
* This module features a set of simple routines for writing VM commands into the output file.
*
* @author Maarten Derks
*/
class VMWriter {
private final PrintWriter pw;
/**
* Creates a new output <code>.vm</code> file / stream, and prepares it for writing.
*
* @param out output file
*/
VMWriter(File out) throws Exception {
pw = new PrintWriter(out);
}
/**
* Writes a VM <code>push</code> command.
*
* @param segment
* @param index
*/
void writePush(Segment segment, int index) {
pw.println("push " + segment.toString().toLowerCase() + " " + index);
}
/**
* Writes a VM <code>pop</code> command.
*
* @param segment
* @param index
*/
void writePop(Segment segment, int index) {
pw.println("pop " + segment.toString().toLowerCase() + " " + index);
}
/**
* Writes a VM arithmetic-logical command.
*
* @param command
*/
void writeArithmetic(Command command) {
pw.println(command.toString().toLowerCase());
}
/**
* Writes a VM <code>label</code> command.
*
* @param label
*/
void writeLabel(String label) {
pw.println("label " + label);
}
/**
* Writes a VM <code>goto</code> command.
*
* @param label
*/
void writeGoto(String label) {
pw.println("goto " + label);
}
/**
* Writes a VM <code>if-goto</code> command.
*
* @param label
*/
void writeIf(String label) {
pw.println("if-goto " + label);
}
/**
* Writes a VM <code>call</code> command.
*
* @param name
* @param nVars
*/
void writeCall(String name, int nVars) {
pw.println("call " + name + " " + nVars);
}
/**
* Writes a VM <code>function</code> command.
*
* @param name
* @param nVars
*/
void writeFunction(String name, int nVars) {
pw.println("function " + name + " " + nVars);
}
/**
* Writes a VM <code>return</code> command.
*/
void writeReturn() {
pw.println("return");
}
/**
* Closes the output file / stream.
*/
void close() {
pw.close();
}
}