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
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::fmt::{self, Display, Formatter};

use serde::{Deserialize, Serialize};
use tree_sitter::Node;

use crate::{
    AbstractTree, Block, BuiltInFunction, Error, Identifier, Map, Result, Type, TypeDefinition,
    Value,
};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum Function {
    BuiltIn(BuiltInFunction),
    ContextDefined(ContextDefinedFunction),
}

impl Display for Function {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Function::BuiltIn(built_in_function) => write!(f, "{}", built_in_function.r#type()),
            Function::ContextDefined(context_defined_function) => {
                write!(f, "{}", context_defined_function.r#type())
            }
        }
    }
}

impl Function {
    pub fn call(
        &self,
        name: Option<String>,
        arguments: &[Value],
        source: &str,
        outer_context: &Map,
    ) -> Result<Value> {
        match self {
            Function::BuiltIn(built_in_function) => {
                built_in_function.call(arguments, source, outer_context)
            }
            Function::ContextDefined(context_defined_function) => {
                context_defined_function.call(name, arguments, source, outer_context)
            }
        }
    }

    pub fn r#type(&self) -> Type {
        match self {
            Function::BuiltIn(built_in_function) => built_in_function.r#type(),
            Function::ContextDefined(context_defined_function) => {
                context_defined_function.r#type().clone()
            }
        }
    }
}

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

        let child_count = node.child_count();
        let mut parameters = Vec::new();
        let mut parameter_types = Vec::new();

        for index in 1..child_count - 3 {
            let child = node.child(index).unwrap();

            if child.kind() == "identifier" {
                let identifier = Identifier::from_syntax_node(source, child, context)?;

                parameters.push(identifier);
            }

            if child.kind() == "type_definition" {
                let type_definition = TypeDefinition::from_syntax_node(source, child, context)?;

                parameter_types.push(type_definition.take_inner());
            }
        }

        let function_context = Map::clone_from(context)?;

        for (parameter_name, parameter_type) in parameters.iter().zip(parameter_types.iter()) {
            function_context.set(
                parameter_name.inner().clone(),
                Value::none(),
                Some(parameter_type.clone()),
            )?;
        }

        let return_type_node = node.child(child_count - 2).unwrap();
        let return_type = TypeDefinition::from_syntax_node(source, return_type_node, context)?;

        let body_node = node.child(child_count - 1).unwrap();
        let body = Block::from_syntax_node(source, body_node, &function_context)?;

        return_type
            .inner()
            .check(&body.expected_type(&function_context)?)
            .map_err(|error| error.at_node(body_node, source))?;

        let r#type = Type::function(parameter_types, return_type.take_inner());

        Ok(Self::ContextDefined(ContextDefinedFunction::new(
            parameters, body, r#type,
        )))
    }

    fn run(&self, _source: &str, _context: &Map) -> Result<Value> {
        Ok(Value::Function(self.clone()))
    }

    fn expected_type(&self, _context: &Map) -> Result<Type> {
        match self {
            Function::BuiltIn(built_in) => Ok(built_in.r#type()),
            Function::ContextDefined(context_defined) => Ok(context_defined.r#type().clone()),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct ContextDefinedFunction {
    parameters: Vec<Identifier>,
    body: Block,
    r#type: Type,
}

impl ContextDefinedFunction {
    pub fn new(parameters: Vec<Identifier>, body: Block, r#type: Type) -> Self {
        Self {
            parameters,
            body,
            r#type,
        }
    }

    pub fn parameters(&self) -> &Vec<Identifier> {
        &self.parameters
    }

    pub fn body(&self) -> &Block {
        &self.body
    }

    pub fn r#type(&self) -> &Type {
        &self.r#type
    }

    pub fn return_type(&self) -> &Type {
        match &self.r#type {
            Type::Function {
                parameter_types: _,
                return_type,
            } => return_type.as_ref(),
            _ => &Type::None,
        }
    }

    pub fn call(
        &self,
        name: Option<String>,
        arguments: &[Value],
        source: &str,
        outer_context: &Map,
    ) -> Result<Value> {
        let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter());
        let function_context = Map::clone_from(outer_context)?;

        for (identifier, value) in parameter_argument_pairs {
            let key = identifier.inner().clone();

            function_context.set(key, value.clone(), None)?;
        }

        if let Some(name) = name {
            function_context.set(
                name,
                Value::Function(Function::ContextDefined(self.clone())),
                None,
            )?;
        }

        let return_value = self.body.run(source, &function_context)?;

        Ok(return_value)
    }
}