swamp_script_analyzer/
operator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */

use crate::err::ResolveError;
use crate::Resolver;
use swamp_script_ast::{BinaryOperator, Expression, PostfixOperator, UnaryOperator};
use swamp_script_semantic::{
    ResolvedBinaryOperator, ResolvedBinaryOperatorKind, ResolvedNode, ResolvedPostfixOperator,
    ResolvedPostfixOperatorKind, ResolvedType, ResolvedUnaryOperator, ResolvedUnaryOperatorKind,
    Spanned,
};
use tracing::debug;

impl<'a> Resolver<'a> {
    pub(crate) fn resolve_binary_op(
        &mut self,
        ast_left: &Expression,
        ast_op: &BinaryOperator,
        ast_right: &Expression,
    ) -> Result<ResolvedBinaryOperator, ResolveError> {
        let left = self.resolve_expression(ast_left)?;
        let left_type = left.resolution();

        let right = self.resolve_expression(ast_right)?;
        let right_type = right.resolution();

        let (resolved_node, kind) = self.convert_binary_operator_kind(ast_op);

        match (&kind, &left_type, &right_type) {
            // String concatenation - allow any type on the right
            (&ResolvedBinaryOperatorKind::Add, ResolvedType::String, _) => {
                Ok(ResolvedBinaryOperator {
                    left: Box::new(left),
                    right: Box::new(right),
                    kind,
                    node: resolved_node,
                    resolved_type: ResolvedType::String,
                })
            }

            // Comparison operators
            (
                ResolvedBinaryOperatorKind::Equal
                | ResolvedBinaryOperatorKind::NotEqual
                | ResolvedBinaryOperatorKind::GreaterThan
                | ResolvedBinaryOperatorKind::GreaterEqual
                | ResolvedBinaryOperatorKind::LessThan
                | ResolvedBinaryOperatorKind::LessEqual,
                _,
                _,
            ) => {
                if !left_type.same_type(&right_type) {
                    debug!(?left_type, ?right_type, "type mismatch in comparison");
                    return Err(ResolveError::IncompatibleTypes(left.span(), right_type));
                }
                Ok(ResolvedBinaryOperator {
                    left: Box::new(left),
                    right: Box::new(right),
                    kind,
                    node: resolved_node,
                    resolved_type: ResolvedType::Bool,
                })
            }

            // All other operators require exact type matches
            _ => {
                if !left_type.same_type(&right_type) {
                    debug!(?left_type, ?right_type, "type mismatch in operation");
                    return Err(ResolveError::IncompatibleTypes(left.span(), right_type));
                }
                Ok(ResolvedBinaryOperator {
                    left: Box::new(left),
                    right: Box::new(right),
                    kind,
                    node: resolved_node,
                    resolved_type: left_type,
                })
            }
        }
    }

    pub(crate) fn resolve_unary_op(
        &mut self,
        ast_op: &UnaryOperator,
        ast_left: &Expression,
    ) -> Result<ResolvedUnaryOperator, ResolveError> {
        let left = self.resolve_expression(ast_left)?;
        let resolved_type = left.resolution();

        let (node, kind) = match ast_op {
            UnaryOperator::Not(node) => (node, ResolvedUnaryOperatorKind::Not),
            UnaryOperator::Negate(node) => (node, ResolvedUnaryOperatorKind::Negate),
        };

        Ok(ResolvedUnaryOperator {
            left: Box::new(left),
            resolved_type,
            kind,
            node: self.to_node(node),
        })
    }

    pub(crate) fn resolve_postfix_op(
        &mut self,
        ast_op: &PostfixOperator,
        ast_left: &Expression,
    ) -> Result<ResolvedPostfixOperator, ResolveError> {
        let left = self.resolve_expression(ast_left)?;
        let resolved_type = left.resolution();

        let (resolved_node, resolved_op_kind) = match ast_op {
            PostfixOperator::Unwrap(node) => {
                (self.to_node(node), ResolvedPostfixOperatorKind::Unwrap)
            }
        };

        Ok(ResolvedPostfixOperator {
            left: Box::new(left),
            kind: resolved_op_kind,
            resolved_type,
            node: resolved_node,
        })
    }

    const fn convert_binary_operator_kind(
        &self,
        binary_operator: &BinaryOperator,
    ) -> (ResolvedNode, ResolvedBinaryOperatorKind) {
        match binary_operator {
            BinaryOperator::Add(node) => (self.to_node(node), ResolvedBinaryOperatorKind::Add),
            BinaryOperator::Subtract(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::Subtract)
            }
            BinaryOperator::Multiply(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::Multiply)
            }
            BinaryOperator::Divide(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::Divide)
            }
            BinaryOperator::Modulo(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::Modulo)
            }
            BinaryOperator::LogicalOr(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::LogicalOr)
            }
            BinaryOperator::LogicalAnd(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::LogicalAnd)
            }
            BinaryOperator::Equal(node) => (self.to_node(node), ResolvedBinaryOperatorKind::Equal),
            BinaryOperator::NotEqual(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::NotEqual)
            }
            BinaryOperator::LessThan(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::LessThan)
            }
            BinaryOperator::LessEqual(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::LessEqual)
            }
            BinaryOperator::GreaterThan(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::GreaterThan)
            }
            BinaryOperator::GreaterEqual(node) => {
                (self.to_node(node), ResolvedBinaryOperatorKind::GreaterEqual)
            }
            BinaryOperator::RangeExclusive(node) => (
                self.to_node(node),
                ResolvedBinaryOperatorKind::RangeExclusive,
            ),
        }
    }
}