git.haldean.org ubik / e2d2d42 libubik / codegen / assign.c
e2d2d42

Tree @e2d2d42 (Download .tar.gz)

assign.c @e2d2d42raw · history · blame

  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * assign.c: node assignment
 * Copyright (C) 2016, Haldean Brown
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <string.h>

#include "ubik/assert.h"
#include "ubik/assign.h"
#include "ubik/feedback.h"
#include "ubik/fun.h"
#include "ubik/resolve.h"
#include "ubik/rt.h"
#include "ubik/rttypes.h"
#include "ubik/streamutil.h"
#include "ubik/uri.h"
#include "ubik/value.h"
#include "ubik/vector.h"

no_ignore static ubik_error
_add_nontotal_predicate_err(
        struct ubik_assign_context *ctx,
        struct ubik_ast_loc loc)
{
        struct ubik_assign_error *res;
        ubik_error err;

        ubik_alloc1(&res, struct ubik_assign_error, ctx->region);
        res->err_type = ASSIGN_ERR_PRED_BLOCK_NOT_TOTAL;
        res->loc = loc;

        err = ubik_vector_append(&ctx->errors, res);
        if (err != OK)
                return err;
        return OK;
}

no_ignore static ubik_error
_set_name_in_scope(
        struct ubik_resolve_scope *scope,
        char *name,
        ubik_word node)
{
        struct ubik_resolve_name *bind;
        size_t i;
        bool found;

        bind = NULL;
        found = false;

        for (i = 0; i < scope->names.n && !found; i++)
        {
                bind = scope->names.elems[i];
                if (strcmp(name, bind->name) == 0)
                        found = true;
        }

        if (!found)
                return ubik_raise(ERR_ABSENT, "name not found in scope");

        bind->node = node;
        return OK;
}

no_ignore static ubik_error
_find_name_in_scope(
        ubik_word *n,
        struct ubik_resolve_scope *scope,
        char *name)
{
        struct ubik_resolve_name *bind;
        size_t i;
        bool found;

        found = false;

        do
        {
                for (i = 0; i < scope->names.n && !found; i++)
                {
                        bind = scope->names.elems[i];
                        if (strcmp(name, bind->name) == 0)
                                found = true;
                }
                scope = scope->parent;
        }
        while (!found && scope != NULL);

        if (!found)
                return ubik_raise(ERR_ABSENT, "name not found in scope");

        *n = bind->node;
        return OK;
}

no_ignore static ubik_error
_assign_atom_node(
        struct ubik_assign_context *ctx,
        struct ubik_node *n,
        struct ubik_ast_expr *expr)
{
        ubik_word referrent;
        enum ubik_resolve_type res_type;
        char *pkg;
        ubik_error err;

        switch (expr->atom->atom_type)
        {
        case ATOM_NUM:
                n->node_type = UBIK_VALUE;

                err = ubik_value_new(&n->value.type, ctx->workspace);
                if (err != OK)
                        return err;
                err = ubik_type_rat(n->value.type);
                if (err != OK)
                        return err;

                err = ubik_value_new(&n->value.value, ctx->workspace);
                if (err != OK)
                        return err;
                n->value.value->type = UBIK_RAT;
                n->value.value->rat = expr->atom->number;
                return OK;

        case ATOM_VALUE:
                n->node_type = UBIK_VALUE;

                err = ubik_value_new(&n->value.type, ctx->workspace);
                if (err != OK)
                        return err;
                /* TODO: type these! */
                err = ubik_type_rat(n->value.type);
                if (err != OK)
                        return err;

                n->value.value = expr->atom->value;
                return OK;

        case ATOM_TYPE_NAME:
        case ATOM_NAME:
                res_type = expr->atom->name_loc->type;

                if (res_type == RESOLVE_GLOBAL || res_type == RESOLVE_NATIVE)
                {
                        n->node_type = UBIK_LOAD;

                        ubik_galloc1(&n->load.loc, struct ubik_uri);
                        if (res_type == RESOLVE_NATIVE)
                                err = ubik_uri_native(
                                        n->load.loc, expr->atom->str);
                        else
                        {
                                pkg = expr->atom->name_loc->package_name;
                                ubik_assert(pkg != NULL);
                                err = ubik_uri_package(
                                        n->load.loc, pkg, expr->atom->str);
                        }
                        if (err != OK)
                        {
                                free(n->load.loc);
                                return err;
                        }
                        return OK;
                }

                referrent = UBIK_INVALID_NODE_ID;
                err = _find_name_in_scope(
                        &referrent, expr->scope, expr->atom->str);
                if (err != OK)
                        return err;
                if (referrent == UBIK_INVALID_NODE_ID)
                        return ubik_raise(ERR_ABSENT, "no node set on name");

                n->node_type = UBIK_REF;
                n->ref.referrent = referrent;
                return OK;

        case ATOM_QUALIFIED:
                n->node_type = UBIK_LOAD;

                ubik_galloc1(&n->load.loc, struct ubik_uri);

                err = ubik_uri_package(
                        n->load.loc,
                        expr->atom->qualified.head,
                        expr->atom->qualified.tail);
                if (err != OK)
                {
                        free(n->load.loc);
                        return err;
                }

                return OK;

        case ATOM_STRING:
                n->node_type = UBIK_VALUE;

                err = ubik_value_new(&n->value.type, ctx->workspace);
                if (err != OK)
                        return err;
                err = ubik_type_str(n->value.type);
                if (err != OK)
                        return err;

                err = ubik_value_new(&n->value.value, ctx->workspace);
                if (err != OK)
                        return err;
                n->value.value->type = UBIK_STR;
                n->value.value->str.length = strlen(expr->atom->str);
                n->value.value->str.data = strdup(expr->atom->str);
                return OK;
        }
        return ubik_raise(ERR_UNKNOWN_TYPE, "compile atom type");
}

