aboutsummaryrefslogtreecommitdiff
path: root/src/targets/unbblc.c
blob: 57bc1cfa80f64adff47526a62e4750a56adc08b6 (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
// Copyright (c) 2024, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

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

#include <target.h>
#include <parse.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;
	}

	// TODO: which endianness should be default?
	if (val)
		*byte |= 1UL << *bit;
	/* *byte |= 1UL << (7 - *bit); */
	(*bit)++;
}

static void fprint_unbblc(struct term *term, struct bloc_parsed *bloc,
			  FILE *file, char *byte, int *bit)
{
	switch (term->type) {
	case ABS:
		write_bit(0, file, byte, bit);
		write_bit(0, file, byte, bit);
		fprint_unbblc(term->u.abs.term, bloc, file, byte, bit);
		break;
	case APP:
		write_bit(0, file, byte, bit);
		write_bit(1, file, byte, bit);
		fprint_unbblc(term->u.app.lhs, bloc, file, byte, bit);
		fprint_unbblc(term->u.app.rhs, bloc, 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;
	case REF:
		if (term->u.ref.index + 1 >= bloc->length)
			fatal("invalid ref index %ld\n", term->u.ref.index);
		fprint_unbblc(
			bloc->entries[bloc->length - term->u.ref.index - 2],
			bloc, file, byte, bit);
		break;
	default:
		fatal("invalid type %d\n", term->type);
	}
}

static void write_unbblc(struct bloc_parsed *bloc, FILE *file)
{
	char byte = 0;
	int bit = 0;
	fprint_unbblc(bloc->entries[bloc->length - 1], bloc, file, &byte, &bit);

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

struct target_spec target_unbblc = {
	.name = "unbblc",
	.exec = write_unbblc,
};