Skip to content

Commit e6493ec

Browse files
committed
Add new_instance method
1 parent 66ff3f0 commit e6493ec

File tree

14 files changed

+291
-146
lines changed

14 files changed

+291
-146
lines changed

parser/src/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,8 @@ pub enum Comparison {
153153
LessOrEqual,
154154
Greater,
155155
GreaterOrEqual,
156+
In,
157+
NotIn,
158+
Is,
159+
IsNot,
156160
}

parser/src/python.lalrpop

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ CompOp: ast::Comparison = {
133133
"<=" => ast::Comparison::LessOrEqual,
134134
">" => ast::Comparison::Greater,
135135
">=" => ast::Comparison::GreaterOrEqual,
136+
"in" => ast::Comparison::In,
137+
"not" "in" => ast::Comparison::NotIn,
138+
"is" => ast::Comparison::Is,
139+
"is" "not" => ast::Comparison::IsNot,
136140
};
137141

138142
Expression: ast::Expression = {
@@ -280,20 +284,22 @@ extern {
280284
"<=" => lexer::Tok::LessEqual,
281285
">" => lexer::Tok::Greater,
282286
">=" => lexer::Tok::GreaterEqual,
287+
"as" => lexer::Tok::As,
283288
"assert" => lexer::Tok::Assert,
284-
"import" => lexer::Tok::Import,
285289
"break" => lexer::Tok::Break,
290+
"class" => lexer::Tok::Class,
286291
"continue" => lexer::Tok::Break,
287-
"return" => lexer::Tok::Return,
288-
"pass" => lexer::Tok::Pass,
289-
"if" => lexer::Tok::If,
290-
"while" => lexer::Tok::While,
292+
"def" => lexer::Tok::Def,
291293
"for" => lexer::Tok::For,
294+
"if" => lexer::Tok::If,
292295
"in" => lexer::Tok::In,
296+
"is" => lexer::Tok::Is,
297+
"import" => lexer::Tok::Import,
298+
"not" => lexer::Tok::Not,
299+
"pass" => lexer::Tok::Pass,
300+
"return" => lexer::Tok::Return,
301+
"while" => lexer::Tok::While,
293302
"with" => lexer::Tok::With,
294-
"as" => lexer::Tok::As,
295-
"def" => lexer::Tok::Def,
296-
"class" => lexer::Tok::Class,
297303
"True" => lexer::Tok::True,
298304
"False" => lexer::Tok::False,
299305
"None" => lexer::Tok::None,

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn run_script(script_file: &String) {
4848
match parse(filepath) {
4949
Ok(program) => {
5050
debug!("Got ast: {:?}", program);
51-
let bytecode = compile::compile(program);
51+
let bytecode = compile::compile(program, compile::Mode::Exec);
5252
debug!("Code object: {:?}", bytecode);
5353
vm.evaluate(bytecode);
5454
}

vm/src/builtins.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn print(args: Vec<Rc<NativeType>>) -> NativeType {
3737
}
3838
*/
3939

40-
pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, PyObjectRef> {
40+
pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
4141
// println!("Woot: {:?}", args);
4242
trace!("print called with {:?}", args);
4343
for a in args {
@@ -48,6 +48,16 @@ pub fn print(rt: &mut Executor, args: Vec<PyObjectRef>) -> Result<PyObjectRef, P
4848
Ok(rt.get_none())
4949
}
5050

51+
pub fn compile(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
52+
// TODO
53+
Ok(rt.new_bool(true))
54+
}
55+
56+
pub fn locals(rt: &mut Executor, args: Vec<PyObjectRef>) -> PyResult {
57+
// TODO
58+
Ok(rt.new_bool(true))
59+
}
60+
5161
/*
5262
* TODO
5363
pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
@@ -66,7 +76,7 @@ pub fn len(args: Vec<Rc<NativeType>>) -> NativeType {
6676

6777
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
6878
// scope[String::from("print")] = print;
69-
let obj = PyObject::new(PyObjectKind::Module, ctx.type_type.clone());
79+
let obj = PyObject::new(PyObjectKind::Module { name: "__builtins__".to_string() }, ctx.type_type.clone());
7080
obj.borrow_mut().dict.insert(
7181
String::from("print"),
7282
PyObject::new(PyObjectKind::RustFunction { function: print }, ctx.type_type.clone()),

vm/src/bytecode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum ComparisonOperator {
8181
In,
8282
NotIn,
8383
Is,
84-
NotIs,
84+
IsNot,
8585
}
8686

8787
#[derive(Debug, Clone)]

vm/src/compile.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ struct Compiler {
1212
nxt_label: usize,
1313
}
1414

15-
pub fn compile(p: ast::Program) -> CodeObject {
15+
pub fn compile(p: ast::Program, mode: Mode) -> CodeObject {
1616
let mut compiler = Compiler::new();
1717
compiler.compile_program(p)
1818
}
1919

20+
pub enum Mode {
21+
Exec,
22+
Eval,
23+
}
24+
2025
type Label = usize;
2126

2227
impl Compiler {
@@ -279,6 +284,10 @@ impl Compiler {
279284
ast::Comparison::LessOrEqual => bytecode::ComparisonOperator::LessOrEqual,
280285
ast::Comparison::Greater => bytecode::ComparisonOperator::Greater,
281286
ast::Comparison::GreaterOrEqual => bytecode::ComparisonOperator::GreaterOrEqual,
287+
ast::Comparison::In => bytecode::ComparisonOperator::In,
288+
ast::Comparison::NotIn => bytecode::ComparisonOperator::NotIn,
289+
ast::Comparison::Is => bytecode::ComparisonOperator::Is,
290+
ast::Comparison::IsNot => bytecode::ComparisonOperator::IsNot,
282291
};
283292
let i = Instruction::CompareOperation { op: i };
284293
self.emit(i);

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn eval(vm: &mut VirtualMachine, source: &String) -> Result<PyObjectRef, PyO
99
match parse_source(source) {
1010
Ok(program) => {
1111
debug!("Got ast: {:?}", program);
12-
let bytecode = compile::compile(program);
12+
let bytecode = compile::compile(program, compile::Mode::Eval);
1313
debug!("Code object: {:?}", bytecode);
1414
vm.evaluate(bytecode)
1515
}

vm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub mod bytecode;
1010
pub mod compile;
1111
pub mod eval;
1212
mod objint;
13+
mod objstr;
1314
mod objtype;
1415
pub mod pyobject;
1516
mod vm;

vm/src/objint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn set_attr(a: &mut PyObjectRef, name: String, b: PyObjectRef) {
1313
*/
1414

1515
pub fn create_type(type_type: PyObjectRef) -> PyObjectRef {
16-
let typ = PyObject::new(PyObjectKind::Type, type_type.clone());
16+
let typ = PyObject::new(PyObjectKind::Class { name: "int".to_string() }, type_type.clone());
1717
typ.borrow_mut().dict.insert(
1818
"__str__".to_string(),
1919
PyObject::new(PyObjectKind::RustFunction { function: str }, type_type.clone()),

vm/src/objlist.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
fn subscript(rt: Executor, a, b: PyObjectRef) -> PyResult {
4+
match b.kind {
5+
(&NativeType::List(ref l), &NativeType::Int(ref index)) => {
6+
let pos_index = (index + l.borrow().len() as i32) % l.borrow().len() as i32;
7+
curr_frame.stack.push(Rc::new(l.borrow()[pos_index as usize].clone()))
8+
},
9+
(&NativeType::List(ref l), &NativeType::Slice(ref opt_start, ref opt_stop, ref opt_step)) => {
10+
let start = match opt_start {
11+
&Some(start) => ((start + l.borrow().len() as i32) % l.borrow().len() as i32) as usize,
12+
&None => 0,
13+
};
14+
let stop = match opt_stop {
15+
&Some(stop) => ((stop + l.borrow().len() as i32) % l.borrow().len() as i32) as usize,
16+
&None => l.borrow().len() as usize,
17+
};
18+
let step = match opt_step {
19+
//Some(step) => step as usize,
20+
&None => 1 as usize,
21+
_ => unimplemented!(),
22+
};
23+
// TODO: we could potentially avoid this copy and use slice
24+
curr_frame.stack.push(Rc::new(NativeType::List(RefCell::new(l.borrow()[start..stop].to_vec()))));
25+
},
26+
}
27+
}
28+

0 commit comments

Comments
 (0)