]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
type parsing for constants, globals and locals. Sanatize constants to select interna...
authorDale Weiler <killfieldengine@gmail.com>
Sat, 28 Apr 2012 07:53:23 +0000 (03:53 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Sat, 28 Apr 2012 07:53:23 +0000 (03:53 -0400)
asm.c
data/test.qs
gmqcc.h
util.c

diff --git a/asm.c b/asm.c
index 99bb406128a91e00833227be1259398210bcc4e4..b1f48802658c14e319760d47221aef36c5764f0a 100644 (file)
--- a/asm.c
+++ b/asm.c
@@ -78,11 +78,34 @@ void asm_clear() {
  * are locals.
  */
 static inline bool asm_parse_type(const char *skip, size_t line, asm_state *state) {
-    if (strstr(skip, "FLOAT:")  == &skip[0]) { return true; }
-    if (strstr(skip, "VECTOR:") == &skip[0]) { return true; }
-    if (strstr(skip, "ENTITY:") == &skip[0]) { return true; }
-    if (strstr(skip, "FIELD:")  == &skip[0]) { return true; }
-    if (strstr(skip, "STRING:") == &skip[0]) { return true; }
+    if (!(strstr(skip, "FLOAT:")  == &skip[0]) &&
+         (strstr(skip, "VECTOR:") == &skip[0]) &&
+         (strstr(skip, "ENTITY:") == &skip[0]) &&
+         (strstr(skip, "FIELD:")  == &skip[0]) &&
+         (strstr(skip, "STRING:") == &skip[0])) return false;
+
+    /* TODO: determine if constant, global, or local */
+    switch (*skip) {
+        /* VECTOR */ case 'V': {
+            const char *find = skip + 7;
+            while (*find == ' ' || *find == '\t') find++;
+            printf("found VECTOR %s\n", find);
+            break;
+        }
+        /* ENTITY */ case 'E': {
+            const char *find = skip + 7;
+            while (*find == ' ' || *find == '\t') find++;
+            printf("found ENTITY %s\n", find);
+            break;
+        }
+        /* STRING */ case 'S': {
+            const char *find = skip + 7;
+            while (*find == ' ' || *find == '\t') find++;
+            printf("found STRING %s\n", find);
+            break;
+        }
+    }
+    
     return false;
 }
 
@@ -165,10 +188,19 @@ static inline bool asm_parse_func(const char *skip, size_t line, asm_state *stat
             code_chars_put(name, strlen(name));
             code_chars_add('\0');
 
-            /* TODO: sanatize `find` to ensure all numerical digits */
-            
-            printf("found internal function %s, -%d\n", name, atoi(find));
+            /*
+             * Sanatize the numerical constant used to select the
+             * internal function.  Must ensure it's all numeric, since
+             * atoi can silently drop characters from a string and still
+             * produce a valid constant that would lead to runtime problems.
+             */
+            if (util_strdigit(find))
+                printf("found internal function %s, -%d\n", name, atoi(find));
+            else
+                printf("invalid internal function identifier, must be all numeric\n");
+                
         } else {
+            /* TODO: function bodies */
         }
 
         mem_d(copy);
index b26b4efb9d10036173548146395b1240c4ef4220..14d3e26247731443229af5d7b30f0fe708fecf2e 100644 (file)
@@ -82,4 +82,7 @@ FUNCTION: pow,            $97
 FUNCTION: findfloat,      $98
 FUNCTION: checkextension, $99
 
-; todo support other crap
+; constants test
+VECTOR: 1, 2, 3
+FLOAT:  1
+STRING: "hello world"
diff --git a/gmqcc.h b/gmqcc.h
index 6e66325e00469c61e9113713b7c37096a6fb2e40..eca1e156f05b3edd615dcd47fd79f9a02098e4bd 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -195,6 +195,7 @@ void  util_memory_d      (void       *, unsigned int, const char *);
 void  util_meminfo       ();
 
 bool  util_strupper      (const char *);
+bool  util_strdigit      (const char *);
 char *util_strdup        (const char *);
 char *util_strrq         (char *);
 char *util_strrnl        (char *);
diff --git a/util.c b/util.c
index daf4b87a99a7b204934fa5b6818b844d3cf07009..942f6617dd35a0964352504d2d2429dbb49ae3ca 100644 (file)
--- a/util.c
+++ b/util.c
@@ -179,6 +179,19 @@ bool util_strupper(const char *str) {
     return true;
 }
 
+/*
+ * Returns true if string is all digits, otherwise
+ * it returns false.
+ */
+bool util_strdigit(const char *str) {
+    while (*str) {
+        if(!isdigit(*str))
+            return false;
+        str++;
+    }
+    return true;
+}
+
 void util_debug(const char *area, const char *ms, ...) {
     if (!opts_debug)
         return;