fjsc: 'word' -> word-parser
							parent
							
								
									be82224fe9
								
							
						
					
					
						commit
						75d63f8407
					
				| 
						 | 
				
			
			@ -4,50 +4,50 @@ USING: kernel tools.test peg fjsc ;
 | 
			
		|||
IN: fjsc.tests
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
 | 
			
		||||
{ T{ ast-stack-effect f V{ } V{ } } } [
 | 
			
		||||
  "( -- )" 'stack-effect' parse
 | 
			
		||||
  "( -- )" stack-effect-parser parse
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
{ f } [
 | 
			
		||||
  ": foo ( a b -- c d ) abcdefghijklmn 123 ;" 'expression' parse not
 | 
			
		||||
  ": foo ( a b -- c d ) abcdefghijklmn 123 ;" expression-parser parse not
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
{ T{ ast-expression f V{ T{ ast-string f "abcd" } } } } [
 | 
			
		||||
  "\"abcd\"" 'statement' parse
 | 
			
		||||
  "\"abcd\"" statement-parser parse
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
{ T{ ast-expression f V{ T{ ast-use f "foo" } } } } [
 | 
			
		||||
  "USE: foo" 'statement' parse
 | 
			
		||||
  "USE: foo" statement-parser parse
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
{ T{ ast-expression f V{ T{ ast-in f "foo" } } } } [
 | 
			
		||||
  "IN: foo" 'statement' parse
 | 
			
		||||
  "IN: foo" statement-parser parse
 | 
			
		||||
] unit-test
 | 
			
		||||
 | 
			
		||||
{ 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -28,7 +28,7 @@ TUPLE: ast-hashtable elements ;
 | 
			
		|||
        [ digit? not ]
 | 
			
		||||
    } 1&& ;
 | 
			
		||||
 | 
			
		||||
: 'identifier-ends' ( -- parser )
 | 
			
		||||
: identifier-ends-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        {
 | 
			
		||||
            [ blank? not ]
 | 
			
		||||
| 
						 | 
				
			
			@ -40,22 +40,22 @@ TUPLE: ast-hashtable elements ;
 | 
			
		|||
        } 1&&
 | 
			
		||||
    ] satisfy repeat0 ;
 | 
			
		||||
 | 
			
		||||
: 'identifier-middle' ( -- parser )
 | 
			
		||||
: identifier-middle-parser ( -- parser )
 | 
			
		||||
    [ identifier-middle? ] satisfy repeat1 ;
 | 
			
		||||
 | 
			
		||||
: 'identifier' ( -- parser )
 | 
			
		||||
: identifier-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        'identifier-ends' ,
 | 
			
		||||
        'identifier-middle' ,
 | 
			
		||||
        'identifier-ends' ,
 | 
			
		||||
        identifier-ends-parser ,
 | 
			
		||||
        identifier-middle-parser ,
 | 
			
		||||
        identifier-ends-parser ,
 | 
			
		||||
    ] seq* [
 | 
			
		||||
        "" concat-as f ast-identifier boa
 | 
			
		||||
    ] action ;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
DEFER: 'expression'
 | 
			
		||||
DEFER: expression-parser
 | 
			
		||||
 | 
			
		||||
: 'effect-name' ( -- parser )
 | 
			
		||||
: effect-name-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        {
 | 
			
		||||
            [ blank? not ]
 | 
			
		||||
| 
						 | 
				
			
			@ -64,54 +64,54 @@ DEFER: 'expression'
 | 
			
		|||
        } 1&&
 | 
			
		||||
    ] satisfy repeat1 [ >string ] action ;
 | 
			
		||||
 | 
			
		||||
: 'stack-effect' ( -- parser )
 | 
			
		||||
: stack-effect-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "(" token hide ,
 | 
			
		||||
        'effect-name' sp repeat0 ,
 | 
			
		||||
        effect-name-parser sp repeat0 ,
 | 
			
		||||
        "--" token sp hide ,
 | 
			
		||||
        'effect-name' sp repeat0 ,
 | 
			
		||||
        effect-name-parser sp repeat0 ,
 | 
			
		||||
        ")" token sp hide ,
 | 
			
		||||
    ] seq* [
 | 
			
		||||
        first2 ast-stack-effect boa
 | 
			
		||||
    ] action ;
 | 
			
		||||
 | 
			
		||||
