Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
ScriptingRuntime.cpp
Go to the documentation of this file.
8#include <filesystem>
9#include <print>
10#include <fstream>
11#include <iostream>
12#include <sstream>
13#include <version.h>
14#include <nativeerror.h>
15
16namespace pulsar
17{
18
19Interpreter::Interpreter(int argc, char *argv[])
20{
21 parseArguments(argc, argv);
22}
23
25{
26 if (m_args.inputFile.empty())
27 return 0;
28
29 return runSource();
30}
31
33{
34 std::ifstream file(m_args.inputFile);
35 if (!file.is_open())
36 {
37 std::cerr << "Could not open file: " << m_args.inputFile << "\n";
38 return 1;
39 }
40
41 std::stringstream buffer;
42 buffer << file.rdbuf();
43 std::string source = buffer.str();
44
45 try
46 {
47 auto vm = createVm();
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 Interpreter::runSourceString(const std::string &source, Phasor::VM &vm)
61{
62 Lexer lexer(source);
63 Parser parser(lexer.tokenize());
65 auto program = parser.parse();
66
67 if (m_args.verbose)
68 {
69 std::println("AST:");
70 program->print();
71 std::println();
72 }
73 auto bytecode = codegen.generate(*program);
74
75 return vm.run(bytecode);
76}
77
78std::unique_ptr<Phasor::VM> Interpreter::createVm()
79{
80 auto vm = std::make_unique<Phasor::VM>();
82 Phasor::StdLib::argv = m_args.scriptArgv;
83 Phasor::StdLib::argc = m_args.scriptArgc;
84
85#if defined(_WIN32)
86 vm->initFFI("plugins");
87#elif defined(__APPLE__)
88 vm->initFFI("/Library/Application Support/org.Phasor.Phasor/plugins");
89#elif defined(__linux__)
90 vm->initFFI("/opt/Phasor/plugins");
91#endif
92
93 vm->setImportHandler([this, vm_ptr = vm.get()](const std::filesystem::path &path) {
94 std::ifstream file(path);
95 if (!file.is_open())
96 throw std::runtime_error("Could not open imported file: " + path.string());
97 std::stringstream buffer;
98 buffer << file.rdbuf();
99 runSourceString(buffer.str(), *vm_ptr);
100 });
101
102 return vm;
103}
104
105void Interpreter::parseArguments(int argc, char *argv[])
106{
107 m_args.program = std::filesystem::path(argv[0]);
108 int defaultArgLocation = 1;
109 for (int i = 1; i < argc; i++)
110 {
111 std::string arg = argv[i];
112
113 if (arg == "-v" || arg == "--verbose")
114 {
115 m_args.verbose = true;
116 }
117 if (arg == "-c" || arg == "--command")
118 {
119 auto vm = createVm();
120 runSourceString(argv[i + 1], *vm);
121 int ret = vm->getStatus();
122 exit(ret);
123 }
124 else if (arg == "-h" || arg == "--help")
125 {
126 showHelp();
127 exit(0);
128 }
129 else if (arg == "--version")
130 {
131 std::println(PHASOR_VERSION_STRING);
132 exit(0);
133 }
134 else
135 {
136 defaultArgLocation = i;
137 m_args.inputFile = arg;
138 break;
139 }
140 }
141 m_args.scriptArgv = argv + defaultArgLocation;
142 m_args.scriptArgc = argc - defaultArgLocation;
143}
144
146{
147 std::println("Pulsar Scripting Language (Phasor v{})\n"
148 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
149 "Usage:\n"
150 " {} [inFile] [...script args]\n\n"
151 "Options:\n"
152 " -v, --verbose Enable verbose output (print AST)\n"
153 " -h, --help Show this help message\n"
154 " --version Print version string to stdout\n"
155 " -c, --command Run a source string from argv",
156 PHASOR_VERSION_STRING, m_args.program.stem().string());
157}
158
159} // namespace pulsar
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
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
int runSourceString(const std::string &source, Phasor::VM &vm)
void parseArguments(int argc, char *argv[])
std::unique_ptr< Phasor::VM > createVm()
struct pulsar::Interpreter::Args m_args
Interpreter(int argc, char *argv[])
The Pulsar Scripting Language.
Definition Compiler.cpp:14
#define error(msg)
Definition nativeerror.h:8