From be06663629def065abe5e4e827f1f7d6c92085f0 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Thu, 15 May 2008 21:21:33 -0400 Subject: [PATCH 01/10] Writing docs for lisp and lisp.parser --- extra/lisp/lisp-docs.factor | 15 +++++++++++++++ extra/lisp/parser/parser-docs.factor | 6 ++++++ 2 files changed, 21 insertions(+) create mode 100644 extra/lisp/lisp-docs.factor create mode 100644 extra/lisp/parser/parser-docs.factor diff --git a/extra/lisp/lisp-docs.factor b/extra/lisp/lisp-docs.factor new file mode 100644 index 0000000000..149f22864e --- /dev/null +++ b/extra/lisp/lisp-docs.factor @@ -0,0 +1,15 @@ +IN: lisp +USING: help.markup help.syntax ; + +ARTICLE: "lisp" "Lisp in Factor" +"This is a simple implementation of a Lisp dialect, which somewhat resembles Scheme." $nl +"It works in two main stages: " +{ $list + { "Parse (via " { $vocab-link "lisp.parser" } " the Lisp code into a " + { $snippet "s-exp" } " tuple." } + { "Transform the " { $snippet "s-exp" } " into a Factor quotation, via " { $link convert-form } } +} + +{ $subsection "lisp.parser" } ; + +ABOUT: "lisp" \ No newline at end of file diff --git a/extra/lisp/parser/parser-docs.factor b/extra/lisp/parser/parser-docs.factor new file mode 100644 index 0000000000..fc16a0a310 --- /dev/null +++ b/extra/lisp/parser/parser-docs.factor @@ -0,0 +1,6 @@ +IN: lisp.parser +USING: help.markup help.syntax ; + +ARTICLE: "lisp.parser" "Parsing strings of Lisp" +"This vocab uses " { $vocab-link "peg.ebnf" } " to turn strings of Lisp into " { $snippet "s-exp" } "s, which are then used by" +{ $vocab-link "lisp" } " to produce Factor quotations." ; \ No newline at end of file From 70ef7d005c79d5e8e09b07eb4bdf8a93859c674c Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Thu, 15 May 2008 22:14:33 -0400 Subject: [PATCH 02/10] Changing wording of summary --- extra/lisp/summary.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/lisp/summary.txt b/extra/lisp/summary.txt index 8c36217f1c..7277c2a5b5 100644 --- a/extra/lisp/summary.txt +++ b/extra/lisp/summary.txt @@ -1 +1 @@ -A Lisp interpreter in Factor +A Lisp interpreter/compiler in Factor From 88576aefe45d74dbc6f24a792e3f6a514e5f79b7 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Thu, 15 May 2008 22:14:53 -0400 Subject: [PATCH 03/10] Adding define-primitive word --- extra/lisp/lisp-tests.factor | 10 ++++------ extra/lisp/lisp.factor | 17 ++++++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/extra/lisp/lisp-tests.factor b/extra/lisp/lisp-tests.factor index df37de2475..06c2260d72 100644 --- a/extra/lisp/lisp-tests.factor +++ b/extra/lisp/lisp-tests.factor @@ -6,14 +6,12 @@ IN: lisp.test init-env -"+" [ first2 + ] lisp-define +"+" "math" "+" define-primitve -{ [ first2 + ] } [ - "+" lisp-get +{ 5 } [ + [ 2 3 ] "+" <lisp-symbol> funcall ] unit-test { 3 } [ - [ - "((lambda (x y) (+ x y)) 1 2)" lisp-string>factor call - ] with-interactive-vocabs + "((lambda (x y) (+ x y)) 1 2)" lisp-string>factor call ] unit-test \ No newline at end of file diff --git a/extra/lisp/lisp.factor b/extra/lisp/lisp.factor index 3e4cdca41f..32df8c5102 100644 --- a/extra/lisp/lisp.factor +++ b/extra/lisp/lisp.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: kernel peg sequences arrays strings combinators.lib namespaces combinators math bake locals locals.private accessors -vectors syntax lisp.parser assocs parser sequences.lib ; +vectors syntax lisp.parser assocs parser sequences.lib words ; IN: lisp DEFER: convert-form @@ -24,7 +24,8 @@ DEFER: funcall : convert-general-form ( s-exp -- quot ) unclip convert-form swap convert-body [ , % funcall ] bake ; - + +! words for convert-lambda <PRIVATE : localize-body ( assoc body -- assoc newbody ) [ dup lisp-symbol? [ over dupd [ name>> ] dip at swap or ] @@ -34,8 +35,6 @@ DEFER: funcall : localize-lambda ( body vars -- newbody newvars ) make-locals dup push-locals swap [ swap localize-body <s-exp> convert-form swap pop-locals ] dip swap ; - -PRIVATE> : split-lambda ( s-exp -- body vars ) first3 -rot nip [ body>> ] bi@ [ name>> ] map ; inline @@ -47,6 +46,7 @@ PRIVATE> : normal-lambda ( body vars -- quot ) localize-lambda <lambda> [ , compose ] bake ; +PRIVATE> : convert-lambda ( s-exp -- quot ) split-lambda dup "&rest" swap member? [ rest-lambda ] [ normal-lambda ] if ; @@ -68,7 +68,7 @@ PRIVATE> : convert-form ( lisp-form -- quot ) dup s-exp? [ body>> convert-list-form ] - [ [ , ] [ ] make ] if ; + [ [ , ] bake ] if ; : lisp-string>factor ( str -- quot ) lisp-expr parse-result-ast convert-form lambda-rewrite call ; @@ -85,7 +85,10 @@ ERROR: no-such-var var ; swap lisp-env get set-at ; : lisp-get ( name -- word ) - dup lisp-env get at [ ] [ no-such-var ] ?if ; + dup lisp-env get at [ ] [ no-such-var throw ] ?if ; : funcall ( quot sym -- * ) - dup lisp-symbol? [ name>> lisp-get ] when call ; inline \ No newline at end of file + dup lisp-symbol? [ name>> lisp-get ] when call ; inline + +: define-primitve ( name vocab word -- ) + swap lookup [ [ , ] compose call ] bake lisp-define ; \ No newline at end of file From ced3a4b632c583b620ad8388df14d1265781fd8f Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Thu, 15 May 2008 22:58:32 -0400 Subject: [PATCH 04/10] Adding more tests to lisp --- extra/lisp/lisp-tests.factor | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extra/lisp/lisp-tests.factor b/extra/lisp/lisp-tests.factor index 06c2260d72..98e1c9e943 100644 --- a/extra/lisp/lisp-tests.factor +++ b/extra/lisp/lisp-tests.factor @@ -14,4 +14,14 @@ init-env { 3 } [ "((lambda (x y) (+ x y)) 1 2)" lisp-string>factor call +] unit-test + +"-" "math" "-" define-primitve + +{ 8.3 } [ + [ 10.4 2.1 ] "-" <lisp-symbol> funcall +] unit-test + +{ 42 } [ + "((lambda (x y z) (+ x (- y z))) 40 3 1)" lisp-string>factor call ] unit-test \ No newline at end of file From a2e1ad28142fe42f6e3bb788237320e505a934d5 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Sun, 18 May 2008 11:58:32 -0400 Subject: [PATCH 05/10] Allowing identifiers to begin with '#' --- extra/lisp/parser/parser.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/lisp/parser/parser.factor b/extra/lisp/parser/parser.factor index 32886f9367..44c79fd962 100644 --- a/extra/lisp/parser/parser.factor +++ b/extra/lisp/parser/parser.factor @@ -24,7 +24,7 @@ rational = integer "/" (digit)+ => [[ first3 nip string number = float | rational | integer -id-specials = "!" | "$" | "%" | "&" | "*" | "/" | ":" | "<" +id-specials = "!" | "$" | "%" | "&" | "*" | "/" | ":" | "<" | "#" | " =" | ">" | "?" | "^" | "_" | "~" | "+" | "-" | "." | "@" letters = [a-zA-Z] => [[ 1array >string ]] initials = letters | id-specials From 5c13565bc7c1da890eec9615d9cbd6433610add5 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Sun, 18 May 2008 11:59:11 -0400 Subject: [PATCH 06/10] Adding more tests to lisp --- extra/lisp/lisp-tests.factor | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/extra/lisp/lisp-tests.factor b/extra/lisp/lisp-tests.factor index 98e1c9e943..fd6e2d93ae 100644 --- a/extra/lisp/lisp-tests.factor +++ b/extra/lisp/lisp-tests.factor @@ -6,22 +6,32 @@ IN: lisp.test init-env +"#f" [ f ] lisp-define +"#t" [ t ] lisp-define + "+" "math" "+" define-primitve +"-" "math" "-" define-primitve { 5 } [ [ 2 3 ] "+" <lisp-symbol> funcall ] unit-test +{ 8.3 } [ + [ 10.4 2.1 ] "-" <lisp-symbol> funcall +] unit-test + { 3 } [ "((lambda (x y) (+ x y)) 1 2)" lisp-string>factor call ] unit-test -"-" "math" "-" define-primitve - -{ 8.3 } [ - [ 10.4 2.1 ] "-" <lisp-symbol> funcall -] unit-test - { 42 } [ "((lambda (x y z) (+ x (- y z))) 40 3 1)" lisp-string>factor call +] unit-test + +{ 1 } [ + "(if #t 1 2)" lisp-string>factor call +] unit-test + +{ "b" } [ + "(cond (#f \"a\") (#t \"b\"))" lisp-string>factor call ] unit-test \ No newline at end of file From 1acf9bc60b5e2589ccea776a99528bf8b99d5f22 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Sun, 18 May 2008 12:00:03 -0400 Subject: [PATCH 07/10] Lookup lisp-symbols in variable list --- extra/lisp/lisp.factor | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extra/lisp/lisp.factor b/extra/lisp/lisp.factor index 32df8c5102..0c5ae34e3f 100644 --- a/extra/lisp/lisp.factor +++ b/extra/lisp/lisp.factor @@ -7,6 +7,7 @@ IN: lisp DEFER: convert-form DEFER: funcall +DEFER: lookup-vars ! Functions to convert s-exps to quotations ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -23,7 +24,7 @@ DEFER: funcall rest [ [ convert-form map ] map ] [ % cond ] bake ; : convert-general-form ( s-exp -- quot ) - unclip convert-form swap convert-body [ , % funcall ] bake ; + unclip convert-form swap convert-body [ , lookup-vars % funcall ] bake ; ! words for convert-lambda <PRIVATE @@ -42,10 +43,10 @@ DEFER: funcall : rest-lambda ( body vars -- quot ) "&rest" swap [ remove ] [ index ] 2bi [ localize-lambda <lambda> ] dip - [ , cut swap [ % , ] bake , compose ] bake ; + [ , lookup-vars cut swap [ % , ] bake , compose ] bake ; : normal-lambda ( body vars -- quot ) - localize-lambda <lambda> [ , compose ] bake ; + localize-lambda <lambda> [ lookup-vars , compose ] bake ; PRIVATE> : convert-lambda ( s-exp -- quot ) @@ -90,5 +91,8 @@ ERROR: no-such-var var ; : funcall ( quot sym -- * ) dup lisp-symbol? [ name>> lisp-get ] when call ; inline +: lookup-vars ( q -- p ) + [ dup lisp-symbol? [ name>> lisp-get ] when ] map ; + : define-primitve ( name vocab word -- ) swap lookup [ [ , ] compose call ] bake lisp-define ; \ No newline at end of file From eddb4f49949b35aed4c88dfc3083ee25764938a5 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Sun, 18 May 2008 12:53:44 -0400 Subject: [PATCH 08/10] Fixing cond, variable lookup --- extra/lisp/lisp.factor | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/extra/lisp/lisp.factor b/extra/lisp/lisp.factor index 0c5ae34e3f..c9bdf6c91a 100644 --- a/extra/lisp/lisp.factor +++ b/extra/lisp/lisp.factor @@ -7,7 +7,7 @@ IN: lisp DEFER: convert-form DEFER: funcall -DEFER: lookup-vars +DEFER: lookup-var ! Functions to convert s-exps to quotations ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -21,10 +21,11 @@ DEFER: lookup-vars rest convert-form ; : convert-cond ( s-exp -- quot ) - rest [ [ convert-form map ] map ] [ % cond ] bake ; + rest [ body>> >array [ convert-form ] map first2 swap `{ [ % funcall ] , } bake ] + map >array [ , cond ] bake ; : convert-general-form ( s-exp -- quot ) - unclip convert-form swap convert-body [ , lookup-vars % funcall ] bake ; + unclip convert-form swap convert-body [ , % funcall ] bake ; ! words for convert-lambda <PRIVATE @@ -43,10 +44,10 @@ DEFER: lookup-vars : rest-lambda ( body vars -- quot ) "&rest" swap [ remove ] [ index ] 2bi [ localize-lambda <lambda> ] dip - [ , lookup-vars cut swap [ % , ] bake , compose ] bake ; + [ , cut swap [ % , ] bake , compose ] bake ; : normal-lambda ( body vars -- quot ) - localize-lambda <lambda> [ lookup-vars , compose ] bake ; + localize-lambda <lambda> [ , compose ] bake ; PRIVATE> : convert-lambda ( s-exp -- quot ) @@ -68,8 +69,10 @@ PRIVATE> [ drop convert-general-form ] if ; : convert-form ( lisp-form -- quot ) - dup s-exp? [ body>> convert-list-form ] - [ [ , ] bake ] if ; + { { [ dup s-exp? ] [ body>> convert-list-form ] } + { [ dup lisp-symbol? ] [ [ , lookup-var ] bake ] } + [ [ , ] bake ] + } cond ; : lisp-string>factor ( str -- quot ) lisp-expr parse-result-ast convert-form lambda-rewrite call ; @@ -88,11 +91,11 @@ ERROR: no-such-var var ; : lisp-get ( name -- word ) dup lisp-env get at [ ] [ no-such-var throw ] ?if ; +: lookup-var ( lisp-symbol -- quot ) + name>> lisp-get ; + : funcall ( quot sym -- * ) - dup lisp-symbol? [ name>> lisp-get ] when call ; inline - -: lookup-vars ( q -- p ) - [ dup lisp-symbol? [ name>> lisp-get ] when ] map ; + dup lisp-symbol? [ lookup-var ] when call ; inline : define-primitve ( name vocab word -- ) swap lookup [ [ , ] compose call ] bake lisp-define ; \ No newline at end of file From b2cbe83be8c7159d53a000d5b1c3f2711b2ac177 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Mon, 19 May 2008 17:09:30 -0400 Subject: [PATCH 09/10] Adding tests for 'begin' --- extra/lisp/lisp-tests.factor | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/extra/lisp/lisp-tests.factor b/extra/lisp/lisp-tests.factor index fd6e2d93ae..05ce63a69d 100644 --- a/extra/lisp/lisp-tests.factor +++ b/extra/lisp/lisp-tests.factor @@ -34,4 +34,12 @@ init-env { "b" } [ "(cond (#f \"a\") (#t \"b\"))" lisp-string>factor call +] unit-test + +{ 5 } [ + "(begin (+ 1 4))" lisp-string>factor call +] unit-test + +{ 3 } [ + "((lambda (x) (if x (begin (+ 1 2)) (- 3 5))) #t)" lisp-string>factor call ] unit-test \ No newline at end of file From 12d0367d73ad9b717c13c058a65b6cc9fb35c722 Mon Sep 17 00:00:00 2001 From: James Cash <james.nvc@gmail.com> Date: Mon, 19 May 2008 17:09:43 -0400 Subject: [PATCH 10/10] Fixing 'begin' --- extra/lisp/lisp.factor | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extra/lisp/lisp.factor b/extra/lisp/lisp.factor index c9bdf6c91a..0f5e4b4d2e 100644 --- a/extra/lisp/lisp.factor +++ b/extra/lisp/lisp.factor @@ -2,7 +2,7 @@ ! See http://factorcode.org/license.txt for BSD license. USING: kernel peg sequences arrays strings combinators.lib namespaces combinators math bake locals locals.private accessors -vectors syntax lisp.parser assocs parser sequences.lib words ; +vectors syntax lisp.parser assocs parser sequences.lib words quotations ; IN: lisp DEFER: convert-form @@ -18,7 +18,7 @@ DEFER: lookup-var rest [ convert-form ] map reverse first3 [ % , , if ] bake ; : convert-begin ( s-exp -- quot ) - rest convert-form ; + rest [ convert-form ] map >quotation [ , [ funcall ] each ] bake ; : convert-cond ( s-exp -- quot ) rest [ body>> >array [ convert-form ] map first2 swap `{ [ % funcall ] , } bake ]