start implementing state adt

This commit is contained in:
NotAFile 2022-02-03 01:55:12 +01:00
parent c1dc524cab
commit faf92307e2
7 changed files with 94 additions and 42 deletions

View File

@ -1,6 +1,6 @@
module identity ( comb identity (
a: Logic a: Logic
) -> Logic ) -> Logic
{ {
assign x = a; a
} }

View File

@ -286,7 +286,7 @@ fn lower_assignment(
pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> { pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> {
let mut writer = rtlil::ILWriter::new(); let mut writer = rtlil::ILWriter::new();
let mut ir_module = rtlil::Module::new(make_pubid(pa_module.name.fragment())); let mut ir_module = rtlil::Module::new(make_pubid("test"));
let mut context = Context { let mut context = Context {
callables: get_builtins() callables: get_builtins()
.into_iter() .into_iter()
@ -297,6 +297,7 @@ pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> {
}; };
writer.write_line("autoidx 1"); writer.write_line("autoidx 1");
/*
for (idx, port) in pa_module.ports.iter().enumerate() { for (idx, port) in pa_module.ports.iter().enumerate() {
// FIXME: Actually resolve types // FIXME: Actually resolve types
let sigtype = TypeStruct::logic_width(TODO_WIDTH); let sigtype = TypeStruct::logic_width(TODO_WIDTH);
@ -328,5 +329,6 @@ pub fn lower_module(pa_module: parser::Module) -> Result<String, CompileError> {
} }
} }
ir_module.write_rtlil(&mut writer); ir_module.write_rtlil(&mut writer);
*/
Ok(writer.finish()) Ok(writer.finish())
} }

View File

@ -1,16 +1,40 @@
use super::declaration::{declaration, NetDecl}; 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::many0; use nom::multi::separated_list0;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::{ use nom::{
combinator::{cut, map}, combinator::{cut, map, opt},
sequence::{delimited, preceded}, sequence::{delimited, preceded},
}; };
#[derive(Debug)]
pub struct StateBlock<'a> { pub struct StateBlock<'a> {
name: Token<'a>, pub name: Token<'a>,
items: Vec<NetDecl<'a>>, pub variants: Vec<StateVariant<'a>>,
}
#[derive(Debug)]
pub struct StateVariant<'a> {
pub name: Token<'a>,
pub params: Option<Vec<Token<'a>>>,
}
fn state_variant(input: TokenSpan) -> IResult<TokenSpan, StateVariant> {
map(
tuple((
token(tk::Ident),
opt(delimited(
token(tk::LParen),
separated_list0(token(tk::Comma), token(tk::Ident)),
token(tk::RParen),
)),
)),
|(name, param)| StateVariant {
name: name.clone(),
params: param,
},
)(input)
} }
pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> { pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
@ -19,12 +43,13 @@ pub fn state(input: TokenSpan) -> IResult<TokenSpan, StateBlock> {
token(tk::State), token(tk::State),
cut(tuple(( cut(tuple((
token(tk::Ident), token(tk::Ident),
delimited(token(tk::LBrace), many0(declaration), token(tk::RBrace)), delimited(
token(tk::LBrace),
separated_list0(token(tk::Comma), state_variant),
token(tk::RBrace),
),
))), ))),
), ),
|(name, items)| StateBlock { |(name, variants)| StateBlock { name, variants },
name: name.clone(),
items,
},
)(input) )(input)
} }

View File

@ -1,18 +1,24 @@
use super::module::inputs_list; use super::{
use super::tokens::TokenKind as tk; module::inputs_list,
use crate::parser::tokens::token; module::PortDecl,
use crate::parser::typename; tokens::{token, TokenKind as tk, TokenSpan},
use crate::parser::IResult; typename, IResult, Span,
use crate::parser::Module; };
use crate::parser::TokenSpan;
use nom::combinator::cut;
use nom::combinator::map;
use nom::multi::many0;
use nom::sequence::delimited; use nom::sequence::delimited;
use nom::sequence::preceded; use nom::sequence::preceded;
use nom::sequence::tuple; use nom::sequence::tuple;
use nom::{
combinator::{cut, map},
multi::many0,
};
pub fn comb_block(input: TokenSpan) -> IResult<TokenSpan, Module> { #[derive(Debug)]
pub struct CombBlock<'a> {
pub name: Span<'a>,
pub ports: Vec<PortDecl<'a>>,
}
pub fn comb_block(input: TokenSpan) -> IResult<TokenSpan, CombBlock> {
map( map(
preceded( preceded(
token(tk::Comb), token(tk::Comb),
@ -27,11 +33,10 @@ pub fn comb_block(input: TokenSpan) -> IResult<TokenSpan, Module> {
), ),
))), ))),
), ),
|(name, inputs, _ret, _items)| Module { |(name, inputs, _ret, _items)| CombBlock {
// TODO: bring back returns // TODO: bring back returns
name: name.span(), name: name.span(),
ports: inputs, ports: inputs,
items: todo!(),
}, },
)(input) )(input)
} }

View File

