From 30d607aca2aa3784d583649e360b2ca63afa4114 Mon Sep 17 00:00:00 2001 From: Russel Arbore <russel.jma@gmail.com> Date: Mon, 4 Sep 2023 21:31:32 -0500 Subject: [PATCH] Parse type, use ordered_float --- Cargo.lock | 25 +++++++++++++++++++++++++ hercules_ir/Cargo.toml | 3 ++- hercules_ir/src/ir.rs | 18 ++++++++++-------- hercules_ir/src/parse.rs | 17 +++++++++++++---- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 31939e3d..ace37b6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,11 +2,18 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "hercules_ir" version = "0.1.0" dependencies = [ "nom", + "ordered-float", ] [[package]] @@ -30,3 +37,21 @@ dependencies = [ "memchr", "minimal-lexical", ] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + +[[package]] +name = "ordered-float" +version = "3.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a54938017eacd63036332b4ae5c8a49fc8c0c1d6d629893057e4f13609edd06" +dependencies = [ + "num-traits", +] diff --git a/hercules_ir/Cargo.toml b/hercules_ir/Cargo.toml index a67e06c4..c7056cc4 100644 --- a/hercules_ir/Cargo.toml +++ b/hercules_ir/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" authors = ["Russel Arbore <rarbore2@illinois.edu>"] [dependencies] -nom = "*" \ No newline at end of file +nom = "*" +ordered-float = "*" \ No newline at end of file diff --git a/hercules_ir/src/ir.rs b/hercules_ir/src/ir.rs index f5eb4a68..2589e0bd 100644 --- a/hercules_ir/src/ir.rs +++ b/hercules_ir/src/ir.rs @@ -1,3 +1,5 @@ +extern crate ordered_float; + #[derive(Clone)] pub struct Module { pub functions: Vec<Function>, @@ -13,7 +15,7 @@ pub struct Function { pub nodes: Vec<Node>, } -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq, Hash)] pub enum Type { Control(u64), Integer8, @@ -24,14 +26,14 @@ pub enum Type { Float64, } -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq, Hash)] pub enum Constant { Integer8(u8), Integer16(u16), Integer32(u32), Integer64(u64), - Float32(f32), - Float64(f64), + Float32(ordered_float::OrderedFloat<f32>), + Float64(ordered_float::OrderedFloat<f64>), } #[derive(Clone)] @@ -91,7 +93,7 @@ pub enum Node { }, } -#[derive(Clone)] +#[derive(Clone, Copy)] pub struct FunctionID(u32); impl FunctionID { @@ -104,7 +106,7 @@ impl FunctionID { } } -#[derive(Clone)] +#[derive(Clone, Copy)] pub struct NodeID(u32); impl NodeID { @@ -117,7 +119,7 @@ impl NodeID { } } -#[derive(Clone)] +#[derive(Clone, Copy)] pub struct ConstantID(u32); impl ConstantID { @@ -130,7 +132,7 @@ impl ConstantID { } } -#[derive(Clone)] +#[derive(Clone, Copy)] pub struct TypeID(u32); impl TypeID { diff --git a/hercules_ir/src/parse.rs b/hercules_ir/src/parse.rs index d6245d8e..e854ddb1 100644 --- a/hercules_ir/src/parse.rs +++ b/hercules_ir/src/parse.rs @@ -76,7 +76,7 @@ fn parse_function<'a>( for (_, id) in context.node_ids.iter() { fixed_nodes[id.idx()] = Node::Parameter { index: id.idx() } } - return Ok(( + Ok(( ir_text, Function { name: String::from(function_name), @@ -84,7 +84,7 @@ fn parse_function<'a>( return_type, nodes: fixed_nodes, }, - )); + )) } fn parse_node<'a>( @@ -95,7 +95,16 @@ fn parse_node<'a>( } fn parse_type<'a>(ir_text: &'a str, context: &mut Context<'a>) -> nom::IResult<&'a str, TypeID> { - todo!() + let (ir_text, ty) = + nom::combinator::map(nom::bytes::complete::tag("i32"), |_| Type::Integer32)(ir_text)?; + let id = if let Some(id) = context.interned_types.get(&ty) { + *id + } else { + let id = TypeID::new(context.interned_types.len()); + context.interned_types.insert(ty, id); + id + }; + Ok((ir_text, id)) } fn parse_constant<'a>( @@ -111,6 +120,6 @@ mod tests { #[test] fn parse_ir1() { - parse("fn add(x: i32, y: i32) -> i32 return(z) z = add(start, x, y)"); + parse("fn add(x: i32, y: i32) -> i32 r = return(z) z = add(start, x, y)"); } } -- GitLab