diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 17:53:56 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-08-24 17:53:56 -0400 |
| commit | 02a864390d47ef165f4113eb9eeb67cafbd9b803 (patch) | |
| tree | eb5760e108ce8cfac1d6bd12a0ba58a31d5fe9d0 /src | |
| parent | 124f22a4b6ba59d52d4400e6f2470e2c18984032 (diff) | |
Move ConvertDef logic
Diffstat (limited to 'src')
| -rw-r--r-- | src/compile/files.c | 24 | ||||
| -rw-r--r-- | src/compile/functions.c | 25 | ||||
| -rw-r--r-- | src/compile/functions.h | 1 |
3 files changed, 27 insertions, 23 deletions
diff --git a/src/compile/files.c b/src/compile/files.c index 05fb6ca4..006885e1 100644 --- a/src/compile/files.c +++ b/src/compile/files.c @@ -269,29 +269,7 @@ Text_t compile_statement_namespace_header(env_t *env, Path_t header_path, ast_t compile_declaration(t, namespace_name(env, env->namespace, Text$from_str(decl_name))), ";\n"); } case FunctionDef: return compile_function_declaration(env, ast); - case ConvertDef: { - DeclareMatch(def, ast, ConvertDef); - - Text_t arg_signature = Text("("); - for (arg_ast_t *arg = def->args; arg; arg = arg->next) { - type_t *arg_type = get_arg_ast_type(env, arg); - arg_signature = Texts(arg_signature, compile_declaration(arg_type, Texts("_$", arg->name))); - if (arg->next) arg_signature = Texts(arg_signature, ", "); - } - arg_signature = Texts(arg_signature, ")"); - - type_t *ret_t = def->ret_type ? parse_type_ast(env, def->ret_type) : Type(VoidType); - Text_t ret_type_code = compile_type(ret_t); - Text_t name = Text$from_str(get_type_name(ret_t)); - if (name.length == 0) - code_err(ast, - "Conversions are only supported for text, struct, and enum " - "types, not ", - type_to_str(ret_t)); - Text_t name_code = - namespace_name(env, env->namespace, Texts(name, "$", String(get_line_number(ast->file, ast->start)))); - return Texts(ret_type_code, " ", name_code, arg_signature, ";\n"); - } + case ConvertDef: return compile_convert_declaration(env, ast); default: return EMPTY_TEXT; } assert(ns_env); diff --git a/src/compile/functions.c b/src/compile/functions.c index d9bee1e2..33ea9f98 100644 --- a/src/compile/functions.c +++ b/src/compile/functions.c @@ -49,6 +49,31 @@ Text_t compile_function_declaration(env_t *env, ast_t *ast) { } public +Text_t compile_convert_declaration(env_t *env, ast_t *ast) { + DeclareMatch(def, ast, ConvertDef); + + Text_t arg_signature = Text("("); + for (arg_ast_t *arg = def->args; arg; arg = arg->next) { + type_t *arg_type = get_arg_ast_type(env, arg); + arg_signature = Texts(arg_signature, compile_declaration(arg_type, Texts("_$", arg->name))); + if (arg->next) arg_signature = Texts(arg_signature, ", "); + } + arg_signature = Texts(arg_signature, ")"); + + type_t *ret_t = def->ret_type ? parse_type_ast(env, def->ret_type) : Type(VoidType); + Text_t ret_type_code = compile_type(ret_t); + Text_t name = Text$from_str(get_type_name(ret_t)); + if (name.length == 0) + code_err(ast, + "Conversions are only supported for text, struct, and enum " + "types, not ", + type_to_str(ret_t)); + Text_t name_code = + namespace_name(env, env->namespace, Texts(name, "$", String(get_line_number(ast->file, ast->start)))); + return Texts(ret_type_code, " ", name_code, arg_signature, ";\n"); +} + +public Text_t compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t *call_args) { Table_t used_args = {}; Text_t code = EMPTY_TEXT; diff --git a/src/compile/functions.h b/src/compile/functions.h index 596e8966..d269a10b 100644 --- a/src/compile/functions.h +++ b/src/compile/functions.h @@ -6,6 +6,7 @@ #include "../stdlib/datatypes.h" Text_t compile_function_declaration(env_t *env, ast_t *ast); +Text_t compile_convert_declaration(env_t *env, ast_t *ast); Text_t compile_function_call(env_t *env, ast_t *ast); Text_t compile_arguments(env_t *env, ast_t *call_ast, arg_t *spec_args, arg_ast_t *call_args); Text_t compile_lambda(env_t *env, ast_t *ast); |
