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
use serde::{Deserialize, Serialize};
use tree_sitter::Node;

use crate::{
    value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, Type, Value, Yield,
};

use super::{function_call::FunctionCall, logic::Logic, math::Math};

/// Abstract representation of an expression statement.
///
/// Unlike statements, which can involve complex logic, an expression is
/// expected to evaluate to a value. However, an expression can still contain
/// nested statements and may evaluate to an empty value.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Expression {
    Value(ValueNode),
    Identifier(Identifier),
    Index(Box<Index>),
    Math(Box<Math>),
    Logic(Box<Logic>),
    FunctionCall(Box<FunctionCall>),
    Yield(Box<Yield>),
}

impl AbstractTree for Expression {
    fn from_syntax_node(source: &str, node: Node, context: &Map) -> Result<Self> {
        Error::expect_syntax_node(source, "expression", node)?;

        let child = if node.child(0).unwrap().is_named() {
            node.child(0).unwrap()
        } else {
            node.child(1).unwrap()
        };

        let expression = match child.kind() {
            "value" => Expression::Value(ValueNode::from_syntax_node(source, child, context)?),
            "identifier" => {
                Expression::Identifier(Identifier::from_syntax_node(source, child, context)?)
            }
            "index" => {
                Expression::Index(Box::new(Index::from_syntax_node(source, child, context)?))
            }
            "math" => Expression::Math(Box::new(Math::from_syntax_node(source, child, context)?)),
            "logic" => {
                Expression::Logic(Box::new(Logic::from_syntax_node(source, child, context)?))
            }
            "function_call" => Expression::FunctionCall(Box::new(FunctionCall::from_syntax_node(
                source, child, context,
            )?)),
            "yield" => {
                Expression::Yield(Box::new(Yield::from_syntax_node(source, child, context)?))
            }
            _ => {
                return Err(Error::UnexpectedSyntaxNode {
                    expected: "value_node, identifier, index, math, logic, function_call or yield"
                        .to_string(),
                    actual: child.kind().to_string(),
                    location: child.start_position(),
                    relevant_source: source[child.byte_range()].to_string(),
                })
            }
        };

        Ok(expression)
    }

    fn run(&self, source: &str, context: &Map) -> Result<Value> {
        match self {
            Expression::Value(value_node) => value_node.run(source, context),
            Expression::Identifier(identifier) => identifier.run(source, context),
            Expression::Math(math) => math.run(source, context),
            Expression::Logic(logic) => logic.run(source, context),
            Expression::FunctionCall(function_call) => function_call.run(source, context),
            Expression::Index(index) => index.run(source, context),
            Expression::Yield(r#yield) => r#yield.run(source, context),
        }
    }

    fn expected_type(&self, context: &Map) -> Result<Type> {
        match self {
            Expression::Value(value_node) => value_node.expected_type(context),
            Expression::Identifier(identifier) => identifier.expected_type(context),
            Expression::Math(math) => math.expected_type(context),
            Expression::Logic(logic) => logic.expected_type(context),
            Expression::FunctionCall(function_call) => function_call.expected_type(context),
            Expression::Index(index) => index.expected_type(context),
            Expression::Yield(r#yield) => r#yield.expected_type(context),
        }
    }
}