diff options
Diffstat (limited to 'src/compile')
| -rw-r--r-- | src/compile/assignments.c | 43 | ||||
| -rw-r--r-- | src/compile/assignments.h | 3 | ||||
| -rw-r--r-- | src/compile/binops.c | 2 | ||||
| -rw-r--r-- | src/compile/cli.c | 2 | ||||
| -rw-r--r-- | src/compile/declarations.c | 52 | ||||
| -rw-r--r-- | src/compile/declarations.h | 10 | ||||
| -rw-r--r-- | src/compile/enums.c | 2 | ||||
| -rw-r--r-- | src/compile/files.c | 2 | ||||
| -rw-r--r-- | src/compile/functions.c | 2 | ||||
| -rw-r--r-- | src/compile/optionals.c | 2 | ||||
| -rw-r--r-- | src/compile/promotions.c | 2 | ||||
| -rw-r--r-- | src/compile/statements.c | 1 | ||||
| -rw-r--r-- | src/compile/structs.c | 2 |
13 files changed, 72 insertions, 53 deletions
diff --git a/src/compile/assignments.c b/src/compile/assignments.c index a31aee56..c7e61e26 100644 --- a/src/compile/assignments.c +++ b/src/compile/assignments.c @@ -7,6 +7,7 @@ #include "../stdlib/text.h" #include "../stdlib/util.h" #include "../typecheck.h" +#include "declarations.h" #include "integers.h" #include "pointers.h" #include "promotions.h" @@ -85,48 +86,6 @@ Text_t compile_update_assignment(env_t *env, ast_t *ast) { } public -Text_t compile_declaration(type_t *t, Text_t name) { - if (t->tag == FunctionType) { - DeclareMatch(fn, t, FunctionType); - Text_t code = Texts(compile_type(fn->ret), " (*", name, ")("); - for (arg_t *arg = fn->args; arg; arg = arg->next) { - code = Texts(code, compile_type(arg->type)); - if (arg->next) code = Texts(code, ", "); - } - if (!fn->args) code = Texts(code, "void"); - return Texts(code, ")"); - } else if (t->tag != ModuleType) { - return Texts(compile_type(t), " ", name); - } else { - return EMPTY_TEXT; - } -} - -public -Text_t compile_declared_value(env_t *env, ast_t *declare_ast) { - DeclareMatch(decl, declare_ast, Declare); - type_t *t = decl->type ? parse_type_ast(env, decl->type) : get_type(env, decl->value); - - if (t->tag == AbortType || t->tag == VoidType || t->tag == ReturnType) - code_err(declare_ast, "You can't declare a variable with a ", type_to_str(t), " value"); - - if (decl->value) { - Text_t val_code = compile_maybe_incref(env, decl->value, t); - if (t->tag == FunctionType) { - assert(promote(env, decl->value, &val_code, t, Type(ClosureType, t))); - t = Type(ClosureType, t); - } - return val_code; - } else { - Text_t val_code = compile_empty(t); - if (val_code.length == 0) - code_err(declare_ast, "This type (", type_to_str(t), - ") cannot be uninitialized. You must provide a value."); - return val_code; - } -} - -public Text_t compile_assignment(env_t *env, ast_t *target, Text_t value) { return Texts(compile_lvalue(env, target), " = ", value); } diff --git a/src/compile/assignments.h b/src/compile/assignments.h index 1a162623..6ee18342 100644 --- a/src/compile/assignments.h +++ b/src/compile/assignments.h @@ -4,10 +4,7 @@ #include "../ast.h" #include "../environment.h" #include "../stdlib/datatypes.h" -#include "../types.h" Text_t compile_update_assignment(env_t *env, ast_t *ast); -Text_t compile_declaration(type_t *t, Text_t name); -Text_t compile_declared_value(env_t *env, ast_t *declare_ast); Text_t compile_assignment(env_t *env, ast_t *target, Text_t value); Text_t compile_lvalue(env_t *env, ast_t *ast); diff --git a/src/compile/binops.c b/src/compile/binops.c index ec30f0c8..38fb52db 100644 --- a/src/compile/binops.c +++ b/src/compile/binops.c @@ -8,7 +8,7 @@ #include "../stdlib/util.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" +#include "declarations.h" #include "functions.h" #include "optionals.h" #include "promotions.h" diff --git a/src/compile/cli.c b/src/compile/cli.c index 48504207..cccee5a5 100644 --- a/src/compile/cli.c +++ b/src/compile/cli.c @@ -7,7 +7,7 @@ #include "../stdlib/util.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" +#include "declarations.h" #include "optionals.h" #include "text.h" #include "types.h" diff --git a/src/compile/declarations.c b/src/compile/declarations.c new file mode 100644 index 00000000..cb961db0 --- /dev/null +++ b/src/compile/declarations.c @@ -0,0 +1,52 @@ +// This file defines how to compile variable declarations +#include "../ast.h" +#include "../compile.h" +#include "../environment.h" +#include "../stdlib/datatypes.h" +#include "../stdlib/text.h" +#include "../stdlib/util.h" +#include "../typecheck.h" +#include "promotions.h" +#include "types.h" + +public +Text_t compile_declaration(type_t *t, Text_t name) { + if (t->tag == FunctionType) { + DeclareMatch(fn, t, FunctionType); + Text_t code = Texts(compile_type(fn->ret), " (*", name, ")("); + for (arg_t *arg = fn->args; arg; arg = arg->next) { + code = Texts(code, compile_type(arg->type)); + if (arg->next) code = Texts(code, ", "); + } + if (!fn->args) code = Texts(code, "void"); + return Texts(code, ")"); + } else if (t->tag != ModuleType) { + return Texts(compile_type(t), " ", name); + } else { + return EMPTY_TEXT; + } +} + +public +Text_t compile_declared_value(env_t *env, ast_t *declare_ast) { + DeclareMatch(decl, declare_ast, Declare); + type_t *t = decl->type ? parse_type_ast(env, decl->type) : get_type(env, decl->value); + + if (t->tag == AbortType || t->tag == VoidType || t->tag == ReturnType) + code_err(declare_ast, "You can't declare a variable with a ", type_to_str(t), " value"); + + if (decl->value) { + Text_t val_code = compile_maybe_incref(env, decl->value, t); + if (t->tag == FunctionType) { + assert(promote(env, decl->value, &val_code, t, Type(ClosureType, t))); + t = Type(ClosureType, t); + } + return val_code; + } else { + Text_t val_code = compile_empty(t); + if (val_code.length == 0) + code_err(declare_ast, "This type (", type_to_str(t), + ") cannot be uninitialized. You must provide a value."); + return val_code; + } +} diff --git a/src/compile/declarations.h b/src/compile/declarations.h new file mode 100644 index 00000000..72272d6b --- /dev/null +++ b/src/compile/declarations.h @@ -0,0 +1,10 @@ +// This file defines how to compile variable declarations +#pragma once + +#include "../ast.h" +#include "../environment.h" +#include "../stdlib/datatypes.h" +#include "../types.h" + +Text_t compile_declaration(type_t *t, Text_t name); +Text_t compile_declared_value(env_t *env, ast_t *declare_ast); diff --git a/src/compile/enums.c b/src/compile/enums.c index 759d3bc0..12b58b26 100644 --- a/src/compile/enums.c +++ b/src/compile/enums.c @@ -7,7 +7,7 @@ #include "../stdlib/tables.h" #include "../stdlib/text.h" #include "../typecheck.h" -#include "assignments.h" +#include "declarations.h" #include "pointers.h" #include "structs.h" #include "types.h" diff --git a/src/compile/files.c b/src/compile/files.c index f221201b..908b8110 100644 --- a/src/compile/files.c +++ b/src/compile/files.c @@ -13,7 +13,7 @@ #include "../stdlib/text.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" +#include "declarations.h" #include "enums.h" #include "files.h" #include "functions.h" diff --git a/src/compile/functions.c b/src/compile/functions.c index b01f599b..fdde7cfe 100644 --- a/src/compile/functions.c +++ b/src/compile/functions.c @@ -13,8 +13,8 @@ #include "../stdlib/util.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" #include "blocks.h" +#include "declarations.h" #include "integers.h" #include "lists.h" #include "promotions.h" diff --git a/src/compile/optionals.c b/src/compile/optionals.c index 3113ee49..6dd5c2b7 100644 --- a/src/compile/optionals.c +++ b/src/compile/optionals.c @@ -8,7 +8,7 @@ #include "../stdlib/util.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" +#include "declarations.h" #include "text.h" #include "types.h" diff --git a/src/compile/promotions.c b/src/compile/promotions.c index c3d7eddc..d017568a 100644 --- a/src/compile/promotions.c +++ b/src/compile/promotions.c @@ -8,7 +8,7 @@ #include "../stdlib/text.h" #include "../typecheck.h" #include "../types.h" -#include "assignments.h" +#include "declarations.h" #include "functions.h" #include "integers.h" #include "lists.h" diff --git a/src/compile/statements.c b/src/compile/statements.c index 03505691..2e46e42b 100644 --- a/src/compile/statements.c +++ b/src/compile/statements.c @@ -17,6 +17,7 @@ #include "../typecheck.h" #include "assignments.h" #include "blocks.h" +#include "declarations.h" #include "functions.h" #include "optionals.h" #include "pointers.h" diff --git a/src/compile/structs.c b/src/compile/structs.c index af012cd5..2dc4a60b 100644 --- a/src/compile/structs.c +++ b/src/compile/structs.c @@ -9,7 +9,7 @@ #include "../stdlib/tables.h" #include "../stdlib/text.h" #include "../typecheck.h" -#include "assignments.h" +#include "declarations.h" #include "functions.h" #include "pointers.h" #include "types.h" |
