Compiler (Luma → Go)

The Luma compiler translates Luma source code into idiomatic Go. This ensures both high performance and portability while retaining Luma’s simplicity and expressiveness.

High-Level Overview

Luma is not interpreted. Instead, it compiles to Go code which is then built into a native binary. This brings:

  • Fast execution - same performance as Go.
  • Easy deployment - native cross-platform binaries.
  • Go ecosystem access - potential interop in the future.

Type-Aware Codegen

The compiler tracks types explicitly from variable declarations:

x: int = 5
y: float = 3.5
total: float = x + y

This becomes:

x := int(5)
y := float64(3.5)
total := float64(x) + y

The compiler infers and injects coercions automatically depending on expected types. For example:

  • If the variable type is int, the expression is cast to int.
  • Parentheses are respected in mathematical operations.

Core Function Injection

Functions like print(), type(), and to_int() are mapped to internal Go helpers:

print("Hello")

Compiles to:

_luma_print("Hello")

These helpers are injected automatically from the /core folder before user code, enabling fast built-in functionality.

Example

Luma Code:

a: int = 5
b: float = 15.0
result: float = (a + b) / 2
print("Average: ${result}")

Generated Go:

a := int(5)
b := float64(15.0)
result := (float64(a) + b) / float64(2)
_luma_print("Average: " + _luma_sprint(result))

Built-In Helpers

Core Luma functions are mapped to internal helpers in Go:

Luma Code Compiled Go Function
print(“Hello”) _luma_print(“Hello”)
type(x) _luma_type(x)
to_int(“42”) _luma_toInt(“42”)
x.is_type(“float”) _luma_is_type(x, “float”)

The compiler injects these helpers from /core automatically.

CLI Usage

Luma comes with a command-line tool for compiling, debugging, and exploring code:

luma run app.luma          # Run a Luma script directly
luma tokens app.luma       # Show tokenized output
luma compile app.luma      # View generated Go code
luma build app.luma        # Build native binary
luma repl                  # Start REPL session
luma version               # Display Luma version

You can inspect the generated Go source before building:

luma compile hello.luma
# → Outputs main_temp.go to stdout

Design Goals Recap

  • Readability: Generated Go is clean and debug-friendly.
  • Safety: Type coercion is handled explicitly during compilation.
  • Speed: Leverages Go’s compiler and runtime performance.
  • Modularity: Easy to extend for more backends in future.
Last updated on