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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// AluVM Assembler
// To find more on AluVM please check <https://www.aluvm.org>
//
// Designed & written in 2021 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
// for Pandora Core AG

#[macro_use]
extern crate pest_derive;
#[macro_use]
extern crate amplify;

mod model;
mod pipelines;

use std::error::Error;
use std::num::ParseIntError;

use aluvm::data::encoding::DecodeError;
use aluvm::isa::Instr;
use aluvm::libs::{CodeEofError, IsaSegError};
use amplify::{hex, IoError};
pub use model::{ast, issues, module, product};
pub use pipelines::{analyzer, compiler, linker, parser};
use rustc_apfloat::ParseError;

use crate::issues::Src;
use crate::module::CallTableError;
use crate::parser::Rule;
use crate::product::DyError;

#[derive(Debug, Display, Error, From)]
pub enum MainError {
    #[display("\x1B[1;31mInternal lexer error X{1:04}:\x1B[0m {0}")]
    Lexer(String, u16),

    #[display("\x1B[1;31mInternal compiler error C{1:04}:\x1B[0m {0}")]
    Compiler(CompilerError, u16),

    #[display("\x1B[1;31mInternal linker error L{1:04}:\x1B[0m {0}")]
    Linker(LinkerError, u16),

    #[display("\x1B[1;31mError:\x1B[0m {0}")]
    #[from]
    Build(BuildError),

    #[display(
        "{1}\n\x1B[1;31mError:\x1B[0m could not compile `{0}` due to a previous parsing error"
    )]
    Parser(String, pest::error::Error<Rule>),

    #[display(
        "{3}\n\x1B[1;31mError:\x1B[0m could not compile `{0}` due to {1} previous syntax \
         error(s); {2} warning(s) emitted"
    )]
    Syntax(String, usize, usize, String),

    #[display(
        "{3}\n\x1B[1;31mError:\x1B[0m could not complete compillation of `{0}` due to {1} \
         previous error(s); {2} warning(s) emitted"
    )]
    Compile(String, usize, usize, String),

    #[display(
        "\x1B[1;31mError:\x1B[0m {0}\n\n\x1B[1;31mError:\x1B[0m failing due to broken binary data \
         in module `{1}` "
    )]
    Module(DecodeError, String),

    #[display(
        "{3}\n\x1B[1;31mError:\x1B[0m could not link `{0}` due to {1} previous error(s); {2} \
         warning(s) emitted"
    )]
    Linking(String, usize, usize, String),
}

