Browse Source

global structs

Kajetan Johannes Hammerle 3 years ago
parent
commit
a2d1d66512
7 changed files with 51 additions and 13 deletions
  1. 32 1
      DataType.c
  2. 5 0
      DataType.h
  3. 2 0
      Main.c
  4. 7 9
      Test.c
  5. 0 3
      libraries/Time.c
  6. 4 0
      utils/Functions.c
  7. 1 0
      utils/Functions.h

+ 32 - 1
DataType.c

@@ -10,6 +10,9 @@ static int typeNameIndex = 0;
 static int typeNameSwap = 0;
 static char typeName[2][ARRAY_NAME];
 
+static bool useGlobals = false;
+static Structs globalStructs;
+
 static void dtAppend(const char* s) {
     int index = 0;
     while(typeNameIndex < (ARRAY_NAME - 1) && s[index] != '\0') {
@@ -211,7 +214,7 @@ void stsDelete(Structs* sts) {
     free(sts->data);
 }
 
-Struct* stsSearch(Structs* sts, const char* name) {
+static Struct* stsInternSearch(Structs* sts, const char* name) {
     for(int i = 0; i < sts->entries; i++) {
         if(strcmp(sts->data[i].name, name) == 0) {
             return sts->data + i;
@@ -220,6 +223,16 @@ Struct* stsSearch(Structs* sts, const char* name) {
     return NULL;
 }
 
+Struct* stsSearch(Structs* sts, const char* name) {
+    if(useGlobals) {
+        Struct* st = stsInternSearch(&globalStructs, name);
+        if(st != NULL) {
+            return st;
+        }
+    }
+    return stsInternSearch(sts, name);
+}
+
 Struct* stsAdd(Structs* sts, const char* name) {
     if(sts->entries >= sts->capacity) {
         sts->capacity *= 2;
@@ -231,4 +244,22 @@ Struct* stsAdd(Structs* sts, const char* name) {
     sts->data[index].name = name;
     sts->data[index].vars = NULL;
     return sts->data + index;
+}
+
+void gstsInit() {
+    stsInit(&globalStructs);
+    useGlobals = true;
+}
+
+void gstsDelete() {
+    stsDelete(&globalStructs);
+    useGlobals = false;
+}
+
+Structs* gstsGet() {
+    return &globalStructs;
+}
+
+Struct* gstsAdd(const char* name) {
+    return stsAdd(&globalStructs, name);
 }

+ 5 - 0
DataType.h

@@ -81,4 +81,9 @@ void stsDelete(Structs* sts);
 Struct* stsSearch(Structs* sts, const char* name);
 Struct* stsAdd(Structs* sts, const char* name);
 
+void gstsInit();
+void gstsDelete();
+Structs* gstsGet();
+Struct* gstsAdd(const char* name);
+
 #endif

+ 2 - 0
Main.c

@@ -41,8 +41,10 @@ static void start(int argAmount, const char** args) {
 
 int main(int argAmount, const char** args) {
     gfsInit();
+    gstsInit();
     lTimeRegister();
     start(argAmount, args);
+    gstsDelete();
     gfsDelete();
     return 0;
 }

+ 7 - 9
Test.c

@@ -180,21 +180,19 @@ static void tsScanDirectory() {
     }
 }
 
-static void tsAddPrinter(Structs* sts, DataType in, ScriptFunction sf) {
+static void tsAddPrinter(DataType in, ScriptFunction sf) {
     Function f;
     gfInit(&f, "test", dtVoid(), sf);
-    fAddArgument(&f, in, sts);
+    gfAddArgument(&f, in);
     gfsAdd(&f);
 }
 
 void tsStart(const char* path) {
-    Structs sts;
-    stsInit(&sts);
-    tsAddPrinter(&sts, dtConst(dtInt32()), tsInt32Printer);
-    tsAddPrinter(&sts, dtConst(dtInt64()), tsInt64Printer);
-    tsAddPrinter(&sts, dtConst(dtFloat()), tsFloatPrinter);
-    tsAddPrinter(&sts, dtConst(dtBool()), tsBoolPrinter);
-    tsAddPrinter(&sts, dtConst(dtText()), tsTextPrinter);
+    tsAddPrinter(dtConst(dtInt32()), tsInt32Printer);
+    tsAddPrinter(dtConst(dtInt64()), tsInt64Printer);
+    tsAddPrinter(dtConst(dtFloat()), tsFloatPrinter);
+    tsAddPrinter(dtConst(dtBool()), tsBoolPrinter);
+    tsAddPrinter(dtConst(dtText()), tsTextPrinter);
 
     doneTests = 0;
     allTests = 0;

+ 0 - 3
libraries/Time.c

@@ -24,9 +24,6 @@ static void lTimeGetNanos(Script* sc) {
 }
 
 void lTimeRegister() {
-    Structs sts;
-    stsInit(&sts);
-
     Function f;
     gfInit(&f, "getMillis", dtInt64(), lTimeGetMillis);
     gfsAdd(&f);

+ 4 - 0
utils/Functions.c

@@ -103,6 +103,10 @@ void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf) {
     f->scriptFunction = sf;
 }
 
+bool gfAddArgument(Function* f, DataType type) {
+    return fAddArgument(f, type, gstsGet());
+}
+
 void gfsInit() {
     fsInit(&globalFunctions);
     useGlobals = true;

+ 1 - 0
utils/Functions.h

@@ -37,6 +37,7 @@ Function* fsSearch(Functions* fs, Function* f, bool constMatch);
 void fsAdd(Functions* fs, Function* f);
 
 void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf);
+bool gfAddArgument(Function* f, DataType type);
 void gfsInit();
 bool gfsAdd(Function* f);
 bool gfsCall(Script* sc, int id);