interp.rs 30 KB

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