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
|
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "error.h"
#include "logic.h"
#include "wire.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 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;
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) {
printf("expression %s\n", str);
return 0;
}
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;
}
|