Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Phasor_main.cpp
Go to the documentation of this file.
4#include <print>
5#include <string>
6#include <vector>
7#include <filesystem>
8#include <iterator>
9
10#include <version.h>
11
12#ifdef _WIN32
13#include <io.h>
14#define IS_TERMINAL _isatty(_fileno(stdin))
15#else
16#include <unistd.h>
17#define IS_TERMINAL isatty(fileno(stdin))
18#endif
19
23std::string readStdin()
24{
25 std::string content;
26 std::string line;
27 while (std::getline(std::cin, line))
28 {
29 content += line + "\n";
30 }
31 return content;
32}
33
34namespace fs = std::filesystem;
35
36void showHelp(const fs::path &program = "phasor")
37{
38 const std::string programName = program.stem().string();
39 std::println("Phasor Programming Language and Toolchain v{}\n"
40 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
41 "Usage: [RAWSCRIPT] | {} [SCRIPT, BYTECODE]\n"
42 "A. PIPE: <text> | {}\n"
43 "B. JIT/BYTECODE: {} <file>\n"
44 "C. REPL: {}\n\n"
45 "Example:",
46 PHASOR_VERSION_STRING, programName, programName, programName, programName);
47
48#ifdef _WIN32
49 std::println("A. CMD: echo \"print(^\"Hi\\!\\n^\");\" | {}\n"
50 "A. PWSH: \"print(`\"Hi\\!\\n`\");\" | {}\n"
51 "B. {} hello.phs\n"
52 "B. {} hello.phsb",
53 programName, programName, programName, programName);
54#else
55 std::println("A. echo \"print(\\\"Hi\\!\\\\\\\\n\\\");\" | {}\n"
56 "B. {} hello.phs\n"
57 "B. {} hello.phsb",
58 programName, programName, programName);
59#endif
60 std::println(R"(
61Options:
62 -h, --help Show this help message and exit
63 -v, --version Show the version number and exit
64 -c, --command Run a raw script string)");
65}
66
67int main(int argc, char *argv[])
68{
69 try
70 {
71 if (!IS_TERMINAL)
72 {
73 const std::string source = readStdin();
74 if (!source.empty())
75 {
76 return Phasor::Frontend::runScript(source, nullptr, "");
77 }
78 }
79 if (argc < 2)
80 {
82 }
83
84 const fs::path programPath = argv[0];
85 const fs::path file = argv[1];
86
87 if (!fs::exists(file))
88 {
89 const std::string raw = file.string();
90 if (!raw.empty() && (raw.front() == '-' || raw.front() == '/'))
91 {
92 std::string m_path = raw;
93 m_path.erase(0, m_path.find_first_not_of("-/"));
94 if (m_path == "help" || m_path == "h" || m_path == "?" || m_path == "h" || m_path == "help")
95 {
96 showHelp(programPath);
97 return 0;
98 }
99 else if (m_path == "version" || m_path == "v")
100 {
101 std::println(PHASOR_VERSION_STRING);
102 return 0;
103 }
104 else if (m_path == "command" || m_path == "c")
105 {
106 Phasor::ScriptingRuntime ScriptRT(argc, argv);
107 auto vm = ScriptRT.createVm();
108 return ScriptRT.runSourceString(argv[2], *vm);
109 }
110 else
111 {
112 std::println(std::cerr, "Invalid argument: {}", m_path);
113 }
114 }
115 else
116 {
117 std::println(std::cerr, "File not found: {}", raw);
118 }
119 return 1;
120 }
121
122 const std::string ext = file.extension().string();
123
124 if (ext == ".phs")
125 {
126 Phasor::ScriptingRuntime ScriptRT(argc, argv);
127 return ScriptRT.run();
128 }
129 else if (ext == ".phsb")
130 {
131 Phasor::BinaryRuntime BinRT(argc, argv);
132 return BinRT.run();
133 }
134 else if (ext == ".phir")
135 {
136 std::println("Phasor IR (.phir) compilation not yet implemented.");
137 return 0;
138 }
139 else
140 {
141 std::println(std::cerr, "Unknown extension: {}", ext);
142 return 1;
143 }
144 }
145 catch (const std::exception &e)
146 {
147 std::println(std::cerr, "Error: {}", e.what());
148 return 1;
149 }
150
151 return 1;
152}
int main()
Definition LSP_main.cpp:130
std::string readStdin()
Reads all content from stdin until EOF (piped input).
#define IS_TERMINAL
void showHelp(const fs::path &program="phasor")
CLI wrapper for running Phasor bytecode binaries.
CLI wrapper for running Phasor scripts.
int runRepl(VM *vm=nullptr, bool verbose=false)
Run an REPL.
Definition Frontend.cpp:96
int runScript(const std::string &source, VM *vm, const std::filesystem::path &path="", bool verbose=false)
Run a script.
Definition Frontend.cpp:17