Define half-working

This commit is contained in:
greg 2017-12-07 20:28:09 -08:00
parent afd2b018f4
commit 73612d1465

View File

@ -3,8 +3,11 @@ use schala_lib::{ProgrammingLanguageInterface, EvalOptions, ReplOutput};
use std::iter::Peekable; use std::iter::Peekable;
use std::vec::IntoIter; use std::vec::IntoIter;
use std::str::Chars; use std::str::Chars;
use std::collections::HashMap;
pub struct EvaluatorState { } pub struct EvaluatorState {
vars: HashMap<String, Sexp>
}
pub struct Rukka { pub struct Rukka {
state: EvaluatorState state: EvaluatorState
@ -45,7 +48,11 @@ impl ProgrammingLanguageInterface for Rukka {
} }
impl EvaluatorState { impl EvaluatorState {
fn new() -> EvaluatorState { EvaluatorState { } } fn new() -> EvaluatorState {
EvaluatorState {
vars: HashMap::new(),
}
}
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> { fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
use self::Sexp::*; use self::Sexp::*;
Ok(match expr { Ok(match expr {
@ -87,7 +94,14 @@ impl EvaluatorState {
Cons(_, _) => False, Cons(_, _) => False,
_ => True, _ => True,
}, },
"define" => unimplemented!(), "define" => match operands {
Cons(box SymbolAtom(sym), box Cons(box expr, box Nil)) => {
let evaluated = self.eval(expr)?;
self.vars.insert(sym, evaluated);
Nil
},
_ => return Err(format!("Bad assignment")),
}
"lambda" => unimplemented!(), "lambda" => unimplemented!(),
"cond" => unimplemented!(), "cond" => unimplemented!(),
_ => unimplemented!(), _ => unimplemented!(),