Skip to content

Instantly share code, notes, and snippets.

@eddyb
Forked from brendanzab/stack_edsl.rs
Last active March 29, 2023 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eddyb/9461271 to your computer and use it in GitHub Desktop.
Save eddyb/9461271 to your computer and use it in GitHub Desktop.
fn pop<T, ..Stack>((_, ..stack): (T, ..Stack)) -> Stack {
stack
}
fn dup<T: Clone, ..Stack>((x, ..stack): (T, ..Stack)) -> (T, T, ..Stack) {
(x.clone(), x, ..stack)
}
fn swap<T, U, ..Stack>((x, y, ..stack): (T, U, ..Stack)) -> (U, T, ..Stack) {
(y, x, ..stack)
}
fn eq<T: Eq, ..Stack>((x, y, ..stack): (T, T, ..Stack)) -> (bool, ..Stack) {
(x == y, ..stack)
}
fn add<T: Add<T, T>, ..Stack>((x, y, ..stack): (T, T, ..Stack)) -> (T, ..Stack) {
(x + y, ..stack)
}
fn mul<T: Mul<T, T>, ..Stack>((x, y, ..stack): (T, T, ..Stack)) -> (T, ..Stack) {
(x * y, ..stack)
}
fn square<T: Clone + Mul<T, T>, ..Stack>(stack: (T, ..Stack)) -> (T, ..Stack) {
mul(dup(stack))
}
#[test]
fn test(){
assert_eq!(pop((4, 2)), (2,));
assert_eq!(dup((3,)), (3, 3));
assert_eq!(swap((4, 2)), (2, 4));
assert_eq!(eq((4, 2)), (false,));
assert_eq!(eq((4, 4)), (true,));
assert_eq!(add((4, 2)), (6,));
assert_eq!(mul((4, 2)), (8,));
assert_eq!(square((3,)), (9,));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment