diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2025-02-14 09:34:48 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2025-02-14 09:34:48 +1300 |
commit | e39505931b05be321ee2b04c41a9739f00c19208 (patch) | |
tree | 529a4162ed2d34a7c283a928136808bd20750563 /src/tokens | |
parent | 1995f8a8f2cb5ea810afc173fe8dfa2f5355f684 (diff) | |
download | torque-asm-e39505931b05be321ee2b04c41a9739f00c19208.zip |
Implement semantic parsing
Diffstat (limited to 'src/tokens')
-rw-r--r-- | src/tokens/semantic.rs | 64 |
1 files changed, 53 insertions, 11 deletions
diff --git a/src/tokens/semantic.rs b/src/tokens/semantic.rs index a665b9a..66db7b2 100644 --- a/src/tokens/semantic.rs +++ b/src/tokens/semantic.rs @@ -5,6 +5,7 @@ use crate::*; pub struct Program { pub definitions: Vec<Definition>, pub invocations: Vec<Invocation>, + pub errors: Vec<ParseError>, } /// A symbol definition. @@ -28,7 +29,8 @@ pub enum ArgumentDefinitionVariant { pub enum DefinitionVariant { Integer(IntegerDefinition), - Block(Vec<BlockToken>), + Block(BlockDefinition), + Reference(ReferenceDefinition), } pub struct IntegerDefinition { @@ -39,7 +41,11 @@ pub struct IntegerDefinition { pub enum IntegerDefinitionVariant { Literal(usize), Constant(ConstantExpression), - Reference(String), +} + +pub struct BlockDefinition { + pub tokens: Vec<BlockToken>, + pub errors: Vec<ParseError>, } pub struct BlockToken { @@ -53,20 +59,56 @@ pub enum BlockTokenVariant { Word(PackedBinaryLiteral), } +/// References aren't necessarily an integer or a block +pub struct ReferenceDefinition { + pub source: SourceSpan, + pub name: String, +} + pub struct Invocation { pub name: String, - pub source: SourceSpan, - pub arguments: Vec<InvocationArgument>, + pub arguments: Vec<DefinitionVariant>, } -pub struct InvocationArgument { +pub struct ParseError { pub source: SourceSpan, - pub variant: InvocationArgumentVariant, + pub variant: ParseErrorVariant, +} + +pub enum ParseErrorVariant { + UnterminatedMacroDefinition, + UnterminatedBlockDefinition, + /// Name of the macro. + InvalidArgumentDefinition(String), + InvalidToken, } -pub enum InvocationArgumentVariant { - BlockLiteral(Vec<BlockToken>), - IntegerLiteral(usize), - ConstantExpression(ConstantExpression), - Reference(String), + +// ------------------------------------------------------------------------ // + +impl Program { + pub fn print_definitions(&self) { + for definition in &self.definitions { + let variant = match definition.variant { + DefinitionVariant::Integer(_) => "integer", + DefinitionVariant::Block(_) => "block", + DefinitionVariant::Reference(_) => "reference", + }; + println!("DEFINE {} ({variant})", definition.name); + for argument in &definition.arguments { + let variant = match argument.variant { + ArgumentDefinitionVariant::Integer => "integer", + ArgumentDefinitionVariant::Block => "block", + }; + println!(" ARGUMENT {} ({variant})", argument.name); + } + let variant = match &definition.variant { + DefinitionVariant::Integer(integer) => todo!(), + DefinitionVariant::Block(block) => todo!(), + DefinitionVariant::Reference(reference) => todo!() + }; + + println!(); + } + } } |