Created
June 16, 2017 15:32
-
-
Save fkaa/5fef46fead46c54f568dcfe5ef46f0ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn example() -> Result<()> { | |
let mut state = State::new(); | |
fn add(stack: FunctionStack) -> Result<i32> { | |
let sz = stack.check_size(1..)?; | |
let mut sum = 0f64; | |
for i in 1..(sz + 1) { | |
sum += stack.arg::<f64>(i)?; | |
} | |
stack.push(sum); | |
Ok(1) | |
} | |
state.set("test", Table::new(|cxt| { | |
cxt.set("add", add); | |
})); | |
state.eval(r#" | |
print(test.add(5)) -- 5 | |
print(test.add(5, 10)) -- 15 | |
print(test.add(5, 10, 100, 200)) -- 315 | |
"#)?; | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment