Extend C-ENUM: to allow specifying the constant vvalues like in C. Add C-TYPED-ENUM: to automatically typedef a type.

release
Erik Charlebois 2010-04-06 02:46:23 -07:00
parent 2a2d9eb0a2
commit 4b78fe690b
3 changed files with 28 additions and 7 deletions

View File

@ -105,7 +105,7 @@ $nl
"Important guidelines for passing data in byte arrays:"
{ $subsections "byte-arrays-gc" }
"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:"
{ $subsections POSTPONE: TYPEDEF: }
"A utility for defining " { $link "destructors" } " for deallocating memory:"

View File

@ -60,13 +60,24 @@ HELP: TYPEDEF:
HELP: C-ENUM:
{ $syntax "C-ENUM: words... ;" }
{ $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." }
{ $examples
"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:"
{ $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:

View File

@ -24,9 +24,19 @@ SYNTAX: CALLBACK:
SYNTAX: TYPEDEF:
scan-c-type CREATE-C-TYPE dup save-location typedef ;
SYNTAX: C-ENUM:
";" parse-tokens
[ [ create-in ] dip define-constant ] each-index ;
: define-enum-members ( counter -- )
scan dup ";" = not [
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:
void CREATE-C-TYPE typedef ;