26 if (
m_args.inputFile.empty())
34 std::ifstream file(
m_args.inputFile);
37 std::cerr <<
"Could not open file: " <<
m_args.inputFile <<
"\n";
41 std::stringstream buffer;
42 buffer << file.rdbuf();
43 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();
73 auto bytecode = codegen.
generate(*program);
75 return vm.
run(bytecode);
80 auto vm = std::make_unique<Phasor::VM>();
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");
93 vm->setImportHandler([
this, vm_ptr = vm.get()](
const std::filesystem::path &path) {
94 std::ifstream file(path);
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);
107 m_args.program = std::filesystem::path(argv[0]);
108 int defaultArgLocation = 1;
109 for (
int i = 1; i < argc; i++)
111 std::string arg = argv[i];
113 if (arg ==
"-v" || arg ==
"--verbose")
117 if (arg ==
"-c" || arg ==
"--command")
121 int ret = vm->getStatus();
124 else if (arg ==
"-h" || arg ==
"--help")
129 else if (arg ==
"--version")
131 std::println(PHASOR_VERSION_STRING);
136 defaultArgLocation = i;
141 m_args.scriptArgv = argv + defaultArgLocation;
142 m_args.scriptArgc = argc - defaultArgLocation;
147 std::println(
"Pulsar Scripting Language (Phasor v{})\n"
148 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
150 " {} [inFile] [...script args]\n\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());
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()
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.
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.