Better imports for types

This commit is contained in:
Bruce Hill 2024-04-21 14:58:33 -04:00
parent 3f10460a6e
commit 3590bf3407
5 changed files with 37 additions and 4 deletions

View File

@ -816,7 +816,7 @@ CORD expr_as_text(env_t *env, CORD expr, type_t *t, CORD color)
}
case ArrayType: return CORD_asprintf("Array$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
case TableType: return CORD_asprintf("Table$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
case FunctionType: return CORD_asprintf("Func$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
case FunctionType: case ClosureType: return CORD_asprintf("Func$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
case PointerType: return CORD_asprintf("Pointer$as_text(stack(%r), %r, %r)", expr, color, compile_type_info(env, t));
case StructType: case EnumType: return CORD_asprintf("(%r)->CustomInfo.as_text(stack(%r), %r, %r)",
compile_type_info(env, t), expr, color, compile_type_info(env, t));
@ -1922,6 +1922,8 @@ CORD compile_type_info(env_t *env, type_t *t)
return CORD_asprintf("$ClosureInfo(%r)", Text$quoted(type_to_cord(t), false));
}
case TypeInfoType: return "&$TypeInfo";
case MemoryType: return "&$Memory";
case VoidType: return "&$Void";
default:
compiler_err(NULL, 0, 0, "I couldn't convert to a type info: %T", t);
}
@ -2147,15 +2149,15 @@ module_code_t compile_file(ast_t *ast)
.env=env,
.object_files=env->code->object_files,
.header=CORD_all(
// CORD_asprintf("#line 1 %r\n", Text$quoted(ast->file->filename, false)),
// "#line 1 ", Text$quoted(ast->file->filename, false), "\n",
"#include <tomo/tomo.h>\n",
env->code->imports, "\n",
env->code->typedefs, "\n",
env->code->typecode, "\n",
env->code->fndefs, "\n"
),
.c_file=CORD_all(
// CORD_asprintf("#line 1 %r\n", Text$quoted(ast->file->filename, false)),
env->code->imports, "\n",
// "#line 1 ", Text$quoted(ast->file->filename, false), "\n",
env->code->staticdefs, "\n",
env->code->funcs, "\n",
env->code->typeinfos, "\n"

View File

@ -49,6 +49,8 @@ env_t *new_compilation_unit(void)
CORD struct_val;
array_t namespace;
} global_types[] = {
{"Void", Type(VoidType), "Void_t", "$Void", {}},
{"Memory", Type(MemoryType), "Memory_t", "$Memory", {}},
{"Bool", Type(BoolType), "Bool_t", "$Bool", TypedArray(ns_entry_t,
{"from_text", "Bool$from_text", "func(text:Text, success=!Bool)->Bool"},
)},

9
test/use.tm Normal file
View File

@ -0,0 +1,9 @@
imported := use ./use_import
func asdf()->imported.ImportedType
return imported.get_value()
func main()
>> [:imported.ImportedType]
>> asdf()
= ImportedType(name="Hello")

7
test/use_import.tm Normal file
View File

@ -0,0 +1,7 @@
struct ImportedType(name:Text)
func get_value()->ImportedType
return ImportedType("Hello")
func main()
pass

View File

@ -21,6 +21,19 @@ type_t *parse_type_ast(env_t *env, type_ast_t *ast)
const char *name = Match(ast, VarTypeAST)->name;
type_t *t = Table$str_get(*env->types, name);
if (t) return t;
while (strchr(name, '.')) {
char *module_name = heap_strn(name, strcspn(name, "."));
binding_t *b = get_binding(env, module_name);
if (!b || b->type->tag != ModuleType)
code_err(ast, "I don't know a module with the name '%s'", module_name);
env_t *imported = Table$str_get(*env->imports, Match(b->type, ModuleType)->name);
assert(imported);
env = imported;
name = strchr(name, '.') + 1;
t = Table$str_get(*env->types, name);
if (t) return t;
}
code_err(ast, "I don't know a type with the name '%s'", name);
}
case PointerTypeAST: {