-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
167 lines (137 loc) · 4.64 KB
/
Copy pathparser.cpp
File metadata and controls
167 lines (137 loc) · 4.64 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//Defining parser module methods
#include <iostream>
#include <bits/stdc++.h>
#include "parser.h"
using namespace std;
string removeAllWhiteSpaces(string variableToClean); //Prototype
//Routine to open file and get ready to parse it.
void Parser::initializer(string filenameToParse)
{
// ifstream asmFile( filenameToParse);
asmFile.open(filenameToParse);
if( !asmFile || !asmFile.is_open())
{
cerr << "Error in opening file " << filenameToParse << endl;
exit(1);
}
lineCounter = 0;
}
//Routine to check if there are more commands (more non-empty lines and we aren't at the end of the file)
bool Parser::hasMoreCommands()
{
getline(asmFile, lineHolder);
if ( !asmFile) //asmFile.eof() was previously here
{
asmFile.close();
return false;
}
//Handling comments and whitespace
string holderA = lineHolder;
const int indexOfStartOfComment = lineHolder.find("//");
if(indexOfStartOfComment != -1)
{
holderA = holderA.erase(indexOfStartOfComment);
}
if(lineHolder.length() == 0 || holderA.length() == 0)
{
lineHolder = "IGNORE";
} else
{
lineHolder = holderA;
}
return true;
}
//Routine to read next command and make it current command
void Parser::advance()
{
cout << "Next instruction to process is: " << lineHolder << endl;
cout << "Length before whitespace removal: " << lineHolder.length() << endl;
nextCommand = removeAllWhiteSpaces(lineHolder);
cout << "Length after whitespace removal: " << nextCommand.length() << endl;
if(nextCommand[0] == '@') lineCounter++;
else if(nextCommand.find('=') != -1 || nextCommand.find(';') != -1) lineCounter++;
}
//Routine to get the comand type of a line of asm file
string Parser::commandType()
{
if(nextCommand[0] == '@') return "A_COMMAND";
if(nextCommand.find('=') != -1 || nextCommand.find(';') != -1) return "C_COMMAND";
if(nextCommand[0] == '(' && nextCommand[nextCommand.length() - 1] == ')') return "L_COMMAND";
return nextCommand;
}
//Routine to return the symbol or decimal of current command
string Parser::symbol()
{
if(Parser::commandType() == "A_COMMAND")
{
string aCommand = (nextCommand.erase(0,1));
cout << "acommand: " << aCommand << " of length " << aCommand.length() << endl;
return aCommand;
}
else if(Parser::commandType() == "L_COMMAND")
{
string lCommand = (nextCommand.erase(0,1)).erase(nextCommand.length() - 1, 1);
cout << "lcommand: " << lCommand << endl;
return lCommand;
}
return "VOID";
}
//Routine to return the dest mnemonic of a C_COMMAND
string Parser::dest()
{
if(Parser::commandType() == "C_COMMAND")
{
string instruction = nextCommand;
int indexOfEqualTo = nextCommand.find('=');
if(indexOfEqualTo != -1)
{
string destMnemonic = instruction.erase(indexOfEqualTo);
cout << "nextCommand Instruction destMnemonic :" << nextCommand << " " << instruction << " " << destMnemonic << endl;
return destMnemonic;
}
}
return "null";
}
//Routine to return the comp mnemonic of a C_COMMAND
string Parser::comp()
{
if(Parser::commandType() == "C_COMMAND")
{
string instruction1 = nextCommand;
string instruction2 = nextCommand;
int indexOfEqualTo = nextCommand.find('=');
int indexOfSemiColon = nextCommand.find(';');
if(indexOfEqualTo != -1 && indexOfSemiColon == -1)
{
string compMnemonic = instruction1.erase(0, indexOfEqualTo + 1);
return compMnemonic;
}
if(indexOfEqualTo == -1 && indexOfSemiColon != -1)
{
string compMnemonic = instruction2.erase(indexOfSemiColon);
return compMnemonic;
}
}
return "null";
}
//Routine to return the jump mnemonic of a C_COMMAND
string Parser::jump()
{
if(Parser::commandType() == "C_COMMAND")
{
string instruction = nextCommand;
int indexOfSemicolon = nextCommand.find(';');
if(indexOfSemicolon != -1)
{
string jumpMnemonic = instruction.erase(0, indexOfSemicolon + 1);
return jumpMnemonic;
}
return "null";
}
}
//Function to remove all leading, trailing and in-between white spaces from string
string removeAllWhiteSpaces(string variableToClean)
{
variableToClean.erase(std::remove_if(variableToClean.begin(), variableToClean.end(), ::isspace), variableToClean.end());
return variableToClean;
}