forked from VipinindKumar/vm-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java
142 lines (126 loc) · 3.87 KB
/
Parser.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**Handles the parsing of a single .vm file, and
* encapsulates access to the input code.
* It reads VM commands, parses them, and
* provides convenient access to their components
*
**/
import java.io.*;
public class Parser {
public String command;
public String type;
public BufferedReader in;
// Opens the file and get ready to parse it
public Parser(File inFile) {
try {
in = new BufferedReader(new FileReader(inFile));
}
catch (IOException e) {
System.out.println(e);
}
}
// are there more commands in the file?
public boolean hasMoreCommands() {
try {
while ((command = in.readLine()) != null) {
// skip empty lines
if (command.isEmpty()) {
continue;
}
type = command.trim().split(" ")[0];
// check for one line and multi line comments and skip past them
command = command.trim().toLowerCase();
// Single line comments
if (command.charAt(0) == '/' && command.charAt(1) == '/') {
continue;
}
// Multi line comments
else if (command.charAt(0) == '/' && command.charAt(1) == '*') {
while ((command = in.readLine()) != null) {
int len = command.length();
if (command.charAt(len - 1) == '/' && command.charAt(len - 2) == '*') {
break;
}
continue;
}
continue;
}
// trim end of line comments away
if (command.contains("//")) {
command = command.split("//")[0].trim();
}
return true;
}
// if there's no more commands in the file
in.close();
}
catch (IOException e) {
System.out.println(e);
}
return false;
}
// raed the next command form input
// and makes it current command
// called if hasMoreCommands() return true
public String advance() {
return command;
}
/** return the type of VM command
*
* C_ARITHMETIC, C_PUSH, C_POP, C_LABEL, C_GOTO,
* C_IF, C_FUNCTION, C_RETURN, C_CALL
*
**/
public String commandType() {
if (type.equals("push")) {
type = "C_PUSH";
}
else if (type.equals("pop")) {
type = "C_POP";
}
else if (type.equals("label")) {
type = "C_LABEL";
}
else if (type.equals("goto")) {
type = "C_GOTO";
}
else if (type.equals("if-goto")) {
type = "C_IF";
}
else if (type.equals("function")) {
type = "C_FUNCTION";
}
else if (type.equals("return")) {
type = "C_RETURN";
}
else if (type.equals("call")) {
type = "C_CALL";
}
else {
type = "C_ARITHMETIC";
}
return type;
}
/** returns first argument of the command
*
* if command type is C_ARITHMETIC return command itself
* Should not be called if command is C_RETURN
*
**/
public String arg1() {
if (type.equals("C_ARITHMETIC")) {
return command;
}
else {
return command.split(" ")[1];
}
}
/** Returns second argument of the command
*
* Should be called only if current command is
* C_PUSH, C_POP, C_FUNCTION, C_CALL
*
**/
public String arg2() {
return command.split(" ")[2];
}
}