DEV Community

Cover image for Loop implementation of greenServeFE programming language
greenServeFE
greenServeFE

Posted on

Loop implementation of greenServeFE programming language

Understanding While Loops in greenServeFE

I've been developing greenServeFE, a VM-based execution engine for the greenServe programming language, written in C.

One of its core features is loop execution through compiled bytecode.

A simple while loop

# Start counting from 1.
define count = 1;

# Repeat while the condition is true.
while (count <= 5) {
    print("Iteration: " + count);

    # Update the counter.
    count = count + 1;
}

print("Loop completed!");
Enter fullscreen mode Exit fullscreen mode

Output:

Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Loop completed!
Enter fullscreen mode Exit fullscreen mode

How does it execute?

Unlike a simple recursive AST interpreter, greenServeFE compiles source code into bytecode before execution.

The while loop follows this execution sequence:

  1. Evaluate the loop condition.
  2. Jump out if the condition is false.
  3. Execute the loop body.
  4. Jump back to evaluate the condition again.

The compiler generates conditional and unconditional jump instructions, while the VM executes them using its instruction pointer and operand stack.

greenServeFE also supports for/in, nested loops, break, and continue.

Architecture

.gsve source
     |
   Lexer
     |
   Parser
     |
  Compiler
     |
   Bytecode
     |
    VM
     |
   Output
Enter fullscreen mode Exit fullscreen mode

The engine is implemented in C, with native shared-library modules providing additional capabilities without changing the core language syntax or VM.

Project: greenServeFE - greenServe Fast Execution

Top comments (0)