You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.1 KiB
88 lines
2.1 KiB
use sexp::Sexp;
|
|
|
|
use crate::asm::data::{Mask, Rd, RdData, RdObj, Wr};
|
|
use crate::asm::parse::parse_data::{parse_rd, parse_wr};
|
|
use crate::asm::parse::sexp_expect::expect_string_atom;
|
|
|
|
/// Utility for argument parsing
|
|
pub struct TokenParser {
|
|
orig_len: usize,
|
|
args: Vec<Sexp>,
|
|
}
|
|
|
|
impl IntoIterator for TokenParser {
|
|
type Item = Sexp;
|
|
type IntoIter = std::vec::IntoIter<Sexp>;
|
|
|
|
fn into_iter(mut self) -> Self::IntoIter {
|
|
self.args.reverse();
|
|
self.args.into_iter()
|
|
}
|
|
}
|
|
|
|
impl TokenParser {
|
|
/// Create a new argument parser
|
|
pub fn new(mut args: Vec<Sexp>) -> Self {
|
|
args.reverse();
|
|
Self {
|
|
orig_len: args.len(),
|
|
args,
|
|
}
|
|
}
|
|
|
|
pub fn have_more(&self) -> bool {
|
|
self.len() > 0
|
|
}
|
|
|
|
/// Get remaining items count
|
|
pub fn len(&self) -> usize {
|
|
self.args.len()
|
|
}
|
|
|
|
/// Get len before parsing started
|
|
pub fn orig_len(&self) -> usize {
|
|
self.orig_len
|
|
}
|
|
|
|
/// Check if parsing started
|
|
pub fn parsing_started(&self) -> bool {
|
|
self.args.len() != self.orig_len
|
|
}
|
|
|
|
/// Get the next entry
|
|
pub fn next(&mut self) -> Option<Sexp> {
|
|
self.args.pop()
|
|
}
|
|
|
|
/// Look at the next entry
|
|
pub fn peek(&mut self) -> Option<&Sexp> {
|
|
self.args.last()
|
|
}
|
|
|
|
/// Get the next string entry
|
|
pub fn next_string(&mut self) -> anyhow::Result<String> {
|
|
Ok(expect_string_atom(self.next())?)
|
|
}
|
|
|
|
/// Get the next entry as read location
|
|
pub fn next_rd(&mut self) -> anyhow::Result<Rd> {
|
|
parse_rd(self.next())
|
|
}
|
|
|
|
/// Get the next entry as read location
|
|
pub fn next_rdobj(&mut self) -> anyhow::Result<RdObj> {
|
|
match parse_rd(self.next())? {
|
|
Rd(RdData::ObjectPtr(reg), Mask::FULL) => {
|
|
return Ok(RdObj::new(reg));
|
|
}
|
|
other => {
|
|
anyhow::bail!("Not a valid object handle syntax: {:?}", other);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Get the next entry as write location
|
|
pub fn next_wr(&mut self) -> anyhow::Result<Wr> {
|
|
parse_wr(self.next())
|
|
}
|
|
}
|
|
|