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,
}
}
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);
}

View File

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

View File

@ -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<TokenSpan, PortDecl> {
@ -52,6 +53,7 @@ pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
map(
many1(alt((
map(state, ModuleItem::State),
map(struct_def, ModuleItem::Struct),
map(comb_block, ModuleItem::Comb),
))),
|items| Module { items },

View File

@ -87,6 +87,7 @@ pub enum TokenKind {
Proc,
Comb,
Let,
Struct,
// whitespace
Comment,
// Error
@ -234,6 +235,7 @@ fn lex_keywords(input: Span) -> IResult<Span, Token> {
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)