- nicer way to combine two paths\r
- catchstack lists\r
- OOP\r
-- refactor sort\r
- ditch object paths\r
- browser responder for word links in HTTPd; inspect responder for\r
objects\r
-- use keep instead of tuck, try to remove usages of transp\r
- worddef props\r
- prettyprint: when unparse called due to recursion, write a link\r
- prettyprinter should output {{ ... }} syntax for hashtables\r
: compile ( word -- )
[ postpone-word compile-postponed ] with-compiler ;
-: compiled word compile ; parsing
+: compiled
+ #! Compile the most recently defined word.
+ word compile ; parsing
dup cons? [ tail ] when not ;
: partition-add ( obj ? ret1 ret2 -- ret1 ret2 )
- rot [ >r cons r> ] [ swapd cons ] ifte ; inline
+ rot [ swapd cons ] [ >r cons r> ] ifte ; inline
-: partition-step ( list combinator -- cdr combinator car ? )
- over car over call >r >r unswons r> swap r> ; inline
+: partition-step ( ref list combinator -- ref cdr combinator car ? )
+ pick pick car pick call >r >r unswons r> swap r> ; inline
-: (partition) ( list combinator ret1 ret2 -- ret1 ret2 )
+: (partition) ( ref list combinator ret1 ret2 -- ret1 ret2 )
>r >r over [
partition-step r> r> partition-add (partition)
] [
- 2drop r> r>
+ 3drop r> r>
] ifte ; inline
-: partition ( list ref combinator -- list1 list2 )
+: partition ( ref list combinator -- list1 list2 )
#! The combinator must have stack effect:
#! ( ref element -- ? )
- cons [ ] [ ] (partition) ; inline
+ [ ] [ ] (partition) ; inline
: sort ( list comparator -- sorted )
#! To sort in ascending order, comparator must have stack
#! effect ( x y -- x>y ).
over [
- ( Partition ) [ >r uncons over r> partition ] keep
+ ( Partition ) [ >r uncons dupd r> partition ] keep
( Recurse ) [ sort swap ] keep sort
( Combine ) swapd cons append
] [
IN: syntax
-: recursive-infer ( -- )
- #! Mark the last word to be recursively inferred (eg, cond).
- word t "recursive-infer" set-word-property ; parsing
-
: inline ( -- )
#! Mark the last word to be inlined.
word t "inline" set-word-property ; parsing
#! either execute the word in the meta interpreter (if it is
#! side-effect-free and all parameters are literal), or
#! simply apply its stack effect to the meta-interpreter.
- dup car ensure-d
swap "infer" word-property dup [
- nip call
+ swap car ensure-d call
] [
drop consume/produce
] ifte ;
base-case off effect entry-effect set
] extend ;
+: init-inference ( recursive-state -- )
+ init-interpreter
+ 0 d-in set
+ 0 r-in set
+ recursive-state set ;
+
DEFER: (infer)
+: with-recursive-state ( word quot -- )
+ over <recursive-state> cons recursive-state cons@
+ call
+ recursive-state uncons@ drop ;
+
+: recursive-infer ( quot -- )
+ [
+ recursive-state get init-inference
+ (infer) effect
+ ] with-scope ;
+
: apply-compound ( word -- )
#! Infer a compound word's stack effect.
- dup <recursive-state> cons recursive-state cons@
- word-parameter (infer)
- recursive-state uncons@ drop ;
+ [
+ word-parameter [
+ recursive-infer consume/produce
+ ] [
+ [ (infer) ] when
+ ] catch
+ ] with-recursive-state ;
: apply-word ( word -- )
#! Apply the word's stack effect to the inferencer state.
push-d
] ifte ;
-: init-inference ( -- )
- init-interpreter
- 0 d-in set
- 0 r-in set
- f recursive-state set ;
-
: (infer) ( quot -- )
#! Recursive calls to this word are made for nested
#! quotations.
: infer ( quot -- [ in | out ] )
#! Stack effect of a quotation.
- [ init-inference (infer) effect ] with-scope ;
+ [ f init-inference (infer) effect ] with-scope ;
+
+: try-infer ( quot -- effect/f )
+ #! Push f if inference fails.
+ [ infer ] [ [ drop f ] when ] catch ;
: meta-infer ( word -- )
#! Mark a word as being partially evaluated.
\ r> [ pop-r push-d ] "infer" set-word-property
\ drop meta-infer
+\ 2drop meta-infer
+\ 3drop meta-infer
\ dup meta-infer
+\ 2dup meta-infer
+\ 3dup meta-infer
\ swap meta-infer
\ over meta-infer
\ pick meta-infer
\ nip meta-infer
\ tuck meta-infer
\ rot meta-infer
+\ -rot meta-infer
+\ 2nip meta-infer
+\ transp meta-infer
+\ dupd meta-infer
+\ swapd meta-infer
\ + [ 2 | 1 ] "infer-effect" set-word-property
\ - [ 2 | 1 ] "infer-effect" set-word-property