Skip to content

Commit 77215f3

Browse files
committed
feat: iter (map, aempty, range)
1 parent 80ac8a8 commit 77215f3

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

stack-control/src/bytecode/commands/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{compiletime::command_map::CommandMap, runtime::{stack::Stack, value::{Array, Value}}};
22

3-
use super::{math::append_math, stack_manipulators::append_stack_manipulators, CommandExecutable, DescribedCommand, DescribedCommandMaker, RuntimeException};
3+
use super::{iters::append_iters, math::append_math, stack_manipulators::append_stack_manipulators, CommandExecutable, DescribedCommand, DescribedCommandMaker, RuntimeException};
44

55
use indoc::indoc;
66

@@ -9,7 +9,7 @@ pub fn bind_default_commands(map: &mut CommandMap) {
99

1010
append_stack_manipulators(map);
1111
append_math(map);
12-
12+
append_iters(map);
1313
}
1414

1515
fn _test(_stack: &mut Stack) -> Result<(), RuntimeException> {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::runtime::{stack::Stack, value::{Array, Value}};
2+
3+
use super::{core::define_commands, RuntimeException};
4+
use indoc::indoc;
5+
6+
fn _test(stack: &mut Stack) -> Result<(), RuntimeException> {
7+
if let Value::Number(num) = stack.pop()? {
8+
stack.push(Value::Array(Array::from(
9+
(0..(num.round() as i32)).map(|n| Value::Number(n.into())).collect()
10+
)));
11+
} Err(RuntimeException::WrongElementType)
12+
}
13+
14+
define_commands!(append_iters
15+
EachCommand('∴', ["map", "each"]) {
16+
with {
17+
description: String::from(indoc! {"
18+
Applies function to each value
19+
"})
20+
}
21+
stack {
22+
let f = stack.pop()?;
23+
if let Value::Array(arr) = stack.pop()? {
24+
let old = arr.move_out();
25+
let mut new = vec![];
26+
for e in old {
27+
stack.push(e);
28+
f.invoke(stack)?;
29+
new.push(stack.pop()?);
30+
}
31+
stack.push(Value::Array(Array::from(new)));
32+
} else { return Err(RuntimeException::WrongElementType) }
33+
Ok(())
34+
}
35+
},
36+
37+
EmptyArrayCommand('∅', ["aempty", "emptyarr"]) {
38+
with {
39+
description: String::from(indoc! {"
40+
Places an empty array on top of stack
41+
"})
42+
}
43+
stack {
44+
stack.push(Value::Array(Array::new()));
45+
Ok(())
46+
}
47+
},
48+
49+
Range('⇡', ["range"]) {
50+
with {
51+
52+
}
53+
stack {
54+
if let Value::Number(num) = stack.pop()? {
55+
stack.push(Value::Array(Array::from(
56+
(0..(num.round() as i32)).map(|n| Value::Number(n.into())).collect()
57+
)));
58+
return Ok(())
59+
} Err(RuntimeException::WrongElementType)
60+
}
61+
}
62+
);

stack-control/src/runtime/value.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl Array {
3333
self.pointer.borrow_mut()
3434
}
3535

36+
pub fn move_out(self) -> Vec<Value> {
37+
self.pointer.take()
38+
}
39+
3640
fn own(&mut self) {
3741
let clone = self.pointer.borrow().clone();
3842
self.set(clone);

0 commit comments

Comments
 (0)