Skip to content

Commit

Permalink
rename makeList etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabjan authored and kanaka committed May 2, 2021
1 parent ff01165 commit c75c9eb
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 75 deletions.
38 changes: 19 additions & 19 deletions impls/sml/core.sml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ exception MalException of mal_type
*)

fun buildMap (k::v::rest) acc = buildMap rest (malAssoc acc k v)
| buildMap [] acc = makeMap (rev acc)
| buildMap [] acc = malMap (rev acc)
| buildMap _ _ = raise NotApplicable "maps can only be constructed from an even number of arguments"

fun collectLists ls = collectLists' ls []
Expand Down Expand Up @@ -121,22 +121,22 @@ val coreNs = List.concat [
prim "swap!" (fn (ATOM a::(FN (f,_))::args) => let val x = f ((!a)::args) in (a := x; x) end | _ => raise Domain),

(* Listoids *)
prim "list" (fn args => makeList args),
prim "vector" (fn args => makeVector (args)),
prim "vec" (fn [LIST (xs,_)] => makeVector (xs) | [v as VECTOR _] => v | _ => raise Domain),
prim "concat" (fn args => makeList (List.concat (collectLists args))),
prim "list" (fn args => malList args),
prim "vector" (fn args => malVector (args)),
prim "vec" (fn [LIST (xs,_)] => malVector (xs) | [v as VECTOR _] => v | _ => raise Domain),
prim "concat" (fn args => malList (List.concat (collectLists args))),
prim "cons"
(fn [hd, LIST (tl,_)] => makeList (hd::tl)
| [hd, VECTOR (tl,_)] => makeList (hd::tl)
(fn [hd, LIST (tl,_)] => malList (hd::tl)
| [hd, VECTOR (tl,_)] => malList (hd::tl)
| _ => raise Domain),
prim "conj"
(fn (LIST (l,_)::args) => makeList (rev args @ l)
| (VECTOR (v,_)::args) => makeVector (v @ args)
(fn (LIST (l,_)::args) => malList (rev args @ l)
| (VECTOR (v,_)::args) => malVector (v @ args)
| _ => raise Domain),
prim "seq"
(fn [LIST ([],_)] => NIL | [l as LIST _] => l
| [VECTOR ([],_)] => NIL | [VECTOR (v,_)] => makeList v
| [STRING ""] => NIL | [STRING s] => String.explode s |> List.map (STRING o String.str) |> makeList
| [VECTOR ([],_)] => NIL | [VECTOR (v,_)] => malList v
| [STRING ""] => NIL | [STRING s] => String.explode s |> List.map (STRING o String.str) |> malList
| [NIL] => NIL
| _ => raise Domain),
prim "count"
Expand All @@ -154,13 +154,13 @@ val coreNs = List.concat [
| [NIL] => NIL
| _ => raise Domain),
prim "rest"
(fn [LIST (l,_)] => makeList (case l of (_::xs) => xs | _ => [])
| [VECTOR (v,_)] => makeList (case v of (_::xs) => xs | _ => [])
| [NIL] => makeList ([])
(fn [LIST (l,_)] => malList (case l of (_::xs) => xs | _ => [])
| [VECTOR (v,_)] => malList (case v of (_::xs) => xs | _ => [])
| [NIL] => malList ([])
| _ => raise Domain),
prim "map"
(fn [FN (f,_), LIST (l,_)] => makeList (List.map (fn x => f [x]) l)
| [FN (f,_), VECTOR (v,_)] => makeList (List.map (fn x => f [x]) v)
(fn [FN (f,_), LIST (l,_)] => malList (List.map (fn x => f [x]) l)
| [FN (f,_), VECTOR (v,_)] => malList (List.map (fn x => f [x]) v)
| _ => raise Domain),

(* Maps *)
Expand All @@ -169,13 +169,13 @@ val coreNs = List.concat [
prim "assoc"
(fn (MAP (m,_)::(args as _::_)) => buildMap args m | _ => raise Domain),
prim "dissoc"
(fn (MAP (m,_)::(args as _::_)) => makeMap (foldl (fn (k, acc) => malDissoc acc k) m args) | _ => raise Domain),
(fn (MAP (m,_)::(args as _::_)) => malMap (foldl (fn (k, acc) => malDissoc acc k) m args) | _ => raise Domain),
prim "get"
(fn [MAP (m,_), k] => valOrElse (malGet m k) (fn () => NIL) | [NIL, _] => NIL | _ => raise Domain),
prim "keys"
(fn [MAP (m,_)] => makeList (map #1 m) | _ => raise Domain),
(fn [MAP (m,_)] => malList (map #1 m) | _ => raise Domain),
prim "vals"
(fn [MAP (m,_)] => makeList (map #2 m) | _ => raise Domain),
(fn [MAP (m,_)] => malList (map #2 m) | _ => raise Domain),

(* Metaprogramming and metadata *)
prim "read-string"
Expand Down
12 changes: 6 additions & 6 deletions impls/sml/reader.sml
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ and readForm r =
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 (makeList [SYMBOL "deref", a], r') end
| SOME QUOTE => let val (a, r') = readForm (rest r) in (makeList [SYMBOL "quote", a], r') end
| SOME BACK_TICK => let val (a, r') = readForm (rest r) in (makeList [SYMBOL "quasiquote", a], r') end
| SOME TILDE => let val (a, r') = readForm (rest r) in (makeList [SYMBOL "unquote", a], r') end
| SOME TILDE_AT => let val (a, r') = readForm (rest r) in (makeList [SYMBOL "splice-unquote", a], r') end
| 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
val (v, r'') = readForm r'
in
(makeList [SYMBOL "with-meta", v, m], r'')
(malList [SYMBOL "with-meta", v, m], r'')
end

and readList acc r =
Expand Down
22 changes: 11 additions & 11 deletions impls/sml/step7_quote.sml
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ and evalQuote e [x] = x
and evalQuasiquote e args = eval e (expandQuasiquote args)

and expandQuasiquote [LIST ([SYMBOL "unquote", x],_)] = x
| expandQuasiquote [LIST (l,_)] = makeList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = makeList [SYMBOL "vec", makeList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = makeList ([SYMBOL "quote", m])
| expandQuasiquote [s as SYMBOL _] = makeList ([SYMBOL "quote", s])
| expandQuasiquote [LIST (l,_)] = malList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = malList [SYMBOL "vec", malList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = malList ([SYMBOL "quote", m])
| expandQuasiquote [s as SYMBOL _] = malList ([SYMBOL "quote", s])
| expandQuasiquote [x] = x
| expandQuasiquote _ = raise NotApplicable "quasiquote needs one argument"
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, makeList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], makeList acc]
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, malList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], malList acc]

and evalApply e (FN (f,_)) args = f (map (eval e) args)
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (makeList args))
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (malList args))

and evalSymbol e s = valOrElse (lookup e s)
(fn _ => raise NotDefined ("symbol '" ^ s ^ "' not found"))

and bindLet args e = bind' (eval e) args e
and bind args e = bind' identity args e
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (makeList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (makeList []) e; e)
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (malList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (malList []) e; e)
| bind' evl (SYMBOL s::v::rest) e = (def s (evl v) e; bind' evl rest e)
| bind' _ [] e = e
| bind' _ _ _ = raise NotApplicable "bindings must be a list of symbol/form pairs"
Expand Down Expand Up @@ -107,12 +107,12 @@ fun main () = (
rep replEnv ("(do " ^ prelude ^ " nil)");
case CommandLine.arguments () of
prog::args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
rep replEnv ("(load-file \"" ^ prog ^ "\")");
()
)
| args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
repl replEnv
)
)
22 changes: 11 additions & 11 deletions impls/sml/step8_macros.sml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ and evalQuote e [x] = x
and evalQuasiquote e args = eval e (expandQuasiquote args)

and expandQuasiquote [LIST ([SYMBOL "unquote", x],_)] = x
| expandQuasiquote [LIST (l,_)] = makeList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = makeList [SYMBOL "vec", makeList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = makeList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = makeList [SYMBOL "quote", s]
| expandQuasiquote [LIST (l,_)] = malList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = malList [SYMBOL "vec", malList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = malList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = malList [SYMBOL "quote", s]
| expandQuasiquote [x] = x
| expandQuasiquote _ = raise NotApplicable "quasiquote needs one argument"
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, makeList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], makeList acc]
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, malList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], malList acc]

and evalDefmacro e [SYMBOL s, ast] = defMacro e s (eval e ast)
| evalDefmacro _ _ = raise NotApplicable "defmacro! needs a name, and a fn*"
Expand All @@ -65,15 +65,15 @@ and expandMacro e [(ast as LIST (SYMBOL s::args, _))] = (case lookup e s of SOME
| expandMacro _ _ = raise NotApplicable "macroexpand needs one argument"

and evalApply e (FN (f,_)) args = f (map (eval e) args)
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (makeList args))
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (malList args))

and evalSymbol e s = valOrElse (lookup e s)
(fn _ => raise NotDefined ("symbol '" ^ s ^ "' not found"))

and bindLet args e = bind' (eval e) args e
and bind args e = bind' identity args e
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (makeList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (makeList []) e; e)
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (malList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (malList []) e; e)
| bind' evl (SYMBOL s::v::rest) e = (def s (evl v) e; bind' evl rest e)
| bind' _ [] e = e
| bind' _ _ _ = raise NotApplicable "bindings must be a list of symbol/form pairs"
Expand Down Expand Up @@ -133,12 +133,12 @@ fun main () = (
rep replEnv ("(do " ^ prelude ^ " nil)");
case CommandLine.arguments () of
prog::args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
rep replEnv ("(load-file \"" ^ prog ^ "\")");
()
)
| args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
repl replEnv
)
)
22 changes: 11 additions & 11 deletions impls/sml/step9_try.sml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ and evalQuote e [x] = x
and evalQuasiquote e args = eval e (expandQuasiquote args)

and expandQuasiquote [LIST ([SYMBOL "unquote", x],_)] = x
| expandQuasiquote [LIST (l,_)] = makeList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = makeList [SYMBOL "vec", makeList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = makeList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = makeList [SYMBOL "quote", s]
| expandQuasiquote [LIST (l,_)] = malList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = malList [SYMBOL "vec", malList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = malList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = malList [SYMBOL "quote", s]
| expandQuasiquote [x] = x
| expandQuasiquote _ = raise NotApplicable "quasiquote needs one argument"
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, makeList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], makeList acc]
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, malList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], malList acc]