@ -23,9 +23,10 @@ pub use crate::parser::declaration::{
pub use crate::parser::expression::{expression, Call, Expression, Operation}; pub use crate::parser::expression::{expression, Call, Expression, Operation};
pub use crate::parser::module::{module, Module, ModuleItem, PortDirection}; pub use crate::parser::module::{module, Module, ModuleItem, PortDirection};
use crate::parser::tokens::TokenSpan; use crate::parser::tokens::TokenSpan;
use nom::combinator::all_consuming;
pub fn parse(input: TokenSpan) -> IResult<TokenSpan, Module> { pub fn parse(input: TokenSpan) -> IResult<TokenSpan, Module> {
module(input) all_consuming(module)(input)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -1,10 +1,16 @@
use nom::{combinator::map, multi::separated_list0}; use nom::{
branch::alt,
combinator::map,
multi::{many1, separated_list0},
};
use crate::parser::{ use crate::parser::{
adt::{state, StateBlock},
comb::{comb_block, CombBlock},
declaration, declaration,
proc::ProcBlock, proc::ProcBlock,
tokens::{token, TokenKind as tk, TokenSpan}, tokens::{token, TokenKind as tk, TokenSpan},
Assign, IResult, NetDecl, Span, IResult, NetDecl, Span,
}; };
#[derive(Debug)] #[derive(Debug)]
@ -21,15 +27,14 @@ pub struct PortDecl<'a> {
#[derive(Debug)] #[derive(Debug)]
pub struct Module<'a> { pub struct Module<'a> {
pub name: Span<'a>,
pub ports: Vec<PortDecl<'a>>,
pub items: Vec<ModuleItem<'a>>, pub items: Vec<ModuleItem<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ModuleItem<'a> { pub enum ModuleItem<'a> {
Assign(Assign<'a>), Comb(CombBlock<'a>),
Proc(ProcBlock<'a>), Proc(ProcBlock<'a>),
State(StateBlock<'a>),
} }
fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> { fn port_decl(i: TokenSpan) -> IResult<TokenSpan, PortDecl> {
@ -43,8 +48,14 @@ pub fn inputs_list(input: TokenSpan) -> IResult<TokenSpan, Vec<PortDecl>> {
separated_list0(token(tk::Comma), port_decl)(input) separated_list0(token(tk::Comma), port_decl)(input)
} }
pub fn module(_input: TokenSpan) -> IResult<TokenSpan, Module> { pub fn module(input: TokenSpan) -> IResult<TokenSpan, Module> {
todo!(); map(
many1(alt((
map(state, ModuleItem::State),
map(comb_block, ModuleItem::Comb),
))),
|items| Module { items },
)(input)
} }
#[cfg(test)] #[cfg(test)]

View File

@ -3,18 +3,20 @@
use super::{ use super::{
error::{Error, InputPos}, error::{Error, InputPos},
literals::{identifier, ws0}, literals::{identifier, ws0},
IResult, Span, IErr, IResult, Span,
}; };
use nom::{ use nom::{
branch::alt, branch::alt,
bytes::complete::tag, bytes::complete::tag,
character::complete::{anychar, digit1}, character::complete::{anychar, digit1},
combinator::{consumed, map, recognize}, combinator::{consumed, map, recognize},
error::ParseError,
multi::many0, multi::many0,
InputTake,
}; };
use std::fmt; use std::fmt;
#[derive(Clone)] #[derive(Clone, Copy)]
pub struct Token<'a> { pub struct Token<'a> {
span: Span<'a>, span: Span<'a>,
kind: TokenKind, kind: TokenKind,
@ -38,7 +40,7 @@ impl<'a> Token<'a> {
Self { span, kind } Self { span, kind }
} }
pub fn span(&self) -> Span { pub fn span(self) -> Span<'a> {
self.span self.span
} }
pub fn kind(&self) -> TokenKind { pub fn kind(&self) -> TokenKind {
@ -85,7 +87,7 @@ pub enum TokenKind {
Error, Error,
} }
#[derive(Debug, Clone)] #[derive(Debug, Copy, Clone)]
pub struct TokenSpan<'a> { pub struct TokenSpan<'a> {
rest: &'a [Token<'a>], rest: &'a [Token<'a>],
pos: usize, pos: usize,
@ -106,7 +108,7 @@ impl<'a> TokenSpan<'a> {
} }
} }
impl nom::InputTake for TokenSpan<'_> { impl InputTake for TokenSpan<'_> {
fn take(&self, count: usize) -> Self { fn take(&self, count: usize) -> Self {
TokenSpan::with_pos(&self.rest[..count], self.pos + count) TokenSpan::with_pos(&self.rest[..count], self.pos + count)
} }
@ -133,10 +135,16 @@ impl InputPos for TokenSpan<'_> {
} }
/// combinator that matches a token kind /// combinator that matches a token kind
pub fn token<'a>(kind: TokenKind) -> impl FnMut(TokenSpan<'a>) -> IResult<TokenSpan, &Token> { pub fn token<'a>(kind: TokenKind) -> impl FnMut(TokenSpan<'a>) -> IResult<TokenSpan, Token> {
move |input: TokenSpan| { move |input: TokenSpan| {
let next = &input.rest[0]; let next = if let Some(i) = input.rest.first() {
let kind = kind; *i
} else {
return Err(nom::Err::Error(Error::from_error_kind(
input,
nom::error::ErrorKind::Eof,
)));
};
if next.kind == kind { if next.kind == kind {
let rest = TokenSpan::with_pos(&input.rest[1..], input.pos + 1); let rest = TokenSpan::with_pos(&input.rest[1..], input.pos + 1);
Ok((rest, next)) Ok((rest, next))
@ -210,7 +218,7 @@ fn lex_keywords(input: Span) -> IResult<Span, Token> {
map(tag("match"), |_| TokenKind::Match), map(tag("match"), |_| TokenKind::Match),
map(tag("proc"), |_| TokenKind::Proc), map(tag("proc"), |_| TokenKind::Proc),
map(tag("comb"), |_| TokenKind::Comb), map(tag("comb"), |_| TokenKind::Comb),
map(tag("state"), |_| TokenKind::Proc), map(tag("state"), |_| TokenKind::State),
))), ))),
|(span, kind)| Token::new(span, kind), |(span, kind)| Token::new(span, kind),
)(input) )(input)