Parseid
Parser for Asmodeus Language
┌───────────────────────────────────────────────────────┐
│ │
│ ██████╗ █████╗ ██████╗ ███████╗███████╗██╗██████╗ │
│ ██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔════╝██║██╔══██╗ │
│ ██████╔╝███████║██████╔╝███████╗█████╗ ██║██║ ██║ │
│ ██╔═══╝ ██╔══██║██╔══██╗╚════██║██╔══╝ ██║██║ ██║ │
│ ██║ ██║ ██║██║ ██║███████║███████╗██║██████╔╝ │
│ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚═════╝ │
│ │
│ Tokens Parser for Asmodeus Language │
└───────────────────────────────────────────────────────┘
Parseid is the syntax analyzer for Asmodeus assembly language. It transforms the token stream from Lexariel into a structured Abstract Syntax Tree (AST) that represents the semantic structure of the program. Built with robust error handling and recovery capabilities.
🎯 Features
Core Parsing Capabilities
- Complete Syntax Analysis: All Asmodeus language constructs
- AST Generation: Well-structured Abstract Syntax Tree output
- Macro System: Full macro definition and call parsing
- Label Resolution: Forward and backward label reference support
- Directive Processing: Assembler directives (
RST,RPA, etc.) - Error Recovery: Continues parsing after syntax errors
Advanced Features
- Multiple Addressing Modes: Direct, immediate, indirect, register-based
- Operand Validation: Type checking and format validation
- Position Tracking: Maintains source location for error reporting
- Instruction Validation: Ensures correct instruction formats
- Comment Preservation: Maintains documentation in AST
🚀 Quick Start
Basic Usage
use ;
use tokenize;
// Parse from source code directly
let source = r#"
start:
POB #42 ; Load immediate value
WYJSCIE ; Output the value
STP ; Stop program
"#;
let ast = parse_source?;
println!;
// Or parse from tokens
let tokens = tokenize?;
let ast = parse?;
Examining the AST
use *;
let source = r#"
main:
POB data_value
DOD #10
WYJSCIE
STP
data_value: RST 42
"#;
let ast = parse_source?;
for element in ast.elements
📚 AST Structure
Program Elements
The AST represents a program as a collection of elements:
Instructions
Labels and Directives
Macros
🔧 API Reference
Main Functions
// Parse from source code (convenience function)
;
// Parse from token vector
;
Parser Class
For more control over parsing:
use Parser;
let tokens = tokenize?;
let mut parser = new;
let ast = parser.parse?;
// Access parser state if needed
println!;
Error Types
📖 Examples
Basic Program Parsing
use ;
let program = r#"
; Calculate sum of two numbers
start:
POB first ; Load first number
DOD second ; Add second number
WYJSCIE ; Output result
STP ; Stop
first: RST 25 ; First operand
second: RST 17 ; Second operand
"#;
let ast = parse_source?;
// Count different element types
let mut instruction_count = 0;
let mut label_count = 0;
let mut directive_count = 0;
for element in &ast.elements
println!;
Addressing Mode Analysis
let addressing_examples = r#"
test_addressing:
POB value ; Direct
POB #42 ; Immediate
POB [address] ; Indirect
POB [[ptr]] ; Multiple indirect
POB R1 ; Register
POB [R1] ; Register indirect
POB R1+5 ; Base + offset
POB +10 ; Relative positive
POB -5 ; Relative negative
STP
"#;
let ast = parse_source?;
for element in ast.elements
Macro Definition Parsing
let macro_program = r#"
MAKRO add_and_output num1 num2
POB num1
DOD num2
WYJSCIE
KONM
MAKRO multiply_by_two value
POB value
DOD value
KONM
start:
add_and_output #10 #20
multiply_by_two result
STP
result: RPA
"#;
let ast = parse_source?;
// Extract macro definitions
let macros: = ast.elements.iter
.filter_map
.collect;
for macro_def in macros
Directive Processing
let data_section = r#"
; Data definitions
number: RST 42 ; Initialize with value
buffer: RPA ; Reserve without init
array: RST 1, 2, 3, 4 ; Array initialization
string_len: RST 0x0D ; Hex value
flag: RST 0b1010 ; Binary value
"#;
let ast = parse_source?;
for element in ast.elements
Error Handling and Recovery
use ;
let source_with_errors = r#"
start:
POB ; Missing operand - error
DOD #42 ; Valid instruction
INVALID_OP ; Invalid opcode - error
STP ; Valid instruction
"#;
match parse_source
Complex Program Analysis
let complex_program = r#"
; Factorial calculation using macros
MAKRO factorial_step current result
POB result
MNO current ; Requires extended mode
LAD result
POB current
ODE one
LAD current
KONM
start:
POB n ; Load initial value
LAD current ; Set counter
POB one ; Initialize result
LAD result
factorial_loop:
POB current ; Check if done
SOZ done
factorial_step current result
SOB factorial_loop
done:
POB result ; Output final result
WYJSCIE
STP
; Data section
n: RST 5 ; Calculate 5!
one: RST 1 ; Constant
current: RPA ; Current counter
result: RPA ; Accumulator
"#;
let ast = parse_source?;
// Analyze program structure
let mut analysis = new;
for element in &ast.elements
println!;
🧪 Testing
Unit Tests
Specific Test Categories
# Test instruction parsing
# Test addressing mode parsing
# Test macro parsing
# Test directive parsing
# Test error handling
Integration Tests
🔍 Performance Characteristics
- Speed: ~500K tokens per second parsing
- Memory: O(n) where n is number of tokens
- Error Recovery: Minimal performance impact
- AST Size: Typically 2-3x larger than token count
Performance Testing
use parse_source;
use Instant;
let large_program = include_str!;
let start = now;
let ast = parse_source?;
let duration = start.elapsed;
println!;
🔧 Advanced Usage
Custom AST Processing
use *;
let ast = parse_source?;
let mut visitor = AstVisitor ;
visitor.visit_program;
println!;
AST Transformation
use *;
🚫 Error Recovery
Parseid implements sophisticated error recovery:
let source_with_multiple_errors = r#"
start:
POB ; Error: missing operand
DOD #42 ; Valid after error
BADOP ; Error: invalid opcode
WYJSCIE ; Valid after error
STP ; Valid
"#;
// Parser will try to recover and continue parsing
match parse_source
🔗 Integration with Asmodeus Pipeline
Parseid sits between Lexariel and Hephasm in the pipeline:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Lexariel │───▶│ Parseid │───▶│ Hephasm │
│ (Lexer) │ │ (Parser) │ │ (Assembler) │
│ │ │ │ │ │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Tokens │ │ AST │ │ Machine │
│ │ │ │ │ Code │
└─────────┘ └─────────┘ └─────────┘
Complete Pipeline Usage
use tokenize;
use parse;
use assemble_program;
let source = "POB #42\nWYJSCIE\nSTP";
let tokens = tokenize?; // Lexariel
let ast = parse?; // Parseid
let machine_code = assemble_program?; // Hephasm
🎨 AST Visualization
For debugging and development:
use *;
📜 License
This crate is part of the Asmodeus project and is licensed under the MIT License.
🔗 Related Components
- Lexariel - Lexer that generates tokens for Parseid
- Hephasm - Assembler that consumes Parseid AST
- Shared - Common types and utilities
- Main Asmodeus - Complete language toolchain