mirror of
https://github.com/RustPython/Parser.git
synced 2025-07-16 01:25:25 +00:00
40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
use crate::compile::Label;
|
|
use rustpython_bytecode::bytecode::{CodeObject, Instruction, Location};
|
|
|
|
pub trait OutputStream: From<CodeObject> + Into<CodeObject> {
|
|
/// Output an instruction
|
|
fn emit(&mut self, instruction: Instruction, location: Location);
|
|
/// Set a label on an instruction
|
|
fn set_label(&mut self, label: Label);
|
|
/// Mark the inner CodeObject as a generator
|
|
fn mark_generator(&mut self);
|
|
}
|
|
|
|
pub struct CodeObjectStream {
|
|
code: CodeObject,
|
|
}
|
|
|
|
impl From<CodeObject> for CodeObjectStream {
|
|
fn from(code: CodeObject) -> Self {
|
|
CodeObjectStream { code }
|
|
}
|
|
}
|
|
impl From<CodeObjectStream> for CodeObject {
|
|
fn from(stream: CodeObjectStream) -> Self {
|
|
stream.code
|
|
}
|
|
}
|
|
|
|
impl OutputStream for CodeObjectStream {
|
|
fn emit(&mut self, instruction: Instruction, location: Location) {
|
|
self.code.instructions.push(instruction);
|
|
self.code.locations.push(location);
|
|
}
|
|
fn set_label(&mut self, label: Label) {
|
|
let position = self.code.instructions.len();
|
|
self.code.label_map.insert(label, position);
|
|
}
|
|
fn mark_generator(&mut self) {
|
|
self.code.is_generator = true;
|
|
}
|
|
}
|