and evalDefmacro e [SYMBOL s, ast] = defMacro e s (eval e ast)
| evalDefmacro _ _ = raise NotApplicable "defmacro! needs a name, and a fn*"
Expand All @@ -77,15 +77,15 @@ and exnVal (MalException x) = x
| exnVal exn = STRING (exnMessage exn)

and evalApply e (FN (f,_)) args = f (map (eval e) args)
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (makeList args))
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (malList args))

and evalSymbol e s = valOrElse (lookup e s)
(fn _ => raise NotDefined ("'" ^ s ^ "' not found"))

and bindLet args e = bind' (eval e) args e
and bind args e = bind' identity args e
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (makeList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (makeList []) e; e)
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (malList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (malList []) e; e)
| bind' evl (SYMBOL s::v::rest) e = (def s (evl v) e; bind' evl rest e)
| bind' _ [] e = e
| bind' _ _ _ = raise NotApplicable "bindings must be a list of symbol/form pairs"
Expand Down Expand Up @@ -146,12 +146,12 @@ fun main () = (
rep replEnv ("(do " ^ prelude ^ " nil)");
case CommandLine.arguments () of
prog::args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
rep replEnv ("(load-file \"" ^ prog ^ "\")");
()
)
| args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
repl replEnv
)
)
22 changes: 11 additions & 11 deletions impls/sml/stepA_mal.sml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ and evalQuote e [x] = x
and evalQuasiquote e args = eval e (expandQuasiquote args)

