aboutsummaryrefslogtreecommitdiff
path: root/src/compile/tables.c
blob: e9067c0ffd731f99c4a0260528aeaed42a6e2f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// This file defines how to compile tables

#include "../ast.h"
#include "expressions.h"
#include "../environment.h"
#include "../stdlib/datatypes.h"
#include "../stdlib/text.h"
#include "../typecheck.h"
#include "../types.h"
#include "functions.h"
#include "optionals.h"
#include "pointers.h"
#include "promotions.h"
#include "statements.h"
#include "types.h"

static ast_t *add_to_table_comprehension(ast_t *entry, ast_t *subject) {
    DeclareMatch(e, entry, TableEntry);
    return WrapAST(entry, MethodCall, .name = "set", .self = subject,
                   .args = new (arg_ast_t, .value = e->key, .next = new (arg_ast_t, .value = e->value)));
}

Text_t compile_typed_table(env_t *env, ast_t *ast, type_t *table_type) {
    DeclareMatch(table, ast, Table);
    if (!table->entries) {
        Text_t code = Text("((Table_t){");
        if (table->fallback) code = Texts(code, ".fallback=heap(", compile(env, table->fallback), ")");
        return Texts(code, "})");
    }

    type_t *key_t = Match(table_type, TableType)->key_type;
    type_t *value_t = Match(table_type, TableType)->value_type;

    if (value_t->tag == OptionalType)
        code_err(ast, "Tables whose values are optional (", type_to_str(value_t), ") are not currently supported.");

    for (ast_list_t *entry = table->entries; entry; entry = entry->next) {
        if (entry->ast->tag == Comprehension) goto table_comprehension;
    }

    { // No comprehension:
        env_t *key_scope = key_t->tag == EnumType ? with_enum_scope(env, key_t) : env;
        env_t *value_scope = value_t->tag == EnumType ? with_enum_scope(env, value_t) : env;
        Text_t code = Texts("Table(", compile_type(key_t), ", ", compile_type(value_t), ", ", compile_type_info(key_t),
                            ", ", compile_type_info(value_t));
        if (table->fallback) code = Texts(code, ", /*fallback:*/ heap(", compile(env, table->fallback), ")");
        else code = Texts(code, ", /*fallback:*/ NULL");

        size_t n = 0;
        for (ast_list_t *entry = table->entries; entry; entry = entry->next)
            ++n;
        code = Texts(code, ", ", String((int64_t)n));

        for (ast_list_t *entry = table->entries; entry; entry = entry->next) {
            DeclareMatch(e, entry->ast, TableEntry);
            code = Texts(code, ",\n\t{", compile_to_type(key_scope, e->key, key_t), ", ",
                         compile_to_type(value_scope, e->value, value_t), "}");
        }
        return Texts(code, ")");
    }

table_comprehension: {
    static int64_t comp_num = 1;
    env_t *scope = fresh_scope(env);
    const char *comprehension_name = String("table$", comp_num++);
    ast_t *comprehension_var =
        LiteralCode(Texts("&", comprehension_name), .type = Type(PointerType, .pointed = table_type, .is_stack = true));

    Text_t code = Texts("({ Table_t ", comprehension_name, " = {");
    if (table->fallback) code = Texts(code, ".fallback=heap(", compile(env, table->fallback), "), ");

    code = Texts(code, "};");

    Closure_t comp_action = {.fn = add_to_table_comprehension, .userdata = comprehension_var};
    scope->comprehension_action = &comp_action;
    for (ast_list_t *entry = table->entries; entry; entry = entry->next) {
        if (entry->ast->tag == Comprehension) code = Texts(code, "\n", compile_statement(scope, entry->ast));
        else code = Texts(code, compile_statement(env, add_to_table_comprehension(entry->ast, comprehension_var)));
    }
    code = Texts(code, " ", comprehension_name, "; })");
    return code;
}
}

public
Text_t compile_table_method_call(env_t *env, ast_t *ast) {
    DeclareMatch(call, ast, MethodCall);
    type_t *self_t = get_type(env, call->self);

    int64_t pointer_depth = 0;
    type_t *self_value_t = self_t;
    for (; self_value_t->tag == PointerType; self_value_t = Match(self_value_t, PointerType)->pointed)
        pointer_depth += 1;

    Text_t self = compile(env, call->self);

#define EXPECT_POINTER()                                                                                               \
    do {                                                                                                               \
        if (pointer_depth < 1) code_err(call->self, "I expected a table pointer here, not a table value");             \
        else if (pointer_depth > 1)                                                                                    \
            code_err(call->self, "I expected a table pointer here, not a nested table pointer");                       \
    } while (0)

    DeclareMatch(table, self_value_t, TableType);
    if (streq(call->name, "get")) {
        self = compile_to_pointer_depth(env, call->self, 0, false);
        arg_t *arg_spec = new (arg_t, .name = "key", .type = table->key_type);
        return Texts("Table$get_optional(", self, ", ", compile_type(table->key_type), ", ",
                     compile_type(table->value_type), ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
                     "_, ", optional_into_nonnone(table->value_type, Text("(*_)")), ", ",
                     compile_none(table->value_type), ", ", compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "get_or_set")) {
        self = compile_to_pointer_depth(env, call->self, 1, false);
        arg_t *arg_spec = new (
            arg_t, .name = "key", .type = table->key_type,
            .next = new (arg_t, .name = "default", .type = table->value_type, .default_val = table->default_value));
        return Texts("*Table$get_or_setdefault(", self, ", ", compile_type(table->key_type), ", ",
                     compile_type(table->value_type), ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
                     compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "has")) {
        self = compile_to_pointer_depth(env, call->self, 0, false);
        arg_t *arg_spec = new (arg_t, .name = "key", .type = table->key_type);
        return Texts("Table$has_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
                     compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "set")) {
        EXPECT_POINTER();
        arg_t *arg_spec = new (arg_t, .name = "key", .type = table->key_type,
                               .next = new (arg_t, .name = "value", .type = table->value_type));
        return Texts("Table$set_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
                     compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "remove")) {
        EXPECT_POINTER();
        arg_t *arg_spec = new (arg_t, .name = "key", .type = table->key_type);
        return Texts("Table$remove_value(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ", ",
                     compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "clear")) {
        EXPECT_POINTER();
        (void)compile_arguments(env, ast, NULL, call->args);
        return Texts("Table$clear(", self, ")");
    } else if (streq(call->name, "sorted")) {
        self = compile_to_pointer_depth(env, call->self, 0, false);
        (void)compile_arguments(env, ast, NULL, call->args);
        return Texts("Table$sorted(", self, ", ", compile_type_info(self_value_t), ")");
    } else if (streq(call->name, "with_fallback")) {
        self = compile_to_pointer_depth(env, call->self, 0, false);
        arg_t *arg_spec = new (arg_t, .name = "fallback", .type = Type(OptionalType, self_value_t));
        return Texts("Table$with_fallback(", self, ", ", compile_arguments(env, ast, arg_spec, call->args), ")");
    } else code_err(ast, "There is no '", call->name, "' method for tables");
}