24 if (!path.empty() && std::filesystem::exists(path))
29 auto program = parser.
parse();
42 auto bytecode = codegen.
generate(*program);
53#elif defined(__APPLE__)
54 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
55#elif defined(__linux__)
56 vm->
initFFI(
"/usr/lib/phasor/plugins/");
60 std::ifstream file(path);
63 throw std::runtime_error(
"Could not open imported file: " + path.string());
65 std::stringstream buffer;
66 buffer << file.rdbuf();
72 status = vm->
run(bytecode);
79 vm->
reset(
true,
false,
false);
111#elif defined(__APPLE__)
112 vm->
initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
113#elif defined(__linux__)
114 vm->
initFFI(
"/usr/lib/phasor/plugins/");
118 std::ifstream file(path);
121 throw std::runtime_error(
"Could not open imported file: " + path.string());
123 std::stringstream buffer;
124 buffer << file.rdbuf();
132 std::println(std::cerr,
"Failed to create FFI handler!");
136 std::unordered_map<std::string, int> globalVars;
139 bool cleanExit =
false;
141 std::println(
"Phasor REPL (using Phasor VM v{})\n"
142 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
143 "Type 'exit();' to quit. Function declarations will not work.",
144 PHASOR_VERSION_STRING);
151 if (!std::getline(std::cin, line))
154 if (line.starts_with(
"exit"))
161 std::println(std::cerr,
"Empty line");
168 auto program = parser.
parse();
175 std::println(
"AST:");
181 auto bytecode = codegen.
generate(*program, globalVars, nextVarIdx,
true);
183 globalVars = bytecode.variables;
184 nextVarIdx = bytecode.nextVarIndex;
186 status = vm->
run(bytecode);
188 catch (
const std::exception &e)
190 error(std::format(
"{}\n", e.what()));
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()
void setSourcePath(const std::filesystem::path &path)
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.
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.