1+ use super :: objfloat;
12use super :: objtype;
23use super :: pyobject:: {
3- AttributeProtocol , PyContext , PyFuncArgs , PyObjectKind , PyObjectRef , TypeProtocol ,
4+ AttributeProtocol , PyContext , PyFuncArgs , PyObjectKind , PyObjectRef , PyResult , TypeProtocol ,
45} ;
56use super :: vm:: VirtualMachine ;
67
@@ -11,16 +12,115 @@ fn str(vm: &mut VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjec
1112}
1213
1314// Retrieve inner int value:
14- fn get_value ( obj : PyObjectRef ) -> i32 {
15+ pub fn get_value ( obj : PyObjectRef ) -> i32 {
1516 if let PyObjectKind :: Integer { value } = & obj. borrow ( ) . kind {
1617 * value
1718 } else {
1819 panic ! ( "Inner error getting int" ) ;
1920 }
2021}
2122
23+ fn int_add ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
24+ arg_check ! (
25+ vm,
26+ args,
27+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
28+ ) ;
29+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
30+ Ok ( vm. ctx . new_int ( get_value ( i. clone ( ) ) + get_value ( i2. clone ( ) ) ) )
31+ } else if objtype:: isinstance ( i2. clone ( ) , vm. ctx . float_type ( ) ) {
32+ Ok ( vm
33+ . ctx
34+ . new_float ( get_value ( i. clone ( ) ) as f64 + objfloat:: get_value ( i2. clone ( ) ) ) )
35+ } else {
36+ Err ( vm. new_type_error ( format ! ( "Cannot add {:?} and {:?}" , i, i2) ) )
37+ }
38+ }
39+
40+ fn int_sub ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
41+ arg_check ! (
42+ vm,
43+ args,
44+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
45+ ) ;
46+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
47+ Ok ( vm. ctx . new_int ( get_value ( i. clone ( ) ) - get_value ( i2. clone ( ) ) ) )
48+ } else {
49+ Err ( vm. new_type_error ( format ! ( "Cannot substract {:?} and {:?}" , i, i2) ) )
50+ }
51+ }
52+
53+ fn int_mul ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
54+ arg_check ! (
55+ vm,
56+ args,
57+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
58+ ) ;
59+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
60+ Ok ( vm. ctx . new_int ( get_value ( i. clone ( ) ) * get_value ( i2. clone ( ) ) ) )
61+ } else {
62+ Err ( vm. new_type_error ( format ! ( "Cannot multiply {:?} and {:?}" , i, i2) ) )
63+ }
64+ }
65+
66+ fn int_truediv ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
67+ arg_check ! (
68+ vm,
69+ args,
70+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
71+ ) ;
72+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
73+ Ok ( vm
74+ . ctx
75+ . new_float ( get_value ( i. clone ( ) ) as f64 / get_value ( i2. clone ( ) ) as f64 ) )
76+ } else if objtype:: isinstance ( i2. clone ( ) , vm. ctx . float_type ( ) ) {
77+ Ok ( vm
78+ . ctx
79+ . new_float ( get_value ( i. clone ( ) ) as f64 / objfloat:: get_value ( i2. clone ( ) ) ) )
80+ } else {
81+ Err ( vm. new_type_error ( format ! ( "Cannot multiply {:?} and {:?}" , i, i2) ) )
82+ }
83+ }
84+
85+ fn int_mod ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
86+ arg_check ! (
87+ vm,
88+ args,
89+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
90+ ) ;
91+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
92+ Ok ( vm. ctx . new_int ( get_value ( i. clone ( ) ) % get_value ( i2. clone ( ) ) ) )
93+ } else {
94+ Err ( vm. new_type_error ( format ! ( "Cannot modulo {:?} and {:?}" , i, i2) ) )
95+ }
96+ }
97+
98+ fn int_pow ( vm : & mut VirtualMachine , args : PyFuncArgs ) -> PyResult {
99+ arg_check ! (
100+ vm,
101+ args,
102+ required = [ ( i, Some ( vm. ctx. int_type( ) ) ) , ( i2, None ) ]
103+ ) ;
104+ let v1 = get_value ( i. clone ( ) ) ;
105+ if objtype:: isinstance ( i2. clone ( ) , vm. ctx . int_type ( ) ) {
106+ let v2 = get_value ( i2. clone ( ) ) ;
107+ Ok ( vm. ctx . new_int ( v1. pow ( v2 as u32 ) ) )
108+ } else if objtype:: isinstance ( i2. clone ( ) , vm. ctx . float_type ( ) ) {
109+ let v2 = objfloat:: get_value ( i2. clone ( ) ) ;
110+ Ok ( vm. ctx . new_float ( ( v1 as f64 ) . powf ( v2) ) )
111+ } else {
112+ Err ( vm. new_type_error ( format ! ( "Cannot modulo {:?} and {:?}" , i, i2) ) )
113+ }
114+ }
115+
22116pub fn init ( context : & PyContext ) {
23117 let ref int_type = context. int_type ;
24- int_type. set_attr ( "__str__" , context. new_rustfunc ( str) ) ;
118+ int_type. set_attr ( "__add__" , context. new_rustfunc ( int_add) ) ;
119+ int_type. set_attr ( "__mod__" , context. new_rustfunc ( int_mod) ) ;
120+ int_type. set_attr ( "__mul__" , context. new_rustfunc ( int_mul) ) ;
121+ int_type. set_attr ( "__pow__" , context. new_rustfunc ( int_pow) ) ;
25122 int_type. set_attr ( "__repr__" , context. new_rustfunc ( str) ) ;
123+ int_type. set_attr ( "__str__" , context. new_rustfunc ( str) ) ;
124+ int_type. set_attr ( "__sub__" , context. new_rustfunc ( int_sub) ) ;
125+ int_type. set_attr ( "__truediv__" , context. new_rustfunc ( int_truediv) ) ;
26126}
0 commit comments