interp.rs 28 KB

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