24 m_vm = std::make_unique<VM>();
30 m_vm = std::make_unique<VM>();
40 auto program = parser.
parse();
44 m_vm = std::make_unique<VM>();
48 :
m_vm(const_cast<
VM *>(&vm), [](
VM *) {}), m_script(script), m_argc(argc), m_argv(
const_cast<char **
>(argv))
50 Lexer lexer(m_script);
51 Parser parser(lexer.tokenize());
53 m_bytecode = codegen.
generate(*parser.parse());
61 m_vm = std::shared_ptr<VM>(vm, [](
VM *) {});
63 m_vm = std::make_shared<VM>();
77 using RawFunctionPtr =
Value (*)(
const std::vector<Value> &,
VM *);
78 RawFunctionPtr rawPtr =
reinterpret_cast<RawFunctionPtr
>(function);
80 m_vm->registerNativeFunction(name, nativeFunction);
89 auto program = parser.
parse();
92 auto bytecode = codegen.
generate(*program);
94 return vm->
run(bytecode);
106 m_vm->initFFI(
"plugins");
107#elif defined(__APPLE__)
108 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
109#elif defined(__linux__)
110 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
112 m_vm->setImportHandler([](
const std::filesystem::path &path) {
113 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
120 m_vm->reset(
true,
false,
false);
126 catch (
const std::exception &)
141 m_vm->initFFI(
"plugins");
142#elif defined(__APPLE__)
143 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
144#elif defined(__linux__)
145 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
147 m_vm->setImportHandler([](
const std::filesystem::path &path) {
148 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
153 return static_cast<int>(ret.
asInt());
155 catch (
const std::exception &)
170 m_vm->initFFI(
"plugins");
171#elif defined(__APPLE__)
172 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
173#elif defined(__linux__)
174 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
176 m_vm->setImportHandler([](
const std::filesystem::path &path) {
177 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
184 catch (
const std::exception &)
Bytecode binary format deserializer.
Bytecode deserialize(const std::vector< u8 > &data)
Deserialize bytecode from binary buffer.
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()
NativeRuntime(const std::vector< u8 > &bytecodeData, const int argc, const char **argv)
void addNativeFunction(const std::string &name, void *function)
int runFunctionInt(std::string functionName)
std::shared_ptr< Phasor::VM > m_vm
static int eval(VM *vm, const std::string &script)
std::optional< std::string > runFunctionString(std::string functionName)
std::vector< u8 > m_bytecodeData
std::unique_ptr< AST::Program > parse()
static char ** argv
Command line arguments.
static void registerFunctions(VM &vm)
static int argc
Number of command line arguments.
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
A value in the Phasor VM.
PhsString asString() const noexcept
Get the value as a Small String.
i64 asInt() const noexcept
Get the value as an integer.
The Phasor Programming Language and Runtime.
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Complete bytecode structure.