doge-compiler 0.3.3

Compiler for the Doge programming language — lexer, parser, semantic checks, and Rust codegen.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
use super::*;

impl Codegen {
    pub(super) fn call(
        &self,
        callee: &Expr,
        args: &[Expr],
        kwargs: &[(String, Expr)],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        match callee {
            Expr::Ident { name, .. } => {
                // A local shadows every other meaning — call through its value. A
                // known nested function still gets a compile-time arity check, but
                // it is reached through the dispatcher, so keyword arguments (which
                // need a compile-time-known target) are not available.
                if emit.locals.contains_key(name) {
                    if let Some(params) = emit.local_funcs.get(name) {
                        self.reject_kwargs(
                            kwargs,
                            span,
                            &format!("call {name} without keyword arguments"),
                        )?;
                        self.check_positional_arity(name, params, args.len(), span)?;
                    } else {
                        self.reject_kwargs(
                            kwargs,
                            span,
                            "call this by a known function name to use keyword arguments",
                        )?;
                    }
                    let callee_val = self.resolve_read(emit, name);
                    return self.indirect_call(&callee_val, args, emit);
                }
                if crate::builtins::is_builtin(name) {
                    self.reject_kwargs(
                        kwargs,
                        span,
                        &format!("{name} takes positional arguments only"),
                    )?;
                    return self.builtin_call(name, args, span, emit);
                }
                if let Some(params) = emit.table().funcs.get(name) {
                    let (prelude, parts) =
                        self.resolve_args(name, params, args, kwargs, span, emit)?;
                    let target = func_wrapper(emit.file_id, name);
                    return Ok(self.finish_call(emit, &prelude, parts, &target));
                }
                // `Shibe(...)` — a constructor, resolved statically.
                if emit.class(name).is_some() {
                    return self.constructor_call(name, args, kwargs, span, emit);
                }
                // A module name is not itself callable — you call one of its members.
                if let Some(member) = self.module_first_member(emit, name) {
                    return Err(self
                        .diag(span, format!("{name} is a module, not a function"))
                        .with_headline("very module. much confuse.")
                        .with_hint(format!("call a member — {name}.{member}(…)")));
                }
                // A top-level variable holding a function value: call it indirectly.
                self.reject_kwargs(
                    kwargs,
                    span,
                    "call this by a known function name to use keyword arguments",
                )?;
                let callee_val = format!("env.{}.clone()", field_name(emit.file_id, name));
                self.indirect_call(&callee_val, args, emit)
            }
            // `nerd.sqrt(16)` / `utils.square(6)` — a member call on a module.
            Expr::Attr { obj, name, .. }
                if matches!(obj.as_ref(), Expr::Ident { name: base, .. }
                    if emit.module(base).is_some() || emit.user_module(base).is_some()) =>
            {
                let Expr::Ident { name: base, .. } = obj.as_ref() else {
                    unreachable!("compiler bug: guarded to an Ident base")
                };
                if let Some(module) = emit.module(base) {
                    self.reject_kwargs(
                        kwargs,
                        span,
                        &format!("{base}.{name} takes positional arguments only"),
                    )?;
                    self.module_call(base, module, name, args, span, emit)
                } else {
                    let fid = emit
                        .user_module(base)
                        .expect("compiler bug: module vanished");
                    self.user_module_call(base, fid, name, args, kwargs, span, emit)
                }
            }
            // `kabosu.speak(...)` — a method call, dispatched at runtime, so it takes
            // positional arguments only.
            Expr::Attr { obj, name, .. } => {
                self.reject_kwargs(kwargs, span, "pass these arguments positionally")?;
                emit.uses_method_call.set(true);
                let recv = self.expr(obj, emit)?;
                let mut arg_parts = Vec::with_capacity(args.len());
                for arg in args {
                    arg_parts.push(self.expr(arg, emit)?);
                }
                let call = format!(
                    "call_method({recv}, \"{}\", vec![{}], &mut *env)",
                    escape_str(name),
                    arg_parts.join(", ")
                );
                Ok(self.fail(emit, call))
            }
            // Any other callee expression — `f()()`, `xs[0]()` — is called through
            // the value it evaluates to.
            _ => {
                self.reject_kwargs(
                    kwargs,
                    span,
                    "call this by a known function name to use keyword arguments",
                )?;
                let callee_val = self.expr(callee, emit)?;
                self.indirect_call(&callee_val, args, emit)
            }
        }
    }

