Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Compiler.cpp
Go to the documentation of this file.
1#include "Compiler.hpp"
7#include <filesystem>
8#include <fstream>
9#include <print>
10#include <sstream>
11
12namespace pulsar
13{
14
15Compiler::Compiler(int argc, char *argv[], char *envp[])
16{
17 m_args.envp = envp;
18 parseArguments(argc, argv);
19}
20
21int Compiler::run()
22{
23 if (m_args.showLogo)
24 std::println("Pulsar Compiler\n(C) 2026 Daniel McGuire\n");
25 if (m_args.inputFile.empty())
26 {
27 std::println(std::cerr, "Error: No input file provided");
28 return 1;
29 }
30
31 if (m_args.irMode)
32 return compileToIR();
33
34 return compileToBytecode();
35}
36
38{
39 if (std::filesystem::path(m_args.inputFile).extension() == ".phsb")
40 {
41 std::println(std::cerr, "Error: Cannot compile a bytecode file");
42 return 1;
43 }
44
45 std::ifstream file(m_args.inputFile);
46 if (!file.is_open())
47 {
48 std::println(std::cerr, "Could not open file: {}", m_args.inputFile);
49 return 1;
50 }
51
52 std::stringstream buffer;
53 buffer << file.rdbuf();
54 std::string source = buffer.str();
55
56 try
57 {
58 Lexer lexer(source);
59 Parser parser(lexer.tokenize());
60 auto program = parser.parse();
61 CodeGenerator codegen;
62 auto bytecode = codegen.generate(*program);
63
64 if (m_args.outputFile.empty())
65 {
67 std::filesystem::path path(m_args.outputFile);
68 path.replace_extension(".phsb");
69 m_args.outputFile = path.string();
70 }
71
72 BytecodeSerializer serializer;
73 if (!serializer.saveToFile(bytecode, m_args.outputFile))
74 {
75 std::println(std::cerr, "Failed to save bytecode to: {}", m_args.outputFile);
76 return 1;
77 }
78
79 if (m_args.showLogo)
80 std::println("Compiled successfully: {} -> {}", m_args.inputFile, m_args.outputFile);
81 return 0;
82 }
83 catch (const std::exception &e)
84 {
85 std::println(std::cerr, "Compilation Error: {}", e.what());
86 return 1;
87 }
88}
89
91{
92 if (std::filesystem::path(m_args.inputFile).extension() == ".phir")
93 {
94 std::println(std::cerr, "Error: Cannot compile a Phasor IR file");
95 return 1;
96 }
97
98 std::ifstream file(m_args.inputFile);
99 if (!file.is_open())
100 {
101 std::println(std::cerr, "Could not open file: {}", m_args.inputFile);
102 return 1;
103 }
104
105 std::stringstream buffer;
106 buffer << file.rdbuf();
107 std::string source = buffer.str();
108
109 try
110 {
111 Lexer lexer(source);
112 Parser parser(lexer.tokenize());
113 auto program = parser.parse();
114 CodeGenerator codegen;
115 auto bytecode = codegen.generate(*program);
116
117 if (m_args.outputFile.empty())
118 {
120 std::filesystem::path path(m_args.outputFile);
121 path.replace_extension(".phir");
122 m_args.outputFile = path.string();
123 }
124
125 if (!PhasorIR::saveToFile(bytecode, m_args.outputFile))
126 {
127 std::println(std::cerr, "Failed to save Phasor IR to: {}", m_args.outputFile);
128 return 1;
129 }
130
131 std::println("Compiled successfully to IR: {} -> {}", m_args.inputFile, m_args.outputFile);
132 return 0;
133 }
134 catch (const std::exception &e)
135 {
136 std::println(std::cerr, "Compilation Error: {}", e.what());
137 return 1;
138 }
139}
140
141void Compiler::parseArguments(int argc, char *argv[])
142{
143 int defaultArgLocation = 1;
144 for (int i = 1; i < argc; i++)
145 {
146 std::string arg = argv[i];
147
148 if (arg == "-v" || arg == "--verbose")
149 {
150 m_args.verbose = true;
151 }
152 else if (arg == "--no-logo")
153 {
154 m_args.showLogo = false;
155 }
156 else if (arg == "-o" || arg == "--output")
157 {
158 if (i + 1 < argc)
159 {
160 m_args.outputFile = argv[++i];
161 }
162 else
163 {
164 std::println(std::cerr, "Error: {} requires an argument", arg);
165 exit(1);
166 }
167 }
168 else if (arg == "-i" || arg == "--ir")
169 {
170 m_args.irMode = true;
171 }
172 else if (arg == "-h" || arg == "--help")
173 {
174 showHelp(argv[0]);
175 exit(0);
176 }
177 else
178 {
179 defaultArgLocation = i;
180 m_args.inputFile = arg;
181 break; // Stop parsing after finding the input file
182 }
183 }
184 m_args.scriptArgv = argv + defaultArgLocation;
185 m_args.scriptArgc = argc - defaultArgLocation;
186}
187
188void Compiler::showHelp(const std::string &programName)
189{
190 std::string filename = std::filesystem::path(programName).filename().string();
191 std::println("Pulsar Compiler\n\n"
192 "Usage:\n"
193 " {} [options] <file>\n\n"
194 "Options:\n"
195 " -o, --output FILE Specify output file\n"
196 " -i, --ir Compile to IR format (.phir) instead of bytecode\n"
197 " -v, --verbose Enable verbose output\n"
198 " -h, --help Show this help message", filename);
199}
200
201} // namespace pulsar
Bytecode binary format serializer.
bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phsb file.
Code generator for Phasor VM.
Definition CodeGen.hpp:90
Bytecode generate(const AST::Program &program, const std::map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
Definition CodeGen.cpp:7
struct Phasor::Compiler::Args m_args
void parseArguments(int argc, char *argv[])
Definition Compiler.cpp:142
int compileToBytecode()
Definition Compiler.cpp:38
void showHelp(const std::string &programName)
Definition Compiler.cpp:189
Lexer.
Definition Lexer.hpp:13
std::vector< Token > tokenize()
Definition Lexer.cpp:26
Parser.
Definition Parser.hpp:13
std::unique_ptr< AST::Program > parse()
Definition Parser.cpp:73
static bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phir file.
Definition PhasorIR.cpp:624
Compiler(int argc, char *argv[], char *envp[])
The Pulsar Scripting Language.
Definition Compiler.cpp:13
std::string inputFile
Definition Compiler.hpp:24
std::string outputFile
Definition Compiler.hpp:25