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
// Copyright (c) 2016-2021 Fabian Schuiki

//! This crate contains the fundamental utilities used to by the rest of the
//! moore compiler.

#[macro_use]
extern crate bitflags;

#[macro_use]
pub mod arenas;
pub mod errors;
pub mod grind;
pub mod id;
pub mod lexer;
pub mod name;
pub mod score;
pub mod source;
pub mod util;

pub use self::id::NodeId;
use crate::errors::{DiagBuilder2, DiagEmitter, Severity};
use std::cell::Cell;

pub struct Session {
    pub opts: SessionOptions,
    /// Whether any error diagnostics were produced.
    pub failed: Cell<bool>,
}

impl Session {
    /// Create a new session.
    pub fn new() -> Session {
        Session {
            opts: Default::default(),
            failed: Cell::new(false),
        }
    }

    pub fn failed(&self) -> bool {
        self.failed.get()
    }
}

impl DiagEmitter for Session {
    fn emit(&self, diag: DiagBuilder2) {
        if diag.severity >= Severity::Error {
            self.failed.set(true);
        }
        eprintln!("{}", diag);
    }
}

impl SessionContext for Session {
    fn has_verbosity(&self, verb: Verbosity) -> bool {
        self.opts.verbosity.contains(verb)
    }
}

/// Access session options and emit diagnostics.
pub trait SessionContext: DiagEmitter {
    /// Check if a verbosity option is set.
    fn has_verbosity(&self, verb: Verbosity) -> bool;
}

/// A set of options for a session.
///
/// The arguments passed on the command line are intended to modify these values
/// in order to configure the execution of the program.
#[derive(Debug, Default)]
pub struct SessionOptions {
    pub ignore_duplicate_defs: bool,
    /// Print a trace of scoreboard invocations for debugging purposes.
    pub trace_scoreboard: bool,
    /// The verbosity options.
    pub verbosity: Verbosity,
    /// The optimization level.
    pub opt_level: usize,
}

bitflags! {
    /// A set of verbosity options for a session.
    ///
    /// These flags control how much information the compiler emits.
    #[derive(Default)]
    pub struct Verbosity: u16 {
        const TYPES         = 1 << 0;
        const EXPR_TYPES    = 1 << 1;
        const TYPE_CONTEXTS = 1 << 2;
        const TYPECK        = 1 << 3;
        const NAMES         = 1 << 4;
        const CASTS         = 1 << 5;
        const PORTS         = 1 << 6;
        const CONSTS        = 1 << 7;
        const INSTS         = 1 << 8;
        const FUNC_ARGS     = 1 << 9;
        const CALL_ARGS     = 1 << 10;
    }
}