interp.rs 31 KB

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