rune 0.9.0

An embeddable dynamic programming language for Rust.
Documentation
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
412
413
use crate::ast;
use crate::load::{FileSourceLoader, SourceLoader, Sources};
use crate::query::{Build, BuildEntry, Query};
#[cfg(compiler_v2)]
use crate::shared::ResultExt as _;
use crate::shared::{Consts, Gen};
use crate::worker::{LoadFileKind, Task, Worker};
use crate::{Diagnostics, Options, Spanned as _, Storage};
use runestick::{Context, Location, Source, Span};
use std::rc::Rc;
use std::sync::Arc;

mod assembly;
mod compile_error;
mod compile_visitor;
mod unit_builder;
mod v1;
#[cfg(compiler_v2)]
mod v2;

pub use self::compile_error::{CompileError, CompileErrorKind, CompileResult, ImportEntryStep};
pub use self::compile_visitor::{CompileVisitor, NoopCompileVisitor};
pub use self::unit_builder::{BuildError, InsertMetaError, LinkerError, UnitBuilder};
use crate::parsing::Resolve as _;

pub(crate) use self::assembly::{Assembly, AssemblyInst};

/// Compile the given source with default options.
pub fn compile(
    context: &Context,
    sources: &mut Sources,
    unit: &UnitBuilder,
    diagnostics: &mut Diagnostics,
) -> Result<(), ()> {
    let visitor = Rc::new(NoopCompileVisitor::new());
    let source_loader = Rc::new(FileSourceLoader::new());

    compile_with_options(
        context,
        sources,
        unit,
        diagnostics,
        &Default::default(),
        visitor,
        source_loader,
    )?;

    Ok(())
}

/// Encode the given object into a collection of asm.
pub fn compile_with_options<'a>(
    context: &Context,
    sources: &mut Sources,
    unit: &UnitBuilder,
    diagnostics: &mut Diagnostics,
    options: &Options,
    visitor: Rc<dyn CompileVisitor>,
    source_loader: Rc<dyn SourceLoader + 'a>,
) -> Result<(), ()> {
    // Global storage.
    let storage = Storage::new();
    // Shared id generator.
    let gen = Gen::new();
    // Constants storage.
    let consts = Consts::default();

    // The worker queue.
    let mut worker = Worker::new(
        context,
        sources,
        options,
        unit.clone(),
        consts,
        diagnostics,
        visitor.clone(),
        source_loader,
        storage.clone(),
        gen,
    );

    // Queue up the initial sources to be loaded.
    for source_id in worker.sources.source_ids() {
        let mod_item = match worker.query.insert_root_mod(source_id, Span::empty()) {
            Ok(result) => result,
            Err(error) => {
                worker.diagnostics.error(source_id, error);
                return Err(());
            }
        };

        worker.queue.push_back(Task::LoadFile {
            kind: LoadFileKind::Root,
            source_id,
            mod_item,
        });
    }

    worker.run();

    if worker.diagnostics.has_error() {
        return Err(());
    }

    loop {
        while let Some(entry) = worker.query.next_build_entry() {
            let source_id = entry.location.source_id;

            let task = CompileBuildEntry {
                visitor: &visitor,
                context,
                options,
                storage: &storage,
                unit,
                diagnostics: worker.diagnostics,
                consts: &worker.consts,
                query: &mut worker.query,
            };

            if let Err(error) = task.compile(entry) {
                worker.diagnostics.error(source_id, error);
            }
        }

        match worker.query.queue_unused_entries() {
            Ok(true) => (),
            Ok(false) => break,
            Err((source_id, error)) => {
                worker.diagnostics.error(source_id, error);
            }
        }
    }

    if worker.diagnostics.has_error() {
        return Err(());
    }

    Ok(())
}

struct CompileBuildEntry<'a> {
    visitor: &'a Rc<dyn CompileVisitor>,
    context: &'a Context,
    options: &'a Options,
    storage: &'a Storage,
    unit: &'a UnitBuilder,
    diagnostics: &'a mut Diagnostics,
    consts: &'a Consts,
    query: &'a mut Query,
}

