aboutsummaryrefslogtreecommitdiff
path: root/src/print.c
blob: d73f9c5ccddced0abf579fb896375ea9c71edc78 (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
62
63
64
65
66
67
68
69
70
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

#include <stdio.h>

#include <print.h>
#include <log.h>

void print_bruijn(struct term *term)
{
	switch (term->type) {
	case ABS:
		printf("[");
		print_bruijn(term->u.abs.term);
		printf("]");
		break;
	case APP:
		printf("(");
		print_bruijn(term->u.app.lhs);
		printf(" ");
		print_bruijn(term->u.app.rhs);
		printf(")");
		break;
	case VAR:
		printf("%d", term->u.var.index);
		break;
	case REF:
		printf("<%ld>", term->u.ref.index);
		break;
	default:
		fatal("invalid type %d\n", term->type);
	}
}

void print_blc(struct term *term)
{
	switch (term->type) {
	case ABS:
		printf("00");
		print_blc(term->u.abs.term);
		break;
	case APP:
		printf("01");
		print_blc(term->u.app.lhs);
		print_blc(term->u.app.rhs);
		break;
	case VAR:
		for (int i = 0; i <= term->u.var.index; i++)
			printf("1");
		printf("0");
		break;
	default:
		fatal("invalid type %d\n", term->type);
	}
}

void print_bloc(struct bloc_parsed *bloc)
{
	printf("\n=== START BLOC ===\n");
	printf("| entries:\t%ld\n", bloc->length);
	for (size_t i = 0; i < bloc->length - 1; i++) {
		printf("| entry %ld:\t", bloc->length - i - 2);
		print_bruijn(bloc->entries[i]);
		printf("\n");
	}
	printf("| final:\t");
	print_bruijn(bloc->entries[bloc->length - 1]);
	printf("\n");
	printf("=== END BLOC ===\n\n");
}