Skip to content
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
68 changes: 0 additions & 68 deletions src/builtin/functions/common.rs

This file was deleted.

28 changes: 11 additions & 17 deletions src/builtin/functions/conditionals.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use crate::{
Error, TulispContext, TulispObject, TulispValue,
builtin::functions::common::eval_2_arg_special_form,
destruct_bind, destruct_eval_bind,
Error, TulispContext, TulispObject, destruct_bind, destruct_eval_bind,
eval::{eval_and_then, eval_basic},
list,
lists::{last, length},
};
use std::rc::Rc;

pub(crate) fn add(ctx: &mut TulispContext) {
fn impl_if(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
eval_2_arg_special_form(ctx, "if", args, true, |ctx, cond, then, else_body| {
if eval_and_then(ctx, cond, |_, x| Ok(x.is_truthy()))? {
ctx.eval(then)
} else {
ctx.eval_progn(else_body)
}
})
}
intern_set_func!(ctx, impl_if, "if");
ctx.add_special_form("if", |ctx, args| {
destruct_bind!((cond then &rest body) = args);
if ctx.eval_and_then(&cond, |_, x| Ok(x.is_truthy()))? {
ctx.eval(&then)
} else {
ctx.eval_progn(&body)
}
});

fn when(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
destruct_bind!((cond &rest body) = args);
Expand All @@ -36,15 +31,14 @@ pub(crate) fn add(ctx: &mut TulispContext) {
}
ctx.add_macro("unless", unless);

fn cond(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("cond", |ctx, args| {
for item in args.base_iter() {
if item.car_and_then(|x| eval_and_then(ctx, x, |_, x| Ok(x.is_truthy())))? {
return item.cdr_and_then(|x| ctx.eval_progn(x));
}
}
Ok(TulispObject::nil())
}
intern_set_func!(ctx, cond, "cond");
});

// Constructs for combining conditions
fn not(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
Expand Down
37 changes: 15 additions & 22 deletions src/builtin/functions/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::lists;
use crate::value::DefunParams;
use crate::{destruct_bind, list};
use std::convert::TryInto;
use std::rc::Rc;

pub(super) fn reduce_with(
ctx: &mut TulispContext,
Expand Down Expand Up @@ -221,7 +220,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
Ok(result)
});

fn setq(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("setq", |ctx, args| {
let value = args.cdr_and_then(|args| {
if args.null() {
return Err(Error::type_mismatch(
Expand All @@ -239,20 +238,19 @@ pub(crate) fn add(ctx: &mut TulispContext) {
})?;
args.car_and_then(|name| name.set(value.clone()))?;
Ok(value)
}
intern_set_func!(ctx, setq);
});

fn set(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("set", |ctx, args| {
let value = args.cdr_and_then(|args| {
if args.null() {
return Err(Error::type_mismatch(
"setq requires exactly 2 arguments".to_string(),
"set requires exactly 2 arguments".to_string(),
));
}
args.cdr_and_then(|x| {
if !x.null() {
return Err(Error::type_mismatch(
"setq requires exactly 2 arguments".to_string(),
"set requires exactly 2 arguments".to_string(),
));
}
args.car_and_then(|arg| ctx.eval(arg))
Expand All @@ -262,8 +260,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
ctx.eval_and_then(name_sym, |_, name| name.set(value.clone()))
})?;
Ok(value)
}
intern_set_func!(ctx, set);
});

fn impl_let(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
destruct_bind!((varlist &rest rest) = args);
Expand Down Expand Up @@ -492,7 +489,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {

// List functions

fn impl_cons(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("cons", |ctx, args| {
let cdr = args.cdr_and_then(|args| {
if args.null() {
return Err(Error::type_mismatch(
Expand All @@ -510,8 +507,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
})?;
let car = args.car_and_then(|arg| ctx.eval(arg))?;
Ok(TulispObject::cons(car, cdr))
}
intern_set_func!(ctx, impl_cons, "cons");
});

ctx.add_special_form("append", |ctx, args| {
destruct_eval_bind!(ctx, (first &rest rest) = args);
Expand All @@ -521,7 +517,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
Ok(first)
});

fn dolist(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("dolist", |ctx, args| {
destruct_bind!((spec &rest body) = args);
destruct_bind!((var list &optional result) = spec);
let mut list = ctx.eval(&list)?;
Expand All @@ -534,10 +530,9 @@ pub(crate) fn add(ctx: &mut TulispContext) {
}
var.unset()?;
ctx.eval(&result)
}
intern_set_func!(ctx, dolist);
});

fn dotimes(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("dotimes", |ctx, args| {
destruct_bind!((spec &rest body) = args);
destruct_bind!((var count &optional result) = spec);
var.set_scope(TulispObject::from(0))?;
Expand All @@ -548,10 +543,9 @@ pub(crate) fn add(ctx: &mut TulispContext) {
}
var.unset()?;
ctx.eval(&result)
}
intern_set_func!(ctx, dotimes);
});

fn list(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
ctx.add_special_form("list", |ctx, args| {
let (ctxobj, span) = (args.ctxobj(), args.span());
let mut cons: Option<Cons> = None;
for ele in args.base_iter() {
Expand All @@ -566,8 +560,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
Some(cons) => Ok(TulispValue::List { cons, ctxobj }.into_ref(span)),
None => Ok(TulispObject::nil()),
}
}
intern_set_func!(ctx, list);
});

ctx.add_special_form("mapcar", |ctx, args| {
destruct_eval_bind!(ctx, (func seq) = args);
Expand Down Expand Up @@ -622,7 +615,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
}
args.car_and_then(|arg| eval_and_then(ctx, &arg, |_, x| Ok(x.$name().into())))
}
intern_set_func!(ctx, $name);
ctx.add_special_form(stringify!($name), $name);
};
}
predicate_function!(consp);
Expand Down
12 changes: 0 additions & 12 deletions src/builtin/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ macro_rules! binary_ops {
}};
}

macro_rules! intern_set_func {
($ctx:ident, $func: ident, $name: expr) => {
$ctx.intern($name)
.set_global(TulispValue::Func(Rc::new($func)).into_ref(None))
.unwrap();
};
($ctx:ident, $func: ident) => {
intern_set_func!($ctx, $func, stringify!($func));
};
}

pub(crate) mod common;
mod comparison_of_strings;
mod conditionals;
mod equality_predicates;
Expand Down
10 changes: 4 additions & 6 deletions src/builtin/functions/numbers/arithmetic_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ use crate::eval::eval;

use crate::{Error, TulispContext, TulispObject, TulispValue};

use std::rc::Rc;

pub(crate) fn add(ctx: &mut TulispContext) {
fn add(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
reduce_with(ctx, args, binary_ops!(std::ops::Add::add))
}
intern_set_func!(ctx, add, "+");
ctx.add_special_form("+", add);

fn sub(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
if let Some(cons) = args.as_list_cons() {
Expand All @@ -26,12 +24,12 @@ pub(crate) fn add(ctx: &mut TulispContext) {
))
}
}
intern_set_func!(ctx, sub, "-");
ctx.add_special_form("-", sub);

fn mul(ctx: &mut TulispContext, args: &TulispObject) -> Result<TulispObject, Error> {
reduce_with(ctx, args, binary_ops!(std::ops::Mul::mul))
}
intern_set_func!(ctx, mul, "*");
ctx.add_special_form("*", mul);

fn div(ctx: &mut TulispContext, rest: &TulispObject) -> Result<TulispObject, Error> {
let mut iter = rest.base_iter();
Expand All @@ -47,7 +45,7 @@ pub(crate) fn add(ctx: &mut TulispContext) {
}
reduce_with(ctx, rest, binary_ops!(std::ops::Div::div))
}
intern_set_func!(ctx, div, "/");
ctx.add_special_form("/", div);

ctx.add_special_form("1+", |ctx, args| {
destruct_eval_bind!(ctx, (number) = args);
Expand Down
15 changes: 7 additions & 8 deletions src/builtin/functions/numbers/comparison_of_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
Error, TulispContext, TulispObject, TulispValue, builtin::functions::functions::reduce_with,
Error, TulispContext, TulispObject, builtin::functions::functions::reduce_with,
destruct_eval_bind,
};
use std::rc::Rc;

macro_rules! compare_ops {
($oper:expr) => {{
Expand Down Expand Up @@ -70,26 +69,26 @@ macro_rules! compare_impl {

pub(crate) fn add(ctx: &mut TulispContext) {
compare_impl!(gt, ">");
intern_set_func!(ctx, gt, ">");
ctx.add_special_form(">", gt);

compare_impl!(ge, ">=");
intern_set_func!(ctx, ge, ">=");
ctx.add_special_form(">=", ge);

compare_impl!(lt, "<");
intern_set_func!(ctx, lt, "<");
ctx.add_special_form("<", lt);

compare_impl!(le, "<=");
intern_set_func!(ctx, le, "<=");
ctx.add_special_form("<=", le);

fn max(ctx: &mut TulispContext, rest: &TulispObject) -> Result<TulispObject, Error> {
reduce_with(ctx, rest, max_min_ops!(max))
}
intern_set_func!(ctx, max, "max");
ctx.add_special_form("max", max);

fn min(ctx: &mut TulispContext, rest: &TulispObject) -> Result<TulispObject, Error> {
reduce_with(ctx, rest, max_min_ops!(min))
}
intern_set_func!(ctx, min, "min");
ctx.add_special_form("min", min);

ctx.add_special_form("abs", |ctx, args| {
destruct_eval_bind!(ctx, (number) = args);
Expand Down
Loading