47 lines
855 B
Rust
47 lines
855 B
Rust
use std::collections::HashMap;
|
|
|
|
use crate::{OpCode, LispValue};
|
|
|
|
pub struct LispState {
|
|
table: HashMap<String, LispValue>,
|
|
}
|
|
|
|
impl LispState {
|
|
pub fn new() -> LispState {
|
|
let mut table = HashMap::new();
|
|
|
|
table.insert(String::from("print"), LispValue::RustFunction(String::from("print"), |x| {
|
|
let mut strings = Vec::new();
|
|
for val in x {
|
|
strings.push(val.to_string());
|
|
}
|
|
|
|
let str = strings.join(" ");
|
|
println!("{}", str);
|
|
|
|
LispValue::Nil
|
|
}));
|
|
|
|
LispState {
|
|
table
|
|
}
|
|
}
|
|
|
|
pub fn execute(&self, instructions: Vec<OpCode>) {
|
|
for op in instructions {
|
|
match op {
|
|
OpCode::Call(func, args) => {
|
|
let f = self.table.get(&func).unwrap();
|
|
if let LispValue::RustFunction(_, f) = f {
|
|
f(args);
|
|
} else {
|
|
todo!();
|
|
}
|
|
},
|
|
OpCode::Eval(ins) => {
|
|
self.execute(ins);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|