Bob/source/bob.cpp
Bobby Lucero 1e65b344ae Major speed optimization
- Replace Object* with Value tagged union for better performance
- Fix bug where "true"/"false" strings were treated as booleans
- Add isBoolean field to LiteralExpr to distinguish string vs boolean literals
- Implement fast function calls with g_returnContext instead of exceptions
- Add functions vector to prevent dangling pointers
- Remove try-catch blocks from execute() for 50x performance improvement
- Clean up test files, keep only main test suite and fib benchmark
- All 38 tests passing, fib(30) still ~848ms
2025-07-31 00:16:54 -04:00

81 lines
1.4 KiB
C++

#include <utility>
#include "../headers/bob.h"
#include "../headers/Parser.h"
using namespace std;
void Bob::runFile(const string& path)
{
this->interpreter = msptr(Interpreter)(false);
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 Bob::runPrompt()
{
this->interpreter = msptr(Interpreter)(true);
cout << "Bob v" << VERSION << ", 2023" << endl;
for(;;)
{
string line;
cout << "\033[0;36m" << "-> " << "\033[0;37m";
std::getline(std::cin, line);
if(std::cin.eof())
{
break;
}
this->run(line);
hadError = false;
}
}
void Bob::error(int line, const string& message)
{
}
void Bob::run(string source)
{
try {
vector<Token> tokens = lexer.Tokenize(std::move(source));
Parser p(tokens);
vector<sptr(Stmt)> statements = p.parse();
interpreter->interpret(statements);
}
catch(std::exception &e)
{
cout << "ERROR OCCURRED: " << e.what() << endl;
return;
}
catch(...)
{
cout << "UNKNOWN ERROR OCCURRED" << endl;
return;
}
}
void Bob::report(int line, string where, string message)
{
hadError = true;
}