Skip to content

Commit 31f9a81

Browse files
committed
Support custom keywords in let-keywords.
1 parent d430a64 commit 31f9a81

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Sources/LispKit/Primitives/ControlFlowLibrary.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,21 @@ public final class ControlFlowLibrary: NativeLibrary {
407407
var prevIndex = -1
408408
let initialIp = compiler.emitPlaceholder()
409409
let backfillIp = compiler.offsetToNext(0)
410+
var keywords = [Symbol : Symbol]()
410411
// Backfill keyword bindings with defaults
411412
while case .pair(let binding, let rest) = bindings {
412-
guard case .pair(.symbol(let sym), .pair(let expr, .null)) = binding else {
413-
throw RuntimeError.eval(.malformedBinding, binding, bindingList)
413+
let sym: Symbol
414+
let expr: Expr
415+
switch binding {
416+
case .pair(.symbol(let s), .pair(let def, .null)):
417+
sym = s
418+
expr = def
419+
case .pair(.symbol(let s), .pair(.symbol(let key), .pair(let def, .null))):
420+
keywords[s] = key
421+
sym = s
422+
expr = def
423+
default:
424+
throw RuntimeError.eval(.malformedBinding, binding, bindingList)
414425
}
415426
compiler.emit(.pushUndef)
416427
let pushValueIp = compiler.emitPlaceholder()
@@ -450,7 +461,11 @@ public final class ControlFlowLibrary: NativeLibrary {
450461
while case .pair(.pair(.symbol(let sym), _), let rest) = bindings {
451462
let binding = group.allocBindingFor(sym)
452463
compiler.emit(.dup)
453-
compiler.pushConstant(.symbol(compiler.context.symbols.intern(sym.identifier + ":")))
464+
if let key = keywords[sym] {
465+
compiler.pushConstant(.symbol(key))
466+
} else {
467+
compiler.pushConstant(.symbol(compiler.context.symbols.intern(sym.identifier + ":")))
468+
}
454469
compiler.emit(.eq)
455470
let keywordCompIp = compiler.emitPlaceholder()
456471
compiler.emit(.pop)

0 commit comments

Comments
 (0)