17bool startsWith(
const std::string &input,
const std::string &prefix)
19 if (input.size() >= prefix.size() && input.compare(0, prefix.size(), prefix) == 0)
34 auto program = parser.
parse();
35 auto bytecode = codegen.
generate(*program);
46#elif defined(__APPLE__)
47 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
48#elif defined(__linux__)
49 vm->
initFFI(
"/usr/lib/phasor/plugins/");
53 std::ifstream file(path);
56 throw std::runtime_error(
"Could not open imported file: " + path.string());
58 std::stringstream buffer;
59 buffer << file.rdbuf();
65 status = vm->
run(bytecode);
72 vm->
reset(
true,
false,
false);
104#elif defined(__APPLE__)
105 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
106#elif defined(__linux__)
107 vm->
initFFI(
"/usr/lib/phasor/plugins/");
111 std::ifstream file(path);
114 throw std::runtime_error(
"Could not open imported file: " + path.string());
116 std::stringstream buffer;
117 buffer << file.rdbuf();
125 std::println(std::cerr,
"Failed to create FFI handler!");
129 std::unordered_map<std::string, int> globalVars;
132 bool cleanExit =
false;
134 std::println(
"Pulsar REPL (using Phasor VM v{})\n"
135 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
136 "Type 'exit()' to quit. Function declarations will not work.",
137 PHASOR_VERSION_STRING);
144 if (!std::getline(std::cin, line))
154 std::println(std::cerr,
"Empty line");
161 auto program = parser.
parse();
162 auto bytecode = codegen.
generate(*program, globalVars, nextVarIdx,
true);
164 globalVars = bytecode.variables;
165 nextVarIdx = bytecode.nextVarIndex;
167 status = vm->
run(bytecode);
169 catch (
const std::exception &e)
171 std::string errorMsg = std::string(e.what()) +
" | " + vm->
getInformation() +
"\n";
bool startsWith(const std::string &input, const std::string &prefix)
Code generator for Phasor VM.
Bytecode generate(const AST::Program &program, const std::unordered_map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
std::vector< Token > tokenize()
std::unique_ptr< AST::Program > parse()
static void registerFunctions(VM &vm)
void setImportHandler(const ImportHandler &handler)
Set the import handler for importing modules.
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
void initFFI(const std::filesystem::path &path)
Initialize the FFI plugins.
void reset(const bool &resetStack=true, const bool &resetFunctions=true, const bool &resetVariables=true)
Reset the virtual machine.
std::string getInformation()
Get VM information for debugging.
int runRepl(Phasor::VM *vm=nullptr)
Run an REPL.
int runScript(const std::string &source, Phasor::VM *vm=nullptr)
Run a script.