Skip to content

Commit

Permalink
Progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
maloneymr committed May 8, 2024
1 parent 5100be4 commit bc4146b
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 44 deletions.
28 changes: 0 additions & 28 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub use typecheckq::TypecheckQ;
pub use packageq::PackageQ;

use std::sync::Arc;
use crate::hir;
use crate::common::*;

#[salsa::database(
Expand All @@ -34,30 +33,3 @@ pub fn compile(input: &str) -> VirdantResult<()> {
package.mlir(&mut stdout).map_err(|_err| VirdantError::Unknown)?;
Ok(())
}

pub fn check_module(input: &str) -> VirdantResult<hir::Package> {
let mut db = Database::default();
db.set_source(Arc::new(input.to_string()));
Ok(db.package_hir()?)
}

#[test]
fn test_checker() {
let mut db = Database::default();
db.set_source(Arc::new("
public module Top {
incoming clk : Clock;
incoming in : Word[8];
outgoing out : Word[8];
reg r : Word[8] on clk <= in;
out := in->add(1w8);
submodule foo of Foo;
}
module Foo {
wire w : Word[8] := 0;
}
".to_string()));

db.check().unwrap();
}
8 changes: 8 additions & 0 deletions src/db/packageq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ use crate::common::*;
use super::TypecheckQ;
use crate::hir;
use crate::ast;
use crate::elab;

#[salsa::query_group(PackageQStorage)]
pub trait PackageQ: TypecheckQ {
fn check_moddef(&self, moddef: Ident) -> VirdantResult<()>;
fn check(&self) -> Result<(), VirdantError>;
fn elaborate(&self, moddef: Ident) -> VirdantResult<elab::Elab>;

fn package_hir(&self) -> VirdantResult<hir::Package>;
}

Expand Down Expand Up @@ -49,6 +52,11 @@ fn check_moddef(db: &dyn PackageQ, moddef: Ident) -> VirdantResult<()> {
errors.check()
}

fn elaborate(db: &dyn PackageQ, moddef: Ident) -> VirdantResult<elab::Elab> {
let package = db.package_hir()?;
package.elab(moddef.into())
}

fn package_hir(db: &dyn PackageQ) -> VirdantResult<hir::Package> {
db.check()?;
let mut moddefs = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion src/db/structureq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn moddef_component_names(db: &dyn StructureQ, moddef: Ident) -> Result<Vec<Iden
for decl in moddef.decls {
match decl {
ast::Decl::Component(component) => result.push(component.name.clone()),
ast::Decl::Submodule(submodule) => (),
ast::Decl::Submodule(_submodule) => (),
ast::Decl::Connect(_connect) => (),
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/db/typecheckq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::ast;
#[salsa::query_group(TypecheckQStorage)]
pub trait TypecheckQ: StructureQ {
fn moddef_context(&self, moddef: Ident) -> Result<Context<Path, Arc<Type>>, VirdantError>;
fn moddef_component_type(&self, moddef: Ident, component: Ident) -> Result<ast::Type, VirdantError>;

fn moddef_hir_typed(&self, moddef: Ident) -> VirdantResult<hir::ModDef>;
fn typecheck_component(&self, moddef: Ident, component: Ident) -> VirdantResult<hir::Expr>;
fn moddef_component_type(&self, moddef: Ident, component: Ident) -> Result<ast::Type, VirdantError>;
fn moddef_component_hir_typed(&self, moddef: Ident, component: Ident) -> VirdantResult<hir::Component>;
}

Expand Down
4 changes: 1 addition & 3 deletions src/elab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::collections::HashMap;
use crate::common::*;
use crate::hir;
use crate::sim::{Sim, SimBuilder};
use crate::types::Type;
use crate::context::Context;

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Elab {
pub moddef: Arc<hir::ModDef>,
pub submodules: HashMap<Ident, Elab>,
Expand Down
7 changes: 0 additions & 7 deletions src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ pub enum Component {
Reg(Ident, Arc<Type>, Expr, /*Option<Value>,*/ Expr),
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum HirType {
Clock,
Word(Width),
Vec(Arc<Type>, usize),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InlineConnect(pub ConnectType, pub Expr);

Expand Down
3 changes: 1 addition & 2 deletions src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ pub fn simulator(input: &str, top: &str) -> VirdantResult<Sim> {

let mut db = Database::default();
db.set_source(Arc::new(input.to_string()));
let elaborated = db.elaborate(top.into())?;

let package = db.package_hir()?;
let elaborated = package.elab(top.into())?;
let sim = elaborated.simulator();
Ok(sim)
}
27 changes: 25 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use crate::context::Context;
use crate::types::Type;
use crate::parse::{parse_package, parse_expr};
use crate::ast;
use crate::db;
use crate::db::*;
use crate::hir::*;

#[test]
fn test_examples() {
let examples_dir = std::path::Path::new("examples");
let mut errors = vec![];
let mut db = Database::default();

if let Ok(entries) = std::fs::read_dir(examples_dir) {
for entry in entries {
Expand All @@ -22,9 +23,10 @@ fn test_examples() {
Ok(text) => text,
Err(_) => panic!("Failed to read file {:?}", entry.path()),
};
db.set_source(Arc::new(text.to_string()));

if let Err(_error) = std::panic::catch_unwind(|| {
db::check_module(&text).unwrap();
db.check().unwrap();
}) {
errors.push(filename.to_string());
}
Expand Down Expand Up @@ -212,3 +214,24 @@ fn test_typecheck_exprs() {
expr.eval(ctx);
}
}

#[test]
fn test_checker() {
let mut db = Database::default();
db.set_source(Arc::new("
public module Top {
incoming clk : Clock;
incoming in : Word[8];
outgoing out : Word[8];
reg r : Word[8] on clk <= in;
out := in->add(1w8);
submodule foo of Foo;
}
module Foo {
wire w : Word[8] := 0;
}
".to_string()));

db.check().unwrap();
}

0 comments on commit bc4146b

Please sign in to comment.