2008-04-17 02:37:03 -04:00
|
|
|
! Copyright (C) 2008 James Cash
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-04-21 20:25:55 -04:00
|
|
|
USING: kernel peg.ebnf peg.expr math.parser sequences arrays strings combinators.lib
|
2008-04-23 09:01:33 -04:00
|
|
|
namespaces combinators math bake ;
|
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
|
|
|
TUPLE: lisp-symbol name ;
|
|
|
|
|
|
|
|
C: <symbol> lisp-symbol
|
|
|
|
|
2008-04-20 01:41:16 -04:00
|
|
|
EBNF: lisp-expr
|
2008-04-21 20:25:55 -04:00
|
|
|
_ = (" " | "\t" | "\n")*
|
|
|
|
LPAREN = "("
|
|
|
|
RPAREN = ")"
|
|
|
|
digit = [0-9]
|
|
|
|
integer = (digit)+ => [[ string>number ]]
|
|
|
|
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> ]]
|
2008-04-20 01:41:16 -04:00
|
|
|
atom = number
|
2008-04-21 20:25:55 -04:00
|
|
|
| identifier
|
|
|
|
list-item = _ (atom|list) _ => [[ second ]]
|
|
|
|
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 )
|
2008-04-23 09:01:33 -04:00
|
|
|
1 tail [ convert-form ] map reverse first3 [ % , , if ] bake ;
|
2008-04-21 20:25:55 -04:00
|
|
|
|
|
|
|
: convert-general-form ( lisp-form -- quot )
|
2008-04-23 09:01:33 -04:00
|
|
|
unclip swap convert-body [ % , ] bake ;
|
2008-04-21 20:25:55 -04:00
|
|
|
|
|
|
|
: convert-list-form ( lisp-form -- quot )
|
|
|
|
dup first
|
2008-04-23 09:01:33 -04:00
|
|
|
{ { [ dup "if" <symbol> equal? ] [ drop convert-if ] }
|
2008-04-21 20:25:55 -04:00
|
|
|
[ drop convert-general-form ]
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
: convert-form ( lisp-form -- quot )
|
|
|
|
{ { [ dup [ sequence? ] [ number? not ] bi and ] [ convert-list-form ] }
|
|
|
|
[ [ , ] [ ] make ]
|
|
|
|
} cond ;
|