Skip to content

NightmarePog/Laze

Repository files navigation

Laze

CI Docs License: MIT Language: Lua Self-hosted

Demo

Laze is a small, statically-typed language that compiles to Lua. The output is a single self-contained .lua file with no runtime dependencies, which makes it a good fit for environments where you have Lua but not much else: ComputerCraft computers, embedded scripting runtimes, or anywhere a plain .lua file is easier to deploy than a set of modules.

The main thing Laze gives you over writing Lua directly is structure. Classes with real constructors, a type system that catches mistakes before runtime, a module system that resolves imports at compile time, and a clean interop layer for calling existing Lua APIs with type signatures.

Quick example

// Counter.class.laz
import std.Sys

private count: int = 0

constructor() { }

increment() {
    .count = .count + 1
}

value(): int {
    return .count
}

static main() {
    mut c = Counter()
    c.increment()
    c.increment()
    Sys.print(f"count: {c.value()}")
}

Run it:

lua bin/lazec.lua Counter.class.laz
lua Counter.lua

File kinds

Laze has three file kinds, distinguished by the suffix before .laz:

Suffix Kind Used for
.class.laz class Instantiable types with a constructor and instance methods
.object.laz object Static namespaces -- utility functions, Lua API wrappers
.trait.laz trait Method contracts that classes can implement

The filename (without the suffix) is the class or module name, so filenames are PascalCase.

Building

The compiler looks for std/ next to your source file. Copy or symlink the std/ directory from this repo into your project, or point the compiler at it with --pkg-path:

# Option A: copy/symlink std/ into your project
cp -r /path/to/lazarus/std ./std

# Option B: pass --pkg-path instead
lua bin/lazec.lua MyProgram.class.laz --pkg-path /path/to/lazarus
# Compile a file (output: <ClassName>.lua in the current directory)
lua bin/lazec.lua path/to/Entry.class.laz

# Check for errors without producing output
lua bin/lazec.lua path/to/File.class.laz --check

# Set optimization level (O0 is default, Os is smallest output)
lua bin/lazec.lua Entry.class.laz -O2

# Compile as a library (no entry-point call, exports LAZE_META header)
lua bin/lazec.lua Lib.class.laz --lib

Via make:

make selfbuild FILE=path/to/Entry.class.laz

Language reference

Documentation: nightmarepog.github.io/Laze

Source is in doc/design/.

Standard library

Module Kind Contents
std.Str object String utilities: split, trim, pad, find, replace
std.Sys object IO, process: print, read, file ops, argv, exit
std.Num object Math: floor, ceil, abs, sqrt, clamp, pow
std.Path object File path manipulation
std.List class Ordered list with push, pop, map, filter, fold
std.Map class Key-value map with get, has, delete, keys, values
std.Option class Optional values: Some/None, unwrap, unwrap_or
std.Result class Ok/Err result type, unwrap, unwrap_or, error
std.File class File handle operations
std.Json class JSON serialization
std.Time object Time and clock
std.Task object Async task utilities

Compiler development

The compiler is self-hosted: it is written in Laze and compiled by the binary it produces. The source lives in compiler/. The compiled output is bin/lazec.lua.

make selfhost    # rebuild bin/lazec.lua from compiler/ (verifies the fixpoint)
make lint        # run selene
make format      # run stylua

See doc/adding-features.md for how to add a language feature. Set LAZARUS_DEBUG=1 to include the Lua stack trace in error output.

About

A statically-typed language that compiles to Lua. Self-hosted compiler, zero runtime dependencies.

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages