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
//! Utilities for implementing a REPL.

use ast::{Decl, PrintStyle};
use cst::{Decl as CstDecl, Expr};
use eval::Evaluator;

/// A command entered at the REPL.
#[derive(Clone, Debug, PartialEq)]
pub enum ReplCommand {
    /// Adds a new declaration.
    Decl(CstDecl),

    /// Sets the function used to construct an evaluator.
    Evaluator(fn(Vec<Decl<()>>) -> Box<Evaluator>),

    /// Evaluates an expression.
    Expr(Expr),

    /// Prints a help menu.
    Help,

    /// Lists all declarations.
    List,

    /// Changes the print style.
    PrintStyle(PrintStyle),

    /// Quits the REPL.
    Quit,

    /// Clears all declarations from the REPL.
    Reset,

    /// Gets the type of an expression.
    Typeof(Expr),
}

impl ReplCommand {
    /// Returns a help message for the commands.
    pub fn help() -> &'static str {
        r"<expr>              Evaluates an expression
:decl <decl>        Adds a declaration
:l                  Lists all declarations
:list               Lists all declarations
:t <expr>           Prints the type of an expression

:cbn                Switches to call-by-name evaluation
:cbv                Switches to call-by-value evaluation
:lazy               Switches to lazy evaluation

:ast                Switches to AST print style
:cst                Switches to CST print style

:help               Prints this help message
:q                  Quits the REPL
:quit               Quits the REPL
:reset              Removes all decls from the REPL"
    }
}