MCPcopy
hub / github.com/fluentpython/example-code-2e / expand_quasiquote

Function expand_quasiquote

18-with-match/lispy/original/lispy.py:278–290  ·  view source on GitHub ↗

Expand `x => 'x; `,x => x; `(,@x y) => (append x y)

(x)

Source from the content-addressed store, hash-verified

276_append, _cons, _let = map(Sym, "append cons let".split())
277
278def expand_quasiquote(x):
279 """Expand `x => 'x; `,x => x; `(,@x y) => (append x y) """
280 if not is_pair(x):
281 return [_quote, x]
282 require(x, x[0] is not _unquotesplicing, "can't splice here")
283 if x[0] is _unquote:
284 require(x, len(x)==2)
285 return x[1]
286 elif is_pair(x[0]) and x[0][0] is _unquotesplicing:
287 require(x[0], len(x[0])==2)
288 return [_append, x[0][1], expand_quasiquote(x[1:])]
289 else:
290 return [_cons, expand_quasiquote(x[0]), expand_quasiquote(x[1:])]
291
292def let(*args):
293 args = list(args)

Callers 1

expandFunction · 0.85

Calls 2

is_pairFunction · 0.85
requireFunction · 0.85

Tested by

no test coverage detected