moore_common/
lib.rs

1// Copyright (c) 2016-2021 Fabian Schuiki
2
3//! This crate contains the fundamental utilities used to by the rest of the
4//! moore compiler.
5
6#[macro_use]
7extern crate bitflags;
8
9#[macro_use]
10pub mod arenas;
11pub mod errors;
12pub mod grind;
13pub mod id;
14pub mod lexer;
15pub mod name;
16pub mod score;
17pub mod source;
18pub mod util;
19
20pub use self::id::NodeId;
21use crate::errors::{DiagBuilder2, DiagEmitter, Severity};
22use std::cell::Cell;
23
24pub struct Session {
25    pub opts: SessionOptions,
26    /// Whether any error diagnostics were produced.
27    pub failed: Cell<bool>,
28}
29
30impl Session {
31    /// Create a new session.
32    pub fn new() -> Session {
33        Session {
34            opts: Default::default(),
35            failed: Cell::new(false),
36        }
37    }
38
39    pub fn failed(&self) -> bool {
40        self.failed.get()
41    }
42}
43
44impl DiagEmitter for Session {
45    fn emit(&self, diag: DiagBuilder2) {
46        if diag.severity >= Severity::Error {
47            self.failed.set(true);
48        }
49        eprintln!("{}", diag);
50    }
51}
52
53impl SessionContext for Session {
54    fn has_verbosity(&self, verb: Verbosity) -> bool {
55        self.opts.verbosity.contains(verb)
56    }
57}
58
59/// Access session options and emit diagnostics.
60pub trait SessionContext: DiagEmitter {
61    /// Check if a verbosity option is set.
62    fn has_verbosity(&self, verb: Verbosity) -> bool;
63}
64
65/// A set of options for a session.
66///
67/// The arguments passed on the command line are intended to modify these values
68/// in order to configure the execution of the program.
69#[derive(Debug, Default)]
70pub struct SessionOptions {
71    pub ignore_duplicate_defs: bool,
72    /// Print a trace of scoreboard invocations for debugging purposes.
73    pub trace_scoreboard: bool,
74    /// The verbosity options.
75    pub verbosity: Verbosity,
76    /// The optimization level.
77    pub opt_level: usize,
78}
79
80bitflags! {
81    /// A set of verbosity options for a session.
82    ///
83    /// These flags control how much information the compiler emits.
84    #[derive(Default)]
85    pub struct Verbosity: u16 {
86        const TYPES         = 1 << 0;
87        const EXPR_TYPES    = 1 << 1;
88        const TYPE_CONTEXTS = 1 << 2;
89        const TYPECK        = 1 << 3;
90        const NAMES         = 1 << 4;
91        const CASTS         = 1 << 5;
92        const PORTS         = 1 << 6;
93        const CONSTS        = 1 << 7;
94        const INSTS         = 1 << 8;
95        const FUNC_ARGS     = 1 << 9;
96        const CALL_ARGS     = 1 << 10;
97    }
98}