    /// Keyword arguments are only accepted where the callee is known at compile
    /// time (a top-level user function, a constructor, or a user-module function).
    /// Everywhere else they are a compile error with a `hint` on what to do.
    pub(super) fn reject_kwargs(
        &self,
        kwargs: &[(String, Expr)],
        span: Span,
        hint: &str,
    ) -> Result<(), Diagnostic> {
        if kwargs.is_empty() {
            return Ok(());
        }
        Err(self
            .diag(
                span,
                "keyword arguments only work when doge knows the function at compile time",
            )
            .with_headline("very keyword. much dynamic.")
            .with_hint(hint.to_string()))
    }

    /// Call a value: dispatch through `call_value`, which routes a bound method
    /// back through `call_method` and everything else through `call_function`. The
    /// call is fallible and routed through [`Codegen::fail`].
    pub(super) fn indirect_call(
        &self,
        callee_val: &str,
        args: &[Expr],
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        emit.uses_call_function.set(true);
        let mut arg_parts = Vec::with_capacity(args.len());
        for arg in args {
            arg_parts.push(self.expr(arg, emit)?);
        }
        let call = format!(
            "call_value({callee_val}, vec![{}], &mut *env)",
            arg_parts.join(", ")
        );
        Ok(self.fail(emit, call))
    }

    /// A constructor call `Shibe(args)`: resolve against `init`'s parameters (with
    /// defaults, variadic, and keyword arguments), then a `n_<id>(args…, &mut
    /// *env)` through the fail suffix.
    pub(super) fn constructor_call(
        &self,
        name: &str,
        args: &[Expr],
        kwargs: &[(String, Expr)],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        let class = emit
            .class(name)
            .expect("compiler bug: constructor for an unknown class");
        self.emit_constructor_call(name, class, args, kwargs, span, emit)
    }

    /// Emit a constructor call `n_<id>(args…, &mut *env)` for `class`, resolving
    /// `args`/`kwargs` against its `init` header. `label` is the callee name shown
    /// in arity/keyword diagnostics (`Shibe` locally, `utils.Shibe` cross-file).
    #[allow(clippy::too_many_arguments)]
    pub(super) fn emit_constructor_call(
        &self,
        label: &str,
        class: &Class,
        args: &[Expr],
        kwargs: &[(String, Expr)],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        let init_params = init_params(emit.classes, class);
        let (prelude, parts) = self.resolve_args(label, init_params, args, kwargs, span, emit)?;
        let target = format!("{CTOR_PREFIX}{}", class.id);
        Ok(self.finish_call(emit, &prelude, parts, &target))
    }

    /// A stdlib member call `module.member(args)`: static arity against the table,
    /// then `{runtime_fn}(&a0, &a1, …)` through the fail suffix. Calling a const,
    /// or an unknown member, is a real error.
    pub(super) fn module_call(
        &self,
        module_name: &str,
        module: &Module,
        member: &str,
        args: &[Expr],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        let func = match module.func(member) {
            Some(func) => func,
            None => {
                if module.const_expr(member).is_some() {
                    return Err(self
                        .diag(
                            span,
                            format!("{module_name}.{member} is a constant, not a function"),
                        )
                        .with_headline("very module. much confuse.")
                        .with_hint(format!("use it as a value — bark {module_name}.{member}")));
                }
                return Err(self.unknown_member(module_name, member, module, span));
            }
        };
        let max_arity = func.max_arity();
        if args.len() < func.arity || args.len() > max_arity {
            let takes = if func.optional {
                format!("{} or {} arguments", func.arity, max_arity)
            } else {
                let noun = if func.arity == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                format!("{} {noun}", func.arity)
            };
            return Err(self
                .diag(
                    span,
                    format!("{module_name}.{member} takes {takes}, got {}", args.len()),
                )
                .with_headline(ARITY_HEADLINE)
                .with_hint(func.hint));
        }
        // `pack.zoom(f, args)` also receives the generated pup trampoline and a
        // snapshot of the globals, so the pup can rebuild a fresh world and run the
        // call on its own thread. It calls values indirectly, so it needs the
        // function dispatcher too.
        if func.runtime_fn == crate::stdlib::PACK_ZOOM_RUNTIME_FN {
            emit.uses_call_function.set(true);
            emit.uses_pup_entry.set(true);
            let f = self.expr(&args[0], emit)?;
            let job = self.expr(&args[1], emit)?;
            let call = format!("pack_zoom(pup_entry, snapshot_env(&*env), &{f}, &{job})");
            return Ok(self.fail(emit, call));
        }
        let mut parts = Vec::with_capacity(max_arity);
        for arg in args {
            parts.push(format!("&{}", self.expr(arg, emit)?));
        }
        // An omitted trailing optional argument reaches the runtime as `Value::None`.
        while parts.len() < max_arity {
            parts.push("&Value::None".to_string());
        }
        let call = format!("{}({})", func.runtime_fn, parts.join(", "));
        Ok(self.fail(emit, call))
    }

