Skip to content

Commit 1e3b45e

Browse files
committed
Add complex basic type
1 parent 5053d6d commit 1e3b45e

File tree

16 files changed

+149
-10
lines changed

16 files changed

+149
-10
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,5 @@ pub enum Comparison {
300300
pub enum Number {
301301
Integer { value: i32 },
302302
Float { value: f64 },
303+
Complex { real: f64, imag: f64 },
303304
}

parser/src/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub enum Tok {
44
Name { name: String },
55
Number { value: String },
6+
Complex { real: f64, imag: f64 },
67
String { value: String },
78
Bytes { value: Vec<u8> },
89
Newline,

tests/snippets/basic_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@
3737
pass
3838

3939
assert int() == 0
40+
41+
a = complex(2, 4)
42+
assert type(a) is complex
43+
assert type(a + a) is complex

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Shing Lyu <[email protected]>"]
55

66
[dependencies]
77
bitflags = "1.0.4"
8+
num-complex = "0.2"
89
log = "0.3"
910
rustpython_parser = {path = "../parser"}
1011
serde = "1.0.66"

vm/src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ fn builtin_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
126126
compile::compile(vm, &source, mode, None)
127127
}
128128

129-
// builtin_complex
130129
// builtin_delattr
131130

132131
fn builtin_dir(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -543,6 +542,7 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
543542
dict.insert(String::from("bytes"), ctx.bytes_type());
544543
dict.insert(String::from("chr"), ctx.new_rustfunc(builtin_chr));
545544
dict.insert(String::from("compile"), ctx.new_rustfunc(builtin_compile));
545+
dict.insert(String::from("complex"), ctx.complex_type());
546546
dict.insert(String::from("dict"), ctx.dict_type());
547547
dict.insert(String::from("divmod"), ctx.new_rustfunc(builtin_divmod));
548548
dict.insert(String::from("dir"), ctx.new_rustfunc(builtin_dir));

vm/src/bytecode.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ let call_function = 0x64;
1010
/*
1111
* Primitive instruction type, which can be encoded and decoded.
1212
*/
13-
extern crate rustpython_parser;
1413

15-
use self::rustpython_parser::ast;
14+
use num_complex::Complex64;
15+
use rustpython_parser::ast;
1616
use std::collections::HashMap;
1717
use std::fmt;
1818

@@ -197,6 +197,7 @@ pub enum CallType {
197197
pub enum Constant {
198198
Integer { value: i32 }, // TODO: replace by arbitrary big int math.
199199
Float { value: f64 },
200+
Complex { value: Complex64 },
200201
Boolean { value: bool },
201202
String { value: String },
202203
Bytes { value: Vec<u8> },

vm/src/compile.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* https://github.com/micropython/micropython/blob/master/py/compile.c
77
*/
88

9-
extern crate rustpython_parser;
10-
11-
use self::rustpython_parser::{ast, parser};
129
use super::bytecode::{self, CallType, CodeObject, Instruction};
1310
use super::pyobject::{PyObject, PyObjectKind, PyResult};
1411
use super::vm::VirtualMachine;
12+
use num_complex::Complex64;
13+
use rustpython_parser::{ast, parser};
1514

1615
struct Compiler {
1716
code_object_stack: Vec<CodeObject>,
@@ -846,6 +845,9 @@ impl Compiler {
846845
let const_value = match value {
847846
ast::Number::Integer { value } => bytecode::Constant::Integer { value: *value },
848847
ast::Number::Float { value } => bytecode::Constant::Float { value: *value },
848+
ast::Number::Complex { real, imag } => bytecode::Constant::Complex {
849+
value: Complex64::new(*real, *imag),
850+
},
849851
};
850852
self.emit(Instruction::LoadConst { value: const_value });
851853
}
@@ -1301,8 +1303,8 @@ mod tests {
13011303
use super::bytecode::CodeObject;
13021304
use super::bytecode::Constant::*;
13031305
use super::bytecode::Instruction::*;
1304-
use super::rustpython_parser::parser;
13051306
use super::Compiler;
1307+
use rustpython_parser::parser;
13061308
fn compile_exec(source: &str) -> CodeObject {
13071309
let mut compiler = Compiler::new();
13081310
compiler.push_new_code_object(Option::None, "<module>".to_string());

vm/src/frame.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,7 @@ impl Frame {
10741074
match *value {
10751075
bytecode::Constant::Integer { ref value } => vm.ctx.new_int(*value),
10761076
bytecode::Constant::Float { ref value } => vm.ctx.new_float(*value),
1077+
bytecode::Constant::Complex { ref value } => vm.ctx.new_complex(*value),
10771078
bytecode::Constant::String { ref value } => vm.new_str(value.clone()),
10781079
bytecode::Constant::Bytes { ref value } => vm.ctx.new_bytes(value.clone()),
10791080
bytecode::Constant::Boolean { ref value } => vm.new_bool(value.clone()),

vm/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ extern crate bitflags;
33
#[macro_use]
44
extern crate log;
55
// extern crate env_logger;
6+
extern crate num_complex;
67
extern crate serde;
78
extern crate serde_json;
89

10+
extern crate rustpython_parser;
11+
912
//extern crate eval; use eval::eval::*;
1013
// use py_code_object::{Function, NativeType, PyCodeObject};
1114

0 commit comments

Comments
 (0)