Skip to content

Commit

Permalink
Allow parsers to be stored and called indirectly
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha committed Sep 18, 2023
1 parent b3431a0 commit 4102b29
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 80 deletions.
35 changes: 18 additions & 17 deletions src/dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
use std::ops::Deref;

use combine::{easy, ParseResult, Parser};
use rustc_hash::FxHashMap;

use crate::{
attribute::AttrId,
context::Context,
context::{Context, Ptr},
op::OpId,
parsable::{self, EasableStream, Parsable, StateStream},
parsable::{self, Parsable, ParserFn, StateStream},
printable::{self, Printable},
r#type::TypeId,
r#type::{TypeId, TypeObj},
};

/// Dialect name: Safe wrapper around a String.
Expand Down Expand Up @@ -38,9 +39,9 @@ impl Printable for DialectName {
impl Parsable for DialectName {
type Parsed = DialectName;

fn parse<'a, S: EasableStream + 'a>(
state_stream: &mut StateStream<'a, S>,
) -> ParseResult<Self::Parsed, easy::ParseError<S>>
fn parse<'a>(
state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
Expand Down Expand Up @@ -71,13 +72,13 @@ impl Deref for DialectName {
/// Dialects are identified by their names.
pub struct Dialect {
/// Name of this dialect.
name: DialectName,
pub name: DialectName,
/// Ops that are part of this dialect.
ops: Vec<OpId>,
pub ops: Vec<OpId>,
/// Types that are part of this dialect.
types: Vec<TypeId>,
pub types: FxHashMap<TypeId, ParserFn<Ptr<TypeObj>>>,
/// Attributes that are part of this dialect.
attributes: Vec<AttrId>,
pub attributes: Vec<AttrId>,
}

impl Printable for Dialect {
Expand All @@ -97,7 +98,7 @@ impl Dialect {
Dialect {
name,
ops: vec![],
types: vec![],
types: FxHashMap::default(),
attributes: vec![],
}
}
Expand All @@ -114,9 +115,9 @@ impl Dialect {
}

/// Add a [Type](crate::type::Type) to this dialect.
pub fn add_type(&mut self, ty: TypeId) {
pub fn add_type(&mut self, ty: TypeId, ty_parser: ParserFn<Ptr<TypeObj>>) {
assert!(ty.dialect == self.name);
self.types.push(ty);
self.types.insert(ty, ty_parser);
}

/// Add an [Attribute](crate::attribute::Attribute) to this dialect.
Expand Down Expand Up @@ -149,19 +150,19 @@ mod test {
use crate::{
context::Context,
dialects,
parsable::{self, easy_positioned_state_stream, Parsable},
parsable::{self, state_stream_from_iterator, Parsable},
printable::Printable,
};

use super::DialectName;

#[test]
fn parse_dialect() {
fn parse_dialect_name() {
let mut ctx = Context::new();
dialects::builtin::register(&mut ctx);

let state_stream =
easy_positioned_state_stream("non_existant", parsable::State { ctx: &mut ctx });
state_stream_from_iterator("non_existant".chars(), parsable::State { ctx: &mut ctx });

let res = DialectName::parser().parse(state_stream);
let err_msg = format!("{}", res.err().unwrap());
Expand All @@ -173,7 +174,7 @@ mod test {
expected_err_msg.assert_eq(&err_msg);

let state_stream =
easy_positioned_state_stream("builtin", parsable::State { ctx: &mut ctx });
state_stream_from_iterator("builtin".chars(), parsable::State { ctx: &mut ctx });

let parsed = DialectName::parser().parse(state_stream).unwrap().0;
assert_eq!(parsed.disp(&ctx).to_string(), "builtin");
Expand Down
33 changes: 24 additions & 9 deletions src/dialects/builtin/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
dialect::Dialect,
error::CompilerError,
impl_type,
parsable::{EasableStream, Parsable, StateStream},
parsable::{Parsable, StateStream},
printable::{self, ListSeparator, Printable, PrintableIter},
r#type::{Type, TypeObj},
storage_uniquer::TypeValueHash,
Expand Down Expand Up @@ -53,9 +53,9 @@ impl IntegerType {

impl Parsable for IntegerType {
type Parsed = Ptr<TypeObj>;
fn parse<'a, S: EasableStream + 'a>(
state_stream: &mut StateStream<'a, S>,
) -> ParseResult<Self::Parsed, easy::ParseError<S>>
fn parse<'a>(
state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
Expand Down Expand Up @@ -156,15 +156,28 @@ impl Printable for FunctionType {
}
}

impl Parsable for FunctionType {
type Parsed = Ptr<TypeObj>;

fn parse<'a>(
_state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
todo!()
}
}

impl Verify for FunctionType {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
todo!()
}
}

pub fn register(dialect: &mut Dialect) {
IntegerType::register_type_in_dialect(dialect);
FunctionType::register_type_in_dialect(dialect);
IntegerType::register_type_in_dialect(dialect, IntegerType::parser_fn);
FunctionType::register_type_in_dialect(dialect, FunctionType::parser_fn);
}

#[cfg(test)]
Expand All @@ -176,7 +189,7 @@ mod tests {
use crate::{
context::Context,
dialects::builtin::types::{IntegerType, Signedness},
parsable::{self, easy_positioned_state_stream, Parsable},
parsable::{self, state_stream_from_iterator, Parsable},
};
#[test]
fn test_integer_types() {
Expand Down Expand Up @@ -217,7 +230,8 @@ mod tests {
#[test]
fn test_integer_parsing() {
let mut ctx = Context::new();
let state_stream = easy_positioned_state_stream("si64", parsable::State { ctx: &mut ctx });
let state_stream =
state_stream_from_iterator("si64".chars(), parsable::State { ctx: &mut ctx });

let res = IntegerType::parser()
.and(eof())
Expand All @@ -231,7 +245,8 @@ mod tests {
#[test]
fn test_integer_parsing_errs() {
let mut ctx = Context::new();
let state_stream = easy_positioned_state_stream("asi64", parsable::State { ctx: &mut ctx });
let a = "asi64".to_string();
let state_stream = state_stream_from_iterator(a.chars(), parsable::State { ctx: &mut ctx });

let res = IntegerType::parser().parse(state_stream);
let err_msg = format!("{}", res.err().unwrap());
Expand Down
33 changes: 31 additions & 2 deletions src/dialects/llvm/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use combine::{easy, ParseResult};

use crate::{
common_traits::Verify,
context::{Context, Ptr},
dialect::Dialect,
error::CompilerError,
impl_type,
parsable::{Parsable, StateStream},
printable::{self, Printable},
r#type::{Type, TypeObj},
storage_uniquer::TypeValueHash,
Expand Down Expand Up @@ -200,6 +203,19 @@ impl PartialEq for StructType {
}
}

impl Parsable for StructType {
type Parsed = Ptr<TypeObj>;

fn parse<'a>(
_state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
todo!()
}
}

impl Eq for StructType {}

#[derive(Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -235,15 +251,28 @@ impl Printable for PointerType {
}
}

impl Parsable for PointerType {
type Parsed = Ptr<TypeObj>;

fn parse<'a>(
_state_stream: &mut StateStream<'a>,
) -> ParseResult<Self::Parsed, easy::ParseError<StateStream<'a>>>
where
Self: Sized,
{
todo!()
}
}

impl Verify for PointerType {
fn verify(&self, _ctx: &Context) -> Result<(), CompilerError> {
todo!()
}
}

pub fn register(dialect: &mut Dialect) {
StructType::register_type_in_dialect(dialect);
PointerType::register_type_in_dialect(dialect);
StructType::register_type_in_dialect(dialect, StructType::parser_fn);
PointerType::register_type_in_dialect(dialect, PointerType::parser_fn);
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 4102b29

Please sign in to comment.