Skip to main content

rils_builtins/
numeric.rs

1//! Static descriptions of APIs shipped as part of Rils.
2//!
3//! Nothing in this crate performs I/O or executes script code. Tooling and
4//! runtimes share these declarations while choosing their own implementation
5//! strategy for runtime, intrinsic and host-backed items.
6
7pub use rils_syntax::IntegerType;
8
9/// A recursive type expression independent of the frontend's concrete `Type`.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum TypePattern {
12    SelfType,
13    Generic(&'static str),
14    BoundGeneric {
15        name: &'static str,
16        bounds: &'static [TypePattern],
17    },
18    AnyInteger,
19    Unknown,
20    Unit,
21    Bool,
22    Char,
23    String,
24    F32,
25    F64,
26    U32,
27    U8,
28    Usize,
29    Named {
30        path: &'static str,
31        arguments: &'static [TypePattern],
32    },
33    Option(&'static TypePattern),
34    Result {
35        ok: &'static TypePattern,
36        error: &'static TypePattern,
37    },
38    Tuple(&'static [TypePattern]),
39    Array {
40        element: &'static TypePattern,
41        length: usize,
42    },
43    ArrayParameter {
44        element: &'static TypePattern,
45        length: &'static str,
46    },
47    Slice(&'static TypePattern),
48    Function {
49        parameters: &'static [TypePattern],
50        result: &'static TypePattern,
51    },
52    Reference {
53        mutable: bool,
54        inner: &'static TypePattern,
55    },
56    Associated {
57        base: &'static TypePattern,
58        trait_name: Option<&'static str>,
59        trait_arguments: &'static [TypePattern],
60        name: &'static str,
61        arguments: &'static [TypePattern],
62    },
63}
64
65#[derive(Clone, Copy, Debug)]
66pub struct BuiltinSignature {
67    /// Names from the language declaration, excluding the receiver.
68    pub parameter_names: &'static [&'static str],
69    pub parameters: &'static [TypePattern],
70    /// Required callable capabilities, indexed in `parameters` (without receiver).
71    /// An empty list imposes no invocation contract beyond the type signature.
72    pub parameter_capabilities: &'static [(usize, crate::CallableTraitKind)],
73    pub result: TypePattern,
74    pub variadic: bool,
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum IntrinsicKind {
79    Method,
80    AssociatedFunction,
81}
82
83#[derive(Clone, Copy, Debug)]
84pub struct IntrinsicDeclaration {
85    pub symbol: &'static str,
86    pub name: &'static str,
87    pub kind: IntrinsicKind,
88    pub signature: BuiltinSignature,
89    pub documentation: &'static str,
90}
91
92pub fn integer_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
93    crate::INTEGER_INTRINSICS
94        .iter()
95        .find(|item| item.kind == IntrinsicKind::Method && item.name == name)
96}
97
98pub fn integer_associated_function(name: &str) -> Option<&'static IntrinsicDeclaration> {
99    crate::INTEGER_INTRINSICS
100        .iter()
101        .find(|item| item.kind == IntrinsicKind::AssociatedFunction && item.name == name)
102}
103
104pub fn float_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
105    crate::FLOAT_INTRINSICS
106        .iter()
107        .find(|item| item.name == name)
108}
109
110pub fn intrinsic(symbol: &str) -> Option<&'static IntrinsicDeclaration> {
111    crate::INTEGER_INTRINSICS
112        .iter()
113        .chain(crate::FLOAT_INTRINSICS)
114        .find(|item| item.symbol == symbol)
115}
116
117/// Resolves a numeric operation path, including the target type of an
118/// associated integer function.
119pub fn intrinsic_by_symbol(
120    symbol: &str,
121) -> Option<(&'static IntrinsicDeclaration, Option<IntegerType>)> {
122    if let Some(rest) = symbol.strip_prefix("core::integer::")
123        && let Some((target, method)) = rest.split_once("::")
124        && let Some(target) = IntegerType::from_name(target)
125    {
126        let declaration = integer_associated_function(method)?;
127        return Some((declaration, Some(target)));
128    }
129    crate::INTEGER_INTRINSICS
130        .iter()
131        .chain(crate::FLOAT_INTRINSICS)
132        .find(|declaration| declaration.symbol == symbol)
133        .map(|declaration| (declaration, None))
134}