refactor: move arg count

We only use this in the evaluator env, so move it to there.
This commit is contained in:
Roman Godmaire 2024-05-11 07:53:39 -04:00
parent de4e9b4bd3
commit 4f882b35a6
6 changed files with 11 additions and 11 deletions

View file

@ -1,8 +1,7 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use super::{NativeFunc, Value};
use crate::macros::arg_count;
use super::{macros::arg_count, NativeFunc, Value};
use crate::Node;
macro_rules! arithmetic {
@ -69,9 +68,7 @@ pub(super) fn core() -> HashMap<String, Value> {
(
"eq?",
NativeFunc(|args| {
if args.len() < 2 {
return Node::Error(format!("expected at least 2 args, got {}", args.len()));
}
arg_count!(min: 2, args.len());
args.into_iter()
.reduce(|lhs, rhs| match (lhs, rhs) {

View file

@ -1,7 +1,7 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use super::{NativeFunc, Value};
use super::{macros::arg_count, NativeFunc, Value};
use crate::Node;
pub(super) fn io() -> HashMap<String, Value> {
@ -39,8 +39,7 @@ pub(super) fn io() -> HashMap<String, Value> {
(
"read-file",
NativeFunc(|args| {
// TODO: implement this
// arg_count!(1, args.len());
arg_count!(1, args.len());
let val = args[0].borrow();
if let Node::String(path) = val {

View file

@ -1,4 +1,3 @@
// TODO: argument count checking should happen at parse time, not at runtime
#[macro_export]
macro_rules! arg_count {
($expected:expr, $given:expr) => {
@ -7,6 +6,12 @@ macro_rules! arg_count {
}
};
(min: $min:expr, $given:expr) => {
if $given < $min {
return Node::Error(format!("expected at least {} args, got {}", $min, $given));
}
};
(modulo: $modulo:expr, $given:expr) => {
if $given % $modulo != 0 {
return Node::Error(format!(

View file

@ -6,6 +6,7 @@ use crate::Node;
mod core;
mod io;
mod macros;
#[derive(Debug, Clone)]
pub struct NativeFunc(fn(args: Vec<Node>) -> Node);

View file

@ -7,7 +7,6 @@ pub fn eval(env: &Environment, ast: Vec<Node>) -> Result<Vec<Node>> {
let mut exprs = Vec::new();
for node in ast {
dbg!(&node);
let res = eval_node(env, node)?;
if let Node::Void = res {
continue;

View file

@ -2,7 +2,6 @@
mod env;
mod error;
mod evaluator;
mod macros;
pub use env::Environment;
pub use error::{Error, Result};