1 ! Copyright (C) 2015 Jordan Lewis.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators combinators.short-circuit
4 continuations fry grouping hashtables io kernel locals lib.env lib.printer
5 lib.reader lib.types math namespaces quotations readline sequences ;
8 CONSTANT: repl-bindings H{
19 : eval-ast ( ast env -- ast )
21 { [ over malsymbol? ] [ env-get ] }
22 { [ over sequence? ] [ '[ _ EVAL ] map ] }
23 { [ over assoc? ] [ '[ [ _ EVAL ] bi@ ] assoc-map ] }
27 :: eval-def! ( key value env -- maltype )
28 value env EVAL [ key env env-set ] keep ;
30 : eval-let* ( bindings body env -- maltype )
31 [ swap 2 group ] [ new-env ] bi* [
32 dup '[ first2 _ EVAL swap _ env-set ] each
35 : READ ( str -- maltype ) read-str ;
37 :: EVAL ( maltype env -- maltype )
38 maltype dup { [ array? ] [ empty? not ] } 1&& [
39 unclip dup dup malsymbol? [ name>> ] when {
40 { "def!" [ drop first2 env eval-def! ] }
41 { "let*" [ drop first2 env eval-let* ] }
43 drop env eval-ast dup quotation? [
44 [ env eval-ast ] dip with-datastack first
54 : PRINT ( maltype -- str ) pr-str ;
58 READ repl-env get EVAL PRINT
60 nip pr-str "Error: " swap append
64 f repl-bindings <malenv> repl-env set
67 [ REP print flush ] unless-empty