diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ArzuParser.cpp | 7 | ||||
| -rw-r--r-- | src/PolishNotationParser.cpp | 35 | ||||
| -rw-r--r-- | src/PolishNotationParser.h | 2 | ||||
| -rw-r--r-- | src/test.arzu | 3 |
4 files changed, 27 insertions, 20 deletions
diff --git a/src/ArzuParser.cpp b/src/ArzuParser.cpp index 0556a27..519bc76 100644 --- a/src/ArzuParser.cpp +++ b/src/ArzuParser.cpp @@ -7,7 +7,7 @@ bool isDigit(char ch) { } bool isName(char ch) { - return ch == '$'; + return 'a' <= ch && ch <= 'z'; } bool isSeparator(char ch) { @@ -63,7 +63,7 @@ MemoryData* arzu_Int_absorb(ifstream& inFile) { MemoryData* arzu_Name_absorb(ifstream& inFile) { Name* name = new Name(); - while (isDigit(inFile.peek()) || ('0' <= inFile.peek() && inFile.peek() <= 'z')) + while (!isSeparator(inFile.peek())) name->value.push_back(inFile.get()); return name; } @@ -75,6 +75,7 @@ ArzuParser::ArzuParser() : PNParser() { this->addInstr(Instruction("/", 2, arzu_div)); this->addInstr(Instruction("devar", 2, arzu_devar)); - this->addAtom(Atom(isDigit, arzu_Int_absorb)); + // Name atom MUST always be first atom!!! this->addAtom(Atom(isName, arzu_Name_absorb)); + this->addAtom(Atom(isDigit, arzu_Int_absorb)); } 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--) { diff --git a/src/PolishNotationParser.h b/src/PolishNotationParser.h index 1de6f26..2757d60 100644 --- a/src/PolishNotationParser.h +++ b/src/PolishNotationParser.h @@ -25,7 +25,7 @@ struct Atom { class PNParser { deque<Instruction> instr; - list<Atom> atoms; + deque<Atom> atoms; void evaluateFunction(Memory& mem, Function* func, ifstream& inFile); void pushToWork(Memory& mem, MemoryData* data, ifstream& inFile); diff --git a/src/test.arzu b/src/test.arzu index cb31da8..57419d1 100644 --- a/src/test.arzu +++ b/src/test.arzu @@ -1,2 +1 @@ -devar $a 10 -+ $a 1 +devar a 10 |
