Phasor 3.1.1
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#ifdef _WIN32
11#include <io.h>
12#define IS_TERMINAL _isatty(_fileno(stdin))
13#else
14#include <unistd.h>
15#define IS_TERMINAL isatty(fileno(stdin))
16#endif
17
21std::string readStdin()
22{
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::println("Phasor Programming Language\n"
38 "Usage: [RAWSCRIPT] | {} [SCRIPT, BYTECODE]\n"
39 "A. PIPE: <text> | {}\n"
40 "B. JIT/BYTECODE: {} <file>\n"
41 "C. REPL: {}\n\n"
42 "Example:", programName, programName, programName, programName);
43
44#ifdef _WIN32
45 std::println("A. CMD: echo \"print(^\"Hi\\!\\n^);\" | {}\n"
46 "A. PWSH: echo \"print(`\"Hi\\!\\n`);\" | {}\n"
47 "B. {} hello.phs\n"
48 "B. {} hello.phsb", programName, programName, programName, programName);
49#else
50 std::println("A. echo \"print(\\\"Hi\\!\\n\\\");\" | {}\n"
51 "B. {} hello.phs\n"
52 "B. {} hello.phsb", programName, programName, programName);
53#endif
54}
55
56int main(int argc, char *argv[], char *envp[])
57{
58 try
59 {
60 if (!IS_TERMINAL)
61 {
62 const std::string source = readStdin();
63 if (!source.empty())
64 {
65 Phasor::ScriptingRuntime ScriptRT(argc, argv, envp);
66 return Phasor::Frontend::runScript(source, nullptr, "");
67 }
68 }
69 if (argc < 2)
70 {
72 }
73
74 const fs::path program = argv[0];
75 const fs::path file = argv[1];
76
77 if (!fs::exists(file))
78 {
79 const std::string raw = file.string();
80 if (!raw.empty() && (raw.front() == '-' || raw.front() == '/'))
81 {
82 std::string m_path = raw;
83 m_path.erase(0, m_path.find_first_not_of("-/"));
84 if (m_path == "help" || m_path == "h" || m_path == "?" || m_path == "h" || m_path == "help")
85 {
86 showHelp(program);
87 return 0;
88 }
89 std::println(std::cerr, "Invalid argument: {}", m_path);
90 }
91 else
92 std::println(std::cerr, "File not found: {}", raw);
93 return 1;
94 }
95
96 const std::string ext = file.extension().string();
97
98 if (ext == ".phs")
99 {
100 Phasor::ScriptingRuntime ScriptRT(argc, argv, envp);
101 return ScriptRT.run();
102 }
103 else if (ext == ".phsb")
104 {
105 Phasor::BinaryRuntime BinRT(argc, argv, envp);
106 return BinRT.run();
107 }
108 else if (ext == ".phir")
109 {
110 std::println("Phasor IR (.phir) compilation not yet implemented.");
111 return 0;
112 }
113 else
114 {
115 std::println(std::cerr, "Unknown extension: {}", ext);
116 return 1;
117 }
118 }
119 catch (const std::exception &e)
120 {
121 std::println(std::cerr, "Error: {}", e.what());
122 return 1;
123 }
124
125 return 1;
126}
int main()
Definition LSP_main.cpp:121
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:87
int runScript(const std::string &source, VM *vm, const std::filesystem::path &path="", bool verbose=false)
Run a script.
Definition Frontend.cpp:17