Phasor 2.2.0
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"
12#include <filesystem>
13#include <iostream>
14
15#ifdef _WIN32
16#include <Windows.h>
17#define error(msg) MessageBoxA(NULL, std::string(msg).c_str(), "Phasor Runtime Error", MB_OK | MB_ICONERROR)
18#else
19#define error(msg) std::cerr << "Error: " << msg << std::endl
20#endif
21
22namespace Phasor
23{
24
25NativeRuntime::NativeRuntime(const std::vector<uint8_t> &bytecodeData, const int argc, const char **argv)
26 : m_bytecodeData(bytecodeData), m_argc(argc), m_argv(const_cast<char **>(argv))
27{
28 BytecodeDeserializer deserializer;
30 m_vm = std::make_unique<VM>();
31}
32
33NativeRuntime::NativeRuntime(const std::string &script, const int argc, char **argv) : m_script(script), m_argc(argc), m_argv(argv)
34{
35 Lexer lexer(m_script);
36 auto tokens = lexer.tokenize();
37
38 Parser parser(tokens);
39 auto program = parser.parse();
40
41 CodeGenerator codegen;
42 m_bytecode = codegen.generate(*program);
43 m_vm = std::make_unique<VM>();
44}
45
47{
48 m_vm.reset();
49 m_vm = nullptr;
50}
51
52void NativeRuntime::addNativeFunction(const std::string &name, void *function)
53{
54 using RawFunctionPtr = Value (*)(const std::vector<Value> &, VM *);
55 RawFunctionPtr rawPtr = reinterpret_cast<RawFunctionPtr>(function);
56 NativeFunction nativeFunction = rawPtr;
57 m_vm->registerNativeFunction(name, nativeFunction);
58}
59
60void NativeRuntime::eval(VM *vm, const std::string &script)
61{
62 Lexer lexer(script);
63 auto tokens = lexer.tokenize();
64
65 Parser parser(tokens);
66 auto program = parser.parse();
67
68 CodeGenerator codegen;
69 auto bytecode = codegen.generate(*program);
70
71 vm->run(bytecode);
72}
73
75{
76
77 try
78 {
82#if defined(_WIN32)
83 FFI ffi("plugins", m_vm.get());
84#elif defined(__APPLE__)
85 FFI ffi("/Library/Application Support/org.Phasor.Phasor/plugins", m_vm.get());
86#elif defined(__linux__)
87 FFI ffi("/opt/Phasor/plugins", m_vm.get());
88#endif
89 m_vm->setImportHandler([](const std::filesystem::path &path) {
90 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
91 });
92
93 m_vm->run(m_bytecode);
94 }
95 catch (const std::exception &e)
96 {
97 std::string errorMsg = std::string(e.what()) + " | " + m_vm->getInformation() + "\n";
98 error(errorMsg);
99 return 1;
100 }
101 return 0;
102}
103
104} // namespace Phasor
#define error(msg)
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:243
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
Manages loading, registering, and unloading native FFI plugins.
Definition ffi.hpp:48
Lexer.
Definition Lexer.hpp:30
std::vector< Token > tokenize()
Definition Lexer.cpp:25
void addNativeFunction(const std::string &name, void *function)
NativeRuntime(const std::vector< uint8_t > &bytecodeData, const int argc, const char **argv)
std::unique_ptr< VM > m_vm
static void eval(VM *vm, const std::string &script)
std::vector< uint8_t > m_bytecodeData
Parser.
Definition Parser.hpp:12
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:13
static void registerFunctions(VM &vm)
Register all standard library functions.
Definition StdLib.cpp:10
static char ** argv
Command line arguments.
Definition StdLib.hpp:30
static int argc
Number of command line arguments.
Definition StdLib.hpp:31
Virtual Machine.
Definition VM.hpp:18
void run(const Bytecode &bytecode)
Run the virtual machine.
Definition VM.cpp:9
A value in the Phasor VM.
Definition Value.hpp:33
The Phasor Programming Language and Runtime.
Definition AST.hpp:8
std::function< Value(const std::vector< Value > &args, VM *vm)> NativeFunction
Native function signature.
Definition StdLib.hpp:19