1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-17 09:40:21 +03:00

formatting

This commit is contained in:
Fabian 2021-04-07 14:49:00 +02:00 committed by Joel Martin
parent f3e4237e8d
commit 4594bff0e3
4 changed files with 36 additions and 40 deletions

View File

@ -78,8 +78,7 @@ fun scanSpecial ss =
end
fun scanString ss =
Ss.getc ss
|> Option.mapPartial (fn (#"\"", rest) => spanString rest rest | _ => NONE)
Ss.getc ss |> Option.mapPartial (fn (#"\"", rest) => spanString rest rest | _ => NONE)
and spanString from to = case Ss.getc to of
SOME (#"\\", rest) => Ss.getc rest |> Option.mapPartial (fn (_, more) => spanString from more)
@ -104,10 +103,9 @@ fun scanToken ss =
end
fun tokenize s = tokenize' [] (Ss.full s)
and tokenize' acc ss =
case scanToken ss of
SOME (token, rest) => tokenize' (token::acc) rest
| NONE => rev acc
and tokenize' acc ss = case scanToken ss of
SOME (token, rest) => tokenize' (token::acc) rest
| NONE => rev acc
fun readAtom r = case next r of
SOME (LIT_ATOM "nil", r') => (NIL, r')
@ -121,17 +119,16 @@ fun readAtom r = case next r of
| SOME (token, _) => raise SyntaxError ("unexpected token reading atom: " ^ (tokenString token))
| NONE => raise SyntaxError "end of input reached when reading atom"
and readForm r =
case peek r of
SOME PAREN_LEFT => readList [] (rest r)
| SOME BRACKET_LEFT => readVector [] (rest r)
| SOME BRACE_LEFT => readMap [] (rest r)
| SOME AT => let val (a, r') = readAtom (rest r) in (malList [SYMBOL "deref", a], r') end
| SOME QUOTE => let val (a, r') = readForm (rest r) in (malList [SYMBOL "quote", a], r') end
| SOME BACK_TICK => let val (a, r') = readForm (rest r) in (malList [SYMBOL "quasiquote", a], r') end
| SOME TILDE => let val (a, r') = readForm (rest r) in (malList [SYMBOL "unquote", a], r') end
| SOME TILDE_AT => let val (a, r') = readForm (rest r) in (malList [SYMBOL "splice-unquote", a], r') end
| _ => readAtom r
and readForm r = case peek r of
SOME PAREN_LEFT => readList [] (rest r)
| SOME BRACKET_LEFT => readVector [] (rest r)
| SOME BRACE_LEFT => readMap [] (rest r)
| SOME AT => let val (a, r') = readAtom (rest r) in (malList [SYMBOL "deref", a], r') end
| SOME QUOTE => let val (a, r') = readForm (rest r) in (malList [SYMBOL "quote", a], r') end
| SOME BACK_TICK => let val (a, r') = readForm (rest r) in (malList [SYMBOL "quasiquote", a], r') end
| SOME TILDE => let val (a, r') = readForm (rest r) in (malList [SYMBOL "unquote", a], r') end
| SOME TILDE_AT => let val (a, r') = readForm (rest r) in (malList [SYMBOL "splice-unquote", a], r') end
| _ => readAtom r
and readWithMeta r =
let val (m, r') = readForm r
@ -159,7 +156,6 @@ fun clean ts =
ts |> List.filter (fn x => x <> SPACE)
|> List.filter (fn COMMENT _ => false | _ => true)
fun readStr s =
case tokenize s |> clean of
[] => raise Nothing
| ts => ts |> READER |> readForm |> #1
fun readStr s = case tokenize s |> clean of
[] => raise Nothing
| ts => ts |> READER |> readForm |> #1

View File

@ -1,14 +1,14 @@
fun READ s: string =
fun read s: string =
s
fun EVAL s: string =
fun eval s: string =
s
fun PRINT s: string =
fun print s: string =
s
fun rep s: string =
(PRINT o EVAL o READ) s
(print o eval o read) s
fun repl () =
let open TextIO

View File

@ -1,14 +1,14 @@
fun READ s =
fun read s =
readStr s
fun EVAL f =
fun eval f =
f
fun PRINT f =
fun print f =
prReadableStr f
fun rep s =
s |> READ |> EVAL |> PRINT
s |> read |> eval |> print
handle SyntaxError msg => "SYNTAX ERROR: " ^ msg
| Nothing => ""

View File

@ -1,29 +1,29 @@
exception NotDefined of string
exception NotApplicable of string
fun READ s =
fun read s =
readStr s
fun EVAL e ast = case ast of
LIST (_::_,_) => eval_apply e ast
| _ => eval_ast e ast
fun eval e ast = case ast of
LIST (_::_,_) => evalApply e ast
| _ => evalAst e ast
and eval_ast e ast = case ast of
and evalAst e ast = case ast of
SYMBOL s => (case lookup e s of SOME v => v | NONE => raise NotDefined ("unable to resolve symbol '" ^ s ^ "'"))
| LIST (l,_) => LIST (List.map (EVAL e) l, NO_META)
| VECTOR (v,_) => VECTOR (List.map (EVAL e) v, NO_META)
| MAP (m,_) => MAP (List.map (fn (k, v) => (EVAL e k, EVAL e v)) m, NO_META)
| LIST (l,_) => LIST (List.map (eval e) l, NO_META)
| VECTOR (v,_) => VECTOR (List.map (eval e) v, NO_META)
| MAP (m,_) => MAP (List.map (fn (k, v) => (eval e k, eval e v)) m, NO_META)
| _ => ast
and eval_apply e ast = case eval_ast e ast of
and evalApply e ast = case evalAst e ast of
LIST ((FN (f,_))::args, _) => f args
| _ => raise NotApplicable "eval_apply needs a non-empty list"
fun PRINT f =
fun print f =
prReadableStr f
fun rep e s =
s |> READ |> EVAL e |> PRINT
s |> read |> eval e |> print
handle Nothing => ""
| e => "ERROR: " ^ (exnMessage e)