123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #include <stdlib.h>
- #include <string.h>
- #include "utils/Functions.h"
- static bool useGlobals = false;
- static Functions globalFunctions;
- void fInit(Function* f, const char* name, int line) {
- f->name = name;
- f->arguments = 0;
- f->returnType = dtVoid();
- f->address = -1;
- f->size = 0;
- f->line = line;
- f->global = false;
- f->scriptFunction = NULL;
- }
- bool fAddArgument(Function* f, DataType type, Structs* sts) {
- if(f->arguments >= FUNCTION_ARGUMENTS) {
- return true;
- }
- f->size += dtGetSize(type, sts);
- f->argumentTypes[f->arguments++] = type;
- return false;
- }
- static bool fConstCompare(Function* a, Function* b) {
- if(a->arguments != b->arguments) {
- return false;
- }
- for(int i = 0; i < a->arguments; i++) {
- if(!dtNullCompare(a->argumentTypes[i], b->argumentTypes[i])) {
- return false;
- }
- }
- return strcmp(a->name, b->name) == 0;
- }
- static bool fCompare(Function* a, Function* b) {
- if(a->arguments != b->arguments || strcmp(a->name, b->name) != 0) {
- return false;
- }
- for(int i = 0; i < a->arguments; i++) {
- DataType dt = b->argumentTypes[i];
- if(!dtNullCompare(a->argumentTypes[i], dt) &&
- !dtNullCompare(a->argumentTypes[i], dtConst(dt))) {
- return false;
- }
- }
- return strcmp(a->name, b->name) == 0;
- }
- void fsInit(Functions* fs) {
- fs->capacity = 16;
- fs->entries = 0;
- fs->data = malloc(sizeof(Function) * fs->capacity);
- }
- void fsDelete(Functions* fs) {
- free(fs->data);
- }
- static Function* fsInternSearch(Functions* fs, Function* f, bool constMatch) {
- for(int i = 0; i < fs->entries; i++) {
- if(fConstCompare(fs->data + i, f)) {
- return fs->data + i;
- }
- }
- if(constMatch) {
- return NULL;
- }
- for(int i = 0; i < fs->entries; i++) {
- if(fCompare(fs->data + i, f)) {
- return fs->data + i;
- }
- }
- return NULL;
- }
- Function* fsSearch(Functions* fs, Function* f, bool constMatch) {
- if(useGlobals) {
- Function* gf = fsInternSearch(&globalFunctions, f, constMatch);
- if(gf != NULL) {
- return gf;
- }
- }
- return fsInternSearch(fs, f, constMatch);
- }
- void fsAdd(Functions* fs, Function* f) {
- if(fs->entries >= fs->capacity) {
- fs->capacity *= 2;
- fs->data = realloc(fs->data, sizeof(Function) * fs->capacity);
- }
- fs->data[fs->entries++] = *f;
- }
- void gfInit(Function* f, const char* name, DataType r, ScriptFunction sf) {
- fInit(f, name, -1);
- f->returnType = r;
- f->scriptFunction = sf;
- }
- bool gfAddArgument(Function* f, DataType type) {
- return fAddArgument(f, type, gstsGet());
- }
- void gfsInit() {
- fsInit(&globalFunctions);
- useGlobals = true;
- }
- bool gfsAdd(Function* f) {
- if(fsSearch(&globalFunctions, f, true) != NULL) {
- return true;
- }
- int index = globalFunctions.entries;
- fsAdd(&globalFunctions, f);
- globalFunctions.data[index].line = index;
- globalFunctions.data[index].global = true;
- return false;
- }
- bool gfsCall(Script* sc, int id) {
- if(!useGlobals || id < 0 || id >= globalFunctions.entries) {
- return true;
- }
- globalFunctions.data[id].scriptFunction(sc);
- return false;
- }
- void gfsDelete() {
- fsDelete(&globalFunctions);
- useGlobals = false;
- }
|