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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::thread;

use codespan_reporting::diagnostic::Diagnostic;

use arret_syntax::datum::DataStr;
use arret_syntax::span::FileId;

use crate::context;
use crate::context::ModuleId;
use crate::hir;
use crate::hir::scope::Scope;
use crate::reporting::{diagnostic_for_syntax_error, errors_to_diagnostics, new_primary_label};
use crate::ty;
use crate::CompileCtx;

use crate::mir::eval_hir::EvalHirCtx;
use crate::mir::Value;
use crate::typeck::infer::{infer_module, infer_repl_expr};

/// Indicates the kind of evaluation to perform on the input
///
/// This applies to expressions; it has no effect on empty input or definitions
#[derive(Clone, Copy)]
pub enum EvalKind {
    /// Infers the type of the expression
    ///
    /// This only runs as far as type checking
    Type,

    /// Fully evaluates the expression
    Value,
}

#[derive(Debug, PartialEq)]
pub struct EvaledExprValue {
    /// Rendered type of the expression
    pub type_str: String,

    /// Rendered value of the expression
    pub value_str: String,

    /// Indicates if the type is a literal
    ///
    /// REPL implementations may want to suppress printing the type of literal values as they
    /// contain no additional information.
    pub type_is_literal: bool,
}

#[derive(Debug, PartialEq)]
pub enum EvaledLine {
    /// Line was all whitepsace
    EmptyInput,

    /// Line added new definitions
    ///
    /// All bound identifiers in the root scope are returned. This is useful for tab &
    /// autocompletion.
    Defs(Vec<DataStr>),

    /// Line was evaluated to a type with the given name
    ExprType(String),

    /// Line was evaluate to a value
    ExprValue(EvaledExprValue),
}

struct ReplEngine<'ccx> {
    root_scope: Scope<'static>,
    ccx: &'ccx CompileCtx,

    inferred_module_vars: HashMap<context::ModuleId, Arc<HashMap<hir::LocalId, ty::Ref<ty::Poly>>>>,
    seen_modules: HashSet<context::ModuleId>,

    ehx: EvalHirCtx,
}

impl<'ccx> ReplEngine<'ccx> {
    fn new(ccx: &'ccx CompileCtx) -> Self {
        Self {
            root_scope: Scope::root(),
            ccx,

            seen_modules: HashSet::new(),
            inferred_module_vars: HashMap::new(),

            ehx: EvalHirCtx::new(ccx.enable_optimisations()),
        }
    }

    /// Returns all names bound in the root scope and namespace
    fn bound_names(&self) -> Vec<DataStr> {
        self.root_scope
            .bound_idents()
            .filter_map(move |ident| {
                if ident.ns_id() == Scope::root_ns_id() {
                    Some(ident.name().clone())
                } else {
                    None
                }
            })
            .collect()
    }

    /// Visits a subtree of modules and adds any missing defs and inferred module vars
    fn visit_module_tree(
        &mut self,
        root_module: &Arc<context::Module>,
    ) -> Result<(), Vec<Diagnostic<FileId>>> {
        if self.seen_modules.contains(&root_module.module_id) {
            return Ok(());
        }

        self.seen_modules.insert(root_module.module_id);

        // Make sure our imports are first
        for import in root_module.imports.values() {
            self.visit_module_tree(import)?;
        }

        self.inferred_module_vars
            .insert(root_module.module_id, root_module.inferred_locals.clone());

        self.ehx
            .visit_module_defs(root_module.module_id, &root_module.defs)?;

        Ok(())
    }

