🔧 Meta-programming
Macros, manipulação de AST e avaliação em compile-time
Macros
// Definir macro
macro_define("assert", ["condition", "message"],
"if not $condition then error($message) end")
// Usar macro
assert(x > 0, "x must be positive")
// Expandir
let expanded = macro_expand("assert", [true, "test"])
AST Manipulation
// Parse código para AST
let ast = ast_parse("let x = 1")
// Criar nó AST
let node = ast_node("Identifier", {name: "foo"})
let num = num(42)
let bin = bin("+", ident("a"), num(1))
// Walker AST
ast_walk(ast, fn(node) => print(node.type))
// Map transform
let transformed = ast_map(ast, fn(n) =>
if n.type == "Identifier"
then ast_node("StringLiteral", {value: n.name})
else n
end
)
Compile-time Evaluation
// Avaliar em compile-time
let const_result = const_eval("1 + 2 * 3") // 7
// Inline constant
let inlined = const_inline(42)
// Evaluate se constante
let evaluated = ct_eval(node)
Node Constructors
| Função | Descrição | Exemplo |
|---|---|---|
ident(name) | Identifier | ident("x") |
num(value) | Number | num(42) |
str(value) | String | str("hello") |
bin(op, left, right) | Binary | bin("+", a, b) |
call(name, args) | Function call | call("print", [x]) |