Skip to content

Commit e40c844

Browse files
committed
Make bytecode crate independent of parser crate.
1 parent 183415e commit e40c844

File tree

8 files changed

+63
-45
lines changed

8 files changed

+63
-45
lines changed

Cargo.lock

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bytecode/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ bitflags = "1.1"
99
num-bigint = { version = "0.2", features = ["serde"] }
1010
num-complex = { version = "0.2", features = ["serde"] }
1111
serde = { version = "1.0", features = ["derive"] }
12-
rustpython_parser = { path = "../parser" }

bytecode/src/bytecode.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,39 @@
44
use bitflags::bitflags;
55
use num_bigint::BigInt;
66
use num_complex::Complex64;
7-
use rustpython_parser::ast;
87
use serde::{Deserialize, Serialize};
98
use std::collections::{HashMap, HashSet};
109
use std::fmt;
1110

11+
/// Sourcode location.
12+
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
13+
pub struct Location {
14+
row: usize,
15+
column: usize,
16+
}
17+
18+
impl Location {
19+
pub fn new(row: usize, column: usize) -> Self {
20+
Location { row, column }
21+
}
22+
23+
pub fn get_row(&self) -> usize {
24+
self.row
25+
}
26+
27+
pub fn get_column(&self) -> usize {
28+
self.column
29+
}
30+
}
31+
1232
/// Primary container of a single code object. Each python function has
1333
/// a codeobject. Also a module has a codeobject.
1434
#[derive(Clone, PartialEq, Serialize, Deserialize)]
1535
pub struct CodeObject {
1636
pub instructions: Vec<Instruction>,
1737
/// Jump targets.
1838
pub label_map: HashMap<Label, usize>,
19-
pub locations: Vec<ast::Location>,
39+
pub locations: Vec<Location>,
2040
pub arg_names: Vec<String>, // Names of positional arguments
2141
pub varargs: Varargs, // *args or *
2242
pub kwonlyarg_names: Vec<String>,
@@ -45,6 +65,17 @@ pub enum NameScope {
4565
Global,
4666
}
4767

68+
/// Transforms a value prior to formatting it.
69+
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
70+
pub enum ConversionFlag {
71+
/// Converts by calling `str(<value>)`.
72+
Str,
73+
/// Converts by calling `ascii(<value>)`.
74+
Ascii,
75+
/// Converts by calling `repr(<value>)`.
76+
Repr,
77+
}
78+
4879
/// A Single bytecode instruction.
4980
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5081
pub enum Instruction {
@@ -181,7 +212,7 @@ pub enum Instruction {
181212
},
182213
Unpack,
183214
FormatValue {
184-
conversion: Option<ast::ConversionFlag>,
215+
conversion: Option<ConversionFlag>,
185216
spec: String,
186217
},
187218
PopException,
@@ -436,23 +467,3 @@ impl fmt::Debug for CodeObject {
436467
)
437468
}
438469
}
439-
440-
impl From<ast::Varargs> for Varargs {
441-
fn from(varargs: ast::Varargs) -> Varargs {
442-
match varargs {
443-
ast::Varargs::None => Varargs::None,
444-
ast::Varargs::Unnamed => Varargs::Unnamed,
445-
ast::Varargs::Named(param) => Varargs::Named(param.arg),
446-
}
447-
}
448-
}
449-
450-
impl<'a> From<&'a ast::Varargs> for Varargs {
451-
fn from(varargs: &'a ast::Varargs) -> Varargs {
452-
match varargs {
453-
ast::Varargs::None => Varargs::None,
454-
ast::Varargs::Unnamed => Varargs::Unnamed,
455-
ast::Varargs::Named(ref param) => Varargs::Named(param.arg.clone()),
456-
}
457-
}
458-
}

