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
|
// 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 <free.h>
#include <parse.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:
fprintf(stderr, "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;
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);
}
|