From 4594bff0e3bb13a5f97aa5fb144e53b696cebbcb Mon Sep 17 00:00:00 2001 From: Fabian Date: Wed, 7 Apr 2021 14:49:00 +0200 Subject: [PATCH] formatting --- impls/sml/reader.sml | 38 +++++++++++++++------------------- impls/sml/step0_repl.sml | 8 +++---- impls/sml/step1_read_print.sml | 8 +++---- impls/sml/step2_eval.sml | 22 ++++++++++---------- 4 files changed, 36 insertions(+), 40 deletions(-) diff --git a/impls/sml/reader.sml b/impls/sml/reader.sml index 25075b64..a66b44b3 100644 --- a/impls/sml/reader.sml +++ b/impls/sml/reader.sml @@ -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 diff --git a/impls/sml/step0_repl.sml b/impls/sml/step0_repl.sml index 82406e83..3485ec9b 100644 --- a/impls/sml/step0_repl.sml +++ b/impls/sml/step0_repl.sml @@ -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 diff --git a/impls/sml/step1_read_print.sml b/impls/sml/step1_read_print.sml index 62ca7ac4..1b47c069 100644 --- a/impls/sml/step1_read_print.sml +++ b/impls/sml/step1_read_print.sml @@ -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 => "" diff --git a/impls/sml/step2_eval.sml b/impls/sml/step2_eval.sml index bdd05dfa..69fd2943 100644 --- a/impls/sml/step2_eval.sml +++ b/impls/sml/step2_eval.sml @@ -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)