-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.cpp
More file actions
173 lines (160 loc) · 2.5 KB
/
Tokenizer.cpp
File metadata and controls
173 lines (160 loc) · 2.5 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
168
169
170
171
172
#include "Tokenizer.h"
#include <iostream>
#include <cmath>
using namespace std;
Tokenizer::Tokenizer() {
token_type = NUM;
end = false;
unary_minus = true;
}
void Tokenizer::prompt() const {
cout << "Expression: ";
}
void Tokenizer::next() {
eat_spaces();
char temp = cin.peek();
if (('0' <= temp && temp <= '9') || temp == '.') {
double a;
cin >> a;
token_type = NUM;
unary_minus = false;
num = a;
}
else if (temp == 'p')
{
cin.ignore();
if (cin.peek() == 'i')
{
cin.ignore();
token_type = NUM;
unary_minus = false;
num = PI;
}
else {
token_type = ERR;
while (cin.peek() != NEWLINE)
cin.ignore();
cin.ignore();
}
}
else {
token_type = OP;
op = op_code(temp);
unary_minus = true;
if (temp == RPN_CHAR) {
unary_minus = false;
}
else if (temp == NEWLINE) {
token_type = END;
end = true;
}
else if (op == UNK)
{
token_type = ERR;
while (cin.peek() != NEWLINE)
cin.ignore();
}
cin.ignore();
}
}
void Tokenizer::nextnoignore() {
eat_spaces();
char temp = cin.peek();
if (temp == NEWLINE) {
token_type = END;
end = true;
}
}
int Tokenizer::type() const {
return token_type;
}
double Tokenizer::num_read() const {
return num;
}
int Tokenizer::op_read() const {
return op;
}
bool Tokenizer::end_of_stream() const {
return end;
}
void Tokenizer::reset() {
token_type = NUM;
end = false;
unary_minus = true;
}
int Tokenizer::op_code(char oppi) {
if (oppi == UNM_CHAR && unary_minus) return UNM;
switch (oppi) {
case ADD_CHAR: return ADD;
case SUB_CHAR: return SUB;
case MLT_CHAR: return MLT;
case DIV_CHAR: return DIV;
case LPN_CHAR: return LPN;
case RPN_CHAR: return RPN;
case FAC_CHAR: return FAC;
case EXP_CHAR: return EXP;
case 's':
{
cin.ignore();
if (cin.peek() == 'i')
{
cin.ignore();
if (cin.peek() == 'n')
{
return SIN;
}
}
else if (cin.peek() == 'e')
{
cin.ignore();
if (cin.peek() == 'c')
{
return SEC;
}
}
break;
}
case 'c':
{
cin.ignore();
if (cin.peek() == 'o')
{
cin.ignore();
if (cin.peek() == 's')
{
return COS;
}
else if (cin.peek() == 't')
{
return COT;
}
}
else if (cin.peek() == 's')
{
cin.ignore();
if (cin.peek() == 'c')
{
return CSC;
}
}
break;
}
case 't':
{
cin.ignore();
if (cin.peek() == 'a')
{
cin.ignore();
if (cin.peek() == 'n')
{
return TAN;
}
}
break;
}
}
return UNK;
}
void Tokenizer::eat_spaces() const {
while (cin.peek() == SPC) cin.ignore();
}