Assembler #6
1
.idea/editor.xml
generated
1
.idea/editor.xml
generated
@@ -27,6 +27,7 @@
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_EMBEDDED_STATEMENT_STYLE/@EntryValue" value="LINE_BREAK" type="string" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="true" type="bool" />
|
||||
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_LINES/@EntryValue" value="false" type="bool" />
|
||||
<option name="/Default/CodeStyle/Generate/=CppEqualityOperators/Options/=GenerateSecondaryOperators/@EntryIndexedValue" value="False" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNaming/Rules/=Classes_0020and_0020structs/@EntryIndexedValue" value="<NamingElement Priority="1"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="__interface" /><type Name="class" /><type Name="struct" /></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNaming/Rules/=Concepts/@EntryIndexedValue" value="<NamingElement Priority="2"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="concept" /></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
<option name="/Default/CodeStyle/Naming/CppNaming/Rules/=Enum_0020members/@EntryIndexedValue" value="<NamingElement Priority="14"><Descriptor Static="Indeterminate" Constexpr="Indeterminate" Const="Indeterminate" Volatile="Indeterminate" Accessibility="NOT_APPLICABLE"><type Name="scoped enumerator" /><type Name="unscoped enumerator" /></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></NamingElement>" type="string" />
|
||||
|
||||
@@ -2,6 +2,14 @@
|
||||
|
||||
namespace tyche::as {
|
||||
|
||||
Assembler::Assembler(std::string const& source)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ByteArray Assembler::assemble() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
} // tyche
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
#ifndef TYCHE_ASSEMBLER_HH
|
||||
#define TYCHE_ASSEMBLER_HH
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../common/bytearray.hh"
|
||||
|
||||
namespace tyche::as {
|
||||
|
||||
class Assembler {
|
||||
public:
|
||||
explicit Assembler(std::string const& source);
|
||||
|
||||
[[nodiscard]] ByteArray assemble() const;
|
||||
};
|
||||
|
||||
} // tyche
|
||||
|
||||
@@ -1,5 +1,52 @@
|
||||
#include "assembler.hh"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "../bytecode/bytecodeprototype.hh"
|
||||
#include "../bytecode/bytecode.hh"
|
||||
#include "../vm/instruction.hh"
|
||||
|
||||
using namespace tyche;
|
||||
using namespace tyche::as;
|
||||
using namespace tyche::bc;
|
||||
using namespace tyche::vm;
|
||||
|
||||
TEST(Assember, Assembler)
|
||||
{
|
||||
BytecodePrototype bp;
|
||||
bp.constants.emplace_back(3.14f);
|
||||
bp.constants.emplace_back("Hello world");
|
||||
bp.functions.emplace_back(0, 0);
|
||||
bp.functions.at(0).code.append_byte((uint8_t) Instruction::PushInt8);
|
||||
bp.functions.at(0).code.append_int8(2);
|
||||
bp.functions.at(0).code.append_byte((uint8_t) Instruction::PushInt8);
|
||||
bp.functions.at(0).code.append_int8(3);
|
||||
bp.functions.at(0).code.append_byte((uint8_t) Instruction::Sum);
|
||||
bp.functions.at(0).code.append_byte((uint8_t) Instruction::Return);
|
||||
bp.functions.emplace_back(0, 0);
|
||||
bp.functions.at(1).code.append_byte((uint8_t) Instruction::PushInt16);
|
||||
bp.functions.at(1).code.append_int16(5000);
|
||||
bp.functions.at(1).code.append_byte((uint8_t) Instruction::Return);
|
||||
ByteArray expected = Bytecode::generate(bp);
|
||||
|
||||
std::string src = R"(
|
||||
.const
|
||||
0: 3.14
|
||||
1: "Hello world"
|
||||
|
||||
.function 0
|
||||
pushi 2
|
||||
pushi 3
|
||||
sum
|
||||
ret
|
||||
.function 1
|
||||
pushi 5000
|
||||
ret
|
||||
)";
|
||||
|
||||
ByteArray actual = Assembler(src).assemble();
|
||||
ASSERT_EQ(expected, actual);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
@@ -47,6 +47,8 @@ public:
|
||||
|
||||
[[nodiscard]] std::string hexdump() const;
|
||||
|
||||
friend bool operator==(ByteArray const& lhs, ByteArray const& rhs) { return lhs.data_ == rhs.data_; }
|
||||
|
||||
private:
|
||||
std::vector<uint8_t> data_ {};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user