refactor: move arg count
We only use this in the evaluator env, so move it to there.
This commit is contained in:
parent
de4e9b4bd3
commit
4f882b35a6
6 changed files with 11 additions and 11 deletions
7
mute-interpreter/src/env/core.rs
vendored
7
mute-interpreter/src/env/core.rs
vendored
|
@ -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) {
|
||||
|
|
5
mute-interpreter/src/env/io.rs
vendored
5
mute-interpreter/src/env/io.rs
vendored
|
@ -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 {
|
||||
|
|
|
@ -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!(
|
1
mute-interpreter/src/env/mod.rs
vendored
1
mute-interpreter/src/env/mod.rs
vendored
|
@ -6,6 +6,7 @@ use crate::Node;
|
|||
|
||||
mod core;
|
||||
mod io;
|
||||
mod macros;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NativeFunc(fn(args: Vec<Node>) -> Node);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
mod env;
|
||||
mod error;
|
||||
mod evaluator;
|
||||
mod macros;
|
||||
|
||||
pub use env::Environment;
|
||||
pub use error::{Error, Result};
|
||||
|
|
Loading…
Reference in a new issue