interp.rs 30 KB

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