Replacing s-exp tuple with cons cells in parser, updating tests
parent
99e546ef65
commit
27586218e8
|
@ -40,7 +40,30 @@ IN: lisp.parser.tests
|
||||||
"+" "atom" \ lisp-expr rule parse parse-result-ast
|
"+" "atom" \ lisp-expr rule parse parse-result-ast
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
{ T{ s-exp f
|
{ T{ cons f f f }
|
||||||
V{ T{ lisp-symbol f "foo" } 1 2 "aoeu" } } } [
|
} [
|
||||||
|
"()" lisp-expr parse-result-ast
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ T{
|
||||||
|
cons
|
||||||
|
f
|
||||||
|
T{ lisp-symbol f "foo" }
|
||||||
|
T{
|
||||||
|
cons
|
||||||
|
f
|
||||||
|
1
|
||||||
|
T{ cons f 2 T{ cons f "aoeu" T{ cons f f f } } }
|
||||||
|
} } } [
|
||||||
"(foo 1 2 \"aoeu\")" lisp-expr parse-result-ast
|
"(foo 1 2 \"aoeu\")" lisp-expr parse-result-ast
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
{ T{ cons f
|
||||||
|
1
|
||||||
|
T{ cons f
|
||||||
|
T{ cons f 3 T{ cons f 4 T{ cons f f f } } }
|
||||||
|
T{ cons f 2 T{ cons f f } } }
|
||||||
|
}
|
||||||
|
} [
|
||||||
|
"(1 (3 4) 2)" lisp-expr parse-result-ast
|
||||||
|
] unit-test
|
|
@ -1,15 +1,21 @@
|
||||||
! 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
|
USING: kernel peg peg.ebnf peg.expr math.parser sequences arrays strings
|
||||||
combinators.lib math ;
|
combinators.lib math fry accessors ;
|
||||||
|
|
||||||
IN: lisp.parser
|
IN: lisp.parser
|
||||||
|
|
||||||
TUPLE: lisp-symbol name ;
|
TUPLE: lisp-symbol name ;
|
||||||
C: <lisp-symbol> lisp-symbol
|
C: <lisp-symbol> lisp-symbol
|
||||||
|
|
||||||
TUPLE: s-exp body ;
|
TUPLE: cons car cdr ;
|
||||||
C: <s-exp> s-exp
|
: cons \ cons new ;
|
||||||
|
|
||||||
|
: <car> ( x -- cons )
|
||||||
|
cons swap >>car ;
|
||||||
|
|
||||||
|
: seq>cons ( seq -- cons )
|
||||||
|
<reversed> cons [ <car> swap >>cdr ] reduce ;
|
||||||
|
|
||||||
EBNF: lisp-expr
|
EBNF: lisp-expr
|
||||||
_ = (" " | "\t" | "\n")*
|
_ = (" " | "\t" | "\n")*
|
||||||
|
@ -24,8 +30,9 @@ rational = integer "/" (digit)+ => [[ first3 nip string
|
||||||
number = float
|
number = float
|
||||||
| rational
|
| rational
|
||||||
| integer
|
| integer
|
||||||
id-specials = "!" | "$" | "%" | "&" | "*" | "/" | ":" | "<" | "#"
|
id-specials = "!" | "$" | "%" | "&" | "*" | "/" | ":"
|
||||||
| " =" | ">" | "?" | "^" | "_" | "~" | "+" | "-" | "." | "@"
|
| "<" | "#" | " =" | ">" | "?" | "^" | "_"
|
||||||
|
| "~" | "+" | "-" | "." | "@"
|
||||||
letters = [a-zA-Z] => [[ 1array >string ]]
|
letters = [a-zA-Z] => [[ 1array >string ]]
|
||||||
initials = letters | id-specials
|
initials = letters | id-specials
|
||||||
numbers = [0-9] => [[ 1array >string ]]
|
numbers = [0-9] => [[ 1array >string ]]
|
||||||
|
@ -36,6 +43,6 @@ string = dquote ( escaped | !(dquote) . )* dquote => [[ second >string ]]
|
||||||
atom = number
|
atom = number
|
||||||
| identifier
|
| identifier
|
||||||
| string
|
| string
|
||||||
list-item = _ (atom|s-expression) _ => [[ second ]]
|
list-item = _ ( atom | s-expression ) _ => [[ second ]]
|
||||||
s-expression = LPAREN (list-item)* RPAREN => [[ second <s-exp> ]]
|
s-expression = LPAREN (list-item)* RPAREN => [[ second seq>cons ]]
|
||||||
;EBNF
|
;EBNF
|
Loading…
Reference in New Issue