assembler: we failing DSA with this one
This commit is contained in:
@@ -2,16 +2,105 @@
|
||||
//! easier to build from scratch and edit his code than it would be to try and wrangle it
|
||||
//! into shape.
|
||||
|
||||
use crate::source::source_info::SourceInfo;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum TokenType {
|
||||
Symbol(Symbol),
|
||||
Register(Register),
|
||||
/// Symbol reference (e.g., `loop_start`, `my_data`).
|
||||
Symbol(SymbolToken),
|
||||
/// CPU register (e.g., `r1`, `r2`, `sp`).
|
||||
Register(RegisterToken),
|
||||
/// Immediate value (e.g., `42`, `0xFF`).
|
||||
Immediate(u32),
|
||||
StringLit(String),
|
||||
Opcode(Opcode),
|
||||
/// String literal (e.g., `"hello world"`).
|
||||
String(String),
|
||||
/// Assembly instruction (e.g., `add`, `jmp`, `nop`).
|
||||
Instruction(InstructionToken),
|
||||
/// Label definition (e.g., `loop_start:`).
|
||||
Label(LabelToken),
|
||||
/// Assembler directive (e.g., `.global`, `.section`, `.dw`).
|
||||
Directive(DirectiveToken),
|
||||
/// End of line.
|
||||
Newline,
|
||||
/// End of file.
|
||||
Eof,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Token {
|
||||
/// The type of the token.
|
||||
token_type: TokenType,
|
||||
/// Where in the source code is this [`Token`]?
|
||||
source_info: SourceInfo,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SymbolToken {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct LabelToken {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct DirectiveToken {
|
||||
pub directive: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct RegisterToken {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct InstructionToken {
|
||||
pub mnemonic: String,
|
||||
}
|
||||
|
||||
impl Token {
|
||||
#[must_use]
|
||||
pub const fn new(token_type: TokenType, source_info: SourceInfo) -> Self {
|
||||
Self {
|
||||
token_type,
|
||||
source_info,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn symbol(name: String, source_info: SourceInfo) -> Self {
|
||||
Self::new(TokenType::Symbol(SymbolToken { name }), source_info)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn label(name: String, source_info: SourceInfo) -> Self {
|
||||
Self::new(TokenType::Label(LabelToken { name }), source_info)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn instruction(mnemonic: String, source_info: SourceInfo) -> Self {
|
||||
Self::new(
|
||||
TokenType::Instruction(InstructionToken { mnemonic }),
|
||||
source_info,
|
||||
)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn register(name: String, source_info: SourceInfo) -> Self {
|
||||
Self::new(TokenType::Register(RegisterToken { name }), source_info)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn immediate(value: u32, source_info: SourceInfo) -> Self {
|
||||
Self::new(TokenType::Immediate(value), source_info)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn directive(directive: String, source_info: SourceInfo) -> Self {
|
||||
Self::new(
|
||||
TokenType::Directive(DirectiveToken { directive }),
|
||||
source_info,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user