blob: aa7c17b92664c31791a56c2c5efbc375662d332b (
plain) (
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
|
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <log.h>
#include <term.h>
struct term *term_new(term_type_t type)
{
struct term *term = malloc(sizeof(*term));
if (!term)
fatal("out of memory!\n");
term->type = type;
return term;
}
void term_free(struct term *term)
{
switch (term->type) {
case ABS:
term_free(term->u.abs.term);
free(term);
break;
case APP:
term_free(term->u.app.lhs);
term_free(term->u.app.rhs);
free(term);
break;
case VAR:
free(term);
break;
default:
fatal("invalid type %d\n", term->type);
}
}
void term_print(struct term *term)
{
switch (term->type) {
case ABS:
fprintf(stderr, "[");
term_print(term->u.abs.term);
fprintf(stderr, "]");
break;
case APP:
fprintf(stderr, "(");
term_print(term->u.app.lhs);
fprintf(stderr, " ");
term_print(term->u.app.rhs);
fprintf(stderr, ")");
break;
case VAR:
fprintf(stderr, "%ld", term->u.var.index);
break;
default:
fatal("invalid type %d\n", term->type);
}
}
|