Bob interpreter refactored into object
This commit is contained in:
parent
650771e6fe
commit
23799125a7
0
headers/AST.h
Normal file
0
headers/AST.h
Normal file
@ -64,12 +64,14 @@ std::vector<Token> Lexer::Tokenize(std::string source){
|
||||
}
|
||||
else if(t == ' ' || t == '\t' || t == '\n')
|
||||
{
|
||||
src.erase(src.begin());
|
||||
src.erase(src.begin()); //ignore t
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("Unknown Token: '" + std::string(1, t) + "'");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
95
src/bob.cpp
95
src/bob.cpp
@ -3,38 +3,83 @@
|
||||
#include <string>
|
||||
#include "../headers/Lexer.h"
|
||||
|
||||
#define VERSION "0.0.1"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Bob
|
||||
{
|
||||
public:
|
||||
Lexer lexer;
|
||||
|
||||
public:
|
||||
void runFile(string path)
|
||||
{
|
||||
ifstream file = ifstream(path);
|
||||
|
||||
string source = "";
|
||||
|
||||
if(file.is_open()){
|
||||
source = string(istreambuf_iterator<char>(file), istreambuf_iterator<char>());
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "File not found" << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
this->run(source);
|
||||
}
|
||||
|
||||
void runPrompt()
|
||||
{
|
||||
cout << "Bob v" << VERSION << ", 2023" << endl;
|
||||
for(;;)
|
||||
{
|
||||
string line;
|
||||
cout << "-> ";
|
||||
std::getline(std::cin, line);
|
||||
|
||||
if(std::cin.eof())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
this->run(line);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
bool hadError = false;
|
||||
|
||||
private:
|
||||
void run(string source)
|
||||
{
|
||||
vector<Token> tokens = lexer.Tokenize(source);
|
||||
|
||||
for(Token t : tokens){
|
||||
cout << "{type: " << t.type << ", value: " << t.value << "}" << endl;
|
||||
}
|
||||
}
|
||||
};
|
||||
int main(){
|
||||
|
||||
string TokenTypeMappings[] = {
|
||||
"Identifier",
|
||||
"Number",
|
||||
"Equals",
|
||||
"OpenParen",
|
||||
"CloseParen",
|
||||
"BinaryOperator",
|
||||
"TestKeyword"
|
||||
};
|
||||
Lexer l;
|
||||
|
||||
string path = "source.bob";
|
||||
ifstream file = ifstream(path);
|
||||
// string TokenTypeMappings[] = {
|
||||
// "Identifier",
|
||||
// "Number",
|
||||
// "Equals",
|
||||
// "OpenParen",
|
||||
// "CloseParen",
|
||||
// "BinaryOperator",
|
||||
// "TestKeyword"
|
||||
// };
|
||||
|
||||
string source = "";
|
||||
Bob bobLang;
|
||||
|
||||
if(file.is_open()){
|
||||
source = string(istreambuf_iterator<char>(file), istreambuf_iterator<char>());
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "File not found" << endl;
|
||||
}
|
||||
//bobLang.runFile("source.bob");
|
||||
bobLang.runPrompt();
|
||||
|
||||
|
||||
vector<Token> tokens = l.Tokenize(source);
|
||||
for(Token t : tokens){
|
||||
cout << "Type: " << TokenTypeMappings[t.type] << ", Value: " + t.value << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user