no_ignore static ubik_error
_assign_apply_node(
        struct ubik_assign_context *ctx,
        struct ubik_node *n,
        struct ubik_vector *nodes,
        struct ubik_ast_expr *expr)
{
        struct ubik_ast_expr *head;
        struct ubik_node *argref;
        ubik_error err;

        if (expr->apply.recursive_app)
        {
                head = expr->apply.head;
                while (head->expr_type == EXPR_APPLY)
                        head = head->apply.head;
                ubik_assert(head->expr_type == EXPR_LAMBDA);

                ubik_alloc1(&argref, struct ubik_node, ctx->region);
                argref->node_type = UBIK_REF;
                argref->id = nodes->n;
                argref->ref.referrent = head->gen;
                err = ubik_vector_append(nodes, argref);
                if (err != OK)
                        return err;

                expr->apply.tail->gen = argref->id;
        }

        n->node_type = UBIK_APPLY;
        n->apply.func = expr->apply.head->gen;
        n->apply.arg = expr->apply.tail->gen;
        return OK;
}

no_ignore static ubik_error
_assign_pred_case(
        struct ubik_assign_context *ctx,
        struct ubik_vector *nodes,
        struct ubik_ast_case *case_stmt)
{
        struct ubik_node *n;
        ubik_error err;

        ubik_alloc1(&n, struct ubik_node, ctx->region);

        if (case_stmt->next != NULL)
        {
                err = _assign_pred_case(ctx, nodes, case_stmt->next);
                if (err != OK)
                        goto failed;

                err = ubik_assign_nodes(ctx, nodes, case_stmt->head);
                if (err != OK)
                        goto failed;

                err = ubik_assign_nodes(ctx, nodes, case_stmt->tail);
                if (err != OK)
                        goto failed;

                n->node_type = UBIK_COND;
                n->cond.condition = case_stmt->head->gen;
                n->cond.if_true = case_stmt->tail->gen;
                n->cond.if_false = case_stmt->next->gen;
        }
        else
        {
                /* This is a user error but we still have enough information to
                 * go ahead and generate code. We rely on the caller to pay
                 * attention to the errors present in the context after
                 * assignment is complete. */
                if (case_stmt->head != NULL)
                {
                        err = _add_nontotal_predicate_err(ctx, case_stmt->loc);
                        if (err != OK)
                                goto failed;
                }
                err = ubik_assign_nodes(ctx, nodes, case_stmt->tail);
                if (err != OK)
                        goto failed;

                n->node_type = UBIK_REF;
                n->ref.referrent = case_stmt->tail->gen;
        }

        n->id = nodes->n;
        err = ubik_vector_append(nodes, n);
        if (err != OK)
                goto failed;
        case_stmt->gen = n->id;

        return OK;

failed:
        free(n);
        return err;
}

no_ignore static ubik_error
_assign_pred_block(
        struct ubik_assign_context *ctx,
        struct ubik_node *n,
        struct ubik_vector *nodes,
        struct ubik_ast_expr *expr)
{
        ubik_error err;

        err = _assign_pred_case(ctx, nodes, expr->cond_block.case_stmts);
        if (err != OK)
                return err;

        n->node_type = UBIK_REF;
        n->id = nodes->n;
        n->ref.referrent = expr->cond_block.case_stmts->gen;

        return OK;
}

no_ignore static ubik_error
_assign_block(
        struct ubik_assign_context *ctx,
        struct ubik_node *n,
        struct ubik_vector *nodes,
        struct ubik_ast_expr *expr)
{
        size_t i;
        struct ubik_ast_binding *bind;
        struct ubik_node *subnode;
        ubik_error err;
        ubik_word node_index;

        if (expr->block->types.n != 0)
                return ubik_raise(ERR_NOT_IMPLEMENTED, "scoped types");

        /* first set all of the binding nodes to REF nodes, so that things
         * can point at the definitions of names before the names actually
         * exist. This also allows for circular references between binding
         * expressions. */
        for (i = 0; i < expr->block->bindings.n; i++)
        {
                bind = expr->block->bindings.elems[i];

                ubik_alloc1(&subnode, struct ubik_node, ctx->region);
                subnode->node_type = UBIK_REF;
                subnode->id = nodes->n;
                /* leave referrent set to an invalid value; we'll set it after
                   we generate a node for this name. */
                subnode->ref.referrent = UBIK_INVALID_NODE_ID;

                err = ubik_vector_append(nodes, subnode);
                if (err != OK)
                        return err;

                err = _set_name_in_scope(
                        expr->block->scope,
                        bind->name,
                        subnode->id);
                if (err != OK)
                        return err;
        }

