Phasor 2.2.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Phasor_main.cpp
Go to the documentation of this file.
5#include <iostream>
6#include <string>
7#include <vector>
8#include <filesystem>
9#include <iterator>
10
11#ifdef _WIN32
12#include <io.h>
13#define IS_TERMINAL _isatty(_fileno(stdin))
14#else
15#include <unistd.h>
16#define IS_TERMINAL isatty(fileno(stdin))
17#endif
18
22std::string readStdin() {
23 std::string content;
24 std::string line;
25 while (std::getline(std::cin, line))
26 {
27 content += line + "\n";
28 }
29 return content;
30}
31
32namespace fs = std::filesystem;
33
34void showHelp(const fs::path &program = "phasor")
35{
36 const std::string programName = program.stem().string();
37 std::cout << "Phasor Programming Language\n";
38 std::cout << "Usage: [RAWSCRIPT] | " << programName << " [SCRIPT, BYTECODE]\n";
39 std::cout << "A. PIPE: <text> | " << programName << "\n";
40 std::cout << "B. JIT/BYTECODE: " << programName << " <file>\n";
41 std::cout << "C. REPL: " << programName << "\n\n";
42 std::cout << "Example:\n";
43
44#ifdef _WIN32
45 std::cout << "A. CMD: echo \"print(^\"Hi\\!\\n^\");\" | " << programName << "\n";
46 std::cout << "A. PWSH: echo \"print(`\"Hi\\!\n`\");\" | " << programName << "\n";
47 std::cout << "B. " << programName << " hello.phs\n";
48 std::cout << "B. " << programName << " hello.phsb" << std::endl;
49#else
50 std::cout << "A. echo \"print(\\\"Hi\\!\\n\\\");\" | " << programName << "\n";
51 std::cout << "B. " << programName << " hello.phs\n";
52 std::cout << "B. " << programName << " hello.phsb" << std::endl;
53#endif
54}
55
56int main(int argc, char *argv[], char *envp[])
57{
58 try {
59 if (!IS_TERMINAL) {
60 const std::string source = readStdin();
61 if (!source.empty()) {
62 Phasor::ScriptingRuntime ScriptRT(argc, argv, envp);
64 return 0;
65 }
66 }
67 if (argc < 2) {
68 Phasor::Repl Repl(argc, argv, envp);
69 return Repl.run();
70 }
71
72 const fs::path program = argv[0];
73 const fs::path file = argv[1];
74
75 if (!fs::exists(file)) {
76 const std::string raw = file.string();
77 if (!raw.empty() && (raw.front() == '-' || raw.front() == '/')) {
78 std::string m_path = raw;
79 m_path.erase(0, m_path.find_first_not_of("-/"));
80 if (m_path == "help" || m_path == "h" || m_path == "?" || m_path == "h" || m_path == "help") {
81 showHelp(program);
82 return 0;
83 }
84 std::cerr << "Invalid argument: " << m_path << "\n";
85 } else
86 std::cerr << "File not found: " << raw << "\n";
87 return 1;
88 }
89
90
91 const std::string ext = file.extension().string();
92
93 if (ext == ".phs") {
94 Phasor::ScriptingRuntime ScriptRT(argc, argv, envp);
95 return ScriptRT.run();
96 } else if (ext == ".phsb") {
97 Phasor::BinaryRuntime BinRT(argc, argv, envp);
98 return BinRT.run();
99 } else if (ext == ".phir") {
100 std::cout << "Phasor IR (.phir) compilation not yet implemented.\n";
101 return 0;
102 } else {
103 std::cerr << "Unknown extension: " << ext << "\n";
104 return 1;
105 }
106 }
107 catch (const std::exception &e)
108 {
109 std::cerr << "Error: " << e.what() << "\n";
110 return 1;
111 }
112
113 return 1;
114}
std::string readStdin()
Reads all content from stdin until EOF (piped input).
#define IS_TERMINAL
void showHelp(const fs::path &program="phasor")
fn main()
The main execution loop of the Phasor Shell.
Definition Shell.dox:38
CLI wrapper for running Phasor bytecode binaries.
Read-Eval-Print Loop for Phasor Programming Language.
Definition Repl.hpp:27
int run()
Definition Repl.cpp:25
CLI wrapper for running Phasor scripts.
void runScript(const std::string &source, VM *vm=nullptr)
Run a script.
Definition Frontend.cpp:33