roan_engine/interpreter/
structs.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::{
    context::Context,
    module::{ExportType, Module, StoredImpl, StoredStruct, StoredTraitImpl},
    value::Value,
    vm::VM,
};
use anyhow::Result;
use log::debug;
use roan_ast::{Struct, StructConstructor, StructImpl, TraitDef, TraitImpl};
use roan_error::{error::RoanError, TextSpan};
use std::collections::HashMap;

impl Module {
    /// Interpret a struct implementation.
    ///
    /// # Arguments
    /// * `impl_stmt` - [`StructImpl`] - The struct implementation to interpret.
    pub fn interpret_struct_impl(
        &mut self,
        impl_stmt: StructImpl,
        ctx: &mut Context,
    ) -> Result<()> {
        let struct_name = impl_stmt.struct_name.literal();

        let mut found_struct = self.get_struct(&struct_name, impl_stmt.struct_name.span.clone())?;
        let stored_impl = StoredImpl {
            def: impl_stmt.clone(),
            defining_module: self.id(),
        };
        found_struct.impls.push(stored_impl.clone());

        if let Some(existing_struct) = self
            .structs
            .iter_mut()
            .find(|s| s.name.literal() == struct_name)
        {
            *existing_struct = found_struct;

            if let Some(export) = self.exports.iter_mut().find(|(n, _)| n == &struct_name) {
                if let ExportType::Struct(s) = &mut export.1 {
                    s.impls.push(stored_impl);
                }
            }
        }

        ctx.upsert_module(self.id().clone(), self.clone());

        Ok(())
    }

    /// Interpret a trait implementation.
    ///
    /// # Arguments
    /// * `impl_stmt` - [`TraitImpl`] - The trait implementation to interpret.
    pub fn interpret_trait_impl(&mut self, impl_stmt: TraitImpl, ctx: &mut Context) -> Result<()> {
        let for_name = impl_stmt.struct_name.literal();
        let trait_name = impl_stmt.trait_name.literal();

        let mut struct_def = self.get_struct(&for_name, impl_stmt.struct_name.span.clone())?;
        let trait_def = self.get_trait(&trait_name, impl_stmt.trait_name.span.clone())?;
        if struct_def
            .trait_impls
            .iter()
            .any(|t| t.def.trait_name.literal() == trait_name)
        {
            return Err(RoanError::StructAlreadyImplementsTrait(
                for_name,
                trait_name,
                impl_stmt.trait_name.span.clone(),
            )
            .into());
        }

        let missing_methods: Vec<String> = trait_def
            .methods
            .iter()
            .filter(|m| !impl_stmt.methods.iter().any(|i| i.name == m.name))
            .map(|m| m.name.clone())
            .collect();

        if !missing_methods.is_empty() {
            return Err(RoanError::TraitMethodNotImplemented(
                trait_name,
                missing_methods,
                impl_stmt.trait_name.span.clone(),
            )
            .into());
        }

        let stored_trait_impl = StoredTraitImpl {
            def: impl_stmt.clone(),
            defining_module: self.id(),
        };

        struct_def.trait_impls.push(stored_trait_impl.clone());

        if let Some(existing_struct) = self
            .structs
            .iter_mut()
            .find(|s| s.name.literal() == for_name)
        {
            *existing_struct = struct_def;

            if let Some(export) = self.exports.iter_mut().find(|(n, _)| n == &for_name) {
                if let ExportType::Struct(s) = &mut export.1 {
                    s.trait_impls.push(stored_trait_impl);
                }
            }
        }

        ctx.upsert_module(self.id().clone(), self.clone());

        Ok(())
    }

    pub fn get_trait(&self, name: &str, span: TextSpan) -> Result<TraitDef> {
        Ok(self
            .traits
            .iter()
            .find(|t| t.name.literal() == name)
            .cloned()
            .ok_or_else(|| RoanError::TraitNotFoundError(name.into(), span))?)
    }

    pub fn get_struct(&self, name: &str, span: TextSpan) -> Result<StoredStruct> {
        let x = self.structs.iter().find(|s| s.name.literal() == name);

        Ok(x.cloned()
            .ok_or_else(|| RoanError::StructNotFoundError(name.into(), span))?)
    }

    /// Interpret a struct definition.
    ///
    /// # Arguments
    /// * `struct_def` - [`Struct`] - The struct definition to interpret.
    /// * `ctx` - [`Context`] - The context in which to interpret the struct definition.
    ///
    /// # Returns
    /// The result of interpreting the struct definition.
    pub fn interpret_struct(&mut self, struct_stmt: Struct, ctx: &mut Context) -> Result<()> {
        let def = struct_stmt.clone();
        let stored_struct = StoredStruct {
            defining_module: self.id(),
            struct_token: def.struct_token,
            name: def.name,
            fields: def.fields,
            public: def.public,
            impls: vec![],
            trait_impls: vec![],
        };

        self.structs.push(stored_struct.clone());

        if struct_stmt.public {
            self.exports.push((
                struct_stmt.name.literal(),
                ExportType::Struct(stored_struct),
            ));
        }

        ctx.upsert_module(self.id().clone(), self.clone());

        Ok(())
    }

    /// Interpret trait definition.
    ///
    /// # Arguments
    /// * `trait_def` - [`TraitDef`] - The trait definition to interpret.
    /// * `ctx` - [`Context`] - The context in which to interpret the trait definition.
    ///
    /// # Returns
    /// The result of interpreting the trait definition.
    pub fn interpret_trait(&mut self, trait_stmt: TraitDef, ctx: &mut Context) -> Result<()> {
        self.traits.push(trait_stmt.clone());

        if trait_stmt.public {
            self.exports.push((
                trait_stmt.name.literal(),
                ExportType::Trait(trait_stmt.clone()),
            ));
        }

        ctx.upsert_module(self.id().clone(), self.clone());

        Ok(())
    }

    /// Interpret a struct constructor expression.
    ///
    /// # Arguments
    /// * `constructor` - [StructConstructor] expression to interpret.
    /// * `ctx` - The context in which to interpret the struct constructor expression.
    /// * `vm` - The virtual machine to use.
    ///
    /// # Returns
    /// The result of the struct constructor expression.
    pub fn interpret_struct_constructor(
        &mut self,
        constructor: StructConstructor,
        ctx: &mut Context,
        vm: &mut VM,
    ) -> Result<Value> {
        debug!("Interpreting struct constructor");
        let found = self.get_struct(&constructor.name, constructor.token.span.clone())?;

        let mut fields = HashMap::new();

        for (field_name, expr) in constructor.fields.iter() {
            self.interpret_expr(expr, ctx, vm)?;
            fields.insert(field_name.clone(), vm.pop().unwrap());
        }

        Ok(Value::Struct(found, fields))
    }
}