25 std::println(
"Phasor Compiler\n(C) 2026 Daniel McGuire\n");
26 if (
m_args.inputFile.empty())
28 std::println(std::cerr,
"Error: No input file provided\n");
40 if (std::filesystem::path(
m_args.inputFile).extension() ==
".phsb")
42 std::println(std::cerr,
"Error: Cannot compile a bytecode file (use phasordecomp)");
46 std::ifstream file(
m_args.inputFile);
49 println(std::cerr,
"Could not open file: {}",
m_args.inputFile);
53 std::stringstream buffer;
54 buffer << file.rdbuf();
55 std::string source = buffer.str();
61 auto program = parser.
parse();
63 auto bytecode = codegen.
generate(*program);
65 if (
m_args.outputFile.empty())
68 std::filesystem::path path(
m_args.outputFile);
69 path.replace_extension(
".phsb");
70 m_args.outputFile = path.string();
76 std::println(std::cerr,
"Failed to save bytecode to: {}",
m_args.outputFile);
81 std::println(
"Compiled successfully: {} -> {}",
m_args.inputFile,
m_args.outputFile);
84 catch (
const std::exception &e)
86 std::println(std::cerr,
"Compilation Error: {}", e.what());
93 if (std::filesystem::path(
m_args.inputFile).extension() ==
".phir")
95 std::println(std::cerr,
"Error: Cannot compile a Phasor IR file (use phasorasm)");
99 std::ifstream file(
m_args.inputFile);
102 std::println(std::cerr,
"Could not open file: {}\n",
m_args.inputFile);
106 std::stringstream buffer;
107 buffer << file.rdbuf();
108 std::string source = buffer.str();
114 auto program = parser.
parse();
116 auto bytecode = codegen.
generate(*program);
118 if (
m_args.outputFile.empty())
121 std::filesystem::path path(
m_args.outputFile);
122 path.replace_extension(
".phir");
123 m_args.outputFile = path.string();
128 std::println(std::cerr,
"Failed to save Phasor IR to: {}",
m_args.outputFile);
132 std::println(
"Compiled successfully to IR: {} -> {}",
m_args.inputFile,
m_args.outputFile);
135 catch (
const std::exception &e)
137 std::println(std::cerr,
"Compilation Error: {}", e.what());
144 int defaultArgLocation = 1;
145 for (
int i = 1; i < argc; i++)
147 std::string arg = argv[i];
149 if (arg ==
"-v" || arg ==
"--verbose")
153 else if (arg ==
"--no-logo")
157 else if (arg ==
"-o" || arg ==
"--output")
161 m_args.outputFile = argv[++i];
165 std::print(std::cerr,
"Error: {} requires an argument", arg);
169 else if (arg ==
"-i" || arg ==
"--ir")
173 else if (arg ==
"-h" || arg ==
"--help")
180 defaultArgLocation = i;
185 m_args.scriptArgv = argv + defaultArgLocation;
186 m_args.scriptArgc = argc - defaultArgLocation;
191 std::string filename = std::filesystem::path(programName).filename().string();
193 std::println(
"Phasor Compiler\n\nUsage:\n"
194 " {} [options] <file.phs>\n\n"
195 "Options:\n -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);
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.
struct Phasor::Compiler::Args m_args
void parseArguments(int argc, char *argv[])
Compiler(int argc, char *argv[], char *envp[])
void showHelp(const std::string &programName)
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.