Mercurial > python-compiler.rs
comparison src/ast_convert.rs @ 87:624393ed3b0b
Don’t cast anything to str, except Num for now.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 22 Jun 2016 23:05:24 +0100 |
parents | a7b1db623f41 |
children | 5923cd4bfc36 |
comparison
equal
deleted
inserted
replaced
86:a7b1db623f41 | 87:624393ed3b0b |
---|---|
2 | 2 |
3 use cpython::{Python, PyObject, PyBool, PyResult}; | 3 use cpython::{Python, PyObject, PyBool, PyResult}; |
4 use cpython::ObjectProtocol; //for call method | 4 use cpython::ObjectProtocol; //for call method |
5 | 5 |
6 fn get_str(py: Python, object: PyObject) -> String { | 6 fn get_str(py: Python, object: PyObject) -> String { |
7 object.extract(py).unwrap() | |
8 } | |
9 | |
10 fn convert_to_str(py: Python, object: PyObject) -> String { | |
7 let pystring = object.str(py).unwrap(); | 11 let pystring = object.str(py).unwrap(); |
8 let mut string = pystring.to_string(py).unwrap(); | 12 let mut string = pystring.to_string(py).unwrap(); |
9 string.to_mut().to_string() | 13 string.to_mut().to_string() |
10 } | 14 } |
11 | 15 |
382 } | 386 } |
383 }; | 387 }; |
384 expr::NameConstant(constant) | 388 expr::NameConstant(constant) |
385 } else if is_instance(&ast, &num_type) { | 389 } else if is_instance(&ast, &num_type) { |
386 let n = ast.getattr(py, "n").unwrap(); | 390 let n = ast.getattr(py, "n").unwrap(); |
387 let n = get_str(py, n); | 391 // FIXME: we should convert these to either bigint, double or complex. |
392 // TODO: find a suitable bigint library. | |
393 let n = convert_to_str(py, n); | |
388 expr::Num(n) | 394 expr::Num(n) |
389 } else if is_instance(&ast, &str_type) { | 395 } else if is_instance(&ast, &str_type) { |
390 let s = ast.getattr(py, "s").unwrap(); | 396 let s = ast.getattr(py, "s").unwrap(); |
391 let s = get_str(py, s); | 397 let s = get_str(py, s); |
392 expr::Str(s) | 398 expr::Str(s) |