Skip to content

Commit

Permalink
Merge pull request #37 from yeslogic/brendan/owned-ir-lowering
Browse files Browse the repository at this point in the history
Run examples through owned IR lowering
  • Loading branch information
brendanzab authored Nov 11, 2017
2 parents 9dd2cff + e4e3c99 commit 99da98c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/ir/owned/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ use std::rc::Rc;

use name::{Name, Named};
use source::Span;
use syntax::ast::{binary, host, Field};
use syntax;
use syntax::ast::{binary, host, Field, Var};

/// The definitions in this program
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Program<N> {
pub defs: Vec<Definition<N>>,
}

/// The definition of a parseable type
///
Expand All @@ -25,6 +32,7 @@ use syntax::ast::{binary, host, Field};
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Definition<N> {
name: N,
ty: host::RcType<N>,
parser: RcParseExpr<N>,
}
Expand All @@ -39,6 +47,8 @@ pub enum RepeatBound<N> {
/// A small parser combinator language
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseExpr<N> {
/// A reference to another parser
Var(Var<N, u32>),
/// Parse a bit
Bit,
/// The name of another parsable type
Expand Down Expand Up @@ -115,11 +125,20 @@ impl<N: Name> ParseExpr<N> {

// Lowering

impl<'a, N: Name + for<'b> From<&'b str>> From<&'a binary::Type<N>> for Definition<N> {
fn from(src: &'a binary::Type<N>) -> Definition<N> {
impl<'a, N: Name + for<'b> From<&'b str>> From<&'a syntax::ast::Program<N>> for Program<N> {
fn from(src: &'a syntax::ast::Program<N>) -> Program<N> {
Program {
defs: src.defs.iter().map(Definition::from).collect(),
}
}
}

impl<'a, N: Name + for<'b> From<&'b str>> From<&'a syntax::ast::Definition<N>> for Definition<N> {
fn from(src: &'a syntax::ast::Definition<N>) -> Definition<N> {
Definition {
ty: src.repr(),
parser: Rc::new(ParseExpr::from(src)),
name: src.name.clone(),
ty: src.ty.repr(),
parser: Rc::new(ParseExpr::from(&*src.ty)),
}
}
}
Expand All @@ -130,6 +149,7 @@ impl<'a, N: Name + for<'b> From<&'b str>> From<&'a binary::Type<N>> for ParseExp
use syntax::ast::host::Expr;

match *src {
Type::Var(_, ref var) => ParseExpr::Var(var.clone()),
Type::Const(TypeConst::Bit) => ParseExpr::Bit,
Type::Array(_, ref elem_ty, ref size_expr) => {
let elem_parser = ParseExpr::from(&**elem_ty);
Expand Down Expand Up @@ -188,7 +208,8 @@ impl<'a, N: Name + for<'b> From<&'b str>> From<&'a binary::Type<N>> for ParseExp
)),
)
}
Type::Var(_, _) | Type::Abs(_, _, _) | Type::App(_, _, _) => unimplemented!(),
Type::Abs(_, _, _) => unimplemented!("Abs: {:?}", src),
Type::App(_, _, _) => unimplemented!("App: {:?}", src),
}
}
}
12 changes: 12 additions & 0 deletions tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ fn cmap() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

#[test]
Expand All @@ -26,6 +28,8 @@ fn edid() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

#[test]
Expand All @@ -37,6 +41,8 @@ fn heroes_of_might_and_magic_bmp() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

#[test]
Expand All @@ -48,6 +54,8 @@ fn ieee754() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

#[test]
Expand All @@ -59,6 +67,8 @@ fn object_id() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

#[test]
Expand All @@ -70,4 +80,6 @@ fn stl() {
program.substitute(&base_defs);

check::check_program(&program).unwrap();

ddl::ir::owned::ast::Program::from(&program);
}

0 comments on commit 99da98c

Please sign in to comment.