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/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/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/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/=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/=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" />
|
<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" />
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ void Lexer::reset()
|
|||||||
|
|
||||||
Token Lexer::peek() const
|
Token Lexer::peek() const
|
||||||
{
|
{
|
||||||
return look_ahead_;
|
return current_token_;
|
||||||
}
|
}
|
||||||
|
|
||||||
Token Lexer::ingest()
|
Token Lexer::ingest()
|
||||||
{
|
{
|
||||||
Token t = look_ahead_;
|
Token t = current_token_;
|
||||||
ingest_next_token();
|
ingest_next_token();
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
@@ -26,21 +26,54 @@ void Lexer::ingest_next_token()
|
|||||||
{
|
{
|
||||||
char c = source_.at(pos_);
|
char c = source_.at(pos_);
|
||||||
|
|
||||||
|
TokenType type {};
|
||||||
|
std::string token;
|
||||||
|
|
||||||
if (pos_ >= source_.size()) {
|
if (pos_ >= source_.size()) {
|
||||||
look_ahead_ = { TokenType::EOF_, "" };
|
type = TokenType::EOF_;
|
||||||
} else if (c == '.') {
|
} else if (c == '.') {
|
||||||
|
type = TokenType::Directive;
|
||||||
|
token += '.';
|
||||||
|
while (c = source_.at(++pos_), isalpha(c) || c == '_')
|
||||||
|
token += c;
|
||||||
} else if (c == '"') {
|
} else if (c == '"') {
|
||||||
|
type = TokenType::String;
|
||||||
} else if (isdigit(c)) {
|
++pos_;
|
||||||
|
while (true) {
|
||||||
|
if (source_.at(pos_) == '\'') { // TODO - improve this for special characters
|
||||||
|
++pos_;
|
||||||
|
} else if (source_.at(pos_) == '"') {
|
||||||
|
++pos_;
|
||||||
|
break;
|
||||||
|
} else if (pos_ >= source_.size()) {
|
||||||
|
throw AssemblyError("Unterminated string");
|
||||||
|
}
|
||||||
|
token += source_.at(pos_++);
|
||||||
|
}
|
||||||
|
} else if (isdigit(c) || c == '-') {
|
||||||
|
type = TokenType::Integer;
|
||||||
|
token += c;
|
||||||
|
while (c = source_.at(++pos_), isdigit(c) || c == '.') {
|
||||||
|
token += c;
|
||||||
|
if (c == '.') {
|
||||||
|
if (type == TokenType::Integer)
|
||||||
|
type = TokenType::Float;
|
||||||
|
else
|
||||||
|
throw AssemblyError("Double point in floating point number");
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (isalpha(c)) {
|
} else if (isalpha(c)) {
|
||||||
|
type = TokenType::Instruction;
|
||||||
|
token += c;
|
||||||
|
while (c = source_.at(++pos_), isalpha(c))
|
||||||
|
token += c;
|
||||||
} else if (c == '\n') {
|
} else if (c == '\n') {
|
||||||
|
type = TokenType::Enter;
|
||||||
} else if (c != ' ' && c != '\t' && c != '\r') {
|
} else if (c != ' ' && c != '\t' && c != '\r') {
|
||||||
throw AssemblyError(std::string("Unexpected character '") + c + "' (ascii: " + std::to_string((int) c) + ")");
|
throw AssemblyError(std::string("Unexpected character '") + c + "' (ascii: " + std::to_string((int) c) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current_token_ = { .type = type, .token = token };
|
||||||
}
|
}
|
||||||
|
|
||||||
} // tyche
|
} // tyche
|
||||||
|
|||||||
@@ -6,11 +6,13 @@
|
|||||||
|
|
||||||
namespace tyche::as {
|
namespace tyche::as {
|
||||||
|
|
||||||
enum class TokenType { BOF, Directive, Instruction, Number, Float, String, Enter, EOF_ };
|
enum class TokenType { BOF, Directive, Instruction, Integer, Float, String, Enter, EOF_ };
|
||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
TokenType type;
|
TokenType type;
|
||||||
std::string token;
|
std::string token;
|
||||||
|
|
||||||
|
friend bool operator==(Token const& lhs, Token const& rhs) { return std::tie(lhs.type, lhs.token) == std::tie(rhs.type, rhs.token); }
|
||||||
};
|
};
|
||||||
|
|
||||||
class Lexer {
|
class Lexer {
|
||||||
@@ -24,7 +26,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
const std::string source_;
|
const std::string source_;
|
||||||
size_t pos_ = 0;
|
size_t pos_ = 0;
|
||||||
Token look_ahead_;
|
Token current_token_ { TokenType::BOF, "" };
|
||||||
|
|
||||||
void ingest_next_token();
|
void ingest_next_token();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,14 @@ using namespace tyche::as;
|
|||||||
using namespace tyche::bc;
|
using namespace tyche::bc;
|
||||||
using namespace tyche::vm;
|
using namespace tyche::vm;
|
||||||
|
|
||||||
|
TEST(Lexer, Lexer)
|
||||||
|
{
|
||||||
|
Lexer lexer(".dir push 382 -12 3.14 -12.8 \"Hello\" \"Hel\"lo\"\n");
|
||||||
|
|
||||||
|
ASSERT_EQ(lexer.ingest(), (Token { TokenType::Directive, ".dir" }));
|
||||||
|
ASSERT_EQ(lexer.ingest(), (Token { TokenType::Instruction, "push" }));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Assember, Assembler)
|
TEST(Assember, Assembler)
|
||||||
{
|
{
|
||||||
BytecodePrototype bp;
|
BytecodePrototype bp;
|
||||||
|
|||||||
Reference in New Issue
Block a user