Parser

This is a parser designed for the Vondel Microarchitecture. It provides functionality to parse assembly code written for the Vondel Microarchitecture.

Usage

To use the parser, follow these steps:

  1. Create an instance of the Parser struct by providing the input tokens as a Rc<[TokWithCtx]> parameter.
  2. Use the new function of the Parser struct to initialize it with the provided tokens.
  3. Use the various parsing functions provided by the Parser struct to parse the assembly code.

Example

Using our lexer

#![allow(unused)]
fn main() {
let input = r"
.data
    dividend:   .word 10    # Dividend
    divisor:    .word 3     # Divisor
    quotient:   .word 0     # Quotient
    remainder:  .word 0     # Remainder
    address:    .byte 77    # Address
";

let mut l = Lexer::new(input);
let toks = l.get_deez_toks_w_ctx();
let rc_slice = Rc::from(toks.into_boxed_slice());
let mut p = Parser::new(rc_slice);
p.get_deez_program()
}

Using a Vector of TokWithCtx

#![allow(unused)]
fn main() {
let toks = toks_from_api()?;
let mut p = Parser::new(rc_slice);
p.get_deez_program()
}

Parser Structure

The parser is implemented using the following structs:

Program

The Program struct represents the parsed assembly program. It contains the following fields:

  • sections: A vector of Sections representing the different sections of the program.
  • errors: A vector of Error representing any parsing errors encountered during the parsing process.

Parser

The Parser struct is the main parser implementation. It contains the following fields:

  • toks: A shared reference to the input tokens.
  • cur_tok: The current token being processed.
  • peek_tok: The next token to be processed.
  • idx: The index of the current token in the toks vector.
  • cur_line: The current line number.
  • cur_column: The current column number.

The Parser struct provides the following methods:

  • new(toks: Rc<[TokWithCtx]>): Creates a new instance of the Parser struct with the provided input tokens.
  • next_token(): Moves to the next token in the input.
  • expect_peek(expected: AsmToken) -> Result<()>: Checks if the next token matches the expected token.
  • curr_token_is(expected: AsmToken) -> bool: Checks if the current token matches the expected token.
  • peek_token_is(expected: AsmToken) -> bool: Checks if the next token matches the expected token.
  • get_label() -> Result<Rc<str>>: Retrieves the label from the current token.
  • get_number() -> Result<Rc<str>>: Retrieves the number from the current token.
  • get_register() -> Result<Rc<Register>>: Retrieves the register from the current token.
  • get_opcode() -> Result<Rc<Opcode>>: Retrieves the opcode from the current token.
  • get_pseudo_op() -> Result<Rc<PseudoOps>>: Retrieves the pseudo operation from the current token.
  • guard_a_bus(reg: Rc<Register>) -> Result<Rc<Register>>: Checks if the provided register can be used in the A bus.
  • guard_b_bus(reg: Rc<Register>) -> Result<Rc<Register>>: Checks if the provided register can be used in the B bus.
  • guard_c_bus(reg: Rc<Register>) -> Result<Rc<Register>>: Checks if the provided register can be used in the C bus.
  • parse_data_to_write() -> Result<DataWrited>: Parses the data to be written.
  • parse_data_directive() -> Result<Sections>: Parses the data directive section.
  • get_dest_regs() -> Result<Vec<Rc<Register>>>: Retrieves the destination registers.
  • parse_instruction_til_rs1() -> Result<(Vec<Rc<Register>>, Rc<Register>)>: Parses the instruction until the RS1 register.
  • get_instruction() -> Result<Instruction>: Retrieves the instruction.
  • parse_labeled_section() -> Result<TextSegment>: Parses the labeled section.
  • `parse_text_directive() ->.