and expandQuasiquote [LIST ([SYMBOL "unquote", x],_)] = x
| expandQuasiquote [LIST (l,_)] = makeList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = makeList [SYMBOL "vec", makeList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = makeList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = makeList [SYMBOL "quote", s]
| expandQuasiquote [LIST (l,_)] = malList (foldr quasiFolder [] l)
| expandQuasiquote [VECTOR (v,_)] = malList [SYMBOL "vec", malList (foldr quasiFolder [] v)]
| expandQuasiquote [m as MAP _] = malList [SYMBOL "quote", m]
| expandQuasiquote [s as SYMBOL _] = malList [SYMBOL "quote", s]
| expandQuasiquote [x] = x
| expandQuasiquote _ = raise NotApplicable "quasiquote needs one argument"
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, makeList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], makeList acc]
and quasiFolder (LIST ([SYMBOL "splice-unquote", x],_), acc) = [SYMBOL "concat", x, malList acc]
| quasiFolder (x, acc) = [SYMBOL "cons", expandQuasiquote [x], malList acc]

and evalDefmacro e [SYMBOL s, ast] = defMacro e s (eval e ast)
| evalDefmacro _ _ = raise NotApplicable "defmacro! needs a name, and a fn*"
Expand All @@ -77,15 +77,15 @@ and exnVal (MalException x) = x
| exnVal exn = STRING (exnMessage exn)

