quickpython 0.1.3

A lightweight Python bytecode VM written in Rust
Documentation
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
use crate::bytecode::ByteCode;
use regex::Regex;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

/// Native function type - Rust functions callable from Python
pub type NativeFunction = fn(Vec<Value>) -> Result<Value, Value>;

/// Module structure
#[derive(Clone)]
pub struct Module {
    pub name: String,
    pub attributes: HashMap<String, Value>,
}

impl Module {
    pub fn new(name: &str) -> Self {
        Module {
            name: name.to_string(),
            attributes: HashMap::new(),
        }
    }

    pub fn add_function(&mut self, name: &str, func: NativeFunction) {
        self.attributes
            .insert(name.to_string(), Value::NativeFunction(func));
    }

    pub fn get_attribute(&self, name: &str) -> Option<Value> {
        self.attributes.get(name).cloned()
    }
}

impl std::fmt::Debug for Module {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Module")
            .field("name", &self.name)
            .field("attributes", &self.attributes.keys().collect::<Vec<_>>())
            .finish()
    }
}

/// Match object for regex matches
#[derive(Debug, Clone)]
pub struct MatchObject {
    pub text: String,
    pub start: usize,
    pub end: usize,
    pub groups: Vec<Option<String>>,
}

impl MatchObject {
    pub fn new(text: String, start: usize, end: usize, groups: Vec<Option<String>>) -> Self {
        MatchObject {
            text,
            start,
            end,
            groups,
        }
    }
}

/// List value with version tracking for iterator modification detection
#[derive(Debug, Clone)]
pub struct ListValue {
    pub items: Vec<Value>,
    pub version: usize,
}

impl ListValue {
    pub fn new() -> Self {
        ListValue {
            items: Vec::new(),
            version: 0,
        }
    }

    pub fn with_items(items: Vec<Value>) -> Self {
        ListValue { items, version: 0 }
    }

    pub fn increment_version(&mut self) {
        self.version = self.version.wrapping_add(1);
    }
}

/// Dictionary key type - only String and Int are supported
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DictKey {
    String(String),
    Int(i32),
}

/// Iterator state for different types
#[derive(Debug, Clone)]
pub enum IteratorState {
    Range {
        current: i32,
        stop: i32,
        step: i32,
    },
    List {
        list: Rc<RefCell<ListValue>>,
        index: usize,
        version: usize, // Version at iterator creation time
    },
    DictKeys {
        keys: Vec<DictKey>,
        index: usize,
    },
    String {
        chars: Vec<char>,
        index: usize,
    },
}

/// Exception type enumeration
#[derive(Debug, Clone, PartialEq)]
pub enum ExceptionType {
    Exception,         // 基础异常
    RuntimeError,      // 运行时错误
    IndexError,        // 索引越界
    KeyError,          // 键不存在
    ValueError,        // 值错误
    TypeError,         // 类型错误
    ZeroDivisionError, // 除零错误
    IteratorError,     // 迭代器错误(自定义)
    OSError,           // 操作系统错误
    AttributeError,    // 属性错误
}

impl ExceptionType {
    pub fn as_i32(&self) -> i32 {
        match self {
            ExceptionType::Exception => 0,
            ExceptionType::RuntimeError => 1,
            ExceptionType::IndexError => 2,
            ExceptionType::KeyError => 3,
            ExceptionType::ValueError => 4,
            ExceptionType::TypeError => 5,
            ExceptionType::ZeroDivisionError => 6,
            ExceptionType::IteratorError => 7,
            ExceptionType::OSError => 8,
            ExceptionType::AttributeError => 9,
        }
    }

    pub fn from_i32(value: i32) -> Option<Self> {
        match value {
            0 => Some(ExceptionType::Exception),
            1 => Some(ExceptionType::RuntimeError),
            2 => Some(ExceptionType::IndexError),
            3 => Some(ExceptionType::KeyError),
            4 => Some(ExceptionType::ValueError),
            5 => Some(ExceptionType::TypeError),
            6 => Some(ExceptionType::ZeroDivisionError),
            7 => Some(ExceptionType::IteratorError),
            8 => Some(ExceptionType::OSError),
            9 => Some(ExceptionType::AttributeError),
            _ => None,
        }
    }