: 'define' ( -- parser )
 | 
			
		||||
: define-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        ":" token sp hide ,
 | 
			
		||||
        'identifier' sp [ value>> ] action ,
 | 
			
		||||
        'stack-effect' sp optional ,
 | 
			
		||||
        'expression' ,
 | 
			
		||||
        identifier-parser sp [ value>> ] action ,
 | 
			
		||||
        stack-effect-parser sp optional ,
 | 
			
		||||
        expression-parser ,
 | 
			
		||||
        ";" token sp hide ,
 | 
			
		||||
    ] seq* [ first3 ast-define boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'quotation' ( -- parser )
 | 
			
		||||
: quotation-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "[" token sp hide ,
 | 
			
		||||
        'expression' [ values>> ] action ,
 | 
			
		||||
        expression-parser [ values>> ] action ,
 | 
			
		||||
        "]" token sp hide ,
 | 
			
		||||
    ] seq* [ first ast-quotation boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'array' ( -- parser )
 | 
			
		||||
: array-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "{" token sp hide ,
 | 
			
		||||
        'expression' [ values>> ] action ,
 | 
			
		||||
        expression-parser [ values>> ] action ,
 | 
			
		||||
        "}" token sp hide ,
 | 
			
		||||
    ] seq* [ first ast-array boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'word' ( -- parser )
 | 
			
		||||
: word-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "\\" token sp hide ,
 | 
			
		||||
        'identifier' sp ,
 | 
			
		||||
        identifier-parser sp ,
 | 
			
		||||
    ] seq* [ first value>> f ast-word boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'atom' ( -- parser )
 | 
			
		||||
: atom-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        'identifier' ,
 | 
			
		||||
        'integer' [ ast-number boa ] action ,
 | 
			
		||||
        'string' [ ast-string boa ] action ,
 | 
			
		||||
        identifier-parser ,
 | 
			
		||||
        integer-parser [ ast-number boa ] action ,
 | 
			
		||||
        string-parser [ ast-string boa ] action ,
 | 
			
		||||
    ] choice* ;
 | 
			
		||||
 | 
			
		||||
: 'comment' ( -- parser )
 | 
			
		||||
: comment-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        [
 | 
			
		||||
            "#!" token sp ,
 | 
			
		||||
| 
						 | 
				
			
			@ -122,55 +122,55 @@ DEFER: 'expression'
 | 
			
		|||
        ] satisfy repeat0 ,
 | 
			
		||||
    ] seq* [ drop ast-comment boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'USE:' ( -- parser )
 | 
			
		||||
: USE-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "USE:" token sp hide ,
 | 
			
		||||
        'identifier' sp ,
 | 
			
		||||
        identifier-parser sp ,
 | 
			
		||||
    ] seq* [ first value>> ast-use boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'IN:' ( -- parser )
 | 
			
		||||
: IN-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "IN:" token sp hide ,
 | 
			
		||||
        'identifier' sp ,
 | 
			
		||||
        identifier-parser sp ,
 | 
			
		||||
    ] seq* [ first value>> ast-in boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'USING:' ( -- parser )
 | 
			
		||||
: USING-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "USING:" token sp hide ,
 | 
			
		||||
        'identifier' sp [ value>> ] action repeat1 ,
 | 
			
		||||
        identifier-parser sp [ value>> ] action repeat1 ,
 | 
			
		||||
        ";" token sp hide ,
 | 
			
		||||
    ] seq* [ first ast-using boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'hashtable' ( -- parser )
 | 
			
		||||
: hashtable-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        "H{" token sp hide ,
 | 
			
		||||
        'expression' [ values>> ] action ,
 | 
			
		||||
        expression-parser [ values>> ] action ,
 | 
			
		||||
        "}" token sp hide ,
 | 
			
		||||
    ] seq* [ first ast-hashtable boa ] action ;
 | 
			
		||||
 | 
			
		||||
