Skip to content

Commit 2a67f22

Browse files
committed
Try to emit proper bytecode
1 parent 0111a83 commit 2a67f22

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/compile_py_code_object.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
extern crate rustpython_parser;
55
extern crate py_code_object;
66
use rustpython_parser::compiler::ast;
7-
use self::py_code_object::{PyCodeObject};
7+
use self::py_code_object::{PyCodeObject, NativeType};
88

99
struct Compiler {
1010
code_object: PyCodeObject,
@@ -66,12 +66,33 @@ impl Compiler {
6666
// self.emit(Instruction::CallFunction { count: count });
6767
}
6868
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));
7177
}
7278
_ => {
7379
panic!("Not impl {:?}", expression);
7480
}
7581
}
7682
}
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+
}
7798
}

0 commit comments

Comments
 (0)