seahorse_dev/core/compile/builtin/
mod.rs1use 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
10pub trait BuiltinSource: std::fmt::Debug + Clone + PartialEq {
14 fn name(&self) -> String;
16
17 fn ty(&self) -> Ty;
19
20 fn as_instance(&self, params: &Vec<Ty>) -> CResult<()>;
24
25 fn attr(&self, attr: &String) -> Option<(Ty, Ty)>;
27
28 fn index(&self) -> Option<(Ty, Ty)>;
30
31 fn static_attr(&self, attr: &String) -> Option<Ty>;
33
34 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
63impl 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}