lisette-syntax 0.13.2

Little language inspired by Rust that compiles to Go
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
use std::fmt::{self, Display, Formatter};

use ecow::EcoString;

use super::{PRELUDE_PACKAGE_ID, is_go_import, is_prelude_symbol};
use crate::ast::Expression;
use crate::types::Symbol;
use crate::types::{CompoundKind, SimpleKind, SubstitutionMap, Type, TypeKind};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MethodName {
    pub owner: Symbol,
    pub name: EcoString,
}

impl MethodName {
    pub fn new(owner: Symbol, name: &str) -> Self {
        Self {
            owner,
            name: name.into(),
        }
    }

    pub fn is_go(&self) -> bool {
        is_go_import(&self.owner)
    }
}

impl Display for MethodName {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}", self.owner, self.name)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum CalleeName {
    Definition(Symbol),
    Method(MethodName),
}

impl CalleeName {
    pub fn definition(&self) -> Option<&Symbol> {
        match self {
            Self::Definition(name) => Some(name),
            Self::Method(_) => None,
        }
    }

    pub fn method(&self) -> Option<&MethodName> {
        match self {
            Self::Definition(_) => None,
            Self::Method(method) => Some(method),
        }
    }

    pub fn is_go(&self) -> bool {
        match self {
            Self::Definition(name) => is_go_import(name),
            Self::Method(method) => method.is_go(),
        }
    }

    pub fn is_prelude(&self) -> bool {
        match self {
            Self::Definition(name) => is_prelude_symbol(name),
            Self::Method(method) => is_prelude_symbol(&method.owner),
        }
    }

    pub fn qualified_name(&self) -> Symbol {
        match self {
            Self::Definition(name) => name.clone(),
            Self::Method(method) => method.owner.with_segment(&method.name),
        }
    }

    pub fn last_segment(&self) -> &str {
        match self {
            Self::Definition(name) => name.last_segment(),
            Self::Method(method) => &method.name,
        }
    }
}

impl Display for CalleeName {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Definition(name) => name.fmt(f),
            Self::Method(method) => method.fmt(f),
        }
    }
}

pub fn resolved_callee(expression: &Expression) -> Option<CalleeName> {
    match expression {
        Expression::Identifier { resolution, .. } => resolution.callee_name(),
        Expression::DotAccess { resolution, .. } => resolution.callee_name(),
        _ => None,
    }
}

pub fn resolved_definition_name(expression: &Expression) -> Option<&str> {
    match expression {
        Expression::Identifier { resolution, .. } => resolution.definition(),
        Expression::DotAccess { resolution, .. } => resolution.defined_name().map(Symbol::as_str),
        _ => None,
    }
}

/// The prelude enum and member an expression resolves to, as `("prelude.Option", "None")`.
pub fn prelude_constructor(expression: &Expression) -> Option<(&str, &str)> {
    let (owner, member) = resolved_definition_name(expression)?.rsplit_once('.')?;
    let owner = match (owner, member) {
        (PRELUDE_PACKAGE_ID, "Some" | "None") => "prelude.Option",
        (PRELUDE_PACKAGE_ID, "Ok" | "Err") => "prelude.Result",
        _ => owner,
    };
    Some((owner, member))
}

pub fn resolved_instantiation(expression: &Expression) -> Option<&SubstitutionMap> {
    match expression {
        Expression::Identifier { resolution, .. } => resolution.instantiation(),
        Expression::DotAccess { resolution, .. } => resolution.instantiation(),
        _ => None,
    }
}

#[derive(Debug, Clone, Copy)]
pub enum ChannelOperation<'a> {
    Receive {
        channel: &'a Expression,
    },
    Send {
        channel: &'a Expression,
        value: &'a Expression,
    },
}

impl<'a> ChannelOperation<'a> {
    pub fn channel(self) -> &'a Expression {
        match self {
            Self::Receive { channel } | Self::Send { channel, .. } => channel,
        }
    }

    pub fn value(self) -> Option<&'a Expression> {
        match self {
            Self::Receive { .. } => None,
            Self::Send { value, .. } => Some(value),
        }
    }
}