    /// Check if this exception type matches the handler type
    /// Exception matches all types (it's the base class)
    pub fn matches(&self, handler: &ExceptionType) -> bool {
        match handler {
            ExceptionType::Exception => true, // Exception catches everything
            _ => self == handler,             // Otherwise exact match
        }
    }
}

/// Traceback frame for exception
#[derive(Debug, Clone)]
pub struct TracebackFrame {
    pub function_name: String,
    pub line_number: usize,
}

/// Exception value structure
#[derive(Debug, Clone)]
pub struct ExceptionValue {
    pub exception_type: ExceptionType,
    pub message: String,
    pub traceback: Option<Vec<TracebackFrame>>,
}

/// Value type for QuickPython runtime
#[derive(Clone)]
pub enum Value {
    Int(i32),
    Float(f64),
    Bool(bool),
    None,
    String(String),
    List(Rc<RefCell<ListValue>>),
    Dict(Rc<RefCell<HashMap<DictKey, Value>>>),
    Tuple(Rc<Vec<Value>>), // Immutable sequence
    Slice {
        start: Option<i32>,
        stop: Option<i32>,
        step: Option<i32>,
    },
    Iterator(Rc<RefCell<IteratorState>>),
    Function(Function),
    Coroutine(Function, Vec<Value>), // (async function, captured args)
    Generator(Rc<RefCell<GeneratorState>>), // Generator object
    AsyncSleep(f64),                 // Async sleep operation (seconds)
    Exception(ExceptionValue),
    Module(Rc<RefCell<Module>>),
    NativeFunction(NativeFunction),
    BoundMethod(Box<Value>, String), // (receiver, method_name)
    Regex(Rc<Regex>),
    Match(Rc<MatchObject>),
    Type(TypeObject),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeObject {
    Int,
    Float,
    Bool,
    Str,
    List,
    Dict,
    Tuple,
    NoneType,
}

impl std::fmt::Debug for Value {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Value::Int(i) => write!(f, "Int({})", i),
            Value::Float(fl) => write!(f, "Float({})", fl),
            Value::Bool(b) => write!(f, "Bool({})", b),
            Value::None => write!(f, "None"),
            Value::String(s) => write!(f, "String({:?})", s),
            Value::List(l) => write!(f, "List({:?})", l),
            Value::Dict(d) => write!(f, "Dict({:?})", d),
            Value::Tuple(t) => write!(f, "Tuple({:?})", t),
            Value::Slice { start, stop, step } => {
                write!(f, "Slice({:?}:{:?}:{:?})", start, stop, step)
            }
            Value::Iterator(i) => write!(f, "Iterator({:?})", i),
            Value::Function(func) => write!(f, "Function({:?})", func),
            Value::Coroutine(func, _) => write!(f, "Coroutine({:?})", func.name),
            Value::Generator(gen_rc) => {
                let gen_state = gen_rc.borrow();
                write!(
                    f,
                    "Generator({}, finished={})",
                    gen_state.function.name, gen_state.finished
                )
            }
            Value::AsyncSleep(seconds) => write!(f, "AsyncSleep({})", seconds),
            Value::Exception(e) => write!(f, "Exception({:?})", e),
            Value::Module(m) => write!(f, "Module({:?})", m),
            Value::NativeFunction(_) => write!(f, "NativeFunction(<native>)"),
            Value::BoundMethod(_, method_name) => write!(f, "BoundMethod(<{}>)", method_name),
            Value::Regex(_) => write!(f, "Regex(<pattern>)"),
            Value::Match(m) => write!(f, "Match({:?})", m),
            Value::Type(t) => write!(f, "Type({:?})", t),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Function {
    pub name: String,
    pub params: Vec<String>,
    pub code: ByteCode,
    pub is_async: bool,
    pub is_generator: bool,
}

/// Generator state for yield/resume
#[derive(Debug, Clone)]
pub struct GeneratorState {
    pub function: Function,
    pub locals: Vec<Value>,
    pub ip: usize,
    pub stack: Vec<Value>,
    pub finished: bool,
}

impl Value {
    pub fn as_int(&self) -> Option<i32> {
        match self {
            Value::Int(i) => Some(*i),
            _ => None,
        }
    }

    pub fn as_float(&self) -> Option<f64> {
        match self {
            Value::Float(f) => Some(*f),
            _ => None,
        }
    }

    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(b) => Some(*b),
            _ => None,
        }
    }

