~swisschili/toolchain-6502

ref: c382994c73972918bc7723b5b7836c74e8e92bd6 toolchain-6502/as/main.c -rw-r--r-- 1.1 KiB
c382994cswissChili Start work on preprocessor 5 months ago
                                                                                
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
#include "as.h"
#include "map.h"

#include <stdio.h>
#include <stdlib.h>
#include <bits/getopt_core.h>
#include <unistd.h>

// TODO: handle all the possible IO errors
int main(int argc, char **argv)
{
	char c;
	FILE *in = stdin,
		*out = stdout;

	while ((c = getopt(argc, argv, "i:o:")) != -1)
	{
		switch (c)
		{
		case 'i':
			in = fopen(optarg, "r");
			break;
		case 'o':
			out = fopen(optarg, "w");
			break;
		case 'h':
		case '?':
			printf("6502 assembler\n"
				   "Usage:\n"
				   "    -i <input> set input file (default stdin)\n"
				   "    -o <output> set output file (default stdout)\n");
		}
	}

	fseek(in, 0, SEEK_END);
	ssize_t len = ftell(in);
	fseek(in, 0, SEEK_SET);

	char *text = malloc(len + 1);
	fread(text, len, 1, in);
	text[len] = 0;

	FILE *temp = tmpfile();

	map_t *macros = new_map();
	uint32_t processed = preproc(text, temp, macros, 0);
	free_map_items(macros);

	fseek(temp, 0, SEEK_END);
	ssize_t temp_len = ftell(in);
	fseek(temp, 0, SEEK_SET);

	char *processed_text = malloc(len + 1);
	fread(processed_text, len, 1, in);
	text[temp_len] = 0;
	
	uint32_t built = assemble(processed_text, out);

	free(text);
	free(processed_text);
}