25 if (
m_args.inputFile.empty())
33 std::ifstream file(
m_args.inputFile);
36 std::cerr <<
"Could not open file: " <<
m_args.inputFile <<
"\n";
40 std::stringstream buffer;
41 buffer << file.rdbuf();
42 std::string source = buffer.str();
50 catch (
const std::exception &e)
52 std::string errorMsg = std::string(e.what()) +
"\n";
65 auto program = parser.
parse();
75 auto bytecode = codegen.
generate(*program);
77 return vm.
run(bytecode);
82 auto vm = std::make_unique<VM>();
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/");
95 vm->setImportHandler([
this, vm_ptr = vm.get()](
const std::filesystem::path &path) {
96 std::ifstream file(path);
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);
109 int defaultArgLocation = 1;
110 for (
int i = 1; i < argc; i++)
112 std::string arg = argv[i];
114 if (arg ==
"-v" || arg ==
"--verbose")
118 if (arg ==
"-c" || arg ==
"--command")
122 int ret = vm->getStatus();
125 else if (arg ==
"-h" || arg ==
"--help")
132 defaultArgLocation = i;
137 m_args.scriptArgv = argv + defaultArgLocation;
138 m_args.scriptArgc = argc - defaultArgLocation;
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"
147 " {} [options] [file.phs] [...script args]\n\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);
Code generator for Phasor VM.
Bytecode generate(const AST::Program &program, const std::unordered_map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
std::vector< Token > tokenize()
std::unique_ptr< AST::Program > parse()
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.
static void registerFunctions(VM &vm)
static int argc
Number of command line arguments.
int run(const Bytecode &bytecode, const size_t startPC=0)
Run the virtual machine Exits -1 on uncaught exception.
The Phasor Programming Language and Runtime.