Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
BinaryRuntime.cpp
Go to the documentation of this file.
1#include "BinaryRuntime.hpp"
5#include <filesystem>
6#include <iostream>
8
9#ifdef _WIN32
10#include <Windows.h>
11#define error(msg) MessageBoxA(NULL, std::string(msg).c_str(), "Phasor Runtime Error", MB_OK | MB_ICONERROR)
12#else
13#define error(msg) std::cerr << "Error: " << msg << std::endl
14#endif
15
16namespace Phasor
17{
18
19BinaryRuntime::BinaryRuntime(int argc, char *argv[], char *envp[])
20{
21 m_args.envp = envp;
22 parseArguments(argc, argv);
23}
24
26{
27 if (m_args.inputFile.empty())
28 {
29 std::cerr << "Error: No input file provided\n";
30 return 1;
31 }
32
33 try
34 {
35 if (m_args.verbose)
36 std::cerr << "DEBUG: Loading bytecode from: " << m_args.inputFile << std::endl;
37
38 BytecodeDeserializer deserializer;
39 Bytecode bytecode = deserializer.loadFromFile(m_args.inputFile);
40
41 if (m_args.verbose)
42 {
43 std::cerr << "DEBUG: Bytecode loaded successfully" << std::endl;
44 std::cerr << "DEBUG: Instructions: " << bytecode.instructions.size() << std::endl;
45 std::cerr << "DEBUG: Constants: " << bytecode.constants.size() << std::endl;
46 }
47
48 auto vm = std::make_unique<VM>();
50 StdLib::argv = m_args.scriptArgv;
51 StdLib::argc = m_args.scriptArgc;
52 StdLib::envp = m_args.envp;
53
54#if defined(_WIN32)
55 FFI ffi("plugins", vm.get());
56#elif defined(__APPLE__)
57 FFI ffi("/Library/Application Support/org.Phasor.Phasor/plugins", vm.get());
58#elif defined(__linux__)
59 FFI ffi("/opt/Phasor/plugins", vm.get());
60#endif
61
62 vm->setImportHandler([](const std::filesystem::path &path) {
63 throw std::runtime_error("Imports not supported in pure binary runtime yet: " + path.string());
64 });
65
66 if (m_args.verbose)
67 std::cerr << "DEBUG: About to run bytecode" << std::endl;
68
69 vm->run(bytecode);
70
71 if (m_args.verbose)
72 std::cerr << "DEBUG: Bytecode execution complete" << std::endl;
73
74 return 0;
75 }
76 catch (const std::exception &e)
77 {
78 error(e.what());
79 return 1;
80 }
81}
82
83void BinaryRuntime::parseArguments(int argc, char *argv[])
84{
85 int defaultArgLocation = 1;
86 for (int i = 1; i < argc; i++)
87 {
88 std::string arg = argv[i];
89
90 if (arg == "-v" || arg == "--verbose")
91 {
92 m_args.verbose = true;
93 }
94 else if (arg == "-h" || arg == "--help")
95 {
96 showHelp(argv[0]);
97 exit(0);
98 }
99 else
100 {
101 defaultArgLocation = i;
102 m_args.inputFile = arg;
103 break; // Stop parsing after finding the input file
104 }
105 }
106 m_args.scriptArgv = argv + defaultArgLocation;
107 m_args.scriptArgc = argc - defaultArgLocation;
108}
109
110void BinaryRuntime::showHelp(const std::string &programName)
111{
112 std::string filename = std::filesystem::path(programName).filename().string();
113 std::cout << "Phasor Binary Runtime\n\n";
114 std::cout << "Usage:\n";
115 std::cout << " " << filename << " [options] <file.phsb> [...script args]\n\n";
116 std::cout << "Options:\n";
117 std::cout << " -v, --verbose Enable verbose output\n";
118 std::cout << " -h, --help Show this help message\n";
119}
120
121} // namespace Phasor
#define error(msg)
struct Phasor::BinaryRuntime::Args m_args
void parseArguments(int argc, char *argv[])
BinaryRuntime(int argc, char *argv[], char *envp[])
void showHelp(const std::string &programName)
Bytecode binary format deserializer.
Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phsb file.
Manages loading, registering, and unloading native FFI plugins.
Definition ffi.hpp:48
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
static char ** envp
Environment variables.
Definition StdLib.hpp:32
The Phasor Programming Language and Runtime.
Definition AST.hpp:8
Complete bytecode structure.
Definition CodeGen.hpp:201
std::vector< Value > constants
Constant pool.
Definition CodeGen.hpp:203
std::vector< Instruction > instructions
List of instructions.
Definition CodeGen.hpp:202