swamp_script_analyzer/
access.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use crate::Analyzer;
6use crate::TypeContext;
7use crate::err::{Error, ErrorKind};
8use swamp_script_semantic::{Expression, ExpressionKind, Function, FunctionRef, Range, RangeMode};
9use swamp_script_types::prelude::*;
10
11impl Analyzer<'_> {
12    #[must_use]
13    pub fn convert_to_function_access_kind(function_ref: &FunctionRef) -> ExpressionKind {
14        match &**function_ref {
15            Function::Internal(internal_function) => {
16                ExpressionKind::InternalFunctionAccess(internal_function.clone())
17            }
18            Function::External(external_function) => {
19                ExpressionKind::ExternalFunctionAccess(external_function.clone())
20            }
21        }
22    }
23
24    #[must_use]
25    pub fn lookup_associated_function(
26        &self,
27        ty: &Type,
28        function_name: &str,
29    ) -> Option<FunctionRef> {
30        self.shared
31            .state
32            .instantiator
33            .associated_impls
34            .get_member_function(ty, function_name)
35            .cloned()
36    }
37
38    #[must_use]
39    pub fn convert_to_function_access_expr(
40        &self,
41        associated_function_info: &FunctionRef,
42        ast_node: &swamp_script_ast::Node,
43    ) -> Expression {
44        let kind = Self::convert_to_function_access_kind(associated_function_info);
45        self.create_expr(
46            kind,
47            Type::Function(associated_function_info.signature().clone()),
48            ast_node,
49        )
50    }
51
52    pub(crate) fn analyze_static_member_access(
53        &mut self,
54        named_type: &swamp_script_ast::QualifiedTypeIdentifier,
55        member_name_node: &swamp_script_ast::Node,
56    ) -> Result<Expression, Error> {
57        let some_type = self.analyze_named_type(named_type)?;
58        let member_name = self.get_text(member_name_node);
59        self.lookup_associated_function(&some_type, member_name)
60            .map_or_else(
61                || Err(self.create_err(ErrorKind::UnknownMemberFunction, member_name_node)),
62                |member_function| {
63                    let expr =
64                        self.convert_to_function_access_expr(&member_function, member_name_node);
65                    Ok(expr)
66                },
67            )
68    }
69
70    pub(crate) fn analyze_min_max_expr(
71        &mut self,
72        min_expr: &swamp_script_ast::Expression,
73        max_expr: &swamp_script_ast::Expression,
74    ) -> Result<(Expression, Expression), Error> {
75        let context = TypeContext::new_argument(&Type::Int);
76
77        let resolved_min = self.analyze_expression(min_expr, &context)?;
78        let resolved_max = self.analyze_expression(max_expr, &context)?;
79
80        Ok((resolved_min, resolved_max))
81    }
82
83    /// # Errors
84    ///
85    pub fn analyze_range(
86        &mut self,
87        min_expr: &swamp_script_ast::Expression,
88        max_expr: &swamp_script_ast::Expression,
89        mode: &swamp_script_ast::RangeMode,
90    ) -> Result<Range, Error> {
91        let (min, max) = self.analyze_min_max_expr(min_expr, max_expr)?;
92
93        let resolved_range_mode = match mode {
94            swamp_script_ast::RangeMode::Inclusive => RangeMode::Inclusive,
95            swamp_script_ast::RangeMode::Exclusive => RangeMode::Exclusive,
96        };
97        Ok(Range {
98            min,
99            max,
100            mode: resolved_range_mode,
101        })
102    }
103}