|
| 1 | +use NativeType; |
| 2 | +use std::rc::Rc; |
| 3 | +use std::ops::Deref; |
| 4 | + |
| 5 | +pub fn print(args: Vec<Rc<NativeType>>) -> NativeType { |
| 6 | + for elem in args { |
| 7 | + // TODO: figure out how python's print vectors |
| 8 | + match elem.deref() { |
| 9 | + &NativeType::NoneType => println!("None"), |
| 10 | + &NativeType::Boolean(ref b)=> { |
| 11 | + if *b { |
| 12 | + println!("True"); |
| 13 | + } else { |
| 14 | + println!("False"); |
| 15 | + } |
| 16 | + }, |
| 17 | + &NativeType::Int(ref x) => println!("{}", x), |
| 18 | + &NativeType::Float(ref x) => println!("{}", x), |
| 19 | + &NativeType::Str(ref x) => println!("{}", x), |
| 20 | + &NativeType::Unicode(ref x) => println!("{}", x), |
| 21 | + _ => panic!("Print for {:?} not implemented yet", elem), |
| 22 | + /* |
| 23 | + List(Vec<NativeType>), |
| 24 | + Tuple(Vec<NativeType>), |
| 25 | + Iter(Vec<NativeType>), // TODO: use Iterator instead |
| 26 | + Code(PyCodeObject), |
| 27 | + Function(Function), |
| 28 | + #[serde(skip_serializing, skip_deserializing)] |
| 29 | + NativeFunction(fn(Vec<NativeType>) -> NativeType ), |
| 30 | + */ |
| 31 | + } |
| 32 | + } |
| 33 | + NativeType::NoneType |
| 34 | +} |
| 35 | + |
| 36 | +pub fn len(args: Vec<Rc<NativeType>>) -> NativeType { |
| 37 | + if args.len() != 1 { |
| 38 | + panic!("len(s) expects exactly one parameter"); |
| 39 | + } |
| 40 | + let len = match args[0].deref() { |
| 41 | + &NativeType::List(ref l) => l.borrow().len(), |
| 42 | + &NativeType::Tuple(ref t) => t.len(), |
| 43 | + &NativeType::Str(ref s) => s.len(), |
| 44 | + _ => panic!("TypeError: object of this type has no len()") |
| 45 | + }; |
| 46 | + NativeType::Int(len as i32) |
| 47 | +} |
0 commit comments