fjsc: 'word' -> word-parser

db4
Doug Coleman 2015-08-15 18:30:53 -07:00
parent be82224fe9
commit 75d63f8407
2 changed files with 64 additions and 64 deletions

View File

@ -4,50 +4,50 @@ USING: kernel tools.test peg fjsc ;
IN: fjsc.tests IN: fjsc.tests
{ T{ ast-expression f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [ { T{ ast-expression f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [
"55 2abc1 100" 'expression' parse "55 2abc1 100" expression-parser parse
] unit-test ] unit-test
{ T{ ast-quotation f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [ { T{ ast-quotation f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [
"[ 55 2abc1 100 ]" 'quotation' parse "[ 55 2abc1 100 ]" quotation-parser parse
] unit-test ] unit-test
{ T{ ast-array f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [ { T{ ast-array f V{ T{ ast-number f 55 } T{ ast-identifier f "2abc1" } T{ ast-number f 100 } } } } [
"{ 55 2abc1 100 }" 'array' parse "{ 55 2abc1 100 }" array-parser parse
] unit-test ] unit-test
{ T{ ast-stack-effect f V{ } V{ "d" "e" "f" } } } [ { T{ ast-stack-effect f V{ } V{ "d" "e" "f" } } } [
"( -- d e f )" 'stack-effect' parse "( -- d e f )" stack-effect-parser parse
] unit-test ] unit-test
{ T{ ast-stack-effect f V{ "a" "b" "c" } V{ "d" "e" "f" } } } [ { T{ ast-stack-effect f V{ "a" "b" "c" } V{ "d" "e" "f" } } } [
"( a b c -- d e f )" 'stack-effect' parse "( a b c -- d e f )" stack-effect-parser parse
] unit-test ] unit-test
{ T{ ast-stack-effect f V{ "a" "b" "c" } V{ } } } [ { T{ ast-stack-effect f V{ "a" "b" "c" } V{ } } } [
"( a b c -- )" 'stack-effect' parse "( a b c -- )" stack-effect-parser parse
] unit-test ] unit-test
{ T{ ast-stack-effect f V{ } V{ } } } [ { T{ ast-stack-effect f V{ } V{ } } } [
"( -- )" 'stack-effect' parse "( -- )" stack-effect-parser parse
] unit-test ] unit-test
{ f } [ { f } [
": foo ( a b -- c d ) abcdefghijklmn 123 ;" 'expression' parse not ": foo ( a b -- c d ) abcdefghijklmn 123 ;" expression-parser parse not
] unit-test ] unit-test
{ T{ ast-expression f V{ T{ ast-string f "abcd" } } } } [ { T{ ast-expression f V{ T{ ast-string f "abcd" } } } } [
"\"abcd\"" 'statement' parse "\"abcd\"" statement-parser parse
] unit-test ] unit-test
{ T{ ast-expression f V{ T{ ast-use f "foo" } } } } [ { T{ ast-expression f V{ T{ ast-use f "foo" } } } } [
"USE: foo" 'statement' parse "USE: foo" statement-parser parse
] unit-test ] unit-test
{ T{ ast-expression f V{ T{ ast-in f "foo" } } } } [ { T{ ast-expression f V{ T{ ast-in f "foo" } } } } [
"IN: foo" 'statement' parse "IN: foo" statement-parser parse
] unit-test ] unit-test
{ T{ ast-expression f V{ T{ ast-using f V{ "foo" "bar" } } } } } [ { T{ ast-expression f V{ T{ ast-using f V{ "foo" "bar" } } } } } [
"USING: foo bar ;" 'statement' parse "USING: foo bar ;" statement-parser parse
] unit-test ] unit-test

View File

@ -28,7 +28,7 @@ TUPLE: ast-hashtable elements ;
[ digit? not ] [ digit? not ]
} 1&& ; } 1&& ;
: 'identifier-ends' ( -- parser ) : identifier-ends-parser ( -- parser )
[ [
{ {
[ blank? not ] [ blank? not ]
@ -40,22 +40,22 @@ TUPLE: ast-hashtable elements ;
} 1&& } 1&&
] satisfy repeat0 ; ] satisfy repeat0 ;
: 'identifier-middle' ( -- parser ) : identifier-middle-parser ( -- parser )
[ identifier-middle? ] satisfy repeat1 ; [ identifier-middle? ] satisfy repeat1 ;
: 'identifier' ( -- parser ) : identifier-parser ( -- parser )
[ [
'identifier-ends' , identifier-ends-parser ,
'identifier-middle' , identifier-middle-parser ,
'identifier-ends' , identifier-ends-parser ,
] seq* [ ] seq* [
"" concat-as f ast-identifier boa "" concat-as f ast-identifier boa
] action ; ] action ;
DEFER: 'expression' DEFER: expression-parser
: 'effect-name' ( -- parser ) : effect-name-parser ( -- parser )
[ [
{ {
[ blank? not ] [ blank? not ]
@ -64,54 +64,54 @@ DEFER: 'expression'
} 1&& } 1&&
] satisfy repeat1 [ >string ] action ; ] satisfy repeat1 [ >string ] action ;
: 'stack-effect' ( -- parser ) : stack-effect-parser ( -- parser )
[ [
"(" token hide , "(" token hide ,
'effect-name' sp repeat0 , effect-name-parser sp repeat0 ,
"--" token sp hide , "--" token sp hide ,
'effect-name' sp repeat0 , effect-name-parser sp repeat0 ,
")" token sp hide , ")" token sp hide ,
] seq* [ ] seq* [
first2 ast-stack-effect boa first2 ast-stack-effect boa
] action ; ] action ;
: 'define' ( -- parser ) : define-parser ( -- parser )
[ [
":" token sp hide , ":" token sp hide ,
'identifier' sp [ value>> ] action , identifier-parser sp [ value>> ] action ,
'stack-effect' sp optional , stack-effect-parser sp optional ,
'expression' , expression-parser ,
";" token sp hide , ";" token sp hide ,
] seq* [ first3 ast-define boa ] action ; ] seq* [ first3 ast-define boa ] action ;
: 'quotation' ( -- parser ) : quotation-parser ( -- parser )
[ [
"[" token sp hide , "[" token sp hide ,
'expression' [ values>> ] action , expression-parser [ values>> ] action ,
"]" token sp hide , "]" token sp hide ,
] seq* [ first ast-quotation boa ] action ; ] seq* [ first ast-quotation boa ] action ;
: 'array' ( -- parser ) : array-parser ( -- parser )
[ [
"{" token sp hide , "{" token sp hide ,
'expression' [ values>> ] action , expression-parser [ values>> ] action ,
"}" token sp hide , "}" token sp hide ,
] seq* [ first ast-array boa ] action ; ] seq* [ first ast-array boa ] action ;
: 'word' ( -- parser ) : word-parser ( -- parser )
[ [
"\\" token sp hide , "\\" token sp hide ,
'identifier' sp , identifier-parser sp ,
] seq* [ first value>> f ast-word boa ] action ; ] seq* [ first value>> f ast-word boa ] action ;
: 'atom' ( -- parser ) : atom-parser ( -- parser )
[ [
'identifier' , identifier-parser ,
'integer' [ ast-number boa ] action , integer-parser [ ast-number boa ] action ,
'string' [ ast-string boa ] action , string-parser [ ast-string boa ] action ,
] choice* ; ] choice* ;
: 'comment' ( -- parser ) : comment-parser ( -- parser )
[ [
[ [
"#!" token sp , "#!" token sp ,
@ -122,55 +122,55 @@ DEFER: 'expression'
] satisfy repeat0 , ] satisfy repeat0 ,
] seq* [ drop ast-comment boa ] action ; ] seq* [ drop ast-comment boa ] action ;
: 'USE:' ( -- parser ) : USE-parser ( -- parser )
[ [
"USE:" token sp hide , "USE:" token sp hide ,
'identifier' sp , identifier-parser sp ,
] seq* [ first value>> ast-use boa ] action ; ] seq* [ first value>> ast-use boa ] action ;
: 'IN:' ( -- parser ) : IN-parser ( -- parser )
[ [
"IN:" token sp hide , "IN:" token sp hide ,
'identifier' sp , identifier-parser sp ,
] seq* [ first value>> ast-in boa ] action ; ] seq* [ first value>> ast-in boa ] action ;
: 'USING:' ( -- parser ) : USING-parser ( -- parser )
[ [
"USING:" token sp hide , "USING:" token sp hide ,
'identifier' sp [ value>> ] action repeat1 , identifier-parser sp [ value>> ] action repeat1 ,
";" token sp hide , ";" token sp hide ,
] seq* [ first ast-using boa ] action ; ] seq* [ first ast-using boa ] action ;
: 'hashtable' ( -- parser ) : hashtable-parser ( -- parser )
[ [
"H{" token sp hide , "H{" token sp hide ,
'expression' [ values>> ] action , expression-parser [ values>> ] action ,
"}" token sp hide , "}" token sp hide ,
] seq* [ first ast-hashtable boa ] action ; ] seq* [ first ast-hashtable boa ] action ;
: 'parsing-word' ( -- parser ) : parsing-word-parser ( -- parser )
[ [
'USE:' , USE-parser ,
'USING:' , USING-parser ,
'IN:' , IN-parser ,
] choice* ; ] choice* ;
: 'expression' ( -- parser ) : expression-parser ( -- parser )
[ [
[ [
'comment' , comment-parser ,
'parsing-word' sp , parsing-word-parser sp ,
'quotation' sp , quotation-parser sp ,
'define' sp , define-parser sp ,
'array' sp , array-parser sp ,
'hashtable' sp , hashtable-parser sp ,
'word' sp , word-parser sp ,
'atom' sp , atom-parser sp ,
] choice* repeat0 [ ast-expression boa ] action ] choice* repeat0 [ ast-expression boa ] action
] delay ; ] delay ;
: 'statement' ( -- parser ) : statement-parser ( -- parser )
'expression' ; expression-parser ;
GENERIC: (compile) ( ast -- ) GENERIC: (compile) ( ast -- )
GENERIC: (literal) ( ast -- ) GENERIC: (literal) ( ast -- )
@ -334,7 +334,7 @@ M: wrapper (parse-factor-quotation) ( object -- ast )
GENERIC: fjsc-parse ( object -- ast ) GENERIC: fjsc-parse ( object -- ast )
M: string fjsc-parse ( object -- ast ) M: string fjsc-parse ( object -- ast )
'expression' parse ; expression-parser parse ;
M: quotation fjsc-parse ( object -- ast ) M: quotation fjsc-parse ( object -- ast )
[ (parse-factor-quotation) ] { } map-as ast-expression boa ; [ (parse-factor-quotation) ] { } map-as ast-expression boa ;
@ -349,11 +349,11 @@ M: quotation fjsc-parse ( object -- ast )
] with-string-writer ; ] with-string-writer ;
: fjsc-compile* ( string -- string ) : fjsc-compile* ( string -- string )
'statement' parse fjsc-compile ; statement-parser parse fjsc-compile ;
: fc* ( string -- ) : fc* ( string -- )
[ [
'statement' parse values>> do-expressions statement-parser parse values>> do-expressions
] { } make [ write ] each ; ] { } make [ write ] each ;
: fjsc-literal ( ast -- string ) : fjsc-literal ( ast -- string )