extern crate little;
use std::collections::HashMap;
use std::io::{ Read, Write };
use std::fmt;
use little::*;
use little::interpreter::Interpreter;
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)]
pub enum Value {
Null,
Str(String)
}
impl LittleValue for Value { }
impl IdentifyValue for Value {
fn identify_value(&self) -> Option<Fingerprint> {
None
}
fn hash_value<H: Sha1Hasher>(&self, _hasher: &mut H) -> Result<(), ()> {
Err(())
}
}
impl Default for Value {
fn default() -> Value {
Value::Null
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Value::Null => Ok(()),
Value::Str(ref s) => write!(f, "{}", s),
}
}
}
fn main() {
let join = |args: &[Value]| {
Ok(Value::Str(format!("{} {}", args[0], args[1])))
};
let mut funs = HashMap::<&'static str, &Function<Value>>::new();
funs.insert("join", &join);
let template = Template::empty()
.with_instructions(vec![
Instruction::Push { location: Mem::Const(Constant(0)) },
Instruction::Push { location: Mem::Parameters },
Instruction::Call { call: Call(0), argc: 2, push_result_to_stack: true },
Instruction::Output { location: Mem::StackTop1 },
])
.with_call("join", Call(0))
.with_constant(Constant(0), Value::Str("Hello".into()));
let mut i = Interpreter::new();
let p = i.build("", template, &funs).unwrap();
let mut output = String::new();
p.execute(
Value::Str("World".into())
)
.read_to_string(&mut output)
.unwrap();
println!("{}", output);
}