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