-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
83 lines (65 loc) · 1.54 KB
/
parse.c
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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "ast.h"
#include "parse.h"
#include "list.h"
//THIS FILE WAS TAKEN FROM LECTURE 5 CALCULATOR CODE WITH LITTLE MODIFICATIONS
int
streq(const char* aa, const char* bb)
{
return strcmp(aa, bb) == 0;
}
int
find_first_index(list* toks, const char* tt)
{
int ii = 0;
for (list* it = toks; it; it = it->tail) {
if (streq(it->head, tt)) {
return ii;
}
ii++;
}
return -1;
}
int
contains(list* toks, const char* tt)
{
return find_first_index(toks, tt) >= 0;
}
list*
slice(list* xs, int i0, int i1)
{
list* ys = 0;
list* it = xs;
for (int ii = 0; ii < i0; ++ii) {
it = it->tail;
}
for (int ii = i0; ii < i1; ++ii) {
ys = cons(it->head, ys);
it = it->tail;
}
return rev_free(ys);
}
calc_ast*
parse(list* toks)
{
char* ops[] = {";", "||", "&", "&&", "|", "<", ">", "$", "="};
//check if contains any of the operators in the string
for (int ii = 0; ii < 9; ++ii) {
char* op = ops[ii];
//TODO potential memory leak
//free_list(ys)
if (contains(toks, op)) {
int jj = find_first_index(toks, op);
list* xs = slice(toks, 0, jj);
list* ys = slice(toks, jj + 1, length(toks));
calc_ast* ast = make_ast_op(op, parse(xs), parse(ys));
return ast;
}
}
// if it only a single value
return make_ast_value(toks);
fprintf(stderr, "Invalid token stream");
exit(1);
}