This repository has been archived on 2024-07-15. You can view files and clone it, but cannot push or open issues or pull requests.
holy_lisp_archive/src/executor.rs
Apache 32ba3f38c3
Functionality laid out
Todo: turn tokens into opcodes!
2024-06-10 20:52:15 -05:00

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);
}
}
}
}
}