summaryrefslogtreecommitdiff
path: root/cpp.c
blob: be45fe7d47fae2b5b158060f250b15547f24bdc0 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "cc.h"
#include "cpp.h"

static void dump(struct macro *list)
{
	for (struct macro *p = list; p != NULL; p = p->next) {
		if (p->file) {
			printf("%s:%ju: ", p->file, p->line);
		} else if (p->line == 0) {
			printf("<BUILTIN>: ");
		} else {
			printf("<COMMAND LINE>: ");
		}
		printf("'%s' => '%s'\n", p->identifier, p->replacement);
	}
}

static char *next_token(FILE *f, const char *fn, uintmax_t line)
{
	char *token = NULL;
	size_t tlen = 0;
	size_t pos = 0;
	int c = 0;
	while ((c = fgetc(f)) != EOF) {
		if (isspace(c)) {
			if (pos == 0) {
				return " ";
			}
			ungetc(c, f);
			return token;
		}

		if (strchr("+-*/=<>!&|[]{}()'\"", c)) {
			if (pos == 0) {
				token = calloc(2, 1);
				token[0] = c;
				return token;
			}
			ungetc(c, f);
			return token;
		}

		if (pos >= tlen) {
			tlen += 16;
			char *tmp = realloc(token, tlen);
			if (!tmp) {
				error(fn, line, "out of memory");
			}
			token = tmp;
			memset(token + pos, '\0', tlen - pos);
		}
		token[pos++] = c;
	}

	if (pos == 0) {
		return NULL;
	}

	return token;
}

int preprocess(const char *in, const char *out, struct macro *predefined)
{
	(void)in; (void)out;
		
	time_t now = time(NULL);
	struct tm *tm = localtime(&now);
	char compiletime[16] = { 0 };
	char compiledate[16] = { 0 };
	strftime(compiletime, sizeof(compiletime), "%T", tm);
	strftime(compiledate, sizeof(compiledate), "%b %e %Y", tm);

	struct macro *perfile = define(NULL, NULL, 0, "__DATE__", compiledate);
	define(perfile, NULL, 0, "__TIME__", compiletime);
	define(perfile, NULL, 0, "__FILE__", in);
	define(perfile, NULL, 0, "__LINE__", "0");

	dump(predefined);
	dump(perfile);

	FILE *f = fopen(in, "r");
	if (f == NULL) {
		error(NULL, 0, "%s: %s\n", in, strerror(errno));
	}

	char *token;
	while ((token = next_token(f, in, 0)) != NULL) {
		printf("%s\n", token);
	}

	fclose(f);

	return 0;
}

static struct macro *do_define(struct macro *list, const char *file, uintmax_t line, const char *macro, const char *replacement)
{
	struct macro *def = list;

	/* TODO: replace existing thing */
	/* TODO: prevent redefining builtins */

	if (def == NULL) {
		def = calloc(1, sizeof(*def));
		list = def;
	} else {
		while (def->next != NULL) {
			def = def->next;
		}
		def->next = calloc(1, sizeof(*def));
		def = def->next;
	}

	if (def == NULL) {
		return NULL;
	}

	if (file) {
		def->file = strdup(file);
		if (file == NULL) {
			return NULL;
		}
	}
	def->line = line;
	def->identifier = strdup(macro);
	def->replacement = strdup(replacement);
	if (def->identifier == NULL || def->replacement == NULL) {
		return NULL;
	}

	return list;
}

struct macro *define(struct macro *list, const char *file, uintmax_t line, const char *macro, const char *replacement)
{
	list = do_define(list, file, line, macro, replacement);
	if (list == NULL) {
		error(file, line, "out of memory defining macro");
	}
	return list;
}