aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-22 13:35:28 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-22 13:35:28 -0500
commit54b8b7af12301153a5e8e6f71287a72195aab438 (patch)
tree191ae9c96250b0451dd4cab0c66a5e7332091ce0
parent58b3b84bd99cbba5ec7cc2ae05d720b7f80374c2 (diff)
Fix function compiling
-rw-r--r--compile.c2
-rw-r--r--typecheck.c10
-rw-r--r--typecheck.h1
3 files changed, 10 insertions, 3 deletions
diff --git a/compile.c b/compile.c
index 411aff2a..7daf83e0 100644
--- a/compile.c
+++ b/compile.c
@@ -546,7 +546,7 @@ CORD compile(env_t *env, ast_t *ast)
CORD_appendf(&kwargs, "} $args = {__VA_ARGS__}; %r_(%r); })\n", name, passed_args);
CORD_appendf(&env->code->staticdefs, "%r", kwargs);
- CORD body = compile(env, fndef->body);
+ CORD body = compile(body_scope, fndef->body);
if (CORD_fetch(body, 0) != '{')
body = CORD_asprintf("{\n%r\n}", body);
CORD_appendf(&env->code->funcs, ") %r", body);
diff --git a/typecheck.c b/typecheck.c
index 30ac197d..06f03552 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -102,6 +102,12 @@ void bind_statement(env_t *env, ast_t *statement)
set_binding(env, Match(decl->var, Var)->name, new(binding_t, .type=type));
break;
}
+ case FunctionDef: {
+ auto def = Match(statement, FunctionDef);
+ type_t *type = get_function_def_type(env, statement);
+ set_binding(env, Match(def->name, Var)->name, new(binding_t, .type=type));
+ break;
+ }
case StructDef: {
auto def = Match(statement, StructDef);
arg_t *fields = NULL;
@@ -148,7 +154,7 @@ void bind_statement(env_t *env, ast_t *statement)
}
}
-static type_t *get_function_def_type(env_t *env, ast_t *ast)
+type_t *get_function_def_type(env_t *env, ast_t *ast)
{
auto fn = Match(ast, FunctionDef);
arg_t *args = NULL;
@@ -160,7 +166,7 @@ static type_t *get_function_def_type(env_t *env, ast_t *ast)
}
REVERSE_LIST(args);
- type_t *ret = parse_type_ast(scope, fn->ret_type);
+ type_t *ret = fn->ret_type ? parse_type_ast(scope, fn->ret_type) : Type(VoidType);
if (has_stack_memory(ret))
code_err(ast, "Functions can't return stack references because the reference may outlive its stack frame.");
return Type(FunctionType, .args=args, .ret=ret);
diff --git a/typecheck.h b/typecheck.h
index 706d01df..1f1f3552 100644
--- a/typecheck.h
+++ b/typecheck.h
@@ -14,5 +14,6 @@ type_t *get_math_type(env_t *env, ast_t *ast, type_t *lhs_t, type_t *rhs_t);
bool is_discardable(env_t *env, ast_t *ast);
type_t *get_namespace_type(env_t *env, ast_t *namespace_ast, type_t *type);
type_t *get_file_type(env_t *env, const char *path);
+type_t *get_function_def_type(env_t *env, ast_t *ast);
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0