|
4 | 4 | extern crate rustpython_parser; |
5 | 5 | extern crate py_code_object; |
6 | 6 | use rustpython_parser::compiler::ast; |
7 | | -use self::py_code_object::{PyCodeObject}; |
| 7 | +use self::py_code_object::{PyCodeObject, NativeType}; |
8 | 8 |
|
9 | 9 | struct Compiler { |
10 | 10 | code_object: PyCodeObject, |
@@ -66,12 +66,33 @@ impl Compiler { |
66 | 66 | // self.emit(Instruction::CallFunction { count: count }); |
67 | 67 | } |
68 | 68 | ast::Expression::Identifier { name } => { |
69 | | - panic!("What to emit?"); |
70 | | - // self.emit(Instruction::LoadName { name }); |
| 69 | + // panic!("What to emit?"); |
| 70 | + let i = self.emit_name(name); |
| 71 | + self.emit("LOAD_NAME".to_string(), Some(i)); |
| 72 | + } |
| 73 | + ast::Expression::Number { value } => { |
| 74 | + // panic!("What to emit?"); |
| 75 | + let i = self.emit_const(value); |
| 76 | + self.emit("LOAD_CONST".to_string(), Some(i)); |
71 | 77 | } |
72 | 78 | _ => { |
73 | 79 | panic!("Not impl {:?}", expression); |
74 | 80 | } |
75 | 81 | } |
76 | 82 | } |
| 83 | + |
| 84 | + fn emit_name(&mut self, name: String) -> usize { |
| 85 | + self.code_object.co_names.push(name); |
| 86 | + // TODO: is this index in vector? |
| 87 | + 0 |
| 88 | + } |
| 89 | + |
| 90 | + fn emit_const(&mut self, value: i32) -> usize { |
| 91 | + self.code_object.co_consts.push(NativeType::Int ( value )); |
| 92 | + 0 |
| 93 | + } |
| 94 | + |
| 95 | + fn emit(&mut self, instruction: String, arg: Option<usize>) { |
| 96 | + self.code_object.co_code.push((0, instruction, arg)); |
| 97 | + } |
77 | 98 | } |
0 commit comments