mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-30 23:27:39 +00:00
Make bytecode crate independent of parser crate.
This commit is contained in:
parent
134e6b2579
commit
57b4d432ef
2 changed files with 27 additions and 10 deletions
|
@ -5,10 +5,7 @@ authors = ["coolreader18 <33094578+coolreader18@users.noreply.github.com>", "Win
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitflags = "1.1"
|
|
||||||
rustpython_bytecode = { path = "../bytecode" }
|
rustpython_bytecode = { path = "../bytecode" }
|
||||||
rustpython_parser = { path = "../parser" }
|
rustpython_parser = { path = "../parser" }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
|
||||||
num-complex = { version = "0.2", features = ["serde"] }
|
num-complex = { version = "0.2", features = ["serde"] }
|
||||||
num-bigint = { version = "0.2", features = ["serde"] }
|
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
|
|
|
@ -599,9 +599,9 @@ impl Compiler {
|
||||||
let line_number = self.get_source_line_number();
|
let line_number = self.get_source_line_number();
|
||||||
self.code_object_stack.push(CodeObject::new(
|
self.code_object_stack.push(CodeObject::new(
|
||||||
args.args.iter().map(|a| a.arg.clone()).collect(),
|
args.args.iter().map(|a| a.arg.clone()).collect(),
|
||||||
Varargs::from(&args.vararg),
|
compile_varargs(&args.vararg),
|
||||||
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
|
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
|
||||||
Varargs::from(&args.kwarg),
|
compile_varargs(&args.kwarg),
|
||||||
self.source_path.clone().unwrap(),
|
self.source_path.clone().unwrap(),
|
||||||
line_number,
|
line_number,
|
||||||
name.to_string(),
|
name.to_string(),
|
||||||
|
@ -1743,7 +1743,7 @@ impl Compiler {
|
||||||
} => {
|
} => {
|
||||||
self.compile_expression(value)?;
|
self.compile_expression(value)?;
|
||||||
self.emit(Instruction::FormatValue {
|
self.emit(Instruction::FormatValue {
|
||||||
conversion: *conversion,
|
conversion: conversion.map(compile_conversion_flag),
|
||||||
spec: spec.clone(),
|
spec: spec.clone(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -1773,7 +1773,7 @@ impl Compiler {
|
||||||
|
|
||||||
// Low level helper functions:
|
// Low level helper functions:
|
||||||
fn emit(&mut self, instruction: Instruction) {
|
fn emit(&mut self, instruction: Instruction) {
|
||||||
let location = self.current_source_location.clone();
|
let location = compile_location(&self.current_source_location);
|
||||||
let cur_code_obj = self.current_code_object();
|
let cur_code_obj = self.current_code_object();
|
||||||
cur_code_obj.instructions.push(instruction);
|
cur_code_obj.instructions.push(instruction);
|
||||||
cur_code_obj.locations.push(location);
|
cur_code_obj.locations.push(location);
|
||||||
|
@ -1834,13 +1834,33 @@ fn get_doc(body: &[ast::LocatedStatement]) -> (&[ast::LocatedStatement], Option<
|
||||||
(body, None)
|
(body, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn compile_location(location: &ast::Location) -> bytecode::Location {
|
||||||
|
bytecode::Location::new(location.get_row(), location.get_column())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile_varargs(varargs: &ast::Varargs) -> bytecode::Varargs {
|
||||||
|
match varargs {
|
||||||
|
ast::Varargs::None => bytecode::Varargs::None,
|
||||||
|
ast::Varargs::Unnamed => bytecode::Varargs::Unnamed,
|
||||||
|
ast::Varargs::Named(param) => bytecode::Varargs::Named(param.arg.clone()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compile_conversion_flag(conversion_flag: ast::ConversionFlag) -> bytecode::ConversionFlag {
|
||||||
|
match conversion_flag {
|
||||||
|
ast::ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii,
|
||||||
|
ast::ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
|
||||||
|
ast::ConversionFlag::Str => bytecode::ConversionFlag::Str,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::Compiler;
|
use super::Compiler;
|
||||||
use crate::bytecode::CodeObject;
|
|
||||||
use crate::bytecode::Constant::*;
|
|
||||||
use crate::bytecode::Instruction::*;
|
|
||||||
use crate::symboltable::make_symbol_table;
|
use crate::symboltable::make_symbol_table;
|
||||||
|
use rustpython_bytecode::bytecode::CodeObject;
|
||||||
|
use rustpython_bytecode::bytecode::Constant::*;
|
||||||
|
use rustpython_bytecode::bytecode::Instruction::*;
|
||||||
use rustpython_parser::parser;
|
use rustpython_parser::parser;
|
||||||
|
|
||||||
fn compile_exec(source: &str) -> CodeObject {
|
fn compile_exec(source: &str) -> CodeObject {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue