瀏覽代碼

Enums. Initial support.

woollybah 6 年之前
父節點
當前提交
39e2446df8

+ 11 - 0
blitz.mod/blitz.bmx

@@ -94,6 +94,7 @@ Import "blitz_thread.c"
 Import "blitz_ex.c"
 Import "blitz_ex.c"
 Import "blitz_gc.c"
 Import "blitz_gc.c"
 Import "blitz_unicode.c"
 Import "blitz_unicode.c"
+Import "blitz_enum.c"
 
 
 '?Threaded
 '?Threaded
 'Import "blitz_gc_ms.c"
 'Import "blitz_gc_ms.c"
@@ -871,6 +872,16 @@ bbdoc: End a user defined structure declaration
 keyword: "EndStruct"
 keyword: "EndStruct"
 End Rem
 End Rem
 
 
+Rem
+bbdoc: Begin an enumeration declaration
+keyword: "Enum"
+End Rem
+
+Rem
+bbdoc: End an enumeration declaration
+keyword: "EndEnum"
+End Rem
+
 Rem
 Rem
 bbdoc: Specify supertype(s) of a user defined type
 bbdoc: Specify supertype(s) of a user defined type
 keyword: "Extends"
 keyword: "Extends"

+ 1 - 0
blitz.mod/blitz.h

@@ -38,6 +38,7 @@
 #include "blitz_array.h"
 #include "blitz_array.h"
 #include "blitz_handle.h"
 #include "blitz_handle.h"
 #include "blitz_app.h" 
 #include "blitz_app.h" 
