1 ! Copyright (C) 2015 Jordan Lewis.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators
4 combinators.short-circuit command-line continuations fry
5 grouping hashtables io kernel lists locals lib.core lib.env
6 lib.printer lib.reader lib.types math namespaces quotations
7 readline sequences splitting vectors ;
14 GENERIC# eval-ast 1 ( ast env -- ast )
15 M: malsymbol eval-ast env-get ;
16 M: sequence eval-ast '[ _ EVAL ] map ;
17 M: assoc eval-ast '[ _ EVAL ] assoc-map ;
18 M: object eval-ast drop ;
20 :: eval-def! ( key value env -- maltype )
21 value env EVAL [ key env env-set ] keep ;
23 : eval-let* ( bindings body env -- maltype env )
24 [ swap 2 group ] [ new-env ] bi* [
25 dup '[ first2 _ EVAL swap _ env-set ] each
28 :: eval-do ( exprs env -- lastform env/f )
32 unclip-last [ env eval-ast drop ] dip env
35 :: eval-if ( params env -- maltype env/f )
36 params first env EVAL { f +nil+ } index not [
39 params length 2 > [ params third env ] [ nil f ] if
42 :: eval-fn* ( params env -- maltype )
43 env params first [ name>> ] map params second <malfn> ;
45 : args-split ( bindlist -- bindlist restbinding/f )
46 { "&" } split1 ?first ;
48 : make-bindings ( args bindlist restbinding/f -- bindingshash )
49 swapd [ over length cut [ zip ] dip ] dip
50 [ swap 2array suffix ] [ drop ] if* >hashtable ;
52 GENERIC# apply 0 ( args fn -- maltype newenv/f )
57 [ binds>> args-split make-bindings ] 2tri <malenv> ;
59 M: callable apply call( x -- y ) f ;
63 : qq_loop ( elt acc -- maltype )
67 [ "splice-unquote" over first symeq? ] } 0&& [
76 : qq_foldr ( xs -- maltype )
80 unclip swap qq_foldr qq_loop
83 GENERIC: quasiquote ( maltype -- maltype )
85 { [ dup length 2 = ] [ "unquote" over first symeq? ] } 0&&
86 [ second ] [ qq_foldr ] if ;
87 M: vector quasiquote qq_foldr "vec" <malsymbol> swap 2array ;
88 M: malsymbol quasiquote "quote" <malsymbol> swap 2array ;
89 M: assoc quasiquote "quote" <malsymbol> swap 2array ;
90 M: object quasiquote ;
92 : READ ( str -- maltype ) read-str ;
94 : EVAL ( maltype env -- maltype )
95 over { [ array? ] [ empty? not ] } 1&& [
96 over first dup malsymbol? [ name>> ] when {
97 { "def!" [ [ rest first2 ] dip eval-def! f ] }
98 { "let*" [ [ rest first2 ] dip eval-let* ] }
99 { "do" [ [ rest ] dip eval-do ] }
100 { "if" [ [ rest ] dip eval-if ] }
101 { "fn*" [ [ rest ] dip eval-fn* f ] }
102 { "quote" [ drop second f ] }
103 { "quasiquoteexpand" [ drop second quasiquote f ] }
104 { "quasiquote" [ [ second quasiquote ] dip ] }
105 [ drop '[ _ EVAL ] map unclip apply ]
106 } case [ EVAL ] when*
111 [ apply [ EVAL ] when* ] mal-apply set-global
113 : PRINT ( maltype -- str ) pr-str ;
117 READ repl-env get EVAL PRINT
119 nip pr-str "Error: " swap append
125 [ REP print flush ] unless-empty
132 [ first "(load-file \"" "\")" surround REP drop ]
136 [ first repl-env get EVAL ] "eval" pick set-at
137 command-line get dup empty? [ rest ] unless "*ARGV*" pick set-at
138 <malenv> repl-env set-global
141 (def! not (fn* (a) (if a false true)))
142 (def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\\nnil)\")))))
143 " string-lines harvest [ REP drop ] each