Skip to content

Commit

Permalink
fix(sol-driver): improve driver suite architeture
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed May 12, 2024
1 parent 428cb6c commit 82733c6
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 57 deletions.
67 changes: 52 additions & 15 deletions sol-driver/src/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,68 @@ use crate::RootDb;

#[macro_export]
macro_rules! make_test {
($name:ident, $run:expr) => {
($category: expr, $name:ident, $run:expr) => {
#[test]
fn $name() {
let source_code =
std::fs::read_to_string(concat!("suite/", stringify!($name), ".sol")).unwrap();
let expect = std::fs::read_to_string(concat!("suite/", stringify!($name), ".expect"))
.unwrap_or_default();
$crate::suite::run_test_suite(stringify!($name), &source_code, &expect, $run);
let source_code = std::fs::read_to_string(concat!(
"suite/",
stringify!($category),
"/",
stringify!($name),
".sol"
))
.expect("can't find the source code");
let expect = std::fs::read_to_string(concat!(
"suite/",
stringify!($category),
"/",
stringify!($name),
".expect"
))
.unwrap_or_default();
$crate::suite::run_test_suite(
stringify!($category),
stringify!($name),
&source_code,
&expect,
$run,
);
}
};
($name:ident, $file:expr, $run:expr) => {
($category: expr, $name:ident, $file:expr, $run:expr) => {
#[test]
fn $name() {
let source_code = std::fs::read_to_string(concat!("suite/", $file, ".sol")).unwrap();
let expect =
std::fs::read_to_string(concat!("suite/", $file, ".expect")).unwrap_or_default();
$crate::suite::run_test_suite($file, &source_code, &expect, $run);
let source_code = std::fs::read_to_string(concat!(
"suite/",
stringify!($category),
"/",
$file,
".sol"
))
.unwrap();
let expect = std::fs::read_to_string(concat!(
"suite/",
stringify!($category),
"/",
$file,
".expect"
))
.unwrap_or_default();
$crate::suite::run_test_suite(
stringify!($category),
$file,
&source_code,
&expect,
$run,
);
}
};
}

#[macro_export]
macro_rules! make_test_suite {
(tests ($e:expr) {$($name:ident $file:expr)*} run $run:expr) => {
const _: &str = $e;
$($crate::make_test!($name, $file, $run);)*
(tests $e:ident {$($name:ident)*} run $run:expr) => {
$($crate::make_test!($e, $name, $run);)*
};
}

Expand All @@ -47,6 +83,7 @@ type Expect<'a> = &'a mut dyn Write;

/// Runs a test suite, with the given `name` and `f`.
pub fn run_test_suite(
category: &str,
file: &str,
source_code: &str,
expect: &str,
Expand All @@ -66,7 +103,7 @@ pub fn run_test_suite(

let output = strip_ansi_escapes::strip_str(String::from_utf8_lossy(&output));
if expect.is_empty() {
std::fs::write(format!("suite/{file}.expect"), output).unwrap();
std::fs::write(format!("suite/{category}/{file}.expect"), output).unwrap();
return;
}
if output != expect {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions sol-driver/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use utils::create_package;
pub mod utils;

make_test_suite! {
tests ("church_encoding") {
church_encoding "church_encoding"
tests church_encoding {
church_encoding
}
run |db, source, output| {
let file = SourceFile::new(&db, "repl".into(), "Repl".into(), source);
Expand Down
2 changes: 1 addition & 1 deletion sol-eval/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::Deref;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::sync::Arc;

use sol_thir::Value;
use sol_thir::value::Value;

use crate::stack::Stack;

Expand Down
3 changes: 1 addition & 2 deletions sol-eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use sol_hir::source::{
type_rep::TypeRep,
HirSource,
};
use sol_thir::{Env, Value};
use sol_thir::{shared::Env, value::Value};

pub mod domain;
pub mod logic;
Expand Down Expand Up @@ -66,7 +66,6 @@ pub fn eval_expr(_db: &dyn sol_hir::HirDb, stack: stack::Stack, _env: Env, expr:
match expr {
Expr::Empty | Expr::Error(_) => stack.unwind("empty expressions can't be evaluated"),
Expr::Path(_) => todo!(),
Expr::Meta(_) => todo!(),
Expr::Literal(_) => todo!(),
Expr::Call(_) => todo!(),
Expr::Ann(_) => todo!(),
Expand Down
24 changes: 11 additions & 13 deletions sol-thir-lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,33 @@
#![feature(trait_upcasting)]

use salsa::DbWithJar;
use sol_diagnostic::{Diagnostic, DiagnosticDb, ErrorId, ErrorKind};
use sol_hir::{
debruijin::{Index, Level},
lowering::HirLowering,
package::HasManifest,
primitives::PrimitiveProvider,
solver::{Definition, Reference},
source::{
expr::{Expr, Meta},
literal::Literal,
pattern::Pattern,
type_rep::TypeRep,
HirElement, HirError, Location,
expr::Expr, literal::Literal, pattern::Pattern, type_rep::TypeRep, HirElement, HirError,
Location,
},
HirDb,
};
use sol_syntax::ParseDb;
use sol_thir::{
shared::{ConstructorKind, Context},
debruijin::Level,
shared::{Context, Env},
source::Term,
value::{Type, Value},
ThirDb,
};

extern crate salsa_2022 as salsa;

#[salsa::jar(db = ThirLoweringDb)]
pub struct Jar(thir_lower, thir_eval, thir_quote, thir_infer, thir_quote);
pub struct Jar(thir_eval, thir_infer, thir_check, thir_quote);

/// The database that stores all the information about the source code. It is
/// implemented using the [`salsa`] crate, and it's used by the [`sol-driver`] crate.
pub trait ThirLoweringDb: ThirDb + DbWithJar<Jar> {}

impl<T> ThirLoweringDb for T where T: HirDb + DbWithJar<Jar> {}
impl<T> ThirLoweringDb for T where T: ThirDb + DbWithJar<Jar> {}

#[salsa::tracked]
fn thir_eval(db: &dyn ThirLoweringDb, env: Env, term: Term) -> Value {
Expand All @@ -53,6 +49,8 @@ fn thir_quote(db: &dyn ThirLoweringDb, lvl: Level, value: Value) -> Term {
#[salsa::tracked]
fn thir_infer(db: &dyn ThirLoweringDb, ctx: Context, expr: Expr) -> (Term, Type) {
ctx.location(db).update(expr.location(db));

todo!()
}

/// The check function to check the type of the term.
Expand Down
31 changes: 10 additions & 21 deletions sol-thir/src/debruijin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ use super::*;
/// Defines a debruijin level. It does represent the level of the context/environment
///
/// It can be transformed into a debruijin index by using the [`Level::as_idx`] method.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Level(pub usize);
#[salsa::tracked]
pub struct Level {
pub value: usize,
}

#[derive(thiserror::Error, Debug, Clone)]
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum Level2IdxError {
/// The level is greater than the index.
#[error("expected l > x")]
Expand All @@ -19,34 +21,21 @@ pub enum Level2IdxError {
LevelEqualToZero,
}

#[salsa::tracked]
impl Level {
/// Transforms a level into a debruijin index.
pub fn as_idx(&self, Level(l): Level) -> Result<Index, Level2IdxError> {
let Level(x) = *self;
if l > x {
#[salsa::tracked]
pub fn as_idx(self, db: &dyn crate::ThirDb, l: Level) -> Result<Index, Level2IdxError> {
if l.value(db) > self.value(db) {
return Err(Level2IdxError::LevelGreaterThanIndex);
}
if l == 0 {
if l.value(db) == 0 {
return Err(Level2IdxError::LevelEqualToZero);
}
Ok(Index(0))
}
}

impl std::ops::Add<usize> for Level {
type Output = Self;

fn add(self, rhs: usize) -> Self::Output {
Self(self.0 + rhs)
}
}

impl std::ops::AddAssign<usize> for Level {
fn add_assign(&mut self, rhs: usize) {
self.0 += rhs
}
}

/// Defines a debruijin index. That can be converted by two levels.
///
/// It's used to represent a variable in the syntax tree.
Expand Down
9 changes: 8 additions & 1 deletion sol-thir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ use crate::{
extern crate salsa_2022 as salsa;

pub mod debruijin;
pub mod sexpr;
pub mod shared;
pub mod source;
pub mod value;

#[salsa::jar(db = ThirDb)]
pub struct Jar(shared::Env, shared::Context, debruijin::Indices);
pub struct Jar(
shared::Env,
shared::Context,
debruijin::Indices,
debruijin::Level,
debruijin::Level_as_idx,
);

pub trait ThirDb:
PrimitiveProvider
Expand Down
Empty file added sol-thir/src/sexpr.rs
Empty file.
26 changes: 24 additions & 2 deletions sol-thir/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::{Arc, Mutex};
use std::{
hash::Hash,
sync::{Arc, Mutex},
};

use super::*;

Expand All @@ -14,7 +17,6 @@ pub struct Context {
pub location: CurrentLocation,
}

#[derive(Debug, Clone, Hash, Eq)]
pub struct CurrentLocation(Arc<Mutex<Location>>);

impl CurrentLocation {
Expand All @@ -31,12 +33,32 @@ impl CurrentLocation {
}
}

impl Clone for CurrentLocation {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl std::fmt::Debug for CurrentLocation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.lock().unwrap().fmt(f)
}
}

impl Hash for CurrentLocation {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.lock().unwrap().hash(state)
}
}

impl PartialEq for CurrentLocation {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}

impl Eq for CurrentLocation {}

#[salsa::tracked]
pub struct Env {
pub values: Vec<value::Value>,
Expand Down

0 comments on commit 82733c6

Please sign in to comment.