interp.rs 32 KB

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