Extend C-ENUM: to allow specifying the constant vvalues like in C. Add C-TYPED-ENUM: to automatically typedef a type.
parent
2a2d9eb0a2
commit
4b78fe690b
|
@ -105,7 +105,7 @@ $nl
|
||||||
"Important guidelines for passing data in byte arrays:"
|
"Important guidelines for passing data in byte arrays:"
|
||||||
{ $subsections "byte-arrays-gc" }
|
{ $subsections "byte-arrays-gc" }
|
||||||
"C-style enumerated types are supported:"
|
"C-style enumerated types are supported:"
|
||||||
{ $subsections POSTPONE: C-ENUM: }
|
{ $subsections POSTPONE: C-ENUM: POSTPONE: C-TYPED-ENUM: }
|
||||||
"C types can be aliased for convenience and consistency with native library documentation:"
|
"C types can be aliased for convenience and consistency with native library documentation:"
|
||||||
{ $subsections POSTPONE: TYPEDEF: }
|
{ $subsections POSTPONE: TYPEDEF: }
|
||||||
"A utility for defining " { $link "destructors" } " for deallocating memory:"
|
"A utility for defining " { $link "destructors" } " for deallocating memory:"
|
||||||
|
|
|
@ -60,13 +60,24 @@ HELP: TYPEDEF:
|
||||||
HELP: C-ENUM:
|
HELP: C-ENUM:
|
||||||
{ $syntax "C-ENUM: words... ;" }
|
{ $syntax "C-ENUM: words... ;" }
|
||||||
{ $values { "words" "a sequence of word names" } }
|
{ $values { "words" "a sequence of word names" } }
|
||||||
{ $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." }
|
{ $description "Creates a sequence of word definitions in the current vocabulary. Each word pushes an integer according to the rules of C enums." }
|
||||||
{ $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." }
|
{ $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." }
|
||||||
{ $examples
|
{ $examples
|
||||||
"Here is an example enumeration definition:"
|
"Here is an example enumeration definition:"
|
||||||
{ $code "C-ENUM: red green blue ;" }
|
{ $code "C-ENUM: red { green 3 } blue ;" }
|
||||||
"It is equivalent to the following series of definitions:"
|
"It is equivalent to the following series of definitions:"
|
||||||
{ $code "CONSTANT: red 0" "CONSTANT: green 1" "CONSTANT: blue 2" }
|
{ $code "CONSTANT: red 0" "CONSTANT: green 3" "CONSTANT: blue 4" }
|
||||||
|
} ;
|
||||||
|
|
||||||
|
HELP: C-TYPED-ENUM:
|
||||||
|
{ $syntax "C-TYPED-ENUM: foo_t FOO BAR { BAZ 4 } BOL ;" }
|
||||||
|
{ $description "Typedefs the first word as an int and creates a sequence of word definitions in the current vocabulary. Each word pushes an integer according to the rules of C enums." }
|
||||||
|
{ $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." }
|
||||||
|
{ $examples
|
||||||
|
"Here is an example enumeration definition:"
|
||||||
|
{ $code "C-TYPED-ENUM: color_t red { green 3 } blue ;" }
|
||||||
|
"It is equivalent to the following series of definitions:"
|
||||||
|
{ $code "TYPEDEF: int color_t" "CONSTANT: red 0" "CONSTANT: green 3" "CONSTANT: blue 4" }
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
HELP: C-TYPE:
|
HELP: C-TYPE:
|
||||||
|
|
|
@ -24,9 +24,19 @@ SYNTAX: CALLBACK:
|
||||||
SYNTAX: TYPEDEF:
|
SYNTAX: TYPEDEF:
|
||||||
scan-c-type CREATE-C-TYPE dup save-location typedef ;
|
scan-c-type CREATE-C-TYPE dup save-location typedef ;
|
||||||
|
|
||||||
SYNTAX: C-ENUM:
|
: define-enum-members ( counter -- )
|
||||||
";" parse-tokens
|
scan dup ";" = not [
|
||||||
[ [ create-in ] dip define-constant ] each-index ;
|
dup "{" =
|
||||||
|
[ 2drop scan create-in scan-word [ define-constant ] keep "}" expect ]
|
||||||
|
[ create-in swap [ define-constant ] keep ]
|
||||||
|
if 1 + define-enum-members
|
||||||
|
] [ 2drop ] if ;
|
||||||
|
|
||||||
|
SYNTAX: C-ENUM: 0 define-enum-members ;
|
||||||
|
|
||||||
|
SYNTAX: C-TYPED-ENUM:
|
||||||
|
int CREATE-C-TYPE dup save-location typedef
|
||||||
|
0 define-enum-members ;
|
||||||
|
|
||||||
SYNTAX: C-TYPE:
|
SYNTAX: C-TYPE:
|
||||||
void CREATE-C-TYPE typedef ;
|
void CREATE-C-TYPE typedef ;
|
||||||
|
|
Loading…
Reference in New Issue