swamp_script_analyzer/
internal.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 */
5
6use crate::Analyzer;
7use crate::err::{Error, ErrorKind};
8use swamp_script_semantic::prelude::IntrinsicFunction;
9use swamp_script_semantic::{Postfix, PostfixKind};
10use swamp_script_types::prelude::*;
11
12impl Analyzer<'_> {
13    pub(crate) fn check_for_internal_member_call(
14        &mut self,
15        ty: &Type,
16        is_mutable: bool,
17        ast_member_function_name: &swamp_script_ast::Node,
18        ast_arguments: &[&swamp_script_ast::Expression],
19    ) -> Result<Option<Postfix>, Error> {
20        match &ty {
21            Type::Tuple(tuple_type) => {
22                let found = self.analyze_tuple_member_call(
23                    tuple_type,
24                    ast_member_function_name,
25                    ast_arguments,
26                )?;
27                Ok(Some(found))
28            }
29            _ => Ok(None),
30        }
31    }
32    fn analyze_tuple_member_call(
33        &mut self,
34        tuple_type: &[Type],
35        ast_member_function_name: &swamp_script_ast::Node,
36        arguments: &[&swamp_script_ast::Expression],
37    ) -> Result<Postfix, Error> {
38        if tuple_type.len() != 2 {
39            return Err(self.create_err(
40                ErrorKind::WrongNumberOfArguments(2, tuple_type.len()),
41                ast_member_function_name,
42            ));
43        }
44
45        let member_function_name_str = self.get_text(ast_member_function_name);
46
47        let resolved_expr = match (&tuple_type[0], &tuple_type[1]) {
48            (Type::Float, Type::Float) => match member_function_name_str {
49                "magnitude" => {
50                    if !arguments.is_empty() {
51                        return Err(self.create_err(
52                            ErrorKind::WrongNumberOfArguments(arguments.len(), 0),
53                            ast_member_function_name,
54                        ));
55                    }
56                    self.create_postfix(
57                        PostfixKind::IntrinsicCall(IntrinsicFunction::Float2Magnitude, vec![]),
58                        &Type::Float,
59                        ast_member_function_name,
60                    )
61                }
62                _ => {
63                    return Err(
64                        self.create_err(ErrorKind::UnknownMemberFunction, ast_member_function_name)
65                    );
66                }
67            },
68            _ => {
69                return Err(self.create_err(
70                    ErrorKind::WrongNumberOfArguments(99, tuple_type.len()),
71                    ast_member_function_name,
72                ));
73            }
74        };
75
76        Ok(resolved_expr)
77    }
78
79    fn check_mutable(
80        &mut self,
81        is_mutable: bool,
82        node: &swamp_script_ast::Node,
83    ) -> Result<(), Error> {
84        if is_mutable {
85            Ok(())
86        } else {
87            Err(self.create_err(ErrorKind::ExpectedMutableLocation, node))
88        }
89    }
90
91    fn create_postfix(
92        &mut self,
93        kind: PostfixKind,
94        ty: &Type,
95        node: &swamp_script_ast::Node,
96    ) -> Postfix {
97        let resolved_node = self.to_node(node);
98
99        Postfix {
100            node: resolved_node,
101            ty: ty.clone(),
102            kind,
103        }
104    }
105}