impl CompileBuildEntry<'_> {
    fn compiler1<'a>(
        &'a mut self,
        location: Location,
        source: &Arc<Source>,
        span: Span,
        asm: &'a mut Assembly,
    ) -> self::v1::Compiler<'a> {
        self::v1::Compiler {
            visitor: self.visitor.clone(),
            storage: self.storage,
            source_id: location.source_id,
            source: source.clone(),
            context: self.context,
            consts: self.consts,
            query: self.query,
            asm,
            unit: self.unit.clone(),
            scopes: self::v1::Scopes::new(self.visitor.clone()),
            contexts: vec![span],
            loops: self::v1::Loops::new(),
            options: self.options,
            diagnostics: self.diagnostics,
        }
    }

    /// Construct an instance of the next version of the compiler.
    #[cfg(compiler_v2)]
    fn compiler2<'a>(
        &'a mut self,
        location: Location,
        source: &'a Arc<Source>,
        span: Span,
        program: &'a mut rune_ssa::Program,
    ) -> self::v2::Compiler<'a> {
        self::v2::Compiler {
            program,
            location,
            contexts: vec![span],
            source,
            scope: self::v2::scope::Stack::new(location.source_id, self.visitor.clone()),
            storage: self.storage,
            context: self.context,
            consts: self.consts,
            query: self.query,
            unit: self.unit.clone(),
            options: self.options,
            diagnostics: self.diagnostics,
            visitor: self.visitor.clone(),
        }
    }

    fn compile(mut self, entry: BuildEntry) -> Result<(), CompileError> {
        let BuildEntry {
            item,
            location,
            build,
            source,
            used,
        } = entry;

        let mut asm = self.unit.new_assembly(location);

        match build {
            Build::Function(f) => {
                use self::v1::AssembleFn as _;

                let args = format_fn_args(&*source, f.ast.args.iter().map(|(a, _)| a))?;

                let span = f.ast.span();
                let count = f.ast.args.len();

                let mut c = self.compiler1(location, &source, span, &mut asm);
                f.ast.assemble_fn(&mut c, false)?;

                // NB: experimental compiler that is work-in-progress
                #[cfg(compiler_v2)]
                if self.options.v2 {
                    let mut program = rune_ssa::Program::new();
                    let mut c2 = self.compiler2(location, &source, span, &mut program);
                    self::v2::AssembleFn::assemble_fn(f.ast.as_ref(), &mut c2, true)?;
                    program.seal().with_span(span)?;
                    println!("{}", program.dump());
                }

                if used.is_unused() {
                    self.diagnostics.not_used(location.source_id, span, None);
                } else {
                    self.unit.new_function(
                        location,
                        item.item.clone(),
                        count,
                        asm,
                        f.call,
                        args,
                    )?;
                }
            }
            Build::InstanceFunction(f) => {
                use self::v1::AssembleFn as _;

                let args = format_fn_args(&*source, f.ast.args.iter().map(|(a, _)| a))?;

                let span = f.ast.span();
                let count = f.ast.args.len();
                let name = f.ast.name.resolve(self.storage, &*source)?;

                let mut c = self.compiler1(location, &source, span, &mut asm);
                let meta = c.lookup_meta(f.instance_span, &f.impl_item)?;

                let type_hash = meta
                    .type_hash_of()
                    .ok_or_else(|| CompileError::expected_meta(span, meta, "instance function"))?;

                f.ast.assemble_fn(&mut c, true)?;

                if used.is_unused() {
                    c.diagnostics.not_used(location.source_id, span, None);
                } else {
                    self.unit.new_instance_function(
                        location,
                        item.item.clone(),
                        type_hash,
                        name.as_ref(),
                        count,
                        asm,
                        f.call,
                        args,
                    )?;
                }
            }
            Build::Closure(closure) => {
                use self::v1::AssembleClosure as _;

                let span = closure.ast.span();
                let args =
                    format_fn_args(&*source, closure.ast.args.as_slice().iter().map(|(a, _)| a))?;

                let mut c = self.compiler1(location, &source, span, &mut asm);
                closure.ast.assemble_closure(&mut c, &closure.captures)?;

                if used.is_unused() {
                    c.diagnostics
                        .not_used(location.source_id, location.span, None);
                } else {
                    self.unit.new_function(
                        location,
                        item.item.clone(),
                        closure.ast.args.len(),
                        asm,
                        closure.call,
                        args,
                    )?;
                }
            }
            Build::AsyncBlock(b) => {
                use self::v1::AssembleClosure as _;

                let args = b.captures.len();
                let span = b.ast.span();

                let mut c = self.compiler1(location, &source, span, &mut asm);
                b.ast.assemble_closure(&mut c, &b.captures)?;

                if used.is_unused() {
                    self.diagnostics
                        .not_used(location.source_id, location.span, None);
                } else {
                    self.unit.new_function(
                        location,
                        item.item.clone(),
                        args,
                        asm,
                        b.call,
                        Vec::new(),
                    )?;
                }
            }
            Build::Unused => {
                self.diagnostics
                    .not_used(location.source_id, location.span, None);
            }
            Build::Import(import) => {
                // Issue the import to check access.
                let result = self
                    .query
                    .import(location.span, &item.module, &item.item, used)?;

                if used.is_unused() {
                    self.diagnostics
                        .not_used(location.source_id, location.span, None);
                }

                let missing = match &result {
                    Some(item) => {
                        if self.context.contains_prefix(item) || self.query.contains_prefix(item) {
                            None
                        } else {
                            Some(item)
                        }
                    }
                    None => Some(&import.entry.target),
                };

                if let Some(item) = missing {
                    return Err(CompileError::new(
                        location.span,
                        CompileErrorKind::MissingItem { item: item.clone() },
                    ));
                }
            }
            Build::ReExport => {
                let import =
                    match self
                        .query
                        .import(location.span, &item.module, &item.item, used)?
                    {
                        Some(item) => item,
                        None => {
                            return Err(CompileError::new(
                                location.span,
                                CompileErrorKind::MissingItem {
                                    item: item.item.clone(),
                                },
                            ))
                        }
                    };

                self.unit
                    .new_function_reexport(location, &item.item, &import)?;
            }
        }

        Ok(())
    }
}

fn format_fn_args<'a, I>(source: &Source, arguments: I) -> Result<Vec<String>, CompileError>
where
    I: IntoIterator<Item = &'a ast::FnArg>,
{
    let mut args = Vec::new();

    for arg in arguments {
        match arg {
            ast::FnArg::SelfValue(..) => {
                args.push(String::from("self"));
            }
            ast::FnArg::Pat(pat) => {
                let span = pat.span();

                if let Some(s) = source.source(span) {
                    args.push(s.to_owned());
                } else {
                    args.push(String::from("*"));
                }
            }
        }
    }

    Ok(args)
}