Autoformatting

This commit is contained in:
Windel Bouwman committed 2018-07-15 13:16:00 +02:00
1 parent ac37d015d8
commit b653b96a46
14 files changed
+193 -137

No files matched your search

+19 -10
View File
@@ -1,8 +1,8 @@
use std::str::FromStr;
use std::str::CharIndices;
pub use super::token::Tok;
use std::iter::FromIterator;
use std::collections::HashMap;
use std::iter::FromIterator;
use std::str::CharIndices;
use std::str::FromStr;
pub struct Lexer<'input> {
chars: CharIndices<'input>,
@@ -219,7 +219,6 @@ impl<'input> Lexer<'input> {
if col == l2 {
// TODO: handle wrong indentations
}
}
}
}
@@ -270,9 +269,15 @@ impl<'input> Lexer<'input> {
match self.chr0 {
Some('=') => {
self.next_char();
return Some(Ok((tok_start, Tok::DoubleStarEqual, self.location + 1)));
},
_ => return Some(Ok((tok_start, Tok::DoubleStar, self.location + 1))),
return Some(Ok((
tok_start,
Tok::DoubleStarEqual,
self.location + 1,
)));
}
_ => {
return Some(Ok((tok_start, Tok::DoubleStar, self.location + 1)))
}
}
}
_ => return Some(Ok((tok_start, Tok::Star, self.location + 1))),
@@ -382,7 +387,7 @@ impl<'input> Lexer<'input> {
Some('=') => {
self.next_char();
return Some(Ok((tok_start, Tok::NotEqual, self.location + 1)));
},
}
_ => panic!("Invalid token '!'"),
}
}
@@ -527,10 +532,14 @@ impl<'input> Iterator for Lexer<'input> {
// let mut X = HashMap::new();
// X.insert('=', Tok::Equal);
let token = self.inner_next();
trace!("Lex token {:?}, nesting={:?}, indent stack: {:?}", token, self.nesting, self.indentation_stack);
trace!(
"Lex token {:?}, nesting={:?}, indent stack: {:?}",
token,
self.nesting,
self.indentation_stack
);
token
}
}
#[cfg(test)]
+2 -2
View File
@@ -1,10 +1,10 @@
#[macro_use]
extern crate log;
pub mod ast;
mod lexer;
pub mod parser;
mod python;
pub mod ast;
mod token;
mod lexer;
pub use self::parser::parse;
+3 -3
View File
@@ -1,11 +1,11 @@
use std::error::Error;
use std::path::Path;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use super::python;
use super::ast;
use super::lexer;
use super::python;
pub fn read_file(filename: &Path) -> Result<String, String> {
match File::open(&filename) {
@@ -64,8 +64,8 @@ pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
#[cfg(test)]
mod tests {
use super::parse_program;
use super::ast;
use super::parse_program;
#[test]
fn test_parse_print_hello() {
+5 -2
View File
@@ -58,12 +58,15 @@ fn run_script(script_file: &String) {
}
fn run_shell() {
println!("Welcome to the magnificent Rust Python {} interpreter", crate_version!());
println!(
"Welcome to the magnificent Rust Python {} interpreter",
crate_version!()
);
let mut vm = VirtualMachine::new();
// Read a single line:
loop {
let mut input = String::new();
print!(">>>>> "); // Use 5 items. pypy has 4, cpython has 3.
print!(">>>>> "); // Use 5 items. pypy has 4, cpython has 3.
io::stdout().flush().ok().expect("Could not flush stdout");
io::stdin().read_line(&mut input);
// input = input.trim().to_string();
+23 -10
View File
@@ -1,7 +1,7 @@
// use std::ops::Deref;
use std::io::{self, Write};
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult, PyContext, Executor};
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult};
/*
* Original impl:
@@ -76,22 +76,35 @@ pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
// scope[String::from("print")] = print;
let obj = PyObject::new(PyObjectKind::Module { name: "__builtins__".to_string() }, ctx.type_type.clone());
obj.borrow_mut().dict.insert(
String::from("print"),
PyObject::new(PyObjectKind::RustFunction { function: print }, ctx.type_type.clone()),
);
obj.borrow_mut().dict.insert(
String::from("type"),
let obj = PyObject::new(
PyObjectKind::Module {
name: "__builtins__".to_string(),
},
ctx.type_type.clone(),
);
obj.borrow_mut().dict.insert(
String::from("print"),
PyObject::new(
PyObjectKind::RustFunction { function: print },
ctx.type_type.clone(),
),
);
obj.borrow_mut()
.dict
.insert(String::from("type"), ctx.type_type.clone());
obj.borrow_mut().dict.insert(
String::from("all"),
PyObject::new(PyObjectKind::RustFunction { function: all }, ctx.type_type.clone()),
PyObject::new(
PyObjectKind::RustFunction { function: all },
ctx.type_type.clone(),
),
);
obj.borrow_mut().dict.insert(
String::from("any"),
PyObject::new(PyObjectKind::RustFunction { function: any }, ctx.type_type.clone()),
PyObject::new(
PyObjectKind::RustFunction { function: any },
ctx.type_type.clone(),
),
);
obj
}
+2 -1
View File
@@ -121,7 +121,8 @@ pub enum BlockType {
impl fmt::Debug for CodeObject {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let inst_str = self.instructions.iter()
let inst_str = self.instructions
.iter()
.enumerate()
.map(|(i, inst)| format!("Inst {}: {:?}", i, inst))
.collect::<Vec<_>>()
+26 -26
View File
@@ -16,33 +16,31 @@ pub fn compile(source: &String, mode: Mode) -> Result<CodeObject, String> {
let mut compiler = Compiler::new();
compiler.push_code_object(CodeObject::new());
match mode {
Mode::Exec => {
match parser::parse_program(source) {
Ok(ast) => {
compiler.compile_program(ast);
Ok(compiler.pop_code_object())
}
Err(msg) => Err(msg),
Mode::Exec => match parser::parse_program(source) {
Ok(ast) => {
compiler.compile_program(ast);
Ok(compiler.pop_code_object())
}
}
Mode::Eval => {
match parser::parse_statement(source) {
Ok(statement) => {
let need_none = if let &ast::Statement::Expression { expression: _ } = &statement {
false
} else {
true
};
compiler.compile_statement(statement);
if need_none {
compiler.emit(Instruction::LoadConst { value: bytecode::Constant::None });
}
compiler.emit(Instruction::ReturnValue);
Ok(compiler.pop_code_object())
Err(msg) => Err(msg),
},
Mode::Eval => match parser::parse_statement(source) {
Ok(statement) => {
let need_none = if let &ast::Statement::Expression { expression: _ } = &statement {
false
} else {
true
};
compiler.compile_statement(statement);
if need_none {
compiler.emit(Instruction::LoadConst {
value: bytecode::Constant::None,
});
}
Err(msg) => Err(msg),
compiler.emit(Instruction::ReturnValue);
Ok(compiler.pop_code_object())
}
}
Err(msg) => Err(msg),
},
}
}
@@ -270,7 +268,7 @@ impl Compiler {
ast::Operator::BitXor => bytecode::BinaryOperator::Xor,
ast::Operator::BitAnd => bytecode::BinaryOperator::And,
ast::Operator::Subscript => bytecode::BinaryOperator::Subscript,
ast::Operator::And | ast::Operator::Or => { panic!("Not possible") },
ast::Operator::And | ast::Operator::Or => panic!("Not possible"),
};
self.emit(Instruction::BinaryOperation { op: i });
@@ -324,7 +322,9 @@ impl Compiler {
ast::Operator::BitXor => bytecode::BinaryOperator::Xor,
ast::Operator::BitAnd => bytecode::BinaryOperator::And,
ast::Operator::Subscript => bytecode::BinaryOperator::Subscript,
ast::Operator::And | ast::Operator::Or => { panic!("Implement boolean short circuit?") },
ast::Operator::And | ast::Operator::Or => {
panic!("Implement boolean short circuit?")
}
};
let i = Instruction::BinaryOperation { op: i };
self.emit(i);
+18 -16
View File
@@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::rc::Rc;
use std::fmt;
use std::rc::Rc;
use super::bytecode;
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult, PyContext, Executor};
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult};
#[derive(Clone, Debug)]
pub struct Block {
pub block_type: BlockType,
pub handler: usize, // The destination we should jump to if the block finishes
pub handler: usize, // The destination we should jump to if the block finishes
// level?
}
@@ -18,7 +18,7 @@ pub enum BlockType {
start: bytecode::Label,
end: bytecode::Label,
},
B
B,
}
pub struct Frame {
@@ -29,7 +29,7 @@ pub struct Frame {
blocks: Vec<Block>, // Block frames, for controling loops and exceptions
pub globals: HashMap<String, PyObjectRef>, // Variables
pub locals: HashMap<String, PyObjectRef>, // Variables
pub lasti: usize, // index of last instruction ran
pub lasti: usize, // index of last instruction ran
// cmp_op: Vec<&'a Fn(NativeType, NativeType) -> bool>, // TODO: change compare to a function list
}
@@ -97,22 +97,24 @@ impl Frame {
impl fmt::Debug for Frame {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let stack_str = self.stack
.iter()
.map(|elem| format!("\n > {}", elem.borrow_mut().str()))
.collect::<Vec<_>>()
.join("");
.iter()
.map(|elem| format!("\n > {}", elem.borrow_mut().str()))
.collect::<Vec<_>>()
.join("");
let block_str = self.blocks
.iter()
.map(|elem| format!("\n > {:?}", elem))
.collect::<Vec<_>>()
.join("");
//
.iter()
.map(|elem| format!("\n > {:?}", elem))
.collect::<Vec<_>>()
.join("");
let local_str = self.locals
.iter()
.map(|elem| format!("\n {} = {}", elem.0, elem.1.borrow_mut().str()))
.collect::<Vec<_>>()
.join("");
write!(f, "Frame Object {{ \n Stack:{}\n Blocks:{}\n Locals:{}\n}}", stack_str, block_str, local_str)
write!(
f,
"Frame Object {{ \n Stack:{}\n Blocks:{}\n Locals:{}\n}}",
stack_str, block_str, local_str
)
}
}
+1 -1
View File
@@ -9,11 +9,11 @@ mod builtins;
pub mod bytecode;
pub mod compile;
pub mod eval;
mod frame;
mod objint;
mod objstr;
mod objtype;
pub mod pyobject;
mod vm;
mod frame;
pub use self::vm::VirtualMachine;
+11 -3
View File
@@ -1,4 +1,4 @@
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, Executor};
use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef};
fn str(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
Ok(rt.new_str("todo".to_string()))
@@ -13,10 +13,18 @@ fn set_attr(a: &mut PyObjectRef, name: String, b: PyObjectRef) {
*/
pub fn create_type(type_type: PyObjectRef) -> PyObjectRef {
let typ = PyObject::new(PyObjectKind::Class { name: "int".to_string() }, type_type.clone());
let typ = PyObject::new(
PyObjectKind::Class {
name: "int".to_string(),
},
type_type.clone(),
);
typ.borrow_mut().dict.insert(
"__str__".to_string(),
PyObject::new(PyObjectKind::RustFunction { function: str }, type_type.clone()),
PyObject::new(
PyObjectKind::RustFunction { function: str },
type_type.clone(),
),
);
typ
}
+4 -4
View File
@@ -1,4 +1,4 @@
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, Executor, PyResult};
use super::pyobject::{Executor, PyObject, PyObjectKind, PyObjectRef, PyResult};
fn str_pos(s: &String, p: i32) -> usize {
if p < 0 {
@@ -15,8 +15,8 @@ pub fn subscript(rt: &mut Executor, value: &String, b: PyObjectRef) -> PyResult
match &(*b.borrow()).kind {
&PyObjectKind::Integer { value: ref pos } => {
let idx = str_pos(value, *pos);
Ok(rt.new_str(value[idx..idx+1].to_string()))
},
Ok(rt.new_str(value[idx..idx + 1].to_string()))
}
&PyObjectKind::Slice {
ref start,
ref stop,
@@ -38,7 +38,7 @@ pub fn subscript(rt: &mut Executor, value: &String, b: PyObjectRef) -> PyResult
_ => unimplemented!(),
};
Ok(rt.new_str(value[start2..stop2].to_string()))
},
}
_ => panic!(
"TypeError: indexing type {:?} with index {:?} is not supported (yet?)",
value, b
+3 -2
View File
@@ -9,9 +9,10 @@ use super::pyobject::{PyObject, PyObjectKind, PyObjectRef};
pub fn create_type() -> PyObjectRef {
let typ = PyObject::default().into_ref();
(*typ.borrow_mut()).kind = PyObjectKind::Class { name: "type".to_string() };
(*typ.borrow_mut()).kind = PyObjectKind::Class {
name: "type".to_string(),
};
(*typ.borrow_mut()).typ = Some(typ.clone());
// typ.borrow_mut().dict.insert("__str__".to_string(), PyObject::new(PyObjectKind::RustFunction { function: str }));
typ
}
+17 -9
View File
@@ -1,11 +1,11 @@
use std::rc::Rc;
use std::fmt;
use super::bytecode;
use super::objint;
use super::objtype;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::ops::{Add, Mul, Sub};
use std::rc::Rc;
/* Python objects and references.
@@ -147,9 +147,13 @@ pub enum PyObjectKind {
Function {
code: bytecode::CodeObject,
},
Module { name: String },
Module {
name: String,
},
None,
Class { name: String },
Class {
name: String,
},
RustFunction {
function: RustPyFunc,
},
@@ -209,9 +213,14 @@ impl PyObject {
PyObjectKind::Code { code: _ } => format!("<code>"),
PyObjectKind::Function { code: _ } => format!("<func>"),
PyObjectKind::RustFunction { function: _ } => format!("<rustfunc>"),
PyObjectKind::Module { ref name }=> format!("<module '{}'>", name),
PyObjectKind::Iterator { ref position, ref iterated_obj } => format!(
"<iter pos {} in {}>", position, iterated_obj.borrow_mut().str()
PyObjectKind::Module { ref name } => format!("<module '{}'>", name),
PyObjectKind::Iterator {
ref position,
ref iterated_obj,
} => format!(
"<iter pos {} in {}>",
position,
iterated_obj.borrow_mut().str()
),
_ => {
println!("Not impl {:?}", self);
@@ -253,7 +262,6 @@ impl PyObject {
pub fn into_ref(self) -> PyObjectRef {
Rc::new(RefCell::new(self))
}
}
impl<'a> Add<&'a PyObject> for &'a PyObject {
@@ -372,7 +380,7 @@ impl PartialEq for PyObject {
#[cfg(test)]
mod tests {
use super::{PyObjectKind, PyContext};
use super::{PyContext, PyObjectKind};
#[test]
fn test_add_py_integers() {
+59 -48
View File
@@ -6,19 +6,19 @@
extern crate rustpython_parser;
use std::cell::RefMut;
use std::collections::HashMap;
use std::ops::Deref;
use std::path::Path;
use std::rc::Rc;
use std::cell::RefMut;
use std::ops::Deref;
use self::rustpython_parser::parser;
use super::builtins;
use super::bytecode;
use super::compile;
use super::pyobject::{PyObject, PyObjectKind, PyObjectRef, PyResult, PyContext, Executor};
use super::frame::{Block, BlockType, Frame};
use super::objstr;
use super::frame::{Frame, Block, BlockType};
use super::pyobject::{Executor, PyContext, PyObject, PyObjectKind, PyObjectRef, PyResult};
// use objects::objects;
@@ -130,9 +130,12 @@ impl VirtualMachine {
self.push_value(obj);
None
} else {
let name_error = PyObject::new(PyObjectKind::NameError {
name: name.to_string(),
}, self.get_type());
let name_error = PyObject::new(
PyObjectKind::NameError {
name: name.to_string(),
},
self.get_type(),
);
Some(Err(name_error))
}
}
@@ -232,9 +235,7 @@ impl VirtualMachine {
// TODO:
// self.invoke('__neg__'
match a.kind {
PyObjectKind::Integer { value: ref value1 } => {
Ok(self.ctx.new_int(-*value1))
}
PyObjectKind::Integer { value: ref value1 } => Ok(self.ctx.new_int(-*value1)),
_ => panic!("Not impl {:?}", a),
}
}
@@ -244,7 +245,7 @@ impl VirtualMachine {
Ok(value) => {
self.push_value(value);
None
},
}
Err(value) => Some(Err(value)),
}
}
@@ -293,7 +294,7 @@ impl VirtualMachine {
Ok(value) => {
self.push_value(value);
None
},
}
Err(value) => Some(Err(value)),
}
}
@@ -312,10 +313,8 @@ impl VirtualMachine {
PyObjectKind::Function { ref code } => {
let frame = Frame::new(Rc::new(code.clone()), HashMap::new(), None);
self.run_frame(frame).0
},
PyObjectKind::Class { name: _ } => {
self.new_instance(func_ref.clone(), args)
},
}
PyObjectKind::Class { name: _ } => self.new_instance(func_ref.clone(), args),
_ => {
println!("Not impl {:?}", f);
panic!("Not impl");
@@ -336,7 +335,8 @@ impl VirtualMachine {
match compile::compile(&source, compile::Mode::Exec) {
Ok(bytecode) => {
debug!("Code object: {:?}", bytecode);
let obj = PyObject::new(PyObjectKind::Module { name: name.clone() }, self.get_type());
let obj =
PyObject::new(PyObjectKind::Module { name: name.clone() }, self.get_type());
// As a sort of hack, create a frame and run code in it
let frame = Frame::new(Rc::new(bytecode), HashMap::new(), None);
@@ -390,9 +390,7 @@ impl VirtualMachine {
let obj = match value {
&bytecode::Constant::Integer { ref value } => self.ctx.new_int(*value),
// &bytecode::Constant::Float
&bytecode::Constant::String { ref value } => {
self.new_str(value.clone())
}
&bytecode::Constant::String { ref value } => self.new_str(value.clone()),
&bytecode::Constant::Code { ref code } => {
PyObject::new(PyObjectKind::Code { code: code.clone() }, self.get_type())
}
@@ -405,9 +403,7 @@ impl VirtualMachine {
self.import(name.to_string());
None
}
&bytecode::Instruction::LoadName { ref name } => {
self.load_name(name)
}
&bytecode::Instruction::LoadName { ref name } => self.load_name(name),
&bytecode::Instruction::StoreName { ref name } => {
// take top of stack and assign in scope:
self.store_name(name.clone())
@@ -419,13 +415,15 @@ impl VirtualMachine {
}
&bytecode::Instruction::BuildList { size } => {
let elements = self.pop_multiple(size);
let list_obj = PyObject::new(PyObjectKind::List { elements: elements }, self.get_type());
let list_obj =
PyObject::new(PyObjectKind::List { elements: elements }, self.get_type());
self.push_value(list_obj);
None
}
&bytecode::Instruction::BuildTuple { size } => {
let elements = self.pop_multiple(size);
let list_obj = PyObject::new(PyObjectKind::Tuple { elements: elements }, self.get_type());
let list_obj =
PyObject::new(PyObjectKind::Tuple { elements: elements }, self.get_type());
self.push_value(list_obj);
None
}
@@ -457,42 +455,44 @@ impl VirtualMachine {
let stop = out[1];
let step = if out.len() == 3 { out[2] } else { None };
let obj = PyObject::new(PyObjectKind::Slice { start, stop, step }, self.ctx.type_type.clone());
let obj = PyObject::new(
PyObjectKind::Slice { start, stop, step },
self.ctx.type_type.clone(),
);
self.push_value(obj);
None
}
&bytecode::Instruction::BinaryOperation { ref op } => {
self.execute_binop(op)
}
&bytecode::Instruction::LoadAttr { ref name } => {
self.load_attr(name.to_string())
}
&bytecode::Instruction::UnaryOperation { ref op } => {
self.execute_unop(op)
}
&bytecode::Instruction::CompareOperation { ref op } => {
self.execute_compare(op)
}
&bytecode::Instruction::BinaryOperation { ref op } => self.execute_binop(op),
&bytecode::Instruction::LoadAttr { ref name } => self.load_attr(name.to_string()),
&bytecode::Instruction::UnaryOperation { ref op } => self.execute_unop(op),
&bytecode::Instruction::CompareOperation { ref op } => self.execute_compare(op),
&bytecode::Instruction::ReturnValue => {
let value = self.pop_value();
Some(Ok(value))
}
&bytecode::Instruction::PushBlock { start, end } => {
self.push_block(Block { block_type: BlockType::A { start, end }, handler: 0 });
self.push_block(Block {
block_type: BlockType::A { start, end },
handler: 0,
});
None
},
}
&bytecode::Instruction::PopBlock => {
self.pop_block();
None
}
&bytecode::Instruction::GetIter => {
let iterated_obj = self.pop_value();
let iter_obj = PyObject::new(PyObjectKind::Iterator {
position: 0, iterated_obj: iterated_obj
}, self.ctx.type_type.clone());
let iter_obj = PyObject::new(
PyObjectKind::Iterator {
position: 0,
iterated_obj: iterated_obj,
},
self.ctx.type_type.clone(),
);
self.push_value(iter_obj);
None
},
}
&bytecode::Instruction::ForIter => {
// The top of stack contains the iterator, lets push it forward:
let next_obj: Option<PyObjectRef> = {
@@ -500,7 +500,7 @@ impl VirtualMachine {
let mut ref_mut: RefMut<PyObject> = top_of_stack.deref().borrow_mut();
// We require a mutable pyobject here to update the iterator:
let mut iterator = ref_mut; // &mut PyObject = ref_mut.;
// let () = iterator;
// let () = iterator;
iterator.nxt()
};
@@ -508,15 +508,26 @@ impl VirtualMachine {
match next_obj {
Some(value) => {
self.push_value(value);
},
}
None => {
// Pop iterator from stack:
self.pop_value();
// End of for loop
let end_label = if let Block { block_type: BlockType::A { start, end }, handler } = self.last_block() { *end } else { panic!("Wrong block type") };
let end_label = if let Block {
block_type: BlockType::A { start, end },
handler,
} = self.last_block()
{
*end
} else {
panic!("Wrong block type")
};
self.jump(end_label);
}
};
None
},
}
&bytecode::Instruction::MakeFunction => {
let qualified_name = self.pop_value();
let code = self.pop_value();