-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.cc
100 lines (92 loc) · 1.98 KB
/
parse.cc
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
// parser defs
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "parse.h"
parse::parse(que<char *> *toks) {
this->toks = toks;
objs = new que<Obj *>();
paren_mode = false;
root = NULL;
}
parse::~parse() {
delete toks;
}
void parse::addchild(Obj **optr, Obj *no) {
Obj *o = *optr;
if (o == NULL) {
*optr = no;
} else if (o->type == TYPE_OP) {
if (no->type == TYPE_NUM) {
// new tree is current tree's child
if (o->lchild == NULL) {
o->lchild = no; // add as child
} else if (o->rchild == NULL){
o->rchild = no; // add as child
} else {
addchild(&o->rchild, no);
}
} else {
if (o->prec < no->prec) {
// recurse to position correctly
if (o->lchild == NULL) {
o->lchild = no;
} else if (o->rchild == NULL) {
o->rchild = no;
} else {
addchild(&o->rchild, no);
}
} else {
*optr = no;
no->lchild = o;
}
}
} else if (no->type == TYPE_OP) {
// current tree is new tree's child
*optr = no;
no->lchild = o; // add as child
}
// error
}
// parens > * & / > + & -
bool parse::gentree(Obj **tree) {
char *tok;
do {
if (toks->isDone() && toks->isEmpty()){return false;} // error
toks->pop(tok);
if (tok == NULL) {break;}
#ifdef DEBUG
flock(fileno(stdout), LOCK_EX);
printf((char *) "recieved by parser: '%s'\n", tok);
flock(fileno(stdout), LOCK_UN);
#endif
Obj *o = new Obj(tok);
if (o->type == TYPE_OP && o->op == '(') {
Obj *n = NULL;
gentree(&n);
n->prec = o->prec; // assign '(' precedence to this tree
paren_mode = true;
addchild(tree, n);
paren_mode = false;
} else if (o->type == TYPE_OP && o->op == ')') {
return true;
} else {
addchild(tree, o);
}
delete tok;
} while (tok != NULL);
return true; // worked
}
void parse::parseLine() {
while (!toks->isDone() || !toks->isEmpty()) {
// memi = 0;
paren_mode = 0; // reset
if (gentree(&root)){
objs->push(root);
}
root = NULL;
}
objs->finish();
}