A demos/actions.cn => demos/actions.cn +7 -0
@@ 0,0 1,7 @@
+;
+actions, or functions, are just strings that are executed. arguments passed are available
+through the `args' variable
+~
+
+(`(args add)' `myaction' defact) ; define an action called `myaction' ~
+(1 2 3 4 myaction print) ; call myaction with arguments 1 2 3 4 ~
A demos/args.cn => demos/args.cn +14 -0
@@ 0,0 1,14 @@
+;
+use the variable `args' to refer to CLI arguments and also to function arguments
+
+run this with arguments after it to see them echoed
+
+ex: cane-lang args.cn hi there
+~
+
+(args print)
+
+;
+this might have a strange output with all of the words meshed together. this is expected!
+when an array (args) is converted to a string, the elements are put together without any spaces
+~
M src/interpreter.rs => src/interpreter.rs +12 -8
@@ 315,6 315,9 @@ where
.get_data()
.clone(),
);
+ // Delete old data (since it was copied into
+ // args for new instance)
+ self.call_stack.last_mut().unwrap().get_data().erase();
match action_interp.execute() {
Ok(data) => {
self.call_stack
@@ 354,13 357,14 @@ where
self.call_stack.last_mut().unwrap().get_data().to_owned();
self.call_stack.pop();
match self.call_stack.last_mut().unwrap().get_state() {
- Some(State::Data) | Some(State::Wait) => self
- .call_stack
- .last_mut()
- .unwrap()
- .get_data()
- .get_list()
- .push(data),
+ Some(State::Data) | Some(State::Wait) => {
+ self.call_stack
+ .last_mut()
+ .unwrap()
+ .get_data()
+ .get_list()
+ .push(data);
+ }
_ => (),
}
}
@@ 650,7 654,7 @@ mod tests {
let mut variables = HashMap::new();
let args = Data::new(Types::LIST);
- let test_input = b"(`(args add print)' `act' defact) (1 2 3 4 act)" as &[u8];
+ let test_input = b"(`(args add)' `act' defact) (1 2 3 4 act print)" as &[u8];
c.write_all(test_input).unwrap();
let mut reader = BufReader::new(c);
reader.rewind().unwrap();
M src/stdtypes.rs => src/stdtypes.rs +7 -0
@@ 317,6 317,13 @@ impl Data {
self.val_string = sum.val_string;
}
}
+
+ /// Emptys the data
+ pub fn erase(&mut self) {
+ self.val_string = "".to_string();
+ self.val_list.0 = LinkedList::new();
+ self.val_number.0 = Rational::from((0, 1));
+ }
}
#[cfg(test)]