reference strings, first error reporting

This commit is contained in:
NotAFile 2022-01-05 00:13:56 +01:00
parent 5b4f378526
commit 33149eb5aa
3 changed files with 56 additions and 26 deletions

View File

@ -31,7 +31,36 @@ fn make_pubid(id: &str) -> String {
}
#[derive(Debug)]
pub struct CompileError;
pub enum CompileErrorKind {
UndefinedReference(String)
}
#[derive(Debug)]
pub struct CompileError {
kind: CompileErrorKind,
}
impl CompileError {
fn new(kind: CompileErrorKind) -> Self {
Self {
kind
}
}
}
pub enum GenericParam<T> {
Unsolved,
Solved(T),
}
pub enum Type {
/// a wire of some width
Wire(GenericParam<u32>)
}
// module that can be instantiated like a function
pub struct Callable {
}
fn lower_expression(module: &mut rtlil::Module, expr: &parser::Expression) -> Result<String, CompileError> {
match expr {
@ -43,7 +72,7 @@ fn lower_expression(module: &mut rtlil::Module, expr: &parser::Expression) -> Re
let mut args_resolved = call.args.iter().map(|expr| lower_expression(module, expr));
// TODO: make this sensible
let cell = match call.name.as_str() {
let cell = match *call.name.fragment() {
"and" => {
let arg_a = args_resolved.next().unwrap()?;
let arg_b = args_resolved.next().unwrap()?;
@ -66,12 +95,12 @@ fn lower_expression(module: &mut rtlil::Module, expr: &parser::Expression) -> Re
let cell_id = module.make_genid("reduce_or");
builtin_unop_cell("$reduce_or", &cell_id, &arg_a, &output_gen_id)
}
_ => return Err(CompileError {}),
name => return Err(CompileError::new(CompileErrorKind::UndefinedReference(name.to_owned()))),
};
module.add_cell(cell);
Ok(output_gen_id)
}
parser::Expression::Operation(op) => todo!(),
parser::Expression::Operation(_op) => todo!(),
}
}

View File

@ -2,6 +2,7 @@ mod literals;
mod parser;
mod rtlil;
mod frontend;
mod builtin_cells;
use nom::error::convert_error;
use std::fs::File;

View File

@ -43,8 +43,8 @@ fn intliteral(input: Span) -> IResult<Span, (u64, u64)> {
}
#[derive(Debug)]
pub struct NetDecl {
pub name: String,
pub struct NetDecl<'a> {
pub name: &'a str,
pub width: Option<u64>,
pub value: Option<(u64, u64)>,
}
@ -59,44 +59,44 @@ pub enum PortDirection {
pub struct PortDecl<'a> {
pub pos: Span<'a>,
pub direction: PortDirection,
pub net: NetDecl,
pub net: NetDecl<'a>,
}
#[derive(Debug)]
pub struct Module<'a> {
pub name: String,
pub name: &'a str,
pub ports: Vec<PortDecl<'a>>,
pub statements: Vec<Statement>,
pub statements: Vec<Statement<'a>>,
}
#[derive(Debug)]
pub enum Statement {
Assign(Assign),
pub enum Statement<'a> {
Assign(Assign<'a>),
}
#[derive(Debug)]
pub struct Assign {
pub lhs: String,
pub expr: Expression,
pub struct Assign<'a> {
pub lhs: &'a str,
pub expr: Expression<'a>,
}
#[derive(Debug)]
pub enum Operation {
And { a: String, b: Expression },
Or { a: String, b: Expression },
pub enum Operation<'a> {
And { a: String, b: Expression<'a> },
Or { a: String, b: Expression<'a> },
}
#[derive(Debug)]
pub struct Call {
pub name: String,
pub args: Vec<Expression>,
pub struct Call<'a> {
pub name: Span<'a>,
pub args: Vec<Expression<'a>>,
}
#[derive(Debug)]
pub enum Expression {
Ident(String),
Call(Box<Call>),
Operation(Box<Operation>),
pub enum Expression<'a> {
Ident(&'a str),
Call(Box<Call<'a>>),
Operation(Box<Operation<'a>>),
}
fn declaration(i: Span) -> IResult<Span, NetDecl> {
@ -108,7 +108,7 @@ fn declaration(i: Span) -> IResult<Span, NetDecl> {
opt(preceded(ws0(char('=')), intliteral)),
)),
|(_, width, ident, value)| NetDecl {
name: (*ident.fragment()).into(),
name: ident.fragment(),
width,
value,
},
@ -167,7 +167,7 @@ fn call_item(input: Span) -> IResult<Span, Call> {
),
)),
|(name, args)| Call {
name: (*name.fragment()).into(),
name: name,
args,
},
)(input)