get_interpreter
| Definition: | string s = get_interpreter(bool bQuote=false, object mb=machine_bits(), integer plat=platform(), bool bPrefW=false) |
| Description: | Returns the full path and filename of a Phix executable, eg `C:\Program Files (x86)\Phix\pw.exe`, or "" if no suitable interpreter could be found. |
| pwa/p2js: | Not supported. |
| Comments: |
You can find the implementation in builtins\get_interpreter.e (an autoinclude).
Things are usually much simpler when interpreting: there is a valid interpreter readily available in command_line()[1]. However, it also has to work when compiled, and not only on Windows/Linux, 32 and 64 bit, but also when run from anywhere. It is important not to overly-rely on this routine working in situations over which you have no physical control. For instance, releasing a (pre-compiled) app which does not contain an interpreter (and builtins/ etc) and will therefore only work on systems where phix has previously been installed is unlikely to win you any customers, unless you give very clear instructions and/or use a failing get_interpreter() to trigger some kind of automatic download and installation. bQuote: can be true (1) to add double quotes around results containing spaces. mb: primarily for testing purposes, but there may also be cases where you specifically require a 32 or 64 bit interpreter, for example you might (as I do) only have a 32-bit libcurl.dll, so you need to fire off a 32bit interpreter to download some file with that, even when the main app is 64bit. plat: likewise for testing purposes, only in this case of no practical use for anything else that I can think of. bPrefW: causes the search to look for (eg) "pw.exe" before "p.exe" or vice versa, no real effect on Linux. Should a bPrefW of -1 be specified, which is ok since it is a bool rather than a boolean, it is first replaced with true/false depending on whether the existing command_line()[1] contains a 'w'. Note that an integer mb is advisory: if you ask for 32bit but all it can find is 64bit, it will return that, and vice versa, but at least it looks thoroughly. Should that be unacceptable, pass {32} or {64} to be more strict about the result. You can also (manually) create some or all of the files (In contrast, plat is never advisory: not that there is any need for it, or any overlap in the filenames it searches for.) It searches the path from command_line(), cropped such that ../Phix/demo/.. -> ../Phix/, and current_dir() if different, plus anything in %PATH% that ends with "phix" or "bin" (case insensitive). On Windows it also searches `C:\Program Files (x86)\Phix` and `C:\Program Files\Phix`. On Linux it also searches %HOME%/phix. |
| Example: |
procedure run(string filename)
string i = get_interpreter(true), -- eg `"C:\Program Files (x86)\Phix\p.exe"`
-- or perhaps `/home/pete/phix/p` on Linux
cmd = sprintf(`%s "%s"`,{i,filename})
system(cmd)
end procedure
You could of course store it in a constant, or perhaps even better use it merely as a default for an overrideable system setting. |
| Implementation: | See builtins\get_interpreter.e (an autoinclude) for details of the actual implementation. |
| See Also: | command_line, requires, machine_bits, platform, system, system_exec |