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