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')
|
else if(t == ' ' || t == '\t' || t == '\n')
|
||||||
{
|
{
|
||||||
src.erase(src.begin());
|
src.erase(src.begin()); //ignore t
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Unknown Token: '" + std::string(1, t) + "'");
|
throw std::runtime_error("Unknown Token: '" + std::string(1, t) + "'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
75
src/bob.cpp
75
src/bob.cpp
@ -3,22 +3,18 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "../headers/Lexer.h"
|
#include "../headers/Lexer.h"
|
||||||
|
|
||||||
|
#define VERSION "0.0.1"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int main(){
|
class Bob
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Lexer lexer;
|
||||||
|
|
||||||
string TokenTypeMappings[] = {
|
public:
|
||||||
"Identifier",
|
void runFile(string path)
|
||||||
"Number",
|
{
|
||||||
"Equals",
|
|
||||||
"OpenParen",
|
|
||||||
"CloseParen",
|
|
||||||
"BinaryOperator",
|
|
||||||
"TestKeyword"
|
|
||||||
};
|
|
||||||
Lexer l;
|
|
||||||
|
|
||||||
string path = "source.bob";
|
|
||||||
ifstream file = ifstream(path);
|
ifstream file = ifstream(path);
|
||||||
|
|
||||||
string source = "";
|
string source = "";
|
||||||
@ -29,12 +25,61 @@ int main(){
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "File not found" << endl;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<Token> tokens = l.Tokenize(source);
|
private:
|
||||||
|
bool hadError = false;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void run(string source)
|
||||||
|
{
|
||||||
|
vector<Token> tokens = lexer.Tokenize(source);
|
||||||
|
|
||||||
for(Token t : tokens){
|
for(Token t : tokens){
|
||||||
cout << "Type: " << TokenTypeMappings[t.type] << ", Value: " + t.value << endl;
|
cout << "{type: " << t.type << ", value: " << t.value << "}" << endl;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
// string TokenTypeMappings[] = {
|
||||||
|
// "Identifier",
|
||||||
|
// "Number",
|
||||||
|
// "Equals",
|
||||||
|
// "OpenParen",
|
||||||
|
// "CloseParen",
|
||||||
|
// "BinaryOperator",
|
||||||
|
// "TestKeyword"
|
||||||
|
// };
|
||||||
|
|
||||||
|
Bob bobLang;
|
||||||
|
|
||||||
|
//bobLang.runFile("source.bob");
|
||||||
|
bobLang.runPrompt();
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user