Skip to content
Snippets Groups Projects
Commit 42347bb6 authored by Russel Arbore's avatar Russel Arbore
Browse files

Parse function

parent 59d6a895
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,8 @@ pub struct Module { ...@@ -8,6 +8,8 @@ pub struct Module {
#[derive(Clone)] #[derive(Clone)]
pub struct Function { pub struct Function {
pub name: String, pub name: String,
pub param_types: Vec<TypeID>,
pub return_type: TypeID,
pub nodes: Vec<Node>, pub nodes: Vec<Node>,
} }
...@@ -44,11 +46,11 @@ pub enum Node { ...@@ -44,11 +46,11 @@ pub enum Node {
}, },
Fork { Fork {
control: NodeID, control: NodeID,
factor: u64, factor: usize,
}, },
Join { Join {
control: NodeID, control: NodeID,
factor: u64, factor: usize,
}, },
Phi { Phi {
control: NodeID, control: NodeID,
...@@ -59,7 +61,7 @@ pub enum Node { ...@@ -59,7 +61,7 @@ pub enum Node {
value: NodeID, value: NodeID,
}, },
Parameter { Parameter {
index: u64, index: usize,
}, },
Constant { Constant {
id: ConstantID, id: ConstantID,
...@@ -93,6 +95,10 @@ pub enum Node { ...@@ -93,6 +95,10 @@ pub enum Node {
pub struct FunctionID(u32); pub struct FunctionID(u32);
impl FunctionID { impl FunctionID {
pub fn new(x: usize) -> Self {
FunctionID(x as u32)
}
pub fn idx(&self) -> usize { pub fn idx(&self) -> usize {
self.0 as usize self.0 as usize
} }
...@@ -102,6 +108,10 @@ impl FunctionID { ...@@ -102,6 +108,10 @@ impl FunctionID {
pub struct NodeID(u32); pub struct NodeID(u32);
impl NodeID { impl NodeID {
pub fn new(x: usize) -> Self {
NodeID(x as u32)
}
pub fn idx(&self) -> usize { pub fn idx(&self) -> usize {
self.0 as usize self.0 as usize
} }
...@@ -111,6 +121,10 @@ impl NodeID { ...@@ -111,6 +121,10 @@ impl NodeID {
pub struct ConstantID(u32); pub struct ConstantID(u32);
impl ConstantID { impl ConstantID {
pub fn new(x: usize) -> Self {
ConstantID(x as u32)
}
pub fn idx(&self) -> usize { pub fn idx(&self) -> usize {
self.0 as usize self.0 as usize
} }
...@@ -120,6 +134,10 @@ impl ConstantID { ...@@ -120,6 +134,10 @@ impl ConstantID {
pub struct TypeID(u32); pub struct TypeID(u32);
impl TypeID { impl TypeID {
pub fn new(x: usize) -> Self {
TypeID(x as u32)
}
pub fn idx(&self) -> usize { pub fn idx(&self) -> usize {
self.0 as usize self.0 as usize
} }
......
...@@ -17,8 +17,10 @@ struct Context<'a> { ...@@ -17,8 +17,10 @@ struct Context<'a> {
} }
fn parse_module<'a>(ir_text: &'a str, mut context: Context<'a>) -> nom::IResult<&'a str, Module> { fn parse_module<'a>(ir_text: &'a str, mut context: Context<'a>) -> nom::IResult<&'a str, Module> {
let (rest, functions) = nom::multi::many0(|x| parse_function(x, &mut context))(ir_text)?; let (rest, functions) =
nom::combinator::eof(rest)?; nom::combinator::all_consuming(nom::multi::many0(|x| parse_function(x, &mut context)))(
ir_text,
)?;
let mut types = vec![Type::Control(0); context.interned_types.len()]; let mut types = vec![Type::Control(0); context.interned_types.len()];
for (ty, id) in context.interned_types { for (ty, id) in context.interned_types {
types[id.idx()] = ty; types[id.idx()] = ty;
...@@ -41,6 +43,65 @@ fn parse_function<'a>( ...@@ -41,6 +43,65 @@ fn parse_function<'a>(
ir_text: &'a str, ir_text: &'a str,
context: &mut Context<'a>, context: &mut Context<'a>,
) -> nom::IResult<&'a str, Function> { ) -> nom::IResult<&'a str, Function> {
context.node_ids.clear();
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::bytes::complete::tag("fn")(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let (ir_text, function_name) = nom::character::complete::alphanumeric0(ir_text)?;
let ir_text = nom::character::complete::char('(')(ir_text)?.0;
let (ir_text, params) = nom::multi::separated_list0(
nom::character::complete::char(','),
nom::sequence::tuple((
nom::character::complete::alphanumeric1,
nom::character::complete::multispace0,
nom::character::complete::char(':'),
nom::character::complete::multispace0,
|x| parse_type(x, context),
)),
)(ir_text)?;
for param in params.iter() {
context
.node_ids
.insert(param.0, NodeID::new(context.node_ids.len()));
}
let ir_text = nom::character::complete::char(')')(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::bytes::complete::tag("->")(ir_text)?.0;
let (ir_text, return_type) = parse_type(ir_text, context)?;
let (ir_text, nodes) = nom::multi::many1(|x| parse_node(x, context))(ir_text)?;
let mut fixed_nodes = vec![Node::Start; context.node_ids.len()];
for (name, node) in nodes {
fixed_nodes[context.node_ids.remove(name).unwrap().idx()] = node;
}
for (_, id) in context.node_ids.iter() {
fixed_nodes[id.idx()] = Node::Parameter { index: id.idx() }
}
return Ok((
ir_text,
Function {
name: String::from(function_name),
param_types: params.into_iter().map(|x| x.4).collect(),
return_type,
nodes: fixed_nodes,
},
));
}
fn parse_node<'a>(
ir_text: &'a str,
context: &mut Context<'a>,
) -> nom::IResult<&'a str, (&'a str, Node)> {
todo!()
}
fn parse_type<'a>(ir_text: &'a str, context: &mut Context<'a>) -> nom::IResult<&'a str, TypeID> {
todo!()
}
fn parse_constant<'a>(
ir_text: &'a str,
context: &mut Context<'a>,
) -> nom::IResult<&'a str, ConstantID> {
todo!() todo!()
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment