From c2228bf9861b38f2514d0cc8a270754bb3a03dd7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 24 Feb 2024 16:06:49 -0500 Subject: Rename 'nextlang'->'tomo' --- .gitignore | 2 +- Makefile | 14 +++++----- README.md | 10 +++++++ compile.c | 2 +- nextlang.c | 93 -------------------------------------------------------------- nextlang.h | 87 ---------------------------------------------------------- tomo.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tomo.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 199 insertions(+), 189 deletions(-) create mode 100644 README.md delete mode 100644 nextlang.c delete mode 100644 nextlang.h create mode 100644 tomo.c create mode 100644 tomo.h diff --git a/.gitignore b/.gitignore index 53d5ccbc..b4cbd7ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.o *.so -nextlang +tomo tags diff --git a/Makefile b/Makefile index 4325c358..ded163d8 100644 --- a/Makefile +++ b/Makefile @@ -26,12 +26,12 @@ LDLIBS=-lgc -lgccjit -lcord -lm -lunistring BUILTIN_OBJS=builtins/array.o builtins/bool.o builtins/color.o builtins/nums.o builtins/functions.o builtins/integers.o \ builtins/pointer.o builtins/memory.o builtins/string.o builtins/table.o builtins/types.o -all: libnext.so nextlang +all: libtomo.so tomo -nextlang: nextlang.c SipHash/halfsiphash.o util.o files.o ast.o parse.o environment.o types.o typecheck.o structs.o enums.o compile.o $(BUILTIN_OBJS) +tomo: tomo.c SipHash/halfsiphash.o util.o files.o ast.o parse.o environment.o types.o typecheck.o structs.o enums.o compile.o $(BUILTIN_OBJS) -libnext.so: util.o files.o $(BUILTIN_OBJS) SipHash/halfsiphash.o - $(CC) $^ $(CFLAGS) $(EXTRA) $(CWARN) $(G) $(O) $(OSFLAGS) $(LDLIBS) -Wl,-soname,libnext.so -shared -o $@ +libtomo.so: util.o files.o $(BUILTIN_OBJS) SipHash/halfsiphash.o + $(CC) $^ $(CFLAGS) $(EXTRA) $(CWARN) $(G) $(O) $(OSFLAGS) $(LDLIBS) -Wl,-soname,libtomo.so -shared -o $@ SipHash/halfsiphash.c: git submodule update --init --recursive @@ -39,11 +39,11 @@ SipHash/halfsiphash.c: tags: ctags *.[ch] **/*.[ch] -test: nextlang - for f in test/*; do echo -e "\x1b[1;4m$$f\x1b[m"; VERBOSE=0 CC=tcc ./nextlang "$$f" || break; done +test: tomo + for f in test/*; do echo -e "\x1b[1;4m$$f\x1b[m"; VERBOSE=0 CC=tcc ./tomo "$$f" || break; done clean: - rm -f nextlang *.o SipHash/halfsiphash.o builtins/*.o libnext.so + rm -f tomo *.o SipHash/halfsiphash.o builtins/*.o libtomo.so %.1: %.1.md pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@ diff --git a/README.md b/README.md new file mode 100644 index 00000000..113cf4db --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Tomo - Tomorrow's Language + +Tomo is a programming language designed to anticipate and influence the +language design decisions of the future. + +``` +say("Hello world!") +``` + +Check out the [test/](test/) folder to see some examples. diff --git a/compile.c b/compile.c index 67b5aa78..b5d2b03c 100644 --- a/compile.c +++ b/compile.c @@ -950,7 +950,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 \"nextlang.h\"\n"); + CORD_appendf(&env->code->imports, "#include \"tomo.h\"\n"); for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { bind_statement(env, stmt->ast); diff --git a/nextlang.c b/nextlang.c deleted file mode 100644 index 0e52a790..00000000 --- a/nextlang.c +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include -#include -#include -#include - -#include "ast.h" -#include "builtins/string.h" -#include "compile.h" -#include "parse.h" -#include "typecheck.h" -#include "types.h" - -int main(int argc, char *argv[]) -{ - if (argc < 2) return 1; - - // register_printf_modifier(L"p"); - if (register_printf_specifier('T', printf_type, printf_pointer_size)) - errx(1, "Couldn't set printf specifier"); - if (register_printf_specifier('W', printf_ast, printf_pointer_size)) - errx(1, "Couldn't set printf specifier"); - - const char *autofmt = getenv("AUTOFMT"); - if (!autofmt) autofmt = "indent -kr -l100 -nbbo -nut -sob"; - - file_t *f = load_file(argv[1]); - - ast_t *ast = parse_file(f, NULL); - - if (!ast) - errx(1, "Could not compile!"); - - bool verbose = (getenv("VERBOSE") && strcmp(getenv("VERBOSE"), "1") == 0); - if (verbose) { - FILE *out = popen(heap_strf("bat -P --file-name='%s'", argv[1]), "w"); - fputs(f->text, out); - fclose(out); - } - - if (verbose) { - FILE *out = popen("bat -P --file-name=AST", "w"); - fputs(ast_to_str(ast), out); - fclose(out); - } - - module_code_t module = compile_file(ast); - - CORD program = CORD_all( - "// File: ", f->filename, ".h\n", - module.header, - "\n", - "// File: ", f->filename, ".c\n", - module.c_file, - "\n", - "int main(int argc, const char *argv[]) {\n" - "(void)argc;\n" - "(void)argv;\n" - "GC_INIT();\n" - "detect_color();\n" - "$load();\n" - "return 0;\n" - "}\n" - ); - - if (verbose) { - FILE *out = popen(heap_strf("%s | bat -P --file-name=program.c", autofmt), "w"); - CORD_put(program, out); - fclose(out); - } - - const char *cflags = getenv("CFLAGS"); - if (!cflags) - cflags = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover"; - - const char *ldlibs = "-lgc -lcord -lm -L. -lnext"; - if (getenv("LDLIBS")) - ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS")); - - const char *ldflags = "-Wl,-rpath '-Wl,$ORIGIN'"; - - const char *cc = getenv("CC"); - if (!cc) cc = "tcc"; - const char *run = streq(cc, "tcc") ? heap_strf("tcc -run %s %s %s -", cflags, ldflags, ldlibs) - : heap_strf("gcc -x c %s %s %s - -o program && ./program", cflags, ldflags, ldlibs); - FILE *runner = popen(run, "w"); - CORD_put(program, runner); - fclose(runner); - - return 0; -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/nextlang.h b/nextlang.h deleted file mode 100644 index 113e5820..00000000 --- a/nextlang.h +++ /dev/null @@ -1,87 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "SipHash/halfsiphash.h" -#include "builtins/array.h" -#include "builtins/bool.h" -#include "builtins/color.h" -#include "builtins/datatypes.h" -#include "builtins/functions.h" -#include "builtins/integers.h" -#include "builtins/memory.h" -#include "builtins/nums.h" -#include "builtins/pointer.h" -#include "builtins/string.h" -#include "builtins/table.h" -#include "builtins/types.h" - -#define Void_t void - -CORD as_cord(void *x, bool use_color, const char *fmt, ...); - -#define StrF(...) ({ CORD $c; CORD_sprintf(&$c, __VA_ARGS__); $c; }) -#define $var(var, val) __typeof(val) var = val -#define $cord(x) _Generic(x, bool: x ? "yes" : "no", \ - int8_t: StrF("%d", x), \ - int16_t: StrF("%d", x), \ - int32_t: StrF("%d", x), int64_t: StrF("%ld", x), \ - double: StrF("%g", x), float: StrF("%g", x), \ - CORD: x, \ - array_t: as_cord($stack(x), false, "[ ]"), \ - default: "???") -#define $heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) -#define $stack(x) (__typeof(x)*)((__typeof(x)[1]){x}) -#define $tagged(obj_expr, type_name, tag_name) ({ __typeof(obj_expr) $obj = obj_expr; \ - $obj.$tag == $tag$##type_name##$##tag_name ? &$obj.tag_name : NULL; }) - - -#define not(x) _Generic(x, bool: (bool)!(x), int64_t: ~(x), int32_t: ~(x), int16_t: ~(x), int8_t: ~(x), \ - array_t: ((x).length == 0), table_t: ((x).entries.length == 0), CORD: ((x) == CORD_EMPTY), \ - default: _Static_assert(0, "Not supported")) -#define Bool(x) _Generic(x, bool: (bool)(x), int64_t: (x != 0), int32_t: (x != 0), int16_t: (x != 0), int8_t: (x != 0), CORD: ((x) == CORD_EMPTY), \ - array_t: ((x).length > 0), table_t: ((x).entries.length > 0), CORD: ((x) != CORD_EMPTY), \ - default: _Static_assert(0, "Not supported")) -#define and(x, y) _Generic(x, bool: (bool)((x) && (y)), default: ((x) & (y))) -#define or(x, y) _Generic(x, bool: (bool)((x) || (y)), default: ((x) | (y))) -#define xor(x, y) _Generic(x, bool: (bool)((x) ^ (y)), default: ((x) ^ (y))) -#define mod(x, n) ((x) % (n)) -#define mod1(x, n) (((x) % (n)) + (__typeof(x))1) -#define $cmp(x, y) (_Generic(x, CORD: CORD_cmp(x, y), char*: strcmp(x, y), const char*: strcmp(x, y), default: (x > 0) - (y > 0))) -#define $lt(x, y) (bool)(_Generic(x, int8_t: x < y, int16_t: x < y, int32_t: x < y, int64_t: x < y, float: x < y, double: x < y, bool: x < y, \ - default: $cmp(x, y) < 0)) -#define $le(x, y) (bool)(_Generic(x, int8_t: x <= y, int16_t: x <= y, int32_t: x <= y, int64_t: x <= y, float: x <= y, double: x <= y, bool: x <= y, \ - default: $cmp(x, y) <= 0)) -#define $ge(x, y) (bool)(_Generic(x, int8_t: x >= y, int16_t: x >= y, int32_t: x >= y, int64_t: x >= y, float: x >= y, double: x >= y, bool: x >= y, \ - default: $cmp(x, y) >= 0)) -#define $gt(x, y) (bool)(_Generic(x, int8_t: x > y, int16_t: x > y, int32_t: x > y, int64_t: x > y, float: x > y, double: x > y, bool: x > y, \ - default: $cmp(x, y) > 0)) -#define $eq(x, y) (bool)(_Generic(x, int8_t: x == y, int16_t: x == y, int32_t: x == y, int64_t: x == y, float: x == y, double: x == y, bool: x == y, \ - default: $cmp(x, y) == 0)) -#define $ne(x, y) (bool)(_Generic(x, int8_t: x != y, int16_t: x != y, int32_t: x != y, int64_t: x != y, float: x != y, double: x != y, bool: x != y, \ - default: $cmp(x, y) != 0)) -#define min(x, y) ({ $var($min_lhs, x); $var($min_rhs, y); $le($min_lhs, $min_rhs) ? $min_lhs : $min_rhs; }) -#define max(x, y) ({ $var($min_lhs, x); $var($min_rhs, y); $ge($min_lhs, $min_rhs) ? $min_lhs : $min_rhs; }) - -#define say(str) CORD_put(CORD_cat(str, "\n"), stdout) -#define $test(src, expr, expected) do { \ - CORD $result = $cord(expr); \ - CORD $output = CORD_catn(5, USE_COLOR ? "\x1b[33;1m>>\x1b[0m " : ">> ", src, USE_COLOR ? "\n\x1b[0;2m=\x1b[m " : "\n= ", $result, "\x1b[m"); \ - puts(CORD_to_const_char_star($output)); \ - if (expected && CORD_cmp($result, expected)) { \ - fprintf(stderr, USE_COLOR ? "\x1b[31;1;7mTEST FAILURE!\x1b[27m\nI expected:\n\t\x1b[0;1m%s\x1b[1;31m\nbut got:\n\t%s\x1b[m\n" : "TEST FAILURE!\nI expected:\n\t%s\nbut got:\n\t%s\n", CORD_to_const_char_star(expected), CORD_to_const_char_star($result)); \ - raise(SIGABRT); \ - } \ - } while (0) - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/tomo.c b/tomo.c new file mode 100644 index 00000000..1646f5bd --- /dev/null +++ b/tomo.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include + +#include "ast.h" +#include "builtins/string.h" +#include "compile.h" +#include "parse.h" +#include "typecheck.h" +#include "types.h" + +int main(int argc, char *argv[]) +{ + if (argc < 2) return 1; + + // register_printf_modifier(L"p"); + if (register_printf_specifier('T', printf_type, printf_pointer_size)) + errx(1, "Couldn't set printf specifier"); + if (register_printf_specifier('W', printf_ast, printf_pointer_size)) + errx(1, "Couldn't set printf specifier"); + + const char *autofmt = getenv("AUTOFMT"); + if (!autofmt) autofmt = "indent -kr -l100 -nbbo -nut -sob"; + + file_t *f = load_file(argv[1]); + + ast_t *ast = parse_file(f, NULL); + + if (!ast) + errx(1, "Could not compile!"); + + bool verbose = (getenv("VERBOSE") && strcmp(getenv("VERBOSE"), "1") == 0); + if (verbose) { + FILE *out = popen(heap_strf("bat -P --file-name='%s'", argv[1]), "w"); + fputs(f->text, out); + fclose(out); + } + + if (verbose) { + FILE *out = popen("bat -P --file-name=AST", "w"); + fputs(ast_to_str(ast), out); + fclose(out); + } + + module_code_t module = compile_file(ast); + + CORD program = CORD_all( + "// File: ", f->filename, ".h\n", + module.header, + "\n", + "// File: ", f->filename, ".c\n", + module.c_file, + "\n", + "int main(int argc, const char *argv[]) {\n" + "(void)argc;\n" + "(void)argv;\n" + "GC_INIT();\n" + "detect_color();\n" + "$load();\n" + "return 0;\n" + "}\n" + ); + + if (verbose) { + FILE *out = popen(heap_strf("%s | bat -P --file-name=program.c", autofmt), "w"); + CORD_put(program, out); + fclose(out); + } + + const char *cflags = getenv("CFLAGS"); + if (!cflags) + cflags = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover"; + + const char *ldlibs = "-lgc -lcord -lm -L. -ltomo"; + if (getenv("LDLIBS")) + ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS")); + + const char *ldflags = "-Wl,-rpath '-Wl,$ORIGIN'"; + + const char *cc = getenv("CC"); + if (!cc) cc = "tcc"; + const char *run = streq(cc, "tcc") ? heap_strf("tcc -run %s %s %s -", cflags, ldflags, ldlibs) + : heap_strf("gcc -x c %s %s %s - -o program && ./program", cflags, ldflags, ldlibs); + FILE *runner = popen(run, "w"); + CORD_put(program, runner); + fclose(runner); + + return 0; +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/tomo.h b/tomo.h new file mode 100644 index 00000000..113e5820 --- /dev/null +++ b/tomo.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "SipHash/halfsiphash.h" +#include "builtins/array.h" +#include "builtins/bool.h" +#include "builtins/color.h" +#include "builtins/datatypes.h" +#include "builtins/functions.h" +#include "builtins/integers.h" +#include "builtins/memory.h" +#include "builtins/nums.h" +#include "builtins/pointer.h" +#include "builtins/string.h" +#include "builtins/table.h" +#include "builtins/types.h" + +#define Void_t void + +CORD as_cord(void *x, bool use_color, const char *fmt, ...); + +#define StrF(...) ({ CORD $c; CORD_sprintf(&$c, __VA_ARGS__); $c; }) +#define $var(var, val) __typeof(val) var = val +#define $cord(x) _Generic(x, bool: x ? "yes" : "no", \ + int8_t: StrF("%d", x), \ + int16_t: StrF("%d", x), \ + int32_t: StrF("%d", x), int64_t: StrF("%ld", x), \ + double: StrF("%g", x), float: StrF("%g", x), \ + CORD: x, \ + array_t: as_cord($stack(x), false, "[ ]"), \ + default: "???") +#define $heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) +#define $stack(x) (__typeof(x)*)((__typeof(x)[1]){x}) +#define $tagged(obj_expr, type_name, tag_name) ({ __typeof(obj_expr) $obj = obj_expr; \ + $obj.$tag == $tag$##type_name##$##tag_name ? &$obj.tag_name : NULL; }) + + +#define not(x) _Generic(x, bool: (bool)!(x), int64_t: ~(x), int32_t: ~(x), int16_t: ~(x), int8_t: ~(x), \ + array_t: ((x).length == 0), table_t: ((x).entries.length == 0), CORD: ((x) == CORD_EMPTY), \ + default: _Static_assert(0, "Not supported")) +#define Bool(x) _Generic(x, bool: (bool)(x), int64_t: (x != 0), int32_t: (x != 0), int16_t: (x != 0), int8_t: (x != 0), CORD: ((x) == CORD_EMPTY), \ + array_t: ((x).length > 0), table_t: ((x).entries.length > 0), CORD: ((x) != CORD_EMPTY), \ + default: _Static_assert(0, "Not supported")) +#define and(x, y) _Generic(x, bool: (bool)((x) && (y)), default: ((x) & (y))) +#define or(x, y) _Generic(x, bool: (bool)((x) || (y)), default: ((x) | (y))) +#define xor(x, y) _Generic(x, bool: (bool)((x) ^ (y)), default: ((x) ^ (y))) +#define mod(x, n) ((x) % (n)) +#define mod1(x, n) (((x) % (n)) + (__typeof(x))1) +#define $cmp(x, y) (_Generic(x, CORD: CORD_cmp(x, y), char*: strcmp(x, y), const char*: strcmp(x, y), default: (x > 0) - (y > 0))) +#define $lt(x, y) (bool)(_Generic(x, int8_t: x < y, int16_t: x < y, int32_t: x < y, int64_t: x < y, float: x < y, double: x < y, bool: x < y, \ + default: $cmp(x, y) < 0)) +#define $le(x, y) (bool)(_Generic(x, int8_t: x <= y, int16_t: x <= y, int32_t: x <= y, int64_t: x <= y, float: x <= y, double: x <= y, bool: x <= y, \ + default: $cmp(x, y) <= 0)) +#define $ge(x, y) (bool)(_Generic(x, int8_t: x >= y, int16_t: x >= y, int32_t: x >= y, int64_t: x >= y, float: x >= y, double: x >= y, bool: x >= y, \ + default: $cmp(x, y) >= 0)) +#define $gt(x, y) (bool)(_Generic(x, int8_t: x > y, int16_t: x > y, int32_t: x > y, int64_t: x > y, float: x > y, double: x > y, bool: x > y, \ + default: $cmp(x, y) > 0)) +#define $eq(x, y) (bool)(_Generic(x, int8_t: x == y, int16_t: x == y, int32_t: x == y, int64_t: x == y, float: x == y, double: x == y, bool: x == y, \ + default: $cmp(x, y) == 0)) +#define $ne(x, y) (bool)(_Generic(x, int8_t: x != y, int16_t: x != y, int32_t: x != y, int64_t: x != y, float: x != y, double: x != y, bool: x != y, \ + default: $cmp(x, y) != 0)) +#define min(x, y) ({ $var($min_lhs, x); $var($min_rhs, y); $le($min_lhs, $min_rhs) ? $min_lhs : $min_rhs; }) +#define max(x, y) ({ $var($min_lhs, x); $var($min_rhs, y); $ge($min_lhs, $min_rhs) ? $min_lhs : $min_rhs; }) + +#define say(str) CORD_put(CORD_cat(str, "\n"), stdout) +#define $test(src, expr, expected) do { \ + CORD $result = $cord(expr); \ + CORD $output = CORD_catn(5, USE_COLOR ? "\x1b[33;1m>>\x1b[0m " : ">> ", src, USE_COLOR ? "\n\x1b[0;2m=\x1b[m " : "\n= ", $result, "\x1b[m"); \ + puts(CORD_to_const_char_star($output)); \ + if (expected && CORD_cmp($result, expected)) { \ + fprintf(stderr, USE_COLOR ? "\x1b[31;1;7mTEST FAILURE!\x1b[27m\nI expected:\n\t\x1b[0;1m%s\x1b[1;31m\nbut got:\n\t%s\x1b[m\n" : "TEST FAILURE!\nI expected:\n\t%s\nbut got:\n\t%s\n", CORD_to_const_char_star(expected), CORD_to_const_char_star($result)); \ + raise(SIGABRT); \ + } \ + } while (0) + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 -- cgit v1.2.3