aboutsummaryrefslogtreecommitdiff
path: root/src/build.c
blob: fff565110d554762f74fd530bb876d1aac86d130 (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
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
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

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

#include <build.h>
#include <log.h>

static void write_bit(char val, FILE *file, char *byte, int *bit)
{
	if (*bit > 7) { // flush byte
		fwrite(byte, 1, 1, file);
		*byte = 0;
		*bit = 0;
	}

	if (val)
		*byte |= 1UL << (7 - *bit);
	(*bit)++;
}

static void rec_write_bblc(struct tree *tree, FILE *file, char *byte, int *bit)
{
	switch (tree->type) {
	case ABS:
		write_bit(0, file, byte, bit);
		write_bit(0, file, byte, bit);
		rec_write_bblc(tree->u.abs.term, file, byte, bit);
		break;
	case APP:
		write_bit(0, file, byte, bit);
		write_bit(1, file, byte, bit);
		write_bit(0, file, byte, bit);
		rec_write_bblc(tree->u.app.lhs, file, byte, bit);
		rec_write_bblc(tree->u.app.rhs, file, byte, bit);
		break;
	case VAR:
		for (int i = 0; i <= tree->u.var.index; i++)
			write_bit(1, file, byte, bit);
		write_bit(0, file, byte, bit);
		break;
	case REF:
		write_bit(0, file, byte, bit);
		write_bit(1, file, byte, bit);
		write_bit(1, file, byte, bit);

		// TODO: The bit-order of encoded shorts is kinda arbitrary
		short ref = tree->u.ref.index;
		for (int i = 0; i < 16; i++)
			write_bit((ref >> i) & 1, file, byte, bit);
		break;
	default:
		fatal("invalid type %d\n", tree->type);
	}
}

// writes bit-encoded blc into file
static void write_bblc(struct tree *tree, FILE *file)
{
	char byte = 0;
	int bit = 0;
	rec_write_bblc(tree, file, &byte, &bit);

	if (bit) // flush final
		fwrite(&byte, 1, 1, file);
}

void write_bloc(struct list *table, const char *path)
{
	short length = table->val;
	debug("writing bloc with %ld elements to %s\n", length, path);

	FILE *file = fopen(path, "wb");
	fwrite(BLOC_IDENTIFIER, BLOC_IDENTIFIER_LENGTH, 1, file);
	fwrite(&length, 2, 1, file);

	struct list *iterator = table;
	while (iterator) {
		write_bblc(iterator->data, file);
		iterator = iterator->next;
	}

	fclose(file);
}

static void fprint_bloc_blc(struct term *term, struct bloc_parsed *bloc,
			    FILE *file)
{
	switch (term->type) {
	case ABS:
		fprintf(file, "00");
		fprint_bloc_blc(term->u.abs.term, bloc, file);
		break;
	case APP:
		fprintf(file, "01");
		fprint_bloc_blc(term->u.app.lhs, bloc, file);
		fprint_bloc_blc(term->u.app.rhs, bloc, file);
		break;
	case VAR:
		for (int i = 0; i <= term->u.var.index; i++)
			fprintf(file, "1");
		fprintf(file, "0");
		break;
	case REF:
		if (term->u.ref.index + 1 >= bloc->length)
			fatal("invalid ref index %ld\n", term->u.ref.index);
		fprint_bloc_blc(
			bloc->entries[bloc->length - term->u.ref.index - 2],
			bloc, file);
		break;
	default:
		fatal("invalid type %d\n", term->type);
	}
}

void write_blc(struct bloc_parsed *bloc, const char *path)
{
	FILE *file = fopen(path, "wb");

	fprint_bloc_blc(bloc->entries[bloc->length - 1], bloc, file);
	fprintf(file, "\n");

	fclose(file);
}