+#include "blitz_enum.h"
 
 
 #ifdef __cplusplus
 #ifdef __cplusplus
 extern "C"{
 extern "C"{

+ 1 - 0
blitz.mod/blitz_array.c

@@ -92,6 +92,7 @@ static int arrayCellSize(const char * type, unsigned short data_size, int * flag
 		case 'm':size=sizeof(BBDOUBLE128);break;
 		case 'm':size=sizeof(BBDOUBLE128);break;
 		#endif
 		#endif
 		case '@':size=data_size;*flags=0;break; // structs
 		case '@':size=data_size;*flags=0;break; // structs
+		case '/':size=data_size;*flags=0;break; // enums
 	}
 	}
 
 
 	return size;
 	return size;

+ 1 - 0
blitz.mod/blitz_debug.h

@@ -46,6 +46,7 @@ enum{
 	BBDEBUGSCOPE_LOCALBLOCK=3,
 	BBDEBUGSCOPE_LOCALBLOCK=3,
 	BBDEBUGSCOPE_USERINTERFACE=4,
 	BBDEBUGSCOPE_USERINTERFACE=4,
 	BBDEBUGSCOPE_USERSTRUCT=5,
 	BBDEBUGSCOPE_USERSTRUCT=5,
+	BBDEBUGSCOPE_USERENUM=6,
 };
 };
 
 
 struct BBDebugScope{
 struct BBDebugScope{

+ 89 - 0
blitz.mod/blitz_enum.c

@@ -0,0 +1,89 @@
+
+#include "blitz.h"
+
+
+
+BBArray * bbEnumValues(BBEnum * bbEnum) {
+	BBArray * values = &bbEmptyArray;
+
+	int size = 4;
+	char t = bbEnum->type[0];
+	switch( t ) {
+		case 'b':size=1;break;
+		case 's':size=2;break;
+		case 'l':size=8;break;
+		case 'y':size=8;break;
+		case 'z':size=sizeof(BBSIZET);break;
+	}
+
+	values = bbArrayNew1DStruct(bbEnum->atype, bbEnum->length, size);
+
+	char * p = BBARRAYDATA(values, 0);
+
+	memcpy(p, bbEnum->values, size * bbEnum->length);
+	
+	return values;
+}
+
+BBString * bbEnumToString_b(BBEnum * bbEnum, BBBYTE ordinal) {
+	BBBYTE * value = (BBBYTE*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_s(BBEnum * bbEnum, BBSHORT ordinal) {
+	BBSHORT * value = (BBSHORT*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_i(BBEnum * bbEnum, BBINT ordinal) {
+	BBINT * value = (BBINT*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_u(BBEnum * bbEnum, BBUINT ordinal) {
+	BBUINT * value = (BBUINT*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_l(BBEnum * bbEnum, BBLONG ordinal) {
+	BBLONG * value = (BBLONG*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_y(BBEnum * bbEnum, BBULONG ordinal) {
+	BBULONG * value = (BBULONG*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}
+
+BBString * bbEnumToString_t(BBEnum * bbEnum, BBSIZET ordinal) {
+	BBSIZET * value = (BBSIZET*)bbEnum->values;
+	for (int i = 0; i < bbEnum->length; i++) {
+		if (*value++ == ordinal)
+			return bbEnum->names[i];
+	}
+	return &bbEmptyString;
+}

+ 34 - 0
blitz.mod/blitz_enum.h

@@ -0,0 +1,34 @@
+
+#ifndef BLITZ_ENUM_H
+#define BLITZ_ENUM_H
+
+#include "blitz_types.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+struct BBEnum {
+	const char * name;
+	char * type; // the numeric type
+	char * atype; // array type
+	int flags; // 1 if a flags enum
+	int length; // number of values
+	void * values;
+	BBString * names[1]; // array of names
+};
+
+BBArray * bbEnumValues(BBEnum * bbEnum);
+BBString * bbEnumToString_b(BBEnum * bbEnum, BBBYTE ordinal);
+BBString * bbEnumToString_s(BBEnum * bbEnum, BBSHORT ordinal);
+BBString * bbEnumToString_i(BBEnum * bbEnum, BBINT ordinal);
+BBString * bbEnumToString_u(BBEnum * bbEnum, BBUINT ordinal);
+BBString * bbEnumToString_l(BBEnum * bbEnum, BBLONG ordinal);
+BBString * bbEnumToString_y(BBEnum * bbEnum, BBULONG ordinal);
+BBString * bbEnumToString_t(BBEnum * bbEnum, BBSIZET ordinal);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 38 - 0
blitz.mod/blitz_object.c

@@ -233,3 +233,41 @@ BBObject * bbNullObjectTest( BBObject *o ) {
 	return o;
 	return o;
 }
 }
 
 
+static struct avl_root *enum_root = 0;
+
+int enum_node_compare(const void *x, const void *y) {
+
+        struct enum_node * node_x = (struct enum_node *)x;
+        struct enum_node * node_y = (struct enum_node *)y;
+
+        return strcmp(node_x->scope->name, node_y->scope->name);
+}
+
+void bbObjectRegisterEnum( BBDebugScope *p ) {
+	struct enum_node * node = (struct enum_node *)malloc(sizeof(struct enum_node));
+	node->scope = p;
+	
+	struct enum_node * old_node = (struct enum_node *)avl_map(&node->link, enum_node_compare, &enum_root);
+	if (&node->link != &old_node->link) {
+		// this object already exists here...
+		// delete the new node, since we don't need it
+		// note : should never happen as structs should only ever be registered once.
+		free(node);
+	}
+}
+
+BBDebugScope * bbObjectEnumInfo( char * name ) {
+	// create something to look up
+	struct enum_node node;
+	BBDebugScope scope;
+	scope.name = name;
+	node.scope = &scope;
+	
+	struct enum_node * found = (struct enum_node *)tree_search(&node, enum_node_compare, enum_root);
+
+	if (found) {
+		return found->scope;
+	}
+	
+	return 0;
+}

+ 8 - 0
blitz.mod/blitz_object.h

@@ -95,6 +95,14 @@ BBDebugScope * bbObjectStructInfo( char * name );
 
 
 BBObject * bbNullObjectTest( BBObject *o );
 BBObject * bbNullObjectTest( BBObject *o );
 
 
+struct enum_node {
+	struct avl_root link;
+	BBDebugScope * scope;
+};
+
+void bbObjectRegisterEnum( BBDebugScope *p );
+BBDebugScope * bbObjectEnumInfo( char * name );
+
 #ifdef __cplusplus
 #ifdef __cplusplus
 }
 }
 #endif
 #endif

+ 2 - 0
blitz.mod/blitz_types.h

@@ -23,6 +23,7 @@ typedef struct BBArray	BBArray;
 typedef struct BBInterfaceTable BBInterfaceTable;
 typedef struct BBInterfaceTable BBInterfaceTable;
 typedef struct BBInterface BBInterface;
 typedef struct BBInterface BBInterface;
 typedef struct BBInterfaceOffsets BBInterfaceOffsets;
 typedef struct BBInterfaceOffsets BBInterfaceOffsets;
+typedef struct BBEnum BBEnum;
 
 
 typedef unsigned char	BBBYTE;
 typedef unsigned char	BBBYTE;
 typedef unsigned short	BBSHORT;
 typedef unsigned short	BBSHORT;
@@ -62,6 +63,7 @@ extern const char *bbDoubleTypeTag;	//"d"
 extern const char *bbStringTypeTag;	//"$"
 extern const char *bbStringTypeTag;	//"$"
 extern const char *bbObjectTypeTag;	//":Object"
 extern const char *bbObjectTypeTag;	//":Object"
 extern const char *bbBytePtrTypeTag;//"*b"
 extern const char *bbBytePtrTypeTag;//"*b"
+extern const char *bbEnumTypeTag;   //"/"
 
 
 struct bbDataDef { 
 struct bbDataDef { 
 	char * type; 
 	char * type;