+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanAdd functions to blip5dbb718

master
10.6 KiB422 linesraw
1#include "parse.h"
2#include <iostream>
3
4Scope global(nullptr);
5Scope* currentScope = &global;
6std::vector<Scope*> callStack;
7bool ret = false;
8int retval;
9
10int* lookupVar(std::string s) {
11    Scope* temp = currentScope;
12    if (!temp->vars.count(s)) {
13        temp = temp->outer;
14        if (!temp || !temp->vars.count(s)) {
15            return nullptr;
16        }
17    }
18    return &temp->vars[s];
19}
20
21Statement* lookupFunc(std::string s) {
22    Scope* temp = currentScope;
23    if (!temp->funcs.count(s)) {
24        temp = temp->outer;
25        if (!temp || !temp->funcs.count(s)) {
26            return nullptr;
27        }
28    }
29    return temp->funcs[s];
30}
31
32bool strcmp(const char* a, const char* b) {
33    while (*a && *b) {
34        if (*a != *b) {
35            return false;
36        }
37        ++a, ++b;
38    }
39    if (*a || *b) {
40        return false;
41    }
42    return true;
43}
44
45int atoi(const char* a) {
46    int temp = 0;
47    while (*a) {
48        temp *= 10;
49        temp += *a++ - '0';
50    }
51    return temp;
52}
53
54Expression::Expression() {
55    lhs = rhs = nullptr;
56    tok = current;
57    advance();
58    if (strcmp(tok.data, "call")) {
59        rhs = new Expression(current);
60        advance();
61        advance();
62        Expression* e = this;
63        while (!strcmp(current.data, "sgra")) {
64            e->lhs = new Expression(tok);
65            e = e->lhs;
66            e->rhs = new Expression();
67        }
68        advance();
69        return;
70    }
71    if (tok.type != OPERATOR) {
72        return;
73    }
74    if (strcmp(tok.data, "~") || strcmp(tok.data, "!")) {
75        lhs = new Expression();
76        return;
77    }
78    lhs = new Expression();
79    rhs = new Expression();
80}
81
82Expression::Expression(Token t) {
83    lhs = rhs = nullptr;
84    tok = t;
85}
86
87Expression::~Expression() {
88    if (lhs != nullptr) {
89        delete lhs;
90    }
91    if (rhs != nullptr) {
92        delete rhs;
93    }
94}
95
96int Expression::evaluate() {
97    if (tok.type == NUMBER) {
98        return atoi(tok.data);
99    }
100    if (tok.type == NAME) {
101        if (strcmp(tok.data, "call")) {
102            Statement* s = lookupFunc(rhs->tok.data);
103            if (!s) {
104                std::cout << "\nError: undeclared function\n";
105                return 0;
106            }
107            s->local = new Scope(&global);
108            Expression* e = lhs;
109            for (int i = 0; e; ++i, e = e->lhs) {
110                s->local->vars[s->args[i].data] = e->rhs->evaluate();
111            }
112            callStack.push_back(currentScope);
113            currentScope = s->local;
114            for (unsigned int i = 0; !ret && i < s->contents.size(); ++i) {
115                s->contents[i]->execute();
116            }
117            if (!ret) {
118                retval = 0;
119            }
120            ret = false;
121            if (!callStack.empty()) {
122                currentScope = callStack[callStack.size() - 1];
123                callStack.pop_back();
124            } else {
125                currentScope = currentScope->outer;
126            }
127            delete s->local;
128            s->local = nullptr;
129            return retval;
130        }
131        int* temp = lookupVar(tok.data);
132        if (!temp) {
133            // error undeclared identifier
134            return 0;
135        }
136        return *temp;
137    }
138    if (strcmp(tok.data, "+")) {
139        return lhs->evaluate() + rhs->evaluate();
140    }
141    if (strcmp(tok.data, "-")) {
142        return lhs->evaluate() - rhs->evaluate();
143    }
144    if (strcmp(tok.data, "*")) {
145        return lhs->evaluate() * rhs->evaluate();
146    }
147    if (strcmp(tok.data, "/")) {
148        return lhs->evaluate() / rhs->evaluate();
149    }
150    if (strcmp(tok.data, "%")) {
151        return lhs->evaluate() % rhs->evaluate();
152    }
153    if (strcmp(tok.data, "&&")) {
154        int noShortCircuit = lhs->evaluate();
155        return rhs->evaluate() && noShortCircuit;
156    }
157    if (strcmp(tok.data, "||")) {
158        int noShortCircuit = lhs->evaluate();
159        return rhs->evaluate() || noShortCircuit;
160    }
161    if (strcmp(tok.data, ">")) {
162        return lhs->evaluate() > rhs->evaluate();
163    }
164    if (strcmp(tok.data, "<")) {
165        return lhs->evaluate() < rhs->evaluate();
166    }
167    if (strcmp(tok.data, "==")) {
168        return lhs->evaluate() == rhs->evaluate();
169    }
170    if (strcmp(tok.data, "!=")) {
171        return lhs->evaluate() != rhs->evaluate();
172    }
173    if (strcmp(tok.data, "<=")) {
174        return lhs->evaluate() <= rhs->evaluate();
175    }
176    if (strcmp(tok.data, ">=")) {
177        return lhs->evaluate() >= rhs->evaluate();
178    }
179    if (strcmp(tok.data, "~")) {
180        return -lhs->evaluate();
181    }
182    if (strcmp(tok.data, "!")) {
183        return !lhs->evaluate();
184    }
185    std::cout << "expected expression, got: " << current.data << std::endl;
186    exit(1);
187}
188
189void Expression::debugPrint() {
190    std::cout << tok.data << ' ';
191    if (lhs) {
192        lhs->debugPrint();
193    }
194    if (rhs) {
195        rhs->debugPrint();
196    }
197}
198
199void Statement::parseText() {
200    type = TEXT;
201    advance();
202    tok = current;
203    advance();
204}
205
206void Statement::parseOutput() {
207    type = OUTPUT;
208    advance();
209    exp = new Expression();
210}
211
212void Statement::parseVar() {
213    type = VAR;
214    advance();
215    tok = current;
216    advance();
217    exp = new Expression();
218}
219
220void Statement::parseSet() {
221    type = SET;
222    advance();
223    tok = current;
224    advance();
225    exp = new Expression();
226}
227
228void Statement::parseDo() {
229    type = DO;
230    advance();
231    exp = new Expression();
232    while (!strcmp(current.data, "od")) { contents.push_back(new Statement()); }
233    advance();
234}
235
236void Statement::parseIf() {
237    type = IF;
238    advance();
239    exp = new Expression();
240    while (!(strcmp(current.data, "fi") || strcmp(current.data, "else"))) {
241        contents.push_back(new Statement());
242    }
243    if (strcmp(current.data, "else")) {
244        advance();
245        while (!strcmp(current.data, "fi")) {
246            elseContents.push_back(new Statement());
247        }
248    }
249    advance();
250}
251
252void Statement::parseFunc() {
253    type = FUNC;
254    advance();
255    tok = current;
256    advance();
257    advance();
258    while (!strcmp(current.data, "smarap")) {
259        args.push_back(current);
260        advance();
261    }
262    advance();
263    while (!strcmp(current.data, "nufed")) {
264        contents.push_back(new Statement());
265    }
266    advance();
267}
268
269void Statement::parseReturn() {
270    type = RETURN;
271    advance();
272    exp = new Expression();
273}
274
275Statement::Statement() {
276    exp = nullptr;
277    local = nullptr;
278    if (strcmp(current.data, "text")) {
279        parseText();
280    } else if (strcmp(current.data, "output")) {
281        parseOutput();
282    } else if (strcmp(current.data, "var")) {
283        parseVar();
284    } else if (strcmp(current.data, "set")) {
285        parseSet();
286    } else if (strcmp(current.data, "do")) {
287        parseDo();
288    } else if (strcmp(current.data, "if")) {
289        parseIf();
290    } else if (strcmp(current.data, "defun")) {
291        parseFunc();
292    } else if (strcmp(current.data, "return")) {
293        parseReturn();
294    } else {
295        std::cout << "Expected keyword, got: " << current.data << std::endl;
296        exit(1);
297    }
298}
299
300Statement::~Statement() {
301    if (exp) {
302        delete exp;
303    }
304    if (local) {
305        delete local;
306    }
307    for (unsigned int i = 0; i < contents.size(); ++i) { delete contents[i]; }
308    for (unsigned int i = 0; i < elseContents.size(); ++i) {
309        delete elseContents[i];
310    }
311}
312
313static void printString(const char* s) {
314    while (*s) {
315        if (*s == '\\') {
316            if (*++s == 'n') {
317                std::cout << '\n';
318            } else if (*s == 't') {
319                std::cout << '\t';
320            } else if (*s == '\\') {
321                std::cout << '\\';
322            } else {
323                // escaped something weird
324            }
325            ++s;
326        } else {
327            std::cout << *s++;
328        }
329    }
330}
331
332void Statement::execute() {
333    if (type == TEXT) {
334        printString(tok.data);
335    } else if (type == OUTPUT) {
336        std::cout << exp->evaluate();
337    } else if (type == VAR) {
338        if (currentScope->vars.count(tok.data)) {
339            std::cout << "variable " << tok.data
340                      << " incorrectly re-initialized" << std::endl;
341        }
342        currentScope->vars[tok.data] = exp->evaluate();
343    } else if (type == SET) {
344        int* var = lookupVar(tok.data);
345        if (!var) {
346            std::cout << "variable " << tok.data << " not declared"
347                      << std::endl;
348            currentScope->vars[tok.data] = exp->evaluate();
349        } else {
350            *var = exp->evaluate();
351        }
352    } else if (type == DO) {
353        while (exp->evaluate() && !ret) {
354            for (unsigned int i = 0; i < contents.size() && !ret; ++i) {
355                contents[i]->execute();
356            }
357        }
358    } else if (type == IF) {
359        if (exp->evaluate()) {
360            for (unsigned int i = 0; i < contents.size() && !ret; ++i) {
361                contents[i]->execute();
362            }
363        } else {
364            for (unsigned int i = 0; i < elseContents.size() && !ret; ++i) {
365                elseContents[i]->execute();
366            }
367        }
368    } else if (type == RETURN) {
369        retval = exp->evaluate();
370        ret = true;
371    } else if (type == FUNC) {
372        // Execution is handled by "call" expression
373        currentScope->funcs[tok.data] = this;
374    }
375}
376
377void Statement::debugPrint() {
378    if (type == TEXT) {
379        std::cout << "text: " << tok.data;
380    } else if (type == OUTPUT) {
381        std::cout << "output: ";
382        exp->debugPrint();
383    } else if (type == VAR) {
384        std::cout << "var: " << tok.data << " = ";
385        exp->debugPrint();
386    } else if (type == SET) {
387        std::cout << "set: " << tok.data << " = ";
388        exp->debugPrint();
389    } else if (type == DO) {
390        std::cout << "while: ";
391        exp->debugPrint();
392        std::cout << std::endl;
393        for (unsigned int i = 0; i < contents.size(); ++i) {
394            contents[i]->debugPrint();
395        }
396    } else if (type == IF) {
397        std::cout << "if: ";
398        exp->debugPrint();
399        std::cout << "then: " << std::endl;
400        for (unsigned int i = 0; i < contents.size(); ++i) {
401            contents[i]->debugPrint();
402        }
403        std::cout << "else: " << std::endl;
404        for (unsigned int i = 0; i < elseContents.size(); ++i) {
405            elseContents[i]->debugPrint();
406        }
407    } else if (type == FUNC) {
408        std::cout << "func: ";
409        for (unsigned int i = 0; i < args.size(); ++i) {
410            std::cout << args[i].data << " ";
411        }
412        std::cout << std::endl;
413        for (unsigned int i = 0; i < contents.size(); ++i) {
414            contents[i]->debugPrint();
415        }
416    } else if (type == RETURN) {
417        std::cout << "return: ";
418        exp->debugPrint();
419    }
420    std::cout << "\tDone\n";
421}
422