Skip to content

Commit 36bef81

Browse files
committed
Change eval compile to statement
1 parent 5783133 commit 36bef81

7 files changed

Lines changed: 47 additions & 13 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ To test RustPython, do the following:
1212
$ cargo run demo.py
1313
42
1414

15+
Or use the interactive shell:
16+
17+
$ cargo run
18+
Welcome to rustpython
19+
>>>>> 2+2
20+
4
21+
1522
Or use pip to install extra modules:
1623

1724
$ cargo run -m pip install requests

parser/src/lexer.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<'input> Lexer<'input> {
157157

158158
fn is_char(&self) -> bool {
159159
match self.chr0 {
160-
Some('a'...'z') => return true,
160+
Some('a'...'z') | Some('A'...'Z') => return true,
161161
_ => return false,
162162
}
163163
}
@@ -226,8 +226,7 @@ impl<'input> Lexer<'input> {
226226

227227
match self.chr0 {
228228
Some('0'...'9') => return Some(self.lex_number()),
229-
// TODO: 'A'...'Z'
230-
Some('a'...'z') => return Some(self.lex_identifier()),
229+
Some('a'...'z') | Some('A'...'Z') => return Some(self.lex_identifier()),
231230
Some('#') => {
232231
self.lex_comment();
233232
continue;
@@ -370,6 +369,17 @@ impl<'input> Lexer<'input> {
370369
_ => return Some(Ok((tok_start, Tok::At, self.location + 1))),
371370
}
372371
}
372+
Some('!') => {
373+
let tok_start = self.location;
374+
self.next_char();
375+
match self.chr0 {
376+
Some('=') => {
377+
self.next_char();
378+
return Some(Ok((tok_start, Tok::NotEqual, self.location + 1)));
379+
},
380+
_ => panic!("Invalid token '!'"),
381+
}
382+
}
373383
Some('~') => {
374384
self.next_char();
375385
return Some(Ok((0, Tok::Tilde, 0)));

parser/src/parser.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ pub fn parse_program(source: &String) -> Result<ast::Program, String> {
4646
}
4747
}
4848

49+
pub fn parse_statement(source: &String) -> Result<ast::Statement, String> {
50+
let lxr = lexer::Lexer::new(&source);
51+
match python::StatementParser::new().parse(lxr) {
52+
Err(why) => Err(String::from(format!("{:?}", why))),
53+
Ok(p) => Ok(p),
54+
}
55+
}
56+
4957
pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
5058
let lxr = lexer::Lexer::new(&source);
5159
match python::ExpressionParser::new().parse(lxr) {
@@ -56,7 +64,7 @@ pub fn parse_expression(source: &String) -> Result<ast::Expression, String> {
5664

5765
#[cfg(test)]
5866
mod tests {
59-
use super::parse_source;
67+
use super::parse_program;
6068
use super::ast;
6169

6270
#[test]
@@ -87,7 +95,7 @@ mod tests {
8795
#[test]
8896
fn test_parse_print_2() {
8997
let source = String::from("print('Hello world', 2)\n");
90-
let parse_ast = parse_source(&source).unwrap();
98+
let parse_ast = parse_program(&source).unwrap();
9199
assert_eq!(
92100
parse_ast,
93101
ast::Program {

parser/src/python.lalrpop

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Suite: Vec<ast::Statement> = {
2020
"\n" indent <s:Statement+> dedent => s,
2121
};
2222

23-
Statement: ast::Statement = {
23+
pub Statement: ast::Statement = {
2424
SimpleStatement,
2525
CompoundStatement,
2626
};

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn run_shell() {
6666
print!(">>>>> "); // Use 5 items. pypy has 4, cpython has 3.
6767
io::stdout().flush().ok().expect("Could not flush stdout");
6868
io::stdin().read_line(&mut input);
69-
input = input.trim().to_string();
69+
// input = input.trim().to_string();
7070
debug!("You entered {:?}", input);
7171
let result = eval(&mut vm, &input);
7272
match result {

vm/src/compile.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ pub fn compile(source: &String, mode: Mode) -> Result<CodeObject, String> {
2626
}
2727
}
2828
Mode::Eval => {
29-
match parser::parse_expression(source) {
30-
Ok(ast) => {
31-
compiler.compile_expression(ast);
29+
match parser::parse_statement(source) {
30+
Ok(statement) => {
31+
let need_none = if let &ast::Statement::Expression { expression: _ } = &statement {
32+
false
33+
} else {
34+
true
35+
};
36+
compiler.compile_statement(statement);
37+
if need_none {
38+
compiler.emit(Instruction::LoadConst { value: bytecode::Constant::None });
39+
}
3240
compiler.emit(Instruction::ReturnValue);
3341
Ok(compiler.pop_code_object())
3442
}
@@ -90,7 +98,8 @@ impl Compiler {
9098
self.compile_expression(expression);
9199

92100
// Pop result of stack, since we not use it:
93-
self.emit(Instruction::Pop);
101+
// TODO: do we need to clean the stack here?
102+
// self.emit(Instruction::Pop);
94103
}
95104
ast::Statement::If { test, body } => {
96105
self.compile_expression(test);

vm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,15 @@ impl VirtualMachine {
295295
fn _eq(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
296296
let b2 = &*b.borrow();
297297
let a2 = &*a.borrow();
298-
let result_bool = a == b;
298+
let result_bool = a2 == b2;
299299
let result = self.ctx.new_bool(result_bool);
300300
Ok(result)
301301
}
302302

303303
fn _ne(&mut self, a: PyObjectRef, b: PyObjectRef) -> PyResult {
304304
let b2 = &*b.borrow();
305305
let a2 = &*a.borrow();
306-
let result_bool = a != b;
306+
let result_bool = a2 != b2;
307307
let result = self.ctx.new_bool(result_bool);
308308
Ok(result)
309309
}

0 commit comments

Comments
 (0)