-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.c
More file actions
176 lines (154 loc) · 4.4 KB
/
rule.c
File metadata and controls
176 lines (154 loc) · 4.4 KB
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
#include "rule.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "exit_codes.h"
void printrule(const rule_str_t *rule) {
printf("Rule:: In %s, read %s: write %s, move %d, enter %s\n",
rule->from_state, rule->from_symbol, rule->to_symbol, rule->direction,
rule->to_state);
}
void free_rule(const rule_str_t *rule) {
if (rule->from_state) free(rule->from_state);
if (rule->from_symbol) free(rule->from_symbol);
if (rule->to_symbol) free(rule->to_symbol);
if (rule->to_state) free(rule->to_state);
}
char *next_piece(char *s, char **lscan, char **rscan) {
while (isspace(**lscan)) (*lscan)++;
if (!**lscan) return NULL;
*rscan = *lscan;
while (!isspace(*++*rscan))
if (!**rscan) break;
int piece_len = *rscan - *lscan;
char *res = malloc(piece_len + 1);
memcpy(res, *lscan, piece_len);
res[piece_len] = 0;
*lscan = *rscan;
// printf("\tHave a piece! [%s]\n", res);
return res;
}
int is_empty(char *s) {
if (*s == ';') return 1; // comments are 'empty'
if (!isspace(*s)) return 0;
char *scan = s;
while (*++scan)
if (!isspace(*scan)) return *scan == ';'; // allow comments with spaces before the `;`
return 1;
}
int has_gte_n_fields(char *s, unsigned int n) {
unsigned int num_fields = 0;
char prev_c = ' ';
char *scan = s;
while (*scan) {
if (isspace(*scan) && !isspace(prev_c)) num_fields++;
if (num_fields == n) return 1;
prev_c = *(scan++);
}
return 0;
}
enum rule_parse_result {
PARSE_RULE_AOK,
PARSE_RULE_EMPTY,
PARSE_ERR_MULTICHAR_SYMBOL,
PARSE_ERR_EARLY_EOL,
PARSE_ERR_BAD_DIR
};
enum rule_parse_result storule(char *s, rule_str_t *rule) {
// printf("Parse this!: %s", s);
if (is_empty(s)) return PARSE_RULE_EMPTY;
if (!has_gte_n_fields(s, 5)) return PARSE_ERR_EARLY_EOL;
// printf("...okay...\n");
char *lscan = s;
char *rscan = s;
char *piece;
piece = next_piece(s, &lscan, &rscan);
rule->from_state = piece;
piece = next_piece(s, &lscan, &rscan);
rule->from_symbol = piece;
if (piece[1]) return PARSE_ERR_MULTICHAR_SYMBOL;
piece = next_piece(s, &lscan, &rscan);
rule->to_symbol = piece;
if (piece[1]) return PARSE_ERR_MULTICHAR_SYMBOL;
piece = next_piece(s, &lscan, &rscan);
char dir_char = *piece;
char second_char = piece[1];
free(piece);
if (second_char) return PARSE_ERR_BAD_DIR;
switch (dir_char) {
case 'l':
rule->direction = LEFT;
break;
case '*':
rule->direction = STAY;
break;
case 'r':
rule->direction = RIGHT;
break;
default:
return PARSE_ERR_BAD_DIR;
}
piece = next_piece(s, &lscan, &rscan);
rule->to_state = piece;
// printf("Done parsing: %s", s);
return PARSE_RULE_AOK;
}
const char *rule_parse_err_string(enum rule_parse_result code) {
switch (code) {
case PARSE_RULE_AOK:
return "No error";
case PARSE_ERR_MULTICHAR_SYMBOL:
return "Attempted symbol has multiple characters";
case PARSE_ERR_EARLY_EOL:
return "Reached end of line early";
case PARSE_ERR_BAD_DIR:
return "Attempted direction is not in \"lr*\"";
default:
return "FIXME: No string for that code";
}
}
rule_node *new_rule_node() {
rule_node *node = malloc(sizeof(rule_node));
node->next = NULL;
node->rule.from_state = NULL;
node->rule.from_symbol = NULL;
node->rule.to_symbol = NULL;
node->rule.to_state = NULL;
}
void free_rule_nodes(rule_node *head) {
if (head->next) free_rule_nodes(head->next);
free_rule(&head->rule);
free(head);
}
const int LINE_BUF_SIZE = 255;
rule_node *read_rules(FILE *input) {
rule_str_t rule;
enum rule_parse_result parse_result;
rule_node *rules_head = new_rule_node();
rule_node *curr_node = rules_head, *end_node = NULL;
unsigned int linenum = 0;
char *line_buf = malloc(LINE_BUF_SIZE);
while (fgets(line_buf, LINE_BUF_SIZE, input)) {
linenum++;
parse_result = storule(line_buf, &(curr_node->rule));
if (parse_result == PARSE_RULE_AOK) {
curr_node->next = new_rule_node();
end_node = curr_node;
curr_node = curr_node->next;
} else if (parse_result == PARSE_RULE_EMPTY) {
continue;
} else {
printf("Error: %s\n\ton line %d: %s\n",
rule_parse_err_string(parse_result), linenum, line_buf);
exit(EXIT_PARSE_ERR);
}
}
free(line_buf);
free_rule_nodes(curr_node);
if (end_node) {
end_node->next = NULL;
} else {
rules_head = NULL;
}
return rules_head;
}