Skip to content
ALB Wiki

Diff — ALB MoonScript backend

Comparing rev 2 → rev 3

{{infobox
| title = ALB MoonScript backend
| tool = `albm.exe` ("ALBM — AdaLogic BASIC MoonScript transpiler")
| produces = `.moon` → `moonc` → `.lua` (Lua 5.4)
| runs_on = stock Lua 5.4 + `alb_sdl3.dll` + `SDL3.dll`
| emitter_unit = `Emit_Native_MoonScript` (emit_native_moonscript.adb) — ~8,300 lines
| sibling = [[ALB Lua backend]] — the canonical runtime page; this is its dialect twin
| status = ALB through a second translation — BASIC's words, MoonScript's clothes, Lua's legs
}}
The **ALB MoonScript backend** — **ALBM** — compiles [[AdaLogic BASIC]] into **MoonScript**, the whitespace-significant, arrow-functioned superset that compiles down to Lua. The chain is three languages deep: `.alb` → `.moon` → `moonc` → `.lua` → Lua 5.4 + the `alb_sdl3` bridge — and the remarkable part is that nothing is lost on the way down.
```alb One-liner
albm game.alb --build
→ game.moon + moonc + SDL3.dll + run.bat → moonc game.moon && lua game.lua
```
-
## Why a second translation?
+
# Why a second translation?
Because MoonScript is *already a translation layer*. The emitted `.moon` isn't the destination — `moonc` compiles it to Lua 5.4 at build time, so the backend inherits everything the [[ALB Lua backend|Lua backend]] is: the same `albVas` virtual address space, the same Dumptruck GC grid, the same Prolog rule engine, the same `alb_sdl3.dll` bridge, the same `MODE NO CONSOLE` overlay switch — all documented on the sibling page.
What MoonScript adds is on the *writer's* side: it makes the generated source shorter, cleaner, and more idiomatic than raw Lua — which matters when the "generated file" is also the deliverable. A hand-checkable artifact, not just an intermediate.
-
## The dialect, worn well
+
# The dialect, worn well
The emitter doesn't write "Lua with different keywords" — it writes real MoonScript:
```moon
albEnsureSDL = () ->
  ok, mod = pcall(require, "alb_sdl3")
  ALB_FATAL("ALBM requires alb_sdl3.dll…") if not ok
  albSDL = mod
  albSDL.init()
PURE = (num, den) -> return albPurePack(ALB_num(num), ALB_num(den or 1))
export ALB_new_array = (n, fill) ->
```
- **`(args) ->` arrow functions** everywhere — `ALB_NN_ACT = (code, value) ->`, `albEnsureTextRow = (row) ->` — the runtime is a library of lambdas, not `function … end` scaffolding
- **`export` declarations** — `export ALB_new_array`, `export ALB_band`, `export ALB_shl` — MoonScript's module-style top-level names
- **Postfix conditionals** — `ALB_FATAL(err) if not ok`, `r + p if aa == 1 and bb == 1` — guards read *after* the action, the way MoonScript intends
- **`ALB_EXPORTS = ALB_EXPORTS or {}`** — the foreign-export table, initialized idempotently
- **`ALB_NOTIFY_KNOWS_CHANGE(pred, a1, …)`** — KNOWS updates notify watchers: the logic runtime's change-notification, expressed as a one-line arrow
Even the sized-integer shims come out as readable MoonScript — `ALB_band` isn't a builtin call; it's the same repeated-halving algorithm the Lua twin uses (`a = math.floor(a / 2)`), written in the dialect's terser clothing.
-
## The pipeline
+
# The pipeline
`albm` emits `.moon`; `--build` assembles the full kit in `moon_<name>/`: the `.moon` file, `moonc` itself, `SDL3.dll`, and a `run.bat` that does `moonc game.moon && lua game.lua`. `--run` executes it in place. The runtime is **stock Lua 5.4** — not LuaJIT — because `alb_sdl3.dll` is a standard Lua C-module and doesn't need the JIT's FFI.
```fact Two translations, zero losses
ALB → MoonScript → Lua is the only backend in the family that's a *compiler chain of transpilers* — and it still carries Prolog rules, the claim grid, temporal state, and a deterministic neural network down through all three languages. Whitespace, arrows, and tables; the logic survives every hop.
```
-
## Under the hood
+
# Under the hood
`Emit_Native_MoonScript` (~8,300 lines, SPARK-annotated) declares itself the Lua twin openly — the spec comment reads *"feature surface mirrors Emit_Native_Lua"* — and it does: same `Compile_To_File(Root, Output_Filename)` AST-walker, same `Set_No_Console_Overlay` hook, same runtime rebuilt table-by-table, emitted in the dialect instead of raw Lua. `albm.adb` drives flags, moonc staging, and the run script.
The difference is purely *surface*, and that is precisely the point: ALB doesn't target runtimes, it targets **languages** — and MoonScript is proof the whole stack can wear a second dialect and lose nothing.
-
## See also
+
# See also
- [[ALB Lua backend]] — the canonical twin: shared runtime, `alb_sdl3` bridge, and the full feature list this page defers to
- [[AdaLogic BASIC]] — the language
- [[ALB TypeScript backend]] · [[ALB FASM backend]] · [[ALB C backend]] · [[ALB .NET backend]] · [[ALB Ada backend]] · [[ALB NASM backend]] · [[ALB FASM16 backend]]
-
## References
+
# References
1. ALB Handbook — alb-lang.org/handbook — the language reference for all backends.
2. ALB Technology page — alb-lang.org/technology — the sixteen-backend matrix.
3. ALB source — github.com/rockyoliver454/AdaLogic-BASIC-ALB-Programming-Language — the emitter sources are Apache 2.0.
[[Category:Backends]]
[[Category:AdaLogic BASIC]]