I am doing my own programming language at the moment.
An interpreter language.
When a for-loop is, not so difficult. I have the start line and he jumps x-times back to the start line ( “{” ) from the end line ("}") of the for loop. These are 3 global variables.
BUT how can I do a nested for loop? I don’t whether there are 2 or 7 nested for loops.
So, do I have to use a stack? To replace my 3 global variables?
A stack like:
ArrayList< ForLoopData> = new ArrayList();
and
class ForLoopData {
int max_i_value;
int current_i_value;
String variableName;
}//class
that’s your issue. don’t use global variables, that should be done through indeed a stack or through the use of recursive calls (which will use the stack)
I solved it using a stack to
hold the for-loop data
Thank you!
Chrisir
Here is the code
(the usage of this is elsewhere)
// see https://de.wikipedia.org/wiki/Stapelspeicher#Stapelorientierte_Sprachen
class ForLoopData {
int max_i_value;
int current_i_value;
int nodeStartNumber;
String variableName;
//constr
ForLoopData ( int max_i_value_,
int current_i_value_,
int nodeStartNumber_,
String variableName_ ) {
max_i_value = max_i_value_;
current_i_value = current_i_value_;
nodeStartNumber = nodeStartNumber_;
variableName = variableName_;
}//constr
//
}//class
// =================================================================
class Item {
ForLoopData value; // Das zu verwaltende Objekt
Item next; // Referenz auf den nächsten Knoten
Item(ForLoopData _value, Item _next) {
value = _value;
next = _next;
}
}//class
// =================================================================
class Stack {
Item _peek = null;
int counter = 0;
// Prüft, ob der Stapel leer ist
boolean empty() {
return
_peek == null;
}
// Speichert ein neues Objekt
void push(ForLoopData _value) {
_peek = new Item(_value, _peek);
counter++;
}
// Gibt das oberste Objekt wieder und entfernt es aus dem Stapel
ForLoopData pop() {
ForLoopData temp = _peek.value;
if (!empty())
_peek = _peek.next;
counter--;
return temp;
}
// Gibt das oberste Objekt wieder
ForLoopData peek() {
return !empty() ? _peek.value : null;
}//method
//
}//class
//