1 // This file defines how to compile variable declarations
4 #include "../environment.h"
5 #include "../stdlib/datatypes.h"
6 #include "../stdlib/text.h"
7 #include "../stdlib/util.h"
8 #include "../typecheck.h"
9 #include "compilation.h"
12 Text_t compile_declaration(type_t *t, Text_t name) {
13 if (t->tag == FunctionType) {
14 DeclareMatch(fn, t, FunctionType);
15 Text_t code = Texts(compile_type(fn->ret), " (*", name, ")(");
16 for (arg_t *arg = fn->args; arg; arg = arg->next) {
17 code = Texts(code, compile_type(arg->type));
18 if (arg->next) code = Texts(code, ", ");
20 if (!fn->args) code = Texts(code, "void");
21 return Texts(code, ")");
22 } else if (t->tag != ModuleType) {
23 return Texts(compile_type(t), " ", name);
30 Text_t compile_declared_value(env_t *env, ast_t *declare_ast) {
31 DeclareMatch(decl, declare_ast, Declare);
32 type_t *t = decl->type ? parse_type_ast(env, decl->type) : get_type(env, decl->value);
34 if (t->tag == AbortType || t->tag == VoidType || t->tag == ReturnType)
35 code_err(declare_ast, "You can't declare a variable with a ", type_to_text(t), " value");
38 Text_t val_code = compile_maybe_incref(env, decl->value, t);
39 if (t->tag == FunctionType) assert(promote(env, decl->value, &val_code, t, Type(ClosureType, t)));
42 Text_t val_code = compile_empty(t);
43 if (val_code.length == 0)
44 code_err(declare_ast, "This type (", type_to_text(t),
45 ") cannot be uninitialized. You must provide a value.");