factor/unmaintained/lisp/lisp.factor

178 lines
5.3 KiB
Factor
Raw Normal View History

2008-04-17 02:37:03 -04:00
! Copyright (C) 2008 James Cash
! See http://factorcode.org/license.txt for BSD license.
2008-10-02 18:24:55 -04:00
USING: kernel peg sequences arrays strings
namespaces combinators math locals locals.private locals.backend accessors
2008-10-02 18:24:55 -04:00
vectors syntax lisp.parser assocs parser words
quotations fry lists summary combinators.short-circuit continuations multiline ;
2008-04-17 12:37:31 -04:00
IN: lisp
2008-04-17 02:37:03 -04:00
2008-04-21 20:25:55 -04:00
DEFER: convert-form
2008-04-30 16:59:50 -04:00
DEFER: funcall
2008-05-18 12:53:44 -04:00
DEFER: lookup-var
DEFER: lookup-macro
2008-06-05 20:38:12 -04:00
DEFER: lisp-macro?
DEFER: lisp-var?
2008-06-05 18:15:05 -04:00
DEFER: define-lisp-macro
2008-07-02 23:54:06 -04:00
2008-04-30 16:59:50 -04:00
! Functions to convert s-exps to quotations
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: convert-body ( cons -- quot )
2008-06-05 04:14:26 -04:00
[ ] [ convert-form compose ] foldl ; inline
2008-07-02 23:54:06 -04:00
: convert-cond ( cons -- quot )
2008-06-18 15:09:02 -04:00
cdr [ 2car [ convert-form ] bi@ 2array ]
2008-09-10 23:11:40 -04:00
{ } lmap-as '[ _ cond ] ;
2008-07-02 23:54:06 -04:00
: convert-general-form ( cons -- quot )
2008-09-10 23:11:40 -04:00
uncons [ convert-body ] [ convert-form ] bi* '[ _ @ funcall ] ;
2008-05-15 22:14:53 -04:00
2008-07-02 23:54:06 -04:00
! words for convert-lambda
<PRIVATE
: localize-body ( assoc body -- newbody )
{
2008-09-10 23:11:40 -04:00
{ [ dup list? ] [ [ lisp-symbol? ] rot '[ [ name>> _ at ] [ ] bi or ] traverse ] }
{ [ dup lisp-symbol? ] [ name>> swap at ] }
[ nip ]
} cond ;
2008-06-05 18:15:05 -04:00
: localize-lambda ( body vars -- newvars newbody )
swap [ make-locals dup push-locals ] dip
dupd [ localize-body convert-form ] with lmap>array
>quotation swap pop-locals ;
2008-07-02 23:54:06 -04:00
: split-lambda ( cons -- body-cons vars-seq )
cdr uncons [ name>> ] lmap>array ; inline
2008-07-02 23:54:06 -04:00
2008-06-03 23:41:05 -04:00
: rest-lambda ( body vars -- quot )
"&rest" swap [ remove ] [ index ] 2bi
[ localize-lambda <lambda> lambda-rewrite call ] dip
2008-09-10 23:11:40 -04:00
swap '[ _ cut '[ @ _ seq>list ] call _ call call ] 1quotation ;
2008-07-02 23:54:06 -04:00
2008-05-11 19:38:38 -04:00
: normal-lambda ( body vars -- quot )
2008-08-23 13:03:09 -04:00
localize-lambda <lambda> lambda-rewrite '[ @ compose call call ] 1quotation ;
2008-05-15 22:14:53 -04:00
PRIVATE>
2008-07-02 23:54:06 -04:00
: convert-lambda ( cons -- quot )
2008-05-26 04:35:18 -04:00
split-lambda "&rest" over member? [ rest-lambda ] [ normal-lambda ] if ;
2008-07-02 23:54:06 -04:00
: convert-quoted ( cons -- quot )
cadr 1quotation ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: convert-defmacro ( cons -- quot )
2008-09-03 10:21:27 -04:00
cdr [ convert-lambda ] [ car name>> ] bi define-lisp-macro [ ] ;
2008-07-02 23:54:06 -04:00
2008-06-18 15:09:02 -04:00
: macro-expand ( cons -- quot )
2008-08-23 13:03:09 -04:00
uncons [ list>seq >quotation ] [ lookup-macro ] bi* call call ;
2008-09-03 10:21:27 -04:00
: expand-macros ( cons -- cons )
2008-09-14 21:28:36 -04:00
dup list? [ [ expand-macros ] lmap dup car lisp-macro? [ macro-expand expand-macros ] when ] when ;
2008-09-03 10:21:27 -04:00
: convert-begin ( cons -- quot )
cdr [ convert-form ] [ ] lmap-as [ 1 tail* ] [ but-last ] bi
2008-09-10 23:11:40 -04:00
[ '[ { } _ with-datastack drop ] ] map prepend '[ _ [ call ] each ] ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: form-dispatch ( cons lisp-symbol -- quot )
2008-05-30 01:44:54 -04:00
name>>
{ { "lambda" [ convert-lambda ] }
2008-06-05 18:15:05 -04:00
{ "defmacro" [ convert-defmacro ] }
2008-05-30 01:44:54 -04:00
{ "quote" [ convert-quoted ] }
{ "cond" [ convert-cond ] }
{ "begin" [ convert-begin ] }
2008-05-30 01:44:54 -04:00
[ drop convert-general-form ]
} case ;
2008-07-02 23:54:06 -04:00
: convert-list-form ( cons -- quot )
dup car
2008-09-03 10:21:27 -04:00
{
2008-07-02 23:54:06 -04:00
{ [ dup lisp-symbol? ] [ form-dispatch ] }
2008-05-30 01:44:54 -04:00
[ drop convert-general-form ]
} cond ;
2008-07-02 23:54:06 -04:00
2008-04-21 20:25:55 -04:00
: convert-form ( lisp-form -- quot )
{
{ [ dup cons? ] [ convert-list-form ] }
{ [ dup lisp-var? ] [ lookup-var 1quotation ] }
2008-09-10 23:11:40 -04:00
{ [ dup lisp-symbol? ] [ '[ _ lookup-var ] ] }
[ 1quotation ]
2008-05-26 04:35:18 -04:00
} cond ;
: lisp-string>factor ( str -- quot )
2008-08-23 13:03:09 -04:00
lisp-expr expand-macros convert-form ;
: lisp-eval ( str -- * )
lisp-string>factor call ;
2008-07-02 23:54:06 -04:00
2008-04-30 16:59:50 -04:00
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2008-05-05 12:49:16 -04:00
SYMBOL: lisp-env
2008-06-03 23:41:05 -04:00
SYMBOL: macro-env
2008-07-02 23:54:06 -04:00
2008-06-18 12:13:28 -04:00
ERROR: no-such-var variable-name ;
M: no-such-var summary drop "No such variable" ;
2008-04-30 16:59:50 -04:00
2008-05-05 12:49:16 -04:00
: init-env ( -- )
2008-06-03 23:41:05 -04:00
H{ } clone lisp-env set
H{ } clone macro-env set ;
2008-04-30 16:59:50 -04:00
2008-06-05 18:15:05 -04:00
: lisp-define ( quot name -- )
lisp-env get set-at ;
: define-lisp-var ( lisp-symbol body -- )
swap name>> lisp-define ;
2008-07-02 23:54:06 -04:00
2008-05-05 12:49:16 -04:00
: lisp-get ( name -- word )
2008-06-18 15:09:02 -04:00
lisp-env get at ;
2008-07-02 23:54:06 -04:00
2008-05-18 12:53:44 -04:00
: lookup-var ( lisp-symbol -- quot )
2008-06-18 15:09:02 -04:00
[ name>> ] [ lisp-var? ] bi [ lisp-get ] [ no-such-var ] if ;
2008-07-02 23:54:06 -04:00
: lisp-var? ( lisp-symbol -- ? )
dup lisp-symbol? [ name>> lisp-env get key? ] [ drop f ] if ;
2008-07-02 23:54:06 -04:00
2008-05-05 12:49:16 -04:00
: funcall ( quot sym -- * )
[ 1array [ call ] with-datastack >quotation ] dip curry call ; inline
2008-07-02 23:54:06 -04:00
: define-primitive ( name vocab word -- )
2008-09-10 23:11:40 -04:00
swap lookup 1quotation '[ _ compose call ] swap lisp-define ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: lookup-macro ( lisp-symbol -- lambda )
2008-06-03 23:41:05 -04:00
name>> macro-env get at ;
2008-07-02 23:54:06 -04:00
2008-06-05 18:15:05 -04:00
: define-lisp-macro ( quot name -- )
macro-env get set-at ;
2008-07-02 23:54:06 -04:00
2008-06-03 23:41:05 -04:00
: lisp-macro? ( car -- ? )
2008-06-04 12:47:53 -04:00
dup lisp-symbol? [ name>> macro-env get key? ] [ drop f ] if ;
2008-09-03 10:21:27 -04:00
: define-lisp-builtins ( -- )
2008-09-03 10:21:27 -04:00
init-env
f "#f" lisp-define
t "#t" lisp-define
"+" "math" "+" define-primitive
"-" "math" "-" define-primitive
"<" "math" "<" define-primitive
">" "math" ">" define-primitive
"cons" "lists" "cons" define-primitive
"car" "lists" "car" define-primitive
"cdr" "lists" "cdr" define-primitive
"append" "lists" "lappend" define-primitive
"nil" "lists" "nil" define-primitive
"nil?" "lists" "nil?" define-primitive
"set" "lisp" "define-lisp-var" define-primitive
2008-09-14 21:28:36 -04:00
"(set 'list (lambda (&rest xs) xs))" lisp-eval
"(defmacro setq (var val) (list 'set (list 'quote var) val))" lisp-eval
<" (defmacro defun (name vars &rest body)
2008-09-14 21:28:36 -04:00
(list 'setq name (cons 'lambda (cons vars body)))) "> lisp-eval
2008-09-14 21:28:36 -04:00
"(defmacro if (pred tr fl) (list 'cond (list pred tr) (list (quote #t) fl)))" lisp-eval
2008-09-03 10:21:27 -04:00
;
: <LISP
2008-09-14 21:28:36 -04:00
"LISP>" parse-multiline-string "(begin " prepend ")" append define-lisp-builtins
lisp-string>factor parsed \ call parsed ; parsing