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