24 if (!path.empty() && std::filesystem::exists(path)) {
28 auto program = parser.
parse();
39 auto bytecode = codegen.
generate(*program);
50#elif defined(__APPLE__)
51 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
52#elif defined(__linux__)
53 vm->
initFFI(
"/usr/lib/phasor/plugins/");
57 std::ifstream file(path);
60 throw std::runtime_error(
"Could not open imported file: " + path.string());
62 std::stringstream buffer;
63 buffer << file.rdbuf();
74 status = vm->
run(bytecode);
102#elif defined(__APPLE__)
103 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
104#elif defined(__linux__)
105 vm->
initFFI(
"/usr/lib/phasor/plugins/");
109 std::ifstream file(path);
112 throw std::runtime_error(
"Could not open imported file: " + path.string());
114 std::stringstream buffer;
115 buffer << file.rdbuf();
120 if (ownVM)
delete vm;
121 std::println(std::cerr,
"Failed to create FFI handler!");
125 std::map<std::string, int> globalVars;
128 bool cleanExit =
false;
130 std::println(
"Phasor REPL (using Phasor VM v{})\n"
131 "(C) 2026 Daniel McGuire\n\n"
132 "Type 'exit();' to quit. Function declarations will not work.", PHASOR_VERSION_STRING);
139 if (!std::getline(std::cin, line))
142 if (line.starts_with(
"exit"))
149 std::println(std::cerr,
"Empty line");
156 auto program = parser.
parse();
161 std::println(
"AST:");
167 auto bytecode = codegen.
generate(*program, globalVars, nextVarIdx,
true);
170 nextVarIdx = bytecode.nextVarIndex;
172 status = vm->
run(bytecode);
174 catch (
const std::exception &e)
176 error(std::format(
"{}\n", e.what()));
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()
void setSourcePath(const std::filesystem::path &path)
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.
int runRepl(VM *vm=nullptr, bool verbose=false)
Run an REPL.
int runScript(const std::string &source, VM *vm, const std::filesystem::path &path="", bool verbose=false)
Run a script.
std::map< std::string, int > variables
Variable name -> index mapping.