mirror of
https://github.com/kanaka/mal.git
synced 2024-09-11 13:55:55 +03:00
add reader macro ^
This commit is contained in:
parent
06894e6c8b
commit
3edde8cc2e
@ -117,10 +117,11 @@ fun readAtom r = case next r of
|
||||
|> optIfNone (fn () => Option.filter (String.isPrefix ":") s |> Option.map (KEYWORD o (triml 1)))
|
||||
|> valIfNone (fn () => SYMBOL s), r')
|
||||
| SOME (LIT_STR s, r') => (malUnescape s |> STRING, r')
|
||||
| SOME (CARET, r') => readWithMeta r'
|
||||
| SOME (token, _) => raise SyntaxError ("unexpected token reading atom: " ^ (tokenString token))
|
||||
| NONE => raise SyntaxError "end of input reached when reading atom"
|
||||
|
||||
fun readForm r =
|
||||
and readForm r =
|
||||
case peek r of
|
||||
SOME PAREN_LEFT => readList [] (rest r)
|
||||
| SOME BRACKET_LEFT => readVector [] (rest r)
|
||||
@ -132,6 +133,13 @@ fun readForm r =
|
||||
| SOME TILDE_AT => let val (a, r') = readForm (rest r) in (LIST [SYMBOL "splice-unquote", a], r') end
|
||||
| _ => readAtom r
|
||||
|
||||
and readWithMeta r =
|
||||
let val (m, r') = readForm r
|
||||
val (v, r'') = readForm r'
|
||||
in
|
||||
(LIST [SYMBOL "with-meta", v, m], r'')
|
||||
end
|
||||
|
||||
and readList acc r =
|
||||
if peek r = SOME PAREN_RIGHT
|
||||
then (LIST (rev acc), (rest r))
|
||||
|
Loading…
Reference in New Issue
Block a user