]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/syntax/syntax-docs.factor
move alien.inline, alien.cxx, alien.marshall to unmaintained; nuke alien.structs
[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 ;
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 { $subsection POSTPONE: ALIEN: }
17 { $subsection POSTPONE: DLL" } ;
18
19 HELP: LIBRARY:
20 { $syntax "LIBRARY: name" }
21 { $values { "name" "a logical library name" } }
22 { $description "Sets the logical library for consequent " { $link POSTPONE: FUNCTION: } " definitions that follow." } ;
23
24 HELP: FUNCTION:
25 { $syntax "FUNCTION: return name ( parameters )" }
26 { $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, ..." } } }
27 { $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."
28 $nl
29 "The new word must be compiled before being executed." }
30 { $examples
31 "For example, suppose the " { $snippet "foo" } " library exports the following function:"
32 { $code
33     "void the_answer(char* question, int value) {"
34     "    printf(\"The answer to %s is %d.\n\",question,value);"
35     "}"
36 }
37 "You can define a word for invoking it:"
38 { $unchecked-example
39     "LIBRARY: foo\nFUNCTION: void the_answer ( char* question, int value ) ;"
40     "USE: compiler"
41     "\"the question\" 42 the_answer"
42     "The answer to the question is 42."
43 } }
44 { $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:"
45 { $code
46     "FUNCTION: void glHint ( GLenum target, GLenum mode ) ;"
47     "FUNCTION: void glHint GLenum target GLenum mode ;"
48 } } ;
49
50 HELP: TYPEDEF:
51 { $syntax "TYPEDEF: old new" }
52 { $values { "old" "a C type" } { "new" "a C type" } }
53 { $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } " if ." }
54 { $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." } ;
55
56 HELP: C-ENUM:
57 { $syntax "C-ENUM: words... ;" }
58 { $values { "words" "a sequence of word names" } }
59 { $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." }
60 { $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." }
61 { $examples
62     "Here is an example enumeration definition:"
63     { $code "C-ENUM: red green blue ;" }
64     "It is equivalent to the following series of definitions:"
65     { $code "CONSTANT: red 0" "CONSTANT: green 1" "CONSTANT: blue 2" }
66 } ;
67
68 HELP: CALLBACK:
69 { $syntax "CALLBACK: return type ( parameters ) ;" }
70 { $values { "return" "a C return type" } { "type" "a type name" } { "parameters" "a comma-separated sequence of type/name pairs; " { $snippet "type1 arg1, type2 arg2, ..." } } }
71 { $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 with the " { $snippet "\"cdecl\"" } " ABI." }
72 { $examples
73     { $code
74         "CALLBACK: bool FakeCallback ( int message, void* payload ) ;"
75         ": MyFakeCallback ( -- alien )"
76         "    [| message payload |"
77         "        \"message #\" write"
78         "        message number>string write"
79         "        \" received\" write nl"
80         "        t"
81         "    ] FakeCallback ;"
82     }
83 } ;
84
85 HELP: STDCALL-CALLBACK:
86 { $syntax "STDCALL-CALLBACK: return type ( parameters ) ;" }
87 { $values { "return" "a C return type" } { "type" "a type name" } { "parameters" "a comma-separated sequence of type/name pairs; " { $snippet "type1 arg1, type2 arg2, ..." } } }
88 { $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 with the " { $snippet "\"stdcall\"" } " ABI." }
89 { $examples
90     { $code
91         "STDCALL-CALLBACK: bool FakeCallback ( int message, void* payload ) ;"
92         ": MyFakeCallback ( -- alien )"
93         "    [| message payload |"
94         "        \"message #\" write"
95         "        message number>string write"
96         "        \" received\" write nl"
97         "        t"
98         "    ] FakeCallback ;"
99     }
100 } ;
101
102 { POSTPONE: CALLBACK: POSTPONE: STDCALL-CALLBACK: } related-words 
103
104 HELP: &:
105 { $syntax "&: symbol" }
106 { $values { "symbol" "A C library symbol name" } }
107 { $description "Pushes the address of a symbol named " { $snippet "symbol" } " from the current library, set with " { $link POSTPONE: LIBRARY: } "." } ;
108
109 HELP: typedef
110 { $values { "old" "a string" } { "new" "a string" } }
111 { $description "Aliases the C type " { $snippet "old" } " under the name " { $snippet "new" } "." }
112 { $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." } ;
113
114 { POSTPONE: TYPEDEF: typedef } related-words
115
116 HELP: c-struct?
117 { $values { "type" "a string" } { "?" "a boolean" } }
118 { $description "Tests if a C type is a structure defined by " { $link POSTPONE: STRUCT: } "." } ;
119
120 HELP: define-function
121 { $values { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } }
122 { $description "Defines a word named " { $snippet "function" } " in the current vocabulary (see " { $link "vocabularies" } "). The word calls " { $link alien-invoke } " with the specified parameters." }
123 { $notes "This word is used to implement the " { $link POSTPONE: FUNCTION: } " parsing word." } ;