M: byte-array byte-length length ;
+M: f byte-length drop 0 ;
+
: c-getter ( name -- quot )
c-type-getter [
[ "Cannot read struct fields with this type" throw ]
[ push ] [ drop ] 2bi ;
: set-default-password ( ctx -- )
- [ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
- [
- [ handle>> ] [ default-pasword ] bi
- SSL_CTX_set_default_passwd_cb_userdata
- ] bi ;
+ dup config>> password>> [
+ [ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
+ [
+ [ handle>> ] [ default-pasword ] bi
+ SSL_CTX_set_default_passwd_cb_userdata
+ ] bi
+ ] [ drop ] if ;
: use-private-key-file ( ctx -- )
dup config>> key-file>> [
-USING: help.markup help.syntax quotations kernel ;
+USING: help.markup help.syntax quotations kernel
+stack-checker.transforms sequences ;
IN: macros
HELP: MACRO:
{ $syntax "MACRO: word ( inputs... -- ) definition... ;" }
-{ $description "Defines a compile-time code transformation. If all inputs to the word are literal and the word calling the macro has a static stack effect, then the macro body is invoked at compile-time to produce a quotation; this quotation is then spliced into the compiled code. If the inputs are not literal, or if the word is invoked from a word which does not have a static stack effect, the macro body will execute every time and the result will be passed to " { $link call } "."
-$nl
-"The stack effect declaration must be present because it tells the compiler how many literal inputs to expect."
-}
+{ $description "Defines a code transformation. The definition must have stack effect " { $snippet "( inputs... -- quot )" } "." }
{ $notes
- "Semantically, the following two definitions are equivalent:"
+ "A call of a macro inside a word definition is replaced with the quotation expansion at compile-time if precisely the following conditions hold:"
+ { $list
+ { "All inputs to the macro call are literal" }
+ { "The word calling the macro has a static stack effect" }
+ { "The expansion quotation produced by the macro has a static stack effect" }
+ }
+ "If any of these conditions fail to hold, the macro will still work, but expansion will be performed at run-time."
+ $nl
+ "Other than possible compile-time expansion, the following two definition styles are equivalent:"
{ $code "MACRO: foo ... ;" }
{ $code ": foo ... call ;" }
- "However, the compiler folds in macro definitions at compile-time where possible; if the macro body performs an expensive calculation, it can lead to a performance boost."
+ "Conceptually, macros allow computation to be moved from run-time to compile-time, splicing the result of this computation into the generated quotation."
+}
+{ $examples
+ "A macro that calls a quotation but preserves any values it consumes off the stack:"
+ { $code
+ "USING: fry generalizations ;"
+ "MACRO: preserving ( quot -- )"
+ " [ infer in>> length ] keep '[ _ ndup @ ] ;"
+ }
+ "Using this macro, we can define a variant of " { $link if } " which takes a predicate quotation instead of a boolean; any values consumed by the predicate quotation are restored immediately after:"
+ { $code
+ ": ifte ( pred true false -- ) [ preserving ] 2dip if ; inline"
+ }
+ "Note that " { $snippet "ifte" } " is an ordinary word, and it passes one of its inputs to the macro. If another word calls " { $snippet "ifte" } " with all three input quotations literal, then " { $snippet "ifte" } " will be inlined and " { $snippet "preserving" } " will expand at compile-time, and the generated machine code will be exactly the same as if the inputs consumed by the predicate were duplicated by hand."
+ $nl
+ "The " { $snippet "ifte" } " combinator presented here has similar semantics to the " { $snippet "ifte" } " combinator of the Joy programming language."
} ;
HELP: macro
{ $class-description "Class of words defined with " { $link POSTPONE: MACRO: } "." } ;
ARTICLE: "macros" "Macros"
-"The " { $vocab-link "macros" } " vocabulary implements macros in the Lisp sense; compile-time code transformers and generators. Macros can be used to calculate lookup tables and generate code at compile time, which can improve performance, the level of abstraction and simplify code."
+"The " { $vocab-link "macros" } " vocabulary implements " { $emphasis "macros" } ", which are code transformations that may run at compile-time under the right circumstances."
+$nl
+"Macros can be used to give static stack effects to combinators that otherwise would not have static stack effects. Macros can be used to calculate lookup tables and generate code at compile time, which can improve performance, the level of abstraction and simplify code."
+$nl
+"Factor macros are similar to Lisp macros; they are not like C preprocessor macros."
$nl
"Defining new macros:"
{ $subsection POSTPONE: MACRO: }
-"Macros are really just a very thin layer of syntax sugar over " { $link "compiler-transforms" } "." ;
+"A slightly lower-level facility, " { $emphasis "compiler transforms" } ", allows an ordinary word definition to co-exist with a version that performs compile-time expansion."
+{ $subsection define-transform }
+"An example is the " { $link member? } " word. If the input sequence is a literal, the compile transform kicks in and converts the " { $link member? } " call into a series of conditionals. Otherwise, if the input sequence is not literal, a call to the definition of " { $link member? } " is generated." ;
ABOUT: "macros"
definitions quotations namespaces memoize accessors ;
IN: macros
+<PRIVATE
+
: real-macro-effect ( word -- effect' )
"declared-effect" word-prop in>> 1 <effect> ;
+PRIVATE>
+
: define-macro ( word definition -- )
[ "macro" set-word-prop ]
[ over real-macro-effect memoize-quot [ call ] append define ]
MACRO: pack ( str -- quot )
[ pack-table at '[ _ execute ] ] { } map-as
- '[ _ spread ]
- '[ _ input<sequence ]
- '[ _ B{ } append-outputs-as ] ;
+ '[ [ [ _ spread ] input<sequence ] B{ } append-outputs-as ] ;
PRIVATE>
[ [ ch>packed-length ] { } map-as start/end ]
[ [ unpack-table at '[ @ ] ] { } map-as ] bi
[ '[ [ _ _ ] dip <slice> @ ] ] 3map
- '[ _ cleave ] '[ _ output>array ] ;
+ '[ [ _ cleave ] output>array ] ;
PRIVATE>
M: object infer-call*
\ literal-expected inference-warning ;
-: infer-slip ( -- )
- 1 infer->r infer-call 1 infer-r> ;
+: infer-nslip ( n -- )
+ [ infer->r infer-call ] [ infer-r> ] bi ;
-: infer-2slip ( -- )
- 2 infer->r infer-call 2 infer-r> ;
+: infer-slip ( -- ) 1 infer-nslip ;
-: infer-3slip ( -- )
- 3 infer->r infer-call 3 infer-r> ;
+: infer-2slip ( -- ) 2 infer-nslip ;
-: infer-dip ( -- )
- literals get
- [ \ dip def>> infer-quot-here ]
- [ pop 1 infer->r infer-quot-here 1 infer-r> ]
- if-empty ;
+: infer-3slip ( -- ) 3 infer-nslip ;
-: infer-2dip ( -- )
- literals get
- [ \ 2dip def>> infer-quot-here ]
- [ pop 2 infer->r infer-quot-here 2 infer-r> ]
+: infer-ndip ( word n -- )
+ [ literals get ] 2dip
+ [ '[ _ def>> infer-quot-here ] ]
+ [ '[ _ [ pop ] dip [ infer->r infer-quot-here ] [ infer-r> ] bi ] ] bi*
if-empty ;
-: infer-3dip ( -- )
- literals get
- [ \ 3dip def>> infer-quot-here ]
- [ pop 3 infer->r infer-quot-here 3 infer-r> ]
- if-empty ;
+: infer-dip ( -- ) \ dip 1 infer-ndip ;
+
+: infer-2dip ( -- ) \ 2dip 2 infer-ndip ;
+
+: infer-3dip ( -- ) \ 3dip 3 infer-ndip ;
+
+: infer-builder ( quot word -- )
+ [
+ [ 2 consume-d ] dip
+ [ dup first2 ] dip call make-known
+ [ push-d ] [ 1array ] bi
+ ] dip #call, ; inline
+
+: infer-curry ( -- ) [ <curried> ] \ curry infer-builder ;
-: infer-curry ( -- )
- 2 consume-d
- dup first2 <curried> make-known
- [ push-d ] [ 1array ] bi
- \ curry #call, ;
-
-: infer-compose ( -- )
- 2 consume-d
- dup first2 <composed> make-known
- [ push-d ] [ 1array ] bi
- \ compose #call, ;
+: infer-compose ( -- ) [ <composed> ] \ compose infer-builder ;
: infer-execute ( -- )
pop-literal nip
"[ [ 5 ] t foo ] infer."
} ;
-ARTICLE: "compiler-transforms" "Compiler transforms"
-"Compiler transforms can be used to allow words to compile which would otherwise not have a stack effect, and to expand combinators into more efficient code at compile time."
-{ $subsection define-transform }
-"An example is the " { $link cond } " word. If the association list of quotations it is given is literal, the entire form is expanded into a series of nested calls to " { $link if } "."
-$nl
-"The " { $vocab-link "macros" } " vocabulary defines some nice syntax sugar which makes compiler transforms easier to work with." ;
-
ARTICLE: "inference" "Stack effect inference"
"The stack effect inference tool is used to check correctness of code before it is run. It is also used by the optimizing compiler to build the high-level SSA representation on which optimizations can be performed. Only words for which a stack effect can be inferred will compile with the optimizing compiler; all other words will be compiled with the non-optimizing compiler (see " { $link "compiler" } ")."
$nl
{ $subsection "inference-recursive-combinators" }
{ $subsection "inference-branches" }
{ $subsection "inference-errors" }
-{ $subsection "compiler-transforms" }
{ $see-also "effects" } ;
ABOUT: "inference"
[ bogus-error ] must-infer
[ [ clear ] infer. ] [ inference-error? ] must-fail-with
+
+: debugging-curry-folding ( quot -- )
+ [ debugging-curry-folding ] curry call ; inline recursive
+
+[ [ ] debugging-curry-folding ] must-infer
\ No newline at end of file
HELP: define-transform
{ $values { "word" word } { "quot" "a quotation taking " { $snippet "n" } " inputs from the stack and producing another quotation as output" } { "n" "a non-negative integer" } }
-{ $description "Defines a compiler transform for the optimizing compiler. When a call to " { $snippet "word" } " is being compiled, the compiler ensures that the top " { $snippet "n" } " stack values are literal; if they are not, compilation fails. The literal values are passed to the quotation, which is expected to produce a new quotation. The call to the word is then replaced by this quotation." }
-{ $examples "Here is a word which pops " { $snippet "n" } " values from the stack:"
-{ $code ": ndrop ( n -- ) [ drop ] times ;" }
-"This word is inefficient; it does not have a static stack effect. This means that words calling " { $snippet "ndrop" } " cannot be compiled by the optimizing compiler, and additionally, a call to this word will always involve a loop with arithmetic, even if the value of " { $snippet "n" } " is known at compile time. A compiler transform can fix this:"
-{ $code "\\ ndrop [ \\ drop <repetition> >quotation ] 1 define-transform" }
-"Now, a call like " { $snippet "4 ndrop" } " is replaced with " { $snippet "drop drop drop drop" } " at compile time; the optimizer then ensures that this compiles as a single machine instruction, which is a lot cheaper than an actual call to " { $snippet "ndrop" } "."
+{ $description "Defines a compiler transform for the optimizing compiler."
+ "When a call to " { $snippet "word" } " is being compiled, the compiler first checks that the top " { $snippet "n" } " stack values are literal, and if so, calls the quotation with those inputs at compile time. The quotation can output a new quotation, or " { $link f } "."
$nl
-"The " { $link cond } " word compiles to efficient code because it is transformed using " { $link cond>quot } ":"
+"If the quotation outputs " { $link f } ", or if not all inputs are literal, a call to the word is compiled as usual, or compilation fails if the word does not have a static stack effect."
+$nl
+"Otherwise, if the transform output a new quotation, the quotation replaces the word's call site." }
+{ $examples "The " { $link cond } " word compiles to efficient code because it is transformed using " { $link cond>quot } ":"
{ $code "\\ cond [ cond>quot ] 1 define-transform" } } ;
[ [ "a" "b" "c" ] very-smart-combo ] must-infer
[ [ [ "a" "b" ] very-smart-combo "c" ] very-smart-combo ] must-infer
+
+! Caveat found by Doug
+DEFER: curry-folding-test ( quot -- )
+
+\ curry-folding-test [ length \ drop <repetition> >quotation ] 1 define-transform
+
+{ 3 0 } [ [ 1 2 3 ] curry-folding-test ] must-infer-as
+{ 3 0 } [ 1 [ 2 3 ] curry curry-folding-test ] must-infer-as
+{ 3 0 } [ [ 1 2 ] 3 [ ] curry compose curry-folding-test ] must-infer-as
\ No newline at end of file
rstate infer-quot
] [ word give-up-transform ] if* ;
+: literals? ( values -- ? ) [ literal-value? ] all? ;
+
: (apply-transform) ( word quot n -- )
- ensure-d dup [ known literal? ] all? [
+ ensure-d dup literals? [
dup empty? [ dup recursive-state get ] [
[ ]
[ [ literal value>> ] map ]
: copy-values ( values -- values' )
[ copy-value ] map ;
+GENERIC: (literal-value?) ( value -- ? )
+
+M: object (literal-value?) drop f ;
+
+GENERIC: (literal) ( value -- literal )
+
! Literal value
TUPLE: literal < identity-tuple value recursion hashcode ;
+: literal ( value -- literal ) known (literal) ;
+
+: literal-value? ( value -- ? ) known (literal-value?) ;
+
M: literal hashcode* nip hashcode>> ;
: <literal> ( obj -- value )
recursive-state get over hashcode \ literal boa ;
-GENERIC: (literal) ( value -- literal )
+M: literal (literal-value?) drop t ;
M: literal (literal) ;
-: literal ( value -- literal )
- known (literal) ;
+: curried/composed-literal ( input1 input2 quot -- literal )
+ [ [ literal ] bi@ ] dip
+ [ [ [ value>> ] bi@ ] dip call ] [ drop nip recursion>> ] 3bi
+ over hashcode \ literal boa ; inline
! Result of curry
TUPLE: curried obj quot ;
C: <curried> curried
+: >curried< ( curried -- obj quot )
+ [ obj>> ] [ quot>> ] bi ; inline
+
+M: curried (literal-value?) >curried< [ literal-value? ] both? ;
+M: curried (literal) >curried< [ curry ] curried/composed-literal ;
+
! Result of compose
TUPLE: composed quot1 quot2 ;
C: <composed> composed
+
+: >composed< ( composed -- quot1 quot2 )
+ [ quot1>> ] [ quot2>> ] bi ; inline
+
+M: composed (literal-value?) >composed< [ literal-value? ] both? ;
+M: composed (literal) >composed< [ compose ] curried/composed-literal ;
\ No newline at end of file
{ $subsection assert }
{ $subsection assert= } ;
+ARTICLE: "dataflow-combinators" "Data flow combinators"
+"Data flow combinators pass values between quotations:"
+{ $subsection "slip-keep-combinators" }
+{ $subsection "cleave-combinators" }
+{ $subsection "spread-combinators" }
+{ $subsection "apply-combinators" } ;
+
ARTICLE: "dataflow" "Data and control flow"
{ $subsection "evaluator" }
{ $subsection "words" }
{ $subsection "booleans" }
{ $subsection "shuffle-words" }
"A central concept in Factor is that of a " { $emphasis "combinator" } ", which is a word taking code as input."
-$nl
-"Data flow combinators:"
-{ $subsection "slip-keep-combinators" }
-{ $subsection "cleave-combinators" }
-{ $subsection "spread-combinators" }
-{ $subsection "apply-combinators" }
-"Control flow combinators:"
+{ $subsection "dataflow-combinators" }
{ $subsection "conditionals" }
{ $subsection "looping-combinators" }
-"Additional combinators:"
{ $subsection "compositional-combinators" }
{ $subsection "combinators" }
"More combinators are defined for working on data structures, such as " { $link "sequences-combinators" } " and " { $link "assocs-combinators" } "."
"Advanced topics:"
{ $subsection "assertions" }
{ $subsection "implementing-combinators" }
+{ $subsection "macros" }
{ $subsection "errors" }
{ $subsection "continuations" } ;
return retval;
}
-complex float ffi_test_45(complex float x, complex double y)
+_Complex float ffi_test_45(_Complex float x, _Complex double y)
{
return x + 2 * y;
}
DLLEXPORT struct test_struct_14 ffi_test_44();
-complex float ffi_test_45(complex float x, complex double y);
+DLLEXPORT _Complex float ffi_test_45(_Complex float x, _Complex double y);
#include <fcntl.h>
#include <limits.h>
#include <math.h>
-#include <complex.h>
#include <stdbool.h>
#include <setjmp.h>