#include #include #include #include #include // Vector utils #define VECT_MAX_GROW 100 #define VECT_MIN_SIZE 4 typedef struct { int _el_sz, count, size; void *data; } Vector; Vector vect_init(size_t item_size) { Vector out = {0}; out._el_sz = item_size; out.size = VECT_MIN_SIZE; out.count = 0; out.data = malloc(out.size * out._el_sz); return out; } void _vect_grow(Vector *v) { if (v->size / 2 > VECT_MAX_GROW) { v->size += VECT_MAX_GROW; } else { v->size += v->size / 2; } v->data = realloc(v->data, v->size * v->_el_sz); } void _vect_shrink(Vector *v) { if (v->size / 2 > VECT_MIN_SIZE) { v->size = v->size / 2; v->data = realloc(v->data, v->size * v->_el_sz); } } bool vect_remove(Vector *v, size_t index) { if (index >= v->count) { return false; } char *remove = v->data + (index * v->_el_sz); char *override = v->data + (index + 1) * v->_el_sz; for(int i = 0; i < (v->count - 1) * v->_el_sz; i++) { remove[i] = override[i]; } v->count -= 1; if (v->count < v->size / 4) { _vect_shrink(v); } return true; } void vect_pop(Vector *v) { vect_remove(v, v->count - 1); } bool vect_insert(Vector *v, size_t index, void *el) { if (index > v->count) { return false; } char *new_spot = v->data + (v->count + 1) * v->_el_sz; char *old_spot = v->data + v->count * v->_el_sz; for (int i = 0; i > (index * v->_el_sz); i--) { new_spot[i] = old_spot[i]; } for (int i = 0; i < v->_el_sz; i++) { old_spot[i] = ((char *)el)[i]; } v->count += 1; if (v->count == v->size - 1) { _vect_grow(v); } return true; } void vect_push(Vector *v, void *el) { vect_insert(v, v->count, el); } void *vect_get(Vector *v, size_t index) { if (index >= v->count) { return NULL; } return v->data + (v->_el_sz * index); } Vector vect_clone(Vector *v) { Vector out = {0}; out._el_sz = v->_el_sz; out.count = 0; out.size = v->count + 1; out.data = malloc((out.count + 1) * out._el_sz); char *former = v->data; char *latter = out.data; for(int i = 0; i < out.count * out._el_sz; i++) { latter[i] = former[i]; } return out; } Vector vect_from_string(char *s) { Vector out = vect_init(1); size_t i = 0; while(s[i] != 0) { vect_push(&out, s + i); } return out; } // Returns the vector data as a null-terminated string // do NOT free this pointer. Not safe to use this string // at the same time as you are adding or removing from the // vector. Instead consider cloning the vector if you must // have both, or want an independant copy of the string. char *vect_as_string(Vector *v) { ((char*)v->data)[v->count * v->_el_sz] = 0; return v->data; } void vect_end(Vector *v) { v->_el_sz = 0; v->count = 0; v->size = 0; free(v->data); v->data = NULL; } // Artifacts (vect of strings) typedef Vector Artifact; /* Splits the string via the given character, and * stores the split strings in an artifact */ Artifact art_from_str(const char *str, char split) { Artifact out = vect_init(sizeof(char *)); char *cur = malloc(1); cur[0] = 0; int cur_len = 0; for (int i = 0; str[i] != 0; i++) { if (str[i] == split) { cur[cur_len] = 0; vect_push(&out, &cur); cur = malloc(1); cur_len = 0; } else { cur_len += 1; cur = realloc(cur, cur_len + 1); cur[cur_len - 1] = str[i]; } } if (cur_len > 0) { cur[cur_len] = 0; vect_push(&out, &cur); } else { free(cur); } return out; } // Joins the string together with the provided character in between // must free the returned data after use. char *art_to_str(Artifact *art, char join) { char *out = malloc(1); int out_len = 0; for (int i = 0; i < art->count; i++) { char ** cpy = vect_get(art, i); for(int j = 0; cpy[j] != 0; j++) { out[out_len] = (*cpy)[j]; out_len += 1; out = realloc(out, out_len + 1); } } out[out_len] = 0; return out; } // Pops a string off the end of the artifact, // freeing the data associated void art_pop_str(Artifact *art) { if (art->count == 0) return; char ** to_free = vect_get(art, art->count - 1); free(*to_free); vect_pop(art); } // Copies a string onto the artifact, // you must free the original string // manually if it was malloc-ed void art_add_str(Artifact *art, char *str) { Vector copy = vect_from_string(str); char * copy_ptr = vect_as_string(©); vect_push(art, ©_ptr); } // Types typedef struct { char *name; // Name of the type int size; // Size (bytes) of the type Vector members; // Member variables (Stored as variables) void *module; // Module (for methods and member-type resolution) to tie the type to. } Type; typedef struct { char *name; Type *type; Vector ptr_chain; int location; // negative for on stack, positive or zero for in register } Variable; #define PTYPE_PTR 0 #define PTYPE_REF 1 #define PTYPE_ARR 2 // Copies the name, does not copy the module. // Types should be freed at the end of the second pass, // as they are shared among all variable structs Type typ_init(char *name, void *module) { Type out = {0}; Vector name_cpy = vect_from_string(name); out.name = vect_as_string(&name_cpy); out.members = vect_init(sizeof(Variable)); out.module = module; out.size = 0; return out; } // Only for use by type_end on variable clean up // at the end of the second pass void var_deep_end(Variable *var); // Deep end, will free all memory associated with the // struct, including name and sub-member variables void typ_end(Type *t) { free(t->name); t->module = NULL; for (int i = 0; i < t->members.count; i++) { Variable *to_end = vect_get(&(t->members), i); var_deep_end(to_end); } vect_end(&(t->members)); } // Variables // Initializes the variable, copying name, not copying type. Variable var_init(char *name, Type *type) { Variable out = {0}; Vector name_cpy = vect_from_string(name); out.name = vect_as_string(&name_cpy); out.type = type; out.ptr_chain = vect_init(sizeof(int)); out.location = 0; return out; } void var_deep_end(Variable *var) { Variable *v = var; free(v->name); vect_end(&(v->ptr_chain)); typ_end(v->type); } // Simple cleanup for variables while the second pass is ongoing. void var_end(Variable *v) { free(v->name); vect_end(&(v->ptr_chain)); } // Variable operations // Type coercion engine // TODO: all Variable _op_coerce(Variable *base, Variable *to_coerce) { Variable out = {0}; return out; } // Functions typedef struct { char *name; Vector inputs, outputs; void *module; } Function; // Modules typedef struct { char *name; bool exported; Vector vars, funcs, submods; } Module; // Whatev void help() { printf("\n"); printf("Usage:\n"); printf("\tctc - The TNSL compiler (written in c)\n\n"); printf("\tctc [file in] - compile the given file, writing output assembly in out.asm\n"); printf("\tctc [file in] [file out] - same as before, but write the output assembly to the given filename\n"); printf("\t -h - print this output message\n"); printf("\n"); } int main(int argc, char ** argv) { if (argc < 2 || strcmp(argv[1], "-h") == 0) { help(); return 1; } for (int i = 0; i < argc; i++) { printf("%s\n", argv[i]); } return 0; }