pub fn channel_operation(expression: &Expression) -> Option<ChannelOperation<'_>> {
    let Expression::Call {
        expression: callee,
        args,
        ..
    } = expression
    else {
        return None;
    };

    let (channel, method, operands): (&Expression, &str, &[Expression]) = match callee.as_ref() {
        Expression::DotAccess {
            expression: channel,
            member,
            ..
        } => (channel, member, args),
        Expression::Identifier { value, .. } if has_channel_ufcs_prefix(value) => {
            let (channel, operands) = args.split_first()?;
            (channel, value.rsplit('.').next()?, operands)
        }
        _ => return None,
    };

    match (method, operands) {
        ("receive", []) => Some(ChannelOperation::Receive { channel }),
        ("send", [value]) => Some(ChannelOperation::Send { channel, value }),
        _ => None,
    }
}

fn has_channel_ufcs_prefix(identifier: &str) -> bool {
    let Some((prefix, _)) = identifier.rsplit_once('.') else {
        return false;
    };
    matches!(
        prefix.rsplit('.').next(),
        Some("Channel" | "Sender" | "Receiver")
    )
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReceiverCoercion {
    /// Insert `&` to convert `T` to `Ref<T>`
    AutoAddress,
    /// Insert `*` to convert `Ref<T>` to `T`
    AutoDeref,
}

/// What a dot access resolved to during type checking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DotAccessResolution {
    Unresolved,
    /// Named struct field access
    StructField {
        is_exported: bool,
        declaring_type: Option<Symbol>,
    },
    /// Tuple struct field access (e.g., `point.0` on `struct Point(int, int)`).
    /// `is_newtype` is true when the struct has exactly 1 field and no generics,
    /// meaning access should emit a type cast rather than `.F0`.
    TupleStructField {
        index: usize,
        is_newtype: bool,
    },
    /// Tuple element access (e.g., `t.0`, `t.1`)
    TupleElement {
        index: usize,
    },
    /// Package member access (e.g., `mod.func`)
    PackageMember {
        definition: Option<Symbol>,
        instantiation: SubstitutionMap,
    },
    /// ADT enum variant constructor (e.g., `makeColorRed[T]()`)
    EnumVariant {
        definition: Symbol,
        instantiation: SubstitutionMap,
    },
    /// Instance method (has `self` receiver)
    InstanceMethod {
        is_exported: bool,
        receiver_coercion: Option<ReceiverCoercion>,
        method: Option<MethodName>,
        instantiation: SubstitutionMap,
    },
    /// Instance method used as a first-class value (not called).
    /// E.g., `Point.area` used as a callback.
    InstanceMethodValue {
        is_exported: bool,
        method: Option<MethodName>,
        instantiation: SubstitutionMap,
    },
    /// Static method (no `self` receiver)
    StaticMethod {
        is_exported: bool,
        definition: Symbol,
        instantiation: SubstitutionMap,
    },
}

impl DotAccessResolution {
    pub fn reads_receiver(&self) -> bool {
        matches!(
            self,
            Self::StructField { .. }
                | Self::TupleStructField { .. }
                | Self::TupleElement { .. }
                | Self::InstanceMethod { .. }
                | Self::InstanceMethodValue { .. }
        )
    }

    pub fn is_field_read(&self) -> bool {
        matches!(
            self,
            Self::StructField { .. } | Self::TupleStructField { .. } | Self::TupleElement { .. }
        )
    }

    pub fn declaring_type(&self) -> Option<&Symbol> {
        match self {
            Self::StructField { declaring_type, .. } => declaring_type.as_ref(),
            _ => None,
        }
    }

    pub fn receiver_coercion(&self) -> Option<ReceiverCoercion> {
        match self {
            Self::InstanceMethod {
                receiver_coercion, ..
            } => *receiver_coercion,
            _ => None,
        }
    }

