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
|
// 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 term *term, FILE *file, char *byte, int *bit)
{
switch (term->type) {
case ABS:
write_bit(0, file, byte, bit);
write_bit(0, file, byte, bit);
rec_write_bblc(term->u.abs.term, file, byte, bit);
break;
case APP:
write_bit(0, file, byte, bit);
write_bit(1, file, byte, bit);
rec_write_bblc(term->u.app.lhs, file, byte, bit);
rec_write_bblc(term->u.app.rhs, file, byte, bit);
break;
case VAR:
for (int i = 0; i <= term->u.var.index; i++)
write_bit(1, file, byte, bit);
write_bit(0, file, byte, bit);
break;
default:
fprintf(stderr, "Invalid type %d\n", term->type);
}
}
// writes bit-encoded blc into file
static void write_bblc(struct term *term, FILE *file)
{
char byte = 0;
int bit = 0;
rec_write_bblc(term, file, &byte, &bit);
if (bit) // flush final
fwrite(&byte, 1, 1, file);
}
void write_bloc(struct term *term, const char *path)
{
(void)term; // TODO
// example data
short length = 2;
struct term *M = parse_blc("0000000001100111100111100111100111011110");
struct term *N = parse_blc("00000000011001111001111001100111011110");
FILE *file = fopen(path, "wb");
fwrite(BLOC_IDENTIFIER, BLOC_IDENTIFIER_LENGTH, 1, file);
fwrite(&length, 2, 1, file);
write_bblc(M, file);
write_bblc(N, file);
// TODO
char byte = 0;
int bit = 0;
write_bit(0, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
for (int i = 0; i < 16; i++)
write_bit(0, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
for (int i = 0; i < 16; i++)
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
for (int i = 0; i < 16; i++)
write_bit(0, file, &byte, &bit);
write_bit(1, file, &byte, &bit);
for (int i = 0; i < 16; i++)
write_bit(0, file, &byte, &bit);
if (bit) // flush final
fwrite(&byte, 1, 1, file);
fclose(file);
free_term(M);
free_term(N);
}
|