diff --git a/src/frontend.rs b/src/frontend.rs index 2b41f02..e7ee3d7 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -219,7 +219,9 @@ impl Context { typ: self.types.primitives.infer, } } - BlockExpr::Block(block) => todo!(), + BlockExpr::Block(block) => { + todo!("expression blocks not representable in typed ir yet") + } }, }; Ok(t_expr) @@ -306,6 +308,7 @@ impl Context { parser::ModuleItem::Comb(comb) => self.callable_from_block(comb)?, parser::ModuleItem::Proc(_) => todo!("proc block"), parser::ModuleItem::State(_) => todo!("state block"), + parser::ModuleItem::Struct(_) => todo!("struct block"), }; } for item in module.items { @@ -313,6 +316,7 @@ impl Context { parser::ModuleItem::Comb(comb) => self.type_comb(comb)?, parser::ModuleItem::Proc(_) => todo!("proc block"), parser::ModuleItem::State(_) => todo!("state block"), + parser::ModuleItem::Struct(_) => todo!("struct block"), }; typed_module.blocks.push(block); } diff --git a/src/parser/adt.rs b/src/parser/adt.rs index 518b820..3f2a888 100644 --- a/src/parser/adt.rs +++ b/src/parser/adt.rs @@ -1,3 +1,4 @@ +use super::declaration::{declaration, NetDecl}; use super::tokens::{token, Token, TokenKind as tk, TokenSpan}; use super::IResult; use nom::multi::separated_list0; @@ -19,6 +20,12 @@ pub struct StateVariant<'a> { pub params: Option>>, } +#[derive(Debug)] +pub struct StructBlock<'a> { + pub name: Token<'a>, + pub members: Vec>, +} + fn state_variant(input: TokenSpan) -> IResult { map( tuple(( @@ -52,3 +59,20 @@ pub fn state(input: TokenSpan) -> IResult { |(name, variants)| StateBlock { name, variants }, )(input) } + +pub fn struct_def(input: TokenSpan) -> IResult { + map( + preceded( + token(tk::Struct), + cut(tuple(( + token(tk::Ident), + delimited( + token(tk::LBrace), + separated_list0(token(tk::Comma), declaration), + token(tk::RBrace), + ), + ))), + ), + |(name, members)| StructBlock { name, members }, + )(input) +} diff --git a/src/parser/declaration.rs b/src/parser/declaration.rs index f3dbc2b..fb4a014 100644 --- a/src/parser/declaration.rs +++ b/src/parser/declaration.rs @@ -32,7 +32,6 @@ pub struct TypeName<'a> { pub struct NetDecl<'a> { pub name: Span<'a>, pub typ: TypeName<'a>, - pub value: Option>, } #[derive(Debug)] @@ -54,14 +53,10 @@ pub fn assign_statement(input: TokenSpan) -> IResult { // TODO: reallow assignments pub fn declaration(i: TokenSpan) -> IResult { map( - tuple(( - separated_pair(token(tk::Ident), token(tk::Colon), typename), - opt(preceded(token(tk::Assign), token(tk::Number))), - )), - |((ident, typ), _value)| NetDecl { + separated_pair(token(tk::Ident), token(tk::Colon), typename), + |(ident, typ)| NetDecl { name: ident.span(), typ, - value: None, }, )(i) } diff --git a/src/parser/module.rs b/src/parser/module.rs index fb3aedb..34ab1a5 100644 --- a/src/parser/module.rs +++ b/src/parser/module.rs @@ -5,7 +5,7 @@ use nom::{ }; use crate::parser::{ - adt::{state, StateBlock}, + adt::{state, struct_def, StateBlock, StructBlock}, comb::{comb_block, CombBlock}, declaration, proc::ProcBlock, @@ -35,6 +35,7 @@ pub enum ModuleItem<'a> { Comb(CombBlock<'a>), Proc(ProcBlock<'a>), State(StateBlock<'a>), + Struct(StructBlock<'a>), } fn port_decl(i: TokenSpan) -> IResult { @@ -52,6 +53,7 @@ pub fn module(input: TokenSpan) -> IResult { map( many1(alt(( map(state, ModuleItem::State), + map(struct_def, ModuleItem::Struct), map(comb_block, ModuleItem::Comb), ))), |items| Module { items }, diff --git a/src/parser/tokens.rs b/src/parser/tokens.rs index a3c23a6..2333153 100644 --- a/src/parser/tokens.rs +++ b/src/parser/tokens.rs @@ -87,6 +87,7 @@ pub enum TokenKind { Proc, Comb, Let, + Struct, // whitespace Comment, // Error @@ -234,6 +235,7 @@ fn lex_keywords(input: Span) -> IResult { map(tag("comb"), |_| TokenKind::Comb), map(tag("state"), |_| TokenKind::State), map(tag("let"), |_| TokenKind::Let), + map(tag("struct"), |_| TokenKind::Struct), ))), |(span, kind)| Token::new(span, kind), )(input)