interp.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. use crate::ast::*;
  2. use rand::Rng;
  3. use std::cell::RefCell;
  4. use std::collections::HashMap;
  5. use std::fmt;
  6. use std::rc::Rc;
  7. use std::io;
  8. macro_rules! bail {
  9. ($fmt:expr) => { return Err(Error { message: format!($fmt), }) };
  10. ($fmt:expr, $($e:expr),*) => { return Err(Error { message: format!($fmt, $($e),*), }) }
  11. }
  12. #[derive(Debug, Clone)]
  13. pub enum Value {
  14. Lit(Literal),
  15. Tup(Vec<Thunk>),
  16. Builtin(&'static BuiltinFunc),
  17. Closure(Closure),
  18. Nil,
  19. }
  20. impl Value {
  21. fn as_num(&self) -> Result<i64, Error> {
  22. match self {
  23. Value::Lit(Literal::Num(n)) => Ok(*n),
  24. _ => self.with_str(|s| bail!("Expected number, got {}", s)),
  25. }
  26. }
  27. fn as_str(&self) -> Result<&str, Error> {
  28. match self {
  29. Value::Lit(Literal::Str(s)) => Ok(s),
  30. _ => self.with_str(|s| bail!("Expected string, got {}", s)),
  31. }
  32. }
  33. fn as_tup(&self) -> Result<&[Thunk], Error> {
  34. match self {
  35. Value::Tup(vals) => Ok(vals),
  36. _ => self.with_str(|s| bail!("Expected tuple, got {}", s)),
  37. }
  38. }
  39. fn as_closure(&self) -> Result<&Closure, Error> {
  40. match self {
  41. Value::Closure(closure) => Ok(closure),
  42. _ => self.with_str(|s| bail!("Expected tuple, got {}", s)),
  43. }
  44. }
  45. fn with_str<U>(&self, f: impl FnOnce(&str) -> U) -> U {
  46. match self {
  47. Value::Nil => f(""),
  48. Value::Lit(Literal::Str(s)) => f(s),
  49. Value::Lit(Literal::Atom(s)) => f(&format!("{:?}", s)),
  50. Value::Lit(Literal::Num(n)) => f(&format!("{}", n)),
  51. Value::Tup(values) => {
  52. let mut buf = String::new();
  53. buf.push('<');
  54. for (i, val) in values.iter().enumerate() {
  55. if i > 0 {
  56. buf.push_str(", ");
  57. }
  58. match val {
  59. Thunk::Value(v) => buf.push_str(&v.to_string()),
  60. Thunk::Expr(..) => buf.push_str("#<unevaluated>"),
  61. Thunk::Builtin(func) => buf.push_str(&format!("#<builtin {}>", func.name)),
  62. }
  63. }
  64. buf.push('>');
  65. f(&buf)
  66. }
  67. Value::Builtin(func) => f(&format!("#<builtin {}>", func.name)),
  68. Value::Closure(_) => f("#<lambda ...>"),
  69. }
  70. }
  71. }
  72. impl fmt::Display for Value {
  73. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  74. self.with_str(|s| write!(f, "{}", s))
  75. }
  76. }
  77. pub struct BuiltinFunc {
  78. name: &'static str,
  79. callback: &'static dyn Fn(&State, ExprRef, &Env) -> Result<Value, Error>,
  80. }
  81. #[derive(Debug)]
  82. pub struct Error {
  83. message: String,
  84. }
  85. impl From<lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>>
  86. for Error
  87. {
  88. fn from(
  89. err: lalrpop_util::ParseError<usize, crate::lexer::Token<'_>, crate::lexer::LexerError>,
  90. ) -> Error {
  91. Error {
  92. message: format!("{:?}", err),
  93. }
  94. }
  95. }
  96. impl fmt::Display for Error {
  97. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  98. write!(fmt, "{}", self.message)
  99. }
  100. }
  101. impl std::error::Error for Error {}
  102. const BUILTINS: &[BuiltinFunc] = &[
  103. BuiltinFunc {
  104. name: "rep",
  105. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  106. let (rep, expr) = {
  107. let ast = state.ast.borrow();
  108. let args = match &ast[expr] {
  109. Expr::Tup(tup) => tup,
  110. _ => bail!("`rep`: expected tuple"),
  111. };
  112. if args.len() != 2 {
  113. bail!("`rep`: expected two arguments, got {}", args.len())
  114. }
  115. (args[0], args[1])
  116. };
  117. let mut buf = String::new();
  118. let num = state.eval(rep, env)?.as_num()?;
  119. for _ in 0..num {
  120. buf.push_str(&state.eval(expr, env)?.as_str()?.to_string());
  121. }
  122. Ok(Value::Lit(Literal::Str(buf)))
  123. },
  124. },
  125. BuiltinFunc {
  126. name: "length",
  127. callback: &|state: &State, expr: ExprRef, _env: &Env| -> Result<Value, Error> {
  128. let ast = state.ast.borrow();
  129. let args = match &ast[expr] {
  130. Expr::Tup(tup) => tup,
  131. _ => bail!("`length`: expected tuple"),
  132. };
  133. Ok(Value::Lit(Literal::Num(args.len() as i64)))
  134. },
  135. },
  136. BuiltinFunc {
  137. name: "to-upper",
  138. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  139. let s = state.eval(expr, env)?;
  140. Ok(Value::Lit(Literal::Str(s.as_str()?.to_uppercase())))
  141. },
  142. },
  143. BuiltinFunc {
  144. name: "to-lower",
  145. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  146. let s = state.eval(expr, env)?;
  147. Ok(Value::Lit(Literal::Str(s.as_str()?.to_lowercase())))
  148. },
  149. },
  150. BuiltinFunc {
  151. name: "concat",
  152. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  153. let val = state.eval(expr, env)?;
  154. let tup = val.as_tup()?;
  155. let mut contents = Vec::new();
  156. for elem in tup {
  157. for th in state.hnf(elem)?.as_tup()? {
  158. contents.push(th.clone());
  159. }
  160. }
  161. Ok(Value::Tup(contents))
  162. },
  163. },
  164. BuiltinFunc {
  165. name: "tuple-fold",
  166. callback: &|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  167. let val = state.eval(expr, env)?;
  168. let args = val.as_tup()?;
  169. if args.len() != 3 {
  170. bail!("`tuple-fold`: expected 3 arguments, got {}", args.len());
  171. }
  172. let func = &args[0];
  173. let init = &args[1];
  174. let tup = &args[2];
  175. let func = state.hnf(func)?;
  176. let tup = state.hnf(tup)?;
  177. let mut result = init.clone();
  178. for t in tup.as_tup()? {
  179. let partial = state.eval_closure(func.as_closure()?, result)?;
  180. result = Thunk::Value(state.eval_closure(partial.as_closure()?, t.clone())?);
  181. }
  182. state.hnf(&result)
  183. },
  184. },
  185. ];
  186. impl fmt::Debug for BuiltinFunc {
  187. fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
  188. writeln!(fmt, "BuiltinFunc {{ name: {:?}, ... }}", self.name)
  189. }
  190. }
  191. #[derive(Debug, Clone)]
  192. pub enum Thunk {
  193. Expr(ExprRef, Env),
  194. Value(Value),
  195. Builtin(&'static BuiltinFunc),
  196. }
  197. type Env = Option<Rc<Scope>>;
  198. #[derive(Debug)]
  199. pub struct Scope {
  200. vars: HashMap<Name, Thunk>,
  201. parent: Env,
  202. }
  203. #[derive(Debug, Clone)]
  204. pub struct Closure {
  205. func: ExprRef,
  206. scope: Env,
  207. }
  208. pub struct State {
  209. ast: RefCell<ASTArena>,
  210. root_scope: RefCell<HashMap<Name, Thunk>>,
  211. rand: RefCell<rand::rngs::ThreadRng>,
  212. parser: crate::grammar::StmtsParser,
  213. }
  214. impl Default for State {
  215. fn default() -> State {
  216. Self::new()
  217. }
  218. }
  219. impl State {
  220. pub fn new() -> State {
  221. let s = State {
  222. root_scope: RefCell::new(HashMap::new()),
  223. rand: RefCell::new(rand::thread_rng()),
  224. parser: crate::grammar::StmtsParser::new(),
  225. ast: RefCell::new(ASTArena::new()),
  226. };
  227. for builtin in BUILTINS {
  228. let sym = s.ast.borrow_mut().add_string(builtin.name);
  229. s.root_scope
  230. .borrow_mut()
  231. .insert(sym, Thunk::Builtin(builtin));
  232. }
  233. s
  234. }
  235. pub fn get_ast(&self) -> &RefCell<ASTArena> {
  236. &self.ast
  237. }
  238. fn lookup(&self, env: &Env, name: Name) -> Result<Thunk, Error> {
  239. if let Some(env) = env {
  240. if let Some(ne) = env.vars.get(&name) {
  241. Ok(ne.clone())
  242. } else {
  243. self.lookup(&env.parent, name)
  244. }
  245. } else {
  246. match self.root_scope.borrow().get(&name) {
  247. None => bail!("no such thing: {}", &self.ast.borrow()[name]),
  248. Some(ne) => Ok(ne.clone()),
  249. }
  250. }
  251. }
  252. pub fn run(&self, src: &str) -> Result<(), Error> {
  253. let lexed = crate::lexer::tokens(src);
  254. let stmts = self.parser.parse(&mut self.ast.borrow_mut(), lexed)?;
  255. let mut stdout = io::stdout();
  256. for stmt in stmts {
  257. self.execute(&stmt, &mut stdout)?;
  258. }
  259. Ok(())
  260. }
  261. pub fn run_repl(&self, src: &str) -> Result<(), Error> {
  262. let lexed = crate::lexer::tokens(src);
  263. let stmts = {
  264. let mut ast = self.ast.borrow_mut();
  265. self.parser.parse(&mut ast, lexed)
  266. };
  267. let stmts = match stmts {
  268. Ok(stmts) => stmts,
  269. Err(err) => {
  270. let with_puts = format!("puts {}", src);
  271. let lexed = crate::lexer::tokens(&with_puts);
  272. if let Ok(stmts) = self.parser.parse(&mut self.ast.borrow_mut(), lexed) {
  273. stmts
  274. } else {
  275. return Err(err.into());
  276. }
  277. }
  278. };
  279. for stmt in stmts {
  280. self.execute(&stmt, io::stdout())?;
  281. }
  282. Ok(())
  283. }
  284. pub fn autocomplete(&self, fragment: &str, at_beginning: bool) -> Vec<String> {
  285. let mut possibilities = Vec::new();
  286. for name in self.root_scope.borrow().keys() {
  287. if self.ast.borrow()[*name].starts_with(fragment) {
  288. possibilities.push(self.ast.borrow()[*name].to_string());
  289. }
  290. }
  291. if at_beginning && "puts".starts_with(fragment) {
  292. possibilities.push("puts ".to_owned());
  293. }
  294. possibilities
  295. }
  296. pub fn execute(&self, stmt: &Stmt, mut output: impl io::Write) -> Result<(), Error> {
  297. match stmt {
  298. Stmt::Puts(expr) => {
  299. let val = self.eval(*expr, &None)?;
  300. let val = self.force(val)?;
  301. writeln!(output, "{}", val.to_string()).unwrap();
  302. }
  303. Stmt::Fix(name) => {
  304. let (expr, env) = match self.lookup(&None, *name)? {
  305. Thunk::Expr(e, env) => (e, env),
  306. // if it's not an expr, then our work here is done
  307. _ => return Ok(()),
  308. };
  309. let val = self.eval(expr, &env)?;
  310. let val = self.force(val)?;
  311. self.root_scope
  312. .borrow_mut()
  313. .insert(*name, Thunk::Value(val));
  314. }
  315. Stmt::Assn(fixed, name, expr) => {
  316. if *fixed {
  317. let val = self.eval(*expr, &None)?;
  318. self.root_scope
  319. .borrow_mut()
  320. .insert(*name, Thunk::Value(val));
  321. } else {
  322. self.root_scope
  323. .borrow_mut()
  324. .insert(*name, Thunk::Expr(*expr, None));
  325. }
  326. }
  327. Stmt::LitAssn(fixed, name, strs) => {
  328. if *fixed {
  329. let choice = &strs[self.rand.borrow_mut().gen_range(0..strs.len())];
  330. self.root_scope.borrow_mut().insert(
  331. *name,
  332. Thunk::Value(Value::Lit(Literal::Str(choice.clone()))),
  333. );
  334. return Ok(());
  335. }
  336. let choices = strs
  337. .iter()
  338. .map(|s| Choice {
  339. weight: None,
  340. value: self
  341. .ast
  342. .borrow_mut()
  343. .add_expr(Expr::Lit(Literal::Str(s.clone()))),
  344. })
  345. .collect();
  346. let choices = self.ast.borrow_mut().add_expr(Expr::Chc(choices));
  347. self.root_scope
  348. .borrow_mut()
  349. .insert(*name, Thunk::Expr(choices, None));
  350. }
  351. }
  352. Ok(())
  353. }
  354. fn force(&self, val: Value) -> Result<Value, Error> {
  355. match val {
  356. Value::Tup(values) => Ok(Value::Tup(
  357. values
  358. .into_iter()
  359. .map(|t| {
  360. let v = self.hnf(&t)?;
  361. let v = self.force(v)?;
  362. Ok(Thunk::Value(v))
  363. })
  364. .collect::<Result<Vec<Thunk>, Error>>()?,
  365. )),
  366. _ => Ok(val),
  367. }
  368. }
  369. fn hnf(&self, thunk: &Thunk) -> Result<Value, Error> {
  370. match thunk {
  371. Thunk::Expr(expr, env) => self.eval(*expr, env),
  372. Thunk::Value(val) => Ok(val.clone()),
  373. Thunk::Builtin(b) => Ok(Value::Builtin(b)),
  374. }
  375. }
  376. fn eval(&self, expr_ref: ExprRef, env: &Env) -> Result<Value, Error> {
  377. let expr = &self.ast.borrow()[expr_ref];
  378. match expr {
  379. Expr::Lit(l) => Ok(Value::Lit(l.clone())),
  380. Expr::Nil => Ok(Value::Nil),
  381. Expr::Var(v) => {
  382. let (e, env) = match self.lookup(env, *v)? {
  383. Thunk::Expr(e, env) => (e, env),
  384. Thunk::Value(v) => return Ok(v),
  385. Thunk::Builtin(b) => return Ok(Value::Builtin(b)),
  386. };
  387. self.eval(e, &env)
  388. }
  389. Expr::Cat(cat) => {
  390. if cat.len() == 1 {
  391. self.eval(cat[0], env)
  392. } else {
  393. let mut buf = String::new();
  394. for expr in cat {
  395. let val = self.eval(*expr, env)?;
  396. let val = self.force(val)?;
  397. buf.push_str(&val.to_string());
  398. }
  399. Ok(Value::Lit(Literal::Str(buf)))
  400. }
  401. }
  402. Expr::Chc(choices) => {
  403. if choices.len() == 1 {
  404. self.eval(choices[0].value, env)
  405. } else {
  406. self.choose(choices, env)
  407. }
  408. }
  409. Expr::Tup(values) => Ok(Value::Tup(
  410. values
  411. .iter()
  412. .map(|v| Thunk::Expr(*v, env.clone()))
  413. .collect::<Vec<Thunk>>(),
  414. )),
  415. Expr::Range(from, to) => {
  416. let from = self.eval(*from, env)?.as_num()?;
  417. let to = self.eval(*to, env)?.as_num()?;
  418. Ok(Value::Lit(Literal::Num(
  419. self.rand.borrow_mut().gen_range(from..=to),
  420. )))
  421. }
  422. Expr::Fun(_) => Ok(Value::Closure(Closure {
  423. func: expr_ref,
  424. scope: env.clone(),
  425. })),
  426. Expr::Ap(func, val) => match self.eval(*func, env)? {
  427. Value::Closure(c) => {
  428. let scrut = Thunk::Expr(*val, env.clone());
  429. self.eval_closure(&c, scrut)
  430. }
  431. Value::Builtin(builtin) => (builtin.callback)(self, *val, env),
  432. _ => bail!("Bad function: {:?}", func),
  433. },
  434. _ => bail!("unimplemented: {:?}", expr),
  435. }
  436. }
  437. fn eval_closure(&self, closure: &Closure, mut scrut: Thunk) -> Result<Value, Error> {
  438. let ast = self.ast.borrow();
  439. let cases = match &ast[closure.func] {
  440. Expr::Fun(cases) => cases,
  441. _ => bail!("INVARIANT FAILED"),
  442. };
  443. for c in cases {
  444. let mut bindings = Vec::new();
  445. if !self.match_pat(&c.pat, &mut scrut, &mut bindings)? {
  446. continue;
  447. }
  448. let mut new_scope = HashMap::new();
  449. for (name, binding) in bindings {
  450. new_scope.insert(name, binding);
  451. }
  452. let new_scope = Rc::new(Scope {
  453. vars: new_scope,
  454. parent: closure.scope.clone(),
  455. });
  456. return self.eval(c.expr, &Some(new_scope));
  457. }
  458. bail!("No pattern in {:?} matched {:?}", cases, scrut);
  459. }
  460. fn match_pat(
  461. &self,
  462. pat: &Pat,
  463. scrut: &mut Thunk,
  464. bindings: &mut Vec<(Name, Thunk)>,
  465. ) -> Result<bool, Error> {
  466. if let Pat::Var(v) = pat {
  467. bindings.push((*v, scrut.clone()));
  468. return Ok(true);
  469. }
  470. if let Pat::Wildcard = pat {
  471. return Ok(true);
  472. }
  473. // if it's not just a variable, then we'll need to make sure
  474. // we've evaluated `scrut` at least one level from here
  475. if let Thunk::Expr(e, env) = scrut {
  476. *scrut = Thunk::Value(self.eval(*e, env)?)
  477. };
  478. // now we can match deeper patterns, at least a little
  479. match pat {
  480. Pat::Lit(lhs) => {
  481. if let Thunk::Value(Value::Lit(rhs)) = scrut {
  482. Ok(lhs == rhs)
  483. } else {
  484. Ok(false)
  485. }
  486. }
  487. Pat::Tup(pats) => {
  488. if let Thunk::Value(Value::Tup(thunks)) = scrut {
  489. if pats.len() != thunks.len() {
  490. return Ok(false);
  491. }
  492. for (p, t) in pats.iter().zip(thunks) {
  493. if !self.match_pat(p, t, bindings)? {
  494. return Ok(false);
  495. }
  496. }
  497. Ok(true)
  498. } else {
  499. Ok(false)
  500. }
  501. }
  502. _ => Ok(false),
  503. }
  504. }
  505. fn choose(&self, choices: &[Choice], env: &Env) -> Result<Value, Error> {
  506. let max = choices.iter().map(Choice::weight).sum();
  507. let mut choice = self.rand.borrow_mut().gen_range(0..max);
  508. for ch in choices {
  509. if choice < ch.weight() {
  510. return self.eval(ch.value, env);
  511. }
  512. choice -= ch.weight();
  513. }
  514. bail!("unreachable")
  515. }
  516. }