1
1
/* This file contains auxiliary functions that check the current compilation
2
2
* state */
3
3
4
- bool is_num_map (string &token, compiler_state &state) {
4
+ #include " ../ldpl.h"
5
+
6
+ bool is_num_map (string &token, compiler_state &state)
7
+ {
5
8
// -- Returns if the variable is a NUMBER MAP or an access to a multicontainer
6
9
// that results in a NUMBER MAP --
7
10
vector<unsigned int > type = variable_type (token, state);
8
- if (type.size () == 2 && type[0 ] == 1 && type[1 ] == 4 ) return true ;
11
+ if (type.size () == 2 && type[0 ] == 1 && type[1 ] == 4 )
12
+ return true ;
9
13
return false ;
10
14
}
11
15
12
- bool is_txt_map (string &token, compiler_state &state) {
16
+ bool is_txt_map (string &token, compiler_state &state)
17
+ {
13
18
// -- Returns if the variable is a TEXT MAP or an access to a multicontainer
14
19
// that results in a TEXT MAP --
15
20
vector<unsigned int > type = variable_type (token, state);
16
- if (type.size () == 2 && type[0 ] == 2 && type[1 ] == 4 ) return true ;
21
+ if (type.size () == 2 && type[0 ] == 2 && type[1 ] == 4 )
22
+ return true ;
17
23
return false ;
18
24
}
19
25
20
- bool is_num_list (string &token, compiler_state &state) {
26
+ bool is_num_list (string &token, compiler_state &state)
27
+ {
21
28
// -- Returns if the variable is a NUMBER LIST or an access to a
22
29
// multicontainer that results in a NUMBER LIST --
23
30
vector<unsigned int > type = variable_type (token, state);
24
- if (type.size () == 2 && type[0 ] == 1 && type[1 ] == 3 ) return true ;
31
+ if (type.size () == 2 && type[0 ] == 1 && type[1 ] == 3 )
32
+ return true ;
25
33
return false ;
26
34
}
27
35
28
- bool is_txt_list (string &token, compiler_state &state) {
36
+ bool is_txt_list (string &token, compiler_state &state)
37
+ {
29
38
// -- Returns if the variable is a TEXT MAP or an access to a multicontainer
30
39
// that results in a TEXT MAP --
31
40
vector<unsigned int > type = variable_type (token, state);
32
- if (type.size () == 2 && type[0 ] == 2 && type[1 ] == 3 ) return true ;
41
+ if (type.size () == 2 && type[0 ] == 2 && type[1 ] == 3 )
42
+ return true ;
33
43
return false ;
34
44
}
35
45
36
- bool is_list_list (string &token, compiler_state &state) {
46
+ bool is_list_list (string &token, compiler_state &state)
47
+ {
37
48
// -- Returns if the variable is a NUMBER/TEXT LIST LIST multicontainer or a
38
49
// multicontainer access that results in a LIST of LISTs --
39
50
vector<unsigned int > type = variable_type (token, state);
@@ -42,7 +53,8 @@ bool is_list_list(string &token, compiler_state &state) {
42
53
return false ;
43
54
}
44
55
45
- bool is_map_list (string &token, compiler_state &state) {
56
+ bool is_map_list (string &token, compiler_state &state)
57
+ {
46
58
// -- Returns if the variable is a multicontainer NUMBER/TEXT LIST MAP or a
47
59
// multicontainer access that results in a LIST of MAPs --
48
60
vector<unsigned int > type = variable_type (token, state);
@@ -51,15 +63,17 @@ bool is_map_list(string &token, compiler_state &state) {
51
63
return false ;
52
64
}
53
65
54
- bool is_scalar_map (string &token, compiler_state &state) {
66
+ bool is_scalar_map (string &token, compiler_state &state)
67
+ {
55
68
// -- Returns if the variable is a NUMBER MAP or an access to a multicontainer
56
69
// that results in a NUMBER MAP --
57
70
// -- or if the variable is a TEXT MAP or an access to a multicontainer that
58
71
// results in a TEXT MAP --
59
72
return is_num_map (token, state) || is_txt_map (token, state);
60
73
}
61
74
62
- bool is_map_map (string &token, compiler_state &state) {
75
+ bool is_map_map (string &token, compiler_state &state)
76
+ {
63
77
// -- Returns if the variable is a NUMBER/TEXT MAP MAP multicontainer or a
64
78
// multicontainer access that results in a MAP of MAPs --
65
79
vector<unsigned int > type = variable_type (token, state);
@@ -68,88 +82,103 @@ bool is_map_map(string &token, compiler_state &state) {
68
82
return false ;
69
83
}
70
84
71
- bool is_map (string &token, compiler_state &state) {
85
+ bool is_map (string &token, compiler_state &state)
86
+ {
72
87
// -- Returns true if the variable is a MAP, regardless of a map of what
73
88
// (multicontainer or not) --
74
89
vector<unsigned int > type = variable_type (token, state);
75
90
return type.back () == 4 ;
76
91
}
77
92
78
- bool is_scalar_list (string &token, compiler_state &state) {
93
+ bool is_scalar_list (string &token, compiler_state &state)
94
+ {
79
95
// -- Returns if the variable is a NUMBER LIST or an access to a
80
96
// multicontainer that results in a NUMBER LIST --
81
97
// -- or if the variable is a TEXT LIST or an access to a multicontainer that
82
98
// results in a TEXT LIST --
83
99
return is_num_list (token, state) || is_txt_list (token, state);
84
100
}
85
101
86
- bool is_num_var (string &token, compiler_state &state) {
102
+ bool is_num_var (string &token, compiler_state &state)
103
+ {
87
104
// -- Checks if token is a NUMBER variable (or an access to a container that
88
105
// results in a NUMBER variable) --
89
106
return (variable_type (token, state) == vector<unsigned int >{1 });
90
107
}
91
108
92
- bool is_txt_var (string &token, compiler_state &state) {
109
+ bool is_txt_var (string &token, compiler_state &state)
110
+ {
93
111
// -- Checks if token is a TEXT variable (or an access to a container that
94
112
// results in a TEXT variable) --
95
113
return (variable_type (token, state) == vector<unsigned int >{2 });
96
114
}
97
115
98
- bool is_scalar_variable (string &token, compiler_state &state) {
116
+ bool is_scalar_variable (string &token, compiler_state &state)
117
+ {
99
118
// -- Returns is an identifier is a valid scalar variable or an access that
100
119
// results in one --
101
120
return is_num_var (token, state) || is_txt_var (token, state);
102
121
}
103
122
104
- bool is_num_expr (string &token, compiler_state &state) {
123
+ bool is_num_expr (string &token, compiler_state &state)
124
+ {
105
125
// -- Returns is an identifier is a valid scalar variable or number or an
106
126
// access that results in one --
107
127
return is_num_var (token, state) || is_number (token);
108
128
}
109
129
110
- bool is_txt_expr (string &token, compiler_state &state) {
130
+ bool is_txt_expr (string &token, compiler_state &state)
131
+ {
111
132
// -- Returns is an identifier is a valid scalar variable or text or an access
112
133
// that results in one --
113
134
return is_txt_var (token, state) || is_string (token);
114
135
}
115
136
116
- bool is_expression (string &token, compiler_state &state) {
137
+ bool is_expression (string &token, compiler_state &state)
138
+ {
117
139
// -- Returns is an identifier is a valid scalar variable or text or number or
118
140
// an access that results in one --
119
141
return is_num_expr (token, state) || is_txt_expr (token, state);
120
142
}
121
143
122
- bool is_external (string &token, compiler_state &state) {
144
+ bool is_external (string &token, compiler_state &state)
145
+ {
123
146
// -- Returns if an identifier maps to an external variable --
124
147
return state.externals [token];
125
148
}
126
149
127
- bool variable_exists (string &token, compiler_state &state) {
150
+ bool variable_exists (string &token, compiler_state &state)
151
+ {
128
152
// -- Returns if a variable has been declared or not --
129
153
// (Bear in mind that myList is a variable, myList:0 is not, that's an access
130
154
// for all this function is concerned)
131
155
return variable_type (token, state) != vector<unsigned int >{0 };
132
156
}
133
157
134
- bool is_subprocedure (string &token, compiler_state &state) {
158
+ bool is_subprocedure (string &token, compiler_state &state)
159
+ {
135
160
// -- Returns if an identifier maps to a valid, existing sub-procedure --
136
161
for (auto &subprocedure : state.subprocedures )
137
- if (subprocedure.first == token) return true ;
162
+ if (subprocedure.first == token)
163
+ return true ;
138
164
return false ;
139
165
}
140
166
141
- bool in_procedure_section (compiler_state &state) {
167
+ bool in_procedure_section (compiler_state &state)
168
+ {
142
169
// -- Returns if the compiler is currently compiling a procedure section or
143
170
// not --
144
- if (state.section_state == 3 ) {
171
+ if (state.section_state == 3 )
172
+ {
145
173
// We're inside a SUB-PROCEDURE procedure with no sections
146
174
state.section_state = 2 ;
147
175
open_subprocedure_code (state);
148
176
}
149
177
return state.section_state == 2 ;
150
178
}
151
179
152
- vector<unsigned int > variable_type (string &token, compiler_state &state) {
180
+ vector<unsigned int > variable_type (string &token, compiler_state &state)
181
+ {
153
182
// -- Returns the LDPL internal representation of the type of a variable --
154
183
//
155
184
// Return the number of the type or {0} if the variable doesn't exist. This
@@ -192,13 +221,17 @@ vector<unsigned int> variable_type(string &token, compiler_state &state) {
192
221
// to discard those indexes that access the other containers and not the one
193
222
// we are trying to get the types of.
194
223
size_t tokensToSkip = 0 ;
195
- for (size_t i = 1 ; i < tokens.size (); ++i) {
224
+ for (size_t i = 1 ; i < tokens.size (); ++i)
225
+ {
196
226
// If the current token is a scalar literal, we can skip it safely.
197
- if (is_number (tokens[i]) || is_string (tokens[i])) {
198
- if (tokensToSkip > 0 ) tokensToSkip--;
227
+ if (is_number (tokens[i]) || is_string (tokens[i]))
228
+ {
229
+ if (tokensToSkip > 0 )
230
+ tokensToSkip--;
199
231
}
200
232
// If it's not, then it must be a variable name.
201
- else {
233
+ else
234
+ {
202
235
// If the variable doesn't exist in the current context, we rise an error.
203
236
if (state.variables [state.current_subprocedure ].count (tokens[i]) == 0 &&
204
237
state.variables [" " ].count (tokens[i]) == 0 )
@@ -211,13 +244,16 @@ vector<unsigned int> variable_type(string &token, compiler_state &state) {
211
244
tokensToSkip += cvar_types.size () - 1 ;
212
245
else
213
246
// If the variable exists and is a scalar, we can skip it
214
- if (tokensToSkip > 0 ) tokensToSkip--;
247
+ if (tokensToSkip > 0 )
248
+ tokensToSkip--;
215
249
}
216
- if (tokensToSkip == 0 ) types.pop_back ();
250
+ if (tokensToSkip == 0 )
251
+ types.pop_back ();
217
252
}
218
253
// We return {0} if there is an incomplete container access
219
254
// that must be complete to resolve as a scalar index
220
- if (tokensToSkip > 0 ) return {0 };
255
+ if (tokensToSkip > 0 )
256
+ return {0 };
221
257
// Now we have the types and can return them.
222
258
return types;
223
259
}
0 commit comments