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
//! A BASIC dialect

mod ast;
mod code;
mod handler;
mod lexer;
mod loader;
mod m;
mod parser;
mod rcstr;
mod trans;
mod val;
mod vm;

pub use ast::*;
pub use code::*;
pub use handler::*;
pub use lexer::*;
pub use loader::*;
pub use m::*;
pub use parser::*;
pub use rcstr::*;
pub use trans::*;
pub use val::*;
pub use vm::*;

pub const PRELUDE_NAME: &'static str = "__prelude";

#[derive(Debug)]
pub struct BasicError {
    pub marks: Vec<Mark>,
    pub message: String,
    pub help: Option<String>,
}

impl From<std::io::Error> for BasicError {
    fn from(e: std::io::Error) -> Self {
        Self {
            marks: vec![],
            message: format!("{:?}", e),
            help: None,
        }
    }
}

impl BasicError {
    pub fn new(marks: Vec<Mark>, message: String) -> Self {
        Self {
            marks,
            message,
            help: None,
        }
    }
    pub fn new_with_help(marks: Vec<Mark>, message: String, help: String) -> Self {
        Self {
            marks,
            message,
            help: Some(help),
        }
    }
    pub fn format(&self) -> String {
        use std::fmt::Write;
        let mut ret = String::new();
        let out = &mut ret;
        writeln!(out, "=================").unwrap();
        writeln!(out, "== STACK TRACE ==").unwrap();
        writeln!(out, "=================").unwrap();
        for mark in &self.marks {
            write!(out, "{}", mark.format()).unwrap();
        }
        writeln!(out, "ERROR: {}", self.message).unwrap();
        ret
    }
}