19#define error(msg) MessageBoxA(NULL, std::string(msg).c_str(), "Phasor Runtime Error", MB_OK | MB_ICONERROR)
21#define error(msg) std::cerr << "Error: " << msg << std::endl
24bool startsWith(
const std::string &input,
const std::string &prefix)
26 if (input.size() >= prefix.size() && input.compare(0, prefix.size(), prefix) == 0)
39 auto program = parser.
parse();
42 auto bytecode = codegen.
generate(*program);
54 FFI ffi(
"plugins", vm);
55#elif defined(__APPLE__)
56 FFI ffi(
"/Library/Application Support/org.Phasor.Phasor/plugins", vm);
57#elif defined(__linux__)
58 FFI ffi(
"/opt/Phasor/plugins", vm);
62 std::ifstream file(path);
65 throw std::runtime_error(
"Could not open imported file: " + path.string());
67 std::stringstream buffer;
68 buffer << file.rdbuf();
82 std::cout <<
"Phasor v" << PHASOR_VERSION_STRING <<
" REPL\n(C) 2026 Daniel McGuire\n\n";
83 std::cout <<
"Type 'exit();' to quit. Function declarations will not work.\n";
94 FFI ffi(
"plugins", vm);
95#elif defined(__APPLE__)
96 FFI ffi(
"/Library/Application Support/org.Phasor.Phasor/plugins", vm);
97#elif defined(__linux__)
98 FFI ffi(
"/opt/Phasor/plugins", vm);
102 std::ifstream file(path);
105 throw std::runtime_error(
"Could not open imported file: " + path.string());
107 std::stringstream buffer;
108 buffer << file.rdbuf();
112 std::map<std::string, int> globalVars;
122 if (!std::getline(std::cin, line))
131 vm->
push(line.substr(4));
140 char instruction[64];
141 unsigned int operand;
142 sscanf(line.c_str(),
"vm_op %63s %d", instruction, &operand);
149 sscanf(line.c_str(),
"vm_getvar %d", &index);
155 sscanf(line.c_str(),
"vm_setvar %63s", value);
156 line =
"var setvarx = " + std::to_string(vm->
addVariable(value));
160 vm->
reset(
true,
true,
true);
167 std::cerr <<
"Empty line\n";
175 auto program = parser.
parse();
177 auto bytecode = codegen.
generate(*program, globalVars, nextVarIdx,
true);
181 nextVarIdx = bytecode.nextVarIndex;
185 catch (
const std::exception &e)
187 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.
Manages loading, registering, and unloading native FFI plugins.
std::vector< Token > tokenize()
std::unique_ptr< AST::Program > parse()
static OpCode stringToOpCode(const std::string &str)
Helper to convert string to OpCode.
static void registerFunctions(VM &vm)
Register all standard library functions.
Value pop()
Pop a value from the stack.
size_t addVariable(const Value &value)
Add a variable to the VM.
void push(const Value &value)
Push a value onto the stack.
void setImportHandler(ImportHandler handler)
void run(const Bytecode &bytecode)
Run the virtual machine.
Value getVariable(const size_t index)
Get a variable from the VM.
Value operation(const OpCode &op, const int &operand1=0, const int &operand2=0, const int &operand3=0, const int &operand4=0, const int &operand5=0)
Execute a single operation.
Value peek()
Peek at the top value on the stack.
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.
std::string toString() const
Convert to string for printing.
void runScript(const std::string &source, VM *vm=nullptr)
Run a script.
void runRepl(VM *vm=nullptr)
Run an REPL.
std::map< std::string, int > variables
Variable name -> index mapping.