summaryrefslogtreecommitdiff
path: root/parser.c
blob: f3fe38df3fc4abd6425e579176789cea8992bf30 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "common.h"
#include "error.h"
#include "logic.h"
#include "gate.h"

enum STATEMENT_TYPE {
	DECL_EXPR,
	DECL_INPUT,
	DECL_OUTPUT,
	MODULE_NAME
};

enum UNARY_OPERATOR {
	UOP_NOT
};

enum BINARY_OPERATOR {
	BOP_AND,
	BOR_OR,
	BOP_NAND,
	BOP_NOR,
	BOP_XOR
};

struct tok_lookup {
	char *str;
	int (*handler)(char*);
};

struct bop_lookup {
	char *str;
	enum BINARY (*handler)(enum BINARY, enum BINARY);
};

struct uop_lookup {
	char *str;
	enum BINARY (*handler)(enum BINARY);
};

static struct uop_lookup uop_handlers[] = {
	{.str = "not",   .handler = logic_not},
};

static struct bop_lookup bop_handlers[] = {
	{.str = "and",   .handler = logic_and},
	{.str = "or",    .handler = logic_or},
	{.str = "nand",  .handler = logic_nand},
	{.str = "nor",   .handler = logic_nor},
	{.str = "xor",   .handler = logic_xor}
};

int
expect(const char *expect, char *actual) {
	int min_len = 0;

	min_len = MIN(strlen(expect), strlen(actual));

	if (strncmp(expect, actual, min_len) != 0) {
		emit_error("Expected '%s' at start of '%s'\n", expect, actual);
		return 1;
	}
	return 0;
}


int parse_uop(char *str, enum BINARY (*handler)(enum BINARY)) {
	/**/
	(void)str;
	(void)handler;
	emit_error("uop unimplemented\n");
	return 0;
}

int parse_bop(char *str, enum BINARY (*handler)(enum BINARY, enum BINARY)) {
	/**/
	(void)str;
	(void)handler;
	emit_error("bop unimplemented\n");
	return 0;
}

int
parse_op(char *str) {
	int match = 0;
	size_t i = 0;

	strtok(str, " ");
	match = 0;
	for (i = 0; i < sizeof(bop_handlers)/sizeof(bop_handlers[0]); i++) {
		if (strcmp(bop_handlers[i].str, str) == 0) {
			match = 1;
			if (parse_bop("FIXME", bop_handlers[i].handler)) {
				return 1;
			}
		}
	}
	for (i = 0; i < sizeof(uop_handlers)/sizeof(uop_handlers[0]); i++) {
		if (strcmp(uop_handlers[i].str, str) == 0) {
			match = 1;
			if (parse_uop("FIXME", uop_handlers[i].handler)) {
				return 1;
			}
		}
	}
	if (match == 0) {
		emit_error("Invalid operator \"%s\"\n", str);
		return 1;
	}
	return 0;
}

int
parse_expr(char *str) {
	const char *expr_sep = ": ";
	size_t ident_len = 0;
	char *ident = NULL;
	char *op = NULL;

	printf("expression %s\n", str);

	/* FIXME magic constant string */
	ident_len = strspn(str, "abcdefghijklmnopqrstuvwxyz0123456789");

	if (expect(expr_sep, str + ident_len)) {
		return -1;
	}

	str[ident_len] = '\0';
	ident = str;
	op = str + ident_len + strlen(expr_sep);

	/* FIXME do something with the identifier */

	return parse_op(op);
}

int
parse_node(char *str) {
	enum NODE_TYPE type = NODE_OUTPUT;

	strtok(str, " ");
	if (strcmp(str, "output") == 0) {
		type = NODE_OUTPUT;
	} else if (strcmp(str, "input") == 0) {
		type = NODE_INPUT;
	} else {
		emit_error("'%s' is not a valid node type\n", str);
		return 1;
	}

	printf("FIXME need to add node of type %s\n", type == NODE_OUTPUT ? "oot" : "innnn");
	return 0;
}

int
parse_module(char *str) {
	printf("FIXME module name is \"%s\" but modules are not implemented\n", str);
	return 0;
}




static struct tok_lookup tok_handlers[] = {
	{.str = "node",   .handler = parse_node},
	{.str = "module", .handler = parse_module},
	{.str = "expr",   .handler = parse_expr}
};





int
parse_line(char *line) {
	size_t i = 0;
	char *tok = line;
	char *delim = " ";
	int match = 0;

	if (   strlen(line) == 0
	    || (strlen(line) == 1 && isspace(line[0]))) {
		return 0;
	}

	tok = strtok(line, delim);

	match = 0;
	for (i = 0; i < sizeof(tok_handlers)/sizeof(tok_handlers[0]); i++) {
		if (strcmp(tok_handlers[i].str, tok) == 0) {
			match = 1;
			if ((tok_handlers[i].handler)(&tok[strlen(tok)+1])) {
				return 1;
			}
		}
	}

	if (match == 0) {
		emit_error("invalid token \"%s\"", tok);
		return 1;
	}
	return 0;
}

int main(void) {
	char buf[4096];
	FILE *fd = stdin;

	while (NULL != fgets(buf, sizeof(buf), fd)) {
		buf[strcspn(buf, "\r\n")] = '\0';
		if (parse_line(buf)) {
			return 1;
		}
	}
	return 0;
}