and evalApply e (FN (f,_)) args = f (map (eval e) args)
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (makeList args))
| evalApply _ x args = raise NotApplicable (prStr x ^ " is not applicable on " ^ prStr (malList args))

and evalSymbol e s = valOrElse (lookup e s)
(fn _ => raise NotDefined ("'" ^ s ^ "' not found"))

and bindLet args e = bind' (eval e) args e
and bind args e = bind' identity args e
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (makeList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (makeList []) e; e)
and bind' evl (SYMBOL "&"::v::(SYMBOL s)::vs) e = (def s (malList (map evl (v::vs))) e; e)
| bind' _ [SYMBOL "&", SYMBOL s] e = (def s (malList []) e; e)
| bind' evl (SYMBOL s::v::rest) e = (def s (evl v) e; bind' evl rest e)
| bind' _ [] e = e
| bind' _ _ _ = raise NotApplicable "bindings must be a list of symbol/form pairs"
Expand Down Expand Up @@ -146,12 +146,12 @@ fun main () = (
rep replEnv ("(do " ^ prelude ^ " nil)");
case CommandLine.arguments () of
prog::args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
rep replEnv ("(load-file \"" ^ prog ^ "\")");
()
)
| args => (
def "*ARGV*" (makeList (map STRING args)) replEnv;
def "*ARGV*" (malList (map STRING args)) replEnv;
def "*host-language*" (STRING "sml") replEnv;
rep replEnv "(println (str \"Mal [\" *host-language* \"]\"))";
repl replEnv
Expand Down
10 changes: 4 additions & 6 deletions impls/sml/types.sml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@ fun malEq ( NIL, NIL) = true
| malEq ( LIST (a,_), VECTOR (b,_)) = ListPair.allEq malEq (a, b)
| malEq (VECTOR (a,_), LIST (b,_)) = ListPair.allEq malEq (a, b)
| malEq ( MAP (a,_), MAP (b,_)) = mapEq a b
| malEq _ = false
| malEq _ = false
and mapEq a b =
a |> List.map (fn (k,va) => (va, malGet b k)) |> List.all (fn (va,SOME vb) => malEq (va, vb) | _ => false) andalso
b |> List.map (fn (k,vb) => (vb, malGet a k)) |> List.all (fn (vb,SOME va) => malEq (vb, va) | _ => false)

and malGet m k = m |> List.find (fn (k',_) => malEq (k, k')) |> Option.map #2

and malAssoc m k v = (k, v) :: (malDissoc m k)

and malDissoc m k = m |> List.filter (not o (fn (k', _) => malEq (k, k')))

and makeList l = LIST (l, NO_META)
fun makeVector xs = VECTOR (xs, NO_META)
fun makeMap kvps = MAP (kvps, NO_META)
fun malList xs = LIST (xs, NO_META)
fun malVector xs = VECTOR (xs, NO_META)
fun malMap kvps = MAP (kvps, NO_META)

0 comments on commit c75c9eb

Please sign in to comment.