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!");
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Loop completed!
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:
- Evaluate the loop condition.
- Jump out if the condition is false.
- Execute the loop body.
- 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
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)