Browse Source

working file reading, begin lexer

canon
Hazel Levine 2 years ago
parent
commit
128168e104
  1. BIN
      bin/bfi
  2. BIN
      build/lex.o
  3. BIN
      build/main.o
  4. BIN
      build/read.o
  5. 18
      src/brainfuck.hpp
  6. 20
      src/lex.cpp
  7. 10
      src/lex.hpp
  8. 26
      src/main.cpp
  9. 11
      src/parse.cpp
  10. 6
      src/read.cpp
  11. 23
      src/read.hpp
  12. 1
      test/hello.bf

BIN
bin/bfi

BIN
build/lex.o

BIN
build/main.o

BIN
build/read.o

18
src/brainfuck.hpp

@ -0,0 +1,18 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
enum class OpCode {
IncPtr,
DecPtr,
IncByte,
DecByte,
Write,
Read,
LoopBegin,
LoopEnd,
Invalid
};

20
src/lex.cpp

@ -0,0 +1,20 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "brainfuck.hpp"
#include "error.hpp"
#include "lex.hpp"
using namespace std;
void Lexer::printCode() {
cout << code << endl;
}
vector<OpCode> Lexer::lexCode(string code) {
vector<OpCode> v;
return v;
}

10
src/parse.hpp → src/lex.hpp

@ -5,21 +5,25 @@
#include <string>
#include <vector>
#include "brainfuck.hpp"
#include "error.hpp"
using namespace std;
typedef string BFProgram;
class Parser {
class Lexer {
public:
Parser(void) {
Lexer(void) {
throw ParseExcept(ErrType::NoInput); // what are you doing??
}
Parser(BFProgram b) {
Lexer(BFProgram b) {
this->code = b;
}
void printCode();
vector<OpCode> lexCode(string code);
private:
BFProgram code;
};

26
src/main.cpp

@ -2,32 +2,22 @@
#include <vector>
#include "error.hpp"
#include "parse.hpp"
#include "lex.hpp"
#include "read.hpp"
using namespace std;
enum class OpCode {
IncPtr,
DecPtr,
IncByte,
DecByte,
Write,
Read,
LoopBegin,
LoopEnd,
Invalid
};
int main(int argc, char* argv[]) {
std::vector<string> argList;
for (int i = 0; i < argc; i++) {
argList.push_back(argv[i]);
}
Reader r(argList[0]);
//Parser parse("content");
r.printFile();
return 0;
if (argc == 2) { // must have only 1 arg: file
Reader r(argList[1]);
r.printFile();
return 0;
} else {
return 1;
}
}

11
src/parse.cpp

@ -1,11 +0,0 @@
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "error.hpp"
#include "parse.hpp"
void Parser::printCode() {
cout << code << endl;
}

6
src/read.cpp

@ -5,6 +5,12 @@
#include "read.hpp"
using namespace std;
void Reader::printFile() {
cout << contents << endl;
}
string Reader::getFile() {
return contents;
}

23
src/read.hpp

@ -3,6 +3,7 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <streambuf>
#include <string>
#include "error.hpp"
@ -18,27 +19,29 @@ public:
Reader(string filename) {
if (filename == "-") { // stdin
for (string line; std::getline(cin, line);) {
contents.append(line);
if (line != "done.") {
contents.append(line);
} else {
break;
}
}
} else {
code.open(filename);
ifstream code(filename);
if (!code) {
throw ParseExcept(ErrType::NoInput);
} else {
for (string line; std::getline(code, line);) {
contents.append(line);
}
std::string str((std::istreambuf_iterator<char>(code)),
std::istreambuf_iterator<char>());
contents = str;
}
}
}
~Reader(void) {
code.close();
code.close();
}
}
void printFile();
string getFile();
private:
ifstream code;
string contents = "";
};

1
test/hello.bf

@ -0,0 +1 @@
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Loading…
Cancel
Save