Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
NativeRuntime.cpp
Go to the documentation of this file.
1#include "NativeRuntime.hpp"
4#include "../../AST/AST.hpp"
11#include <filesystem>
12#include <iostream>
13#include <nativeerror.h>
14
15namespace Phasor
16{
17
18NativeRuntime::NativeRuntime(const std::vector<uint8_t> &bytecodeData, const int argc, const char **argv)
19 : m_bytecodeData(bytecodeData), m_argc(argc), m_argv(const_cast<char **>(argv))
20{
21 BytecodeDeserializer deserializer;
23 m_vm = std::make_unique<VM>();
24}
25
26NativeRuntime::NativeRuntime(const std::string &script, const int argc, char **argv)
27 : m_script(script), m_argc(argc), m_argv(argv)
28{
29 Lexer lexer(m_script);
30 auto tokens = lexer.tokenize();
31
32 Parser parser(tokens);
33 auto program = parser.parse();
34
35 CodeGenerator codegen;
36 m_bytecode = codegen.generate(*program);
37 m_vm = std::make_unique<VM>();
38
39 m_vm->registerNativeFunction("lib_Phasor", runScript);
40}
41
43{
44 m_vm.reset();
45 m_vm = nullptr;
46}
47
48void NativeRuntime::addNativeFunction(const std::string &name, void *function)
49{
50 using RawFunctionPtr = Value (*)(const std::vector<Value> &, VM *);
51 RawFunctionPtr rawPtr = reinterpret_cast<RawFunctionPtr>(function);
52 NativeFunction nativeFunction = rawPtr;
53 m_vm->registerNativeFunction(name, nativeFunction);
54}
55
56int NativeRuntime::eval(VM *vm, const std::string &script)
57{
58 Lexer lexer(script);
59 auto tokens = lexer.tokenize();
60
61 Parser parser(tokens);
62 auto program = parser.parse();
63
64 CodeGenerator codegen;
65 auto bytecode = codegen.generate(*program);
66
67 return vm->run(bytecode);
68}
69
71{
72
73 try
74 {
78#if defined(_WIN32)
79 m_vm->initFFI("plugins");
80#elif defined(__APPLE__)
81 m_vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
82#elif defined(__linux__)
83 m_vm->initFFI("/usr/lib/phasor/plugins/");
84#endif
85 m_vm->setImportHandler([](const std::filesystem::path &path) {
86 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
87 });
88
89 return m_vm->run(m_bytecode);
90 }
91 catch (const std::exception &e)
92 {
93 std::string errorMsg = std::string(e.what()) + " | " + m_vm->getInformation() + "\n";
94 error(errorMsg);
95 return 1;
96 }
97 return 0;
98}
99
100Value NativeRuntime::runScript(const std::vector<Value> &args, VM *vm)
101{
102 VM newVM;
103 StdLib::checkArgCount(args, 1, "phasor_eval");
104 Lexer lexer(args[1].asString());
105 Parser parser(lexer.tokenize());
106 CodeGenerator codegen;
107 auto program = parser.parse();
108 auto bytecode = codegen.generate(*program);
109 return newVM.run(bytecode);
110}
111
112} // namespace Phasor
Bytecode binary format deserializer.
Bytecode deserialize(const std::vector< uint8_t > &data)
Deserialize bytecode from binary buffer.
Code generator for Phasor VM.
Definition CodeGen.hpp:90
Bytecode generate(const AST::Program &program, const std::map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
Definition CodeGen.cpp:7
Lexer.
Definition Lexer.hpp:13
std::vector< Token > tokenize()
Definition Lexer.cpp:26
void addNativeFunction(const std::string &name, void *function)
static Value runScript(const std::vector< Value > &args, VM *vm)
std::unique_ptr< VM > m_vm
NativeRuntime(const std::vector< uint8_t > &bytecodeData, int argc, const char **argv)
static int eval(VM *vm, const std::string &script)
std::vector< uint8_t > m_bytecodeData
Parser.
Definition Parser.hpp:13
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:73
static char ** argv
Command line arguments.
Definition StdLib.hpp:40
static void registerFunctions(VM &vm)
Definition StdLib.hpp:28
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static int argc
Number of command line arguments.
Definition StdLib.hpp:41
Virtual Machine.
Definition VM.hpp:30
int run(const Bytecode &bytecode)
Run the virtual machine Exits -1 on uncaught exception.
Definition Utility.cpp:13
A value in the Phasor VM.
Definition Value.hpp:67
The Phasor Programming Language and Runtime.
Definition AST.hpp:11
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Definition StdLib.hpp:20
#define error(msg)
Definition nativeerror.h:11