Skip to content

Commit cb2dabe

Browse files
committed
add sample, easier input
1 parent 372db69 commit cb2dabe

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

‎lisp.sml‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ withtype map = (key * sexp_i) list
2020
(* (globalenv * localenv) * heap * ctr *)
2121
and context = ((map * map) * map * int)
2222

23+
datatype input_sexp = NIL
24+
| TRUE
25+
| LIST of input_sexp list
26+
| NUM of int
27+
| SYM of string
28+
29+
fun to_sdata NIL = EXP NIL_I
30+
| to_sdata TRUE = EXP TRUE_I
31+
| to_sdata (NUM n) = EXP (NUM_I n)
32+
| to_sdata (SYM s) = EXP (SYM_I s)
33+
| to_sdata (LIST (x::xs)) =
34+
let val (EXP (LIST_I rest)) = to_sdata (LIST xs)
35+
in EXP (LIST_I ((to_sdata x) :: rest)) end
36+
2337
fun make_key (SYM_I s) = SYM_K s
2438
| make_key (GENSYM_I n) = GENSYM_K n
2539
| make_key _ = raise Bad_key;
@@ -220,11 +234,31 @@ val init_ctx =
220234
val ctx = defprim ctx "set" prim_set
221235
val (globalenv, heap, ctr) = ctx
222236
in
223-
((globalenv, []), heap, ctr)
237+
((globalenv, ([]:map)), heap, ctr)
224238
end
225239

226240
(* eval : ctx -> exp sdata -> val sdata *)
227241
fun eval ctx x =
228242
case smallstep ctx x of
229243
(_, VAL v) => VAL v
230244
| (_, EXP e) => eval ctx (EXP e)
245+
246+
(* Sample program: factorial
247+
248+
(progn
249+
(define (fact n)
250+
(cond
251+
((eq n 0) 1)
252+
((quote otherwise) ( * n (fact (- n 1))))))
253+
(fact 5))
254+
255+
*)
256+
257+
val sample = to_sdata (
258+
LIST [SYM "progn",
259+
LIST [SYM "define", LIST [SYM "fact", SYM "n"],
260+
LIST [SYM "cond",
261+
LIST [LIST [SYM "eq", SYM "n", NUM 0], NUM 1],
262+
LIST [TRUE, LIST [SYM "*", SYM "n", LIST [SYM "fact", LIST [SYM "-", SYM "n", NUM 1]]]]]],
263+
LIST [SYM "fact", NUM 5]]);
264+

0 commit comments

Comments
 (0)