Bug fixes
parent
1ad1f5ceba
commit
0c10c13e4d
|
@ -1,53 +1,24 @@
|
||||||
! Copyright (C) 2008 James Cash
|
! Copyright (C) 2008 James Cash
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: kernel peg.ebnf peg.expr math.parser sequences arrays strings combinators.lib
|
USING: kernel peg sequences arrays strings combinators.lib
|
||||||
namespaces combinators math bake locals.private accessors vectors syntax ;
|
namespaces combinators math bake locals.private accessors vectors syntax lisp.parser ;
|
||||||
IN: lisp
|
IN: lisp
|
||||||
|
|
||||||
TUPLE: lisp-symbol name ;
|
|
||||||
|
|
||||||
C: <lisp-symbol> lisp-symbol
|
|
||||||
|
|
||||||
EBNF: lisp-expr
|
|
||||||
_ = (" " | "\t" | "\n")*
|
|
||||||
LPAREN = "("
|
|
||||||
RPAREN = ")"
|
|
||||||
dquote = '"'
|
|
||||||
digit = [0-9]
|
|
||||||
integer = (digit)+ => [[ string>number ]]
|
|
||||||
float = (digit)+ "." (digit)* => [[ first3 >string [ >string ] dipd 3append string>number ]]
|
|
||||||
number = float
|
|
||||||
| integer
|
|
||||||
id-specials = "!" | "$" | "%" | "&" | "*" | "/" | ":" | "<"
|
|
||||||
| " =" | ">" | "?" | "^" | "_" | "~" | "+" | "-" | "." | "@"
|
|
||||||
letters = [a-zA-Z] => [[ 1array >string ]]
|
|
||||||
initials = letters | id-specials
|
|
||||||
numbers = [0-9] => [[ 1array >string ]]
|
|
||||||
subsequents = initials | numbers
|
|
||||||
identifier = initials (subsequents)* => [[ first2 concat append <lisp-symbol> ]]
|
|
||||||
string = dquote ("\" . | !(dquote) . )* dquote => [[ second >string ]]
|
|
||||||
atom = number
|
|
||||||
| identifier
|
|
||||||
| string
|
|
||||||
list-item = _ (atom|list) _ => [[ second ]]
|
|
||||||
list = LPAREN (list-item)* RPAREN => [[ second ]]
|
|
||||||
;EBNF
|
|
||||||
|
|
||||||
DEFER: convert-form
|
DEFER: convert-form
|
||||||
|
|
||||||
: convert-body ( lisp-form -- quot )
|
: convert-body ( s-exp -- quot )
|
||||||
[ convert-form ] map [ ] [ compose ] reduce ; inline
|
[ convert-form ] map [ ] [ compose ] reduce ; inline
|
||||||
|
|
||||||
: convert-if ( lisp-form -- quot )
|
: convert-if ( s-exp -- quot )
|
||||||
1 tail [ convert-form ] map reverse first3 [ % , , if ] bake ;
|
1 tail [ convert-form ] map reverse first3 [ % , , if ] bake ;
|
||||||
|
|
||||||
: convert-begin ( lisp-form -- quot )
|
: convert-begin ( s-exp -- quot )
|
||||||
1 tail convert-body ;
|
1 tail convert-body ;
|
||||||
|
|
||||||
: convert-cond ( lisp-form -- quot )
|
: convert-cond ( s-exp -- quot )
|
||||||
1 tail [ [ convert-body map ] map ] [ % cond ] bake ;
|
1 tail [ [ convert-body map ] map ] [ % cond ] bake ;
|
||||||
|
|
||||||
: convert-general-form ( lisp-form -- quot )
|
: convert-general-form ( s-exp -- quot )
|
||||||
unclip swap convert-body [ % , ] bake ;
|
unclip swap convert-body [ % , ] bake ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
@ -56,11 +27,11 @@ DEFER: convert-form
|
||||||
[ dup vector? [ localize-body ] [ nip ] if ] if ] with map ; inline
|
[ dup vector? [ localize-body ] [ nip ] if ] if ] with map ; inline
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: convert-lambda ( lisp-form -- quot )
|
: convert-lambda ( s-exp -- quot )
|
||||||
1 tail unclip reverse [ name>> ] map dup make-locals dup push-locals
|
1 tail unclip reverse [ name>> ] map dup make-locals dup push-locals
|
||||||
[ swap localize-body convert-body ] dipd pop-locals swap <lambda> ;
|
[ swap localize-body convert-body ] dipd pop-locals swap <lambda> ;
|
||||||
|
|
||||||
: convert-list-form ( lisp-form -- quot )
|
: convert-list-form ( s-exp -- quot )
|
||||||
dup first dup lisp-symbol? [ name>>
|
dup first dup lisp-symbol? [ name>>
|
||||||
{ { "lambda" [ convert-lambda ] }
|
{ { "lambda" [ convert-lambda ] }
|
||||||
{ "if" [ convert-if ] }
|
{ "if" [ convert-if ] }
|
||||||
|
@ -73,3 +44,6 @@ dup first dup lisp-symbol? [ name>>
|
||||||
{ { [ dup vector? ] [ convert-list-form ] }
|
{ { [ dup vector? ] [ convert-list-form ] }
|
||||||
[ [ , ] [ ] make ]
|
[ [ , ] [ ] make ]
|
||||||
} cond ;
|
} cond ;
|
||||||
|
|
||||||
|
: lisp-string>factor ( str -- quot )
|
||||||
|
lisp-expr parse-result-ast convert-form ;
|
Loading…
Reference in New Issue