|
| 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 | +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] |
| 10 | +#[byte_size(8)] |
| 11 | +pub struct S { |
| 12 | + #[offset(0)] |
| 13 | + pub x: i32, |
| 14 | + #[offset(4)] |
| 15 | + pub y: i32, |
| 16 | +} |
| 17 | +pub fn main() { |
| 18 | + __cpp2rust_init_globals(); |
| 19 | + std::process::exit(main_0()); |
| 20 | +} |
| 21 | +fn main_0() -> i32 { |
| 22 | + let base: Value<i32> = Rc::new(RefCell::new(10)); |
| 23 | + let add_base: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(lambda!( |
| 24 | + { |
| 25 | + let base: Ptr<i32> = base.as_pointer(); |
| 26 | + }, |
| 27 | + |x: i32| -> i32 { |
| 28 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 29 | + return ((*x.borrow()) + (base.read())); |
| 30 | + } |
| 31 | + ))); |
| 32 | + assert!((({ (*add_base.borrow()).call(5,) }) == 15)); |
| 33 | + (*base.borrow_mut()) = 100; |
| 34 | + assert!((({ (*add_base.borrow()).call(5,) }) == 105)); |
| 35 | + let s: Value<S> = Rc::new(RefCell::new(S { x: 1, y: 2 })); |
| 36 | + let sum: Value<FnPtr<fn() -> i32>> = Rc::new(RefCell::new(lambda!( |
| 37 | + { |
| 38 | + let s: Ptr<S> = s.as_pointer(); |
| 39 | + }, |
| 40 | + || -> i32 { |
| 41 | + return (s.with(|__s| __s.x) + s.with(|__s| __s.y)); |
| 42 | + } |
| 43 | + ))); |
| 44 | + assert!((({ (*sum.borrow()).call() }) == 3)); |
| 45 | + (*s.borrow_mut()).x = 50; |
| 46 | + assert!((({ (*sum.borrow()).call() }) == 52)); |
| 47 | + let counter: Value<i32> = Rc::new(RefCell::new(0)); |
| 48 | + let bump: Value<FnPtr<fn()>> = Rc::new(RefCell::new(lambda!( |
| 49 | + { |
| 50 | + let counter: Ptr<i32> = counter.as_pointer(); |
| 51 | + }, |
| 52 | + || { |
| 53 | + counter.with_mut(|__v| __v.postfix_inc()); |
| 54 | + } |
| 55 | + ))); |
| 56 | + ({ (*bump.borrow()).call() }); |
| 57 | + ({ (*bump.borrow()).call() }); |
| 58 | + assert!(((*counter.borrow()) == 2)); |
| 59 | + let arr: Value<Box<[u16]>> = Rc::new(RefCell::new(Box::new([3_u16, 1_u16, 2_u16, 0_u16]))); |
| 60 | + let swap: Value<FnPtr<fn(usize, usize)>> = Rc::new(RefCell::new(lambda!( |
| 61 | + { |
| 62 | + let arr: Ptr<u16> = arr.as_pointer(); |
| 63 | + }, |
| 64 | + |i: usize, j: usize| { |
| 65 | + let i: Value<usize> = Rc::new(RefCell::new(i)); |
| 66 | + let j: Value<usize> = Rc::new(RefCell::new(j)); |
| 67 | + let t: Value<u16> = Rc::new(RefCell::new((elem!((arr), (*j.borrow())).read()))); |
| 68 | + elem!((arr), (*j.borrow())).write({ (elem!((arr), (*i.borrow())).read()) }); |
| 69 | + elem!((arr), (*i.borrow())).write((*t.borrow())); |
| 70 | + } |
| 71 | + ))); |
| 72 | + ({ (*swap.borrow()).call(0_usize, 3_usize) }); |
| 73 | + assert!((((*arr.borrow())[(0) as usize] as i32) == 0)); |
| 74 | + assert!((((*arr.borrow())[(3) as usize] as i32) == 3)); |
| 75 | + let total: Value<i32> = Rc::new(RefCell::new(0)); |
| 76 | + let add: Value<FnPtr<fn(i32)>> = Rc::new(RefCell::new(lambda!( |
| 77 | + { |
| 78 | + let t: Ptr<i32> = total.as_pointer(); |
| 79 | + }, |
| 80 | + |x: i32| { |
| 81 | + let x: Value<i32> = Rc::new(RefCell::new(x)); |
| 82 | + { |
| 83 | + let _ptr = t.clone(); |
| 84 | + _ptr.write(_ptr.read() + { (*x.borrow()) }) |
| 85 | + }; |
| 86 | + } |
| 87 | + ))); |
| 88 | + ({ (*add.borrow()).call(2) }); |
| 89 | + ({ (*add.borrow()).call(3) }); |
| 90 | + assert!(((*total.borrow()) == 5)); |
| 91 | + let set_y: Value<FnPtr<fn(i32)>> = Rc::new(RefCell::new(lambda!( |
| 92 | + { |
| 93 | + let y: Ptr<i32> = field_ptr!(s.as_pointer(), y); |
| 94 | + }, |
| 95 | + |v: i32| { |
| 96 | + let v: Value<i32> = Rc::new(RefCell::new(v)); |
| 97 | + y.write({ (*v.borrow()) }); |
| 98 | + } |
| 99 | + ))); |
| 100 | + ({ (*set_y.borrow()).call(9) }); |
| 101 | + assert!(({ (*s.borrow()).y } == 9)); |
| 102 | + return 0; |
| 103 | +} |
| 104 | +pub fn __cpp2rust_init_globals() {} |
0 commit comments