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();
49 catch (
const std::exception &e)
51 std::string errorMsg = std::string(e.what()) +
"\n";
64 auto program = parser.
parse();
68 std::cout <<
"AST:\n";
72 auto bytecode = codegen.
generate(*program);
74 return vm.
run(bytecode);
79 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 else if (arg ==
"-h" || arg ==
"--help")
124 defaultArgLocation = i;
129 m_args.scriptArgv = argv + defaultArgLocation;
130 m_args.scriptArgc = argc - defaultArgLocation;
135 std::cout <<
"Usage:\n" <<
" " <<
m_args.program.stem().string() <<
" [inFile] [...script args]\n\n";
Code generator for Phasor VM.
Bytecode generate(const AST::Program &program, const std::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.
static char ** envp
Environment variables.
int run(const Bytecode &bytecode)
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()
Interpreter(int argc, char *argv[], char *envp[])
struct pulsar::Interpreter::Args m_args
The Pulsar Scripting Language.