Skip to content

Commit a3ef4dc

Browse files
committed
Add FOR ... INCLUSIVE
1 parent 27f5453 commit a3ef4dc

File tree

3 files changed

+94
-35
lines changed

3 files changed

+94
-35
lines changed

src/aux/aux_compile_line.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// | TODO: comment and format this file properly |
66
// +---------------------------------------------+
77

8+
#include "../ldpl.h"
9+
810
// Compiles line per line
911
void compile_line(vector<string> &tokens, compiler_state &state)
1012
{
@@ -442,6 +444,24 @@ void compile_line(vector<string> &tokens, compiler_state &state)
442444
state.where);
443445
return;
444446
}
447+
if (line_like("FOR $num-var FROM $num-expr TO $num-expr INCLUSIVE STEP $num-expr DO", tokens, state))
448+
{
449+
if (!in_procedure_section(state))
450+
badcode("FOR outside PROCEDURE section", state.where);
451+
state.open_loop();
452+
string var = get_c_variable(state, tokens[1]);
453+
string from = get_c_expression(state, tokens[3]);
454+
string to = get_c_expression(state, tokens[5]);
455+
string step = get_c_expression(state, tokens[7]);
456+
string init = var + " = " + from;
457+
string condition =
458+
step + " >= 0 ? " + var + " <= " + to + " : " + var + " >= " + to;
459+
string increment = var + " += " + step;
460+
// C++ Code
461+
state.add_code("for (" + init + "; " + condition + "; " + increment + ") {",
462+
state.where);
463+
return;
464+
}
445465
if (line_like("FOR EACH $anyVar IN $collection DO", tokens, state))
446466
{
447467
if (!in_procedure_section(state))

src/aux/aux_state.cpp

+70-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,50 @@
11
/* This file contains auxiliary functions that check the current compilation
22
* state */
33

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+
{
58
// -- Returns if the variable is a NUMBER MAP or an access to a multicontainer
69
// that results in a NUMBER MAP --
710
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;
913
return false;
1014
}
1115

12-
bool is_txt_map(string &token, compiler_state &state) {
16+
bool is_txt_map(string &token, compiler_state &state)
17+
{
1318
// -- Returns if the variable is a TEXT MAP or an access to a multicontainer
1419
// that results in a TEXT MAP --
1520
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;
1723
return false;
1824
}
1925

20-
bool is_num_list(string &token, compiler_state &state) {
26+
bool is_num_list(string &token, compiler_state &state)
27+
{
2128
// -- Returns if the variable is a NUMBER LIST or an access to a
2229
// multicontainer that results in a NUMBER LIST --
2330
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;
2533
return false;
2634
}
2735

28-
bool is_txt_list(string &token, compiler_state &state) {
36+
bool is_txt_list(string &token, compiler_state &state)
37+
{
2938
// -- Returns if the variable is a TEXT MAP or an access to a multicontainer
3039
// that results in a TEXT MAP --
3140
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;
3343
return false;
3444
}
3545

36-
bool is_list_list(string &token, compiler_state &state) {
46+
bool is_list_list(string &token, compiler_state &state)
47+
{
3748
// -- Returns if the variable is a NUMBER/TEXT LIST LIST multicontainer or a
3849
// multicontainer access that results in a LIST of LISTs --
3950
vector<unsigned int> type = variable_type(token, state);
@@ -42,7 +53,8 @@ bool is_list_list(string &token, compiler_state &state) {
4253
return false;
4354
}
4455

45-
bool is_map_list(string &token, compiler_state &state) {
56+
bool is_map_list(string &token, compiler_state &state)
57+
{
4658
// -- Returns if the variable is a multicontainer NUMBER/TEXT LIST MAP or a
4759
// multicontainer access that results in a LIST of MAPs --
4860
vector<unsigned int> type = variable_type(token, state);
@@ -51,15 +63,17 @@ bool is_map_list(string &token, compiler_state &state) {
5163
return false;
5264
}
5365

54-
bool is_scalar_map(string &token, compiler_state &state) {
66+
bool is_scalar_map(string &token, compiler_state &state)
67+
{
5568
// -- Returns if the variable is a NUMBER MAP or an access to a multicontainer
5669
// that results in a NUMBER MAP --
5770
// -- or if the variable is a TEXT MAP or an access to a multicontainer that
5871
// results in a TEXT MAP --
5972
return is_num_map(token, state) || is_txt_map(token, state);
6073
}
6174

62-
bool is_map_map(string &token, compiler_state &state) {
75+
bool is_map_map(string &token, compiler_state &state)
76+
{
6377
// -- Returns if the variable is a NUMBER/TEXT MAP MAP multicontainer or a
6478
// multicontainer access that results in a MAP of MAPs --
6579
vector<unsigned int> type = variable_type(token, state);
@@ -68,88 +82,103 @@ bool is_map_map(string &token, compiler_state &state) {
6882
return false;
6983
}
7084

71-
bool is_map(string &token, compiler_state &state) {
85+
bool is_map(string &token, compiler_state &state)
86+
{
7287
// -- Returns true if the variable is a MAP, regardless of a map of what
7388
// (multicontainer or not) --
7489
vector<unsigned int> type = variable_type(token, state);
7590
return type.back() == 4;
7691
}
7792

78-
bool is_scalar_list(string &token, compiler_state &state) {
93+
bool is_scalar_list(string &token, compiler_state &state)
94+
{
7995
// -- Returns if the variable is a NUMBER LIST or an access to a
8096
// multicontainer that results in a NUMBER LIST --
8197
// -- or if the variable is a TEXT LIST or an access to a multicontainer that
8298
// results in a TEXT LIST --
8399
return is_num_list(token, state) || is_txt_list(token, state);
84100
}
85101

86-
bool is_num_var(string &token, compiler_state &state) {
102+
bool is_num_var(string &token, compiler_state &state)
103+
{
87104
// -- Checks if token is a NUMBER variable (or an access to a container that
88105
// results in a NUMBER variable) --
89106
return (variable_type(token, state) == vector<unsigned int>{1});
90107
}
91108

92-
bool is_txt_var(string &token, compiler_state &state) {
109+
bool is_txt_var(string &token, compiler_state &state)
110+
{
93111
// -- Checks if token is a TEXT variable (or an access to a container that
94112
// results in a TEXT variable) --
95113
return (variable_type(token, state) == vector<unsigned int>{2});
96114
}
97115

98-
bool is_scalar_variable(string &token, compiler_state &state) {
116+
bool is_scalar_variable(string &token, compiler_state &state)
117+
{
99118
// -- Returns is an identifier is a valid scalar variable or an access that
100119
// results in one --
101120
return is_num_var(token, state) || is_txt_var(token, state);
102121
}
103122

104-
bool is_num_expr(string &token, compiler_state &state) {
123+
bool is_num_expr(string &token, compiler_state &state)
124+
{
105125
// -- Returns is an identifier is a valid scalar variable or number or an
106126
// access that results in one --
107127
return is_num_var(token, state) || is_number(token);
108128
}
109129

110-
bool is_txt_expr(string &token, compiler_state &state) {
130+
bool is_txt_expr(string &token, compiler_state &state)
131+
{
111132
// -- Returns is an identifier is a valid scalar variable or text or an access
112133
// that results in one --
113134
return is_txt_var(token, state) || is_string(token);
114135
}
115136

116-
bool is_expression(string &token, compiler_state &state) {
137+
bool is_expression(string &token, compiler_state &state)
138+
{
117139
// -- Returns is an identifier is a valid scalar variable or text or number or
118140
// an access that results in one --
119141
return is_num_expr(token, state) || is_txt_expr(token, state);
120142
}
121143

122-
bool is_external(string &token, compiler_state &state) {
144+
bool is_external(string &token, compiler_state &state)
145+
{
123146
// -- Returns if an identifier maps to an external variable --
124147
return state.externals[token];
125148
}
126149

127-
bool variable_exists(string &token, compiler_state &state) {
150+
bool variable_exists(string &token, compiler_state &state)
151+
{
128152
// -- Returns if a variable has been declared or not --
129153
// (Bear in mind that myList is a variable, myList:0 is not, that's an access
130154
// for all this function is concerned)
131155
return variable_type(token, state) != vector<unsigned int>{0};
132156
}
133157

134-
bool is_subprocedure(string &token, compiler_state &state) {
158+
bool is_subprocedure(string &token, compiler_state &state)
159+
{
135160
// -- Returns if an identifier maps to a valid, existing sub-procedure --
136161
for (auto &subprocedure : state.subprocedures)
137-
if (subprocedure.first == token) return true;
162+
if (subprocedure.first == token)
163+
return true;
138164
return false;
139165
}
140166

141-
bool in_procedure_section(compiler_state &state) {
167+
bool in_procedure_section(compiler_state &state)
168+
{
142169
// -- Returns if the compiler is currently compiling a procedure section or
143170
// not --
144-
if (state.section_state == 3) {
171+
if (state.section_state == 3)
172+
{
145173
// We're inside a SUB-PROCEDURE procedure with no sections
146174
state.section_state = 2;
147175
open_subprocedure_code(state);
148176
}
149177
return state.section_state == 2;
150178
}
151179

152-
vector<unsigned int> variable_type(string &token, compiler_state &state) {
180+
vector<unsigned int> variable_type(string &token, compiler_state &state)
181+
{
153182
// -- Returns the LDPL internal representation of the type of a variable --
154183
//
155184
// 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) {
192221
// to discard those indexes that access the other containers and not the one
193222
// we are trying to get the types of.
194223
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+
{
196226
// 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--;
199231
}
200232
// If it's not, then it must be a variable name.
201-
else {
233+
else
234+
{
202235
// If the variable doesn't exist in the current context, we rise an error.
203236
if (state.variables[state.current_subprocedure].count(tokens[i]) == 0 &&
204237
state.variables[""].count(tokens[i]) == 0)
@@ -211,13 +244,16 @@ vector<unsigned int> variable_type(string &token, compiler_state &state) {
211244
tokensToSkip += cvar_types.size() - 1;
212245
else
213246
// If the variable exists and is a scalar, we can skip it
214-
if (tokensToSkip > 0) tokensToSkip--;
247+
if (tokensToSkip > 0)
248+
tokensToSkip--;
215249
}
216-
if (tokensToSkip == 0) types.pop_back();
250+
if (tokensToSkip == 0)
251+
types.pop_back();
217252
}
218253
// We return {0} if there is an incomplete container access
219254
// that must be complete to resolve as a scalar index
220-
if (tokensToSkip > 0) return {0};
255+
if (tokensToSkip > 0)
256+
return {0};
221257
// Now we have the types and can return them.
222258
return types;
223259
}

src/ldpl.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#ifndef LDPL_H
2+
#define LDPL_H
13
/* --- STD Includes --- */
24
#include <algorithm>
35
#include <array>
@@ -97,4 +99,5 @@ void open_subprocedure_code(compiler_state &state);
9799
void add_call_code(string &subprocedure, vector<string> &parameters, compiler_state &state);
98100
string current_os();
99101
bool is_map(string &token, compiler_state &state);
100-
void badcode(const string &msg, const code_location where);
102+
void badcode(const string &msg, const code_location where);
103+
#endif

0 commit comments

Comments
 (0)