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::borrow::Borrow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::{NativeFunc, Value};
|
use super::{macros::arg_count, NativeFunc, Value};
|
||||||
use crate::macros::arg_count;
|
|
||||||
use crate::Node;
|
use crate::Node;
|
||||||
|
|
||||||
macro_rules! arithmetic {
|
macro_rules! arithmetic {
|
||||||
|
@ -69,9 +68,7 @@ pub(super) fn core() -> HashMap<String, Value> {
|
||||||
(
|
(
|
||||||
"eq?",
|
"eq?",
|
||||||
NativeFunc(|args| {
|
NativeFunc(|args| {
|
||||||
if args.len() < 2 {
|
arg_count!(min: 2, args.len());
|
||||||
return Node::Error(format!("expected at least 2 args, got {}", args.len()));
|
|
||||||
}
|
|
||||||
|
|
||||||
args.into_iter()
|
args.into_iter()
|
||||||
.reduce(|lhs, rhs| match (lhs, rhs) {
|
.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::borrow::Borrow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use super::{NativeFunc, Value};
|
use super::{macros::arg_count, NativeFunc, Value};
|
||||||
use crate::Node;
|
use crate::Node;
|
||||||
|
|
||||||
pub(super) fn io() -> HashMap<String, Value> {
|
pub(super) fn io() -> HashMap<String, Value> {
|
||||||
|
@ -39,8 +39,7 @@ pub(super) fn io() -> HashMap<String, Value> {
|
||||||
(
|
(
|
||||||
"read-file",
|
"read-file",
|
||||||
NativeFunc(|args| {
|
NativeFunc(|args| {
|
||||||
// TODO: implement this
|
arg_count!(1, args.len());
|
||||||
// arg_count!(1, args.len());
|
|
||||||
|
|
||||||
let val = args[0].borrow();
|
let val = args[0].borrow();
|
||||||
if let Node::String(path) = val {
|
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_export]
|
||||||
macro_rules! arg_count {
|
macro_rules! arg_count {
|
||||||
($expected:expr, $given:expr) => {
|
($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) => {
|
(modulo: $modulo:expr, $given:expr) => {
|
||||||
if $given % $modulo != 0 {
|
if $given % $modulo != 0 {
|
||||||
return Node::Error(format!(
|
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 core;
|
||||||
mod io;
|
mod io;
|
||||||
|
mod macros;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct NativeFunc(fn(args: Vec<Node>) -> Node);
|
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();
|
let mut exprs = Vec::new();
|
||||||
|
|
||||||
for node in ast {
|
for node in ast {
|
||||||
dbg!(&node);
|
|
||||||
let res = eval_node(env, node)?;
|
let res = eval_node(env, node)?;
|
||||||
if let Node::Void = res {
|
if let Node::Void = res {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
mod env;
|
mod env;
|
||||||
mod error;
|
mod error;
|
||||||
mod evaluator;
|
mod evaluator;
|
||||||
mod macros;
|
|
||||||
|
|
||||||
pub use env::Environment;
|
pub use env::Environment;
|
||||||
pub use error::{Error, Result};
|
pub use error::{Error, Result};
|
||||||
|
|
Loading…
Reference in a new issue