]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/syntax/syntax-docs.factor
docs: change $subsection to $subsections
[factor.git] / basis / alien / syntax / syntax-docs.factor
1 IN: alien.syntax
2 USING: alien alien.c-types alien.parser classes.struct help.markup help.syntax see ;
3
4 HELP: DLL"
5 { $syntax "DLL\" path\"" }
6 { $values { "path" "a pathname string" } }
7 { $description "Constructs a DLL handle at parse time." } ;
8
9 HELP: ALIEN:
10 { $syntax "ALIEN: address" }
11 { $values { "address" "a non-negative hexadecimal integer" } }
12 { $description "Creates an alien object at parse time." }
13 { $notes "Alien objects are invalidated between image saves and loads, and hence source files should not contain alien literals; this word is for interactive use only. See " { $link "alien-expiry" } " for details." } ;
14
15 ARTICLE: "syntax-aliens" "Alien object literal syntax"
16 { $subsections
17     POSTPONE: ALIEN:
18     POSTPONE: DLL"
19 } ;
20
21 HELP: LIBRARY:
22 { $syntax "LIBRARY: name" }
23 { $values { "name" "a logical library name" } }
24 { $description "Sets the logical library for consequent " { $link POSTPONE: FUNCTION: } " definitions that follow." } ;
25
26 HELP: FUNCTION:
27 { $syntax "FUNCTION: return name ( parameters )" }
28 { $values { "return" "a C return type" } { "name" "a C function name" } { "parameters" "a comma-separated sequence of type/name pairs; " { $snippet "type1 arg1, type2 arg2, ..." } } }
29 { $description "Defines a new word " { $snippet "name" } " which calls a C library function with the same name, in the logical library given by the most recent " { $link POSTPONE: LIBRARY: } " declaration."
30 $nl
31 "The new word must be compiled before being executed." }
32 { $examples
33 "For example, suppose the " { $snippet "foo" } " library exports the following function:"
34 { $code
35     "void the_answer(char* question, int value) {"
36     "    printf(\"The answer to %s is %d.\n\",question,value);"
37     "}"
38 }
39 "You can define a word for invoking it:"
40 { $unchecked-example
41     "LIBRARY: foo\nFUNCTION: void the_answer ( char* question, int value ) ;"
42     "USE: compiler"
43     "\"the question\" 42 the_answer"
44     "The answer to the question is 42."
45 } }
46 { $notes "Note that the parentheses and commas are only syntax sugar and can be omitted; they serve no purpose other than to make the declaration slightly easier to read:"
47 { $code
48     "FUNCTION: void glHint ( GLenum target, GLenum mode ) ;"
49     "FUNCTION: void glHint GLenum target GLenum mode ;"
50 } } ;
51
52 HELP: TYPEDEF:
53 { $syntax "TYPEDEF: old new" }
54 { $values { "old" "a C type" } { "new" "a C type" } }
55 { $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } " if ." }
56 { $notes "This word differs from " { $link typedef } " in that it runs at parse time, to ensure correct ordering of operations when loading source files. Words defined in source files are compiled before top-level forms are run, so if a source file defines C binding words and uses " { $link typedef } ", the type alias won't be available at compile time." } ;
57
58 HELP: C-ENUM:
59 { $syntax "C-ENUM: words... ;" }
60 { $values { "words" "a sequence of word names" } }
61 { $description "Creates a sequence of word definitions in the current vocabulary. Each word pushes an integer according to its index in the enumeration definition. The first word pushes 0." }
62 { $notes "This word emulates a C-style " { $snippet "enum" } " in Factor. While this feature can be used for any purpose, using integer constants is discouraged unless it is for interfacing with C libraries. Factor code should use " { $link "words.symbol" } " or " { $link "singletons" } " instead." }
63 { $examples
64     "Here is an example enumeration definition:"
65     { $code "C-ENUM: red green blue ;" }
66     "It is equivalent to the following series of definitions:"
67     { $code "CONSTANT: red 0" "CONSTANT: green 1" "CONSTANT: blue 2" }
68 } ;
69
70 HELP: C-TYPE:
71 { $syntax "C-TYPE: type" }
72 { $values { "type" "a new C type" } }
73 { $description "Defines a new, opaque C type. Since it is opaque, " { $snippet "type" } " will not be directly usable as a parameter or return type of a " { $link POSTPONE: FUNCTION: } " or as a slot of a " { $link POSTPONE: STRUCT: } ". However, it can be used as the type of a pointer (that is, as " { $snippet "type*" } ")." $nl
74 { $snippet "C-TYPE:" } " can also be used to forward-declare C types to enable circular dependencies. For example:"
75 { $code """C-TYPE: forward 
76 STRUCT: backward { x forward* } ;
77 STRUCT: forward { x backward* } ; """ } }
78 { $notes "Primitive C types are also displayed using " { $snippet "C-TYPE:" } " syntax when they are displayed by " { $link see } "." } ;
79
80 HELP: CALLBACK:
81 { $syntax "CALLBACK: return type ( parameters ) ;" }
82 { $values { "return" "a C return type" } { "type" "a type name" } { "parameters" "a comma-separated sequence of type/name pairs; " { $snippet "type1 arg1, type2 arg2, ..." } } }
83 { $description "Defines a new function pointer C type word " { $snippet "type" } ". The newly defined word works both as a C type and as a wrapper for " { $link alien-callback } " for callbacks that accept the given return type and parameters. The ABI of the callback is decided from the ABI of the active " { $link POSTPONE: LIBRARY: } " declaration." }
84 { $examples
85     { $code
86         "CALLBACK: bool FakeCallback ( int message, void* payload ) ;"
87         ": MyFakeCallback ( -- alien )"
88         "    [| message payload |"
89         "        \"message #\" write"
90         "        message number>string write"
91         "        \" received\" write nl"
92         "        t"
93         "    ] FakeCallback ;"
94     }
95 } ;
96
97 HELP: &:
98 { $syntax "&: symbol" }
99 { $values { "symbol" "A C library symbol name" } }
100 { $description "Pushes the address of a symbol named " { $snippet "symbol" } " from the current library, set with " { $link POSTPONE: LIBRARY: } "." } ;
101
102 HELP: typedef
103 { $values { "old" "a string" } { "new" "a string" } }
104 { $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } "." }
105 { $notes "Using this word in the same source file which defines C bindings can cause problems, because words are compiled before top-level forms are run. Use the " { $link POSTPONE: TYPEDEF: } " word instead." } ;
106
107 { POSTPONE: TYPEDEF: typedef } related-words
108
109 HELP: c-struct?
110 { $values { "c-type" "a C type name" } { "?" "a boolean" } }
111 { $description "Tests if a C type is a structure defined by " { $link POSTPONE: STRUCT: } "." } ;
112
113 HELP: define-function
114 { $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } }
115 { $description "Defines a word named " { $snippet "function" } " in the current vocabulary (see " { $link "vocabularies" } "). The word calls " { $link alien-invoke } " with the specified parameters." }
116 { $notes "This word is used to implement the " { $link POSTPONE: FUNCTION: } " parsing word." } ;