Bytecode runtime · GSVE examples

Explore greenServeFE
one file at a time.

greenServeFE (greenServe Fast Execution) is a C-based runtime that compiles .gsve programs into bytecode and executes them on a virtual machine. Browse the examples as file cards: click any filename to open its code.

How it runs

Source → compiler → virtual machine

The lexer and parser process the language syntax; the compiler emits bytecode, and the VM executes instructions using values, environments and native functions.

1. Write GSVE

Save a program with the .gsve extension.

2. Compile

The compiler produces instructions, constants, names and child function code objects.

3. Execute

The VM runs bytecode with operand stacks, frames and lexical environments.

.gsve → lexer → parser / AST → bytecode compiler → GSCode → VM → output
Quick start

Build and run

Build the source archive on a Linux environment with the required native development libraries installed.

git clone https://github.com/MacoraOne/MacoraOne.git
cd MacoraOne/greenServeFE-source-codes-v1.0.3
make clean all
./greenServeFE test.gsve
./greenServeFE --dump test.gsve
./greenServeFE --trace test.gsve

The --dump option displays bytecode disassembly; --trace enables execution tracing. Follow the source archive and release README for dependencies and package installation.

Interactive file browser

GSVE example files

These examples follow the syntax and file names in the greenServeFE-source-codes-v1.0.3/docs/examples archive. Select a file card to expand and read its complete example, then open the original source file on GitHub.

23 example files
01-hello.gsveBasics · print output
print("Hello, greenServeFE");
First programView original
02-variables.gsveVariables · strings
define name = "greenServeFE";
define version = 1;
define message = name + " " + str(version);
print(message);
Define values and convert to textView original
03-arithmetic.gsveArithmetic · operators
define a = 20;
define b = 6;
print(a + b);
print(a - b);
print(a * b);
print(a / b);
print(a % b);
Math operatorsView original
06-arrays.gsveArrays · indexing · iteration
define values = [10, 20, 30, 40];
print(values[0]);
values[1] = 25;
print(values);
for (value in values) {
    print(value);
}
Read, update and loop over an arrayView original
07-objects.gsveObjects · properties · keys
define user = {
    name: "Alex",
    role: "developer",
    active: true
};
print(user.name);
user.active = false;
print(user.active);
for (key in user) {
    print(key);
    print(user[key]);
}
Read and update object membersView original
20-data-processing.gsveNested data · records · totals
define products = [
    {name: "Notebook", price: 12},
    {name: "Keyboard", price: 45},
    {name: "Mouse", price: 25}
];

define total = 0;
for (product in products) {
    total = total + product.price;
}
print(total);
Combine arrays, objects and loopsView original
10-while.gsveLoops · while condition
define n = 1;
define total = 0;
while (n <= 5) {
    total = total + n;
    n = n + 1;
}
print(total);
Repeat while a condition is trueView original
11-for-array.gsveLoops · for/in · sum
define total = 0;
for (n in [1, 2, 3, 4, 5]) {
    total = total + n;
}
print(total);
Iterate over array valuesView original
09-if-else.gsveConditions · if / else if
define score = 82;
if (score >= 90) {
    print("excellent");
} else if (score >= 70) {
    print("pass");
} else {
    print("retry");
}
Choose a branch by conditionView original
13-break.gsveLoops · break
for (n in [1, 2, 3, 4, 5]) {
    if (n == 4) {
        break;
    }
    print(n);
}
Exit a loop earlyView original
14-continue.gsveLoops · continue
for (n in [1, 2, 3, 4, 5]) {
    if (n == 3) {
        continue;
    }
    print(n);
}
Skip an iterationView original
15-functions.gsveFunctions · parameters · return
func add(a, b) {
    return a + b;
}

func square(value) {
    return value * value;
}

print(add(4, 5));
print(square(7));
Define and call functionsView original
18-closure.gsveFunctions · closures
func make_adder(base) {
    func add(value) {
        return base + value;
    }
    return add;
}

define add10 = make_adder(10);
print(add10(5));
print(add10(20));
Capture a value from an outer functionView original
24-lengths.gsveBuilt-ins · len
define text = "greenServe";
define items = [1, 2, 3, 4];
define object = {a: 1, b: 2};

