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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
mod opcodes;
mod util;

use everscale_types::prelude::*;

use self::opcodes::{cp0, Context};
use crate::ast;

pub fn assemble(ast: &[ast::Stmt], span: ast::Span) -> Result<Cell, AsmError> {
    let opcodes = cp0();

    let mut context = Context::new();
    for stmt in ast {
        context.add_stmt(opcodes, stmt)?;
    }

    context
        .into_builder(span)?
        .build()
        .map_err(|e| AsmError::StoreError { inner: e, span })
}

pub fn check(ast: &[ast::Stmt], span: ast::Span) -> Vec<AsmError> {
    let opcodes = cp0();

    let mut errors = Vec::new();
    let mut context = Context::new();
    context.set_allow_invalid();
    for stmt in ast {
        if let Err(e) = context.add_stmt(opcodes, stmt) {
            errors.push(e);
        }
    }

    if let Err(e) = context.into_builder(span).and_then(|b| {
        b.build()
            .map_err(|e| AsmError::StoreError { inner: e, span })
    }) {
        errors.push(e);
    }

    errors
}

impl ast::InstrArgValue<'_> {
    fn ty(&self) -> ArgType {
        match self {
            ast::InstrArgValue::Nat(_) => ArgType::Nat,
            ast::InstrArgValue::SReg(_) => ArgType::StackRegister,
            ast::InstrArgValue::CReg(_) => ArgType::ControlRegister,
            ast::InstrArgValue::Slice(_) => ArgType::Slice,
            ast::InstrArgValue::Block(_) => ArgType::Block,
            ast::InstrArgValue::Invalid => ArgType::Invalid,
        }
    }
}

#[derive(Debug, Copy, Clone)]
pub enum ArgType {
    Nat,
    StackRegister,
    ControlRegister,
    Slice,
    Block,
    Invalid,
}

impl ArgType {
    pub fn expected_exact(self) -> ExpectedArgType {
        ExpectedArgType::Exact(self)
    }

    pub fn expected_or(self, other: ArgType) -> ExpectedArgType {
        ExpectedArgType::OneOf(self, Box::new(other.expected_exact()))
    }
}

impl std::fmt::Display for ArgType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Nat => "number",
            Self::StackRegister => "stack register",
            Self::ControlRegister => "control register",
            Self::Slice => "cell slice",
            Self::Block => "instruction block",
            Self::Invalid => "invalid",
        })
    }
}

#[derive(Debug)]
pub enum ExpectedArgType {
    Exact(ArgType),
    OneOf(ArgType, Box<Self>),
}

impl ExpectedArgType {
    pub fn join(mut self, other: ExpectedArgType) -> Self {
        fn join_inner(this: &mut ExpectedArgType, other: ExpectedArgType) {
            let mut value = std::mem::replace(this, ExpectedArgType::Exact(ArgType::Invalid));
            match &mut value {
                ExpectedArgType::Exact(exact) => {
                    *this = ExpectedArgType::OneOf(*exact, Box::new(other))
                }
                ExpectedArgType::OneOf(_, rest) => {
                    join_inner(rest, other);
                    *this = value
                }
            }
        }

        join_inner(&mut self, other);
        self
    }
}

impl std::fmt::Display for ExpectedArgType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Exact(arg_type) => std::fmt::Display::fmt(arg_type, f),
            Self::OneOf(a, rest) => {
                std::fmt::Display::fmt(a, f)?;
                let mut rest = rest.as_ref();
                loop {
                    match rest {
                        Self::Exact(b) => return write!(f, " or {b}"),
                        Self::OneOf(b, next) => {
                            write!(f, ", {b}")?;
                            rest = next;
                        }
                    }
                }
            }
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum AsmError {
    #[error("unknown opcode: {name}")]
    UnknownOpcode { name: Box<str>, span: ast::Span },
    #[error("unaligned continuation of {bits} bits")]
    UnalignedCont { bits: u16, span: ast::Span },
    #[error("expected {expected}, got {found}")]
    ArgTypeMismatch {
        span: ast::Span,
        found: ArgType,
        expected: ExpectedArgType,
    },
    #[error("invalid register")]
    InvalidRegister(ast::Span),
    #[error("too many args")]
    TooManyArgs(ast::Span),
    #[error("not enough args")]
    NotEnoughArgs(ast::Span),
    #[error("out of range")]
    OutOfRange(ast::Span),
    #[error("invalid usage: {details}")]
    WrongUsage {
        details: &'static str,
        span: ast::Span,
    },
    #[error("store error: {inner}")]
    StoreError {
        inner: everscale_types::error::Error,
        span: ast::Span,
    },
    #[error("multiple: {0:?}")]
    Multiple(Box<[AsmError]>),
}

impl AsmError {
    pub fn can_ignore(&self) -> bool {
        matches!(
            self,
            Self::ArgTypeMismatch {
                found: ArgType::Invalid,
                ..
            }
        )
    }

    pub fn span(&self) -> ast::Span {
        match self {
            Self::UnknownOpcode { span, .. }
            | Self::UnalignedCont { span, .. }
            | Self::ArgTypeMismatch { span, .. }
            | Self::InvalidRegister(span)
            | Self::TooManyArgs(span)
            | Self::NotEnoughArgs(span)
            | Self::OutOfRange(span)
            | Self::WrongUsage { span, .. }
            | Self::StoreError { span, .. } => *span,
            Self::Multiple(items) => match items.as_ref() {
                [] => ast::Span::splat(0),
                [first, rest @ ..] => {
                    let mut res = first.span();
                    for item in rest {
                        let item_span = item.span();
                        res.start = std::cmp::min(res.start, item_span.start);
                        res.end = std::cmp::max(res.end, item_span.end);
                    }
                    res
                }
            },
        }
    }
}

pub trait JoinResults<T> {
    fn join_results(self) -> Result<T, AsmError>;
}

impl<T1, T2> JoinResults<(T1, T2)> for (Result<T1, AsmError>, Result<T2, AsmError>) {
    fn join_results(self) -> Result<(T1, T2), AsmError> {
        match self {
            (Ok(a1), Ok(a2)) => Ok((a1, a2)),
            (Ok(_), Err(e)) | (Err(e), Ok(_)) => Err(e),
            (Err(e1), Err(e2)) => Err(AsmError::Multiple(Box::from([e1, e2]))),
        }
    }
}

impl<T1, T2, T3> JoinResults<(T1, T2, T3)>
    for (
        Result<T1, AsmError>,
        Result<T2, AsmError>,
        Result<T3, AsmError>,
    )
{
    fn join_results(self) -> Result<(T1, T2, T3), AsmError> {
        match self {
            (Ok(a1), Ok(a2), Ok(a3)) => Ok((a1, a2, a3)),
            (Ok(_), Ok(_), Err(e)) | (Ok(_), Err(e), Ok(_)) | (Err(e), Ok(_), Ok(_)) => Err(e),
            (Ok(_), Err(e1), Err(e2)) | (Err(e1), Err(e2), Ok(_)) | (Err(e1), Ok(_), Err(e2)) => {
                Err(AsmError::Multiple(Box::from([e1, e2])))
            }
            (Err(e1), Err(e2), Err(e3)) => Err(AsmError::Multiple(Box::from([e1, e2, e3]))),
        }
    }
}