    pub fn defined_name(&self) -> Option<&Symbol> {
        match self {
            Self::PackageMember { definition, .. } => definition.as_ref(),
            Self::EnumVariant { definition, .. } | Self::StaticMethod { definition, .. } => {
                Some(definition)
            }
            Self::Unresolved
            | Self::StructField { .. }
            | Self::TupleStructField { .. }
            | Self::TupleElement { .. }
            | Self::InstanceMethod { .. }
            | Self::InstanceMethodValue { .. } => None,
        }
    }

    pub fn method(&self) -> Option<&MethodName> {
        match self {
            Self::InstanceMethod { method, .. } | Self::InstanceMethodValue { method, .. } => {
                method.as_ref()
            }
            _ => None,
        }
    }

    pub fn callee_name(&self) -> Option<CalleeName> {
        match self.method() {
            Some(method) => Some(CalleeName::Method(method.clone())),
            None => self.defined_name().cloned().map(CalleeName::Definition),
        }
    }

    pub fn instantiation(&self) -> Option<&SubstitutionMap> {
        match self {
            Self::PackageMember { instantiation, .. }
            | Self::EnumVariant { instantiation, .. }
            | Self::InstanceMethod { instantiation, .. }
            | Self::InstanceMethodValue { instantiation, .. }
            | Self::StaticMethod { instantiation, .. } => Some(instantiation),
            Self::Unresolved
            | Self::StructField { .. }
            | Self::TupleStructField { .. }
            | Self::TupleElement { .. } => None,
        }
    }
}

/// What kind of native built-in type (Slice, Map, Channel, etc.) a call targets.
/// Defined here so semantics can classify calls without depending on
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeTypeKind {
    Slice,
    EnumeratedSlice,
    Map,
    Channel,
    Sender,
    Receiver,
    String,
    Array,
}

impl NativeTypeKind {
    pub fn from_type(ty: &Type) -> Option<Self> {
        Some(match ty.strip_refs().kind() {
            TypeKind::Compound { kind, .. } => match kind {
                CompoundKind::Slice => Self::Slice,
                CompoundKind::EnumeratedSlice => Self::EnumeratedSlice,
                CompoundKind::Map => Self::Map,
                CompoundKind::Channel => Self::Channel,
                CompoundKind::Sender => Self::Sender,
                CompoundKind::Receiver => Self::Receiver,
                CompoundKind::Ref | CompoundKind::VarArgs => return None,
            },
            TypeKind::Simple(SimpleKind::String) => Self::String,
            TypeKind::Array { .. } => Self::Array,
            _ => return None,
        })
    }

    pub fn from_constructor_path(path: &str) -> Option<Self> {
        match path {
            "Channel.new" | "Channel.buffered" => Some(Self::Channel),
            "Map.new" => Some(Self::Map),
            "Slice.new" | "Slice.make" => Some(Self::Slice),
            "Array.new" | "Array.from" => Some(Self::Array),
            _ => None,
        }
    }

    pub fn is_constructor_method(name: &str) -> bool {
        matches!(name, "new" | "buffered" | "make")
    }

    pub fn from_name(name: &str) -> Option<Self> {
        match name {
            "Slice" => Some(Self::Slice),
            "EnumeratedSlice" => Some(Self::EnumeratedSlice),
            "Map" => Some(Self::Map),
            "Channel" => Some(Self::Channel),
            "Sender" => Some(Self::Sender),
            "Receiver" => Some(Self::Receiver),
            "string" => Some(Self::String),
            "Array" => Some(Self::Array),
            _ => None,
        }
    }

    pub fn name(self) -> &'static str {
        match self {
            Self::Slice => "Slice",
            Self::EnumeratedSlice => "EnumeratedSlice",
            Self::Map => "Map",
            Self::Channel => "Channel",
            Self::Sender => "Sender",
            Self::Receiver => "Receiver",
            Self::String => "string",
            Self::Array => "Array",
        }
    }
}