You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.1 KiB
55 lines
1.1 KiB
//! Repl commands
|
|
use super::*;
|
|
use std::str::FromStr;
|
|
|
|
use super::env::*;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Context<'a>
|
|
{
|
|
/// Environment containing variable name mappings.
|
|
env: &'a mut Lexenv,
|
|
}
|
|
|
|
/// Trait for commands.
|
|
///
|
|
/// # Defining commands
|
|
/// A command object should be created once only, and then referenced and executed using `params` and through mutating `cx`.
|
|
pub trait Command: fmt::Debug
|
|
{
|
|
fn execute(&self, cx: &mut Context<'_>, params: Vec<Value>) -> eyre::Result<()>;
|
|
}
|
|
|
|
/// Command structurally parsed.
|
|
///
|
|
/// Can be converted into `Command` with the `TryInto` trait.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
pub struct IR
|
|
{
|
|
op: String,
|
|
params: Vec<Value>,
|
|
}
|
|
|
|
impl FromStr for IR
|
|
{
|
|
type Err = CommandParseError;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
/// Error when parsing a command into `IR`.
|
|
#[derive(Debug)]
|
|
pub struct CommandParseError(String);
|
|
|
|
impl error::Error for CommandParseError{}
|
|
|
|
impl fmt::Display for CommandParseError
|
|
{
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
{
|
|
write!(f, "failed to parse command from {:?}", self.0)
|
|
}
|
|
}
|