diff --git a/Makefile b/Makefile index 351f81b..51ec92e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PREFIX=/usr/local VERSION=0.12.1 -CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC \ +CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC -I. \ -fsanitize=signed-integer-overflow -fno-sanitize-recover -fvisibility=hidden -fdollars-in-identifiers LTO=-flto=auto -fno-fat-lto-objects -Wl,-flto LDFLAGS=-Wl,-rpath '-Wl,$$ORIGIN' @@ -41,7 +41,7 @@ tags: ctags *.[ch] **/*.[ch] test: tomo - for f in test/*; do echo -e "\x1b[1;4m$$f\x1b[m"; VERBOSE=0 CC=tcc ./tomo "$$f" || break; done + for f in test/*.tm; do echo -e "\x1b[1;4m$$f\x1b[m"; VERBOSE=0 CC=tcc ./tomo "$$f" || break; done clean: rm -f tomo *.o SipHash/halfsiphash.o builtins/*.o libtomo.so diff --git a/builtins/table.h b/builtins/table.h index 436eaf4..8060b11 100644 --- a/builtins/table.h +++ b/builtins/table.h @@ -7,9 +7,8 @@ #include "datatypes.h" #include "array.h" -#define $Table(key_t, val_t, key_info, value_info, fb, def, ...) ({ \ - struct $entry_s { key_t k; val_t v; }; \ - struct $entry_s $ents[] = {__VA_ARGS__}; \ +#define $Table(key_t, val_t, key_info, value_info, fb, def, N, ...) ({ \ + struct { key_t k; val_t v; } $ents[N] = {__VA_ARGS__}; \ table_t $table = Table_from_entries((array_t){ \ .data=memcpy(GC_MALLOC(sizeof($ents)), $ents, sizeof($ents)), \ .length=sizeof($ents)/sizeof($ents[0]), \ diff --git a/compile.c b/compile.c index caca9aa..9b615bc 100644 --- a/compile.c +++ b/compile.c @@ -565,9 +565,14 @@ CORD compile(env_t *env, ast_t *ast) else code = CORD_all(code, ", /*default:*/ NULL"); + size_t n = 0; + for (ast_list_t *entry = table->entries; entry; entry = entry->next) + ++n; + CORD_appendf(&code, ", %zu", n); + for (ast_list_t *entry = table->entries; entry; entry = entry->next) { auto e = Match(entry->ast, TableEntry); - code = CORD_all(code, ",\n\t(struct $entry_s){", compile(env, e->key), ", ", compile(env, e->value), "}"); + code = CORD_all(code, ",\n\t{", compile(env, e->key), ", ", compile(env, e->value), "}"); } return CORD_cat(code, ")"); @@ -1074,7 +1079,7 @@ CORD compile_type_info(env_t *env, type_t *t) module_code_t compile_file(ast_t *ast) { env_t *env = new_compilation_unit(); - CORD_appendf(&env->code->imports, "#include \"tomo.h\"\n"); + CORD_appendf(&env->code->imports, "#include \n"); for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { bind_statement(env, stmt->ast); diff --git a/tomo.c b/tomo.c index 81eb908..13ac7b7 100644 --- a/tomo.c +++ b/tomo.c @@ -73,9 +73,13 @@ int main(int argc, char *argv[]) pclose(out); } + const char *cconfig = getenv("CCONFIG"); + if (!cconfig) + cconfig = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE"; + const char *cflags = getenv("CFLAGS"); if (!cflags) - cflags = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE"; + cflags = heap_strf("%s -I. -D_DEFAULT_SOURCE", cconfig); const char *ldlibs = "-lgc -lcord -lm -L. -ltomo"; if (getenv("LDLIBS")) @@ -124,7 +128,7 @@ int main(int argc, char *argv[]) return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; prog = popen(heap_strf("%s > %s.c", autofmt, f->filename), "w"); - CORD_put(CORD_all("#include \"", f->filename, ".h\"\n\n", module.c_file), prog); + CORD_put(CORD_all("#include \"", module.module_name, ".tm.h\"\n\n", module.c_file), prog); status = pclose(prog); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) printf("Transpiled to %s.c\n", f->filename); @@ -141,7 +145,7 @@ int main(int argc, char *argv[]) return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; prog = popen(heap_strf("%s -x c %s -E - | %s > %s.c", cc, cflags, autofmt, f->filename), "w"); - CORD_put(CORD_all("#include \"", f->filename, ".h\"\n\n", module.c_file), prog); + CORD_put(CORD_all("#include \"", module.module_name, ".tm.h\"\n\n", module.c_file), prog); status = pclose(prog); if (WIFEXITED(status) && WEXITSTATUS(status) == 0) printf("Transpiled to %s.c\n", f->filename);