Files
pylisp/core/macro_expand.py
T

28 lines
759 B
Python

from typing import Any
from core.primitives.symbol import symbolKey, isSymbol
from core.primitives.list import NIL, isList, cons, car, cdr
def macroExpand(body: Any, bindings: dict[str, Any]) -> Any:
"""Expand a macro by substituting bindings into the body.
Args:
body: The macro body to expand.
bindings: Dictionary of symbol-to-value bindings.
Returns:
The expanded body.
"""
if isSymbol(body):
if symbolKey(body) in bindings:
return bindings[symbolKey(body)] # auto quote
return body
if isList(body):
if body == NIL:
return NIL
return cons(
macroExpand(car(body), bindings), macroExpand(cdr(body), bindings)
)
return body