Skip to main content

seahorse_dev/core/compile/builtin/
mod.rs

1use crate::core::{compile::check::*, util::*};
2
3pub mod prelude;
4pub mod pyth;
5pub mod python;
6pub use prelude::Prelude;
7pub use pyth::Pyth;
8pub use python::Python;
9
10/// A trait to help keep the API consistent.
11///
12/// Represents a single source of builtin objects.
13pub trait BuiltinSource: std::fmt::Debug + Clone + PartialEq {
14    /// Get the name of this builtin.
15    fn name(&self) -> String;
16
17    /// Get the (raw) type of this builtin - either a `Ty::Type` or a `Ty::Function`.
18    fn ty(&self) -> Ty;
19
20    /// Check if an instance of this type can be created with the given type parameters.
21    ///
22    /// Returns a `Result` type instead of just a bool to allow for more customized error messages.
23    fn as_instance(&self, params: &Vec<Ty>) -> CResult<()>;
24
25    /// Get the type of an attribute of this object. Returns two types: (this, attribute).
26    fn attr(&self, attr: &String) -> Option<(Ty, Ty)>;
27
28    /// Get a function for this builtin's index operation. Returns two types: (this, indexed).
29    fn index(&self) -> Option<(Ty, Ty)>;
30
31    /// Get the type of a static attribute of this object.
32    fn static_attr(&self, attr: &String) -> Option<Ty>;
33
34    /// Get the type of a cast from `ty` to an instance of this builtin. Returns two types: (this,
35    /// casted). The original (actual) type gets unified with the this type, and the expected type
36    /// gets unified with the casted type and returned.
37    fn casted(&self, ty: &Ty) -> Option<(Ty, Ty)>;
38}
39
40#[derive(Clone, Debug, PartialEq)]
41pub enum Builtin {
42    Python(Python),
43    Prelude(Prelude),
44    Pyth(Pyth),
45}
46
47impl From<python::Python> for Builtin {
48    fn from(builtin: python::Python) -> Self {
49        Self::Python(builtin)
50    }
51}
52
53macro_rules! match_builtin {
54    ($self:expr, $builtin:pat, $func:expr) => {
55        match $self {
56            Self::Python($builtin) => $func,
57            Self::Prelude($builtin) => $func,
58            Self::Pyth($builtin) => $func,
59        }
60    };
61}
62
63// Impl `BuiltinSource` here for convenience.
64impl BuiltinSource for Builtin {
65    fn name(&self) -> String {
66        match_builtin!(self, builtin, builtin.name())
67    }
68
69    fn ty(&self) -> Ty {
70        match_builtin!(self, builtin, builtin.ty())
71    }
72
73    fn as_instance(&self, params: &Vec<Ty>) -> CResult<()> {
74        match_builtin!(self, builtin, builtin.as_instance(params))
75    }
76
77    fn attr(&self, attr: &String) -> Option<(Ty, Ty)> {
78        match_builtin!(self, builtin, builtin.attr(attr))
79    }
80
81    fn index(&self) -> Option<(Ty, Ty)> {
82        match_builtin!(self, builtin, builtin.index())
83    }
84
85    fn static_attr(&self, attr: &String) -> Option<Ty> {
86        match_builtin!(self, builtin, builtin.static_attr(attr))
87    }
88
89    fn casted(&self, ty: &Ty) -> Option<(Ty, Ty)> {
90        match_builtin!(self, builtin, builtin.casted(ty))
91    }
92}