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
use prism_parser::rule_action::action_result::ActionResult;
use crate::desugar::{ParseEnv, SourceExpr, ParseIndex};

impl ParseEnv {
    pub fn insert_from_action_result<'grm>(
        &mut self,
        value: &ActionResult<'_, 'grm>,
        program: &str,
    ) -> ParseIndex {
        let ActionResult::Construct(span, constructor, args) = value else {
            unreachable!("Parsing an expression always returns a Construct");
        };
        let inner = match *constructor {
            "Type" => {
                assert_eq!(args.len(), 0);
                SourceExpr::Type
            }
            "Let" => {
                assert_eq!(args.len(), 3);
                SourceExpr::Let(
                    args[0].get_value(program).to_string(),
                    self.insert_from_action_result(&args[1], program),
                    self.insert_from_action_result(&args[2], program),
                )
            }
            "Variable" => {
                assert_eq!(args.len(), 1);
                SourceExpr::Variable(
                    args[0].get_value(program).to_string(),
                ) 
            }
            "FnType" => {
                assert_eq!(args.len(), 3);
                SourceExpr::FnType(
                    args[0].get_value(program).to_string(),
                    self.insert_from_action_result(&args[1], program),
                    self.insert_from_action_result(&args[2], program),
                )
            }
            "FnConstruct" => {
                assert_eq!(args.len(), 3);
                SourceExpr::FnConstruct(
                    args[0].get_value(program).to_string(),
                    self.insert_from_action_result(&args[1], program),
                    self.insert_from_action_result(&args[2], program),
                )
            }
            "FnDestruct" => {
                assert_eq!(args.len(), 2);
                SourceExpr::FnDestruct(
                    self.insert_from_action_result(&args[0], program),
                    self.insert_from_action_result(&args[1], program),
                )
            }
            _ => unreachable!(),
        };
        self.store(inner, *span)
    }
}