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
use std::io;

use tokio::io::AsyncWriteExt;
use tokio::net::process::ChildStdin;

use crate::cmd::{Cmd, IOTCM};
use crate::pos::InteractionPoint;
use crate::resp::{AgdaError, DisplayInfo, Resp};

use super::{send_command, AgdaRead};

/// Simple REPL state wrapper.
pub struct ReplState {
    pub stdin: ChildStdin,
    pub agda: AgdaRead,
    pub file: String,
    pub(super) interaction_points: Vec<InteractionPoint>,
    pub(super) iotcm: IOTCM,
}

/// An Agda response that is either something good or some error.
pub type AgdaResult<T> = Result<T, String>;
/// Return type of `next_*` functions.
pub type NextResult<T> = io::Result<AgdaResult<T>>;

pub fn preprint_agda_result<T>(t: AgdaResult<T>) -> Option<T> {
    t.map_err(|e| eprintln!("Errors:\n{}", e)).ok()
}

impl ReplState {
    /// Print all goals.
    pub fn print_goal_list(&self) {
        let ips = self.interaction_points();
        if ips.is_empty() {
            println!("No goals, you're all set.");
        }
        for interaction_point in ips {
            // This shouldn't fail
            let range = &interaction_point.range;
            debug_assert_eq!(range.len(), 1);
            let interval = &range[0];
            println!("?{} at line {}", interaction_point.id, interval.start.line)
        }
    }

    pub async fn reload_file(&mut self) -> io::Result<()> {
        self.command(Cmd::load_simple(self.file.clone())).await
    }

    pub async fn command(&mut self, cmd: Cmd) -> io::Result<()> {
        self.iotcm.command = cmd;
        send_command(&mut self.stdin, &self.iotcm).await
    }

    pub async fn command_raw(&mut self, raw_command: &str) -> io::Result<()> {
        self.stdin.write(raw_command.as_bytes()).await?;
        self.stdin.flush().await
    }

    pub async fn shutdown(&mut self) -> io::Result<()> {
        self.stdin.shutdown().await
    }

    /// Skip information until the next display info.
    pub async fn next_display_info(&mut self) -> io::Result<DisplayInfo> {
        loop {
            match self.response().await? {
                Resp::DisplayInfo { info: Some(info) } => break Ok(info),
                _ => {}
            }
        }
    }

    /// Returns the latest [`next_goals`](Self::next_goals) result.
    pub fn interaction_points(&self) -> &[InteractionPoint] {
        &self.interaction_points
    }

    /// Skip information until the next interaction point (goal) list.
    /// The result can be queried via [`interaction_points`](Self::interaction_points).
    ///
    /// # Note
    ///
    /// This information normally comes right after `all_goals_warnings`,
    /// and when you call [`next_all_goals_warnings`](Self::next_all_goals_warnings),
    /// you've already eliminated errors.
    /// Therefore this method don't deal with errors.
    pub async fn next_goals(&mut self) -> io::Result<()> {
        use Resp::*;
        self.interaction_points = loop {
            match self.response().await? {
                InteractionPoints { interaction_points } => break interaction_points,
                _ => {}
            }
        };
        Ok(())
    }

    /// Skip information until an error.
    pub async fn next_error(&mut self) -> io::Result<AgdaError> {
        use crate::resp::DisplayInfo::Error as DisError;
        loop {
            match self.next_display_info().await? {
                DisError(e) => break Ok(e),
                _ => {}
            }
        }
    }
}

macro_rules! next_resp_of {
    ($f:ident, $p:ident, $d:literal) => {
        next_resp_of!($f, $p, crate::resp::$p, $d);
    };

    ($f:ident, $p:ident, $t:ty, $d:literal) => {
        impl ReplState {
            #[doc($d)]
            pub async fn $f(&mut self) -> NextResult<$t> {
                loop {
                    match self.response().await? {
                        Resp::$p(ga) => break Ok(Ok(ga)),
                        Resp::DisplayInfo {
                            info: Some(crate::resp::DisplayInfo::Error(e)),
                        } => break Ok(e.into()),
                        _ => {}
                    }
                }
            }
        }
    };
}

next_resp_of!(next_give_action, GiveAction, "Skip until next give-action.");
next_resp_of!(next_make_case, MakeCase, "Skip until next make-case.");
next_resp_of!(
    next_highlight,
    HighlightingInfo,
    "Skip until next highlight."
);

macro_rules! next_disp_of {
    ($f:ident, $p:ident, $d:literal) => {
        next_disp_of!($f, $p, crate::resp::$p, $d);
    };

    ($f:ident, $p:ident, $t:ty, $d:literal) => {
        impl ReplState {
            #[doc($d)]
            pub async fn $f(&mut self) -> NextResult<$t> {
                loop {
                    match self.next_display_info().await? {
                        DisplayInfo::Error(e) => break Ok(e.into()),
                        DisplayInfo::$p(agw) => break Ok(Ok(agw)),
                        _ => {}
                    }
                }
            }
        }
    };
}

next_disp_of!(
    next_all_goals_warnings,
    AllGoalsWarnings,
    "Skip until next interaction point (goal) list."
);
next_disp_of!(
    next_goal_specific,
    GoalSpecific,
    "Skip until next goal specific information."
);
next_disp_of!(
    next_module_contents,
    ModuleContents,
    "Skip until next module contents response."
);
next_disp_of!(next_normal_form, NormalForm, "Skip until next normal form.");
next_disp_of!(next_context, Context, "Skip until next context.");
next_disp_of!(
    next_inferred_type,
    InferredType,
    "Skip until next inferred type."
);