Skip to content

Commit

Permalink
add u64 parsing support; use format_type for ArrayType in LLVM …
Browse files Browse the repository at this point in the history
…dialect
  • Loading branch information
vaivaswatha committed Dec 21, 2024
1 parent 0a65910 commit 397aaaf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
35 changes: 2 additions & 33 deletions pliron-llvm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pliron::{
identifier::Identifier,
impl_verify_succ, input_err_noloc,
irfmt::{
parsers::{delimited_list_parser, int_parser, location, spaced, type_parser},
parsers::{delimited_list_parser, location, spaced, type_parser},
printers::{enclosed, list_with_sep},
},
location::Located,
Expand Down Expand Up @@ -315,6 +315,7 @@ impl_verify_succ!(PointerType);
/// Array type, corresponding to LLVM's array type.
#[def_type("llvm.array")]
#[derive(Hash, PartialEq, Eq, Debug)]
#[format_type("`[` $size `x` $elem `]`")]
pub struct ArrayType {
elem: Ptr<TypeObj>,
size: u64,
Expand All @@ -341,38 +342,6 @@ impl ArrayType {
}
}

impl Printable for ArrayType {
fn fmt(
&self,
ctx: &Context,
_state: &printable::State,
f: &mut core::fmt::Formatter<'_>,
) -> core::fmt::Result {
write!(f, "[{} x {}]", self.size, self.elem.disp(ctx))
}
}

impl Parsable for ArrayType {
type Arg = ();
type Parsed = TypePtr<Self>;

fn parse<'a>(
state_stream: &mut StateStream<'a>,
_arg: Self::Arg,
) -> ParseResult<'a, Self::Parsed>
where
Self: Sized,
{
combine::between(
token('['),
token(']'),
spaced((int_parser::<u64>(), spaced(token('x')), type_parser())),
)
.parse_stream(state_stream)
.map(|(size, _, elem)| ArrayType::get(state_stream.state.ctx, elem, size))
.into()
}
}
impl_verify_succ!(ArrayType);

#[def_type("llvm.void")]
Expand Down
13 changes: 13 additions & 0 deletions src/parsable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
context::{Context, Ptr},
identifier::Identifier,
input_err,
irfmt::parsers::int_parser,
location::{self, Located, Location},
op::op_impls,
operation::Operation,
Expand Down Expand Up @@ -414,3 +415,15 @@ impl NameTracker {
Ok(())
}
}

impl Parsable for u64 {
type Arg = ();
type Parsed = u64;

fn parse<'a>(
state_stream: &mut StateStream<'a>,
_arg: Self::Arg,
) -> ParseResult<'a, Self::Parsed> {
int_parser::<u64>().parse_stream(state_stream).into()
}
}

0 comments on commit 397aaaf

Please sign in to comment.