interp.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. use crate::ast::*;
  2. use rand::Rng;
  3. use std::cell::RefCell;
  4. use std::collections::HashMap;
  5. use std::fmt;
  6. use std::rc::Rc;
  7. macro_rules! bail {
  8. ($fmt:expr) => { return Err(Error { message: format!($fmt), }) };
  9. ($fmt:expr, $($e:expr),*) => { return Err(Error { message: format!($fmt, $($e),*), }) }
  10. }
  11. #[derive(Debug, Clone)]
  12. pub enum Value {
  13. Lit(Literal),
  14. Tup(Vec<Thunk>),
  15. Builtin(&'static BuiltinFunc),
  16. Closure(Closure),
  17. Nil,
  18. }
  19. impl Value {
  20. fn as_num(&self) -> Result<i64, Error> {
  21. match self {
  22. Value::Lit(Literal::Num(n)) => Ok(*n),
  23. _ => self.with_str(|s| bail!("Expected number, got {}", s)),
  24. }
  25. }
  26. fn as_str(&self) -> Result<&str, Error> {
  27. match self {
  28. Value::Lit(Literal::Str(s)) => Ok(s),
  29. _ => self.with_str(|s| bail!("Expected string, got {}", s)),
  30. }
  31. }
  32. fn with_str<U>(&self, f: impl FnOnce(&str) -> U) -> U {
  33. match self {
  34. Value::Nil => f(""),
  35. Value::Lit(Literal::Str(s)) => f(s),
  36. Value::Lit(Literal::Atom(s)) => f(&format!("{:?}", s)),
  37. Value::Lit(Literal::Num(n)) => f(&format!("{}", n)),
  38. Value::Tup(values) => {
  39. let mut buf = String::new();
  40. buf.push('<');
  41. for (i, val) in values.iter().enumerate() {
  42. if i > 0 {
  43. buf.push_str(", ");
  44. }
  45. match val {
  46. Thunk::Value(v) => buf.push_str(&v.to_string()),
  47. Thunk::Expr(..) => buf.push_str("#<unevaluated>"),
  48. Thunk::Builtin(func) => buf.push_str(&format!("#<builtin {}>", func.name)),
  49. }
  50. }
  51. buf.push('>');
  52. f(&buf)
  53. }
  54. Value::Builtin(func) => f(&format!("#<builtin {}>", func.name)),
  55. Value::Closure(_) => f(&format!("#<lambda ...>")),
  56. }
  57. }
  58. }
  59. impl fmt::Display for Value {
  60. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  61. self.with_str(|s| write!(f, "{}", s))
  62. }
  63. }
  64. pub struct BuiltinFunc {
  65. name: &'static str,
  66. callback: &'static dyn Fn(&State, ExprRef, &Env) -> Result<Value, Error>,
  67. }
  68. #[derive(Debug)]
  69. pub struct Error {
  70. message: String,
  71. }
  72. impl From<lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>>
  73. for Error
  74. {
  75. fn from(
  76. err: lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>,
  77. ) -> Error {
  78. Error {
  79. message: format!("{:?}", err),
  80. }
  81. }
  82. }
  83. impl fmt::Display for Error {
  84. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  85. write!(fmt, "{}", self.message)
  86. }
  87. }
  88. impl std::error::Error for Error {}
  89. const BUILTINS: &[BuiltinFunc] = &[
  90. BuiltinFunc {
  91. name: "rep",
  92. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  93. let (rep, expr) = {
  94. let ast = state.ast.borrow();
  95. let args = match &ast[expr] {
  96. Expr::Tup(tup) => tup,
  97. _ => bail!("`rep`: expected tuple"),
  98. };
  99. if args.len() != 2 {
  100. bail!("`rep`: expected two arguments, got {}", args.len())
  101. }
  102. (args[0], args[1])
  103. };
  104. let mut buf = String::new();
  105. let num = state.eval(rep, env)?.as_num()?;
  106. for _ in 0..num {
  107. buf.push_str(&state.eval(expr, env)?.as_str()?.to_string());
  108. }
  109. Ok(Value::Lit(Literal::Str(buf)))
  110. },
  111. },
  112. BuiltinFunc {
  113. name: "length",
  114. callback: &|state: &State, expr: ExprRef, _env: &Env| -> Result<Value, Error> {
  115. let ast = state.ast.borrow();
  116. let args = match &ast[expr] {
  117. Expr::Tup(tup) => tup,
  118. _ => bail!("`length`: expected tuple"),
  119. };
  120. Ok(Value::Lit(Literal::Num(args.len() as i64)))
  121. },
  122. },
  123. BuiltinFunc {
  124. name: "to-upper",
  125. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  126. let s = state.eval(expr, env)?;
  127. Ok(Value::Lit(Literal::Str(s.as_str()?.to_uppercase())))
  128. },
  129. },
  130. BuiltinFunc {
  131. name: "to-lower",
  132. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  133. let s = state.eval(expr, env)?;
  134. Ok(Value::Lit(Literal::Str(s.as_str()?.to_lowercase())))
  135. },
  136. },
  137. ];
  138. impl fmt::Debug for BuiltinFunc {
  139. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  140. writeln!(fmt, "BuiltinFunc {{ name: {:?}, ... }}", self.name)
  141. }
  142. }
  143. #[derive(Debug, Clone)]
  144. pub enum Thunk {
  145. Expr(ExprRef, Env),
  146. Value(Value),
  147. Builtin(&'static BuiltinFunc),
  148. }
  149. type Env = Option<Rc<Scope>>;
  150. #[derive(Debug)]
  151. pub struct Scope {
  152. vars: HashMap<Name, Thunk>,
  153. parent: Env,
  154. }
  155. #[derive(Debug, Clone)]
  156. pub struct Closure {
  157. func: ExprRef,
  158. scope: Env,
  159. }
  160. pub struct State {
  161. ast: RefCell<ASTArena>,
  162. root_scope: RefCell<HashMap<Name, Thunk>>,
  163. rand: RefCell<rand::rngs::ThreadRng>,
  164. parser: crate::grammar::StmtsParser,
  165. }
  166. impl Default for State {
  167. fn default() -> State {
  168. Self::new()
  169. }
  170. }
  171. impl State {
  172. pub fn new() -> State {
  173. let s = State {
  174. root_scope: RefCell::new(HashMap::new()),
  175. rand: RefCell::new(rand::thread_rng()),
  176. parser: crate::grammar::StmtsParser::new(),
  177. ast: RefCell::new(ASTArena::new()),
  178. };
  179. for builtin in BUILTINS {
  180. let sym = s.ast.borrow_mut().add_string(builtin.name);
  181. s.root_scope
  182. .borrow_mut()
  183. .insert(sym, Thunk::Builtin(builtin));
  184. }
  185. s
  186. }
  187. fn lookup(&self, env: &Env, name: Name) -> Result<Thunk, Error> {
  188. if let Some(env) = env {
  189. if let Some(ne) = env.vars.get(&name) {
  190. Ok(ne.clone())
  191. } else {
  192. self.lookup(&env.parent, name)
  193. }
  194. } else {
  195. match self.root_scope.borrow().get(&name) {
  196. None => bail!("no such thing: {:?}", name),
  197. Some(ne) => Ok(ne.clone()),
  198. }
  199. }
  200. }
  201. pub fn run(&self, src: &str) -> Result<(), Error> {
  202. let lexed = crate::lexer::tokens(src);
  203. let stmts = self.parser.parse(&mut self.ast.borrow_mut(), lexed)?;
  204. for stmt in stmts {
  205. self.execute(&stmt)?;
  206. }
  207. Ok(())
  208. }
  209. pub fn run_repl(&self, src: &str) -> Result<(), Error> {
  210. let lexed = crate::lexer::tokens(src);
  211. let stmts = {
  212. let mut ast = self.ast.borrow_mut();
  213. self.parser.parse(&mut ast, lexed)
  214. };
  215. let stmts = match stmts {
  216. Ok(stmts) => stmts,
  217. Err(err) => {
  218. let with_puts = format!("puts {}", src);
  219. let lexed = crate::lexer::tokens(&with_puts);
  220. if let Ok(stmts) = self.parser.parse(&mut self.ast.borrow_mut(), lexed) {
  221. stmts
  222. } else {
  223. return Err(err.into());
  224. }
  225. }
  226. };
  227. for stmt in stmts {
  228. self.execute(&stmt)?;
  229. }
  230. Ok(())
  231. }
  232. pub fn autocomplete(&self, fragment: &str, at_beginning: bool) -> Vec<String> {
  233. let mut possibilities = Vec::new();
  234. for name in self.root_scope.borrow().keys() {
  235. if self.ast.borrow()[*name].starts_with(fragment) {
  236. possibilities.push(self.ast.borrow()[*name].to_string());
  237. }
  238. }
  239. if at_beginning && "puts".starts_with(fragment) {
  240. possibilities.push("puts ".to_owned());
  241. }
  242. possibilities
  243. }
  244. pub fn execute(&self, stmt: &Stmt) -> Result<(), Error> {
  245. match stmt {
  246. Stmt::Puts(expr) => {
  247. let val = self.eval(*expr, &None)?;
  248. let val = self.force(val)?;
  249. println!("{}", val.to_string());
  250. }
  251. Stmt::Fix(name) => {
  252. let (expr, env) = match self.lookup(&None, *name)? {
  253. Thunk::Expr(e, env) => (e, env),
  254. // if it's not an expr, then our work here is done
  255. _ => return Ok(()),
  256. };
  257. let val = self.eval(expr, &env)?;
  258. self.root_scope
  259. .borrow_mut()
  260. .insert(*name, Thunk::Value(val));
  261. }
  262. Stmt::Assn(fixed, name, expr) => {
  263. if *fixed {
  264. let val = self.eval(*expr, &None)?;
  265. self.root_scope
  266. .borrow_mut()
  267. .insert(*name, Thunk::Value(val));
  268. } else {
  269. self.root_scope
  270. .borrow_mut()
  271. .insert(*name, Thunk::Expr(*expr, None));
  272. }
  273. }
  274. Stmt::LitAssn(fixed, name, strs) => {
  275. if *fixed {
  276. let choice = &strs[self.rand.borrow_mut().gen_range(0..strs.len())];
  277. self.root_scope.borrow_mut().insert(
  278. *name,
  279. Thunk::Value(Value::Lit(Literal::Str(choice.clone()))),
  280. );
  281. return Ok(());
  282. }
  283. let choices = strs
  284. .iter()
  285. .map(|s| Choice {
  286. weight: None,
  287. value: self
  288. .ast
  289. .borrow_mut()
  290. .add_expr(Expr::Lit(Literal::Str(s.clone()))),
  291. })
  292. .collect();
  293. let choices = self.ast.borrow_mut().add_expr(Expr::Chc(choices));
  294. self.root_scope
  295. .borrow_mut()
  296. .insert(*name, Thunk::Expr(choices, None));
  297. }
  298. }
  299. Ok(())
  300. }
  301. fn force(&self, val: Value) -> Result<Value, Error> {
  302. match val {
  303. Value::Tup(values) => Ok(Value::Tup(
  304. values
  305. .into_iter()
  306. .map(|t| {
  307. if let Thunk::Expr(e, env) = t {
  308. Ok(Thunk::Value(self.eval(e, &env)?))
  309. } else {
  310. Ok(t)
  311. }
  312. })
  313. .collect::<Result<Vec<Thunk>, Error>>()?,
  314. )),
  315. _ => Ok(val),
  316. }
  317. }
  318. fn eval(&self, expr_ref: ExprRef, env: &Env) -> Result<Value, Error> {
  319. let expr = &self.ast.borrow()[expr_ref];
  320. match expr {
  321. Expr::Lit(l) => Ok(Value::Lit(l.clone())),
  322. Expr::Nil => Ok(Value::Nil),
  323. Expr::Var(v) => {
  324. let (e, env) = match self.lookup(env, *v)? {
  325. Thunk::Expr(e, env) => (e, env),
  326. Thunk::Value(v) => return Ok(v.clone()),
  327. Thunk::Builtin(b) => return Ok(Value::Builtin(b)),
  328. };
  329. self.eval(e, &env)
  330. }
  331. Expr::Cat(cat) => {
  332. if cat.len() == 1 {
  333. self.eval(cat[0], env)
  334. } else {
  335. let mut buf = String::new();
  336. for expr in cat {
  337. let val = self.eval(*expr, env)?;
  338. buf.push_str(&val.to_string());
  339. }
  340. Ok(Value::Lit(Literal::Str(buf)))
  341. }
  342. }
  343. Expr::Chc(choices) => {
  344. if choices.len() == 1 {
  345. self.eval(choices[0].value, env)
  346. } else {
  347. self.choose(&choices, env)
  348. }
  349. }
  350. Expr::Tup(values) => Ok(Value::Tup(
  351. values
  352. .iter()
  353. .map(|v| Thunk::Expr(*v, env.clone()))
  354. .collect::<Vec<Thunk>>(),
  355. )),
  356. Expr::Range(from, to) => {
  357. let from = self.eval(*from, env)?.as_num()?;
  358. let to = self.eval(*to, env)?.as_num()?;
  359. Ok(Value::Lit(Literal::Num(
  360. self.rand.borrow_mut().gen_range(from..=to),
  361. )))
  362. }
  363. Expr::Fun(_) => Ok(Value::Closure(Closure {
  364. func: expr_ref,
  365. scope: env.clone(),
  366. })),
  367. Expr::Ap(func, val) => {
  368. let closure = match self.eval(*func, env)? {
  369. Value::Closure(c) => c,
  370. Value::Builtin(builtin) => return (builtin.callback)(self, *val, env),
  371. _ => bail!("Bad function: {:?}", func),
  372. };
  373. let ast = self.ast.borrow();
  374. let cases = match &ast[closure.func] {
  375. Expr::Fun(cases) => cases,
  376. _ => bail!("INVARIANT FAILED"),
  377. };
  378. // this is mutable because we only want to force
  379. // it once per pattern.
  380. let mut scrut = Thunk::Expr(*val, env.clone());
  381. for c in cases {
  382. let mut bindings = Vec::new();
  383. if !self.match_pat(&c.pat, &mut scrut, &mut bindings)? {
  384. continue;
  385. }
  386. let mut new_scope = HashMap::new();
  387. for (name, binding) in bindings {
  388. new_scope.insert(name, binding);
  389. }
  390. let new_scope = Rc::new(Scope {
  391. vars: new_scope,
  392. parent: closure.scope,
  393. });
  394. return self.eval(c.expr, &Some(new_scope));
  395. }
  396. bail!("No pattern in {:?} matched {:?}", cases, scrut);
  397. }
  398. _ => bail!("unimplemented: {:?}", expr),
  399. }
  400. }
  401. fn match_pat(
  402. &self,
  403. pat: &Pat,
  404. scrut: &mut Thunk,
  405. bindings: &mut Vec<(Name, Thunk)>,
  406. ) -> Result<bool, Error> {
  407. if let Pat::Var(v) = pat {
  408. bindings.push((*v, scrut.clone()));
  409. return Ok(true);
  410. }
  411. // if it's not just a variable, then we'll need to make sure
  412. // we've evaluated `scrut` at least one level from here
  413. if let Thunk::Expr(e, env) = scrut {
  414. *scrut = Thunk::Value(self.eval(*e, &env)?)
  415. };
  416. // now we can match deeper patterns, at least a little
  417. match pat {
  418. Pat::Lit(lhs) => {
  419. if let Thunk::Value(Value::Lit(rhs)) = scrut {
  420. Ok(lhs == rhs)
  421. } else {
  422. Ok(false)
  423. }
  424. }
  425. Pat::Tup(pats) => {
  426. if let Thunk::Value(Value::Tup(thunks)) = scrut {
  427. if pats.len() != thunks.len() {
  428. return Ok(false);
  429. }
  430. for (p, t) in pats.iter().zip(thunks) {
  431. if !self.match_pat(p, t, bindings)? {
  432. return Ok(false);
  433. }
  434. }
  435. Ok(true)
  436. } else {
  437. Ok(false)
  438. }
  439. }
  440. _ => Ok(false),
  441. }
  442. }
  443. fn choose(&self, choices: &[Choice], env: &Env) -> Result<Value, Error> {
  444. let max = choices.iter().map(Choice::weight).sum();
  445. let mut choice = self.rand.borrow_mut().gen_range(0..max);
  446. for ch in choices {
  447. if choice < ch.weight() {
  448. return self.eval(ch.value, env);
  449. }
  450. choice -= ch.weight();
  451. }
  452. bail!("unreachable")
  453. }
  454. }