Your
it
appears to be the iterator through the list of the current line. Name it something better. The
Original
could be better named something better too.
1 2
|
list<string> script; // or guión or something like that
list<string>::iterator linea_actual;
|
Now when you use them below it is abundantly clear what they are.
Do not separate variable names (“declarations”) from the names and values (what I have called the “environment”). It only produces useless duplication of information. You only need the environment key→value lookup.
So, for example, the following lines turn into the following code:
|
INT a
|
entorno['a']; // Create the variable named 'a' (and assign it a default value of zero) |
|
a = 12
|
entorno['a'] = 12; // Assign the value 12 to the variable named 'a' |
I do not know what some of your objects are. You seem to have a pile/heap/stack/? (
ye
) and a list (
lista
). Both names are uninformative, and are likely unnecessary.
You can certainly use a stack to process fancy arithmetic operations, but...
You should typically declare variables as close to their use as possible, and let them not exist when they are not needed. All those things like
d
,
p1
,
o1
, etc should not be persistent objects.
Declare them when they are needed, and let them not exist elsewhere.
1 2 3 4 5 6
|
if (cadena.at(2) == '=') {
for (conteo = declaraciones.begin(); conteo != declaraciones.end(); conteo++) {
if (conteo->get_c() == cadena.at(0)) {
if (isnumS(cadena.at(4))) {
int d = atoi(&cadena.at(4));
conteo->set_i(d);
|
You can completely eliminate the name too:
|
conteo->set_i(atoi(&cadena.at(4));
|
However, as I described in your other post (www.cplusplus.com/forum/beginner/253468/
), tokenizing the line is exceedingly useful:
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
|
for (auto linea_actual = script.begin(); linea_actual != script.end(); ++linea_actual)
{
auto tokenes = tokenizar( *linea_actual );
if (tokenes.size() == 2) and (tokenes[0] == "INT"))
{
if (!es_identificador( tokenes[1] )) fooey();
entorno[tokenes[1][0]];
}
if ((tokenes.size() == 3) and (tokenes[1] == "="))
{
if (!es_identificador( tokenes[0] ) or !es_cifra( tokenes[2] ))
fooey();
entorno[tokenes[0][0]] = stoi( tokenes[2] );
}
if ((tokenes.size() == 2) and (tokenes[0] == "JUMP"))
{
if (!es_cifra( tokenes[1] )) fooey();
auto nueva_linea = stoi( tokenes[1] );
if ((nueva_linea < 1) or (nueva_linea >= script.size())) fooey();
linea_actual = std::next( script.begin(), nueva_linea - 1 );
--linea_actual;
continue;
}
if ((tokenes.size() = 6) and (tokenes[0] == "IF"))
{
…
}
…
}
|
Anyway, hope this helps.