1
0

Add sh function; Improve type check error output

This commit is contained in:
Jeff 2023-12-15 17:54:11 -05:00
parent 5beda4695d
commit 3b7e75c41c
3 changed files with 37 additions and 4 deletions

View File

@ -47,9 +47,7 @@ impl AbstractTree for Identifier {
} else {
for built_in_function in BUILT_IN_FUNCTIONS {
if self.0 == built_in_function.name() {
if let Type::Function { return_type, .. } = built_in_function.r#type() {
return Ok(*return_type);
}
return Ok(built_in_function.r#type());
}
}

View File

@ -0,0 +1,33 @@
use std::process::Command;
use crate::{BuiltInFunction, Error, Map, Result, Type, Value};
pub struct Sh;
impl BuiltInFunction for Sh {
fn name(&self) -> &'static str {
"sh"
}
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
Error::expect_argument_amount(self, 1, arguments.len())?;
let command_text = arguments.first().unwrap().as_string()?;
let mut command = Command::new("sh");
for word in command_text.split(' ') {
command.arg(word);
}
let output = command.spawn()?.wait_with_output()?.stdout;
Ok(Value::String(String::from_utf8(output)?))
}
fn r#type(&self) -> crate::Type {
Type::Function {
parameter_types: vec![Type::String],
return_type: Box::new(Type::String),
}
}
}

View File

@ -3,6 +3,7 @@ use crate::{Map, Result, Type, Value};
mod assert;
mod collections;
mod commands;
mod data_formats;
mod fs;
mod network;
@ -14,10 +15,11 @@ mod r#type;
///
/// This is the public interface to access built-in functions by iterating over
/// the references it holds.
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 15] = [
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 16] = [
&assert::Assert,
&assert::AssertEqual,
&collections::Length,
&commands::Sh,
&data_formats::FromJson,
&data_formats::ToJson,
&fs::Read,