#[derive(Clone, Debug, Display, Error)]
#[display(doc_comments)]
pub enum LexerError<'i> {
    /// unknown program segment `{0:?}`
    UnknownSegment(Rule),

    /// library statement `{0:#}` has no detectable name
    /// {0}
    LibNoName(Src<'i>),

    /// library statement `{0:#}` has no detectable lib id
    /// {0}
    LibNoId(Src<'i>),

    /// routine `{0:#}` has no detectable name
    /// {0}
    RoutineNoName(Src<'i>),

    /// routine `{0:#}` does not start with neither .MAIN or .ROUTINE
    /// {0}
    RoutineUnrecognized(Src<'i>),

    /// label in `{0:#}` is not followed by an instruction
    /// {0}
    StatementNoInstruction(Src<'i>),

    /// mnemonic is followed by non-flag rules
    /// {0}
    StatementNoFlag(Src<'i>),

    /// operator `{0:#}` is not composed of mnemonic and flags
    /// {0}
    OperatorMiscomposition(Src<'i>),

    /// flag without flag value
    /// {0}
    FlagWithoutValue(Src<'i>),

    /// register operand contains no register type
    /// {0}
    RegisterNoType(Src<'i>),

    /// register operand does not specify register name
    /// {0}
    RegisterNoName(Src<'i>),

    /// register name must be encoded as a decimal number
    /// {0}
    /// details: {1}
    RegisterNameNonDecimal(Src<'i>, ParseIntError),

    /// register operand does not specify register index
    /// {0}
    RegisterNoIndex(Src<'i>),

    /// register index {2} is not a decimal number
    /// {0}
    /// details: {1}
    RegisterIndexNonDecimal(Src<'i>, ParseIntError, &'i str),

    /// unknown register type
    /// {0}
    RegisterUnknown(Src<'i>),

    /// call statement without library name
    /// {0}
    CallWithoutLibName(Src<'i>),

    /// call statement without routine name
    /// {0}
    CallWithoutRoutineName(Src<'i>),

    /// unknown operand format `{1}` inside the statement `{0:#}`
    /// {0}
    OperandUnknown(Src<'i>, &'i str),

    /// literal contains no data
    /// {0}
    LiteralNoData(Src<'i>),

    /// incorrect decimal literal `{1}`
    /// {0}
    /// details: {2}
    LiteralWrongDec(Src<'i>, &'i str, ParseIntError),

    /// incorrect hex literal `{1}`
    /// {0}
    /// details: {2}
    LiteralWrongHex(Src<'i>, &'i str, hex::Error),

    /// incorrect oct literal `{1}`
    /// {0}
    /// details: {2}
    LiteralWrongOct(Src<'i>, &'i str, ParseIntError),

    /// incorrect bin literal `{1}`
    /// {0}
    /// details: {2}
    LiteralWrongBin(Src<'i>, &'i str, ParseIntError),

    /// float literal contains no whole mantissa part
    /// {0}
    FloatNoWhole(Src<'i>),

    /// float literal contains no fractional mantissa part
    /// {0}
    FloatNoFraction(Src<'i>),

    /// float literal mantissa whole part is not an integer
    /// {0}
    /// details: {1}
    FloatWholeNotNumber(Src<'i>, ParseIntError),

    /// float literal mantissa fraction part is not an integer
    /// {0}
    /// details: {1}
    FloatFractionNotNumber(Src<'i>, ParseIntError),

    /// float literal exponential part is not an integer
    /// {0}
    /// details: {1}
    FloatExponentialNotNumber(Src<'i>, ParseIntError),

    /// unknown type of literal `{1:?}`
    /// {0}
    LiteralUnknown(Src<'i>, Rule),

    /// constant statement has no name
    /// {0}
    ConstNoName(Src<'i>),

    /// constant statement has no value
    /// {0}
    ConstNoValue(Src<'i>),

    /// input variable has no name
    /// {0}
    VarNoName(Src<'i>),

    /// input variable has no type
    /// {0}
    VarNoType(Src<'i>),

    /// input variable has no description
    /// {0}
    VarNoDescription(Src<'i>),

    /// input variable description is not a string literal
    /// {0}
    VarWrongDescription(Src<'i>),

    /// unknown variable type `{0}`
    /// {1}
    VarTypeUnknown(String, Src<'i>),

    /// unable to detect program code
    ProgramAbsent,
}

impl<'i> From<LexerError<'i>> for MainError {
    #[inline]
    fn from(err: LexerError<'i>) -> Self {
        let no = err.errno();
        MainError::Lexer(err.to_string(), no)
    }
}

impl<'i> LexerError<'i> {
    pub fn errno(&self) -> u16 {
        match self {
            LexerError::UnknownSegment(_) => 1,
            LexerError::LibNoName(_) => 2,
            LexerError::LibNoId(_) => 3,
            LexerError::RoutineNoName(_) => 4,
            LexerError::RoutineUnrecognized(_) => 5,
            LexerError::StatementNoInstruction(_) => 6,
            LexerError::StatementNoFlag(_) => 7,
            LexerError::OperatorMiscomposition(_) => 8,
            LexerError::FlagWithoutValue(_) => 9,
            LexerError::RegisterNoType(_) => 10,
            LexerError::RegisterNoName(_) => 11,
            LexerError::RegisterNameNonDecimal(_, _) => 12,
            LexerError::RegisterNoIndex(_) => 13,
            LexerError::RegisterIndexNonDecimal(_, _, _) => 14,
            LexerError::RegisterUnknown(_) => 15,
            LexerError::CallWithoutLibName(_) => 16,
            LexerError::CallWithoutRoutineName(_) => 17,
            LexerError::OperandUnknown(_, _) => 18,
            LexerError::LiteralNoData(_) => 19,
            LexerError::LiteralWrongDec(_, _, _) => 20,
            LexerError::LiteralWrongHex(_, _, _) => 21,
            LexerError::LiteralWrongOct(_, _, _) => 22,
            LexerError::LiteralWrongBin(_, _, _) => 23,
            LexerError::FloatNoWhole(_) => 24,
            LexerError::FloatNoFraction(_) => 25,
            LexerError::FloatWholeNotNumber(_, _) => 26,
            LexerError::FloatFractionNotNumber(_, _) => 27,
            LexerError::FloatExponentialNotNumber(_, _) => 28,
            LexerError::LiteralUnknown(_, _) => 29,
            LexerError::ConstNoName(_) => 30,
            LexerError::ConstNoValue(_) => 31,
            LexerError::VarNoName(_) => 32,
            LexerError::VarNoDescription(_) => 33,
            LexerError::VarWrongDescription(_) => 34,
            LexerError::ProgramAbsent => 35,
            LexerError::VarNoType(_) => 36,
            LexerError::VarTypeUnknown(_, _) => 37,
        }
    }
}

/// Errors happening when editing existing instruction (should never happen and indicate internal
/// compiler bug)
#[derive(Clone, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum InstrError {
    /// referenced position is outside of the code
    #[from(CodeEofError)]
    Read,

    /// instruction has changed from `{0}` to `{1}`
    Changed(&'static str, Instr),
}

#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum CompilerError {
    /// Errors related to ISA segment
    #[from]
    #[display(inner)]
    Isa(IsaSegError),

    /// routine `{0}` is absent in the list of routines
    RoutineMissed(String),

    /// routine `{0}` contains no instructions
    RoutineEmpty(String),

    /// unable to read instruction at position {0}
    InstrRead(u16),

    /// instruction at position {0} has changed from `{1}` into `{2}`
    InstrChanged(u16, &'static str, Instr),

    /// Call table error
    #[from]
    #[display(inner)]
    CallTable(CallTableError),

    /// unable to construct float representation of literal `{0}.{1}e{2}`
    ///
    /// details: {3:?}
    FloatConstruction(u128, u128, u16, ParseError),
}

impl CompilerError {
    pub fn errno(&self) -> u16 {
        match self {
            CompilerError::Isa(err) => match err {
                IsaSegError::SegmentTooLarge(_) => 1,
                IsaSegError::SegmentTooManyExt(_) => 2,
                IsaSegError::IsaIdWrongLength(_) => 3,
                IsaSegError::IsaIdWrongSymbols(_) => 4,
            },
            CompilerError::RoutineMissed(_) => 5,
            CompilerError::RoutineEmpty(_) => 6,
            CompilerError::InstrRead(_) => 7,
            CompilerError::InstrChanged(_, _, _) => 8,
            CompilerError::FloatConstruction(_, _, _, _) => 9,
            CompilerError::CallTable(_) => 10,
        }
    }

    pub fn with(err: InstrError, pos: u16) -> Self {
        match err {
            InstrError::Read => CompilerError::InstrRead(pos),
            InstrError::Changed(from, to) => CompilerError::InstrChanged(pos, from, to),
        }
    }
}

impl From<CompilerError> for MainError {
    #[inline]
    fn from(err: CompilerError) -> Self {
        let no = err.errno();
        MainError::Compiler(err, no)
    }
}

#[derive(Clone, Eq, PartialEq, Hash, Debug, Display, Error)]
#[display(doc_comments)]
pub enum LinkerError {
    /// unable to read instruction at position {0}
    InstrRead(u16),

    /// instruction at position {0} has changed from `{1}` into `{2}`
    InstrChanged(u16, &'static str, Instr),
}

impl LinkerError {
    pub fn errno(&self) -> u16 {
        match self {
            LinkerError::InstrRead(_) => 3,
            LinkerError::InstrChanged(_, _, _) => 4,
        }
    }

    pub fn with(err: InstrError, pos: u16) -> Self {
        match err {
            InstrError::Read => LinkerError::InstrRead(pos),
            InstrError::Changed(from, to) => LinkerError::InstrChanged(pos, from, to),
        }
    }
}

impl From<LinkerError> for MainError {
    #[inline]
    fn from(err: LinkerError) -> Self {
        let no = err.errno();
        MainError::Linker(err, no)
    }
}

#[derive(Debug, Display, Error)]
#[display(doc_comments)]
pub enum BuildError {
    /// unable to create output directory `{dir}`
    ///
    /// details: {details}
    OutputDir { dir: String, details: Box<dyn Error> },

    /// `{0}` provided as one o the arguments is not a file
    NotFile(String),

    /// no file named `{file}`
    ///
    /// details: {details}
    FileNotFound { file: String, details: Box<dyn Error> },

    /// file `{file}` exists, but can't be opened; please check file attributes
    ///
    /// details: {details}
    FileNoAccess { file: String, details: Box<dyn Error> },

    /// unable to create dump file `{file}`
    ///
    /// details: {details}
    DumpFileCreation { file: String, details: Box<dyn Error> },

    /// unable to create object file `{file}`
    ///
    /// details: {details}
    ObjFileCreation { file: String, details: Box<dyn Error> },

    /// unable to write into object file `{file}`
    ///
    /// details: {details}
    ObjFileWrite { file: String, details: Box<dyn Error> },

    /// unable to create product file `{file}`
    ///
    /// details: {details}
    ProductFileCreation { file: String, details: Box<dyn Error> },

    /// unable to write into product file `{file}`
    ///
    /// details: {details}
    ProductFileWrite { file: String, details: Box<dyn Error> },

    /// error disassembling file `{file}` since last instruction is incomplete
    Disassembling { file: String },

    /// path `{0}` specified for objects directory (-O | --obj-dir) is not a directory. Try use
    /// lowercase -o argument if you'd like to specify a single object file
    ObjDirIsFile(String),

    /// can't access objects directory `{0}`
    ///
    /// details: {1}
    ObjDirFail(String, IoError),

    /// path `{0}` specified for library directory (-L | --lib-dir) is not a directory. Try use
    /// lowercase -l argument if you'd like to specify a single library file
    LibDirIsFile(String),

    /// library `{0}` is a directory, not a file. Try use uppercase -L argument if you'd like to
    /// specify a library directory and not a single file
    LibIsDir(String),

    /// can't access library directory `{0}`
    ///
    /// details: {1}
    LibDirFail(String, IoError),

    /// library at `{0}` has incorrect binary data
    ///
    /// details: {1}
    LibIncorrectData(String, DyError),

    /// library file at `{0}` is not accessible
    ///
    /// details: {1}
    LibNotAccessible(String, Box<dyn Error>),
}