Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
Assembler.cpp
Go to the documentation of this file.
1
4
5#include "Assembler.hpp"
6
7#include <filesystem>
8#include <fstream>
9#include <print>
10#include <sstream>
11
12namespace Phasor
13{
14
15Assembler::Assembler(int argc, char *argv[])
16{
17 parseArguments(argc, argv);
18}
19
21{
22 if (!m_args.noLogo)
23 std::println("Phasor Assembler\nCopyright (c) 2026 Daniel McGuire\n");
24
25 if (m_args.showHelp)
26 {
27 showHelp();
28 return 0;
29 }
30
31 if (assembleBinary()) {
32 if (!m_args.silent) std::println("Success! Output to {}", m_args.outputFile.string());
33 return 0;
34 } else {
35 std::println("Failed to assemble program!");
36 return 1;
37 }
38}
39
40bool Assembler::parseArguments(int argc, char *argv[])
41{
42 m_args.program = std::filesystem::path(argv[0]);
43 for (int i = 1; i < argc; i++)
44 {
45 std::string arg = argv[i];
46
47 if (arg == "-h" || arg == "--help")
48 {
49 m_args.showHelp = true;
50 return true;
51 }
52 else if (arg == "-o" || arg == "--output")
53 {
54 if (i + 1 < argc)
55 {
56 m_args.outputFile = argv[++i];
57 }
58 else
59 {
60 std::println(std::cerr, "Error: {} requires an argument", arg);
61 m_args.showHelp = true;
62 return true;
63 }
64 }
65 else if (arg == "-n" || arg == "--nologo")
66 {
67 m_args.noLogo = true;
68 }
69 if (arg == "-s" || arg == "--silent")
70 {
71 m_args.noLogo = true;
72 m_args.silent = true;
73 }
74 else if (arg[0] == '-')
75 {
76 std::println(std::cerr, "Error: Unknown option: {}", arg);
77 m_args.showHelp = true;
78 return true;
79 }
80 else
81 {
82 if (m_args.inputFile.empty())
83 m_args.inputFile = arg;
84 else
85 {
86 std::println(std::cerr, "Error: Multiple input files specified");
87 m_args.showHelp = true;
88 return true;
89 }
90 }
91 }
92 return false;
93}
94
96{
97 std::println("Usage:\n"
98 " {} [options] <input.phsb>\n\n"
99 "Options:\n"
100 " -o, --output <file> Output file\n"
101 " -h, --help Show this help message\n"
102 " -n, --nologo Do not show banner\n"
103 " -s, --silent Do not print anything except errors (no stdout)\n", m_args.program.stem().string());
104}
105
107{
108 BytecodeSerializer bcSerializer{};
109 PhasorIR phir;
110
111 if (m_args.outputFile.empty())
112 {
113 m_args.outputFile = m_args.inputFile;
114 std::filesystem::path path(m_args.outputFile);
115 path.replace_extension(".phsb");
116 m_args.outputFile = path.string();
117 }
118
119 return bcSerializer.saveToFile(phir.loadFromFile(m_args.inputFile), m_args.outputFile);
120}
121
122} // namespace Phasor
struct Phasor::Assembler::Args m_args
Assembler(int argc, char *argv[])
Definition Assembler.cpp:15
bool parseArguments(int argc, char *argv[])
Definition Assembler.cpp:40
Bytecode binary format serializer.
bool saveToFile(const Bytecode &bytecode, const std::filesystem::path &filename)
Save bytecode to .phsb file.
Phasor IR Serializer/Deserializer.
Definition PhasorIR.hpp:18
static Bytecode loadFromFile(const std::filesystem::path &filename)
Load bytecode from .phir file.
Definition PhasorIR.cpp:641
The Phasor Programming Language and Runtime.
Definition AST.hpp:11