diff options
Diffstat (limited to 'src/eval/builtins.rs')
-rw-r--r-- | src/eval/builtins.rs | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/src/eval/builtins.rs b/src/eval/builtins.rs index 7d10a86..c91f7ba 100644 --- a/src/eval/builtins.rs +++ b/src/eval/builtins.rs | |||
@@ -29,6 +29,8 @@ builtins! { | |||
29 | // string | 29 | // string |
30 | isupper, | 30 | isupper, |
31 | islower, | 31 | islower, |
32 | toupper, | ||
33 | tolower, | ||
32 | substr, | 34 | substr, |
33 | 35 | ||
34 | // node | 36 | // node |
@@ -78,6 +80,22 @@ fn islower(ctx: &mut Context, args: &[ast::Expr]) -> Result { | |||
78 | .into()) | 80 | .into()) |
79 | } | 81 | } |
80 | 82 | ||
83 | fn toupper(ctx: &mut Context, args: &[ast::Expr]) -> Result { | ||
84 | Ok(ctx | ||
85 | .eval_expr(&get_args::<1>(args)?[0])? | ||
86 | .as_str()? | ||
87 | .to_uppercase() | ||
88 | .into()) | ||
89 | } | ||
90 | |||
91 | fn tolower(ctx: &mut Context, args: &[ast::Expr]) -> Result { | ||
92 | Ok(ctx | ||
93 | .eval_expr(&get_args::<1>(args)?[0])? | ||
94 | .as_str()? | ||
95 | .to_lowercase() | ||
96 | .into()) | ||
97 | } | ||
98 | |||
81 | fn substr(ctx: &mut Context, args: &[ast::Expr]) -> Result { | 99 | fn substr(ctx: &mut Context, args: &[ast::Expr]) -> Result { |
82 | if let Ok([string, start, end]) = get_args::<3>(args) { | 100 | if let Ok([string, start, end]) = get_args::<3>(args) { |
83 | let v = ctx.eval_expr(string)?; | 101 | let v = ctx.eval_expr(string)?; |
@@ -217,30 +235,3 @@ fn get_args<const N: usize>(args: &[ast::Expr]) -> std::result::Result<&[ast::Ex | |||
217 | got: args.len(), | 235 | got: args.len(), |
218 | }) | 236 | }) |
219 | } | 237 | } |
220 | |||
221 | #[cfg(test)] | ||
222 | mod test { | ||
223 | use super::*; | ||
224 | use crate::{ast::*, eval::*}; | ||
225 | |||
226 | #[test] | ||
227 | fn test_ts_builtins() { | ||
228 | let language = tree_sitter_python::language(); | ||
229 | let mut ctx = Context::new(language).with_program(Program::new()); | ||
230 | |||
231 | assert_eq!( | ||
232 | ctx.eval_block(&Block { | ||
233 | body: vec![Statement::decl(Type::List, "a", Expr::list([Expr::int(5)]),)] | ||
234 | }), | ||
235 | Ok(Value::Unit) | ||
236 | ); | ||
237 | assert_eq!( | ||
238 | ctx.lookup(&String::from("a")).unwrap().clone(), | ||
239 | Variable { | ||
240 | ty: Type::List, | ||
241 | name: "a".to_owned(), | ||
242 | value: vec![5usize.into()].into(), | ||
243 | } | ||
244 | ); | ||
245 | } | ||
246 | } | ||