aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/compile.c23
-rw-r--r--src/compile/optionals.c24
-rw-r--r--src/compile/optionals.h4
3 files changed, 31 insertions, 20 deletions
diff --git a/src/compile.c b/src/compile.c
index 35503644..12717b98 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -148,26 +148,9 @@ Text_t compile(env_t *env, ast_t *ast) {
code_err(ast, "I don't know how to get the negative value of type ", type_to_str(t));
}
case HeapAllocate:
- case StackReference: {
- return compile_typed_allocation(env, ast, get_type(env, ast));
- }
- case Optional: {
- ast_t *value = Match(ast, Optional)->value;
- Text_t value_code = compile(env, value);
- return promote_to_optional(get_type(env, value), value_code);
- }
- case NonOptional: {
- ast_t *value = Match(ast, NonOptional)->value;
- type_t *t = get_type(env, value);
- Text_t value_code = compile(env, value);
- int64_t line = get_line_number(ast->file, ast->start);
- return Texts("({ ", compile_declaration(t, Text("opt")), " = ", value_code, "; ", "if unlikely (",
- check_none(t, Text("opt")), ")\n", "#line ", String(line), "\n", "fail_source(",
- quoted_str(ast->file->filename), ", ", String((int64_t)(value->start - value->file->text)), ", ",
- String((int64_t)(value->end - value->file->text)), ", ",
- "\"This was expected to be a value, but it's none\");\n", optional_into_nonnone(t, Text("opt")),
- "; })");
- }
+ case StackReference: return compile_typed_allocation(env, ast, get_type(env, ast));
+ case Optional: return compile_optional(env, ast);
+ case NonOptional: return compile_non_optional(env, ast);
case Power:
case Multiply:
case Divide:
diff --git a/src/compile/optionals.c b/src/compile/optionals.c
index fa7f6f29..6dad725c 100644
--- a/src/compile/optionals.c
+++ b/src/compile/optionals.c
@@ -4,7 +4,10 @@
#include "../stdlib/datatypes.h"
#include "../stdlib/text.h"
#include "../stdlib/util.h"
+#include "../typecheck.h"
#include "../types.h"
+#include "assignments.h"
+#include "text.h"
#include "types.h"
Text_t optional_into_nonnone(type_t *t, Text_t value) {
@@ -110,3 +113,24 @@ Text_t check_none(type_t *t, Text_t value) {
print_err("Optional check not implemented for: ", type_to_str(t));
return EMPTY_TEXT;
}
+
+public
+Text_t compile_optional(env_t *env, ast_t *ast) {
+ ast_t *value = Match(ast, Optional)->value;
+ Text_t value_code = compile(env, value);
+ return promote_to_optional(get_type(env, value), value_code);
+}
+
+public
+Text_t compile_non_optional(env_t *env, ast_t *ast) {
+ ast_t *value = Match(ast, NonOptional)->value;
+ type_t *t = get_type(env, value);
+ Text_t value_code = compile(env, value);
+ int64_t line = get_line_number(ast->file, ast->start);
+ return Texts("({ ", compile_declaration(t, Text("opt")), " = ", value_code, "; ", "if unlikely (",
+ check_none(t, Text("opt")), ")\n", "#line ", String(line), "\n", "fail_source(",
+ quoted_str(ast->file->filename), ", ", String((int64_t)(value->start - value->file->text)), ", ",
+ String((int64_t)(value->end - value->file->text)), ", ",
+ "\"This was expected to be a value, but it's none\");\n", optional_into_nonnone(t, Text("opt")),
+ "; })");
+}
diff --git a/src/compile/optionals.h b/src/compile/optionals.h
index f8d18b86..2a93d53e 100644
--- a/src/compile/optionals.h
+++ b/src/compile/optionals.h
@@ -1,3 +1,5 @@
+#include "../ast.h"
+#include "../environment.h"
#include "../stdlib/datatypes.h"
#include "../types.h"
@@ -5,3 +7,5 @@ Text_t optional_into_nonnone(type_t *t, Text_t value);
Text_t promote_to_optional(type_t *t, Text_t code);
Text_t compile_none(type_t *t);
Text_t check_none(type_t *t, Text_t value);
+Text_t compile_optional(env_t *env, ast_t *ast);
+Text_t compile_non_optional(env_t *env, ast_t *ast);