interp.rs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. use crate::ast::*;
  2. use crate::rand::*;
  3. use anyhow::{anyhow, bail, Error};
  4. use std::cell::RefCell;
  5. use std::collections::HashMap;
  6. use std::fmt;
  7. use std::io;
  8. use std::rc::Rc;
  9. /// A `Value` is a representation of the resut of evaluation. Note
  10. /// that a `Value` is a representation of something in _weak head
  11. /// normal form_: i.e. for compound expressions (right now just
  12. /// tuples) it might contain other values but it might contain
  13. /// unevaluated expressions as well.
  14. #[derive(Debug, Clone)]
  15. pub enum Value {
  16. Lit(Literal),
  17. Tup(Vec<Thunk>),
  18. Builtin(&'static BuiltinFunc),
  19. Closure(Closure),
  20. Nil,
  21. }
  22. impl fmt::Display for Value {
  23. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  24. self.with_str(|s| write!(f, "{}", s))
  25. }
  26. }
  27. impl Value {
  28. /// Convert this value to a Rust integer, failing otherwise
  29. fn as_num(&self) -> Result<i64, Error> {
  30. match self {
  31. Value::Lit(Literal::Num(n)) => Ok(*n),
  32. _ => self.with_str(|s| bail!("Expected number, got {}", s)),
  33. }
  34. }
  35. /// Convert this value to a Rust string, failing otherwise
  36. fn as_str(&self) -> Result<&str, Error> {
  37. match self {
  38. Value::Lit(Literal::Str(s)) => Ok(s),
  39. _ => self.with_str(|s| bail!("Expected string, got {}", s)),
  40. }
  41. }
  42. /// Convert this value to a Rust slice, failing otherwise
  43. fn as_tup(&self) -> Result<&[Thunk], Error> {
  44. match self {
  45. Value::Tup(vals) => Ok(vals),
  46. _ => self.with_str(|s| bail!("Expected tuple, got {}", s)),
  47. }
  48. }
  49. /// Convert this value to a closure, failing otherwise
  50. fn as_closure(&self) -> Result<&Closure, Error> {
  51. match self {
  52. Value::Closure(closure) => Ok(closure),
  53. _ => self.with_str(|s| bail!("Expected tuple, got {}", s)),
  54. }
  55. }
  56. /// Call the provided function with the string representation of
  57. /// this value. Note that this _will not force the value_ if it's
  58. /// not completely forced already: indeed, this can't, since it
  59. /// doesn't have access to the `State`. Unevaluated fragments of
  60. /// the value will be printed as `#<unevaluated>`.
  61. fn with_str<U>(&self, f: impl FnOnce(&str) -> U) -> U {
  62. match self {
  63. Value::Nil => f(""),
  64. Value::Lit(Literal::Str(s)) => f(s),
  65. Value::Lit(Literal::Atom(s)) => f(&format!("{:?}", s)),
  66. Value::Lit(Literal::Num(n)) => f(&format!("{}", n)),
  67. Value::Tup(values) => {
  68. let mut buf = String::new();
  69. buf.push('<');
  70. for (i, val) in values.iter().enumerate() {
  71. if i > 0 {
  72. buf.push_str(", ");
  73. }
  74. match val {
  75. Thunk::Value(v) => buf.push_str(&v.to_string()),
  76. Thunk::Expr(..) => buf.push_str("#<unevaluated>"),
  77. Thunk::Builtin(func) => buf.push_str(&format!("#<builtin {}>", func.name)),
  78. }
  79. }
  80. buf.push('>');
  81. f(&buf)
  82. }
  83. Value::Builtin(func) => f(&format!("#<builtin {}>", func.name)),
  84. Value::Closure(_) => f("#<lambda ...>"),
  85. }
  86. }
  87. }
  88. /// A representation of a builtin function implemented in Rust. This
  89. /// will be inserted into the global scope under the name provided as
  90. /// `name`.
  91. pub struct BuiltinFunc {
  92. /// The name of the builtin: this is used in error messages, in
  93. /// printing the value (e.g. in the case of `puts some-builtin`),
  94. /// and as the Matzo identifier used for this function.
  95. name: &'static str,
  96. /// The callback here is the Rust implementation of the function,
  97. /// where the provided `ExprRef` is the argument to the function.
  98. callback: &'static dyn Fn(&State, ExprRef, &Env) -> Result<Value, Error>,
  99. }
  100. impl fmt::Debug for BuiltinFunc {
  101. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  102. writeln!(fmt, "BuiltinFunc {{ name: {:?}, ... }}", self.name)
  103. }
  104. }
  105. /// The list of builtins provided at startup.
  106. ///
  107. /// TODO: move this to a separate file and clean it up
  108. const BUILTINS: &[BuiltinFunc] = &[
  109. BuiltinFunc {
  110. name: "rep",
  111. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  112. let (rep, expr) = {
  113. let ast = state.ast.borrow();
  114. let args = match &ast[expr] {
  115. Expr::Tup(tup) => tup,
  116. _ => bail!("`rep`: expected tuple"),
  117. };
  118. if args.len() != 2 {
  119. bail!("`rep`: expected two arguments, got {}", args.len())
  120. }
  121. (args[0], args[1])
  122. };
  123. let mut buf = String::new();
  124. let num = state.eval(rep, env)?.as_num()?;
  125. for _ in 0..num {
  126. buf.push_str(&state.eval(expr, env)?.as_str()?.to_string());
  127. }
  128. Ok(Value::Lit(Literal::Str(buf)))
  129. },
  130. },
  131. BuiltinFunc {
  132. name: "length",
  133. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  134. let args = match state.eval(expr, env)? {
  135. Value::Tup(tup) => tup,
  136. _ => bail!("`length`: expected tuple"),
  137. };
  138. Ok(Value::Lit(Literal::Num(args.len() as i64)))
  139. },
  140. },
  141. BuiltinFunc {
  142. name: "to-upper",
  143. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  144. let s = state.eval(expr, env)?;
  145. Ok(Value::Lit(Literal::Str(s.as_str()?.to_uppercase())))
  146. },
  147. },
  148. BuiltinFunc {
  149. name: "capitalize",
  150. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  151. let s = state.eval(expr, env)?;
  152. Ok(Value::Lit(Literal::Str(titlecase::titlecase(s.as_str()?))))
  153. },
  154. },
  155. BuiltinFunc {
  156. name: "to-lower",
  157. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  158. let s = state.eval(expr, env)?;
  159. Ok(Value::Lit(Literal::Str(s.as_str()?.to_lowercase())))
  160. },
  161. },
  162. BuiltinFunc {
  163. name: "concat",
  164. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  165. let val = state.eval(expr, env)?;
  166. let tup = val.as_tup()?;
  167. let mut contents = Vec::new();
  168. for elem in tup {
  169. for th in state.hnf(elem)?.as_tup()? {
  170. contents.push(th.clone());
  171. }
  172. }
  173. Ok(Value::Tup(contents))
  174. },
  175. },
  176. BuiltinFunc {
  177. name: "tuple-fold",
  178. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  179. let val = state.eval(expr, env)?;
  180. let args = val.as_tup()?;
  181. if let [func, init, tup] = args {
  182. let func = state.hnf(func)?;
  183. let tup = state.hnf(tup)?;
  184. let mut result = init.clone();
  185. for t in tup.as_tup()? {
  186. let partial = state.eval_closure(func.as_closure()?, result)?;
  187. result = Thunk::Value(state.eval_closure(partial.as_closure()?, t.clone())?);
  188. }
  189. state.hnf(&result)
  190. } else {
  191. bail!("`tuple-fold`: expected 3 arguments, got {}", args.len());
  192. }
  193. },
  194. },
  195. ];
  196. /// The name `Thunk` is a bit of a misnomer here: this is
  197. /// _potentially_ a `Thunk`, but represents anything that can be
  198. /// stored in a variable: it might be an unevaluated expression (along
  199. /// with the environment where it should be evaluated), or it might be
  200. /// a partially- or fully-forced value, or it might be a builtin
  201. /// function.
  202. #[derive(Debug, Clone)]
  203. pub enum Thunk {
  204. Expr(ExprRef, Env),
  205. Value(Value),
  206. Builtin(&'static BuiltinFunc),
  207. }
  208. /// An environment is either `None` (i.e. in the root scope) or `Some`
  209. /// of some reference-counted scope (since those scopes might be
  210. /// shared in several places, e.g. as pointers in thunks or closures).
  211. type Env = Option<Rc<Scope>>;
  212. /// A `Scope` represents a _non-root_ scope (since the root scope is
  213. /// treated in a special way) and contains a map from variables to
  214. /// `Thunk`s, along with a parent pointer.
  215. #[derive(Debug)]
  216. pub struct Scope {
  217. vars: HashMap<Name, Thunk>,
  218. parent: Env,
  219. }
  220. /// A `Closure` is a pointer to the expression that represents a
  221. /// function implementation along with the scope in which it was
  222. /// defined.
  223. ///
  224. /// IMPORTANT INVARIANT: the `func` here should be an `ExprRef` which
  225. /// references a `Func`. The reason we don't copy the `Func` in is
  226. /// because, well, that'd be copying, and we can bypass that, but we
  227. /// have to maintain that invariant explicitly, otherwise we'll panic.
  228. #[derive(Debug, Clone)]
  229. pub struct Closure {
  230. func: ExprRef,
  231. scope: Env,
  232. }
  233. /// A `State` contains all the interpreter state needed to run a
  234. /// `Matzo` program.
  235. pub struct State {
  236. /// An `ASTArena` that contains all the packed information that
  237. /// results from parsing a program.
  238. ast: RefCell<ASTArena>,
  239. /// The root scope of the program, which contains all the
  240. /// top-level definitions and builtins.
  241. root_scope: RefCell<HashMap<Name, Thunk>>,
  242. /// The thread-local RNG.
  243. rand: RefCell<Box<dyn MatzoRand>>,
  244. /// The instantiated parser used to parse Matzo programs
  245. parser: crate::grammar::StmtsParser,
  246. }
  247. impl Default for State {
  248. fn default() -> State {
  249. Self::new()
  250. }
  251. }
  252. impl State {
  253. /// This initializes a new `State` and adds all the builtin
  254. /// functions to the root scope
  255. pub fn new() -> State {
  256. let s = State {
  257. root_scope: RefCell::new(HashMap::new()),
  258. rand: RefCell::new(Box::new(DefaultRNG::new())),
  259. parser: crate::grammar::StmtsParser::new(),
  260. ast: RefCell::new(ASTArena::new()),
  261. };
  262. for builtin in BUILTINS {
  263. let sym = s.ast.borrow_mut().add_string(builtin.name);
  264. s.root_scope
  265. .borrow_mut()
  266. .insert(sym, Thunk::Builtin(builtin));
  267. }
  268. s
  269. }
  270. /// This initializes a new `State` and adds all the builtin
  271. /// functions to the root scope
  272. pub fn new_from_seed(seed: u64) -> State {
  273. let s = State {
  274. root_scope: RefCell::new(HashMap::new()),
  275. rand: RefCell::new(Box::new(SeededRNG::from_seed(seed))),
  276. parser: crate::grammar::StmtsParser::new(),
  277. ast: RefCell::new(ASTArena::new()),
  278. };
  279. for builtin in BUILTINS {
  280. let sym = s.ast.borrow_mut().add_string(builtin.name);
  281. s.root_scope
  282. .borrow_mut()
  283. .insert(sym, Thunk::Builtin(builtin));
  284. }
  285. s
  286. }
  287. /// Get the underlying AST. (This is mostly useful for testing
  288. /// purposes, where we don't want to have a function do the
  289. /// parsing and evaluating for us at the same time.)
  290. pub fn get_ast(&self) -> &RefCell<ASTArena> {
  291. &self.ast
  292. }
  293. /// Look up a `Name` in the provided `Env`. This will result in
  294. /// either a `Thunk` (i.e. the named value) or an error that
  295. /// indicates the missing name.
  296. fn lookup(&self, env: &Env, name: Name) -> Result<Thunk, Error> {
  297. if let Some(env) = env {
  298. if let Some(ne) = env.vars.get(&name) {
  299. Ok(ne.clone())
  300. } else {
  301. self.lookup(&env.parent, name)
  302. }
  303. } else {
  304. match self.root_scope.borrow().get(&name) {
  305. None => bail!("no such thing: {}", &self.ast.borrow()[name]),
  306. Some(ne) => Ok(ne.clone()),
  307. }
  308. }
  309. }
  310. /// Evaluate this string as a standalone program, writing the
  311. /// results to stdout.
  312. pub fn run(&self, src: &str) -> Result<(), Error> {
  313. let lexed = crate::lexer::tokens(src);
  314. let stmts = self
  315. .parser
  316. .parse(&mut self.ast.borrow_mut(), lexed)
  317. .map_err(|err| anyhow!("Got {:?}", err))?;
  318. let mut stdout = io::stdout();
  319. for stmt in stmts {
  320. self.execute(&stmt, &mut stdout)?;
  321. }
  322. Ok(())
  323. }
  324. /// Evaluate this string as a standalone program, writing the
  325. /// results to the provided writer.
  326. pub fn run_with_writer(&self, src: &str, w: &mut impl std::io::Write) -> Result<(), Error> {
  327. let lexed = crate::lexer::tokens(src);
  328. let stmts = self
  329. .parser
  330. .parse(&mut self.ast.borrow_mut(), lexed)
  331. .map_err(|err| anyhow!("Got {:?}", err))?;
  332. for stmt in stmts {
  333. self.execute(&stmt, &mut *w)?;
  334. }
  335. Ok(())
  336. }
  337. /// Evaluate this string as a fragment in a REPL, writing the
  338. /// results to stdout. One way this differs from the standalone
  339. /// program is that it actually tries parsing twice: first it
  340. /// tries parsing the fragment normally, and then if that doesn't
  341. /// work it tries adding a `puts` ahead of it: this is hacky, but
  342. /// it allows the REPL to respond by printing values when someone
  343. /// simply types an expression.
  344. pub fn run_repl(&self, src: &str) -> Result<(), Error> {
  345. let lexed = crate::lexer::tokens(src);
  346. let stmts = {
  347. let mut ast = self.ast.borrow_mut();
  348. self.parser.parse(&mut ast, lexed)
  349. };
  350. let stmts = match stmts {
  351. Ok(stmts) => stmts,
  352. Err(err) => {
  353. let with_puts = format!("puts {}", src);
  354. let lexed = crate::lexer::tokens(&with_puts);
  355. if let Ok(stmts) = self.parser.parse(&mut self.ast.borrow_mut(), lexed) {
  356. stmts
  357. } else {
  358. bail!("{:?}", err);
  359. }
  360. }
  361. };
  362. for stmt in stmts {
  363. self.execute(&stmt, io::stdout())?;
  364. }
  365. Ok(())
  366. }
  367. /// Autocomplete this name. This doesn't make use of any
  368. /// contextual information (e.g. like function arguments or
  369. /// `let`-bound names) but instead tries to complete based
  370. /// entirely on the things in root scope.
  371. pub fn autocomplete(&self, fragment: &str, at_beginning: bool) -> Vec<String> {
  372. let mut possibilities = Vec::new();
  373. for name in self.root_scope.borrow().keys() {
  374. if self.ast.borrow()[*name].starts_with(fragment) {
  375. possibilities.push(self.ast.borrow()[*name].to_string());
  376. }
  377. }
  378. if at_beginning && "puts".starts_with(fragment) {
  379. possibilities.push("puts ".to_owned());
  380. }
  381. possibilities
  382. }
  383. /// Execute this statement, writing any output to the provided
  384. /// output writer. Right now, this will always start in root
  385. /// scope: there are no statements within functions.
  386. pub fn execute(&self, stmt: &Stmt, mut output: impl io::Write) -> Result<(), Error> {
  387. match stmt {
  388. // Evaluate the provided expression _all the way_
  389. // (i.e. recurisvely, not to WHNF) and write its
  390. // representation to the output.
  391. Stmt::Puts(expr) => {
  392. let val = self.eval(*expr, &None)?;
  393. let val = self.force(val)?;
  394. writeln!(output, "{}", val.to_string()).unwrap();
  395. }
  396. // Look up the provided name, and if it's not already
  397. // forced completely, then force it completely and
  398. // re-insert this name with the forced version.
  399. Stmt::Fix(name) => {
  400. let val = match self.lookup(&None, *name)? {
  401. Thunk::Expr(e, env) => self.eval(e, &env)?,
  402. // we need to handle this case in case it's
  403. // already in WHNF (e.g. a tuple whose elements
  404. // are not yet values)
  405. Thunk::Value(v) => v,
  406. // if it's not an expr or val, then our work here
  407. // is done
  408. _ => return Ok(()),
  409. };
  410. let val = self.force(val)?;
  411. self.root_scope
  412. .borrow_mut()
  413. .insert(*name, Thunk::Value(val));
  414. }
  415. // assign a given expression to a name, forcing it to a
  416. // value if the assignment is `fixed`.
  417. Stmt::Assn(fixed, name, expr) => {
  418. let thunk = if *fixed {
  419. let val = self.eval(*expr, &None)?;
  420. let val = self.force(val)?;
  421. Thunk::Value(val)
  422. } else {
  423. Thunk::Expr(*expr, None)
  424. };
  425. self.root_scope.borrow_mut().insert(*name, thunk);
  426. }
  427. // assign a simple disjunction of strings to a name,
  428. // forcing it to a value if the assignment is `fixed`.
  429. Stmt::LitAssn(fixed, name, strs) => {
  430. if *fixed {
  431. let choice = &strs[self.rand.borrow_mut().gen_range_usize(0, strs.len())];
  432. self.root_scope.borrow_mut().insert(
  433. *name,
  434. Thunk::Value(Value::Lit(Literal::Str(choice.clone()))),
  435. );
  436. return Ok(());
  437. }
  438. let choices = strs
  439. .iter()
  440. .map(|s| Choice {
  441. weight: None,
  442. value: self
  443. .ast
  444. .borrow_mut()
  445. .add_expr(Expr::Lit(Literal::Str(s.clone()))),
  446. })
  447. .collect();
  448. let choices = self.ast.borrow_mut().add_expr(Expr::Chc(choices));
  449. self.root_scope
  450. .borrow_mut()
  451. .insert(*name, Thunk::Expr(choices, None));
  452. }
  453. }
  454. Ok(())
  455. }
  456. /// Given a value, force it recursively.
  457. fn force(&self, val: Value) -> Result<Value, Error> {
  458. match val {
  459. Value::Tup(values) => Ok(Value::Tup(
  460. values
  461. .into_iter()
  462. .map(|t| {
  463. let v = self.hnf(&t)?;
  464. let v = self.force(v)?;
  465. Ok(Thunk::Value(v))
  466. })
  467. .collect::<Result<Vec<Thunk>, Error>>()?,
  468. )),
  469. _ => Ok(val),
  470. }
  471. }
  472. /// Given a thunk, force it to WHNF.
  473. fn hnf(&self, thunk: &Thunk) -> Result<Value, Error> {
  474. match thunk {
  475. Thunk::Expr(expr, env) => self.eval(*expr, env),
  476. Thunk::Value(val) => Ok(val.clone()),
  477. Thunk::Builtin(b) => Ok(Value::Builtin(b)),
  478. }
  479. }
  480. /// Given an `ExprRef` and an environment, fetch that expression
  481. /// and then evalute it in that environment
  482. fn eval(&self, expr_ref: ExprRef, env: &Env) -> Result<Value, Error> {
  483. let expr = &self.ast.borrow()[expr_ref];
  484. match expr {
  485. // literals should be mostly cheap-ish to copy, so a
  486. // literal evaluates to a `Value` that's a copy of the
  487. // literal
  488. Expr::Lit(l) => Ok(Value::Lit(l.clone())),
  489. // `Nil` evalutes to `Nil`
  490. Expr::Nil => Ok(Value::Nil),
  491. // When a variable is used, we should look it up and
  492. // evaluate it to WHNF
  493. Expr::Var(v) => self.hnf(&self.lookup(env, *v)?),
  494. // for a catenation, we should fully evaluate all the
  495. // expressions, convert them to strings, and concatenate
  496. // them all.
  497. Expr::Cat(cat) => {
  498. // if we ever have a catentation of one, then don't
  499. // bother with the string: just evaluate the
  500. // expression.
  501. if cat.len() == 1 {
  502. self.eval(cat[0], env)
  503. } else {
  504. let mut buf = String::new();
  505. for expr in cat {
  506. let val = self.eval(*expr, env)?;
  507. let val = self.force(val)?;
  508. buf.push_str(&val.to_string());
  509. }
  510. Ok(Value::Lit(Literal::Str(buf)))
  511. }
  512. }
  513. // for choices, we should choose one with the appropriate
  514. // frequency and then evaluate it
  515. Expr::Chc(choices) => {
  516. // if we ever have only one choice, well, choose it:
  517. if choices.len() == 1 {
  518. self.eval(choices[0].value, env)
  519. } else {
  520. self.choose(choices, env)
  521. }
  522. }
  523. // for a tuple, we return a tuple of thunks to begin with,
  524. // to make sure that the values contained within are
  525. // appropriately lazy
  526. Expr::Tup(values) => Ok(Value::Tup(
  527. values
  528. .iter()
  529. .map(|v| Thunk::Expr(*v, env.clone()))
  530. .collect::<Vec<Thunk>>(),
  531. )),
  532. // for a range, choose randomly between the start and end
  533. // expressions
  534. Expr::Range(from, to) => {
  535. let from = self.eval(*from, env)?.as_num()?;
  536. let to = self.eval(*to, env)?.as_num()?;
  537. Ok(Value::Lit(Literal::Num(
  538. self.rand.borrow_mut().gen_range_i64(from, to+1),
  539. )))
  540. }
  541. // for a function, return a closure (i.e. the function
  542. // body paired with the current environment)
  543. Expr::Fun(_) => Ok(Value::Closure(Closure {
  544. func: expr_ref,
  545. scope: env.clone(),
  546. })),
  547. // for application, make sure the thing we're applying is
  548. // either a closure (i.e. the result of evaluating a
  549. // function) or a builtin, and then handle it
  550. // appropriately
  551. Expr::Ap(func, val) => match self.eval(*func, env)? {
  552. Value::Closure(c) => {
  553. let scrut = Thunk::Expr(*val, env.clone());
  554. self.eval_closure(&c, scrut)
  555. }
  556. Value::Builtin(builtin) => (builtin.callback)(self, *val, env),
  557. _ => bail!("Bad function: {:?}", func),
  558. },
  559. // for a let-expression, create a new scope, add the new
  560. // name to it (optionally forcing it if `fixed`) and then
  561. // evaluate the body within that scope.
  562. Expr::Let(fixed, name, val, body) => {
  563. let mut new_scope = HashMap::new();
  564. if *fixed {
  565. let val = self.eval(*val, env)?;
  566. let val = self.force(val)?;
  567. new_scope.insert(*name, Thunk::Value(val));
  568. } else {
  569. new_scope.insert(*name, Thunk::Expr(*val, env.clone()));
  570. };
  571. let new_scope = Rc::new(Scope {
  572. vars: new_scope,
  573. parent: env.clone(),
  574. });
  575. self.eval(*body, &Some(new_scope))
  576. }
  577. Expr::Case(scrut, _) => {
  578. let closure = Closure {
  579. func: expr_ref,
  580. scope: env.clone(),
  581. };
  582. self.eval_closure(&closure, Thunk::Expr(*scrut, env.clone()))
  583. }
  584. }
  585. }
  586. /// Evaluate a closure as applied to a given argument.
  587. ///
  588. /// There's a very subtle thing going on here: when we apply a
  589. /// closure to an expression, we should evaluate that expression
  590. /// _as far as we need to and no further_. That's why the `scrut`
  591. /// argument here is mutable: to start with, it'll be a
  592. /// `Thunk::Expr`. If the function uses a wildcard or variable
  593. /// match, it'll stay that way, but if we start matching against
  594. /// it, we'll evaluate it at least to WHNF to find out whether it
  595. /// maches, and _sometimes_ a little further.
  596. ///
  597. /// Here's where it gets tricky: we need to maintain that
  598. /// evaluation between branches so that we don't get Schrödinger's
  599. /// patterns. An example where that might work poorly if we're not
  600. /// careful is here:
  601. ///
  602. /// ```ignore
  603. /// {Foo => "1"; Foo => "2"; _ => "..."}.(Foo | Bar)
  604. /// ```
  605. ///
  606. /// It should be impossible to get `"2"` in this case. That means
  607. /// that we need to force the argument _and keep branching against
  608. /// the forced argument_. But we also want the following to still
  609. /// contain non-determinism:
  610. ///
  611. /// ```ignore
  612. /// {<Foo, x> => x x "!"; <Bar, x> => x x "?"}.<Foo | Bar, "a" | "b">
  613. /// ```
  614. ///
  615. /// The above program should print one of "aa!", "bb!", "aa?", or
  616. /// "bb?". That means it needs to
  617. /// 1. force the argument first to `<_, _>`, to make sure it's a
  618. /// two-element tuple
  619. /// 2. force the first element of the tuple to `Foo` or `Bar` to
  620. /// discriminate on it, but
  621. /// 3. _not_ force the second element of the tuple, because we
  622. /// want it to vary from invocation to invocation.
  623. ///
  624. /// So the way we do this is, we start by representing the
  625. /// argument as a `Thunk::Expr`, but allow the pattern-matching
  626. /// function to mutably replace it with progressively more
  627. /// evaluated versions of the same expression, and then that's the
  628. /// thing we put into scope in the body of the function.
  629. fn eval_closure(&self, closure: &Closure, mut scrut: Thunk) -> Result<Value, Error> {
  630. let ast = self.ast.borrow();
  631. let cases = match &ast[closure.func] {
  632. Expr::Fun(cases) => cases,
  633. Expr::Case(_, cases) => cases,
  634. // see the note attached to the definition of `Closure`
  635. _ => bail!("INVARIANT FAILED"),
  636. };
  637. // for each case
  638. for c in cases {
  639. // build a set of potential bindings, which `match_pat`
  640. // will update if it finds matching variables
  641. let mut bindings = Vec::new();
  642. if !self.match_pat(&c.pat, &mut scrut, &mut bindings)? {
  643. // if we didn't match, we don't care about any
  644. // bindings we've found: simply skip it
  645. continue;
  646. }
  647. // build a new scope from the bindings discovered
  648. let mut new_scope = HashMap::new();
  649. for (name, binding) in bindings {
  650. new_scope.insert(name, binding);
  651. }
  652. let new_scope = Rc::new(Scope {
  653. vars: new_scope,
  654. parent: closure.scope.clone(),
  655. });
  656. // and now evaluate the chosen branch body in the
  657. // newly-created scope
  658. return self.eval(c.expr, &Some(new_scope));
  659. }
  660. // we couldn't find a matching pattern, so throw an error
  661. bail!("No pattern in {:?} matched {:?}", cases, scrut);
  662. }
  663. /// attempt to match the thunk `scrut` against the pattern
  664. /// `pat`. If it matched, then it'll return `Ok(true)`, if it
  665. /// didn't, it'll return `Ok(false)`, and (because it might need
  666. /// to do incremental evaluation to check if the pattern matches)
  667. /// it'll return an error if forcing parts of the expression
  668. /// returns an error. The `bindings` vector will be filled with
  669. /// name-thunk pairs based on the pattern: if this returns
  670. /// `Ok(true)`, then those are the thunks that should be bound to
  671. /// names in the context, but otherwise those bindings can be
  672. /// safely ignored.
  673. fn match_pat(
  674. &self,
  675. pat: &Pat,
  676. scrut: &mut Thunk,
  677. bindings: &mut Vec<(Name, Thunk)>,
  678. ) -> Result<bool, Error> {
  679. if let Pat::Var(v) = pat {
  680. bindings.push((*v, scrut.clone()));
  681. return Ok(true);
  682. }
  683. if let Pat::Wildcard = pat {
  684. return Ok(true);
  685. }
  686. // if it's not just a variable, then we'll need to make sure
  687. // we've evaluated `scrut` at least one level from here
  688. if let Thunk::Expr(e, env) = scrut {
  689. *scrut = Thunk::Value(self.eval(*e, env)?)
  690. };
  691. // now we can match deeper patterns, at least a little
  692. match pat {
  693. // literals match if the thunk is an identical literal
  694. Pat::Lit(lhs) => {
  695. if let Thunk::Value(Value::Lit(rhs)) = scrut {
  696. Ok(lhs == rhs)
  697. } else {
  698. Ok(false)
  699. }
  700. }
  701. // tuples match if the thunk evaluates to a tuple of the
  702. // same size, and if all the patterns in the tuple match
  703. // the thunks in the expression
  704. Pat::Tup(pats) => {
  705. if let Thunk::Value(Value::Tup(thunks)) = scrut {
  706. if pats.len() != thunks.len() {
  707. return Ok(false);
  708. }
  709. for (p, t) in pats.iter().zip(thunks) {
  710. if !self.match_pat(p, t, bindings)? {
  711. return Ok(false);
  712. }
  713. }
  714. Ok(true)
  715. } else {
  716. Ok(false)
  717. }
  718. }
  719. // otherwise, Does Not Match
  720. _ => Ok(false),
  721. }
  722. }
  723. // this chooses an expressino from a choice, taking into account
  724. // the weights
  725. fn choose(&self, choices: &[Choice], env: &Env) -> Result<Value, Error> {
  726. let max = choices.iter().map(Choice::weight).sum();
  727. let mut choice = self.rand.borrow_mut().gen_range_i64(0, max);
  728. for ch in choices {
  729. if choice < ch.weight() {
  730. return self.eval(ch.value, env);
  731. }
  732. choice -= ch.weight();
  733. }
  734. // if we got here, it means our math was wrong
  735. bail!("unreachable")
  736. }
  737. }