if-conversion working
parent
8db59121c6
commit
b1bd228cb5
|
@ -1,16 +1,47 @@
|
||||||
! 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 ;
|
USING: kernel peg.ebnf peg.expr math.parser sequences arrays strings combinators.lib
|
||||||
|
namespaces combinators math ;
|
||||||
IN: lisp
|
IN: lisp
|
||||||
|
|
||||||
|
TUPLE: lisp-symbol name ;
|
||||||
|
|
||||||
|
C: <symbol> lisp-symbol
|
||||||
|
|
||||||
EBNF: lisp-expr
|
EBNF: lisp-expr
|
||||||
digit = [0-9] => [[ digit> ]]
|
_ = (" " | "\t" | "\n")*
|
||||||
integer = (digit)+ => [[ 10 digits>integer ]]
|
LPAREN = "("
|
||||||
float = (digit)+ "." (digit)* => [[ 3 head 3append string>number ]]
|
RPAREN = ")"
|
||||||
number = integer
|
digit = [0-9]
|
||||||
| float
|
integer = (digit)+ => [[ string>number ]]
|
||||||
identifier = [a-zA-Z] ([a-zA-Z0-9])*
|
float = (digit)+ "." (digit)* => [[ first3 >string [ >string ] 2 ndip 3append string>number ]]
|
||||||
|
number = float
|
||||||
|
| integer
|
||||||
|
identifier = [a-zA-Z] ([^(){} ])* => [[ [ 1 head ] [ second ] bi append >string <symbol> ]]
|
||||||
atom = number
|
atom = number
|
||||||
| identifier
|
| identifier
|
||||||
list = "(" (atom|list)* ")" => [[ second 1array ]]
|
list-item = _ (atom|list) _ => [[ second ]]
|
||||||
;EBNF
|
list = LPAREN (list-item)* RPAREN => [[ second ]]
|
||||||
|
;EBNF
|
||||||
|
|
||||||
|
DEFER: convert-form
|
||||||
|
|
||||||
|
: convert-body ( lisp-form -- quot )
|
||||||
|
[ convert-form ] map [ ] [ compose ] reduce ; inline
|
||||||
|
|
||||||
|
: convert-if ( lisp-form -- quot )
|
||||||
|
1 tail [ convert-form ] map reverse first3 [ % , , \ if , ] [ ] make ;
|
||||||
|
|
||||||
|
: convert-general-form ( lisp-form -- quot )
|
||||||
|
unclip swap convert-body [ % , ] [ ] make ;
|
||||||
|
|
||||||
|
: convert-list-form ( lisp-form -- quot )
|
||||||
|
dup first
|
||||||
|
{ { [ dup "if" <symbol> equal? ] [ convert-if ] }
|
||||||
|
[ drop convert-general-form ]
|
||||||
|
} cond ;
|
||||||
|
|
||||||
|
: convert-form ( lisp-form -- quot )
|
||||||
|
{ { [ dup [ sequence? ] [ number? not ] bi and ] [ convert-list-form ] }
|
||||||
|
[ [ , ] [ ] make ]
|
||||||
|
} cond ;
|
Loading…
Reference in New Issue