Assembler #6
@@ -18,6 +18,8 @@ ByteArray Assembler::assemble()
|
|||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
Token t = lexer_.ingest();
|
Token t = lexer_.ingest();
|
||||||
|
if (t.type == TokenType::Enter)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (t.type == TokenType::Directive) {
|
if (t.type == TokenType::Directive) {
|
||||||
if (std::get<std::string>(t.token) == ".const") {
|
if (std::get<std::string>(t.token) == ".const") {
|
||||||
@@ -30,22 +32,36 @@ ByteArray Assembler::assemble()
|
|||||||
} else {
|
} else {
|
||||||
throw AssemblyError("Invalid directive " + std::get<std::string>(t.token), t.line, t.column);
|
throw AssemblyError("Invalid directive " + std::get<std::string>(t.token), t.line, t.column);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (section == Section::Const && t.type == TokenType::Integer) {
|
} else if (section == Section::Const && t.type == TokenType::Integer) {
|
||||||
int index = std::get<int>(expect_token(TokenType::Integer));
|
int index = std::get<int>(t.token);
|
||||||
if ((size_t) index >= bp.constants.size())
|
if ((size_t) index >= bp.constants.size())
|
||||||
bp.constants.resize(index + 1);
|
bp.constants.resize(index + 1);
|
||||||
expect_token(TokenType::Colon);
|
expect_token(TokenType::Colon);
|
||||||
Token tt = lexer_.ingest();
|
Token tt = lexer_.ingest();
|
||||||
if (tt.type == TokenType::Float) {
|
if (tt.type == TokenType::Float)
|
||||||
bp.constants[index] = std::get<float>(tt.token);
|
bp.constants[index] = std::get<float>(tt.token);
|
||||||
} else if (tt.type == TokenType::String) {
|
else if (tt.type == TokenType::String)
|
||||||
bp.constants[index] = std::get<std::string>(tt.token);
|
bp.constants[index] = std::get<std::string>(tt.token);
|
||||||
} else {
|
else
|
||||||
throw AssemblyError("Expected float or string as constant", tt.line, tt.column);
|
throw AssemblyError("Expected float or string as constant", tt.line, tt.column);
|
||||||
}
|
expect_token(TokenType::Enter);
|
||||||
|
|
||||||
} else if (section == Section::Function && t.type == TokenType::Instruction) {
|
} else if (section == Section::Function && t.type == TokenType::Instruction) {
|
||||||
|
std::string instruction = std::get<std::string>(t.token);
|
||||||
|
int oper = 0;
|
||||||
|
Token tt = lexer_.ingest();
|
||||||
|
if (tt.type == TokenType::Integer) {
|
||||||
|
oper = std::get<int>(tt.token);
|
||||||
|
tt = lexer_.ingest();
|
||||||
|
}
|
||||||
|
emit_instruction(function_id, instruction, oper);
|
||||||
|
if (tt.type != TokenType::Enter)
|
||||||
|
throw AssemblyError("Expected enter", tt.line, tt.column);
|
||||||
|
|
||||||
} else if (t.type == TokenType::EOF_) {
|
} else if (t.type == TokenType::EOF_) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
} else if (t.type != TokenType::Enter) {
|
} else if (t.type != TokenType::Enter) {
|
||||||
throw AssemblyError("Unexpected token of type " + token_type_name(t.type) + ")", t.line, t.column);
|
throw AssemblyError("Unexpected token of type " + token_type_name(t.type) + ")", t.line, t.column);
|
||||||
}
|
}
|
||||||
@@ -54,6 +70,11 @@ ByteArray Assembler::assemble()
|
|||||||
return bc::Bytecode::generate(bp);
|
return bc::Bytecode::generate(bp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Assembler::emit_instruction(uint32_t function_id, std::string const& inst, int oper)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
TokenValue Assembler::expect_token(TokenType type)
|
TokenValue Assembler::expect_token(TokenType type)
|
||||||
{
|
{
|
||||||
Token t = lexer_.ingest();
|
Token t = lexer_.ingest();
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ private:
|
|||||||
Lexer lexer_;
|
Lexer lexer_;
|
||||||
|
|
||||||
TokenValue expect_token(TokenType type);
|
TokenValue expect_token(TokenType type);
|
||||||
|
void emit_instruction(uint32_t function_id, std::string const& inst, int oper);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // tyche
|
} // tyche
|
||||||
|
|||||||
Reference in New Issue
Block a user