Initial commit - fix problems & refactor code
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
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
|
||||
Reference in New Issue
Block a user