-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
144 lines (137 loc) · 3.52 KB
/
lexer.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctype.h>
#include "vasm.h"
using namespace std;
static ifstream infile;
vector<Token> token_list;
vector<Token> get_token_list() {
return token_list;
}
/*
Init input file for reading
*/
bool file_init(string s) {
infile.open(s);
if(!infile.is_open())
return false;
return true;
}
/*
Function for checking variable characters
*/
inline bool lex_var_check(char s){
if((s >= 'A' && s <= 'Z') || isdigit(s))
return true;
return false;
}
/*
Lexer will lex the entire file and store sequence of tokens in token list
*/
bool lex(){
while(infile.peek() != EOF){
Token t;
//Skip spaces
while(infile.peek() == ' ')
infile.get();
//PUNCT
if(infile.peek() == ','){
infile.get();
t.type = COMMA_T;
token_list.push_back(t);
continue;
}
if(infile.peek() == ':'){
infile.get();
t.type = COLON_T;
token_list.push_back(t);
continue;
}
if(infile.peek() == '\n'){
infile.get();
t.type = NEWLINE_T;
token_list.push_back(t);
continue;
}
//IMM
if(infile.peek() == '#'){
infile.get();
int num = 0;
while(isdigit(infile.peek())){
num *= 10;
num += infile.get() - '0';
}
t.type = IMM_T;
t.imm = num;
token_list.push_back(t);
continue;
}
//Hex immediate will start with 0~9 a~e
if(isdigit(infile.peek())
|| (infile.peek() >= 'a'
&& infile.peek() <= 'f')){
string s;
while(isdigit(infile.peek()) ||
(infile.peek() >= 'a' &&
infile.peek() <= 'f')){
s += infile.get();
}
t.type = HEX_T;
t.hex = s;
token_list.push_back(t);
continue;
}
//Variable will be all capital letters
//No variables start with a digit will go through previous checks
if(lex_var_check(infile.peek())){
string s;
while(lex_var_check(infile.peek())){
s += infile.get();
}
t.type = IDENT_T;
t.ident = s;
token_list.push_back(t);
continue;
}
//Error
cout << "Error lexing, unknown character(" << (char)infile.peek() << ")\n";
return false;
}
Token t;
t.type = EOF_T;
token_list.push_back(t);
return true;
}
//Debugging
void print_token(){
cout << "Token: " << endl;
for(auto it = token_list.begin(); it != token_list.end(); it++){
switch(it->type){
case COMMA_T:
cout << "(,) ";
break;
case NEWLINE_T:
cout << endl;
break;
case COLON_T:
cout << "(:) ";
break;
case IMM_T:
cout << "IMM(" << it->imm << ") " ;
break;
case HEX_T:
cout << "IMM_H(" << it->hex << ") ";
break;
case IDENT_T:
cout << "ID(" << it->ident << ") ";
break;
case EOF_T:
cout << "EOF " ;
break;
default:
cout << "Error token ";
}
}
}