diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 19:52:29 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2024-10-28 19:52:47 +1300 |
commit | f4027cae775e3c9c237675f9df35a744d54f3f2e (patch) | |
tree | 733fa3af9e1bd44d61dd83983a2da86cb75c53e9 /src/tokens/semantic.rs | |
parent | 16ee0e9e8dce2c88acc88ba5ffd97e013624fa5e (diff) | |
download | bedrock-asm-f4027cae775e3c9c237675f9df35a744d54f3f2e.zip |
Rewrite assembler
This is an almost complete rewrite of the entire assembler from the
ground up, with a different parsing strategy and a whole new symbol
resolution mechanism for automatically including library files.
The assembly syntax has also been slightly modified, with padding
tokens now being prefixed with '#' instead of '$', and a block-style
anonymous-label syntax which uses the '{' and '}' characters.
Diffstat (limited to 'src/tokens/semantic.rs')
-rw-r--r-- | src/tokens/semantic.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/tokens/semantic.rs b/src/tokens/semantic.rs new file mode 100644 index 0000000..ac5179c --- /dev/null +++ b/src/tokens/semantic.rs @@ -0,0 +1,90 @@ +use crate::*; + +use SemanticTokenVariant as SemVar; + + +pub struct SemanticToken { + pub source: SourceSpan, + pub bytecode: BytecodeSpan, + pub variant: SemanticTokenVariant, +} + + +pub enum SemanticTokenVariant { + LabelDefinition(LabelDefinition), + MacroDefinition(MacroDefinition), + + /// Pointer to the matching label definition. + LabelReference(usize), + /// Pointer to the matching macro definition. + MacroInvocation(usize), + + Literal(Value), + Padding(Value), + Instruction(Instruction), + + Comment(String), + String(Vec<u8>), + + /// Pointer to the matching block close. + BlockOpen(usize), + /// Pointer to the matching block open. + BlockClose(usize), + MarkOpen, + MarkClose, + + Error(SemanticParseError), +} + +impl std::fmt::Debug for SemanticToken { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + match &self.variant { + SemVar::LabelDefinition(def) => write!(f, "LabelDefinition({})", def.name), + SemVar::MacroDefinition(def) => write!(f, "MacroDefinition({})", def.name), + SemVar::LabelReference(pointer) => write!(f, "LabelReference(*{pointer})"), + SemVar::MacroInvocation(pointer) => write!(f, "MacroInvocation(*{pointer})"), + SemVar::Literal(value) => write!(f, "Literal({value})"), + SemVar::Padding(value) => write!(f, "Padding({value})"), + SemVar::Instruction(instr) => write!(f, "Instruction(0x{:02x})", instr.value), + SemVar::Comment(comment) => write!(f, "Comment({comment})"), + SemVar::String(string) => write!(f, "String({})", String::from_utf8_lossy(&string)), + SemVar::BlockOpen(_) => write!(f, "BlockOpen"), + SemVar::BlockClose(_) => write!(f, "BlockClose"), + SemVar::MarkOpen => write!(f, "MarkOpen"), + SemVar::MarkClose => write!(f, "MarkClose"), + SemVar::Error(_) => write!(f, "Error"), + } + } +} + + +pub struct LabelDefinition { + /// The absolute name of the label or sublabel. + pub name: String, + /// List of pointers to label reference tokens. + pub references: Vec<usize>, +} + + +pub struct MacroDefinition { + pub name: String, + pub references: Vec<usize>, + pub body_tokens: Vec<SemanticToken>, +} + + +pub enum SemanticParseError { + LabelDefinitionInMacroDefinition, + MacroDefinitionInMacroDefinition, + + StrayMacroTerminator, + StrayBlockClose, + UnclosedBlock, + + UndefinedSymbol(String), + RedefinedSymbol((String, SourceSpan)), + + MacroInvocationBeforeDefinition((String, SourceSpan)), + + SyntaxError(SyntacticParseError) +} |