Own programming language: How nested for-loop?

Hello all,

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();


class ForLoopData {

       int max_i_value; 
       int current_i_value; 
       String variableName;


Thank you!


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)

it also depends on how your code parser works

1 Like

Okay, thank you…


I solved it using a stack to
hold the for-loop data

Thank you!


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;

  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_;

// =================================================================

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 Stack {

  Item _peek = null;
  int counter = 0; 

  // Prüft, ob der Stapel leer ist
  boolean empty() {
      _peek == null;

  // Speichert ein neues Objekt
  void push(ForLoopData _value) {
    _peek = new Item(_value, _peek);

  // Gibt das oberste Objekt wieder und entfernt es aus dem Stapel
  ForLoopData pop() {
    ForLoopData temp = _peek.value;

    if (!empty())
      _peek = _peek.next;

    return temp;

  // Gibt das oberste Objekt wieder
  ForLoopData peek() {
    return !empty() ? _peek.value : null;