aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-10-12 14:19:59 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-10-12 14:19:59 -0400
commit6e5fb2ac4e5b780c74f310446ddd80d571170b0d (patch)
tree587241a69065c01b890d7ae70a37af08bba646b1 /src
parent3cd3b20f58e9d2c6463d503be09e5d4cfaadee6c (diff)
More code cleanups
Diffstat (limited to 'src')
-rw-r--r--src/compile/assignments.c2
-rw-r--r--src/compile/cli.c10
-rw-r--r--src/compile/functions.c3
-rw-r--r--src/stdlib/cli.c1
-rw-r--r--src/stdlib/paths.c3
-rw-r--r--src/stdlib/stacktrace.c2
-rw-r--r--src/stdlib/stdlib.c3
-rw-r--r--src/stdlib/tables.c15
-rw-r--r--src/typecheck.c6
9 files changed, 23 insertions, 22 deletions
diff --git a/src/compile/assignments.c b/src/compile/assignments.c
index 921b5320..74d1b543 100644
--- a/src/compile/assignments.c
+++ b/src/compile/assignments.c
@@ -161,6 +161,7 @@ Text_t compile_lvalue(env_t *env, ast_t *ast) {
container_t = value_type(container_t);
type_t *index_t = get_type(env, index->index);
if (container_t->tag == ListType) {
+ if (!index->index) code_err(ast, "This list needs an index");
Text_t target_code = compile_to_pointer_depth(env, index->indexed, 1, false);
type_t *item_type = Match(container_t, ListType)->item_type;
Text_t index_code =
@@ -171,6 +172,7 @@ Text_t compile_lvalue(env_t *env, ast_t *ast) {
return Texts("List_lvalue(", compile_type(item_type), ", ", target_code, ", ", index_code, ", ",
(int64_t)(ast->start - ast->file->text), ", ", (int64_t)(ast->end - ast->file->text), ")");
} else if (container_t->tag == TableType) {
+ if (!index->index) code_err(ast, "This table needs an index");
DeclareMatch(table_type, container_t, TableType);
if (table_type->default_value) {
type_t *value_type = get_type(env, table_type->default_value);
diff --git a/src/compile/cli.c b/src/compile/cli.c
index f138bd78..4138b778 100644
--- a/src/compile/cli.c
+++ b/src/compile/cli.c
@@ -88,9 +88,13 @@ Text_t compile_cli_arg_call(env_t *env, Text_t fn_name, type_t *fn_type, const c
for (arg_t *arg = fn_info->args; arg; arg = arg->next) {
code = Texts(code, compile_declaration(arg->type, Texts("_$", Text$from_str(arg->name))));
if (arg->default_val) {
- Text_t default_val =
- arg->type ? compile_to_type(env, arg->default_val, arg->type) : compile(env, arg->default_val);
- if (arg->type->tag != OptionalType) default_val = promote_to_optional(arg->type, default_val);
+ Text_t default_val;
+ if (arg->type) {
+ default_val = compile_to_type(env, arg->default_val, arg->type);
+ if (arg->type->tag != OptionalType) default_val = promote_to_optional(arg->type, default_val);
+ } else {
+ default_val = compile(env, arg->default_val);
+ }
code = Texts(code, " = ", default_val);
} else {
code = Texts(code, " = ", compile_empty(arg->type));
diff --git a/src/compile/functions.c b/src/compile/functions.c
index 757df073..4a2812ba 100644
--- a/src/compile/functions.c
+++ b/src/compile/functions.c
@@ -784,9 +784,6 @@ Text_t compile_function(env_t *env, Text_t name_code, ast_t *ast, Text_t *static
}
}
- Text_t qualified_name = Text$from_str(function_name);
- if (env->namespace && env->namespace->parent && env->namespace->name)
- qualified_name = Texts(env->namespace->name, ".", qualified_name);
return definition;
}
diff --git a/src/stdlib/cli.c b/src/stdlib/cli.c
index 2d54d330..de3b804e 100644
--- a/src/stdlib/cli.c
+++ b/src/stdlib/cli.c
@@ -199,7 +199,6 @@ void _tomo_parse_args(int argc, char *argv[], Text_t usage, Text_t help, const c
if (argv[i][0] == '-' && argv[i][1] == '-') {
if (argv[i][2] == '\0') { // "--" signals the rest of the arguments are literal
used_args[i] = true;
- i += 1;
break;
}
diff --git a/src/stdlib/paths.c b/src/stdlib/paths.c
index 76d0d629..73f62975 100644
--- a/src/stdlib/paths.c
+++ b/src/stdlib/paths.c
@@ -330,10 +330,11 @@ OptionalList_t Path$read_bytes(Path_t path, OptionalInt_t count) {
// be closed by GC finalizers.
GC_gcollect();
fd = open(path_str, O_RDONLY);
- if (fd == -1) return NONE_LIST;
}
}
+ if (fd == -1) return NONE_LIST;
+
struct stat sb;
if (fstat(fd, &sb) != 0) return NONE_LIST;
diff --git a/src/stdlib/stacktrace.c b/src/stdlib/stacktrace.c
index 0953e660..b21d0cbc 100644
--- a/src/stdlib/stacktrace.c
+++ b/src/stdlib/stacktrace.c
@@ -34,7 +34,7 @@ static void fprint_context(FILE *out, const char *filename, int lineno, int cont
num_width += 1;
while ((nread = getline(&line, &size, f)) != -1) {
- if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = '\0';
+ if (line[nread - 1] == '\n') line[nread - 1] = '\0';
if (cur_line >= lineno - context_before) {
int w = 1;
diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c
index 5204194b..dd6f804d 100644
--- a/src/stdlib/stdlib.c
+++ b/src/stdlib/stdlib.c
@@ -61,7 +61,8 @@ void tomo_init(void) {
GC_INIT();
const char *color_env = getenv("COLOR");
USE_COLOR = color_env ? strcmp(color_env, "1") == 0 : isatty(STDOUT_FILENO);
- if (getenv("NO_COLOR") && getenv("NO_COLOR")[0] != '\0') USE_COLOR = false;
+ const char *no_color_env = getenv("NO_COLOR");
+ if (no_color_env && no_color_env[0] != '\0') USE_COLOR = false;
setlocale(LC_ALL, "");
assert(getrandom(TOMO_HASH_KEY, sizeof(TOMO_HASH_KEY), 0) == sizeof(TOMO_HASH_KEY));
diff --git a/src/stdlib/tables.c b/src/stdlib/tables.c
index 3c78b770..45e3abb4 100644
--- a/src/stdlib/tables.c
+++ b/src/stdlib/tables.c
@@ -732,20 +732,20 @@ void Table$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeIn
if (t->fallback) Table$serialize(t->fallback, out, pointers, type);
}
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wstack-protector"
-#endif
public
void Table$deserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_t *type) {
int64_t len;
Int64$deserialize(in, &len, pointers, &Int$info);
Table_t t = EMPTY_TABLE;
+ char keybuf[256], valbuf[256];
+ char *key =
+ (size_t)type->TableInfo.key->size <= sizeof(keybuf) ? keybuf : GC_MALLOC((size_t)type->TableInfo.key->size);
+ char *value =
+ (size_t)type->TableInfo.value->size <= sizeof(valbuf) ? valbuf : GC_MALLOC((size_t)type->TableInfo.value->size);
+
for (int64_t i = 0; i < len; i++) {
- char key[type->TableInfo.key->size];
_deserialize(in, key, pointers, type->TableInfo.key);
- char value[type->TableInfo.value->size];
if (type->TableInfo.value->size > 0) _deserialize(in, value, pointers, type->TableInfo.value);
Table$set(&t, key, value, type);
}
@@ -757,6 +757,3 @@ void Table$deserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_
*(Table_t *)outval = t;
}
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
diff --git a/src/typecheck.c b/src/typecheck.c
index 67fb1168..1ce88806 100644
--- a/src/typecheck.c
+++ b/src/typecheck.c
@@ -76,10 +76,10 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast) {
type_t *val_type = table_type->value ? parse_type_ast(env, table_type->value) : EMPTY_TYPE;
if (!val_type) code_err(ast, "I can't figure out what the value type for this entry is.");
- if (has_stack_memory(val_type))
+ if (table_type->value && has_stack_memory(val_type))
code_err(table_type->value,
"Tables can't have stack references because the list may outlive the stack frame.");
- else if (val_type->tag == OptionalType)
+ else if (table_type->value && val_type->tag == OptionalType)
code_err(ast, "Tables with optional-typed values are not currently supported");
return Type(TableType, .key_type = key_type, .value_type = val_type, .env = env,
@@ -88,7 +88,7 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast) {
case FunctionTypeAST: {
DeclareMatch(fn, ast, FunctionTypeAST);
type_t *ret_t = fn->ret ? parse_type_ast(env, fn->ret) : Type(VoidType);
- if (has_stack_memory(ret_t))
+ if (fn->ret && has_stack_memory(ret_t))
code_err(fn->ret, "Functions are not allowed to return stack references, because the reference may no "
"longer exist on the stack.");
arg_t *type_args = NULL;