Browse Source

complete lexer

canon
Hazel Levine 3 years ago
parent
commit
6a42461a96
  1. BIN
      bin/bfi
  2. BIN
      build/lex.o
  3. BIN
      build/main.o
  4. BIN
      build/parse.o
  5. 35
      src/lex.cpp
  6. 6
      src/lex.hpp
  7. 6
      src/main.cpp

BIN
bin/bfi

BIN
build/lex.o

BIN
build/main.o

BIN
build/parse.o

35
src/lex.cpp

@ -10,11 +10,42 @@
using namespace std;
void Lexer::printCode() {
cout << code << endl;
cout << bf << endl;
}
vector<OpCode> Lexer::lexCode(string code) {
vector<OpCode> Lexer::lexCode() {
vector<OpCode> v;
for (auto c : bf) {
switch(c) {
case '>':
v.push_back(OpCode::IncPtr);
break;
case '<':
v.push_back(OpCode::DecPtr);
break;
case '+':
v.push_back(OpCode::IncByte);
break;
case '-':
v.push_back(OpCode::DecByte);
break;
case '.':
v.push_back(OpCode::Write);
break;
case ',':
v.push_back(OpCode::Read);
break;
case '[':
v.push_back(OpCode::LoopBegin);
break;
case ']':
v.push_back(OpCode::LoopEnd);
break;
default:
break; // all undef characters are comments
}
}
return v;
}

6
src/lex.hpp

@ -19,11 +19,11 @@ public:
}
Lexer(BFProgram b) {
this->code = b;
this->bf = b;
}
void printCode();
vector<OpCode> lexCode(string code);
vector<OpCode> lexCode();
private:
BFProgram code;
BFProgram bf;
};

6
src/main.cpp

@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include "brainfuck.hpp"
#include "error.hpp"
#include "lex.hpp"
#include "read.hpp"
@ -15,7 +16,10 @@ int main(int argc, char* argv[]) {
if (argc == 2) { // must have only 1 arg: file
Reader r(argList[1]);
r.printFile();
Lexer lex(r.getFile());
vector<OpCode> v = lex.lexCode();
return 0;
} else {
return 1;

Loading…
Cancel
Save