From 2d9afed93ad7fb8a06b9374bac6a5704febfc126 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 14 Feb 2024 13:28:28 -0500 Subject: [PATCH] Use dollar identifiers --- Makefile | 3 ++- compile.c | 60 ++++++++++++++++++++++----------------------- nextlang.c | 6 ++--- nextlang.h | 72 +++++++++++++++++++++++++++--------------------------- 4 files changed, 71 insertions(+), 70 deletions(-) diff --git a/Makefile b/Makefile index e2b77a4..e26ad18 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ CC=gcc PREFIX=/usr/local VERSION=0.12.1 -CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC -ftrapv -fvisibility=hidden -flto -fno-fat-lto-objects -Wl,-flto +CCONFIG=-std=c11 -Werror -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -fPIC -ftrapv -fvisibility=hidden \ + -flto -fno-fat-lto-objects -Wl,-flto -fdollars-in-identifiers LDFLAGS=-Wl,-rpath '-Wl,$$ORIGIN' # MAKEFLAGS := --jobs=$(shell nproc) --output-sync=target CWARN=-Wall -Wextra -Wno-format diff --git a/compile.c b/compile.c index 6da984e..dae371d 100644 --- a/compile.c +++ b/compile.c @@ -52,8 +52,8 @@ CORD compile(env_t *env, ast_t *ast) switch (unop->op) { case UNOP_NOT: return CORD_asprintf("not(%r)", expr); case UNOP_NEGATIVE: return CORD_cat("-", expr); - case UNOP_HEAP_ALLOCATE: return CORD_asprintf("__heap(%r)", expr); - case UNOP_STACK_REFERENCE: return CORD_asprintf("__stack(%r)", expr); + case UNOP_HEAP_ALLOCATE: return CORD_asprintf("$heap(%r)", expr); + case UNOP_STACK_REFERENCE: return CORD_asprintf("$stack(%r)", expr); default: break; } errx(1, "Invalid unop"); @@ -71,12 +71,12 @@ CORD compile(env_t *env, ast_t *ast) case BINOP_MINUS: return CORD_asprintf("(%r - %r)", lhs, rhs); case BINOP_LSHIFT: return CORD_asprintf("(%r << %r)", lhs, rhs); case BINOP_RSHIFT: return CORD_asprintf("(%r >> %r)", lhs, rhs); - case BINOP_EQ: return CORD_asprintf("__eq(%r, %r)", lhs, rhs); - case BINOP_NE: return CORD_asprintf("__ne(%r, %r)", lhs, rhs); - case BINOP_LT: return CORD_asprintf("__lt(%r, %r)", lhs, rhs); - case BINOP_LE: return CORD_asprintf("__le(%r, %r)", lhs, rhs); - case BINOP_GT: return CORD_asprintf("__gt(%r, %r)", lhs, rhs); - case BINOP_GE: return CORD_asprintf("__ge(%r, %r)", lhs, rhs); + case BINOP_EQ: return CORD_asprintf("$eq(%r, %r)", lhs, rhs); + case BINOP_NE: return CORD_asprintf("$ne(%r, %r)", lhs, rhs); + case BINOP_LT: return CORD_asprintf("$lt(%r, %r)", lhs, rhs); + case BINOP_LE: return CORD_asprintf("$le(%r, %r)", lhs, rhs); + case BINOP_GT: return CORD_asprintf("$gt(%r, %r)", lhs, rhs); + case BINOP_GE: return CORD_asprintf("$ge(%r, %r)", lhs, rhs); case BINOP_AND: return CORD_asprintf("and(%r, %r)", lhs, rhs); case BINOP_OR: return CORD_asprintf("or(%r, %r)", lhs, rhs); case BINOP_XOR: return CORD_asprintf("xor(%r, %r)", lhs, rhs); @@ -143,7 +143,7 @@ CORD compile(env_t *env, ast_t *ast) } else if (!chunks->next) { CORD code = compile(env, chunks->ast); if (chunks->ast->tag != StringLiteral) - code = CORD_asprintf("__cord(%r)", code); + code = CORD_asprintf("$cord(%r)", code); return code; } else { int64_t num_chunks = 0; @@ -154,7 +154,7 @@ CORD compile(env_t *env, ast_t *ast) for (ast_list_t *chunk = chunks; chunk; chunk = chunk->next) { CORD chunk_code = compile(env, chunk->ast); if (chunk->ast->tag != StringLiteral) - chunk_code = CORD_asprintf("__cord(%r)", chunk_code); + chunk_code = CORD_asprintf("$cord(%r)", chunk_code); CORD_sprintf(&code, "%r, %r", code, chunk_code); } return CORD_cat_char(code, ')'); @@ -174,7 +174,7 @@ CORD compile(env_t *env, ast_t *ast) } case Declare: { auto decl = Match(ast, Declare); - return CORD_asprintf("__declare(%r, %r);", compile(env, decl->var), compile(env, decl->value)); + return CORD_asprintf("$var(%r, %r);", compile(env, decl->var), compile(env, decl->value)); } case Assign: { auto assign = Match(ast, Assign); @@ -185,10 +185,10 @@ CORD compile(env_t *env, ast_t *ast) CORD code = "{ // Assignment\n"; int64_t i = 1; for (ast_list_t *value = assign->values; value; value = value->next) - CORD_appendf(&code, "__declare(_%ld, %r);\n", i++, compile(env, value->ast)); + CORD_appendf(&code, "$var($%ld, %r);\n", i++, compile(env, value->ast)); i = 1; for (ast_list_t *target = assign->targets; target; target = target->next) - CORD_appendf(&code, "%r = _%ld;\n", compile(env, target->ast), i++); + CORD_appendf(&code, "%r = $%ld;\n", compile(env, target->ast), i++); return CORD_cat(code, "\n}"); } case Min: { @@ -204,7 +204,7 @@ CORD compile(env_t *env, ast_t *ast) if (!array->items) return "(array_t){}"; - CORD code = "__array("; + CORD code = "$array("; for (ast_list_t *item = array->items; item; item = item->next) { code = CORD_cat(code, compile(env, item->ast)); if (item->next) code = CORD_cat(code, ", "); @@ -229,10 +229,10 @@ CORD compile(env_t *env, ast_t *ast) CORD_appendf(&env->funcs, "%r %s", arg_type, arg->name); if (arg->next) env->funcs = CORD_cat(env->funcs, ", "); CORD_appendf(&kwargs, "%r %s; ", arg_type, arg->name); - CORD_appendf(&passed_args, "__args.%s", arg->name); + CORD_appendf(&passed_args, "$args.%s", arg->name); if (arg->next) passed_args = CORD_cat(passed_args, ", "); } - CORD_appendf(&kwargs, "} __args = {__VA_ARGS__}; %r_(%r); })\n", name, passed_args); + CORD_appendf(&kwargs, "} $args = {__VA_ARGS__}; %r_(%r); })\n", name, passed_args); CORD_appendf(&env->staticdefs, "%r", kwargs); CORD body = compile(env, fndef->body); @@ -270,11 +270,11 @@ CORD compile(env_t *env, ast_t *ast) } case For: { auto for_ = Match(ast, For); - CORD index = for_->index ? compile(env, for_->index) : "__i"; + CORD index = for_->index ? compile(env, for_->index) : "$i"; return CORD_asprintf("{\n" - "__declare(__iter, %r);\n" - "for (int64_t %r = 1, __len = __length(__iter); %r <= __len; ++%r) {\n" - "__declare(%r, __safe_index(__iter, %s));\n" + "$var($iter, %r);\n" + "for (int64_t %r = 1, $len = $length($iter); %r <= $len; ++%r) {\n" + "$var(%r, $safe_index($iter, %s));\n" "%r\n" "}\n}", compile(env, for_->iter), @@ -309,7 +309,7 @@ CORD compile(env_t *env, ast_t *ast) } CORD_appendf(&env->types, "};\n"); - CORD cord_func = CORD_asprintf("CORD %s__cord(%s_t *obj, bool use_color) {\n" + CORD cord_func = CORD_asprintf("CORD %s$cord(%s_t *obj, bool use_color) {\n" "\tif (!obj) return \"%s\";\n", def->name, def->name, def->name); if (def->secret) { @@ -328,7 +328,7 @@ CORD compile(env_t *env, ast_t *ast) } cord_func = CORD_cat(cord_func, ")\""); for (arg_ast_t *field = def->fields; field; field = field->next) - CORD_appendf(&cord_func, ", __cord(obj->%s)", field->name); + CORD_appendf(&cord_func, ", $cord(obj->%s)", field->name); cord_func = CORD_cat(cord_func, ");\n}"); } @@ -341,7 +341,7 @@ CORD compile(env_t *env, ast_t *ast) CORD_appendf(&env->typedefs, "typedef struct %s_s %s_t;\n", def->name, def->name); CORD_appendf(&env->types, "struct %s_s {\nenum {", def->name); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { - CORD_appendf(&env->types, "%s__%s = %ld, ", def->name, tag->name, tag->value); + CORD_appendf(&env->types, "%s$%s = %ld, ", def->name, tag->name, tag->value); } env->types = CORD_cat(env->types, "} tag;\nunion {\n"); for (tag_ast_t *tag = def->tags; tag; tag = tag->next) { @@ -351,7 +351,7 @@ CORD compile(env_t *env, ast_t *ast) } CORD_appendf(&env->types, "} %s;\n", tag->name); } - env->types = CORD_cat(env->types, "} __data;\n};\n"); + env->types = CORD_cat(env->types, "} $data;\n};\n"); return CORD_EMPTY; } case DocTest: { @@ -360,7 +360,7 @@ CORD compile(env_t *env, ast_t *ast) if (test->expr->tag == Declare) { auto decl = Match(test->expr, Declare); return CORD_asprintf( - "__declare(%r, %r);\n__test(%r, %r, %r);", + "$var(%r, %r);\n$test(%r, %r, %r);", compile(env, decl->var), compile(env, decl->value), compile(env, WrapAST(test->expr, StringLiteral, .cord=src)), compile(env, decl->var), @@ -370,10 +370,10 @@ CORD compile(env_t *env, ast_t *ast) CORD code = "{ // Assignment\n"; int64_t i = 1; for (ast_list_t *value = assign->values; value; value = value->next) - CORD_appendf(&code, "__declare(_%ld, %r);\n", i++, compile(env, value->ast)); + CORD_appendf(&code, "$var($%ld, %r);\n", i++, compile(env, value->ast)); i = 1; for (ast_list_t *target = assign->targets; target; target = target->next) - CORD_appendf(&code, "%r = _%ld;\n", compile(env, target->ast), i++); + CORD_appendf(&code, "%r = $%ld;\n", compile(env, target->ast), i++); CORD expr_cord = "CORD_asprintf(\""; for (ast_list_t *target = assign->targets; target; target = target->next) @@ -381,17 +381,17 @@ CORD compile(env_t *env, ast_t *ast) expr_cord = CORD_cat(expr_cord, "\""); i = 1; for (ast_list_t *target = assign->targets; target; target = target->next) - CORD_appendf(&expr_cord, ", __cord(_%ld)", i++); + CORD_appendf(&expr_cord, ", $cord($%ld)", i++); expr_cord = CORD_cat(expr_cord, ")"); - CORD_appendf(&code, "__test(%r, %r, %r);", + CORD_appendf(&code, "$test(%r, %r, %r);", compile(env, WrapAST(test->expr, StringLiteral, .cord=src)), expr_cord, compile(env, WrapAST(test->expr, StringLiteral, .cord=test->output))); return CORD_cat(code, "\n}"); } else { return CORD_asprintf( - "__test(%r, %r, %r);", + "$test(%r, %r, %r);", compile(env, WrapAST(test->expr, StringLiteral, .cord=src)), compile(env, test->expr), compile(env, WrapAST(test->expr, StringLiteral, .cord=test->output))); diff --git a/nextlang.c b/nextlang.c index 9321490..e3841eb 100644 --- a/nextlang.c +++ b/nextlang.c @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) "%r\n" // static defs "%r\n" // funcs "\n" - "static void __load(void) {\n" + "static void $load(void) {\n" "%r" // main "}\n" "\n" @@ -72,7 +72,7 @@ int main(int argc, char *argv[]) "(void)argv;\n" "GC_INIT();\n" "USE_COLOR = getenv(\"COLOR\") ? strcmp(getenv(\"COLOR\"), \"1\") == 0 : isatty(STDOUT_FILENO);\n" - "__load();\n" + "$load();\n" "return 0;\n" "}\n", f->filename, @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) const char *cflags = getenv("CFLAGS"); if (!cflags) - cflags = "-std=c11 -fsanitize=signed-integer-overflow -fno-sanitize-recover"; + cflags = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover"; const char *ldlibs = "-lgc -lcord -lm -L. -lnext"; if (getenv("LDLIBS")) diff --git a/nextlang.h b/nextlang.h index 9339bdb..c02760d 100644 --- a/nextlang.h +++ b/nextlang.h @@ -36,30 +36,30 @@ #define Void_t void -#define __Array(t) array_t +#define $Array(t) array_t CORD as_cord(void *x, bool use_color, const char *fmt, ...); -#define CORD_asprintf(...) ({ CORD __c; CORD_sprintf(&__c, __VA_ARGS__); __c; }) -#define __declare(var, val) __typeof(val) var = val -#define __cord(x) _Generic(x, bool: x ? "yes" : "no", \ +#define CORD_asprintf(...) ({ 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: CORD_asprintf("%d", x), \ int16_t: CORD_asprintf("%d", x), \ int32_t: CORD_asprintf("%d", x), int64_t: CORD_asprintf("%ld", x), \ double: CORD_asprintf("%g", x), float: CORD_asprintf("%g", x), \ CORD: x, \ - array_t: as_cord(__stack(x), false, "[ ]"), \ + 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 __length(x) _Generic(x, default: (x).length) +#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 $length(x) _Generic(x, default: (x).length) // Convert negative indices to back-indexed without branching: index0 = index + (index < 0)*(len+1)) - 1 -#define __index(x, i) _Generic(x, array_t: ({ __typeof(x) __obj; int64_t __offset = i; __offset += (__offset < 0) * (__obj.length + 1) - 1; assert(__offset >= 0 && offset < __obj.length); __obj.data + __obj.stride * __offset;})) -#define __safe_index(x, i) _Generic(x, array_t: ({ __typeof(x) __obj; int64_t __offset = i - 1; __obj.data + __obj.stride * __offset;})) -#define __array(x, ...) ({ __typeof(x) __items[] = {x, __VA_ARGS__}; \ - (__Array(__typeof(x))){.length=sizeof(__items)/sizeof(__items[0]), \ - .stride=(int64_t)&__items[1] - (int64_t)&__items[0], \ - .data=memcpy(GC_MALLOC(sizeof(__items)), __items, sizeof(__items)), \ +#define $index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i; $offset += ($offset < 0) * ($obj.length + 1) - 1; assert($offset >= 0 && offset < $obj.length); $obj.data + $obj.stride * $offset;})) +#define $safe_index(x, i) _Generic(x, array_t: ({ __typeof(x) $obj; int64_t $offset = i - 1; $obj.data + $obj.stride * $offset;})) +#define $array(x, ...) ({ __typeof(x) $items[] = {x, __VA_ARGS__}; \ + ($Array(__typeof(x))){.length=sizeof($items)/sizeof($items[0]), \ + .stride=(int64_t)&$items[1] - (int64_t)&$items[0], \ + .data=memcpy(GC_MALLOC(sizeof($items)), $items, sizeof($items)), \ .copy_on_write=1}; }) #define not(x) _Generic(x, bool: (bool)!(x), default: ~(x)) @@ -68,29 +68,29 @@ CORD as_cord(void *x, bool use_color, const char *fmt, ...); #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) ({ __declare(__min_lhs, x); __declare(__min_rhs, y); __le(__min_lhs, __min_rhs) ? __min_lhs : __min_rhs; }) -#define max(x, y) ({ __declare(__min_lhs, x); __declare(__min_rhs, y); __ge(__min_lhs, __min_rhs) ? __min_lhs : __min_rhs; }) +#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) puts(CORD_to_const_char_star(__cord(str))) -#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)); \ +#define say(str) puts(CORD_to_const_char_star($cord(str))) +#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)