        for (i = 0; i < expr->block->bindings.n; i++)
        {
                bind = expr->block->bindings.elems[i];
                err = ubik_assign_nodes(ctx, nodes, bind->expr);
                if (err != OK)
                        return err;

                node_index = 0;
                err = _find_name_in_scope(
                        &node_index, expr->block->scope, bind->name);
                if (err != OK)
                        return err;
                subnode = (struct ubik_node *) nodes->elems[node_index];
                subnode->ref.referrent = bind->expr->gen;
        }

        err = ubik_assign_nodes(ctx, nodes, expr->block->immediate);
        if (err != OK)
                return err;
        n->node_type = UBIK_REF;
        n->ref.referrent = expr->block->immediate->gen;
        return OK;
}

no_ignore static ubik_error
_assign_lambda(
        struct ubik_assign_context *ctx,
        struct ubik_node *n,
        struct ubik_ast_expr *expr)
{
        struct ubik_value *subgraph;
        struct ubik_ast_arg_list *t;
        struct ubik_node *input_node;
        struct ubik_vector subnodes = {.region = ctx->region};
        size_t i;
        ubik_error err;

        t = expr->lambda.args;
        i = 0;
        for (i = 0, t = expr->lambda.args;
             t != NULL && t->name != NULL;
             i++, t = t->next)
        {
                ubik_alloc1(&input_node, struct ubik_node, ctx->region);
                input_node->node_type = UBIK_INPUT;
                input_node->id = subnodes.n;
                input_node->input.arg_num = i;

                err = ubik_vector_append(&subnodes, input_node);
                if (err != OK)
                        return err;
                t->gen = input_node->id;

                err = _set_name_in_scope(expr->scope, t->name, t->gen);
                if (err != OK)
                        return err;
        }

        err = ubik_assign_nodes(ctx, &subnodes, expr->lambda.body);
        if (err != OK)
                return err;

        err = ubik_value_new(&subgraph, ctx->workspace);
        if (err != OK)
                return err;
        ubik_fun_from_vector(subgraph, &subnodes, expr->lambda.body->gen);

        n->node_type = UBIK_VALUE;
        n->value.value = subgraph;

        /* TODO: lambda type here. */
        err = ubik_value_new(&n->value.type, ctx->workspace);
        if (err != OK)
                return err;

        return OK;
}

no_ignore ubik_error
ubik_assign_nodes(
        struct ubik_assign_context *ctx,
        struct ubik_vector *nodes,
        struct ubik_ast_expr *expr)
{
        struct ubik_node *n;
        ubik_error err;

        ubik_alloc1(&n, struct ubik_node, ctx->region);

        switch (expr->expr_type)
        {
        case EXPR_ATOM:
                err = _assign_atom_node(ctx, n, expr);
                if (err != OK)
                        return err;
                break;

        case EXPR_APPLY:
                err = ubik_assign_nodes(ctx, nodes, expr->apply.head);
                if (err != OK)
                        return err;

                err = ubik_assign_nodes(ctx, nodes, expr->apply.tail);
                if (err != OK)
                        return err;

                err = _assign_apply_node(ctx, n, nodes, expr);
                if (err != OK)
                        return err;
                break;

        case EXPR_LAMBDA:
                err = _assign_lambda(ctx, n, expr);
                if (err != OK)
                        return err;
                break;

        case EXPR_BLOCK:
                err = _assign_block(ctx, n, nodes, expr);
                if (err != OK)
                        return err;
                break;

        case EXPR_COND_BLOCK:
                switch (expr->cond_block.block_type)
                {
                case COND_PREDICATE:
                        err = _assign_pred_block(ctx, n, nodes, expr);
                        if (err != OK)
                                return err;
                        break;
                case COND_PATTERN:
                        err = ubik_raise(
                                ERR_UNEXPECTED_FAILURE,
                                "pattern blocks should have been transformed");
                        return err;
                }
                break;

        default:
                err = ubik_raise(ERR_UNKNOWN_TYPE, "compile assign node");
                return err;
        }

        n->id = nodes->n;
        err = ubik_vector_append(nodes, n);
        if (err != OK)
                return err;
        expr->gen = n->id;

        return OK;
}

bool
ubik_assign_emit_errors(struct ubik_assign_context *ctx)
{
        size_t i;
        struct ubik_assign_error *ass_err;

        for (i = 0; i < ctx->errors.n; i++)
        {
                ass_err = ctx->errors.elems[i];
                switch (ass_err->err_type)
                {
                case ASSIGN_ERR_PRED_BLOCK_NOT_TOTAL:
                        ubik_feedback_line(
                                ctx->feedback,
                                UBIK_FEEDBACK_ERR,
                                &ass_err->loc,
                                "last case in predicate block must have an "
                                "empty head");
                        break;
                }
        }

        return ctx->errors.n != 0;
}

void
ubik_assign_context_free(struct ubik_assign_context *ctx)
{
        unused(ctx);
}