-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValue.cpp
182 lines (144 loc) · 4.04 KB
/
Value.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
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
173
174
175
176
177
178
179
180
181
182
#include "Value.h"
Type_of_lex GetValue(Lex field)
{
if (reg_seg(field.lex)) return REG_SEG;
if (directive(field.lex)) return DIRECTIVE;
if (reg_8(field.lex)) return REG_8;
if (reg_16(field.lex)) return REG_16;
if (reg_32(field.lex)) return REG_32;
if (command(field.lex)) return COMMAND;
if (single_symb(field.lex)) return SINGLE;
if (hex_const(field.lex)) return HEX_CONST;
if (dec_const(field.lex)) return DEC_CONST;
if (bin_const(field.lex)) return BIN_CONST;
if (text_const(field.lex)) return TEXT_CONST;
if (user_indent(field.lex)) return USER_IDENT;
return UNDEF;
}
Value MakeValue(Lex field)
{
Value buff;
buff.lexVal = field.lex;
buff.line = field.line;
buff.length = field.lex.length();
buff._type = GetValue(field);
buff.count = field.counter;
return buff;
}
bool reg_8(string field)
{
string buff = field;
string templates = "AL AH BL BH CL CH DL DH";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool reg_16(string field)
{
string buff = field;
string templates = "AX BX CX DX SI DI BP";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool reg_32(string field)
{
string buff = field;
string templates = "EAX EBX ECX EDX ESI EDI EBP";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool command(string field)
{
string buff = field;
string templates = "MOV CMP CLI INC ADD DEC XOR OR JB JMP AND";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool directive(string field)
{
string buff = field;
string templates = "SEGMENT ENDS END = DB DW DD ASSUME";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool single_symb(string field)
{
string buff = field;
string templates = ", : [ ]";
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool reg_seg(string field)
{
string buff = field;
string templates = "GS DS CS";
for (int i = 0; i < buff.length(); i++)
buff[i] = (char)toupper(buff[i]);
if (templates.find(buff) != std::string::npos) return true;
else return false;
}
bool hex_const(string field)
{
string buff = field;
string hex_symb = "0 1 2 3 4 5 6 7 8 9 A B C D E F";
string templates = "H h";
if (!(isdigit(buff[0]))) return false;
for (int i = 1; i < buff.length() - 1; i++)
{
buff[i] = (char)toupper(buff[i]);
if (hex_symb.find(buff[i]) == std::string::npos) return false;
}
if (templates.find(buff[buff.length() - 1]) == std::string::npos) return false;
return true;
}
bool dec_const(string field)
{
string buff = field;
string templates = "D d";
for (int i = 0; i < buff.length() - 1; i++)
if (!isdigit(buff[i])) return false;
if (templates.find(buff[buff.length() - 1]) == std::string::npos && !isdigit(buff[buff.length() - 1])) return false;
return true;
}
bool bin_const(string field)
{
string buff = field;
string bin = "1 0";
string templates = "B b";
for (int i = 0; i < buff.length() - 1; i++)
{
buff[i] = (char)toupper(buff[i]);
if (bin.find(buff[i]) == std::string::npos) return false;
}
if (templates.find(buff[buff.length() - 1]) == std::string::npos) return false;
return true;
}
bool text_const(string field)
{
string buff = field;
string symb = "\"";
for (int i = 1; i < buff.length() - 1; i++)
{
if (symb.find(buff[i]) != std::string::npos) return false;
}
if (symb.find(buff[0]) == std::string::npos || symb.find(buff[buff.length() - 1]) == std::string::npos) return false;
return true;
}
bool user_indent(string field)
{
string buff = field;
for (int i = 0; i < buff.length() - 1; i++)
{
if ((!isdigit(buff[i]) && !(isalpha(buff[i])))) return false;
}
return true;
}