Skip to content

feat(lang): add macro system #11

@eddmann

Description

@eddmann

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:

  1. Extend the attribute syntax - @macro_name(args) for declarative macros
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions