Skip to content

Commit

Permalink
combinators.syntax: adding cleave[, spread[, apply[ to see what they …
Browse files Browse the repository at this point in the history
…look like

minor formatting changes to wrap lines
  • Loading branch information
mrjbq7 committed Nov 15, 2024
1 parent 3eb4ff2 commit 5d66419
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
10 changes: 7 additions & 3 deletions extra/combinators/syntax/syntax-tests.factor
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
! Copyright (C) 2024 Your name.
! See https://factorcode.org/license.txt for BSD license.
USING: tools.test combinators.syntax math kernel ;
IN: combinators.syntax.tests

{ 3 1 } [
2 3
*[ 1 + | 2 - ]
] unit-test

{ 6 7 } [
5
&[ 1 + | 2 + ]
] unit-test

{ 7 7 } [
5 2
[| x | &[ x + | x + ] ] call
] unit-test

{ 3 -1 } [
1 2
2 n&[ + | - ]
] unit-test

{ 7 -1 } [
3 4 1 2
2 n*[ + | - ]
] unit-test

{ 7 -1 } [
14 6
2 @[ 7 - ]
] unit-test

{ 1 2 } [
0 1 1 1
2 2 n@[ + ]
Expand Down
49 changes: 34 additions & 15 deletions extra/combinators/syntax/syntax.factor
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
USING: kernel parser sequences vectors words lexer quotations combinators generalizations ;
USING: kernel parser sequences vectors words lexer quotations
combinators generalizations ;
IN: combinators.syntax


: | ( -- ) ; delimiter

<PRIVATE
! unlike normal parse-until, this also pushes the thing that matched the predicate into the accumulator as well
: (parse-until-pred) ( acc end-pred -- ... seq ) [
Expand All @@ -15,36 +16,54 @@ IN: combinators.syntax
} cond
] curry loop ; inline

: parse-until-pred ( end-pred -- seq ) 100 <vector> swap (parse-until-pred) ; inline
: parse-until-pred ( end-pred -- seq )
100 <vector> swap (parse-until-pred) ; inline

: (parse-cleave-like) ( acc -- acc continue? ) [ [ \ | eq? ] [ \ ] eq? ] bi or ] parse-until-pred unclip-last [ >quotation suffix! ] dip \ | eq? ;
: (parse-cleave-like) ( acc -- acc continue? )
[ [ \ | eq? ] [ \ ] eq? ] bi or ] parse-until-pred
unclip-last [ >quotation suffix! ] dip \ | eq? ;

: parse-cleave-quotations ( -- quotations ) 100 <vector> [ (parse-cleave-like) ] loop ;
: parse-cleave-quotations ( -- quotations )
100 <vector> [ (parse-cleave-like) ] loop ;

: parse-cleave-like ( acc word -- acc ) parse-cleave-quotations swap [ suffix! ] bi@ ;
: parse-cleave-like ( acc word -- acc )
parse-cleave-quotations swap [ suffix! ] bi@ ;

! couldn't think of a better name. napply, nspread, ncleave ect. are all macros that take in numbers as the top parameter on the stack, meaning that you have to do a bit of shuffling around before they work
: parse-number-macro-input ( acc word parser-quot -- acc ) [ unclip-last ] [ 1quotation ] [ call( -- quot ) ] tri* -rot 2curry append! ;
! couldn't think of a better name. napply, nspread, ncleave etc.
! are all macros that take in numbers as the top parameter on the
! stack, meaning that you have to do a bit of shuffling around
! before they work
: parse-number-macro-input ( acc word parser-quot -- acc )
[ unclip-last ] [ 1quotation ] [ call( -- quot ) ] tri* -rot 2curry append! ;

: 2parse-number-macro-input ( acc word parser-quot -- acc ) [ 2 cut* ] 2dip [ suffix! >quotation ] dip call( -- quot ) swap curry append! ;
: 2parse-number-macro-input ( acc word parser-quot -- acc )
[ 2 cut* ] 2dip [ suffix! >quotation ] dip call( -- quot ) swap curry append! ;

: parse-ncleave-like ( acc word -- acc ) [ parse-cleave-quotations ] parse-number-macro-input ;
: parse-ncleave-like ( acc word -- acc )
[ parse-cleave-quotations ] parse-number-macro-input ;

: parse-apply ( acc -- acc ) \ napply [ \ ] parse-until >quotation ] parse-number-macro-input ;
: parse-apply ( acc -- acc )
\ napply [ \ ] parse-until >quotation ] parse-number-macro-input ;

: parse-mnapply ( acc -- acc ) \ mnapply [ \ ] parse-until >quotation ] 2parse-number-macro-input ;
: parse-mnapply ( acc -- acc )
\ mnapply [ \ ] parse-until >quotation ] 2parse-number-macro-input ;

PRIVATE>

SYNTAX: cleave[ \ cleave parse-cleave-like ;
SYNTAX: &[ \ cleave parse-cleave-like ;

SYNTAX: spread[ \ spread parse-cleave-like ;
SYNTAX: *[ \ spread parse-cleave-like ;

SYNTAX: apply[ parse-apply ;
SYNTAX: @[ parse-apply ;

SYNTAX: ncleave[ \ ncleave parse-ncleave-like ;
SYNTAX: n&[ \ ncleave parse-ncleave-like ;

SYNTAX: nspread[ \ nspread parse-ncleave-like ;
SYNTAX: n*[ \ nspread parse-ncleave-like ;

SYNTAX: @[ parse-apply ;

SYNTAX: napply[ parse-mnapply ;
SYNTAX: n@[ parse-mnapply ;

0 comments on commit 5d66419

Please sign in to comment.