mirror of
https://github.com/Zenithsiz/dcb.git
synced 2026-02-05 00:39:31 +00:00
Added lifetime to Parsable trait.
This commit is contained in:
parent
f54325bafe
commit
9b79f3b5c9
@ -107,8 +107,8 @@ impl TryEncode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
#[rustfmt::skip]
|
||||
let parsers: &[&dyn Fn() -> Result<Self, ParseError>] = &[
|
||||
&|| alu ::Inst::parse(mnemonic, args, ctx).map(Self::Alu ),
|
||||
|
||||
@ -39,8 +39,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
match imm::Inst::parse(mnemonic, args, ctx) {
|
||||
Ok(inst) => Ok(Self::Imm(inst)),
|
||||
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),
|
||||
|
||||
@ -118,8 +118,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
#[rustfmt::skip]
|
||||
let to_kind = match mnemonic {
|
||||
"addi" => |value: i64| value.try_into().map(Kind::Add ),
|
||||
|
||||
@ -132,8 +132,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
#[rustfmt::skip]
|
||||
let kind = match mnemonic {
|
||||
"add" => Kind::Add ,
|
||||
|
||||
@ -168,8 +168,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let inst = match mnemonic {
|
||||
"cop0" | "cop1" | "cop2" | "cop3" => {
|
||||
let n = mnemonic[3..].parse().expect("Unable to parse 0..=3");
|
||||
|
||||
@ -120,8 +120,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
// Note: Literals are absolute, not relative
|
||||
|
||||
// Calculates the offset between a position and the current one
|
||||
|
||||
@ -39,8 +39,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
match imm::Inst::parse(mnemonic, args, ctx) {
|
||||
Ok(inst) => Ok(Self::Imm(inst)),
|
||||
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),
|
||||
|
||||
@ -88,8 +88,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let (pos, kind) = match mnemonic {
|
||||
"j" => match args {
|
||||
[arg] => (ctx.arg_pos(arg)?, Kind::Jump),
|
||||
|
||||
@ -73,8 +73,8 @@ impl Encode for Inst {
|
||||
}
|
||||
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let (target, kind) = match mnemonic {
|
||||
"jr" => match *args {
|
||||
[LineArg::Register(target)] => (target, Kind::Jump),
|
||||
|
||||
@ -114,8 +114,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let kind = match mnemonic {
|
||||
"lb" => Kind::Byte,
|
||||
"lh" => Kind::HalfWord,
|
||||
|
||||
@ -46,8 +46,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
if mnemonic != "lui" {
|
||||
return Err(ParseError::UnknownMnemonic);
|
||||
}
|
||||
|
||||
@ -154,8 +154,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let inst = match mnemonic {
|
||||
"mflo" | "mfhi" | "mtlo" | "mthi" => {
|
||||
let reg = match *args {
|
||||
|
||||
@ -48,8 +48,8 @@ impl TryEncode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
match imm::Inst::parse(mnemonic, args, ctx) {
|
||||
Ok(inst) => Ok(Self::Imm(inst)),
|
||||
Err(ParseError::UnknownMnemonic) => reg::Inst::parse(mnemonic, args, ctx).map(Self::Reg),
|
||||
|
||||
@ -105,8 +105,8 @@ impl TryEncode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let kind = match mnemonic {
|
||||
"sll" => Kind::LeftLogical,
|
||||
"srl" => Kind::RightLogical,
|
||||
|
||||
@ -91,8 +91,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let kind = match mnemonic {
|
||||
"sllv" => Kind::LeftLogical,
|
||||
"srlv" => Kind::RightLogical,
|
||||
|
||||
@ -104,8 +104,8 @@ impl Encode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let kind = match mnemonic {
|
||||
"sb" => Kind::Byte,
|
||||
"sh" => Kind::HalfWord,
|
||||
|
||||
@ -86,8 +86,8 @@ impl TryEncode for Inst {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parsable for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], _ctx: &Ctx) -> Result<Self, ParseError> {
|
||||
impl<'a> Parsable<'a> for Inst {
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], _ctx: &'a Ctx) -> Result<Self, ParseError> {
|
||||
let kind = match mnemonic {
|
||||
"sys" => Kind::Sys,
|
||||
"break" => Kind::Break,
|
||||
|
||||
@ -13,9 +13,9 @@ use crate::Pos;
|
||||
use std::convert::TryInto;
|
||||
|
||||
/// Instruction parsing
|
||||
pub trait Parsable: Sized {
|
||||
pub trait Parsable<'a>: Sized + 'a {
|
||||
/// Parses this instruction
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &str, args: &[LineArg], ctx: &Ctx) -> Result<Self, ParseError>;
|
||||
fn parse<Ctx: ?Sized + ParseCtx>(mnemonic: &'a str, args: &'a [LineArg], ctx: &'a Ctx) -> Result<Self, ParseError>;
|
||||
}
|
||||
|
||||
/// Parsing context
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user