Skip to content

Commit 3f98e3c

Browse files
committed
Reverse dependency between vm and cpython bytecode adapter
1 parent 0a43d24 commit 3f98e3c

File tree

14 files changed

+1382
-866
lines changed

14 files changed

+1382
-866
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py_code_object/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ env_logger = "0.3"
99
serde = "0.8.22"
1010
serde_derive = "0.8"
1111
serde_json = "0.8"
12+
rustpython_vm = {path = "../vm"}

py_code_object/src/convert.rs

Lines changed: 461 additions & 2 deletions
Large diffs are not rendered by default.

src/compile.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
* Take an AST and transform it into bytecode
33
*/
44

5-
use super::ast;
6-
use super::bytecode::{self, CodeObject, Instruction};
5+
extern crate rustpython_parser;
6+
extern crate rustpython_vm;
7+
8+
use rustpython_parser::ast;
9+
use rustpython_vm::bytecode::{self, CodeObject, Instruction};
710

811
struct Compiler {
912
code_object: CodeObject,

src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ extern crate log;
77
extern crate rustpython_parser;
88
extern crate rustpython_vm;
99

10-
mod compile_py_code_object;
10+
// mod compile_py_code_object;
11+
mod compile;
1112
use clap::{Arg, App};
1213
use std::path::Path;
1314
use rustpython_parser::parse;
14-
use rustpython_vm::VirtualMachine;
15+
use rustpython_vm::evaluate;
1516

1617

1718
fn main() {
@@ -38,10 +39,9 @@ fn main() {
3839
match parse(filepath) {
3940
Ok(program) => {
4041
debug!("Got ast: {:?}", program);
41-
let bytecode = compile_py_code_object::compile(program);
42+
let bytecode = compile::compile(program);
4243
debug!("Code object: {:?}", bytecode);
43-
let mut vm = VirtualMachine::new();
44-
vm.run_code(bytecode);
44+
evaluate(bytecode);
4545
},
4646
Err(msg) => error!("Parsing went horribly wrong: {}", msg),
4747
}

vm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,3 @@ authors = ["Shing Lyu <shing.lyu@gmail.com>"]
66
[dependencies]
77
log = "0.3"
88
env_logger = "0.3"
9-
serde = "0.8.22"
10-
serde_derive = "0.8"
11-
serde_json = "0.8"
12-
py_code_object = { path = "../py_code_object" }

vm/src/builtins.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use NativeType;
2-
use std::rc::Rc;
31
use std::ops::Deref;
2+
use std::io::{self, Write};
43

4+
use super::pyobject::PyObjectRef;
5+
6+
7+
/*
8+
* Original impl:
59
pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
610
for elem in args {
711
// TODO: figure out how python's print vectors
@@ -32,7 +36,20 @@ pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
3236
}
3337
NativeType::NoneType
3438
}
39+
*/
40+
41+
pub fn print(args: Vec<PyObjectRef>) {
42+
// println!("Woot: {:?}", args);
43+
trace!("print called with {:?}", args);
44+
for a in args {
45+
print!("{} ", a.borrow_mut().str());
46+
}
47+
println!();
48+
io::stdout().flush().unwrap();
49+
}
3550

51+
/*
52+
* TODO
3653
pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
3754
if args.len() != 1 {
3855
panic!("len(s) expects exactly one parameter");
@@ -45,3 +62,12 @@ pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
4562
};
4663
NativeType::Int(len as i32)
4764
}
65+
*/
66+
67+
pub fn fill_scope() {
68+
// scope[String::from("print")] = print;
69+
}
70+
71+
fn any() {}
72+
73+
fn all() {}

vm/src/builtins2.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

vm/src/bytecode.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let call_function = 0x64;
1212
*/
1313
use std::collections::HashMap;
1414

15-
#[derive(Debug)]
15+
#[derive(Debug, Clone)]
1616
pub struct CodeObject {
1717
pub instructions: Vec<Instruction>,
1818
pub label_map: HashMap<Label, usize>,
@@ -29,7 +29,7 @@ impl CodeObject {
2929

3030
pub type Label = usize;
3131

32-
#[derive(Debug)]
32+
#[derive(Debug, Clone)]
3333
pub enum Instruction {
3434
LoadName { name: String },
3535
StoreName { name: String },
@@ -43,6 +43,7 @@ pub enum Instruction {
4343
Break,
4444
Jump { target: Label },
4545
JumpIf { target: Label },
46+
MakeFunction { code: CodeObject },
4647
CallFunction { count: usize },
4748
ForIter,
4849
ReturnValue,
@@ -53,14 +54,14 @@ pub enum Instruction {
5354
BuildMap { size: usize },
5455
}
5556

56-
#[derive(Debug)]
57+
#[derive(Debug, Clone)]
5758
pub enum Constant {
58-
Integer { value: i32 },
59+
Integer { value: i32 }, // TODO: replace by arbitrary big int math.
5960
// TODO: Float { value: f64 },
6061
String { value: String },
6162
}
6263

63-
#[derive(Debug)]
64+
#[derive(Debug, Clone)]
6465
pub enum BinaryOperator {
6566
Power,
6667
Multiply,
@@ -77,7 +78,7 @@ pub enum BinaryOperator {
7778
Or,
7879
}
7980

80-
#[derive(Debug)]
81+
#[derive(Debug, Clone)]
8182
pub enum UnaryOperator {
8283
Not,
8384
Minus,

0 commit comments

Comments
 (0)