Skip to content

Language Reference

0x4171341 edited this page Jul 23, 2026 · 4 revisions

Language Reference

Comments

# This is a single-line comment
# L++ uses # for comments (no block comments)

Literals

42                # Int (64-bit signed)
3.14              # Float (64-bit IEEE 754)
true              # Bool
false             # Bool
"hello world"     # Str (ARC-managed string)

Keywords (21)

as break continue def else enum false fn for from if import in match mut pub return spawn struct true while

Types

Type Description Example
Int 64-bit signed integer 42
Float 64-bit IEEE 754 3.14
Str ARC-managed string "hello"
Bool Boolean true, false
Void No return value default for functions
List[Int] Dynamic list list_new()
Map[Int, Int] Hash map map_new()
Custom Struct or Enum type Point, Result

Variables

x := 42              # immutable, type inferred from value
mut y := 0           # mutable — can be reassigned
y = 10               # reassign (only works with mut)
name := "Alice"      # Str
pi := 3.14           # Float
flag := true         # Bool

Immutable by default. Use mut to allow reassignment.

Functions

def add(a: Int, b: Int) -> Int:
    return a + b

def greet(name: Str):          # returns Void (implicit)
    print_str(name)

Parameters are always immutable inside the function body. L++ does not support mut parameters — if you need to modify a value, declare a local mutable copy:

def double_it(x: Int) -> Int:
    mut result := x         # local mutable copy
    result = result * 2
    return result

Structs

struct Point:
    x: Int
    y: Int

p := Point(10, 20)      # positional constructor
print(p.x)              # field access → 10

Enums

Unit variants (no data):

enum Color:
    Red
    Green
    Blue

c := Color.Green

Data-carrying variants:

enum Result:
    Ok(value: Int)
    Err(code: Int)

enum Token:
    Number(value: Int)
    Identifier(name: Int)
    EOF

Constructing:

r := Result.Ok(42)
e := Result.Err(1)
t := Token.EOF

Pattern Matching

match token:
    Number(v):
        print(v)
    Identifier(id):
        print(id)
    EOF:
        print_str("end of file")

Match extracts data from variants via bindings (v, id).

def describe(r: Result):
    match r:
        Ok(value):
            print_str("success:")
            print(value)
        Err(code):
            print_str("error code:")
            print(code)

Error Handling

Define fallible functions with Result return type:

enum Result:
    Ok(value: Int)
    Err(code: Int)

def safe_divide(a: Int, b: Int) -> Result:
    if b == 0:
        return Result.Err(1)
    return Result.Ok(a / b)

Handle errors with match:

match safe_divide(10, 3):
    Ok(v):
        print(v)
    Err(c):
        print_str("division failed")

Propagate errors with ?:

def process(a: Int, b: Int) -> Result:
    v := safe_divide(a, b)?      # returns Err immediately if failed
    return Result.Ok(v + 100)    # only reached on success

Control Flow

# If / else
if x > 0:
    print_str("positive")
else:
    print_str("non-positive")

# While loop
mut i := 0
while i < 10:
    print(i)
    i = i + 1

# For range
for i in range(10):
    print(i)            # 0 through 9

# For-in (list iteration)
for item in my_list:
    print(item)

# Break and continue
mut n := 0
while true:
    n = n + 1
    if n == 5:
        continue        # skip 5
    if n > 10:
        break           # stop at 10
    print(n)

Closures

adder := fn(x: Int) -> Int:
    return x + 10

print(adder(5))    # 15

Closures capture variables from the enclosing scope:

base := 100

add_base := fn(x: Int) -> Int:
    return x + base      # captures 'base'

print(add_base(5))    # 105

Threads

spawn fn():
    print_str("running in background")

print_str("main continues immediately")

Imports

import math                          # loads math.lpp
import utils.helpers                 # loads utils/helpers.lpp
from stdlib.math import abs, pow     # selective import
from stdlib.result import is_ok      # import specific functions

Operators

Category Operators
Arithmetic + - * / %
Comparison == != < > <= >=
Declaration := (declare immutable)
Assignment = (reassign, requires mut)
Try ? (unwrap Result or return Err)
Access . (field / enum variant)

L++ does not currently support bitwise operators (&, |, ^, <<, >>), logical operators (&&, ||), or unary operators (!, -x). Use comparisons and arithmetic instead:

# Instead of !flag:
if flag == false:

# Instead of -x:
neg := 0 - x

# Instead of a && b:
if a:
    if b:
        # both true

Ownership

  • Primitives (Int, Float, Bool) are copied — no ownership tracking.
  • Str is ARC-managed — reference counted, automatically freed.
  • Structs are stack-allocated by default. Promoted to ARC heap when they escape (returned from functions, stored in containers).
  • List and Map are ARC-managed.
  • Ownership cycles are rejected at compile time — no tracing GC needed.

Values move by default. The compiler inserts retain/release automatically:

def create() -> Point:
    return Point(1, 2)    # ownership transferred to caller

def main():
    p := create()         # p owns the Point
    print(p.x)            # borrowed access
    # p is automatically released when main() exits

Standard Library Quick Reference

# Console
print(42)                          # print Int
print_str("hello")                 # print Str
input()                            # read line → Str

# Strings
str_len("abc")                     # → 3
str_concat("a", "b")              # → "ab"
str_repeat("ha", 3)               # → "hahaha"

# Lists
mut lst := list_new()
list_push(lst, 10)
list_get(lst, 0)                   # → 10
list_len(lst)                      # → 1

# Maps
m := map_new()
map_put(m, 1, 100)
map_get(m, 1)                     # → 100

# File I/O
write_file("out.txt", "data")
content := read_file("out.txt")

# Range
for i in range(5):                # 0, 1, 2, 3, 4
    print(i)

Clone this wiki locally