23 m_vm = std::make_unique<VM>();
29 m_vm = std::make_unique<VM>();
39 auto program = parser.
parse();
43 m_vm = std::make_unique<VM>();
47 :
m_vm(const_cast<
VM *>(&vm), [](
VM *) {}), m_script(script), m_argc(argc), m_argv(
const_cast<char **
>(argv))
49 Lexer lexer(m_script);
50 Parser parser(lexer.tokenize());
52 m_bytecode = codegen.
generate(*parser.parse());
60 m_vm = std::shared_ptr<VM>(vm, [](
VM *) {});
62 m_vm = std::make_shared<VM>();
76 using RawFunctionPtr =
Value (*)(
const std::vector<Value> &,
VM *);
77 RawFunctionPtr rawPtr =
reinterpret_cast<RawFunctionPtr
>(function);
79 m_vm->registerNativeFunction(name, nativeFunction);
88 auto program = parser.
parse();
91 auto bytecode = codegen.
generate(*program);
93 return vm->
run(bytecode);
105 m_vm->initFFI(
"plugins");
106#elif defined(__APPLE__)
107 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
108#elif defined(__linux__)
109 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
111 m_vm->setImportHandler([](
const std::filesystem::path &path) {
112 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
119 m_vm->reset(
true,
false,
false);
125 catch (
const std::exception &)
140 m_vm->initFFI(
"plugins");
141#elif defined(__APPLE__)
142 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
143#elif defined(__linux__)
144 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
146 m_vm->setImportHandler([](
const std::filesystem::path &path) {
147 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
152 return static_cast<int>(ret.
asInt());
154 catch (
const std::exception &)
169 m_vm->initFFI(
"plugins");
170#elif defined(__APPLE__)
171 m_vm->initFFI(
"/Library/Application Support/org.Phasor.Phasor/plugins");
172#elif defined(__linux__)
173 m_vm->initFFI(
"/usr/lib/phasor/plugins/");
175 m_vm->setImportHandler([](
const std::filesystem::path &path) {
176 throw std::runtime_error(
"Imports not supported in pure binary runtime yet: " + path.string());
183 catch (
const std::exception &)
Bytecode binary format deserializer.
Bytecode deserialize(const std::vector< uint8_t > &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()
void addNativeFunction(const std::string &name, void *function)
NativeRuntime(const std::vector< uint8_t > &bytecodeData, const int argc, const char **argv)
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< uint8_t > 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.
int64_t asInt() const noexcept
Get the value as an integer.
std::string asString() const noexcept
Get the value as a string.
The Phasor Programming Language and Runtime.
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Complete bytecode structure.