comparison src/ast_convert.rs @ 2:5fc7c2790d8c

Add class support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 May 2016 02:31:55 +0100
parents b90e49ab734b
children 326d7f2a94d4
comparison
equal deleted inserted replaced
1:b90e49ab734b 2:5fc7c2790d8c
186 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap(); 186 return isinstance.call(py, (object, type_), None).unwrap().is_true(py).unwrap();
187 }; 187 };
188 188
189 let ast_module = py.import("ast").unwrap(); 189 let ast_module = py.import("ast").unwrap();
190 let ast_type = ast_module.get(py, "AST").unwrap(); 190 let ast_type = ast_module.get(py, "AST").unwrap();
191 let class_def_type = ast_module.get(py, "ClassDef").unwrap();
191 let function_def_type = ast_module.get(py, "FunctionDef").unwrap(); 192 let function_def_type = ast_module.get(py, "FunctionDef").unwrap();
192 let global_type = ast_module.get(py, "Global").unwrap(); 193 let global_type = ast_module.get(py, "Global").unwrap();
193 let assign_type = ast_module.get(py, "Assign").unwrap(); 194 let assign_type = ast_module.get(py, "Assign").unwrap();
194 let return_type = ast_module.get(py, "Return").unwrap(); 195 let return_type = ast_module.get(py, "Return").unwrap();
195 let import_from_type = ast_module.get(py, "ImportFrom").unwrap(); 196 let import_from_type = ast_module.get(py, "ImportFrom").unwrap();
210 } 211 }
211 map 212 map
212 }; 213 };
213 */ 214 */
214 215
215 if is_instance(&ast, &function_def_type) { 216 if is_instance(&ast, &class_def_type) {
217 let name = ast.getattr(py, "name").unwrap();
218 let bases = ast.getattr(py, "bases").unwrap();
219 //let keywords = ast.getattr(py, "keywords").unwrap();
220 let body = ast.getattr(py, "body").unwrap();
221 //let decorator_list = ast.getattr(py, "decorator_list").unwrap();
222
223 let name = get_str(py, name);
224
225 let mut nodes = vec!();
226 for name_node in bases.iter(py).unwrap() {
227 let name_node = name_node.unwrap();
228 let name_node = parse_expr(py, name_node);
229 nodes.push(name_node);
230 }
231
232 let mut statements = vec!();
233 for statement in body.iter(py).unwrap() {
234 let statement = statement.unwrap();
235 let statement = parse_statement(py, statement);
236 statements.push(statement);
237 }
238
239 Statement::ClassDef(name, nodes, statements)
240 } else if is_instance(&ast, &function_def_type) {
216 let name = ast.getattr(py, "name").unwrap(); 241 let name = ast.getattr(py, "name").unwrap();
217 let args = ast.getattr(py, "args").unwrap(); 242 let args = ast.getattr(py, "args").unwrap();
218 let body = ast.getattr(py, "body").unwrap(); 243 let body = ast.getattr(py, "body").unwrap();
219 244
220 let name = get_str(py, name); 245 let name = get_str(py, name);