]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - asm.c
ast and ir testers - to use: compile into gmqcc and execut the functions in main()
[xonotic/gmqcc.git] / asm.c
diff --git a/asm.c b/asm.c
index 0e1828059d2b61dfb626cf2293963864dbe2dca9..a685d1c841cdadb55c8fe2d495ddd8d5237a8271 100644 (file)
--- a/asm.c
+++ b/asm.c
@@ -43,7 +43,7 @@ VECTOR_MAKE(globals, assembly_constants);
  */
 static char *const asm_getline(size_t *byte, FILE *fp) {
     char   *line = NULL;
-    ssize_t read = util_getline(&line, byte, fp);
+    size_t  read = util_getline(&line, byte, fp);
     *byte = read;
     if (read == -1) {
         mem_d (line);
@@ -78,11 +78,61 @@ 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': {
+            float val1;
+            float val2;
+            float val3;
+            
+            const char *find = skip + 7;
+            while (*find == ' ' || *find == '\t') find++;
+
+            /*
+             * Parse all three elements of the vector.  This will only
+             * pass the first try if we hit a constant, otherwise it's
+             * a global.
+             */
+            #define PARSE_ELEMENT(X,Y,Z)                    \
+                if (isdigit(*X)  || *X == '-'||*X == '+') { \
+                    bool negated = (*X == '-');             \
+                    if  (negated || *X == '+')   { X++; }   \
+                    Y = (negated)?-atof(X):atof(X);         \
+                    X = strchr(X, ',');                     \
+                    Z                                       \
+                }
+
+            PARSE_ELEMENT(find, val1, { if(find) { find +=3; }});
+            PARSE_ELEMENT(find, val2, { if(find) { find +=2; }});
+            PARSE_ELEMENT(find, val3, { if(find) { find +=1; }});
+            #undef PARSE_ELEMENT
+
+            printf("X:[0] = %f\n", val1);
+            printf("Y:[1] = %f\n", val2);
+            printf("Z:[2] = %f\n", val3);
+            
+            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;
 }
 
@@ -138,22 +188,44 @@ static inline bool asm_parse_func(const char *skip, size_t line, asm_state *stat
                 mem_d(name);
                 return false;
             }
-            /* reassign name */
-            mem_d(name);
-            name = util_strchp(name, strchr(name, ','));
-
-            /* add internal function */
+            *strchr(name, ',')='\0';
+            
+            /*
+             * Now add the following items to the code system:
+             *  function
+             *  definition (optional)
+             *  global     (optional)
+             *  name
+             */
             code_functions_add((prog_section_function){
                 -atoi(find), /* needs to be negated */
                  0, 0, 0,
                 .name = code_chars_elements,
                  0, 0,{0}
             });
-            /* add name to string table */
+            code_defs_add((prog_section_def){
+                .type   = TYPE_FUNCTION,
+                .offset = code_globals_elements,
+                .name   = code_chars_elements
+            });
+            code_globals_add(code_chars_elements);
+            
             code_chars_put(name, strlen(name));
             code_chars_add('\0');
-            
-            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);