Skip to content

Assignment #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,40 @@ mod tests {
)
}

#[test]
fn test_parse_tuples() {
let source = String::from("a, b = 4, 5\n");

assert_eq!(
parse_statement(&source),
Ok(ast::LocatedStatement {
location: ast::Location::new(1, 1),
node: ast::Statement::Assign {
targets: vec![ast::Expression::Tuple {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of expressions are allowed at the left hand side (LHS)? Is it tuples, names only? Or are lists also allowed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tuples, lists, names, attributes, indices, slices.

I'm not sure if there are any others.

elements: vec![
ast::Expression::Identifier {
name: "a".to_string()
},
ast::Expression::Identifier {
name: "b".to_string()
}
]
}],
value: ast::Expression::Tuple {
elements: vec![
ast::Expression::Number {
value: ast::Number::Integer { value: 4 }
},
ast::Expression::Number {
value: ast::Number::Integer { value: 5 }
}
]
}
}
})
)
}

#[test]
fn test_parse_class() {
let source = String::from("class Foo(A, B):\n def __init__(self):\n pass\n def method_with_default(self, arg='default'):\n pass\n");
Expand Down
50 changes: 28 additions & 22 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,38 @@ SmallStatement: ast::LocatedStatement = {
};

ExpressionStatement: ast::LocatedStatement = {
<loc:@L> <e:TestList> <e2:AssignSuffix*> => {
//match e2 {
// None => ast::Statement::Expression { expression: e },
// Some(e3) => ast::Statement::Expression { expression: e },
//}
if e2.len() > 0 {
// Dealing with assignment here
// TODO: for rhs in e2 {
let rhs = e2.into_iter().next().unwrap();
// ast::Expression::Tuple { elements: e2.into_iter().next().unwrap()
let v = rhs.into_iter().next().unwrap();
let lhs = ast::LocatedStatement {
location: loc.clone(),
node: ast::Statement::Assign { targets: e, value: v },
};
lhs
} else {
if e.len() > 1 {
panic!("Not good?");
// ast::Statement::Expression { expression: e[0] }
<loc:@L> <expr:TestList> <suffix:AssignSuffix*> => {
// Just an expression, no assignment:
if suffix.is_empty() {
if expr.len() > 1 {
ast::LocatedStatement {
location: loc.clone(),
node: ast::Statement::Expression { expression: ast::Expression::Tuple { elements: expr } }
}
} else {
ast::LocatedStatement {
location: loc.clone(),
node: ast::Statement::Expression { expression: e.into_iter().next().unwrap() },
node: ast::Statement::Expression { expression: expr[0].clone() },
}
}
} else {
let mut targets = vec![if expr.len() > 1 {
ast::Expression::Tuple { elements: expr }
} else {
expr[0].clone()
}];
let mut values : Vec<ast::Expression> = suffix.into_iter().map(|test_list| if test_list.len() > 1 { ast::Expression::Tuple { elements: test_list }} else { test_list[0].clone() }).collect();

while values.len() > 1 {
targets.push(values.remove(0));
}

let value = values[0].clone();

ast::LocatedStatement {
location: loc.clone(),
node: ast::Statement::Assign { targets, value },
}
}
},
<loc:@L> <e1:Test> <op:AugAssign> <e2:TestList> => {
Expand Down Expand Up @@ -120,7 +126,7 @@ FlowStatement: ast::LocatedStatement = {
<loc:@L> "return" <t:TestList?> => {
ast::LocatedStatement {
location: loc,
node: ast::Statement::Return { value: t},
node: ast::Statement::Return { value: t },
}
},
<loc:@L> "raise" <t:Test?> => {
Expand Down
28 changes: 28 additions & 0 deletions tests/snippets/assignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
x = 1
assert x == 1

x = 1, 2, 3
assert x == (1, 2, 3)

x, y = 1, 2
assert x == 1
assert y == 2

x, y = (y, x)

assert x == 2
assert y == 1

((x, y), z) = ((1, 2), 3)

assert (x, y, z) == (1, 2, 3)

q = (1, 2, 3)
(x, y, z) = q
assert y == q[1]

x = (a, b, c) = y = q

assert (a, b, c) == q
assert x == q
assert y == q
3 changes: 3 additions & 0 deletions vm/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ pub enum Instruction {
PrintExpr,
LoadBuildClass,
StoreLocals,
UnpackSequence {
size: usize,
},
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
13 changes: 12 additions & 1 deletion vm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,10 @@ impl Compiler {
ast::Statement::Assign { targets, value } => {
self.compile_expression(value);

for target in targets {
for (i, target) in targets.into_iter().enumerate() {
if i + 1 != targets.len() {
self.emit(Instruction::Duplicate);
}
self.compile_store(target);
}
}
Expand Down Expand Up @@ -548,6 +551,14 @@ impl Compiler {
name: name.to_string(),
});
}
ast::Expression::Tuple { elements } => {
self.emit(Instruction::UnpackSequence {
size: elements.len(),
});
for element in elements {
self.compile_store(element);
}
}
_ => {
panic!("WTF: {:?}", target);
}
Expand Down
14 changes: 14 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use super::import::import;
use super::obj::objlist;
use super::obj::objobject;
use super::obj::objstr;
use super::obj::objtuple;
use super::obj::objtype;
use super::objbool;
use super::pyobject::{
Expand Down Expand Up @@ -1045,6 +1046,19 @@ impl VirtualMachine {
}
None
}
bytecode::Instruction::UnpackSequence { size } => {
let value = self.pop_value();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a type check here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full implementation requires __iter__, which you've just started to provide. I'll extend this to call that.

let elements = objtuple::get_elements(&value);
if elements.len() != *size {
panic!("Wrong number of values to unpack");
}

for element in elements.into_iter().rev() {
self.push_value(element);
}
None
}
}
}

Expand Down