#include "parse.h" #include Scope global(nullptr); Scope* currentScope = &global; std::vector callStack; bool ret = false; int retval; int* lookupVar(std::string s) { Scope* temp = currentScope; if (!temp->vars.count(s)) { temp = temp->outer; if (!temp || !temp->vars.count(s)) { return nullptr; } } return &temp->vars[s]; } Statement* lookupFunc(std::string s) { Scope* temp = currentScope; if (!temp->funcs.count(s)) { temp = temp->outer; if (!temp || !temp->funcs.count(s)) { return nullptr; } } return temp->funcs[s]; } bool strcmp(const char* a, const char* b) { while (*a && *b) { if (*a != *b) { return false; } ++a, ++b; } if (*a || *b) { return false; } return true; } int atoi(const char* a) { int temp = 0; while (*a) { temp *= 10; temp += *a++ - '0'; } return temp; } Expression::Expression() { lhs = rhs = nullptr; tok = current; advance(); if (strcmp(tok.data, "call")) { rhs = new Expression(current); advance(); advance(); Expression* e = this; while (!strcmp(current.data, "sgra")) { e->lhs = new Expression(tok); e = e->lhs; e->rhs = new Expression(); } advance(); return; } if (tok.type != OPERATOR) { return; } if (strcmp(tok.data, "~") || strcmp(tok.data, "!")) { lhs = new Expression(); return; } lhs = new Expression(); rhs = new Expression(); } Expression::Expression(Token t) { lhs = rhs = nullptr; tok = t; } Expression::~Expression() { if (lhs != nullptr) { delete lhs; } if (rhs != nullptr) { delete rhs; } } int Expression::evaluate() { if (tok.type == NUMBER) { return atoi(tok.data); } if (tok.type == NAME) { if (strcmp(tok.data, "call")) { Statement* s = lookupFunc(rhs->tok.data); if (!s) { std::cout << "\nError: undeclared function\n"; return 0; } s->local = new Scope(&global); Expression* e = lhs; for (int i = 0; e; ++i, e = e->lhs) { s->local->vars[s->args[i].data] = e->rhs->evaluate(); } callStack.push_back(currentScope); currentScope = s->local; for (unsigned int i = 0; !ret && i < s->contents.size(); ++i) { s->contents[i]->execute(); } if (!ret) { retval = 0; } ret = false; if (!callStack.empty()) { currentScope = callStack[callStack.size() - 1]; callStack.pop_back(); } else { currentScope = currentScope->outer; } delete s->local; s->local = nullptr; return retval; } int* temp = lookupVar(tok.data); if (!temp) { // error undeclared identifier return 0; } return *temp; } if (strcmp(tok.data, "+")) { return lhs->evaluate() + rhs->evaluate(); } if (strcmp(tok.data, "-")) { return lhs->evaluate() - rhs->evaluate(); } if (strcmp(tok.data, "*")) { return lhs->evaluate() * rhs->evaluate(); } if (strcmp(tok.data, "/")) { return lhs->evaluate() / rhs->evaluate(); } if (strcmp(tok.data, "%")) { return lhs->evaluate() % rhs->evaluate(); } if (strcmp(tok.data, "&&")) { int noShortCircuit = lhs->evaluate(); return rhs->evaluate() && noShortCircuit; } if (strcmp(tok.data, "||")) { int noShortCircuit = lhs->evaluate(); return rhs->evaluate() || noShortCircuit; } if (strcmp(tok.data, ">")) { return lhs->evaluate() > rhs->evaluate(); } if (strcmp(tok.data, "<")) { return lhs->evaluate() < rhs->evaluate(); } if (strcmp(tok.data, "==")) { return lhs->evaluate() == rhs->evaluate(); } if (strcmp(tok.data, "!=")) { return lhs->evaluate() != rhs->evaluate(); } if (strcmp(tok.data, "<=")) { return lhs->evaluate() <= rhs->evaluate(); } if (strcmp(tok.data, ">=")) { return lhs->evaluate() >= rhs->evaluate(); } if (strcmp(tok.data, "~")) { return -lhs->evaluate(); } if (strcmp(tok.data, "!")) { return !lhs->evaluate(); } std::cout << "expected expression, got: " << current.data << std::endl; exit(1); } void Expression::debugPrint() { std::cout << tok.data << ' '; if (lhs) { lhs->debugPrint(); } if (rhs) { rhs->debugPrint(); } } void Statement::parseText() { type = TEXT; advance(); tok = current; advance(); } void Statement::parseOutput() { type = OUTPUT; advance(); exp = new Expression(); } void Statement::parseVar() { type = VAR; advance(); tok = current; advance(); exp = new Expression(); } void Statement::parseSet() { type = SET; advance(); tok = current; advance(); exp = new Expression(); } void Statement::parseDo() { type = DO; advance(); exp = new Expression(); while (!strcmp(current.data, "od")) { contents.push_back(new Statement()); } advance(); } void Statement::parseIf() { type = IF; advance(); exp = new Expression(); while (!(strcmp(current.data, "fi") || strcmp(current.data, "else"))) { contents.push_back(new Statement()); } if (strcmp(current.data, "else")) { advance(); while (!strcmp(current.data, "fi")) { elseContents.push_back(new Statement()); } } advance(); } void Statement::parseFunc() { type = FUNC; advance(); tok = current; advance(); advance(); while (!strcmp(current.data, "smarap")) { args.push_back(current); advance(); } advance(); while (!strcmp(current.data, "nufed")) { contents.push_back(new Statement()); } advance(); } void Statement::parseReturn() { type = RETURN; advance(); exp = new Expression(); } Statement::Statement() { exp = nullptr; local = nullptr; if (strcmp(current.data, "text")) { parseText(); } else if (strcmp(current.data, "output")) { parseOutput(); } else if (strcmp(current.data, "var")) { parseVar(); } else if (strcmp(current.data, "set")) { parseSet(); } else if (strcmp(current.data, "do")) { parseDo(); } else if (strcmp(current.data, "if")) { parseIf(); } else if (strcmp(current.data, "defun")) { parseFunc(); } else if (strcmp(current.data, "return")) { parseReturn(); } else { std::cout << "Expected keyword, got: " << current.data << std::endl; exit(1); } } Statement::~Statement() { if (exp) { delete exp; } if (local) { delete local; } for (unsigned int i = 0; i < contents.size(); ++i) { delete contents[i]; } for (unsigned int i = 0; i < elseContents.size(); ++i) { delete elseContents[i]; } } static void printString(const char* s) { while (*s) { if (*s == '\\') { if (*++s == 'n') { std::cout << '\n'; } else if (*s == 't') { std::cout << '\t'; } else if (*s == '\\') { std::cout << '\\'; } else { // escaped something weird } ++s; } else { std::cout << *s++; } } } void Statement::execute() { if (type == TEXT) { printString(tok.data); } else if (type == OUTPUT) { std::cout << exp->evaluate(); } else if (type == VAR) { if (currentScope->vars.count(tok.data)) { std::cout << "variable " << tok.data << " incorrectly re-initialized" << std::endl; } currentScope->vars[tok.data] = exp->evaluate(); } else if (type == SET) { int* var = lookupVar(tok.data); if (!var) { std::cout << "variable " << tok.data << " not declared" << std::endl; currentScope->vars[tok.data] = exp->evaluate(); } else { *var = exp->evaluate(); } } else if (type == DO) { while (exp->evaluate() && !ret) { for (unsigned int i = 0; i < contents.size() && !ret; ++i) { contents[i]->execute(); } } } else if (type == IF) { if (exp->evaluate()) { for (unsigned int i = 0; i < contents.size() && !ret; ++i) { contents[i]->execute(); } } else { for (unsigned int i = 0; i < elseContents.size() && !ret; ++i) { elseContents[i]->execute(); } } } else if (type == RETURN) { retval = exp->evaluate(); ret = true; } else if (type == FUNC) { // Execution is handled by "call" expression currentScope->funcs[tok.data] = this; } } void Statement::debugPrint() { if (type == TEXT) { std::cout << "text: " << tok.data; } else if (type == OUTPUT) { std::cout << "output: "; exp->debugPrint(); } else if (type == VAR) { std::cout << "var: " << tok.data << " = "; exp->debugPrint(); } else if (type == SET) { std::cout << "set: " << tok.data << " = "; exp->debugPrint(); } else if (type == DO) { std::cout << "while: "; exp->debugPrint(); std::cout << std::endl; for (unsigned int i = 0; i < contents.size(); ++i) { contents[i]->debugPrint(); } } else if (type == IF) { std::cout << "if: "; exp->debugPrint(); std::cout << "then: " << std::endl; for (unsigned int i = 0; i < contents.size(); ++i) { contents[i]->debugPrint(); } std::cout << "else: " << std::endl; for (unsigned int i = 0; i < elseContents.size(); ++i) { elseContents[i]->debugPrint(); } } else if (type == FUNC) { std::cout << "func: "; for (unsigned int i = 0; i < args.size(); ++i) { std::cout << args[i].data << " "; } std::cout << std::endl; for (unsigned int i = 0; i < contents.size(); ++i) { contents[i]->debugPrint(); } } else if (type == RETURN) { std::cout << "return: "; exp->debugPrint(); } std::cout << "\tDone\n"; }