print(len(text));
print(len(items));
print(len(object));
Get string, array and object lengthsView original
26-gsnum-import.gsveModules · import gsnum
import gsnum;

print(gsnum.sum(1, 2, 3, 4));
print(gsnum.mean([10, 20, 30]));
print(gsnum.sqrt(144));
Call functions from a native packageView original
27-gsnum-from-import.gsveModules · from-import
from gsnum import sum, average;

print(sum(1, 2, 3, 4));
print(average(10, 20, 30));
Import named functionsView original
28-gs-table.gsveModules · tables · CSV
import gs_table;

define headers = ["Name", "Score", "Role"];
define rows = [
    ["Alex", 95, "developer"],
    ["Sam", 88, "designer"]
];

print(gs_table.render(headers, rows, {
    title: "Scores",
    align: ["left", "right", "left"]
}));

print(gs_table.csv(headers, rows));
print(gs_table.markdown(headers, rows));
Format data with a native packageView original
29-server-basic.gsveServer · route · static files
import server;

func home(req, res) {
    res.send("Hello from greenServeFE");
}

server.get("/", home);

server.serve(".", 8080, {
    index: "index.html",
    static: true
});
Register a GET route and serve a directoryView original
30-server-api.gsveServer · JSON API · headers
import server;

func status(req, res) {
    res.status(200);
    res.set("X-App", "greenServeFE");
    res.json({
        ok: true,
        method: req.method,
        path: req.path
    });
}

server.get("/api/status", status);

server.serve(".", 8080, {
    static: true
});
Return JSON from an API routeView original
31-server-params.gsveServer · route parameters
import server;

func user(req, res) {
    res.json({
        id: req.params.id,
        path: req.path
    });
}

server.get("/users/:id", user);

server.serve(".", 8080, {
    static: true
});
Read a path parameter such as /users/42View original
32-server-query.gsveServer · query parameters
import server;

func search(req, res) {
    res.json({
        query: req.query,
        raw: req.queryString
    });
}

server.get("/search", search);

server.serve(".", 8080, {
    static: true
});
Read query-string valuesView original
33-server-render.gsveServer · HTML templates
import server;

func home(req, res) {
    res.render("index.html", {
        title: "greenServeFE",
        message: "Server-side rendering works"
    });
}

server.get("/", home);

server.serve(".", 8080, {
    index: "index.html",
    static: true
});
Insert data into an HTML templateView original
34-server-api-client.gsveServer · outbound HTTP request
import server;

define response = server.http_get("https://example.com/");

func home(req, res) {
    res.json({
        status: response.status,
        contentType: response.contentType,
        url: response.url,
        error: response.error
    });
}

server.get("/", home);

server.serve(".", 8080, {
    static: true
});
Call an external URL from the server runtimeView original

Examples that import gsnum, gs_table, or server require the matching native module built for your greenServeFE ABI. Server examples need the server module and should be run from a directory containing any referenced static files or templates.

Runtime features

Language and VM capabilities

Values

Numbers, strings, booleans, null, arrays and objects.

Control flow

if, while, for/in, break and continue.

Functions

Function calls, recursion, closures and captured lexical environments.

Expressions

Arithmetic, comparisons, logical operators, assignment, indexing and member access.

Built-ins

print, len, str, num and type.

Debugging

Inspect generated bytecode with --dump and follow execution with --trace.

Native modules

Import functionality when needed

Native modules are shared libraries loaded by the runtime. The loader can search direct paths, the current directory, ./modules, and paths in GREENSERVE_MODULE_PATH.

ModuleExample usagePurpose
gsnumimport gsnum;Numeric helpers such as sum, mean and square root.
gs_tableimport gs_table;Render tabular data as text, CSV or Markdown.
serverimport server;HTTP routes, serving static files, responses, templates and outbound HTTP helpers.
Install the matching module shared library before running an import example. Module binaries must match the runtime's native ABI. Never commit private TLS keys or secrets into the source repository.
Source code

Explore the implementation