Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1e3b45e

Browse files
committed
Add complex basic type
1 parent5053d6d commit1e3b45e

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 aboutcustomizing 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
pubenumNumber{
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
pubenumTok{
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
assertint()==0
40+
41+
a=complex(2,4)
42+
asserttype(a)iscomplex
43+
asserttype(a+a)iscomplex

‎vm/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Shing Lyu <shing.lyu@gmail.com>"]
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
fnbuiltin_dir(vm:&mutVirtualMachine,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-
externcrate rustpython_parser;
1413

15-
useself::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
pubenumConstant{
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-
externcrate rustpython_parser;
10-
11-
useself::rustpython_parser::{ast, parser};
129
usesuper::bytecode::{self,CallType,CodeObject,Instruction};
1310
usesuper::pyobject::{PyObject,PyObjectKind,PyResult};
1411
usesuper::vm::VirtualMachine;
12+
use num_complex::Complex64;
13+
use rustpython_parser::{ast, parser};
1514

1615
structCompiler{
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
usesuper::bytecode::CodeObject;
13021304
usesuper::bytecode::Constant::*;
13031305
usesuper::bytecode::Instruction::*;
1304-
usesuper::rustpython_parser::parser;
13051306
usesuper::Compiler;
1307+
use rustpython_parser::parser;
13061308
fncompile_exec(source:&str) ->CodeObject{
13071309
letmut 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
externcrate log;
55
// extern crate env_logger;
6+
externcrate num_complex;
67
externcrate serde;
78
externcrate serde_json;
89

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp