From 33149eb5aa291450612dc8070cd4b4e7f836253c Mon Sep 17 00:00:00 2001 From: NotAFile Date: Wed, 5 Jan 2022 00:13:56 +0100 Subject: [PATCH] reference strings, first error reporting --- src/frontend.rs | 37 +++++++++++++++++++++++++++++++++---- src/main.rs | 1 + src/parser.rs | 44 ++++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/src/frontend.rs b/src/frontend.rs index e1f20d4..8aeb19b 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -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 { + Unsolved, + Solved(T), +} + +pub enum Type { + /// a wire of some width + Wire(GenericParam) +} + +// module that can be instantiated like a function +pub struct Callable { +} fn lower_expression(module: &mut rtlil::Module, expr: &parser::Expression) -> Result { 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!(), } } diff --git a/src/main.rs b/src/main.rs index 03067b2..43512f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod literals; mod parser; mod rtlil; mod frontend; +mod builtin_cells; use nom::error::convert_error; use std::fs::File; diff --git a/src/parser.rs b/src/parser.rs index c31b926..7f46583 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -43,8 +43,8 @@ fn intliteral(input: Span) -> IResult { } #[derive(Debug)] -pub struct NetDecl { - pub name: String, +pub struct NetDecl<'a> { + pub name: &'a str, pub width: Option, 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>, - pub statements: Vec, + pub statements: Vec>, } #[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, +pub struct Call<'a> { + pub name: Span<'a>, + pub args: Vec>, } #[derive(Debug)] -pub enum Expression { - Ident(String), - Call(Box), - Operation(Box), +pub enum Expression<'a> { + Ident(&'a str), + Call(Box>), + Operation(Box>), } fn declaration(i: Span) -> IResult { @@ -108,7 +108,7 @@ fn declaration(i: Span) -> IResult { 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 { ), )), |(name, args)| Call { - name: (*name.fragment()).into(), + name: name, args, }, )(input)