~swisschili/toolchain-6502

ref: c382994c73972918bc7723b5b7836c74e8e92bd6 toolchain-6502/as/pp.c -rw-r--r-- 1.3 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "as.h"
#include "map.h"

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

enum /* State */
{
	NORMAL,			/* Default State */
	MACRO_DEF		/* In Macro Definition */
};

enum /* Flags */
{
	PRESERVE_MACRO_ARGS,
};

int preproc(char *in, FILE *out, map_t *macros, int flags)
{
	int line_no = 1,
		err = 0,
		state = NORMAL;
	char *line = strtok_fix(in, "\n"),
		*macro_name = NULL;

	while (line)
	{
		skip_ws(&line);

		if (*line == '%')
		{
			// Line is preprocessor directive
			
			char *directive = parse_label_name(&line);
			if (!directive)
			{
				fprintf(stderr, ERR "Expected preprocessor directive on line %d\n" RESET,
						line_no);
				err = EINVAL;
				goto cleanup;
			}
			else
			{
				if (!strcasecmp(directive, "macro"))
				{
					skip_ws(&line);
					macro_name = parse_label_name(&line);
					if (!macro_name)
					{
						fprintf(stderr, ERR "Expected name after %%macro on line %d\n" RESET,
								line_no);
						err = EINVAL;
						goto cleanup;
					}

					if (!ws_end(&line))
					{
						fprintf(stderr, ERR "Expected new line after macro definition\n" RESET);
						err = EINVAL;
						goto cleanup;
					}

					state = MACRO_DEF;
				}
			}
		}

		fprintf(out, "%s\n", line);
		
		line = strtok_fix(NULL, "\n");
		line_no++;
	}
	
cleanup:
	return err;
}