Support useing .c files and .S files (assembly)

This commit is contained in:
Bruce Hill 2024-09-18 15:39:22 -04:00
parent 2d78f11400
commit 7f5af625e5
5 changed files with 24 additions and 8 deletions

2
ast.h
View File

@ -312,7 +312,7 @@ struct ast_s {
} DocTest;
struct {
const char *path;
enum { USE_LOCAL, USE_MODULE, USE_SHARED_OBJECT, USE_HEADER } what;
enum { USE_LOCAL, USE_MODULE, USE_SHARED_OBJECT, USE_HEADER, USE_C_CODE, USE_ASM } what;
} Use;
struct {
CORD code;

View File

@ -1371,6 +1371,8 @@ CORD compile_statement(env_t *env, ast_t *ast)
if (use->what == USE_LOCAL) {
CORD name = file_base_name(Match(ast, Use)->path);
env->code->variable_initializers = CORD_all(env->code->variable_initializers, name, "$$initialize();\n");
} else if (use->what == USE_C_CODE) {
return CORD_all("#include \"", use->path, "\"\n");
} else if (use->what == USE_MODULE) {
const char *libname = Text$as_c_string(
Text$replace(Text$from_str(use->path), Pattern("{1+ !alphanumeric}"), Text("_"), Pattern(""), false));
@ -3735,6 +3737,10 @@ CORD compile_file(env_t *env, ast_t *ast)
} else if (stmt->ast->tag == InlineCCode) {
CORD code = compile_statement(env, stmt->ast);
env->code->staticdefs = CORD_all(env->code->staticdefs, code, "\n");
} else if (stmt->ast->tag == Use) {
CORD code = compile_statement(env, stmt->ast);
if (code)
env->code->staticdefs = CORD_all(env->code->staticdefs, code);
} else {
CORD code = compile_statement(env, stmt->ast);
assert(!code);
@ -3795,7 +3801,10 @@ CORD compile_statement_header(env_t *env, ast_t *ast)
case USE_LOCAL:
return CORD_all("#include \"", use->path, ".h\"\n");
case USE_HEADER:
return CORD_all("#include ", use->path, "\n");
if (use->path[0] == '<')
return CORD_all("#include ", use->path, "\n");
else
return CORD_all("#include \"", use->path, "\"\n");
default:
return CORD_EMPTY;
}

13
parse.c
View File

@ -2356,14 +2356,19 @@ PARSER(parse_use) {
pos += name_len;
while (match(&pos, ";")) continue;
int what;
if (name[0] == '<' || name[0] == '"')
if (name[0] == '<' || ends_with(name, ".h")) {
what = USE_HEADER;
else if (starts_with(name, "./") || starts_with(name, "/") || starts_with(name, "../") || starts_with(name, "~/"))
} else if (ends_with(name, ".c")) {
what = USE_C_CODE;
} else if (ends_with(name, ".S") || ends_with(name, ".s")) {
what = USE_ASM;
} else if (starts_with(name, "./") || starts_with(name, "/") || starts_with(name, "../") || starts_with(name, "~/")) {
what = USE_LOCAL;
else if (ends_with(name, ".so"))
} else if (ends_with(name, ".so")) {
what = USE_SHARED_OBJECT;
else
} else {
what = USE_MODULE;
}
return NewAST(ctx->file, start, pos, Use, .path=name, .what=what);
}

4
tomo.c
View File

@ -431,6 +431,10 @@ void build_file_dependency_graph(const char *filename, Table_t *to_compile, Tabl
Table$str_set(to_link, lib, lib);
break;
}
case USE_ASM: {
Table$str_set(to_link, use->path, use->path);
break;
}
default: case USE_HEADER: break;
}
}

View File

@ -191,8 +191,6 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
globfree(&tm_files);
return module_env;
}
case USE_SHARED_OBJECT: return NULL;
case USE_HEADER: return NULL;
default: return NULL;
}
}