Skip to content

Commit 2d0e4ef

Browse files
committed
Make the main program parse from parser and run with vm
1 parent 11557e9 commit 2d0e4ef

File tree

9 files changed

+97
-7
lines changed

9 files changed

+97
-7
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
[package]
22
name = "rustpython"
3-
version = "0.1.0"
3+
version = "0.0.1"
44
authors = ["Windel Bouwman", "Shing Lyu <shing.lyu@gmail.com>"]
55

66
[dependencies]
7+
log="0.4.1"
8+
env_logger="0.5.10"
9+
clap = "2.31.2"
710
rustpython_parser = {path = "parser"}
811
rustpython_vm = {path = "vm/RustPython"}

parser/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ lalrpop="0.15.1"
99

1010
[dependencies]
1111
clap="2.20.0"
12+
env_logger="0.5.10"
1213
lalrpop-util="0.15.1"
13-
regex="0.2.2"
1414
log="0.4.1"
15-
env_logger="0.5.10"
15+
py_code_object = { path = "../py_code_object" }
16+
regex="0.2.2"
1617

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Take an AST and transform it into py_code_object compatiable bytecode
3+
*/
4+
extern crate py_code_object;
5+
6+
use super::ast;
7+
use self::py_code_object::{PyCodeObject};
8+
9+
struct Compiler {
10+
code_object: PyCodeObject,
11+
nxt_label: usize,
12+
}
13+
14+
pub fn compile(p: ast::Program) -> PyCodeObject {
15+
let mut compiler = Compiler::new();
16+
compiler.compile_program(p);
17+
compiler.code_object
18+
}
19+
20+
type Label = usize;
21+
22+
impl Compiler {
23+
fn new() -> Self {
24+
Compiler {
25+
code_object: PyCodeObject::new(),
26+
nxt_label: 0,
27+
}
28+
}
29+
30+
fn compile_program(&mut self, program: ast::Program) {
31+
return;
32+
}
33+
}

parser/src/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ mod ast;
66
mod token;
77
mod lexer;
88
mod compile;
9+
pub mod compile_py_code_object;
910
mod bytecode;
1011
mod builtins;
1112
mod pyobject;
1213
mod vm;
1314

1415
pub use self::parser::parse;
15-
pub use self::compile::compile;
1616
pub use self::vm::evaluate;
1717

1818

parser/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#[macro_use]
22
extern crate log;
33

4-
mod compiler;
4+
pub mod compiler;

parser/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
extern crate clap;
32
#[macro_use]
43
extern crate log;

py_code_object/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ pub struct PyCodeObject {
3333
pub co_varnames: Vec<String>,
3434
}
3535

36+
impl PyCodeObject {
37+
pub fn new() -> PyCodeObject {
38+
PyCodeObject {
39+
co_consts: Vec::<NativeType>::new(),
40+
co_names: Vec::<String>::new(),
41+
co_code: Vec::<(usize, String, Option<usize>)>::new(), //size, name, args
42+
co_varnames: Vec::<String>::new(),
43+
}
44+
}
45+
}
46+
3647
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
3748
pub struct Function {
3849
pub code: PyCodeObject

src/main.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
//extern crate rustpython_parser;
2+
#[macro_use]
3+
extern crate clap;
4+
extern crate env_logger;
5+
#[macro_use]
6+
extern crate log;
7+
extern crate rustpython_parser;
28
extern crate rustpython_vm;
39

10+
use clap::{Arg, App};
11+
use std::path::Path;
12+
use rustpython_parser::compiler;
13+
use rustpython_vm::VirtualMachine;
14+
15+
416
fn main() {
5-
unimplemented!();
17+
env_logger::init();
18+
let matches = App::new("RustPython")
19+
.version(crate_version!())
20+
.author(crate_authors!())
21+
.about("Rust implementation of the Python language")
22+
.arg(Arg::with_name("script")
23+
.required(true)
24+
.index(1))
25+
.arg(Arg::with_name("v")
26+
.short("v")
27+
.multiple(true)
28+
.help("Give the verbosity"))
29+
.get_matches();
30+
31+
// Figure out the filename:
32+
let script_file = matches.value_of("script").unwrap_or("foo");
33+
debug!("Running file {}", script_file);
34+
35+
// Parse an ast from it:
36+
let filepath = Path::new(script_file);
37+
match compiler::parse(filepath) {
38+
Ok(program) => {
39+
debug!("Got ast: {:?}", program);
40+
let bytecode = compiler::compile_py_code_object::compile(program);
41+
debug!("Code object: {:?}", bytecode);
42+
let mut vm = VirtualMachine::new();
43+
vm.run_code(bytecode);
44+
},
45+
Err(msg) => error!("Parsing went horribly wrong: {}", msg),
46+
}
647
}
48+

vm/RustPython/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ impl VirtualMachine {
145145
//while curr_frame.lasti < curr_frame.code.co_code.len() {
146146
let op_code = {
147147
let curr_frame = self.curr_frame();
148+
if curr_frame.code.co_code.len() == 0 { panic!("Trying to run an empty frame. Check if the bytecode is empty"); }
148149
let op_code = curr_frame.code.co_code[curr_frame.lasti].clone();
149150
curr_frame.lasti += 1;
150151
op_code

0 commit comments

Comments
 (0)