    /// A user module member call `utils.square(args)`: resolve against the
    /// module's function table (with defaults, variadic, and keyword arguments),
    /// then a direct call to its mangled wrapper. Calling a constant, or an unknown
    /// member, is a real error.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn user_module_call(
        &self,
        module_name: &str,
        fid: u32,
        member: &str,
        args: &[Expr],
        kwargs: &[(String, Expr)],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        // `utils.Shibe(…)` — a constructor for one of the module's classes.
        if let Some(class) = emit.class_in(fid, member) {
            let label = format!("{module_name}.{member}");
            return self.emit_constructor_call(&label, class, args, kwargs, span, emit);
        }
        let table = &emit.tables[fid as usize];
        let params = match table.funcs.get(member) {
            Some(params) => params,
            None => {
                if table.consts.contains(member) {
                    return Err(self
                        .diag(
                            span,
                            format!("{module_name}.{member} is a constant, not a function"),
                        )
                        .with_headline("very module. much confuse.")
                        .with_hint(format!("use it as a value — bark {module_name}.{member}")));
                }
                return Err(self.unknown_user_member(emit, module_name, fid, member, span));
            }
        };
        let label = format!("{module_name}.{member}");
        let (prelude, parts) = self.resolve_args(&label, params, args, kwargs, span, emit)?;
        let target = func_wrapper(fid, member);
        Ok(self.finish_call(emit, &prelude, parts, &target))
    }

    /// `super.method(args)` inside a method body: resolve `method` statically to
    /// the nearest ancestor of the enclosing class that defines it, then call that
    /// ancestor's `mf_` wrapper with the current `self` as the receiver. The
    /// checker validates the context; the fallbacks here keep direct codegen (with
    /// no prior check pass) non-panicking.
    pub(super) fn super_call(
        &self,
        method: &str,
        args: &[Expr],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        let class = emit
            .current_class
            .and_then(|id| emit.classes.iter().find(|c| c.id == id))
            .ok_or_else(|| {
                self.diag(span, "super only works inside a method")
                    .with_headline("very super. much lost.")
                    .with_hint("use super inside a method of a class with a parent")
            })?;
        let parent = class
            .parent
            .and_then(|pid| emit.classes.iter().find(|c| c.id == pid))
            .ok_or_else(|| {
                self.diag(
                    span,
                    format!("{} has no parent to call super on", class.name),
                )
                .with_headline("very super. much orphan.")
                .with_hint(format!(
                    "give it a parent — many {} much Parent:",
                    class.name
                ))
            })?;
        let (def, params) = effective_methods(emit.classes, parent)
            .into_iter()
            .find(|(name, _, _)| *name == method)
            .map(|(_, def, params)| (def, params))
            .ok_or_else(|| {
                self.diag(
                    span,
                    format!("no parent of {} has a method {method}", class.name),
                )
                .with_headline("very super. much unknown.")
                .with_hint(format!("check the method name — super.{method}(…)"))
            })?;

        let label = format!("{}.{method}", def.name);
        let (prelude, mut parts) = self.resolve_args(&label, params, args, &[], span, emit)?;
        parts.insert(0, self.resolve_read(emit, "self"));
        let target = format!("{METHOD_PREFIX}{}_{method}", def.id);
        Ok(self.finish_call(emit, &prelude, parts, &target))
    }

    pub(super) fn builtin_call(
        &self,
        name: &str,
        args: &[Expr],
        span: Span,
        emit: &Emit,
    ) -> Result<String, Diagnostic> {
        let builtin = self.check_builtin_arity(name, args, span)?;
        match builtin.shape {
            BuiltinShape::Fallible => {
                let arg = self.expr(&args[0], emit)?;
                Ok(self.fail(emit, format!("{}(&{arg})", builtin.runtime_fn)))
            }
            BuiltinShape::Infallible => {
                let arg = self.expr(&args[0], emit)?;
                Ok(format!("{}(&{arg})", builtin.runtime_fn))
            }
            BuiltinShape::Range if args.len() == 1 => Ok(self.fail(
                emit,
                format!("range(&Value::int(0i64), &{})", self.expr(&args[0], emit)?),
            )),
            BuiltinShape::Range => Ok(self.fail(
                emit,
                format!(
                    "range(&{}, &{})",
                    self.expr(&args[0], emit)?,
                    self.expr(&args[1], emit)?
                ),
            )),
            BuiltinShape::Prompt if args.is_empty() => {
                Ok(self.fail(emit, format!("{}(None)", builtin.runtime_fn)))
            }
            BuiltinShape::Prompt => Ok(self.fail(
                emit,
                format!(
                    "{}(Some(&{}))",
                    builtin.runtime_fn,
                    self.expr(&args[0], emit)?
                ),
            )),
        }
    }

    /// Builtin arity is statically known (the accepted counts live in the builtin
    /// table). Returns the resolved builtin so the caller can emit by its shape.
    pub(super) fn check_builtin_arity(
        &self,
        name: &str,
        args: &[Expr],
        span: Span,
    ) -> Result<&'static BuiltinFn, Diagnostic> {
        let builtin = crate::builtins::builtin(name)
            .expect("compiler bug: builtin_call on a name that is not a builtin");
        if builtin.accepts(args.len()) {
            return Ok(builtin);
        }
        Err(self
            .diag(
                span,
                format!(
                    "{name} takes {}, got {}",
                    builtin.arity_phrase(),
                    args.len()
                ),
            )
            .with_headline(ARITY_HEADLINE)
            .with_hint(builtin.hint))
    }

    /// A positional-only arity check (for a nested function reached through the
    /// dispatcher): the argument count must be within the header's accepted range.
    pub(super) fn check_positional_arity(
        &self,
        name: &str,
        params: &Params,
        got: usize,
        span: Span,
    ) -> Result<(), Diagnostic> {
        let too_few = got < params.required();
        let too_many = params.max_positional().is_some_and(|max| got > max);
        if too_few || too_many {
            return Err(self.arity_error(name, params, got, span));
        }
        Ok(())
    }

    /// Map a call's positional and keyword arguments onto a header's binding slots:
    /// fill each parameter from a positional argument, a keyword argument, or its
    /// default; collect any surplus positionals into the variadic. Returns a
    /// `let`-binding prelude (non-empty only when keyword arguments force an
    /// evaluation-order rewrite) and the argument expressions in binding order.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn resolve_args(
        &self,
        callee: &str,
        params: &Params,
        args: &[Expr],
        kwargs: &[(String, Expr)],
        span: Span,
        emit: &Emit,
    ) -> Result<(String, Vec<String>), Diagnostic> {
        let n = params.params.len();
        let has_vararg = params.has_vararg();

        if !has_vararg && args.len() > n {
            return Err(self.arity_error(callee, params, args.len() + kwargs.len(), span));
        }

        if kwargs.is_empty() {
            if args.len() < params.required() {
                return Err(self.arity_error(callee, params, args.len(), span));
            }
            let mut out = Vec::with_capacity(n + has_vararg as usize);
            for i in 0..n {
                if i < args.len() {
                    out.push(self.expr(&args[i], emit)?);
                } else {
                    let default = params.params[i]
                        .default
                        .as_ref()
                        .expect("compiler bug: unfilled required slot without an arity error");
                    out.push(self.expr(default, emit)?);
                }
            }
            if has_vararg {
                let mut extras = Vec::new();
                for arg in &args[n..] {
                    extras.push(self.expr(arg, emit)?);
                }
                out.push(format!("Value::list(vec![{}])", extras.join(", ")));
            }
            return Ok((String::new(), out));
        }

        // Keyword arguments present: evaluate every provided argument (positional
        // then keyword) into a temporary in written order, then arrange the
        // temporaries into binding order so evaluation stays left-to-right.
        let mut temps: Vec<String> = Vec::new();
        let mut slot: Vec<Option<usize>> = vec![None; n];
        let mut vararg_temps: Vec<usize> = Vec::new();
        for (i, arg) in args.iter().enumerate() {
            let ti = temps.len();
            temps.push(self.expr(arg, emit)?);
            if i < n {
                slot[i] = Some(ti);
            } else {
                vararg_temps.push(ti);
            }
        }
        for (kname, kexpr) in kwargs {
            let idx = match params.params.iter().position(|p| &p.name == kname) {
                Some(idx) => idx,
                None => {
                    let names_vararg = params.vararg.as_deref() == Some(kname.as_str());
                    let detail = if names_vararg {
                        format!("{kname} is the many parameter and cannot be given by keyword")
                    } else {
                        format!("{callee} has no parameter {kname}")
                    };
                    return Err(self
                        .diag(span, detail)
                        .with_headline("very keyword. much unknown.")
                        .with_hint(params.render(callee)));
                }
            };
            if slot[idx].is_some() {
                return Err(self
                    .diag(span, format!("{callee} got parameter {kname} twice"))
                    .with_headline("very keyword. much repeat.")
                    .with_hint(params.render(callee)));
            }
            let ti = temps.len();
            temps.push(self.expr(kexpr, emit)?);
            slot[idx] = Some(ti);
        }

        let mut out = Vec::with_capacity(n + has_vararg as usize);
        for (i, filled) in slot.iter().enumerate() {
            match filled {
                Some(ti) => out.push(format!("__a{ti}")),
                None => match params.params[i].default.as_ref() {
                    Some(default) => out.push(self.expr(default, emit)?),
                    None => {
                        return Err(self.arity_error(
                            callee,
                            params,
                            args.len() + kwargs.len(),
                            span,
                        ))
                    }
                },
            }
        }
        if has_vararg {
            let items: Vec<String> = vararg_temps.iter().map(|ti| format!("__a{ti}")).collect();
            out.push(format!("Value::list(vec![{}])", items.join(", ")));
        }

        let mut prelude = String::new();
        for (ti, expr) in temps.iter().enumerate() {
            prelude.push_str(&format!("let __a{ti} = {expr}; "));
        }
        Ok((prelude, out))
    }

    /// Assemble a direct call from resolved argument expressions: append `env`,
    /// apply the fail suffix, and wrap in a block when a `let` prelude is present.
    pub(super) fn finish_call(
        &self,
        emit: &Emit,
        prelude: &str,
        mut parts: Vec<String>,
        target: &str,
    ) -> String {
        parts.push("&mut *env".to_string());
        let call = self.fail(emit, format!("{target}({})", parts.join(", ")));
        if prelude.is_empty() {
            call
        } else {
            format!("{{ {prelude}{call} }}")
        }
    }

    /// The arity diagnostic for a call whose count falls outside the header's
    /// accepted range: a fixed count, a `R to X` range, or an `at least R` when a
    /// variadic makes the upper bound unbounded. The hint echoes the call shape.
    pub(super) fn arity_error(
        &self,
        callee: &str,
        params: &Params,
        got: usize,
        span: Span,
    ) -> Diagnostic {
        let required = params.required();
        let phrase = match params.max_positional() {
            Some(max) if max == required => {
                let noun = if required == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                format!("{callee} takes {required} {noun}, got {got}")
            }
            Some(max) => format!("{callee} takes {required} to {max} arguments, got {got}"),
            None => {
                let noun = if required == 1 {
                    "argument"
                } else {
                    "arguments"
                };
                format!("{callee} takes at least {required} {noun}, got {got}")
            }
        };
        self.diag(span, phrase)
            .with_headline(ARITY_HEADLINE)
            .with_hint(params.render(callee))
    }
}