diff options
Diffstat (limited to 'src/eval/builtins.rs')
-rw-r--r-- | src/eval/builtins.rs | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/src/eval/builtins.rs b/src/eval/builtins.rs index 7820a8e..7d10a86 100644 --- a/src/eval/builtins.rs +++ b/src/eval/builtins.rs | |||
@@ -48,11 +48,8 @@ builtins! { | |||
48 | fn print(ctx: &mut Context, args: &[ast::Expr]) -> Result { | 48 | fn print(ctx: &mut Context, args: &[ast::Expr]) -> Result { |
49 | for arg in args { | 49 | for arg in args { |
50 | let val = ctx.eval_expr(arg)?; | 50 | let val = ctx.eval_expr(arg)?; |
51 | let mut default_stream =Box::new(std::io::stdout()) as Box<dyn std::io::Write> ; | 51 | let mut default_stream = Box::new(std::io::stdout()) as Box<dyn std::io::Write>; |
52 | let stream = ctx | 52 | let stream = ctx.output_stream.as_mut().unwrap_or(&mut default_stream); |
53 | .output_stream | ||
54 | .as_mut() | ||
55 | .unwrap_or(&mut default_stream); | ||
56 | write!(stream, "{val}").unwrap(); | 53 | write!(stream, "{val}").unwrap(); |
57 | } | 54 | } |
58 | Ok(Value::Unit) | 55 | Ok(Value::Unit) |
@@ -157,9 +154,9 @@ fn kind(ctx: &mut Context, args: &[ast::Expr]) -> Result { | |||
157 | 154 | ||
158 | fn member(ctx: &mut Context, args: &[ast::Expr]) -> Result { | 155 | fn member(ctx: &mut Context, args: &[ast::Expr]) -> Result { |
159 | let [list_expr, element_expr] = get_args::<2>(args)?; | 156 | let [list_expr, element_expr] = get_args::<2>(args)?; |
160 | let list = ctx.eval_expr(&list_expr)?; | 157 | let list = ctx.eval_expr(list_expr)?; |
161 | let v = list.as_list()?; | 158 | let v = list.as_list()?; |
162 | let element = ctx.eval_expr(&element_expr)?; | 159 | let element = ctx.eval_expr(element_expr)?; |
163 | v.iter() | 160 | v.iter() |
164 | .any(|item| item == &element) | 161 | .any(|item| item == &element) |
165 | .wrap(Value::Boolean) | 162 | .wrap(Value::Boolean) |
@@ -174,7 +171,7 @@ fn push(ctx: &mut Context, args: &[ast::Expr]) -> Result { | |||
174 | lhs | 171 | lhs |
175 | ))); | 172 | ))); |
176 | }; | 173 | }; |
177 | let element = ctx.eval_expr(&rhs)?; | 174 | let element = ctx.eval_expr(rhs)?; |
178 | let variable = ctx.lookup_mut(ident)?; | 175 | let variable = ctx.lookup_mut(ident)?; |
179 | variable.mutate(|v| match &mut v.value { | 176 | variable.mutate(|v| match &mut v.value { |
180 | Value::List(l) => { | 177 | Value::List(l) => { |
@@ -195,9 +192,7 @@ fn pop(ctx: &mut Context, args: &[ast::Expr]) -> Result { | |||
195 | }; | 192 | }; |
196 | let variable = ctx.lookup_mut(ident)?; | 193 | let variable = ctx.lookup_mut(ident)?; |
197 | variable.mutate(|v| match &mut v.value { | 194 | variable.mutate(|v| match &mut v.value { |
198 | Value::List(l) => l | 195 | Value::List(l) => l.pop().ok_or(Error::ArrayOutOfBounds { idx: 0, len: 0 }), |
199 | .pop() | ||
200 | .ok_or_else(|| Error::ArrayOutOfBounds { idx: 0, len: 0 }), | ||
201 | _ => Err(v.ty().expected([ast::Type::List])), | 196 | _ => Err(v.ty().expected([ast::Type::List])), |
202 | }) | 197 | }) |
203 | } | 198 | } |
@@ -211,12 +206,7 @@ fn isempty(ctx: &mut Context, args: &[ast::Expr]) -> Result { | |||
211 | .is_empty() | 206 | .is_empty() |
212 | .wrap(Value::Boolean) | 207 | .wrap(Value::Boolean) |
213 | .wrap(Ok), | 208 | .wrap(Ok), |
214 | ast::Type::String => v | 209 | ast::Type::String => v.as_str().unwrap().is_empty().wrap(Value::Boolean).wrap(Ok), |
215 | .as_str() | ||
216 | .unwrap() | ||
217 | .is_empty() | ||
218 | .wrap(Value::Boolean) | ||
219 | .wrap(Ok), | ||
220 | _ => Err(v.ty().expected([ast::Type::List])), | 210 | _ => Err(v.ty().expected([ast::Type::List])), |
221 | } | 211 | } |
222 | } | 212 | } |