1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use std;
use std::fmt;
use std::cell::Cell;
use std::collections::{HashMap, VecDeque};
use std::str::FromStr;
use num;
#[allow(unused_imports)]
use num::{ToPrimitive};
use num::Num as NumTrait;
use serde::ser::{Serializer};
use python_ast::{Id, Tag, Num, OwnedTk};
use ::api::result::{ObjectResult, RtResult, Error};
use ::api::RtObject;
use ::modules::builtins::Type as BuiltinType;
use ::objects::collection::sequence::is_sequence;
use ::objects;
use ::runtime::{OpCode, Runtime};
pub type ObjectId = usize;
pub type HashId = u64;
pub type Integer = num::BigInt;
pub type Count = usize;
pub type Float = f64;
pub type Boolean = bool;
pub type Complex = num::complex::Complex<Float>;
pub type String = std::string::String;
pub type Byte = u8;
pub type Bytes = Vec<Byte>;
pub struct None();
pub type List = Vec<RtObject>;
pub type Tuple = Vec<RtObject>;
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct DictKey(HashId, RtObject);
impl DictKey {
pub fn new(id: HashId, object: &RtObject) -> Self {
DictKey(id, object.clone())
}
pub fn hash(&self) -> HashId {
self.0
}
pub fn value(&self) -> RtObject {
self.1.clone()
}
}
pub type Dict = std::collections::HashMap<DictKey, RtObject>;
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct SetElement(pub HashId, pub RtObject);
pub type Set = std::collections::HashSet<SetElement>;
pub type WrapperFn = Fn(&Runtime, &RtObject, &RtObject, &RtObject) -> ObjectResult;
#[derive(Debug, Serialize)]
pub struct Func {
pub name: String,
pub signature: Signature,
pub module: String,
pub callable: FuncType,
}
#[derive(Serialize)]
pub enum FuncType {
#[serde(skip_serializing)]
Wrapper(Box<WrapperFn>),
#[serde(skip_serializing)]
MethodWrapper(RtObject, Box<WrapperFn>),
Code(Code),
}
impl fmt::Debug for FuncType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let func_type = match self {
&FuncType::Wrapper(ref func) => {
format!("Wrapper(<function at {:?}>)", (func as *const _))
},
&FuncType::MethodWrapper(ref objref, ref func) => {
format!(
"MethodWrapper(<method-wrapper of '{}' object at {:?}>)",
objref.debug_name(),
(func as *const _))
}
&FuncType::Code(_) => format!("{:?}", self)
};
write!(f, "{}", func_type)
}
}
#[derive(Debug)]
pub enum Iterator {
Sequence {source: RtObject, idx_next: Cell<Count>},
Empty,
}
impl Iterator {
pub fn new(source: &RtObject) -> RtResult<Self> {
if is_sequence(source) {
return Ok(Iterator::Sequence {source: source.clone(), idx_next: Cell::new(0)})
}
Err(Error::typerr(
&format!("'{}' is not a sequence", source.debug_name())))
}
}
#[derive(Debug)]
pub struct Object {
pub class: RtObject,
pub dict: RtObject,
pub bases: RtObject,
}
#[allow(dead_code)]
pub struct Module {
pub name: RtObject,
}
#[allow(dead_code)]
pub enum Number {
Int(Integer),
Float(Float),
Bool(Boolean),
Complex(Complex),
}
#[derive(Debug)]
pub struct Type {
pub name: String,
pub module: String,
pub bases: Tuple,
pub subclasses: std::cell::RefCell<List>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Code {
pub co_name: String,
pub co_names: Vec<String>,
pub co_varnames: Vec<String>,
pub co_code: Vec<Instr>,
pub co_consts: Vec<Code>,
}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize)]
pub struct Block {}
#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize)]
pub struct Frame {
pub f_back: RtObject,
pub f_code: RtObject,
pub f_builtins: RtObject,
#[serde(serialize_with = "serialize::integer")]
pub f_lasti: Integer,
pub blocks: VecDeque<Block>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Signature {
args: Box<[String]>,
required_kwargs: Box<[String]>,
vargs: Option<String>,
default_kwargs: std::collections::HashMap<String, RtObject>,
kwargs: Option<String>,
}
pub trait SignatureBuilder {
fn as_args(&self) -> Signature;
}
impl Signature {
pub fn new(args: &[&str],
required_kwargs: &[&str],
vargs: Option<&str>,
kwargs: Option<&str>) -> Self {
Signature {
args: args.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.into_boxed_slice(),
required_kwargs: required_kwargs.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.into_boxed_slice(),
vargs: vargs.map(|s| s.to_string()),
default_kwargs: HashMap::new(),
kwargs: kwargs.map(|s| s.to_string()),
}
}
pub fn args(&self) -> &[String] {
&(*self.args)
}
pub fn has_vargs(&self) -> Boolean {
self.vargs.is_some()
}
pub fn has_kwargs(&self) -> Boolean {
self.kwargs.is_some()
}
pub fn vargs(&self) -> Option<&str> {
match self.vargs {
Some(ref string) => Some(&string),
Option::None => Option::None
}
}
pub fn kwargs(&self) -> Option<&str> {
match self.kwargs {
Some(ref string) => Some(&string),
Option::None => Option::None
}
}
pub fn min_arg_count(&self) -> Integer {
Integer::from(self.args.len() + self.required_kwargs.len())
}
pub fn max_arg_count(&self) -> Option<Integer> {
if self.vargs.is_some() || self.kwargs.is_some() {
return Option::None
}
Some(Integer::from(
self.args.len() +
self.required_kwargs.len() +
self.default_kwargs.len()))
}
}
impl Default for Signature {
fn default() -> Signature {
Signature::new(&[][..], &[], Option::None, Option::None)
}
}
macro_rules! signature_impls {
($i:ty, $($N:expr)+) => {
$(
impl<'a> SignatureBuilder for [$i; $N] {
fn as_args(&self) -> Signature {
Signature::new(&self[..], &[], Option::None, Option::None)
}
}
impl<'a> SignatureBuilder for &'a [$i; $N] {
fn as_args(&self) -> Signature {
Signature::new(&self[..], &[], Option::None, Option::None)
}
}
)+
};
}
signature_impls!(&'a str, 0 1 2 3 4 5 6);
impl<'a> SignatureBuilder for &'a [String] {
fn as_args(&self) -> Signature {
let arr = self.iter().map(String::as_str).collect::<Vec<&str>>();
Signature::new(&arr[..], &[], Option::None, Option::None)
}
}
impl SignatureBuilder for Vec<String> {
fn as_args(&self) -> Signature {
let arr: Vec<&str> = self.iter().map(String::as_str).collect::<Vec<&str>>();
Signature::new(&arr[..], &[], Option::None, Option::None)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct Instr(pub OpCode, pub Option<Native>);
impl Instr {
pub fn to_tuple(&self) -> (OpCode, Option<Native>) {
(self.0.clone(), self.1.clone())
}
pub fn code(&self) -> OpCode {
return self.0.clone()
}
pub fn value(&self) -> Option<Native> {
return self.1.clone()
}
}
#[derive(Debug, Clone, Serialize)]
pub enum Native {
Str(String),
Int(
#[serde(serialize_with = "serialize::integer")]
Integer
),
Float(Float),
Bool(Boolean),
Complex(
#[serde(with = "ComplexSerdeDef")]
Complex
),
Count(Count),
Code(Code),
List(List),
None,
}
impl<'a> From<&'a OwnedTk> for Native {
fn from(tk: &'a OwnedTk) -> Self {
let parsed = String::from_utf8(tk.bytes().to_vec()).unwrap();
let content = parsed.as_str();
match (tk.id(), tk.tag()) {
(Id::Name, _) => Native::Str(parsed.clone()),
(Id::String, _) |
(Id::RawString, _) |
(Id::FormatString, _) |
(Id::ByteString, _) => {
Native::Str(parsed[1..parsed.len()-1].to_string())
},
(Id::Number, Tag::N(Num::Int)) => Native::Int(Integer::from_str(&parsed).unwrap()),
(Id::Number, Tag::N(Num::Binary))=> Native::Int(Integer::from_str_radix(&parsed[2..], 2).unwrap()),
(Id::Number, Tag::N(Num::Octal)) => Native::Int(Integer::from_str_radix(&parsed[2..], 8).unwrap()),
(Id::Number, Tag::N(Num::Hex)) => Native::Int(Integer::from_str_radix(&parsed[2..], 16).unwrap()),
(Id::Number, Tag::N(Num::Float)) => Native::Float(content.parse::<f64>().unwrap()),
(Id::Number, Tag::N(Num::Complex)) => {
let real: Float = 0.0;
let img: Float = content[..content.len()-1].parse::<f64>().unwrap();
Native::Complex(Complex::new(real, img))
},
(Id::True, _) => Native::Bool(true),
(Id::False, _) => Native::Bool(false),
(Id::None, _) => Native::None,
_ => unimplemented!()
}
}
}
impl<'a> From<&'a str> for Native {
fn from(s: &'a str) -> Self {
Native::Str(s.to_string())
}
}
#[derive(Serialize)]
#[serde(remote = "Complex")]
#[doc(hidden)]
struct ComplexSerdeDef {
pub re: Float,
pub im: Float
}
pub mod serialize {
use super::*;
const JSON_MAX_INT_BITS_LOSSLESS: usize = 54;
pub fn integer<S>(int: &Integer, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer
{
if int.bits() > JSON_MAX_INT_BITS_LOSSLESS {
serializer.serialize_str(&format!("{}", int))
} else {
serializer.serialize_i64(int.to_i64().unwrap())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use python_ast::fmt::json;
#[test]
fn value_serialization() {
info!("{}", json(&Native::Int(
Integer::from_str("12341234124312423143214132432145932958392853094543214324").unwrap())));
info!("{}", json(&Native::Complex(Complex::new(234.345, 622.9900000000001))));
}
}