Make bytecode crate independent of parser crate.

This commit is contained in:
Windel Bouwman 2019-06-30 11:42:36 +02:00
parent 134e6b2579
commit 57b4d432ef
2 changed files with 27 additions and 10 deletions

View file

@ -5,10 +5,7 @@ authors = ["coolreader18 <33094578+coolreader18@users.noreply.github.com>", "Win
edition = "2018"
[dependencies]
bitflags = "1.1"
rustpython_bytecode = { path = "../bytecode" }
rustpython_parser = { path = "../parser" }
serde = { version = "1.0", features = ["derive"] }
num-complex = { version = "0.2", features = ["serde"] }
num-bigint = { version = "0.2", features = ["serde"] }
log = "0.3"

View file

@ -599,9 +599,9 @@ impl Compiler {
let line_number = self.get_source_line_number();
self.code_object_stack.push(CodeObject::new(
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(),
Varargs::from(&args.kwarg),
compile_varargs(&args.kwarg),
self.source_path.clone().unwrap(),
line_number,
name.to_string(),
@ -1743,7 +1743,7 @@ impl Compiler {
} => {
self.compile_expression(value)?;
self.emit(Instruction::FormatValue {
conversion: *conversion,
conversion: conversion.map(compile_conversion_flag),
spec: spec.clone(),
});
}
@ -1773,7 +1773,7 @@ impl Compiler {
// Low level helper functions:
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();
cur_code_obj.instructions.push(instruction);
cur_code_obj.locations.push(location);
@ -1834,13 +1834,33 @@ fn get_doc(body: &[ast::LocatedStatement]) -> (&[ast::LocatedStatement], Option<
(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)]
mod tests {
use super::Compiler;
use crate::bytecode::CodeObject;
use crate::bytecode::Constant::*;
use crate::bytecode::Instruction::*;
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;
fn compile_exec(source: &str) -> CodeObject {