    fn eval_line(
        &mut self,
        input: String,
        kind: EvalKind,
    ) -> Result<EvaledLine, Vec<Diagnostic<FileId>>> {
        use std::io::Write;

        use crate::hir::lowering::LoweredReplDatum;

        let source_file = self.ccx.source_loader().load_string("repl".into(), input);

        let input_data = source_file
            .parsed()
            .map_err(|err| vec![diagnostic_for_syntax_error(&err)])?;

        let input_datum = match input_data {
            [] => {
                return Ok(EvaledLine::EmptyInput);
            }
            [input_datum] => input_datum,
            _ => {
                let extra_span = input_data[1].span();

                return Err(vec![Diagnostic::error()
                    .with_message("unexpected trailing datum")
                    .with_labels(vec![new_primary_label(
                        extra_span,
                        "trailing datum",
                    )])]);
            }
        };

        let module_id = ModuleId::alloc();
        let mut child_scope = Scope::child(&self.root_scope);

        let lowered_repl_datum =
            hir::lowering::lower_repl_datum(self.ccx, &mut child_scope, input_datum)
                .map_err(errors_to_diagnostics)?;

        // Bring all the defs back in to root scope
        let exported_bindings = child_scope.into_exported_bindings();
        self.root_scope
            .import_bindings(exported_bindings, module_id);

        match lowered_repl_datum {
            LoweredReplDatum::Import(modules) => {
                for module in modules.values() {
                    self.visit_module_tree(module)?;
                }

                Ok(EvaledLine::Defs(self.bound_names()))
            }
            LoweredReplDatum::EvaluableDef(def) => {
                let inferred_module = infer_module(&self.inferred_module_vars, vec![def])
                    .map_err(errors_to_diagnostics)?;

                self.inferred_module_vars
                    .insert(module_id, Arc::new(inferred_module.inferred_locals));

                self.ehx
                    .consume_module_defs(module_id, inferred_module.defs)?;

                Ok(EvaledLine::Defs(self.bound_names()))
            }
            LoweredReplDatum::NonEvaluableDef => {
                // This was handled entirely by HIR lowering
                Ok(EvaledLine::Defs(self.bound_names()))
            }
            LoweredReplDatum::Expr(decl_expr) => {
                let node = infer_repl_expr(&self.inferred_module_vars, decl_expr)?;
                let type_str = hir::str_for_ty_ref(node.result_ty());

                match kind {
                    EvalKind::Type => Ok(EvaledLine::ExprType(type_str)),
                    EvalKind::Value => {
                        use crate::mir::eval_hir::FunCtx;
                        use arret_runtime_syntax::writer;
                        use std::str;

                        let type_is_literal = ty::props::is_literal(node.result_ty());

                        // Evaluate the expression
                        let mut fcx = FunCtx::new(None);

                        let value = self
                            .ehx
                            .consume_expr(&mut fcx, &mut None, node.into_expr())?;

                        let boxed = self
                            .ehx
                            .value_to_const(&value)
                            .expect("Received register from MIR evaluation");

                        // Write the result to a string
                        let mut output_buf: Vec<u8> = vec![];
                        writer::write_boxed(&mut output_buf, &self.ehx, boxed).unwrap();

                        // Just `#fn` isn't very useful, even with a type. Add the source name.
                        match value {
                            Value::ArretFun(arret_fun) => {
                                if let Some(source_name) = arret_fun.source_name() {
                                    write!(&mut output_buf, "/{}", source_name).unwrap();
                                }
                            }
                            Value::RustFun(rust_fun) => {
                                write!(&mut output_buf, "/{}", rust_fun.symbol()).unwrap();
                            }
                            _ => {}
                        }

                        let value_str = str::from_utf8(output_buf.as_slice()).unwrap().to_owned();

                        Ok(EvaledLine::ExprValue(EvaledExprValue {
                            type_str,
                            value_str,
                            type_is_literal,
                        }))
                    }
                }
            }
        }
    }
}

pub struct ReplCtx {
    send_line: crossbeam_channel::Sender<(String, EvalKind)>,
    receive_result: crossbeam_channel::Receiver<Result<EvaledLine, Vec<Diagnostic<FileId>>>>,
}

#[derive(Debug)]
pub struct EngineDisconnected;

impl ReplCtx {
    /// Creates a new `ReplCtx`
    ///
    /// This will launch a REPL engine thread which can asynchronously evaluate lines.
    pub fn new(ccx: Arc<CompileCtx>) -> Self {
        let (send_line, receive_line) = crossbeam_channel::unbounded();
        let (send_result, receive_result) = crossbeam_channel::unbounded();

        thread::spawn(move || {
            let mut engine = ReplEngine::new(&ccx);

            for (input, kind) in receive_line.iter() {
                let result = engine.eval_line(input, kind);
                send_result.send(result).unwrap();

                if engine.ehx.should_collect() {
                    engine.ehx.collect_garbage();
                }
            }
        });

        Self {
            send_line,
            receive_result,
        }
    }

