summaryrefslogtreecommitdiff
path: root/parser.c
blob: 5b9673986034b38e0eb88d712d938a4ea856fb2d (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
#include <ctype.h>
#include <stdio.h>
#include <string.h>

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


int parse_input(char *);
int parse_module(char *);
int parse_expr(char *);

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

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

static struct op_lookup uop_handlers[] = {
	{.str = "not",   .handler = logic_nand},
};

static struct op_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}
};

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

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;
}

char *
eat_whitespace(char *string) {
	while (*string && isspace(*string)) {
		string++;
	}
	return string;
}

int parse_uop(char *str, char *name, enum BINARY (*handler)(enum BINARY, enum BINARY)) {
	char *gate_name = str;

	strtok(gate_name, " ");

	/* FIXME allow input from other gates, not just inputs */
	struct gate *in = gate_get_input_by_name(gate_name);

	gate_add(name, handler, in, in);
	return 0;


}

int parse_bop(char *str, char *name, enum BINARY (*handler)(enum BINARY, enum BINARY)) {
	char *gate_name1 = str;
	char *gate_name2 = NULL;

	strtok(gate_name1, " ");
	gate_name2 = gate_name1 + strlen(gate_name1) + 1;
	strtok(gate_name2, " ");

	/* FIXME allow input from other gates, not just inputs */
	struct gate *in1 = gate_get_input_by_name(gate_name1);
	struct gate *in2 = gate_get_input_by_name(gate_name2);

	gate_add(name, handler, in1, in2);
	return 0;
}

int
parse_op(char *str, char *name) {
	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(str+strlen(str)+1, name, 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(str+strlen(str)+1, name, 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;

	/* 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);

	return parse_op(op, ident);
}

int
parse_input(char *str) {
	strtok(str, " ");
	str = eat_whitespace(str);
	emit_info("Add input '%s'\n", str);
	gate_input_add(str);
	return 0;
}

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

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;
}