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
use pomelo_parse::ast;

use crate::arena::Idx;
use crate::lower::{HirLower, HirLowerGenerated, LoweringCtxt};
use crate::{
    Builtin, DefLoc, Expr, FloatWrapper, LongVId, MRule, NodeParent, Pat, Scon, TyVar, VId,
};

// Used to lower lists [a1, a2, ... ] to a1 :: a2 :: ... :: nil
// Common code for both [`Expr`] and [`Pat`].
// This is factored out on its own since it's probably the most complicated
// part of this stage of lowering.
//
// elts: e.g., "expr.exprs()" or "pat.pats()"
// vid_kind: construct an `ExprKind::VId` or `PatKind::VId`
// infix_kind: construct an `ExprKind::Infix` or `PatKind::Infix`
pub(super) fn lower_list<H: HirLowerGenerated>(
    ctx: &mut LoweringCtxt,
    origin: NodeParent,
    elts: impl Iterator<Item = H::AstType>,
    vid_kind: impl Fn((LongVId, DefLoc)) -> H::Kind,
    infix_kind: impl Fn(Idx<H>, (VId, DefLoc), Idx<H>) -> H::Kind,
) -> H::Kind {
    let mut rev_indexed = elts
        .map(|e| H::lower(ctx, e))
        .enumerate()
        .collect::<Vec<_>>();
    rev_indexed.reverse();

    let nil = (
        LongVId::from(VId::from_builtin(Builtin::Nil)),
        DefLoc::Builtin,
    );

    if rev_indexed.is_empty() {
        vid_kind(nil)
    } else {
        let cons = VId::from_builtin(Builtin::Cons);

        // The list ends with a nil pat
        let nil_expr = H::generated(ctx, origin, vid_kind(nil.clone()));

        let mut last_idx = nil_expr;
        let mut last = vid_kind(nil);

        // "::" is right-associative, so we walk the list of pats in reverse.
        // We allocate each generated infix expr in the arena, except for the
        // final one ("hd :: ( ... )"), whose `ExprKind` we need to return from the function
        for (i, p_idx) in rev_indexed {
            last = infix_kind(p_idx, (cons, DefLoc::Builtin), last_idx);
            if i == 0 {
                return last;
            }
            last_idx = H::generated(ctx, origin, last.clone());
        }
        last
    }
}

pub(super) fn lower_tyvarseq(
    ctx: &mut LoweringCtxt,
    tyvarseq: impl Iterator<Item = ast::TyVar>,
) -> Box<[TyVar]> {
    tyvarseq.map(|t| TyVar::from_token(ctx, Some(t))).collect()
}

pub(super) fn lower_vids(
    ctx: &mut LoweringCtxt,
    vids: impl Iterator<Item = ast::VId>,
) -> Box<[(VId, DefLoc)]> {
    vids.map(|v| {
        let vid = VId::from_token(ctx, Some(v));
        let loc = ctx.resolver().lookup_vid(&LongVId::from(vid));
        (vid, loc)
    })
    .collect()
}

impl NodeParent {
    pub(super) fn from_expr(ctx: &mut LoweringCtxt, expr: &ast::Expr) -> Self {
        let id = ctx.alloc_ast_id(expr);
        Self::Expr(id)
    }

    pub(super) fn from_pat(ctx: &mut LoweringCtxt, pat: &ast::Pat) -> Self {
        let id = ctx.alloc_ast_id(pat);
        Self::Pat(id)
    }

    pub(super) fn from_dec(ctx: &mut LoweringCtxt, dec: &ast::Dec) -> Self {
        let id = ctx.alloc_ast_id(dec);
        Self::Dec(id)
    }
}

pub(crate) fn lower_match(ctx: &mut LoweringCtxt, match_expr: &ast::Match) -> Box<[MRule]> {
    match_expr.mrules().map(|m| MRule::lower(ctx, &m)).collect()
}

impl MRule {
    fn lower(ctx: &mut LoweringCtxt, mrule: &ast::Mrule) -> Self {
        ctx.in_inner_scope(|ctx| {
            let pat = Pat::lower_opt(ctx, mrule.pat());
            ctx.register_pat_names_in_match(pat);
            let expr = Expr::lower_opt(ctx, mrule.expr());
            Self { pat, expr }
        })
    }
}

impl Scon {
    // TODO: do this in lexing???
    //
    // This also doesn't respect the lexical definitions of these...
    pub(crate) fn lower(node: ast::Scon) -> Self {
        match node {
            ast::Scon::Int(s) => {
                let s = s.text();

                let i = if let Some('~') = s.chars().next() {
                    s[1..].parse::<i128>().map(|i| -i)
                } else {
                    s.parse()
                };
                i.map(Self::Int).unwrap_or_else(|_| Self::Missing)
            }
            ast::Scon::Word(s) => s
                .text()
                .parse()
                .map(Self::Word)
                .unwrap_or_else(|_| Self::Missing),
            ast::Scon::Real(s) => s
                .text()
                .parse::<f64>()
                .map(FloatWrapper::new)
                .map(Self::Real)
                .unwrap_or_else(|_| Self::Missing),
            ast::Scon::Char(s) => s
                .text()
                .chars()
                .next()
                .map(Self::Char)
                .unwrap_or_else(|| Self::Missing),
            ast::Scon::String(s) => {
                // FIXME: intern strings??
                let s = s.text().to_owned();
                Self::String(s)
            }
        }
    }
}