Add modules for class and function objects.

This commit is contained in:
Adam Kelly committed 2018-08-12 18:27:03 +01:00
1 parent 9776385ce9
commit cb3bd23cb1
3 files changed
+59

No files matched your search

+2
View File
@@ -12,7 +12,9 @@ pub mod eval;
mod frame;
mod import;
mod objbool;
mod objclass;
mod objdict;
mod objfunction;
mod objint;
mod objlist;
mod objsequence;
+29
View File
@@ -0,0 +1,29 @@
use super::pyobject::AttributeProtocol;
use super::pyobject::{PyFuncArgs, PyObjectKind, PyObjectRef, PyResult, TypeProtocol};
use super::vm::VirtualMachine;
pub fn get_attribute(
vm: &mut VirtualMachine,
cls: PyObjectRef,
obj: PyObjectRef,
name: &String,
) -> PyResult {
if obj.has_attr(name) {
Ok(obj.get_attr(name))
} else if cls.has_attr(name) {
let attr = cls.get_attr(name);
let attr_class = attr.typ();
if attr_class.has_attr(&String::from("__get__")) {
vm.invoke(
attr_class.get_attr(&String::from("__get__")),
PyFuncArgs {
args: vec![attr, cls],
},
)
} else {
Ok(attr)
}
} else {
Err(vm.new_exception(String::from("TypeError goes here!")))
}
}
+28
View File
@@ -0,0 +1,28 @@
use super::pyobject::{PyFuncArgs, PyObject, PyObjectKind, PyObjectRef, PyResult, TypeProtocol};
use super::vm::VirtualMachine;
use std::collections::HashMap;
pub fn create_type(type_type: PyObjectRef) -> PyObjectRef {
let mut dict = HashMap::new();
dict.insert(
"__get__".to_string(),
PyObject::new(
PyObjectKind::RustFunction {
function: bind_method,
},
type_type.clone(),
),
);
let typ = PyObject::new(
PyObjectKind::Class {
name: "function".to_string(),
dict: PyObject::new(PyObjectKind::Dict { elements: dict }, type_type.clone()),
},
type_type.clone(),
);
typ
}
fn bind_method(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
unimplemented!("not yet!");
}