: 'parsing-word' ( -- parser )
 | 
			
		||||
: parsing-word-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        'USE:' ,
 | 
			
		||||
        'USING:' ,
 | 
			
		||||
        'IN:' ,
 | 
			
		||||
        USE-parser ,
 | 
			
		||||
        USING-parser ,
 | 
			
		||||
        IN-parser ,
 | 
			
		||||
    ] choice* ;
 | 
			
		||||
 | 
			
		||||
: 'expression' ( -- parser )
 | 
			
		||||
: expression-parser ( -- parser )
 | 
			
		||||
    [
 | 
			
		||||
        [
 | 
			
		||||
            'comment' ,
 | 
			
		||||
            'parsing-word' sp ,
 | 
			
		||||
            'quotation' sp ,
 | 
			
		||||
            'define' sp ,
 | 
			
		||||
            'array' sp ,
 | 
			
		||||
            'hashtable' sp ,
 | 
			
		||||
            'word' sp ,
 | 
			
		||||
            'atom' sp ,
 | 
			
		||||
            comment-parser ,
 | 
			
		||||
            parsing-word-parser sp ,
 | 
			
		||||
            quotation-parser sp ,
 | 
			
		||||
            define-parser sp ,
 | 
			
		||||
            array-parser sp ,
 | 
			
		||||
            hashtable-parser sp ,
 | 
			
		||||
            word-parser sp ,
 | 
			
		||||
            atom-parser sp ,
 | 
			
		||||
        ] choice* repeat0 [ ast-expression boa ] action
 | 
			
		||||
    ] delay ;
 | 
			
		||||
 | 
			
		||||
: 'statement' ( -- parser )
 | 
			
		||||
    'expression' ;
 | 
			
		||||
: statement-parser ( -- parser )
 | 
			
		||||
    expression-parser ;
 | 
			
		||||
 | 
			
		||||
GENERIC: (compile) ( ast -- )
 | 
			
		||||
GENERIC: (literal) ( ast -- )
 | 
			
		||||
| 
						 | 
				
			
			@ -334,7 +334,7 @@ M: wrapper (parse-factor-quotation) ( object -- ast )
 | 
			
		|||
GENERIC: fjsc-parse ( object -- ast )
 | 
			
		||||
 | 
			
		||||
M: string fjsc-parse ( object -- ast )
 | 
			
		||||
    'expression' parse ;
 | 
			
		||||
    expression-parser parse ;
 | 
			
		||||
 | 
			
		||||
M: quotation fjsc-parse ( object -- ast )
 | 
			
		||||
    [ (parse-factor-quotation) ] { } map-as ast-expression boa ;
 | 
			
		||||
| 
						 | 
				
			
			@ -349,11 +349,11 @@ M: quotation fjsc-parse ( object -- ast )
 | 
			
		|||
    ] with-string-writer ;
 | 
			
		||||
 | 
			
		||||
: fjsc-compile* ( string -- string )
 | 
			
		||||
    'statement' parse fjsc-compile ;
 | 
			
		||||
    statement-parser parse fjsc-compile ;
 | 
			
		||||
 | 
			
		||||
: fc* ( string -- )
 | 
			
		||||
    [
 | 
			
		||||
        'statement' parse values>> do-expressions
 | 
			
		||||
        statement-parser parse values>> do-expressions
 | 
			
		||||
    ] { } make [ write ] each ;
 | 
			
		||||
 | 
			
		||||
: fjsc-literal ( ast -- string )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue