Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
Assembler.cpp
Go to the documentation of this file.
1
4#include <version.h>
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.showHelp)
23 {
24 showHelp();
25 return 0;
26 }
27
28 if (assembleBinary())
29 {
30 if (!m_args.silent)
31 std::println("Success! Output to {}", m_args.outputFile.string());
32 return 0;
33 }
34 else
35 {
36 std::println("Failed to assemble program!");
37 return 1;
38 }
39}
40
41bool Assembler::parseArguments(int argc, char *argv[])
42{
43 m_args.program = std::filesystem::path(argv[0]);
44 for (int i = 1; i < argc; i++)
45 {
46 std::string arg = argv[i];
47
48 if (arg == "-h" || arg == "--help")
49 {
50 m_args.showHelp = true;
51 return true;
52 }
53 else if (arg == "-o" || arg == "--output")
54 {
55 if (i + 1 < argc)
56 {
57 m_args.outputFile = argv[++i];
58 }
59 else
60 {
61 std::println(std::cerr, "Error: {} requires an argument", arg);
62 m_args.showHelp = true;
63 return true;
64 }
65 }
66 if (arg == "-s" || arg == "--silent")
67 {
68 m_args.silent = true;
69 }
70 else if (arg[0] == '-')
71 {
72 std::println(std::cerr, "Error: Unknown option: {}", arg);
73 m_args.showHelp = true;
74 return true;
75 }
76 else
77 {
78 if (m_args.inputFile.empty())
79 m_args.inputFile = arg;
80 else
81 {
82 std::println(std::cerr, "Error: Multiple input files specified");
83 m_args.showHelp = true;
84 return true;
85 }
86 }
87 }
88 return false;
89}
90
92{
93 std::println("Phasor Assembler v{}\n"
94 "(C) 2026 Daniel McGuire - Licensed under Apache 2.0\n\n"
95 "Usage:\n"
96 " {} [options] <input.phsb>\n\n"
97 "Options:\n"
98 " -o, --output <file> Output file\n"
99 " -h, --help Show this help message\n"
100 " -s, --silent Do not print anything except errors (no stdout)",
101 PHASOR_VERSION_STRING, m_args.program.stem().string());
102}
103
105{
106 BytecodeSerializer bcSerializer{};
107 PhasorIR phir;
108
109 if (m_args.outputFile.empty())
110 {
111 m_args.outputFile = m_args.inputFile;
112 std::filesystem::path path(m_args.outputFile);
113 path.replace_extension(".phsb");
114 m_args.outputFile = path.string();
115 }
116
117 return bcSerializer.saveToFile(phir.loadFromFile(m_args.inputFile), m_args.outputFile);
118}
119
120} // 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:41
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:666
The Phasor Programming Language and Runtime.
Definition AST.hpp:12