    /// Sends a line to be evaluated by the REPL engine
    ///
    /// This is asynchronous and an unlimited number of lines can be sent before reading their
    /// results. This allows the calling thread to remain responsive to user input while evaluation,
    /// garbage collection, etc occurs.
    pub fn send_line(&self, input: String, kind: EvalKind) -> Result<(), EngineDisconnected> {
        self.send_line
            .send((input, kind))
            .map_err(|_| EngineDisconnected)
    }

    /// Receives the next result from the REPL engine
    ///
    /// These will be returned in the order they were submitted with `send_line`.
    pub fn receive_result(&self) -> Result<EvaledLine, Vec<Diagnostic<FileId>>> {
        self.receive_result.recv().unwrap()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    fn eval_line_sync(
        rcx: &mut ReplCtx,
        input: String,
        kind: EvalKind,
    ) -> Result<EvaledLine, Vec<Diagnostic<FileId>>> {
        rcx.send_line(input, kind).unwrap();
        rcx.receive_result()
    }

    fn assert_defs(rcx: &mut ReplCtx, line: &'static str) {
        match eval_line_sync(rcx, line.to_owned(), EvalKind::Value).unwrap() {
            EvaledLine::Defs(_) => {}
            other => {
                panic!("Expected defs, got {:?}", other);
            }
        }
    }

    fn assert_empty(rcx: &mut ReplCtx, line: &'static str) {
        assert_eq!(
            EvaledLine::EmptyInput,
            eval_line_sync(rcx, line.to_owned(), EvalKind::Value).unwrap()
        );
    }

    fn assert_expr(
        rcx: &mut ReplCtx,
        expected_value: &'static str,
        expected_type: &'static str,
        line: &'static str,
    ) {
        assert_eq!(
            EvaledLine::ExprType(expected_type.to_owned()),
            eval_line_sync(rcx, line.to_owned(), EvalKind::Type).unwrap()
        );

        match eval_line_sync(rcx, line.into(), EvalKind::Value).unwrap() {
            EvaledLine::ExprValue(EvaledExprValue {
                value_str,
                type_str,
                ..
            }) => {
                assert_eq!(value_str, expected_value.to_owned());
                assert_eq!(type_str, expected_type.to_owned());
            }
            other => {
                panic!("unexpected REPL result: {:?}", other);
            }
        }
    }

    #[test]
    fn basic_session() {
        use crate::codegen::test::initialise_test_llvm;
        use crate::PackagePaths;

        initialise_test_llvm();

        let ccx = Arc::new(CompileCtx::new(PackagePaths::test_paths(None), true));
        let mut rcx = ReplCtx::new(ccx);

        assert_empty(&mut rcx, "       ");
        assert_empty(&mut rcx, "; COMMENT!");

        assert_expr(&mut rcx, "1", "Int", "1");

        eval_line_sync(
            &mut rcx,
            "(import [stdlib base])".to_owned(),
            EvalKind::Value,
        )
        .expect(
            "unable to load stdlib library; you may need to `cargo build` before running tests",
        );

        // Make sure we can references vars from the imported module
        assert_expr(&mut rcx, "true", "true", "(int? 5)");

        // Make sure we can redefine
        assert_defs(&mut rcx, "(def x 'first)");
        assert_defs(&mut rcx, "(def x 'second)");
        assert_expr(&mut rcx, "second", "'second", "x");

        // `(do)` at the expression level
        assert_expr(&mut rcx, "baz", "'baz", "(do 'foo 'bar 'baz)");

        // Polymorphic capturing closures
        assert_defs(
            &mut rcx,
            "(def return-constant (fn #{T} ([x T]) (fn () -> T x)))",
        );

        assert_defs(&mut rcx, "(def return-one (return-constant 1))");
        assert_defs(&mut rcx, "(def return-two (return-constant 'two))");

        assert_expr(&mut rcx, "1", "Int", "(return-one)");
        assert_expr(&mut rcx, "two", "'two", "(return-two)");
    }
}