23 if (
m_args.showLogo) std::cout <<
"Phasor Compiler\n(C) 2026 Daniel McGuire\n\n";
24 if (
m_args.inputFile.empty())
26 std::cerr <<
"Error: No input file provided\n";
38 if (std::filesystem::path(
m_args.inputFile).extension() ==
".phsb")
40 std::cerr <<
"Error: Cannot compile a bytecode file\n";
44 std::ifstream file(
m_args.inputFile);
47 std::cerr <<
"Could not open file: " <<
m_args.inputFile <<
"\n";
51 std::stringstream buffer;
52 buffer << file.rdbuf();
53 std::string source = buffer.str();
59 auto program = parser.
parse();
61 auto bytecode = codegen.
generate(*program);
63 if (
m_args.outputFile.empty())
66 std::filesystem::path path(
m_args.outputFile);
67 path.replace_extension(
".phsb");
68 m_args.outputFile = path.string();
74 std::cerr <<
"Failed to save bytecode to: " <<
m_args.outputFile <<
"\n";
78 if (
m_args.showLogo) std::cout <<
"Compiled successfully: " <<
m_args.inputFile <<
" -> " <<
m_args.outputFile <<
"\n";
81 catch (
const std::exception &e)
83 std::cerr <<
"Compilation Error: " << e.what() <<
"\n";
90 if (std::filesystem::path(
m_args.inputFile).extension() ==
".phir")
92 std::cerr <<
"Error: Cannot compile a Phasor IR file\n";
96 std::ifstream file(
m_args.inputFile);
99 std::cerr <<
"Could not open file: " <<
m_args.inputFile <<
"\n";
103 std::stringstream buffer;
104 buffer << file.rdbuf();
105 std::string source = buffer.str();
111 auto program = parser.
parse();
113 auto bytecode = codegen.
generate(*program);
115 if (
m_args.outputFile.empty())
118 std::filesystem::path path(
m_args.outputFile);
119 path.replace_extension(
".phir");
120 m_args.outputFile = path.string();
125 std::cerr <<
"Failed to save Phasor IR to: " <<
m_args.outputFile <<
"\n";
129 std::cout <<
"Compiled successfully to IR: " <<
m_args.inputFile <<
" -> " <<
m_args.outputFile <<
"\n";
132 catch (
const std::exception &e)
134 std::cerr <<
"Compilation Error: " << e.what() <<
"\n";
141 int defaultArgLocation = 1;
142 for (
int i = 1; i < argc; i++)
144 std::string arg = argv[i];
146 if (arg ==
"-v" || arg ==
"--verbose")
150 else if (arg ==
"--no-logo")
154 else if (arg ==
"-o" || arg ==
"--output")
158 m_args.outputFile = argv[++i];
162 std::cerr <<
"Error: " << arg <<
" requires an argument\n";
166 else if (arg ==
"-i" || arg ==
"--ir")
170 else if (arg ==
"-h" || arg ==
"--help")
177 defaultArgLocation = i;
182 m_args.scriptArgv = argv + defaultArgLocation;
183 m_args.scriptArgc = argc - defaultArgLocation;
188 std::string filename = std::filesystem::path(programName).filename().string();
189 std::cout <<
"Phasor Compiler\n\n";
190 std::cout <<
"Usage:\n";
191 std::cout <<
" " << filename <<
" [options] <file.phs>\n\n";
192 std::cout <<
"Options:\n";
193 std::cout <<
" -o, --output FILE Specify output file\n";
194 std::cout <<
" -i, --ir Compile to IR format (.phir) instead of bytecode\n";
195 std::cout <<
" -v, --verbose Enable verbose output\n";
196 std::cout <<
" -h, --help Show this help message\n";
Bytecode binary format serializer.
bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phsb file.
Code generator for Phasor VM.
Bytecode generate(const AST::Program &program, const std::map< std::string, int > &existingVars={}, int nextVarIdx=0, bool replMode=false)
Generate bytecode from program.
Compiler(int argc, char *argv[], char *envp[])
void showHelp(const std::string &programName)
void parseArguments(int argc, char *argv[])
struct Phasor::Compiler::Args m_args
std::vector< Token > tokenize()
std::unique_ptr< AST::Program > parse()
static bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phir file.
The Phasor Programming Language and Runtime.