    pub fn as_string(&self) -> Option<&str> {
        match self {
            Value::String(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_list(&self) -> Option<Rc<RefCell<ListValue>>> {
        match self {
            Value::List(list) => Some(list.clone()),
            _ => None,
        }
    }

    pub fn as_dict(&self) -> Option<Rc<RefCell<HashMap<DictKey, Value>>>> {
        match self {
            Value::Dict(dict) => Some(dict.clone()),
            _ => None,
        }
    }

    /// Create an exception value
    pub fn error(exception_type: ExceptionType, message: impl Into<String>) -> Value {
        Value::Exception(ExceptionValue {
            exception_type,
            message: message.into(),
            traceback: None,
        })
    }

    /// Check if value is an exception
    pub fn is_exception(&self) -> bool {
        matches!(self, Value::Exception(_))
    }

    /// Get exception value
    pub fn as_exception(&self) -> Option<&ExceptionValue> {
        match self {
            Value::Exception(exc) => Some(exc),
            _ => None,
        }
    }

    pub fn is_truthy(&self) -> bool {
        match self {
            Value::Bool(b) => *b,
            Value::Int(i) => *i != 0,
            Value::Float(f) => *f != 0.0,
            Value::None => false,
            Value::String(s) => !s.is_empty(),
            Value::List(list) => !list.borrow().items.is_empty(),
            Value::Dict(dict) => !dict.borrow().is_empty(),
            Value::Tuple(tuple) => !tuple.is_empty(),
            Value::Slice { .. } => true,
            Value::Iterator(_) => true,
            Value::Function(_) => true,
            Value::Coroutine(_, _) => true,
            Value::Generator(_) => true,
            Value::AsyncSleep(_) => true,
            Value::Exception(_) => true,
            Value::Module(_) => true,
            Value::NativeFunction(_) => true,
            Value::BoundMethod(_, _) => true,
            Value::Regex(_) => true,
            Value::Match(_) => true,
            Value::Type(_) => true,
        }
    }
}

impl PartialEq for Value {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Value::Int(a), Value::Int(b)) => a == b,
            (Value::Float(a), Value::Float(b)) => a == b,
            // Mixed int/float comparison
            (Value::Int(a), Value::Float(b)) => (*a as f64) == *b,
            (Value::Float(a), Value::Int(b)) => *a == (*b as f64),
            (Value::Bool(a), Value::Bool(b)) => a == b,
            (Value::None, Value::None) => true,
            (Value::String(a), Value::String(b)) => a == b,
            (Value::List(a), Value::List(b)) => {
                let a_items = &a.borrow().items;
                let b_items = &b.borrow().items;
                a_items == b_items
            }
            (Value::Dict(a), Value::Dict(b)) => Rc::ptr_eq(a, b),
            (Value::Tuple(a), Value::Tuple(b)) => a.as_ref() == b.as_ref(),
            (
                Value::Slice {
                    start: s1,
                    stop: st1,
                    step: step1,
                },
                Value::Slice {
                    start: s2,
                    stop: st2,
                    step: step2,
                },
            ) => s1 == s2 && st1 == st2 && step1 == step2,
            (Value::Iterator(a), Value::Iterator(b)) => Rc::ptr_eq(a, b),
            (Value::Function(a), Value::Function(b)) => a == b,
            (Value::Coroutine(f1, _), Value::Coroutine(f2, _)) => f1 == f2,
            (Value::Generator(a), Value::Generator(b)) => Rc::ptr_eq(a, b),
            (Value::AsyncSleep(a), Value::AsyncSleep(b)) => a == b,
            (Value::Exception(a), Value::Exception(b)) => {
                a.exception_type == b.exception_type && a.message == b.message
            }
            (Value::Module(a), Value::Module(b)) => Rc::ptr_eq(a, b),
            (Value::NativeFunction(a), Value::NativeFunction(b)) => std::ptr::eq(a, b),
            (Value::BoundMethod(a1, m1), Value::BoundMethod(a2, m2)) => a1 == a2 && m1 == m2,
            (Value::Regex(a), Value::Regex(b)) => Rc::ptr_eq(a, b),
            (Value::Match(a), Value::Match(b)) => Rc::ptr_eq(a, b),
            (Value::Type(a), Value::Type(b)) => a == b,
            _ => false,
        }
    }
}