diff options
Diffstat (limited to 'src/PolishNotationParser.cpp')
| -rw-r--r-- | src/PolishNotationParser.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/PolishNotationParser.cpp b/src/PolishNotationParser.cpp index f99f404..22905a6 100644 --- a/src/PolishNotationParser.cpp +++ b/src/PolishNotationParser.cpp @@ -16,7 +16,7 @@ void PNParser::addAtom(Atom a) { } bool isSpacing(char ch) { - return ch == ' ' || ch == '\t' || ch == EOF; + return ch != EOF && (ch <= ' ' || ch == '\t'); } void skipSpacing(ifstream& inFile) { @@ -63,7 +63,7 @@ void PNParser::pushToWork(Memory& mem, MemoryData* data, ifstream& inFile) { delete argc; evaluateFunction(mem, func, inFile); - if (mem.work.size() == 1) return; + if (mem.work.size() <= 1) return; data = mem.work.top(); mem.work.pop(); @@ -85,12 +85,13 @@ void PNParser::parseScope(ifstream& inFile, Memory& mem, int start, int end) { bool absorbed = false; while (start < inFile.tellg() && inFile.tellg() < end && inFile.peek() != EOF) { skipSpacing(inFile); + if (inFile.peek() == EOF) break; - /* If value is an atom */ + /* If value is an atom (not a Name) */ - for (Atom& a : this->atoms) { - if (a.isAtomChar(inFile.peek())) { - pushToWork(mem, a.absorb(inFile), inFile); + for (auto i = ++this->atoms.begin(); i != this->atoms.end(); ++i) { + if (i->isAtomChar(inFile.peek())) { + pushToWork(mem, i->absorb(inFile), inFile); absorbed = true; break; } @@ -98,14 +99,13 @@ void PNParser::parseScope(ifstream& inFile, Memory& mem, int start, int end) { if (absorbed) { absorbed = false; continue; } - /* If value is a built-in function */ + // Then value will be a Name + Name* name = static_cast<Name*>(this->atoms[0].absorb(inFile)); - string name; - while (!isSpacing(inFile.peek())) - name.push_back(inFile.get()); + /* If value is a built-in function */ for (int i = instr.size() - 1; i >= 0; i--) { - if (instr[i].name == name) { + if (instr[i].name == name->value) { list<string> temp; temp.resize(instr[i].argc); pushToWork(mem, new Function(temp, i, -1), inFile); @@ -119,16 +119,23 @@ void PNParser::parseScope(ifstream& inFile, Memory& mem, int start, int end) { /* If value is a variable */ Name* cname; - for (auto i = ++mem.vars.rbegin(); i != mem.vars.rend(); ++(++i)) { + for (auto i = ++mem.vars.rbegin(); mem.vars.size() > 0 && i++ != mem.vars.rend(); ++i) { cname = static_cast<Name*>(*i); - if (cname->value == name) { + if (cname->value == name->value) { MemoryData* value = new MemoryData(*static_cast<MemoryData*>(*(--i))); pushToWork(mem, value, inFile); + absorbed = true; break; } } + + if (absorbed) { absorbed = false; continue; } + + /* Else value is just a Name that will be given */ + + pushToWork(mem, name, inFile); } - cout << mem.vars.size(); + cout << static_cast<Int*>(mem.vars.back())->value << endl; // cout << static_cast<Int*>(mem.work.top())->value << endl; for (int i = mem.scopeVars.top(); i > 0; i--) { |
