Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
ScriptingRuntime.cpp
Go to the documentation of this file.
8#include <version.h>
9#include <filesystem>
10#include <fstream>
11#include <iostream>
12#include <sstream>
13#include <nativeerror.h>
14
15namespace Phasor
16{
17
19{
20 parseArguments(argc, argv);
21}
22
24{
25 if (m_args.inputFile.empty())
26 return 0;
27
28 return runSource();
29}
30
32{
33 std::ifstream file(m_args.inputFile);
34 if (!file.is_open())
35 {
36 std::cerr << "Could not open file: " << m_args.inputFile << "\n";
37 return 1;
38 }
39
40 std::stringstream buffer;
41 buffer << file.rdbuf();
42 std::string source = buffer.str();
43
44 auto vm = createVm();
45
46 try
47 {
48 runSourceString(source, *vm);
49 }
50 catch (const std::exception &e)
51 {
52 std::string errorMsg = std::string(e.what()) + "\n";
53 error(errorMsg);
54 return 1;
55 }
56
57 return 0;
58}
59
60int ScriptingRuntime::runSourceString(const std::string &source, VM &vm)
61{
62 Lexer lexer(source);
63 auto tokens = lexer.tokenize();
64 Parser parser(tokens, m_args.inputFile);
65 auto program = parser.parse();
66
67 if (m_args.verbose)
68 {
69 std::println("AST:");
70 program->print();
71 std::println();
72 }
73
74 CodeGenerator codegen;
75 auto bytecode = codegen.generate(*program);
76
77 return vm.run(bytecode);
78}
79
80std::unique_ptr<VM> ScriptingRuntime::createVm()
81{
82 auto vm = std::make_unique<VM>();
84 StdLib::argv = m_args.scriptArgv;
85 StdLib::argc = m_args.scriptArgc;
86
87#if defined(_WIN32)
88 vm->initFFI("plugins");
89#elif defined(__APPLE__)
90 vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
91#elif defined(__linux__)
92 vm->initFFI("/usr/lib/phasor/plugins/");
93#endif
94
95 vm->setImportHandler([this, vm_ptr = vm.get()](const std::filesystem::path &path) {
96 std::ifstream file(path);
97 if (!file.is_open())
98 throw std::runtime_error("Could not open imported file: " + path.string());
99 std::stringstream buffer;
100 buffer << file.rdbuf();
101 runSourceString(buffer.str(), *vm_ptr);
102 });
103
104 return vm;
105}
106
107void ScriptingRuntime::parseArguments(int argc, char *argv[])
108{
109 int defaultArgLocation = 1;
110 for (int i = 1; i < argc; i++)
111 {
112 std::string arg = argv[i];
113
114 if (arg == "-v" || arg == "--verbose")
115 {
116 m_args.verbose = true;
117 }
118 if (arg == "-c" || arg == "--command")
119 {
120 auto vm = createVm();
121 runSourceString(argv[i + 1], *vm);
122 int ret = vm->getStatus();
123 exit(ret);
124 }
125 else if (arg == "-h" || arg == "--help")
126 {
127 showHelp(argv[0]);
128 exit(0);
129 }
130 else
131 {
132 defaultArgLocation = i;
133 m_args.inputFile = arg;
134 break; // Stop parsing after finding the input file
135 }
136 }
137 m_args.scriptArgv = argv + defaultArgLocation;
138 m_args.scriptArgc = argc - defaultArgLocation;
139}
140
141void ScriptingRuntime::showHelp(const std::string &programName)
142{
143 std::string filename = std::filesystem::path(programName).filename().string();
144 std::println("Phasor Scripting Runtime v{}\n"
145 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
146 "Usage:\n"
147 " {} [options] [file.phs] [...script args]\n\n"
148 "Options:\n"
149 " -v, --verbose Enable verbose output (print AST)\n"
150 " -h, --help Show this help message\n"
151 " -c, --command Run a source string from argv",
152 PHASOR_VERSION_STRING, filename);
153}
154
155} // namespace Phasor
Code generator for Phasor VM.
Definition CodeGen.hpp:108
Bytecode generate(const AST::Program &program, const std::unordered_map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
Definition CodeGen.cpp:8
Lexer.
Definition Lexer.hpp:13
std::vector< Token > tokenize()
Definition Lexer.cpp:27
Parser.
Definition Parser.hpp:13
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:74
std::unique_ptr< VM > createVm()
void showHelp(const std::string &programName)
struct Phasor::ScriptingRuntime::Args m_args
ScriptingRuntime(int argc, char *argv[])
void parseArguments(int argc, char *argv[])
int runSourceString(const std::string &source, VM &vm)
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
Virtual Machine.
Definition VM.hpp:33
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
Definition Utility.cpp:88
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
#define error(msg)
Definition nativeerror.h:8