Skip to content

Commit b7f6db7

Browse files
Merge branch 'master' into fix-unicode-handling
2 parents 33a3ec8 + 4242b66 commit b7f6db7

File tree

8 files changed

+73
-33
lines changed

8 files changed

+73
-33
lines changed

parser/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern crate num_traits;
77
pub mod ast;
88
pub mod lexer;
99
pub mod parser;
10+
#[cfg_attr(rustfmt, rustfmt_skip)]
1011
mod python;
1112
pub mod token;
1213

tests/snippets/basic_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
assert type(a) is complex
4646
assert type(a + a) is complex
4747

48+
a = 1
49+
assert a.conjugate() == a
4850

4951
a = 12345
5052

tests/snippets/bools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ def __bool__(self):
4646
assert False * 7 == 0
4747
assert True > 0
4848
assert int(True) == 1
49+
assert True.conjugate() == 1
50+
assert isinstance(True.conjugate(), int)

tests/snippets/tuple.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
y = (1,)
77
assert y[0] == 1
88

9+
assert x + y == (1, 2, 1)
10+
911
assert x * 3 == (1, 2, 1, 2, 1, 2)
1012
# assert 3 * x == (1, 2, 1, 2, 1, 2)
1113
assert x * 0 == ()

vm/src/compile.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,21 +838,21 @@ impl Compiler {
838838
self.compile_test(expression, None, None, EvalContext::Expression)?
839839
}
840840
ast::Expression::Binop { a, op, b } => {
841-
self.compile_expression(&*a)?;
842-
self.compile_expression(&*b)?;
841+
self.compile_expression(a)?;
842+
self.compile_expression(b)?;
843843

844844
// Perform operation:
845845
self.compile_op(op);
846846
}
847847
ast::Expression::Subscript { a, b } => {
848-
self.compile_expression(&*a)?;
849-
self.compile_expression(&*b)?;
848+
self.compile_expression(a)?;
849+
self.compile_expression(b)?;
850850
self.emit(Instruction::BinaryOperation {
851851
op: bytecode::BinaryOperator::Subscript,
852852
});
853853
}
854854
ast::Expression::Unop { op, a } => {
855-
self.compile_expression(&*a)?;
855+
self.compile_expression(a)?;
856856

857857
// Perform operation:
858858
let i = match op {
@@ -865,14 +865,14 @@ impl Compiler {
865865
self.emit(i);
866866
}
867867
ast::Expression::Attribute { value, name } => {
868-
self.compile_expression(&*value)?;
868+
self.compile_expression(value)?;
869869
self.emit(Instruction::LoadAttr {
870870
name: name.to_string(),
871871
});
872872
}
873873
ast::Expression::Compare { a, op, b } => {
874-
self.compile_expression(&*a)?;
875-
self.compile_expression(&*b)?;
874+
self.compile_expression(a)?;
875+
self.compile_expression(b)?;
876876

877877
let i = match op {
878878
ast::Comparison::Equal => bytecode::ComparisonOperator::Equal,

vm/src/exceptions.rs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,23 @@ fn exception_str(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8181

8282
#[derive(Debug)]
8383
pub struct ExceptionZoo {
84-
pub base_exception_type: PyObjectRef,
85-
pub exception_type: PyObjectRef,
86-
pub syntax_error: PyObjectRef,
8784
pub assertion_error: PyObjectRef,
8885
pub attribute_error: PyObjectRef,
86+
pub base_exception_type: PyObjectRef,
87+
pub exception_type: PyObjectRef,
88+
pub file_not_found_error: PyObjectRef,
89+
pub import_error: PyObjectRef,
8990
pub index_error: PyObjectRef,
9091
pub key_error: PyObjectRef,
92+
pub module_not_found_error: PyObjectRef,
9193
pub name_error: PyObjectRef,
92-
pub runtime_error: PyObjectRef,
9394
pub not_implemented_error: PyObjectRef,
95+
pub permission_error: PyObjectRef,
96+
pub runtime_error: PyObjectRef,
9497
pub stop_iteration: PyObjectRef,
98+
pub syntax_error: PyObjectRef,
9599
pub type_error: PyObjectRef,
96100
pub value_error: PyObjectRef,
97-
pub import_error: PyObjectRef,
98-
pub module_not_found_error: PyObjectRef,
99-
pub file_not_found_error: PyObjectRef,
100-
pub permission_error: PyObjectRef,
101101
}
102102

103103
impl ExceptionZoo {
@@ -106,11 +106,13 @@ impl ExceptionZoo {
106106
object_type: &PyObjectRef,
107107
dict_type: &PyObjectRef,
108108
) -> Self {
109+
// Sorted By Hierarchy then alphabetized.
110+
109111
let base_exception_type =
110112
create_type("BaseException", &type_type, &object_type, &dict_type);
111113

112114
let exception_type = create_type("Exception", &type_type, &base_exception_type, &dict_type);
113-
let syntax_error = create_type("SyntaxError", &type_type, &exception_type, &dict_type);
115+
114116
let assertion_error =
115117
create_type("AssertionError", &type_type, &exception_type, &dict_type);
116118
let attribute_error = create_type(
@@ -119,6 +121,7 @@ impl ExceptionZoo {
119121
&exception_type.clone(),
120122
&dict_type,
121123
);
124+
let import_error = create_type("ImportError", &type_type, &exception_type, &dict_type);
122125
let index_error = create_type(
123126
"IndexError",
124127
&type_type,
@@ -128,41 +131,42 @@ impl ExceptionZoo {
128131
let key_error = create_type("KeyError", &type_type, &exception_type.clone(), &dict_type);
129132
let name_error = create_type("NameError", &type_type, &exception_type.clone(), &dict_type);
130133
let runtime_error = create_type("RuntimeError", &type_type, &exception_type, &dict_type);
134+
let stop_iteration = create_type("StopIteration", &type_type, &exception_type, &dict_type);
135+
let syntax_error = create_type("SyntaxError", &type_type, &exception_type, &dict_type);
136+
let type_error = create_type("TypeError", &type_type, &exception_type, &dict_type);
137+
let value_error = create_type("ValueError", &type_type, &exception_type, &dict_type);
138+
139+
let file_not_found_error =
140+
create_type("FileNotFoundError", &type_type, &import_error, &dict_type);
141+
let module_not_found_error =
142+
create_type("ModuleNotFoundError", &type_type, &import_error, &dict_type);
131143
let not_implemented_error = create_type(
132144
"NotImplementedError",
133145
&type_type,
134146
&runtime_error,
135147
&dict_type,
136148
);
137-
let stop_iteration = create_type("StopIteration", &type_type, &exception_type, &dict_type);
138-
let type_error = create_type("TypeError", &type_type, &exception_type, &dict_type);
139-
let value_error = create_type("ValueError", &type_type, &exception_type, &dict_type);
140-
let import_error = create_type("ImportError", &type_type, &exception_type, &dict_type);
141-
let module_not_found_error =
142-
create_type("ModuleNotFoundError", &type_type, &import_error, &dict_type);
143-
let file_not_found_error =
144-
create_type("FileNotFoundError", &type_type, &import_error, &dict_type);
145149
let permission_error =
146150
create_type("PermissionError", &type_type, &import_error, &dict_type);
147151

148152
ExceptionZoo {
149-
base_exception_type: base_exception_type,
150-
exception_type: exception_type,
151-
syntax_error: syntax_error,
152153
assertion_error: assertion_error,
153154
attribute_error: attribute_error,
155+
base_exception_type: base_exception_type,
156+
exception_type: exception_type,
157+
file_not_found_error: file_not_found_error,
158+
import_error: import_error,
154159
index_error: index_error,
155160
key_error: key_error,
161+
module_not_found_error: module_not_found_error,
156162
name_error: name_error,
157-
runtime_error: runtime_error,
158163
not_implemented_error: not_implemented_error,
164+
permission_error: permission_error,
165+
runtime_error: runtime_error,
159166
stop_iteration: stop_iteration,
167+
syntax_error: syntax_error,
160168
type_error: type_error,
161169
value_error: value_error,
162-
import_error: import_error,
163-
module_not_found_error: module_not_found_error,
164-
file_not_found_error: file_not_found_error,
165-
permission_error: permission_error,
166170
}
167171
}
168172
}

vm/src/obj/objint.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ fn int_bit_length(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
489489
Ok(vm.ctx.new_int(bits.to_bigint().unwrap()))
490490
}
491491

492+
fn int_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
493+
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
494+
let v = get_value(i);
495+
Ok(vm.ctx.new_int(v))
496+
}
497+
492498
pub fn init(context: &PyContext) {
493499
let ref int_type = context.int_type;
494500
context.set_attr(&int_type, "__eq__", context.new_rustfunc(int_eq));
@@ -526,4 +532,5 @@ pub fn init(context: &PyContext) {
526532
"bit_length",
527533
context.new_rustfunc(int_bit_length),
528534
);
535+
context.set_attr(&int_type, "conjugate", context.new_rustfunc(int_conjugate));
529536
}

vm/src/obj/objtuple.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ use super::objtype;
1010
use num_bigint::ToBigInt;
1111
use std::hash::{Hash, Hasher};
1212

13+
fn tuple_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
14+
arg_check!(
15+
vm,
16+
args,
17+
required = [(zelf, Some(vm.ctx.tuple_type())), (other, None)]
18+
);
19+
20+
if objtype::isinstance(other, &vm.ctx.tuple_type()) {
21+
let e1 = get_elements(zelf);
22+
let e2 = get_elements(other);
23+
let elements = e1.iter().chain(e2.iter()).map(|e| e.clone()).collect();
24+
Ok(vm.ctx.new_tuple(elements))
25+
} else {
26+
Err(vm.new_type_error(format!(
27+
"Cannot add {} and {}",
28+
zelf.borrow(),
29+
other.borrow()
30+
)))
31+
}
32+
}
33+
1334
fn tuple_count(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
1435
arg_check!(
1536
vm,
@@ -165,6 +186,7 @@ pub fn tuple_contains(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
165186

166187
pub fn init(context: &PyContext) {
167188
let ref tuple_type = context.tuple_type;
189+
context.set_attr(&tuple_type, "__add__", context.new_rustfunc(tuple_add));
168190
context.set_attr(&tuple_type, "__eq__", context.new_rustfunc(tuple_eq));
169191
context.set_attr(
170192
&tuple_type,

0 commit comments

Comments
 (0)