 DEFER: lisp-macro?
 DEFER: lisp-var?
 DEFER: define-lisp-macro
-    
+
 ! Functions to convert s-exps to quotations
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 : convert-body ( cons -- quot )
     [ ] [ convert-form compose ] foldl ; inline
-    
-: convert-cond ( cons -- quot )  
+
+: convert-cond ( cons -- quot )
     cdr [ 2car [ convert-form ] bi@ 2array ]
     { } lmap-as '[ , cond ] ;
-    
+
 : convert-general-form ( cons -- quot )
     uncons [ convert-body ] [ convert-form ] bi* '[ , @ funcall ] ;
 
-! words for convert-lambda  
-<PRIVATE  
-: localize-body ( assoc body -- assoc newbody )  
+! words for convert-lambda
+<PRIVATE
+: localize-body ( assoc body -- assoc newbody )
     {
       { [ dup list? ] [ [ lisp-symbol? ] pick '[ [ name>> , at ] [ ] bi or ] traverse ] }
       { [ dup lisp-symbol? ] [ name>> over at ] }

      { "cond" [ convert-cond ] }
     [ drop convert-general-form ]
    } case ;
    
: convert-list-form ( cons -- quot )  
    dup car
    { { [ dup lisp-macro?  ] [ drop macro-expand ] }
      { [ dup lisp-symbol? ] [ form-dispatch ] } 
     [ drop convert-general-form ]
    } cond ;
    
: convert-form ( lisp-form -- quot )
    {
      { [ dup cons? ] [ convert-list-form ] }


: lisp-define ( quot name -- )
    lisp-env get set-at ;

: defun ( name quot -- name )
    over name>> lisp-define ;

: lisp-get ( name -- word )
    lisp-env get at ;

: lookup-var ( lisp-symbol -- quot )
    [ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;

: lisp-var? ( lisp-symbol -- ? )
    dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;

: funcall ( quot sym -- * )
    [ 1array [ call ] with-datastack >quotation ] dip
    dup lisp-symbol? [ lookup-var ] when curry call ; inline

: define-primitive ( name vocab word -- )
    swap lookup 1quotation '[ , compose call ] swap lisp-define ;

: lookup-macro ( lisp-symbol -- lambda )
    name>> macro-env get at ;

: define-lisp-macro ( quot name -- )
    macro-env get set-at ;

: lisp-macro? ( car -- ? )
    dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;
