parse struct blocks

This commit is contained in:
NotAFile 2022-04-05 14:40:47 +02:00
parent 2dd99ae641
commit b6c6a23ca5
5 changed files with 36 additions and 9 deletions

View File

@ -219,7 +219,9 @@ impl Context {
typ: self.types.primitives.infer, typ: self.types.primitives.infer,
} }
} }
BlockExpr::Block(block) => todo!(), BlockExpr::Block(block) => {
todo!("expression blocks not representable in typed ir yet")
}
}, },
}; };
Ok(t_expr) Ok(t_expr)
@ -306,6 +308,7 @@ impl Context {
parser::ModuleItem::Comb(comb) => self.callable_from_block(comb)?, parser::ModuleItem::Comb(comb) => self.callable_from_block(comb)?,
parser::ModuleItem::Proc(_) => todo!("proc block"), parser::ModuleItem::Proc(_) => todo!("proc block"),
parser::ModuleItem::State(_) => todo!("state block"), parser::ModuleItem::State(_) => todo!("state block"),
parser::ModuleItem::Struct(_) => todo!("struct block"),
}; };
} }
for item in module.items { for item in module.items {
@ -313,6 +316,7 @@ impl Context {
parser::ModuleItem::Comb(comb) => self.type_comb(comb)?, parser::ModuleItem::Comb(comb) => self.type_comb(comb)?,
parser::ModuleItem::Proc(_) => todo!("proc block"), parser::ModuleItem::Proc(_) => todo!("proc block"),
parser::ModuleItem::State(_) => todo!("state block"), parser::ModuleItem::State(_) => todo!("state block"),
parser::ModuleItem::Struct(_) => todo!("struct block"),
}; };
typed_module.blocks.push(block); typed_module.blocks.push(block);
} }

View File

@ -1,3 +1,4 @@
use super::declaration::{declaration, NetDecl};
use super::tokens::{token, Token, TokenKind as tk, TokenSpan}; use super::tokens::{token, Token, TokenKind as tk, TokenSpan};
use super::IResult; use super::IResult;
use nom::multi::separated_list0; use nom::multi::separated_list0;
@ -19,6 +20,12 @@ pub struct StateVariant<'a> {
pub params: Option<Vec<Token<'a>>>, pub params: Option<Vec<Token<'a>>>,
} }
#[derive(Debug)]
pub struct StructBlock<'a> {
pub name: Token<'a>,
pub members: Vec<NetDecl<'a>>,
}
fn state_variant(input: TokenSpan) -> IResult<TokenSpan, StateVariant> { fn state_variant(input: TokenSpan) -> IResult<TokenSpan, StateVariant> {
map( map(
tuple(( tuple((
@ -52,3 +59,20 @@ pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
|(name, variants)| StateBlock { name, variants }, |(name, variants)| StateBlock { name, variants },
)(input) )(input)
} }
pub fn struct_def(input: TokenSpan) -> IResult<TokenSpan, StructBlock> {
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)
}

View File

@ -32,7 +32,6 @@ pub struct TypeName<'a> {
pub struct NetDecl<'a> { pub struct NetDecl<'a> {
pub name: Span<'a>, pub name: Span<'a>,
pub typ: TypeName<'a>, pub typ: TypeName<'a>,
pub value: Option<Span<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -54,14 +53,10 @@ pub fn assign_statement(input: TokenSpan) -> IResult<TokenSpan, Assign> {
// TODO: reallow assignments // TODO: reallow assignments
pub fn declaration(i: TokenSpan) -> IResult<TokenSpan, NetDecl> { pub fn declaration(i: TokenSpan) -> IResult<TokenSpan, NetDecl> {
map( map(
tuple((
separated_pair(token(tk::Ident), token(tk::Colon), typename), separated_pair(token(tk::Ident), token(tk::Colon), typename),
opt(preceded(token(tk::Assign), token(tk::Number))), |(ident, typ)| NetDecl {
)),
|((ident, typ), _value)| NetDecl {
name: ident.span(), name: ident.span(),
typ, typ,
value: None,
}, },
)(i) )(i)
} }

View File

@ -5,7 +5,7 @@ use nom::{
}; };
use crate::parser::{ use crate::parser::{
adt::{state, StateBlock}, adt::{state, struct_def, StateBlock, StructBlock},
comb::{comb_block, CombBlock}, comb::{comb_block, CombBlock},
declaration, declaration,
proc::ProcBlock, proc::ProcBlock,
@ -35,6 +35,7 @@ pub enum ModuleItem<'a> {
Comb(CombBlock<'a>), Comb(CombBlock<'a>),
Proc(ProcBlock<'a>), Proc(ProcBlock<'a>),
State(StateBlock<'a>), State(StateBlock<'a>),
Struct(StructBlock<'a>),
} }
fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> { fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
@ -52,6 +53,7 @@ pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
map( map(
many1(alt(( many1(alt((
map(state, ModuleItem::State), map(state, ModuleItem::State),
map(struct_def, ModuleItem::Struct),
map(comb_block, ModuleItem::Comb), map(comb_block, ModuleItem::Comb),
))), ))),
|items| Module { items }, |items| Module { items },

View File

@ -87,6 +87,7 @@ pub enum TokenKind {
Proc, Proc,
Comb, Comb,
Let, Let,
Struct,
// whitespace // whitespace
Comment, Comment,
// Error // Error
@ -234,6 +235,7 @@ fn lex_keywords(input: Span) -> IResult<Span, Token> {
map(tag("comb"), |_| TokenKind::Comb), map(tag("comb"), |_| TokenKind::Comb),
map(tag("state"), |_| TokenKind::State), map(tag("state"), |_| TokenKind::State),
map(tag("let"), |_| TokenKind::Let), map(tag("let"), |_| TokenKind::Let),
map(tag("struct"), |_| TokenKind::Struct),
))), ))),
|(span, kind)| Token::new(span, kind), |(span, kind)| Token::new(span, kind),
)(input) )(input)