MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
instruct.cpp
Go to the documentation of this file.
1
6#include "mxvm/instruct.hpp"
7#include <sstream>
8
9namespace mxvm {
10 std::vector<std::string> keywords{"program", "object", "module", "data", "code", "section"};
11
12}
13
14std::ostream &operator<<(std::ostream &out, const mxvm::Keywords &key) {
15 size_t i = static_cast<size_t>(key);
16 if (i < mxvm::keywords.size())
17 out << mxvm::keywords[i];
18 else
19 out << "Error";
20 return out;
21}
22
23std::ostream &operator<<(std::ostream &out, const enum Inc &i) {
24 size_t pos = static_cast<size_t>(i);
25 if (pos < IncType.size())
26 out << IncType[pos];
27 else
28 out << "[undefined]";
29 return out;
30}
31
32std::ostream &operator<<(std::ostream &out, const mxvm::Instruction &inc) {
33 out << "[" << inc.instruction;
34 std::ostringstream opc;
35 if (!inc.op1.op.empty())
36 opc << " " << inc.op1.op;
37 if (!inc.op2.op.empty())
38 opc << ", " << inc.op2.op;
39 if (!inc.op3.op.empty())
40 opc << ", " << inc.op3.op;
41 for (const auto &arg : inc.vop) {
42 if (!arg.op.empty())
43 opc << ", " << arg.op;
44 }
45 if (opc.str().length() > 0) {
46 out << opc.str() << "]";
47 } else {
48 out << "]";
49 }
50
51 return out;
52}
53
54std::ostream &operator<<(std::ostream &out, const mxvm::VarType &type) {
55 switch (type) {
57 out << "int";
58 break;
59
61 out << "string";
62 break;
64 out << "extern";
65 break;
67 out << "ptr";
68 break;
70 out << "float";
71 break;
73 out << "byte";
74 break;
75
76 default:
77 out << "unknown";
78 }
79 return out;
80}
81
82namespace mxvm {
83
84 std::string Instruction::toString() const {
85 std::ostringstream stream;
86 stream << *this;
87 return stream.str();
88 }
89
90 std::string Variable::toString() const {
91 std::ostringstream stream;
92 stream << type;
93 return stream.str();
94 }
95} // namespace mxvm
int64_t pos(const char *substr, const char *s)
Definition cstring.c:51
std::ostream & operator<<(std::ostream &out, const mxvm::Keywords &key)
Stream insertion operator for Keywords.
Definition instruct.cpp:14
Instruction set enum, operand/instruction structs, variable types, and Variable/Variable_Value defini...
std::vector< std::string > IncType
String representations of Inc opcodes, indexed by enum value.
Definition instruct.hpp:82
Inc
MXVM instruction set opcodes.
Definition instruct.hpp:21
Definition ast.hpp:14
Keywords
MXVM language keywords.
Definition instruct.hpp:192
VarType
MXVM variable type discriminator.
Definition instruct.hpp:180
std::vector< std::string > keywords
String representations of Keywords enum values.
Definition instruct.cpp:10
A complete MXVM instruction with opcode, operands, and optional label.
Definition instruct.hpp:168
std::string toString() const
Convert this instruction to its assembly text representation.
Definition instruct.cpp:84
std::vector< Operand > vop
variable-length operand list (for print, invoke, etc.)
Definition instruct.hpp:171
Inc instruction
the opcode
Definition instruct.hpp:169
Operand op3
up to three fixed operands
Definition instruct.hpp:170
std::string op
textual operand value
Definition instruct.hpp:161
std::string toString() const
Convert this variable to a human-readable string.
Definition instruct.cpp:90