|
| 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::Seek; |
| 7 | +use std::io::{Read, Write}; |
| 8 | +use std::os::fd::AsFd; |
| 9 | +use std::rc::{Rc, Weak}; |
| 10 | +pub fn apply_0(fn_: impl Fn(i32) -> i32, x: i32) -> i32 { |
| 11 | + let fn_: Value<_> = Rc::new(RefCell::new(fn_)); |
| 12 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 13 | + return ({ |
| 14 | + let _x: i32 = (*x.borrow()); |
| 15 | + (*fn_.borrow())(_x) |
| 16 | + }) |
| 17 | + .clone(); |
| 18 | +} |
| 19 | +pub fn apply_1(fn_: impl Fn(i32) -> i32, x: i32) -> i32 { |
| 20 | + let fn_: Value<_> = Rc::new(RefCell::new(fn_)); |
| 21 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 22 | + return ({ |
| 23 | + let _x: i32 = (*x.borrow()); |
| 24 | + (*fn_.borrow())(_x) |
| 25 | + }) |
| 26 | + .clone(); |
| 27 | +} |
| 28 | +pub fn main() { |
| 29 | + std::process::exit(main_0()); |
| 30 | +} |
| 31 | +fn main_0() -> i32 { |
| 32 | + let base: Value<i32> = Rc::new(RefCell::new(10)); |
| 33 | + let add_base: Value<_> = Rc::new(RefCell::new( |
| 34 | + (|x: i32| { |
| 35 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 36 | + return ((*x.borrow()) + (*base.borrow())); |
| 37 | + }), |
| 38 | + )); |
| 39 | + assert!( |
| 40 | + (({ |
| 41 | + let _fn: _ = (*add_base.borrow()).clone(); |
| 42 | + let _x: i32 = 5; |
| 43 | + apply_0(_fn, _x) |
| 44 | + }) == 15) |
| 45 | + ); |
| 46 | + (*base.borrow_mut()) = 100; |
| 47 | + assert!( |
| 48 | + (({ |
| 49 | + let _fn: _ = (*add_base.borrow()).clone(); |
| 50 | + let _x: i32 = 5; |
| 51 | + apply_0(_fn, _x) |
| 52 | + }) == 105) |
| 53 | + ); |
| 54 | + let factor: Value<i32> = Rc::new(RefCell::new(3)); |
| 55 | + let scale: Value<_> = Rc::new(RefCell::new( |
| 56 | + (|x: i32| { |
| 57 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 58 | + return ((*x.borrow()) * (*factor.borrow())); |
| 59 | + }), |
| 60 | + )); |
| 61 | + assert!( |
| 62 | + (({ |
| 63 | + let _fn: _ = (*scale.borrow()).clone(); |
| 64 | + let _x: i32 = 4; |
| 65 | + apply_1(_fn, _x) |
| 66 | + }) == 12) |
| 67 | + ); |
| 68 | + return 0; |
| 69 | +} |
0 commit comments