diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:18:43 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-06 16:18:43 -0400 |
| commit | 83b7ca60ec9db86a114bc7c9b089ef14742d5876 (patch) | |
| tree | b1df53ed6a79ce8b6171b9f1e4c93ece08dc974b /src | |
| parent | a11340e9d2466280fb22c277cc2e3893f0e9ee38 (diff) | |
| parent | 4bd353b188252a3d04ce6dbd26ea539446cbaa36 (diff) | |
Merge branch 'main' into no-colons
Diffstat (limited to 'src')
| -rw-r--r-- | src/compile.c | 16 | ||||
| -rw-r--r-- | src/typecheck.c | 34 | ||||
| -rw-r--r-- | src/types.c | 3 |
3 files changed, 17 insertions, 36 deletions
diff --git a/src/compile.c b/src/compile.c index 3d621776..5c033ef4 100644 --- a/src/compile.c +++ b/src/compile.c @@ -893,7 +893,7 @@ CORD compile_lvalue(env_t *env, ast_t *ast) compile_type(table_type->key_type), ", ", compile_type(value_type), ", ", compile_to_type(env, index->index, table_type->key_type), ", ", - compile(env, table_type->default_value), ", ", + compile_to_type(env, table_type->default_value, table_type->value_type), ", ", compile_type_info(container_t), ")"); } if (index->unchecked) @@ -3704,15 +3704,14 @@ CORD compile(env_t *env, ast_t *ast) if (indexing->unchecked) code_err(ast, "Table indexes cannot be unchecked"); if (table_type->default_value) { - type_t *value_type = get_type(env, table_type->default_value); return CORD_all("Table$get_or_default(", compile_to_pointer_depth(env, indexing->indexed, 0, false), ", ", compile_type(table_type->key_type), ", ", - compile_type(value_type), ", ", + compile_type(table_type->value_type), ", ", compile(env, indexing->index), ", ", - compile(env, table_type->default_value), ", ", + compile_to_type(env, table_type->default_value, table_type->value_type), ", ", compile_type_info(container_t), ")"); - } else if (table_type->value_type) { + } else { return CORD_all("Table$get_optional(", compile_to_pointer_depth(env, indexing->indexed, 0, false), ", ", compile_type(table_type->key_type), ", ", @@ -3721,8 +3720,6 @@ CORD compile(env_t *env, ast_t *ast) "_, ", promote_to_optional(table_type->value_type, "(*_)"), ", ", compile_none(table_type->value_type), ", ", compile_type_info(container_t), ")"); - } else { - code_err(indexing->index, "This table doesn't have a value type or a default value"); } } else if (container_t->tag == TextType) { return CORD_all("Text$cluster(", compile_to_pointer_depth(env, indexing->indexed, 0, false), ", ", compile_to_type(env, indexing->index, Type(BigIntType)), ")"); @@ -3783,11 +3780,6 @@ CORD compile_type_info(type_t *t) auto table = Match(t, TableType); type_t *key_type = table->key_type; type_t *value_type = table->value_type; - if (!value_type) { - if (!table->env) - compiler_err(NULL, NULL, NULL, "I got a table with a default value, but no environment to get its type!"); - value_type = get_type(table->env, table->default_value); - } return CORD_all("Table$info(", compile_type_info(key_type), ", ", compile_type_info(value_type), ")"); } case PointerType: { diff --git a/src/typecheck.c b/src/typecheck.c index 1aec4f3b..a1a77b5f 100644 --- a/src/typecheck.c +++ b/src/typecheck.c @@ -81,24 +81,15 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast) if (!key_type) code_err(key_type_ast, "I can't figure out what type this is."); if (has_stack_memory(key_type)) code_err(key_type_ast, "Tables can't have stack references because the array may outlive the stack frame."); - if (table_type->value) { - type_t *val_type = parse_type_ast(env, table_type->value); - if (!val_type) code_err(table_type->value, "I can't figure out what type this is."); - if (has_stack_memory(val_type)) - code_err(table_type->value, "Tables can't have stack references because the array may outlive the stack frame."); - else if (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); - } else if (table_type->default_value) { - type_t *t = Type(TableType, .key_type=key_type, - .value_type=get_type(env, table_type->default_value), .default_value=table_type->default_value, - .env=env); - if (has_stack_memory(t)) - code_err(ast, "Tables can't have stack references because the array may outlive the stack frame."); - return t; - } else { - code_err(ast, "No value type or default value!"); - } + + type_t *val_type = parse_type_ast(env, table_type->value); + if (!val_type) code_err(table_type->value, "I can't figure out what type this is."); + if (has_stack_memory(val_type)) + code_err(table_type->value, "Tables can't have stack references because the array may outlive the stack frame."); + else if (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, .default_value=table_type->default_value); } case FunctionTypeAST: { auto fn = Match(ast, FunctionTypeAST); @@ -851,11 +842,8 @@ type_t *get_type(env_t *env, ast_t *ast) } else if (value_t->tag == TableType) { auto table_type = Match(value_t, TableType); if (table_type->default_value) - return get_type(env, table_type->default_value); - else if (table_type->value_type) - return Type(OptionalType, table_type->value_type); - else - code_err(indexing->indexed, "This type doesn't have a value type or a default value"); + return table_type->value_type; + return Type(OptionalType, table_type->value_type); } else if (value_t->tag == TextType) { return value_t; } else { diff --git a/src/types.c b/src/types.c index dc330ca9..29963f76 100644 --- a/src/types.c +++ b/src/types.c @@ -783,9 +783,10 @@ CONSTFUNC type_t *most_complete_type(type_t *t1, type_t *t2) case TableType: { auto table1 = Match(t1, TableType); auto table2 = Match(t2, TableType); + ast_t *default_value = table1->default_value ? table1->default_value : table2->default_value; type_t *key = most_complete_type(table1->key_type, table2->key_type); type_t *value = most_complete_type(table1->value_type, table2->value_type); - return (key && value) ? Type(TableType, key, value) : NULL; + return (key && value) ? Type(TableType, key, value, table1->env, default_value) : NULL; } case FunctionType: { auto fn1 = Match(t1, FunctionType); |
