Skip to content

Commit

Permalink
Add testing infrastructure (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
maplant authored Nov 8, 2024
1 parent 43100ce commit a0e3689
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 77 deletions.
14 changes: 7 additions & 7 deletions proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub fn builtin(name: TokenStream, item: TokenStream) -> TokenStream {
let arg_indices: Vec<_> = (0..num_args).into_iter().collect();
parse_quote! {
fn #wrapper_name(
cont: Option<std::sync::Arc<crate::continuation::Continuation>>,
args: Vec<crate::gc::Gc<crate::value::Value>>
) -> futures::future::BoxFuture<'static, Result<Vec<crate::gc::Gc<crate::value::Value>>, crate::error::RuntimeError>> {
cont: Option<std::sync::Arc<::scheme_rs::continuation::Continuation>>,
args: Vec<::scheme_rs::gc::Gc<::scheme_rs::value::Value>>
) -> futures::future::BoxFuture<'static, Result<Vec<::scheme_rs::gc::Gc<::scheme_rs::value::Value>>, ::scheme_rs::error::RuntimeError>> {
Box::pin(
async move {
#impl_name(
Expand All @@ -45,9 +45,9 @@ pub fn builtin(name: TokenStream, item: TokenStream) -> TokenStream {
let arg_indices: Vec<_> = (0..num_args).into_iter().collect();
parse_quote! {
fn #wrapper_name(
cont: Option<std::sync::Arc<crate::continuation::Continuation>>,
mut required_args: Vec<crate::gc::Gc<crate::value::Value>>
) -> futures::future::BoxFuture<'static, Result<Vec<crate::gc::Gc<crate::value::Value>>, crate::error::RuntimeError>> {
cont: Option<std::sync::Arc<::scheme_rs::continuation::Continuation>>,
mut required_args: Vec<::scheme_rs::gc::Gc<::scheme_rs::value::Value>>
) -> futures::future::BoxFuture<'static, Result<Vec<::scheme_rs::gc::Gc<::scheme_rs::value::Value>>, ::scheme_rs::error::RuntimeError>> {
let var_args = required_args.split_off(#num_args);
Box::pin(
async move {
Expand All @@ -67,7 +67,7 @@ pub fn builtin(name: TokenStream, item: TokenStream) -> TokenStream {
#wrapper

inventory::submit! {
crate::builtin::Builtin::new(#name, #num_args, #is_variadic, #wrapper_name)
::scheme_rs::builtin::Builtin::new(#name, #num_args, #is_variadic, #wrapper_name)
}
}
.into()
Expand Down
46 changes: 0 additions & 46 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,6 @@ pub struct LexicalContour {
}

impl LexicalContour {
/*
fn strip<'a>(&self, ident: &'a Identifier) -> Cow<'a, Identifier> {
if ident.marks.contains(&self.mark) {
let mut stripped = ident.clone();
stripped.mark(self.mark);
Cow::Owned(stripped)
} else {
Cow::Borrowed(ident)
}
}
pub fn strip_unused_marks<'a>(&'a self, ident: &'a mut Identifier) -> BoxFuture<'a, ()> {
Box::pin(async move {
if ident.marks.contains(&self.mark) && self.vars.contains_key(ident)
|| self.macros.contains_key(ident)
{
return;
}
ident.marks.remove(&self.mark);
self.up.strip_unused_marks(ident).await;
})
}
*/

pub fn is_bound<'a>(&'a self, ident: &'a Identifier) -> BoxFuture<'a, bool> {
Box::pin(async move {
self.vars.contains_key(ident)
Expand Down Expand Up @@ -153,18 +129,6 @@ impl ExpansionContext {
} else {
self.up.fetch_macro(ident).await
}
/*
let ident = if ident.marks.contains(&self.mark) {
let stripped = self.strip(ident);
if let result @ Some(_) = self.macro_env.fetch_macro(&stripped).await {
return result;
}
stripped
} else {
Cow::Borrowed(ident)
};
self.up.fetch_macro(&ident).await
*/
})
}
}
Expand All @@ -187,16 +151,6 @@ pub enum Env {
}

impl Env {
/*
pub async fn strip_unused_marks(&self, ident: &mut Identifier) {
match self {
Self::Expansion(expansion) => expansion.read().await.strip_unused_marks(ident).await,
Self::LexicalContour(contour) => contour.read().await.strip_unused_marks(ident).await,
_ => (),
}
}
*/

pub async fn is_bound(&self, ident: &Identifier) -> bool {
match self {
Self::Top => false,
Expand Down
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ pub enum RuntimeErrorKind {
expected: usize,
provided: usize,
},
AssertEqFailed {
expected: String,
actual: String,
},
DivisionByZero,
CompileError(CompileError),
AbandonCurrentContinuation {
Expand Down Expand Up @@ -88,6 +92,15 @@ impl RuntimeError {
}
}

pub fn assert_eq_failed(expected: &str, actual: &str) -> Self {
let expected = expected.to_string();
let actual = actual.to_string();
Self {
backtrace: Vec::new(),
kind: RuntimeErrorKind::AssertEqFailed { expected, actual },
}
}

pub fn undefined_variable(ident: Identifier) -> Self {
Self {
backtrace: Vec::new(),
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate self as scheme_rs;

pub mod ast;
pub mod builtin;
pub mod compile;
Expand All @@ -16,3 +18,5 @@ pub mod proc;
pub mod syntax;
pub mod util;
pub mod value;

pub use proc_macros::*;
5 changes: 5 additions & 0 deletions src/stdlib.scm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
(memp (lambda (x) (eq? x obj)) list))
(define call-with-current-continuation call/cc)

;;
;; WIP: All of the car/cdr combinations
(define caar (lambda (x) (car (car x))))
(define cadr (lambda (x) (car (cdr x))))

;;
;; Complex definitions:
;;
Expand Down
24 changes: 0 additions & 24 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub enum Syntax {
span: Span,
},
Identifier {
// #[debug(flatten = ".name")]
ident: Identifier,
#[debug(skip)]
bound: bool,
Expand All @@ -75,29 +74,6 @@ pub enum Syntax {
},
}

impl fmt::Display for Syntax {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Null { .. } => write!(f, "()")?,
Self::List { list, .. } => {
write!(f, "(")?;
for item in list {
write!(f, "{} ", item)?;
}
write!(f, ")")?;
}
Self::Literal { literal, .. } => {
write!(f, "{:?}", literal)?;
}
Self::Identifier { ident, .. } => {
write!(f, "{}", ident.name)?;
}
_ => (),
}
Ok(())
}
}

impl Syntax {
pub fn mark(&mut self, mark: Mark) {
match self {
Expand Down
45 changes: 45 additions & 0 deletions tests/r6rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::sync::Arc;

use scheme_rs::{
builtin,
continuation::Continuation,
env::Env,
error::RuntimeError,
gc::Gc,
lex::Token,
syntax::ParsedSyntax,
value::{eqv, Value},
};

#[builtin("assert-eq")]
pub async fn test_assert(
_cont: &Option<Arc<Continuation>>,
arg1: &Gc<Value>,
arg2: &Gc<Value>,
) -> Result<Vec<Gc<Value>>, RuntimeError> {
//
if !eqv(arg1, arg2).await {
let arg1 = arg1.read().await.fmt().await;
let arg2 = arg2.read().await.fmt().await;
Err(RuntimeError::assert_eq_failed(&arg1, &arg2))
} else {
Ok(vec![])
}
}

#[tokio::test]
async fn r6rs() {
let top = Env::top().await;

let r6rs_tok = Token::tokenize_file(include_str!("r6rs.scm"), "r6rs.scm").unwrap();
let r6rs_sexprs = ParsedSyntax::parse(&r6rs_tok).unwrap();
for sexpr in r6rs_sexprs {
sexpr
.compile(&top, &None)
.await
.unwrap()
.eval(&top, &None)
.await
.unwrap();
}
}
Loading

0 comments on commit a0e3689

Please sign in to comment.