Summary
Add a compile-time macro system to santa-lang for metaprogramming capabilities.
Description
Introduce macros that allow code transformation at compile time, enabling DSLs, code generation, and reducing boilerplate.
Current @ Usage
The @ symbol is already used for attributes (e.g., @slow for test markers). Macros could:
- Extend the attribute syntax -
@macro_name(args) for declarative macros
- Use a different syntax - e.g.,
macro!, #macro, or backticks
Proposed Syntax Options
Option A: Attribute-style (extend current @ usage)
// Define a macro
@macro log_time = |expr| {
let start = time();
let result = expr;
puts("Took: " + (time() - start) + "ms");
result
};
// Use the macro
@log_time {
expensive_computation()
}
Option B: Exclamation mark (Rust-style)
// Invoke macro
log_time! {
expensive_computation()
}
// Define macro
macro log_time(expr) {
quote {
let start = time();
let result = unquote(expr);
puts("Took: " + (time() - start) + "ms");
result
}
}
Option C: Hash prefix
#log_time {
expensive_computation()
}
Use Cases
Debug/Logging
@debug let x = complex_calculation();
// Expands to: let x = complex_calculation(); puts("x = " + x);
Conditional Compilation
@if_feature("experimental") {
// Only included if feature enabled
}
DSLs
@html {
div(class: "container") {
h1 { "Hello, World!" }
p { "Welcome to santa-lang" }
}
}
Derive/Auto-implement
@derive(eq, hash)
let Point = |x, y| #{x, y};
Implementation Considerations
- Hygiene - Prevent accidental variable capture
- Expansion timing - Before or after parsing?
- Quoting/unquoting - How to construct and splice AST
- Error messages - Point to original source, not expanded code
- Debugging - Macro expansion visualization
References
Summary
Add a compile-time macro system to santa-lang for metaprogramming capabilities.
Description
Introduce macros that allow code transformation at compile time, enabling DSLs, code generation, and reducing boilerplate.
Current
@UsageThe
@symbol is already used for attributes (e.g.,@slowfor test markers). Macros could:@macro_name(args)for declarative macrosmacro!,#macro, or backticksProposed Syntax Options
Option A: Attribute-style (extend current
@usage)Option B: Exclamation mark (Rust-style)
Option C: Hash prefix
Use Cases
Debug/Logging
Conditional Compilation
DSLs
Derive/Auto-implement
Implementation Considerations
References