compiler/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ authors = ["coolreader18 <33094578+coolreader18@users.noreply.github.com>", "Win
55
edition = "2018"
66

77
[dependencies]
8-
bitflags = "1.1"
98
rustpython_bytecode = { path = "../bytecode" }
109
rustpython_parser = { path = "../parser" }
11-
serde = { version = "1.0", features = ["derive"] }
1210
num-complex = { version = "0.2", features = ["serde"] }
13-
num-bigint = { version = "0.2", features = ["serde"] }
1411
log = "0.3"

compiler/src/compile.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,9 @@ impl Compiler {
599599
let line_number = self.get_source_line_number();
600600
self.code_object_stack.push(CodeObject::new(
601601
args.args.iter().map(|a| a.arg.clone()).collect(),
602-
Varargs::from(&args.vararg),
602+
compile_varargs(&args.vararg),
603603
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
604-
Varargs::from(&args.kwarg),
604+
compile_varargs(&args.kwarg),
605605
self.source_path.clone().unwrap(),
606606
line_number,
607607
name.to_string(),
@@ -1743,7 +1743,7 @@ impl Compiler {
17431743
} => {
17441744
self.compile_expression(value)?;
17451745
self.emit(Instruction::FormatValue {
1746-
conversion: *conversion,
1746+
conversion: conversion.map(compile_conversion_flag),
17471747
spec: spec.clone(),
17481748
});
17491749
}
@@ -1773,7 +1773,7 @@ impl Compiler {
17731773

17741774
// Low level helper functions:
17751775
fn emit(&mut self, instruction: Instruction) {
1776-
let location = self.current_source_location.clone();
1776+
let location = compile_location(&self.current_source_location);
17771777
let cur_code_obj = self.current_code_object();
17781778
cur_code_obj.instructions.push(instruction);
17791779
cur_code_obj.locations.push(location);
@@ -1834,13 +1834,33 @@ fn get_doc(body: &[ast::LocatedStatement]) -> (&[ast::LocatedStatement], Option<
18341834
(body, None)
18351835
}
18361836

1837+
fn compile_location(location: &ast::Location) -> bytecode::Location {
1838+
bytecode::Location::new(location.get_row(), location.get_column())
1839+
}
1840+
1841+
fn compile_varargs(varargs: &ast::Varargs) -> bytecode::Varargs {
1842+
match varargs {
1843+
ast::Varargs::None => bytecode::Varargs::None,
1844+
ast::Varargs::Unnamed => bytecode::Varargs::Unnamed,
1845+
ast::Varargs::Named(param) => bytecode::Varargs::Named(param.arg.clone()),
1846+
}
1847+
}
1848+
1849+
fn compile_conversion_flag(conversion_flag: ast::ConversionFlag) -> bytecode::ConversionFlag {
1850+
match conversion_flag {
1851+
ast::ConversionFlag::Ascii => bytecode::ConversionFlag::Ascii,
1852+
ast::ConversionFlag::Repr => bytecode::ConversionFlag::Repr,
1853+
ast::ConversionFlag::Str => bytecode::ConversionFlag::Str,
1854+
}
1855+
}
1856+
18371857
#[cfg(test)]
18381858
mod tests {
18391859
use super::Compiler;
1840-
use crate::bytecode::CodeObject;
1841-
use crate::bytecode::Constant::*;
1842-
use crate::bytecode::Instruction::*;
18431860
use crate::symboltable::make_symbol_table;
1861+
use rustpython_bytecode::bytecode::CodeObject;
1862+
use rustpython_bytecode::bytecode::Constant::*;
1863+
use rustpython_bytecode::bytecode::Instruction::*;
18441864
use rustpython_parser::parser;
18451865

18461866
fn compile_exec(source: &str) -> CodeObject {

vm/src/eval.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate rustpython_parser;
2-
31
use crate::compile;
42
use crate::frame::Scope;
53
use crate::pyobject::PyResult;

vm/src/frame.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use std::cell::RefCell;
22
use std::fmt;
33
use std::rc::Rc;
44

5-
use rustpython_parser::ast;
6-
75
use crate::builtins;
86
use crate::bytecode;
97
use crate::function::PyFuncArgs;
@@ -865,7 +863,7 @@ impl Frame {
865863
Ok(None)
866864
}
867865
bytecode::Instruction::FormatValue { conversion, spec } => {
868-
use ast::ConversionFlag::*;
866+
use bytecode::ConversionFlag::*;
869867
let value = match conversion {
870868
Some(Str) => vm.to_str(&self.pop_value())?.into_object(),
871869
Some(Repr) => vm.to_repr(&self.pop_value())?.into_object(),
@@ -1277,7 +1275,7 @@ impl Frame {
12771275
Ok(None)
12781276
}
12791277

1280-
pub fn get_lineno(&self) -> ast::Location {
1278+
pub fn get_lineno(&self) -> bytecode::Location {
12811279
self.code.locations[*self.lasti.borrow()].clone()
12821280
}
12831281

vm/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ extern crate log;
2323
extern crate maplit;
2424
// extern crate env_logger;
2525

26-
extern crate rustpython_parser;
2726
#[macro_use]
2827
extern crate rustpython_derive;
2928

0 commit comments

Comments
 (0)