|
| 1 | +extern crate libcc2rs; |
| 2 | +use libcc2rs::*; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::collections::BTreeMap; |
| 5 | +use std::io::prelude::*; |
| 6 | +use std::io::{Read, Seek, Write}; |
| 7 | +use std::os::fd::AsFd; |
| 8 | +use std::rc::{Rc, Weak}; |
| 9 | +pub fn apply_int_0(fn_: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 { |
| 10 | + let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_)); |
| 11 | + return ({ (*fn_.borrow()).call(x) }); |
| 12 | +} |
| 13 | +pub fn apply_int_1(fn_: FnPtr<Generic>, mut x: i32) -> i32 { |
| 14 | + let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_)); |
| 15 | + return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) }); |
| 16 | +} |
| 17 | +pub fn apply_int_2(fn_: FnPtr<Generic>, mut x: i32) -> i32 { |
| 18 | + let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_)); |
| 19 | + return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) }); |
| 20 | +} |
| 21 | +pub fn apply_int_3(fn_: FnPtr<Generic>, mut x: i32) -> i32 { |
| 22 | + let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_)); |
| 23 | + return ({ (*fn_.borrow()).spec::<fn(i32) -> i32>().call(x) }); |
| 24 | +} |
| 25 | +pub fn apply_double_4(fn_: FnPtr<Generic>, mut x: f64) -> f64 { |
| 26 | + let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_)); |
| 27 | + return ({ (*fn_.borrow()).spec::<fn(f64) -> f64>().call(x) }); |
| 28 | +} |
| 29 | +pub fn apply_double_5(fn_: FnPtr<Generic>, mut x: f64) -> f64 { |
| 30 | + let fn_: Value<FnPtr<Generic>> = Rc::new(RefCell::new(fn_)); |
| 31 | + return ({ (*fn_.borrow()).spec::<fn(f64) -> f64>().call(x) }); |
| 32 | +} |
| 33 | +pub fn apply_twice_6(fn_: FnPtr<fn(i32) -> i32>, mut x: i32) -> i32 { |
| 34 | + let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(fn_)); |
| 35 | + return ({ |
| 36 | + let _x: i32 = ({ (*fn_.borrow()).call(x) }); |
| 37 | + (*fn_.borrow()).call(_x) |
| 38 | + }) |
| 39 | + .clone(); |
| 40 | +} |
| 41 | +pub fn main() { |
| 42 | + __cpp2rust_init_globals(); |
| 43 | + std::process::exit(main_0()); |
| 44 | +} |
| 45 | +fn main_0() -> i32 { |
| 46 | + let factor: Value<i32> = Rc::new(RefCell::new(3)); |
| 47 | + let scale: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(lambda!( |
| 48 | + { |
| 49 | + let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow()))); |
| 50 | + }, |
| 51 | + |x: i32| -> i32 { |
| 52 | + return (x * (*factor.borrow())); |
| 53 | + }, |
| 54 | + ))); |
| 55 | + assert!((({ apply_twice_6((*scale.borrow()).copy_from(), 4,) }) == 36)); |
| 56 | + assert!( |
| 57 | + (({ |
| 58 | + apply_int_0( |
| 59 | + FnPtr::<fn(i32) -> i32>::new(|x: i32| -> i32 { |
| 60 | + { |
| 61 | + return -x; |
| 62 | + } |
| 63 | + }), |
| 64 | + 9, |
| 65 | + ) |
| 66 | + }) == -9_i32) |
| 67 | + ); |
| 68 | + let generic_scale: Value<FnPtr<Generic>> = Rc::new(RefCell::new(lambda!( |
| 69 | + Generic, |
| 70 | + { |
| 71 | + let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow()))); |
| 72 | + }, |
| 73 | + |x: i32| -> i32 { |
| 74 | + return (x * (*factor.borrow())); |
| 75 | + }, |
| 76 | + |x: f64| -> f64 { |
| 77 | + return (x * ((*factor.borrow()) as f64)); |
| 78 | + }, |
| 79 | + ))); |
| 80 | + assert!((({ apply_int_1((*generic_scale.borrow()).copy_from(), 4,) }) == 12)); |
| 81 | + assert!((({ apply_double_4((*generic_scale.borrow()).copy_from(), 1.5_f64,) }) == 4.5_f64)); |
| 82 | + assert!( |
| 83 | + (({ |
| 84 | + apply_int_2( |
| 85 | + lambda!(Generic, {}, |x: i32| -> i32 { |
| 86 | + return -x; |
| 87 | + },), |
| 88 | + 9, |
| 89 | + ) |
| 90 | + }) == -9_i32) |
| 91 | + ); |
| 92 | + let offset: Value<FnPtr<Generic>> = Rc::new(RefCell::new(lambda!( |
| 93 | + Generic, |
| 94 | + { |
| 95 | + let factor: Value<i32> = Rc::new(RefCell::new((*factor.borrow()))); |
| 96 | + }, |
| 97 | + |x: i32| -> i32 { |
| 98 | + return (x + (*factor.borrow())); |
| 99 | + }, |
| 100 | + |x: f64| -> f64 { |
| 101 | + return (x + ((*factor.borrow()) as f64)); |
| 102 | + }, |
| 103 | + ))); |
| 104 | + assert!((({ apply_int_3((*offset.borrow()).copy_from(), 4,) }) == 7)); |
| 105 | + assert!((({ apply_double_5((*offset.borrow()).copy_from(), 1.5_f64,) }) == 4.5_f64)); |
| 106 | + return 0; |
| 107 | +} |
| 108 | +pub fn __cpp2rust_init_globals() {} |
0 commit comments