Bob/source/bob.cpp
Bobby Lucero 7c57a9a111 Implement functions, closures, standard library, and comprehensive number system
- Add function declarations, calls, and return statements
- Implement lexical scoping with Environment class and closures
- Convert print from statement to standard library function
- Add assert() function to standard library for testing
- Add time() function for microsecond precision benchmarking
- Create StdLib class and BuiltinFunction wrapper for standard library
- Implement first-class functions and higher-order functions
- Add function parameter support (tested up to 100 parameters)
- Support alphanumeric identifiers in variable and function names
- Add underscore support in variable names and identifiers
- Implement string + number and number + string concatenation
- Add boolean + string and string + boolean concatenation
- Support string multiplication (string * number)
- Fix decimal truncation issue by using std::stod for all number parsing
- Add comprehensive number formatting with proper precision handling
- Support huge numbers (epoch timestamps) without integer overflow
- Clean number display (no trailing zeros on integers)
- Add basic error handling with program termination on errors
- Add comprehensive test suite covering all features
- Add escape sequence support (\n, \t, \", \\)
- Add comprehensive documentation and language reference
- Update development roadmap with completed features
2025-07-30 17:51:48 -04:00

86 lines
1.6 KiB
C++

#include <utility>
#include "../headers/bob.h"
#include "../headers/Parser.h"
#include "../headers/ASTPrinter.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));
// for(Token t : tokens){
// cout << "{type: " << enum_mapping[t.type] << ", value: " << t.lexeme << "}" << endl;
// }
Parser p(tokens);
vector<sptr(Stmt)> statements = p.parse();
interpreter->interpret(statements);
//cout << "=========================" << endl;
}
catch(std::exception &e)
{
cout << "ERROR OCCURRED: " << e.what() << endl;
return;
}
}
void Bob::report(int line, string where, string message)
{
hadError = true;
}