builtins.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. use crate::ast::*;
  2. use crate::interp::*;
  3. use anyhow::{bail, Error};
  4. /// The list of builtins provided at startup.
  5. ///
  6. /// TODO: move this to a separate file and clean it up
  7. pub fn builtins() -> Vec<BuiltinFunc> {
  8. vec![
  9. BuiltinFunc {
  10. name: "rep",
  11. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  12. let (rep, expr) = {
  13. let ast = state.ast.borrow();
  14. let args = match &ast[expr] {
  15. Expr::Tup(tup) => tup,
  16. _ => {
  17. let span = state.ast.borrow().get_line(expr.file, expr.span);
  18. bail!("`rep`: expected tuple\n{}", span)
  19. }
  20. };
  21. if args.len() != 2 {
  22. let span = state.ast.borrow().get_line(expr.file, expr.span);
  23. bail!(
  24. "`rep`: expected two arguments, got {}\n{}",
  25. args.len(),
  26. span
  27. )
  28. }
  29. (args[0], args[1])
  30. };
  31. let mut buf = String::new();
  32. let num = state.eval(rep, env)?.as_num(&state.ast.borrow())?;
  33. for _ in 0..num {
  34. buf.push_str(
  35. &state
  36. .eval(expr, env)?
  37. .as_str(&state.ast.borrow())?
  38. .to_string(),
  39. );
  40. }
  41. Ok(Value::Lit(Literal::Str(buf)))
  42. }),
  43. },
  44. BuiltinFunc {
  45. name: "length",
  46. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  47. let args = match state.eval(expr, env)? {
  48. Value::Tup(tup) => tup,
  49. _ => bail!("`length`: expected tuple"),
  50. };
  51. Ok(Value::Lit(Literal::Num(args.len() as i64)))
  52. }),
  53. },
  54. BuiltinFunc {
  55. name: "to-upper",
  56. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  57. let s = state.eval(expr, env)?;
  58. Ok(Value::Lit(Literal::Str(
  59. s.as_str(&state.ast.borrow())?.to_uppercase(),
  60. )))
  61. }),
  62. },
  63. BuiltinFunc {
  64. name: "capitalize",
  65. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  66. let s = state.eval(expr, env)?;
  67. Ok(Value::Lit(Literal::Str(titlecase::titlecase(
  68. s.as_str(&state.ast.borrow())?,
  69. ))))
  70. }),
  71. },
  72. BuiltinFunc {
  73. name: "to-lower",
  74. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  75. let s = state.eval(expr, env)?;
  76. Ok(Value::Lit(Literal::Str(
  77. s.as_str(&state.ast.borrow())?.to_lowercase(),
  78. )))
  79. }),
  80. },
  81. BuiltinFunc {
  82. name: "sub",
  83. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  84. let val = state.eval(expr, env)?;
  85. let args = val.as_tup(&state.ast.borrow())?;
  86. if let [x, y] = args {
  87. let x = state.hnf(x)?.as_num(&state.ast.borrow())?;
  88. let y = state.hnf(y)?.as_num(&state.ast.borrow())?;
  89. Ok(Value::Lit(Literal::Num(x - y)))
  90. } else {
  91. bail!("`sub`: expected 2 arguments, got {}", args.len());
  92. }
  93. }),
  94. },
  95. BuiltinFunc {
  96. name: "concat",
  97. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  98. let val = state.eval(expr, env)?;
  99. let tup = val.as_tup(&state.ast.borrow())?;
  100. let mut contents = Vec::new();
  101. for elem in tup {
  102. for th in state.hnf(elem)?.as_tup(&state.ast.borrow())? {
  103. contents.push(th.clone());
  104. }
  105. }
  106. Ok(Value::Tup(contents))
  107. }),
  108. },
  109. BuiltinFunc {
  110. name: "tuple-index",
  111. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  112. let val = state.eval(expr, env)?;
  113. let args = val.as_tup(&state.ast.borrow())?;
  114. if let [tup, idx] = args {
  115. let tup = state.hnf(tup)?;
  116. let idx = state.hnf(idx)?;
  117. state.hnf(
  118. &tup.as_tup(&state.ast.borrow())?
  119. [idx.as_num(&state.ast.borrow())? as usize],
  120. )
  121. } else {
  122. bail!("`tuple-index`: expected 2 arguments, got {}", args.len());
  123. }
  124. }),
  125. },
  126. BuiltinFunc {
  127. name: "tuple-replace",
  128. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  129. let val = state.eval(expr, env)?;
  130. let args = val.as_tup(&state.ast.borrow())?;
  131. if let [tup, idx, new] = args {
  132. let tup_val = state.hnf(tup)?;
  133. let tup = tup_val.as_tup(&state.ast.borrow())?;
  134. let idx = state.hnf(idx)?.as_num(&state.ast.borrow())?;
  135. let mut modified = Vec::with_capacity(tup.len());
  136. for i in 0..idx {
  137. modified.push(tup[i as usize].clone());
  138. }
  139. modified.push(new.clone());
  140. for i in (idx + 1)..(tup.len() as i64) {
  141. modified.push(tup[i as usize].clone());
  142. }
  143. Ok(Value::Tup(modified))
  144. } else {
  145. bail!("`tuple-index`: expected 2 arguments, got {}", args.len());
  146. }
  147. }),
  148. },
  149. BuiltinFunc {
  150. name: "tuple-fold",
  151. callback: Box::new(|state: &State, expr: ExprRef, env: &Env| -> Result<Value, Error> {
  152. let val = state.eval(expr, env)?;
  153. let args = val.as_tup(&state.ast.borrow())?;
  154. if let [func, init, tup] = args {
  155. let func = state.hnf(func)?;
  156. let tup = state.hnf(tup)?;
  157. let mut result = init.clone();
  158. for t in tup.as_tup(&state.ast.borrow())? {
  159. let partial =
  160. state.eval_closure(func.as_closure(&state.ast.borrow())?, result)?;
  161. result =
  162. Thunk::Value(state.eval_closure(
  163. partial.as_closure(&state.ast.borrow())?,
  164. t.clone(),
  165. )?);
  166. }
  167. state.hnf(&result)
  168. } else {
  169. bail!("`tuple-fold`: expected 3 arguments, got {}", args.len());
  170. }
  171. }),
  172. },
  173. ]
  174. }