allow hollywoo readermacros to handle optional arguments

This commit is contained in:
2021-11-27 21:13:25 -07:00
parent 599ec4a94d
commit 7f06abe9e1
2 changed files with 17 additions and 8 deletions

View File

@@ -344,6 +344,8 @@ class Helpers {
FieldExp(field, removeTypeAnnotations(innerExp));
case KeyValueExp(keyExp, valueExp):
KeyValueExp(removeTypeAnnotations(keyExp), removeTypeAnnotations(valueExp));
case None:
None;
default:
throw CompileError.fromExp(exp, 'cannot remove type annotations');
};
@@ -404,6 +406,7 @@ class Helpers {
});
interp.variables.set("k", k.forMacroEval());
interp.variables.set("Macros", Macros);
interp.variables.set("Stream", Stream);
for (name => value in k.macroVars) {
interp.variables.set(name, value);
}

View File

@@ -50,14 +50,20 @@
readerMacroStart "$(.toUpperCase methodName) "]
`{
(defReaderMacro ,readerMacroStart [stream &builder b]
(b.callSymbol
,methodName
,(for arg args
(exprCase arg
((exprTyped Continuation cc)
(b.callSymbol "b.symbol" [(b.str "cc")]))
(_
(b.callSymbol "read" [(b.symbol "stream")]))))))
(let [nextLineStream
(Stream.fromString (stream.expect "hollywoo macro line" ->(stream.takeLine)))]
(b.callSymbol
,methodName
,(for arg args
(exprCase arg
((exprTyped Continuation cc)
(b.callSymbol "b.symbol" [(b.str "cc")]))
(_
(b.callSymbol "try" [
(b.callSymbol "read" [(b.symbol "nextLineStream")])
// optional arguments may not be present:
(b.callSymbol "catch" [(b.list [(b.symbol "e")]) (b.callSymbol "b.symbol" [(b.str "null")])])
])))))))
(#unless subclass (method ,nameAndType ,argList ,@body))
}))