Przeglądaj źródła

ncd: NCDValue: add variable values for future internal use in the AST

ambrop7 13 lat temu
rodzic
commit
54a1bb3eaf
2 zmienionych plików z 75 dodań i 0 usunięć
  1. 43 0
      ncd/NCDValue.c
  2. 32 0
      ncd/NCDValue.h

+ 43 - 0
ncd/NCDValue.c

@@ -51,6 +51,7 @@ static void value_assert (NCDValue *o)
         case NCDVALUE_STRING:
         case NCDVALUE_LIST:
         case NCDVALUE_MAP:
+        case NCDVALUE_VAR:
             return;
         default:
             ASSERT(0);
@@ -124,6 +125,10 @@ int NCDValue_InitCopy (NCDValue *o, NCDValue *v)
             return 0;
         } break;
         
+        case NCDVALUE_VAR: {
+            return NCDValue_InitVar(o, v->var_name);
+        } break;
+        
         default:
             ASSERT(0);
     }
@@ -161,6 +166,10 @@ void NCDValue_Free (NCDValue *o)
             }
         } break;
         
+        case NCDVALUE_VAR: {
+            free(o->var_name);
+        } break;
+        
         default:
             ASSERT(0);
     }
@@ -640,6 +649,34 @@ NCDValue * NCDValue_MapFindValueByString (NCDValue *o, const char *key_str)
     return NCDValue_MapKeyValue(o, ekey);
 }
 
+int NCDValue_IsVar (NCDValue *o)
+{
+    value_assert(o);
+    
+    return o->type == NCDVALUE_VAR;
+}
+
+int NCDValue_InitVar (NCDValue *o, const char *var_name)
+{
+    ASSERT(var_name)
+    
+    if (!(o->var_name = strdup(var_name))) {
+        return 0;
+    }
+    
+    o->type = NCDVALUE_VAR;
+    
+    return 1;
+}
+
+const char * NCDValue_VarName (NCDValue *o)
+{
+    value_assert(o);
+    ASSERT(o->type == NCDVALUE_VAR)
+    
+    return o->var_name;
+}
+
 int NCDValue_Compare (NCDValue *o, NCDValue *v)
 {
     value_assert(o);
@@ -721,6 +758,12 @@ int NCDValue_Compare (NCDValue *o, NCDValue *v)
         }
     }
     
+    if (o->type == NCDVALUE_VAR) {
+        int res = strcmp(o->var_name, v->var_name);
+        
+        return (res > 0) - (res < 0);
+    }
+    
     ASSERT(0)
     return 0;
 }

+ 32 - 0
ncd/NCDValue.h

@@ -40,6 +40,7 @@
 #define NCDVALUE_STRING 1
 #define NCDVALUE_LIST 2
 #define NCDVALUE_MAP 3
+#define NCDVALUE_VAR 4
 
 /**
  * Holds an NCD "value", which is used in the NCD programming when passing arguments to
@@ -72,6 +73,9 @@ typedef struct {
             BAVL map_tree;
             size_t map_count;
         };
+        struct {
+            char *var_name;
+        };
     };
 } NCDValue;
 
@@ -430,6 +434,34 @@ void NCDValue_MapRemove (NCDValue *o, NCDValue *ekey, NCDValue *out_key, NCDValu
  */
 NCDValue * NCDValue_MapFindValueByString (NCDValue *o, const char *key_str);
 
+/**
+ * Checks if the value is a variable value.
+ * 
+ * @param o the value
+ * @return 1 if variable, 0 if not
+ */
+int NCDValue_IsVar (NCDValue *o);
+
+/**
+ * Initializes a variable value.
+ * WARNING: variable values are only used internally by NCD as part of
+ * the AST, and must never be used as statement or template arguments
+ * during program execution.
+ * 
+ * @param o value structure to initialize
+ * @param var_name name of the variable
+ * @return 1 on success, 0 on failure
+ */
+int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
+
+/**
+ * Returns the name of the variable.
+ * 
+ * @param o variable value
+ * @return variable name
+ */
+const char * NCDValue_VarName (NCDValue *o);
+
 /**
  * Compares a value with another value.
  * This function defines a total order on the set of all possible values.