17bool startsWith(
const std::string &input,
const std::string &prefix)
19 if (input.size() >= prefix.size() && input.compare(0, prefix.size(), prefix) == 0)
33 auto program = parser.
parse();
34 auto bytecode = codegen.
generate(*program);
45#elif defined(__APPLE__)
46 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
47#elif defined(__linux__)
48 vm->
initFFI(
"/usr/lib/phasor/plugins/");
52 std::ifstream file(path);
55 throw std::runtime_error(
"Could not open imported file: " + path.string());
57 std::stringstream buffer;
58 buffer << file.rdbuf();
67 status = vm->
run(bytecode);
92#elif defined(__APPLE__)
93 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
94#elif defined(__linux__)
95 vm->
initFFI(
"/usr/lib/phasor/plugins/");
99 std::ifstream file(path);
102 throw std::runtime_error(
"Could not open imported file: " + path.string());
104 std::stringstream buffer;
105 buffer << file.rdbuf();
110 if (ownVM)
delete vm;
111 std::println(std::cerr,
"Failed to create FFI handler!");
115 std::map<std::string, int> globalVars;
118 bool cleanExit =
false;
120 std::println(
"Pulsar REPL (using Phasor VM v{})\n"
121 "(C) 2026 Daniel McGuire\n\n"
122 "Type 'exit();' to quit. Function declarations will not work.", PHASOR_VERSION_STRING);
130 if (!std::getline(std::cin, line))
140 std::println(std::cerr,
"Empty line");
147 auto program = parser.
parse();
148 auto bytecode = codegen.
generate(*program, globalVars, nextVarIdx,
true);
151 nextVarIdx = bytecode.nextVarIndex;
153 status = vm->
run(bytecode);
155 catch (
const std::exception &e)
157 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::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.
void initFFI(const std::filesystem::path &path)
int run(const Bytecode &bytecode)
Run the virtual machine Exits -1 on uncaught exception.
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.
std::map< std::string, int > variables
Variable name -> index mapping.