ssf/ir/
value_definition.rs1use super::expression::Expression;
2use crate::types::{self, Type};
3use std::collections::{HashMap, HashSet};
4
5#[derive(Clone, Debug, PartialEq)]
6pub struct ValueDefinition {
7 name: String,
8 body: Expression,
9 type_: types::Value,
10}
11
12impl ValueDefinition {
13 pub fn new(
14 name: impl Into<String>,
15 body: impl Into<Expression>,
16 type_: impl Into<types::Value>,
17 ) -> Self {
18 Self {
19 name: name.into(),
20 body: body.into(),
21 type_: type_.into(),
22 }
23 }
24
25 pub fn name(&self) -> &str {
26 &self.name
27 }
28
29 pub fn body(&self) -> &Expression {
30 &self.body
31 }
32
33 pub fn type_(&self) -> &types::Value {
34 &self.type_
35 }
36
37 pub(crate) fn rename_variables(&self, names: &HashMap<String, String>) -> Self {
38 Self::new(
39 self.name.clone(),
40 self.body.rename_variables(names),
41 self.type_.clone(),
42 )
43 }
44
45 pub(crate) fn find_variables(&self, excluded_variables: &HashSet<String>) -> HashSet<String> {
46 self.body.find_variables(&excluded_variables)
47 }
48
49 pub(crate) fn infer_environment(
50 &self,
51 variables: &HashMap<String, Type>,
52 global_variables: &HashSet<String>,
53 ) -> Self {
54 Self::new(
55 self.name.clone(),
56 self.body.infer_environment(variables, global_variables),
57 self.type_.clone(),
58 )
59 }
60
61 pub(crate) fn convert_types(&self, convert: &impl Fn(&Type) -> Type) -> Self {
62 Self::new(
63 self.name.clone(),
64 self.body.convert_types(convert),
65 convert(&self.type_.clone().into()).into_value().unwrap(),
66 )
67 }
68}