Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace let* implementation with let #50

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/builtin/functions/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{destruct_bind, list};
use std::convert::TryInto;
use std::rc::Rc;

use tulisp_proc_macros::crate_add_func;
use tulisp_proc_macros::{crate_fn, crate_fn_no_eval};

pub(super) fn reduce_with(
Expand Down Expand Up @@ -203,7 +204,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
Ok(value)
}

#[crate_fn_no_eval(add_func = "ctx", name = "let")]
#[crate_fn_no_eval]
fn impl_let(
ctx: &mut TulispContext,
varlist: TulispObject,
Expand Down Expand Up @@ -254,6 +255,8 @@ pub(crate) fn add(ctx: &mut TulispContext) {

ret
}
crate_add_func!(ctx, impl_let, "let");
crate_add_func!(ctx, impl_let, "let*");

#[crate_fn_no_eval(add_func = "ctx")]
fn progn(ctx: &mut TulispContext, rest: TulispObject) -> Result<TulispObject, Error> {
Expand Down
36 changes: 0 additions & 36 deletions src/builtin/macros.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::rc::Rc;

use tulisp_proc_macros::{crate_add_macro, crate_fn_no_eval};

use crate::cons::Cons;
use crate::context::TulispContext;
use crate::error::Error;
use crate::TulispObject;
Expand Down Expand Up @@ -41,37 +38,6 @@ fn thread_last(_ctx: &mut TulispContext, vv: &TulispObject) -> Result<TulispObje
}
}

#[crate_fn_no_eval]
fn let_star(
ctx: &mut TulispContext,
varlist: TulispObject,
rest: TulispObject,
) -> Result<TulispObject, Error> {
fn unwrap_varlist(
ctx: &mut TulispContext,
varlist: TulispObject,
body: TulispObject,
) -> Result<TulispObject, Error> {
destruct_bind!((nextvar &rest rest) = varlist);

let mut ret = Cons::new(ctx.intern("let"), TulispObject::nil());
ret.push(list!(,nextvar)?)?;
if !rest.null() {
ret.push(unwrap_varlist(ctx, rest, body)?)?;
} else {
for ele in body.base_iter() {
ret.push(ele.clone())?;
}
}
Ok(TulispValue::List {
cons: ret,
ctxobj: Some(ctx.intern("let").get()?),
}
.into_ref())
}
unwrap_varlist(ctx, varlist, rest)
}

pub(crate) fn add(ctx: &mut TulispContext) {
ctx.intern("->")
.set_scope(TulispValue::Macro(Rc::new(thread_first)).into_ref())
Expand All @@ -85,6 +51,4 @@ pub(crate) fn add(ctx: &mut TulispContext) {
ctx.intern("thread-last")
.set_scope(TulispValue::Macro(Rc::new(thread_last)).into_ref())
.unwrap();

crate_add_macro!(ctx, let_star, "let*");
}
44 changes: 22 additions & 22 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn test_conditionals() -> Result<(), Error> {

tulisp_assert! {
program: "(macroexpand '(if-let (c) (+ c 10) 2))",
result_str: "'(let ((s (and t c))) (if s (+ c 10) 2))",
result_str: "'(let* ((s (and t c))) (if s (+ c 10) 2))",
}
tulisp_assert! {
program: "(defun test (&optional c) (if-let (c) (+ c 10) 2)) (list (test) (test 2))",
Expand All @@ -203,27 +203,27 @@ fn test_conditionals() -> Result<(), Error> {

tulisp_assert! {
program: "(macroexpand '(when-let (c) (+ c 10)))",
result_str: "'(let ((s (and t c))) (if s (+ c 10) nil))",
result_str: "'(let* ((s (and t c))) (if s (+ c 10) nil))",
}
tulisp_assert! {
program: "(macroexpand '(when-let (c) (+ c 10) 2))",
result_str: "'(let ((s (and t c))) (if s (progn (+ c 10) 2) nil))",
result_str: "'(let* ((s (and t c))) (if s (progn (+ c 10) 2) nil))",
}
tulisp_assert! {
program: "(macroexpand '(when-let ((q c) d (w 10)) 2 (+ c d w)))",
result_str: r#"'
(let ((q (and t c)))
(let ((d (and q d)))
(let ((w (and d 10)))
(if w
(progn 2 (+ c d w))
nil))))
(let* ((q (and t c))
(d (and q d))
(w (and d 10)))
(if w
(progn 2 (+ c d w))
nil))
"#,
}

tulisp_assert! {
program: "(macroexpand '(while-let (c) (+ c 10)))",
result_str: "'(while (let ((s (and t c))) (if s (progn (+ c 10) t) nil)))",
result_str: "'(while (let* ((s (and t c))) (if s (progn (+ c 10) t) nil)))",
}
tulisp_assert! {
program: "(let ((ll '(1 2 3)) (vv 0)) (while-let (x (car ll)) (setq ll (cdr ll)) (setq vv (+ vv x))) vv)",
Expand Down Expand Up @@ -835,18 +835,18 @@ fn test_threading_macros() -> Result<(), Error> {
(print a))))
"##,
result_str: r##"
'(let ((s (and t a)))
(let ((s (and s b)))
(if s
(print a)
(let ((s (and t a)))
(if s
(print a)
(let ((s (and t b)))
(if s
(print b)
nil)))))))
"##,
'(let* ((s (and t a))
(s (and s b)))
(if s
(print a)
(let* ((s (and t a)))
(if s
(print a)
(let* ((s (and t b)))
(if s
(print b)
nil))))))
"##,
}

tulisp_assert! {
Expand Down