swamp_script_semantic/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5pub mod intr;
6pub mod modules;
7pub mod ns;
8pub mod prelude;
9pub mod symtbl;
10
11use crate::intr::IntrinsicFunction;
12pub use fixed32::Fp;
13use seq_fmt::comma;
14use seq_map::{SeqMap, SeqMapError};
15use std::cell::RefCell;
16use std::cmp::PartialEq;
17use std::fmt;
18use std::fmt::{Debug, Display, Formatter};
19use std::hash::Hash;
20use std::rc::Rc;
21
22#[derive(Clone, Eq, PartialEq, Default)]
23pub struct Node {
24    pub span: Span,
25}
26
27impl Debug for Node {
28    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
29        if self.span.file_id == 0xffff {
30            write!(f, "<{}:{}>", self.span.offset, self.span.length)
31        } else {
32            write!(
33                f,
34                "<{}:{} ({})>",
35                self.span.offset, self.span.length, self.span.file_id
36            )
37        }
38    }
39}
40
41pub type FileId = u16;
42
43#[derive(PartialEq, Eq, Hash, Default, Clone)]
44pub struct Span {
45    pub file_id: FileId,
46    pub offset: u32,
47    pub length: u16,
48}
49
50impl Span {
51    pub fn dummy() -> Self {
52        Span {
53            offset: 0,
54            length: 0,
55            file_id: 0xffff,
56        }
57    }
58
59    // Helper method to get the end position
60    pub fn end(&self) -> u32 {
61        self.offset + self.length as u32
62    }
63}
64
65impl Debug for Span {
66    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67        write!(f, "<{}:{} ({})>", self.offset, self.length, self.file_id)
68    }
69}
70
71#[derive(Clone, Eq, PartialEq)]
72pub struct ParameterNode {
73    pub name: Node,
74    pub is_mutable: Option<Node>,
75}
76
77impl Debug for ParameterNode {
78    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
79        write!(f, "Parameter")
80    }
81}
82
83impl ParameterNode {
84    #[inline]
85    #[must_use]
86    pub const fn is_mutable(&self) -> bool {
87        self.is_mutable.is_some()
88    }
89}
90
91#[derive(Debug, Clone, Eq, PartialEq)]
92pub struct Signature {
93    pub parameters: Vec<TypeForParameter>,
94    pub return_type: Box<Type>,
95}
96
97impl Display for Signature {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
99        write!(f, "({})->{}", comma(&self.parameters), self.return_type)
100    }
101}
102
103impl Signature {
104    pub fn same_type(&self, other: &Signature) -> bool {
105        if self.parameters.len() != other.parameters.len()
106            || !self.return_type.compatible_with(&other.return_type)
107        {
108            return false;
109        }
110
111        for (param, other_param) in self.parameters.iter().zip(other.parameters.clone()) {
112            if !&param
113                .resolved_type
114                .compatible_with(&other_param.resolved_type)
115            {
116                return false;
117            }
118
119            if param.is_mutable != other_param.is_mutable {
120                return false;
121            }
122        }
123
124        true
125    }
126}
127
128#[derive(Debug, Clone, Eq, PartialEq)]
129pub struct ExternalType {
130    pub type_name: String, // To identify the specific Rust type
131    pub number: u32,       // For type comparison
132}
133
134pub type ExternalTypeRef = Rc<ExternalType>;
135
136#[derive(Debug, Clone)]
137pub struct TypeWithMut {
138    pub resolved_type: Type,
139    pub is_mutable: bool,
140}
141
142#[derive(Debug, Clone)]
143pub struct TypeForParameter {
144    pub name: String,
145    pub resolved_type: Type,
146    pub is_mutable: bool,
147    pub node: Option<ParameterNode>,
148}
149
150impl Display for TypeForParameter {
151    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
152        write!(
153            f,
154            "{}{}: {:?}",
155            if self.is_mutable { "mut " } else { "" },
156            self.name,
157            self.resolved_type
158        )
159    }
160}
161
162impl Eq for TypeForParameter {}
163
164impl PartialEq for TypeForParameter {
165    fn eq(&self, other: &Self) -> bool {
166        let types_equal = self.resolved_type.compatible_with(&other.resolved_type);
167
168        types_equal && (self.is_mutable == other.is_mutable)
169    }
170}
171
172#[derive(Clone, Eq, PartialEq)]
173pub enum Type {
174    // Primitives
175    Int,
176    Float,
177    String,
178    Bool,
179
180    Unit,  // Empty or nothing
181    Never, // Not even empty since control flow has escaped with break or return.
182
183    // Containers
184    Array(ArrayTypeRef),
185    Tuple(TupleTypeRef),
186    Struct(StructTypeRef),
187    Map(MapTypeRef),
188
189    Enum(EnumTypeRef),
190    Generic(Box<Type>, Vec<Type>),
191
192    Function(Signature),
193    Iterable(Box<Type>),
194
195    Optional(Box<Type>),
196    External(ExternalTypeRef),
197}
198
199impl Type {
200    #[must_use]
201    pub const fn is_concrete(&self) -> bool {
202        !matches!(self, Self::Unit | Self::Never)
203    }
204}
205
206impl Debug for Type {
207    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
208        match self {
209            Self::Int => write!(f, "Int"),
210            Self::Float => write!(f, "Float"),
211            Self::String => write!(f, "String"),
212            Self::Bool => write!(f, "Bool"),
213            Self::Unit => write!(f, "Unit"),
214            Self::Never => write!(f, "!"),
215            Self::Array(array_type_ref) => write!(f, "[{:?}]", array_type_ref.item_type),
216            Self::Tuple(tuple_type_ref) => write!(f, "( {:?} )", tuple_type_ref.0),
217            Self::Struct(struct_type_ref) => {
218                write!(f, "{}", struct_type_ref.borrow().assigned_name)
219            }
220            Self::Map(map_type_ref) => write!(
221                f,
222                "[{:?}:{:?}]",
223                map_type_ref.key_type, map_type_ref.value_type
224            ),
225            Self::Generic(base, parameters) => write!(f, "{base:?}<{parameters:?}>"),
226            Self::Enum(enum_type_ref) => write!(f, "{:?}", enum_type_ref.borrow().assigned_name),
227            Self::Function(function_type_signature) => {
228                write!(f, "{:?}", function_type_signature)
229            }
230            Self::Iterable(type_generated) => write!(f, "Iterable<{type_generated:?}>"),
231            Self::Optional(base_type) => write!(f, "{base_type:?}?"),
232            Self::External(rust_type) => write!(f, "{:?}?", rust_type.type_name),
233        }
234    }
235}
236
237impl Display for Type {
238    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
239        match self {
240            Self::Int => write!(f, "Int"),
241            Self::Float => write!(f, "Float"),
242            Self::String => write!(f, "String"),
243            Self::Bool => write!(f, "Bool"),
244            Self::Unit => write!(f, "Unit"),
245            Self::Never => write!(f, "!"),
246            Self::Array(array_ref) => write!(f, "[{}]", &array_ref.item_type.to_string()),
247            Self::Tuple(tuple) => write!(f, "({})", comma(&tuple.0)),
248            Self::Struct(struct_ref) => write!(f, "{}", struct_ref.borrow().assigned_name),
249            Self::Map(map_ref) => write!(f, "[{}:{}]", map_ref.key_type, map_ref.value_type),
250            Self::Generic(base_type, params) => write!(f, "{base_type}<{}>", comma(params)),
251            Self::Enum(enum_type) => write!(f, "{}", enum_type.borrow().assigned_name),
252            Self::Function(signature) => write!(f, "function {signature}"),
253            Self::Iterable(generating_type) => write!(f, "Iterable<{generating_type}>"),
254            Self::Optional(base_type) => write!(f, "{base_type}?"),
255            Self::External(rust_type) => write!(f, "RustType {}", rust_type.type_name),
256        }
257    }
258}
259
260#[derive(Debug)]
261pub enum SemanticError {
262    CouldNotInsertStruct,
263    DuplicateTypeAlias(String),
264    CanOnlyUseStructForMemberFunctions,
265    ResolveNotStruct,
266    DuplicateStructName(String),
267    DuplicateEnumType(String),
268    DuplicateEnumVariantType(String, String),
269    DuplicateFieldName(String),
270    DuplicateExternalFunction(String),
271    DuplicateRustType(String),
272    DuplicateConstName(String),
273    CircularConstantDependency(Vec<ConstantId>),
274    DuplicateConstantId(ConstantId),
275    IncompatibleTypes,
276    WasNotImmutable,
277    WasNotMutable,
278    DuplicateSymbolName,
279    DuplicateNamespaceLink(String),
280    MismatchedTypes { expected: Type, found: Vec<Type> },
281}
282
283impl Type {
284    pub fn expect_struct_type(&self) -> Result<StructTypeRef, SemanticError> {
285        match self {
286            Type::Struct(struct_type_ref) => Ok(struct_type_ref.clone()),
287            _ => Err(SemanticError::ResolveNotStruct),
288        }
289    }
290
291    pub fn assignable_type(&self, other: &Type) -> bool {
292        if self.compatible_with(other) {
293            true
294        } else if let Self::Optional(inner_type) = self {
295            inner_type.compatible_with(other)
296        } else {
297            false
298        }
299    }
300
301    #[must_use]
302    pub fn compatible_with(&self, other: &Type) -> bool {
303        match (self, other) {
304            (Self::Function(a), Self::Function(b)) => a.same_type(b),
305            (_, Self::Never) => true,
306            (Self::Int, Self::Int) => true,
307            (Self::Float, Self::Float) => true,
308            (Self::String, Self::String) => true,
309            (Self::Bool, Self::Bool) => true,
310            (Self::Unit, Self::Unit) => true,
311            (Self::Array(_), Self::Array(_)) => true,
312            (Self::Map(a), Self::Map(b)) => {
313                a.key_type.compatible_with(&b.key_type)
314                    && a.value_type.compatible_with(&b.value_type)
315            }
316            (Self::Struct(a), Self::Struct(b)) => compare_struct_types(a, b),
317            (Self::Tuple(a), Self::Tuple(b)) => {
318                if a.0.len() != b.0.len() {
319                    return false;
320                }
321                a.0.iter()
322                    .zip(b.0.iter())
323                    .all(|(a, b)| a.compatible_with(b))
324            }
325            (Self::Enum(_), Self::Enum(_)) => true,
326            (Self::Iterable(a), Self::Iterable(b)) => a.compatible_with(b),
327            //(Self::EnumVariant(a), Self::EnumVariant(b)) => a.owner.number == b.owner.number,
328            (Self::Optional(inner_type_a), Self::Optional(inner_type_b)) => {
329                inner_type_a.compatible_with(inner_type_b)
330            }
331            (Self::External(type_ref_a), Self::External(type_ref_b)) => {
332                type_ref_a.number == type_ref_b.number
333            }
334
335            (Self::Generic(base_a, params_a), Self::Generic(base_b, params_b)) => {
336                if !base_a.compatible_with(base_b) {
337                    return false;
338                }
339
340                if params_a.len() != params_b.len() {
341                    return false;
342                }
343
344                for (param_a, param_b) in params_a.iter().zip(params_b) {
345                    if !param_a.compatible_with(param_b) {
346                        return false;
347                    }
348                }
349                true
350            }
351            _ => false,
352        }
353    }
354}
355
356fn compare_struct_types(a: &StructTypeRef, b: &StructTypeRef) -> bool {
357    let a_borrow = a.borrow();
358    let b_borrow = b.borrow();
359    if a_borrow.assigned_name != b_borrow.assigned_name {
360        return false;
361    }
362
363    if a_borrow.anon_struct_type.defined_fields.len()
364        != b_borrow.anon_struct_type.defined_fields.len()
365    {
366        return false;
367    }
368
369    for ((a_name, a_type), (b_name, b_type)) in a_borrow
370        .anon_struct_type
371        .defined_fields
372        .iter()
373        .zip(b_borrow.anon_struct_type.defined_fields.clone())
374    {
375        if *a_name != b_name {
376            return false;
377        }
378
379        if !a_type.field_type.compatible_with(&b_type.field_type) {
380            return false;
381        }
382    }
383
384    true
385}
386
387impl Node {
388    pub fn new_unknown() -> Self {
389        Self {
390            span: Span {
391                file_id: 0xffff,
392                offset: 0,
393                length: 0,
394            },
395        }
396    }
397}
398
399#[derive(Debug, Eq, PartialEq)]
400pub struct LocalIdentifier(pub Node);
401
402//#[derive(Debug)]
403pub struct InternalFunctionDefinition {
404    pub body: Expression,
405    pub name: LocalIdentifier,
406    pub signature: Signature,
407}
408
409impl Debug for InternalFunctionDefinition {
410    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
411        write!(f, "{:?}\n{:?}", self.signature, self.body)
412    }
413}
414
415impl PartialEq<Self> for InternalFunctionDefinition {
416    fn eq(&self, other: &Self) -> bool {
417        self.name == other.name
418    }
419}
420
421impl Eq for InternalFunctionDefinition {}
422
423pub type InternalFunctionDefinitionRef = Rc<InternalFunctionDefinition>;
424
425pub type ExternalFunctionId = u32;
426
427pub type ConstantId = u32;
428
429pub struct ExternalFunctionDefinition {
430    pub name: Option<Node>,
431    pub assigned_name: String,
432    pub signature: Signature,
433    pub id: ExternalFunctionId,
434}
435
436impl PartialEq<Self> for ExternalFunctionDefinition {
437    fn eq(&self, other: &Self) -> bool {
438        self.id == other.id
439    }
440}
441
442impl Eq for ExternalFunctionDefinition {}
443
444impl Debug for ExternalFunctionDefinition {
445    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
446        write!(f, "external fn")
447    }
448}
449
450pub type ExternalFunctionDefinitionRef = Rc<crate::ExternalFunctionDefinition>;
451
452#[derive(Debug)]
453pub struct Variable {
454    pub name: Node,
455    pub resolved_type: Type,
456    pub mutable_node: Option<Node>,
457
458    pub scope_index: usize,
459    pub variable_index: usize,
460}
461
462impl Variable {
463    #[must_use]
464    pub const fn is_mutable(&self) -> bool {
465        self.mutable_node.is_some()
466    }
467}
468
469pub type VariableRef = Rc<Variable>;
470
471#[derive(Debug)]
472pub struct MutVariable {
473    pub variable_ref: VariableRef,
474}
475
476//type MutVariableRef = Rc<MutVariable>;
477
478#[derive(Debug)]
479pub enum BinaryOperatorKind {
480    Add,
481    Subtract,
482    Multiply,
483    Divide,
484    Modulo,
485    LogicalOr,
486    LogicalAnd,
487    Equal,
488    NotEqual,
489    LessThan,
490    LessEqual,
491    GreaterThan,
492    GreaterEqual,
493    RangeExclusive,
494}
495
496#[derive(Debug)]
497pub struct BinaryOperator {
498    pub left: Box<Expression>,
499    pub right: Box<Expression>,
500    pub kind: BinaryOperatorKind,
501    pub node: Node,
502}
503
504#[derive(Debug)]
505pub enum UnaryOperatorKind {
506    Not,
507    Negate,
508}
509#[derive(Debug)]
510pub struct UnaryOperator {
511    pub left: Box<Expression>,
512    pub kind: UnaryOperatorKind,
513    pub node: Node,
514}
515
516#[derive()]
517pub struct InternalFunctionCall {
518    pub arguments: Vec<ArgumentExpressionOrLocation>,
519
520    pub function_definition: InternalFunctionDefinitionRef,
521    pub function_expression: Box<Expression>,
522}
523
524impl Debug for InternalFunctionCall {
525    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
526        write!(
527            f,
528            "InFuncCall({:?} {:?})",
529            self.function_expression, self.arguments
530        )
531    }
532}
533
534#[derive(Debug)]
535pub struct ExternalFunctionCall {
536    pub arguments: Vec<ArgumentExpressionOrLocation>,
537    pub function_definition: ExternalFunctionDefinitionRef,
538    pub function_expression: Box<Expression>,
539}
540
541#[must_use]
542pub fn comma_seq<K: Clone + Hash + Eq + Display, V: Display>(values: &SeqMap<K, V>) -> String {
543    let mut result = String::new();
544    for (i, (key, value)) in values.iter().enumerate() {
545        if i > 0 {
546            result.push_str(", ");
547        }
548        result.push_str(format!("{key}: {value}").as_str());
549    }
550    result
551}
552
553pub fn comma_seq_nl<K: Clone + Hash + Eq + Display, V: Display>(
554    values: &SeqMap<K, V>,
555    prefix: &str,
556) -> String {
557    let mut result = String::new();
558    for (key, value) in values.iter() {
559        result.push_str(format!("{}{}: {}\n", prefix, key, value).as_str());
560    }
561    result
562}
563
564pub fn comma_tuple_ref<K: Display, V: Display>(values: &[(&K, &V)]) -> String {
565    let mut result = String::new();
566    for (i, (key, value)) in values.iter().enumerate() {
567        if i > 0 {
568            result.push_str(", ");
569        }
570        result.push_str(format!("{}: {}", key, value).as_str());
571    }
572    result
573}
574
575#[derive(Debug)]
576pub struct MemberCall {
577    pub function: FunctionRef,
578    pub arguments: Vec<ArgumentExpressionOrLocation>,
579}
580
581#[derive(Debug, Eq, PartialEq, Clone)]
582pub struct StructTypeField {
583    pub identifier: Option<Node>,
584    pub field_type: Type,
585}
586
587impl Display for StructTypeField {
588    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
589        write!(f, "{:?}:{}", self.identifier, self.field_type)
590    }
591}
592
593#[derive(Debug)]
594pub struct MapIndexLookup {
595    pub map_type: Type,
596    pub item_type: Type,
597    pub map_type_ref: MapTypeRef,
598    pub index_expression: Box<Expression>,
599    pub map_expression: Box<Expression>,
600}
601
602#[derive(Debug)]
603pub struct ArrayItem {
604    pub item_type: Type,
605    pub int_expression: Expression,
606    pub array_expression: Expression,
607    pub array_type: Type,
608}
609
610pub type ArrayItemRef = Rc<ArrayItem>;
611
612#[derive(Debug)]
613pub enum PrecisionType {
614    Float,
615    String,
616}
617
618#[derive(Debug)]
619pub enum FormatSpecifierKind {
620    LowerHex,                            // :x
621    UpperHex,                            // :X
622    Binary,                              // :b
623    Float,                               // :f
624    Precision(u32, Node, PrecisionType), // :..2f or :..5s
625}
626
627#[derive(Debug)]
628pub struct FormatSpecifier {
629    pub node: Node,
630    pub kind: FormatSpecifierKind,
631}
632
633#[derive(Debug)]
634pub enum StringPart {
635    Literal(Node, String),
636    Interpolation(Expression, Option<FormatSpecifier>),
637}
638
639pub type FunctionRef = Rc<Function>;
640
641#[derive(Debug, Eq, PartialEq)]
642pub enum Function {
643    Internal(InternalFunctionDefinitionRef),
644    External(ExternalFunctionDefinitionRef),
645}
646
647impl Function {
648    #[must_use]
649    pub fn name(&self) -> Option<&Node> {
650        match self {
651            Self::Internal(x) => Some(&x.name.0),
652            Self::External(y) => y.name.as_ref(),
653        }
654    }
655
656    #[must_use]
657    pub fn node(&self) -> Node {
658        match self {
659            Self::Internal(x) => x.name.0.clone(),
660            Self::External(_y) => Node::new_unknown(),
661        }
662    }
663
664    #[must_use]
665    pub fn signature(&self) -> &Signature {
666        match self {
667            Self::Internal(internal) => &internal.signature,
668            Self::External(external) => &external.signature,
669        }
670    }
671}
672
673#[derive(Debug)]
674pub struct BooleanExpression {
675    #[allow(unused)]
676    pub expression: Box<Expression>,
677}
678
679#[derive(Debug)]
680pub struct Match {
681    pub arms: Vec<MatchArm>,
682    pub expression: Box<MutOrImmutableExpression>,
683}
684
685#[derive(Debug)]
686pub struct MatchArm {
687    #[allow(unused)]
688    pub pattern: Pattern,
689    pub expression: Box<Expression>,
690    pub expression_type: Type,
691}
692
693#[derive(Debug)]
694pub enum Pattern {
695    Normal(NormalPattern, Option<BooleanExpression>),
696    Wildcard(Node),
697}
698
699#[derive(Debug)]
700pub enum NormalPattern {
701    PatternList(Vec<PatternElement>),
702    EnumPattern(EnumVariantTypeRef, Option<Vec<PatternElement>>),
703    Literal(Literal),
704}
705
706#[derive(Debug)]
707pub enum PatternElement {
708    Variable(VariableRef),
709    VariableWithFieldIndex(VariableRef, usize),
710    Wildcard(Node),
711}
712
713#[derive(Debug)]
714pub struct Iterable {
715    pub key_type: Option<Type>, // It does not have to support a key type
716    pub value_type: Type,
717
718    pub resolved_expression: Box<MutOrImmutableExpression>,
719}
720
721#[derive(Debug)]
722pub struct StructInstantiation {
723    pub source_order_expressions: Vec<(usize, Expression)>,
724    pub struct_type_ref: StructTypeRef,
725}
726
727#[derive(Debug, Eq, PartialEq)]
728pub enum CompoundOperatorKind {
729    Add,
730    Sub,
731    Mul,
732    Div,
733    Modulo,
734}
735
736#[derive(Debug)]
737pub struct CompoundOperator {
738    pub node: Node,
739    pub kind: CompoundOperatorKind,
740}
741
742#[derive(Debug)]
743pub struct VariableCompoundAssignment {
744    pub variable_ref: VariableRef, // compound only support single variable
745    pub expression: Box<Expression>,
746    pub compound_operator: CompoundOperator,
747}
748
749pub fn create_rust_type(name: &str, type_number: TypeNumber) -> ExternalTypeRef {
750    let rust_type = ExternalType {
751        type_name: name.to_string(),
752        number: type_number,
753    };
754    Rc::new(rust_type)
755}
756
757#[derive(Debug)]
758pub struct Guard {
759    pub condition: Option<BooleanExpression>,
760    pub result: Expression,
761}
762
763#[derive(Debug, Clone, Eq, PartialEq)]
764pub enum RangeMode {
765    Inclusive,
766    Exclusive,
767}
768
769#[derive(Debug)]
770pub struct Postfix {
771    pub node: Node,
772    pub ty: Type,
773    pub kind: PostfixKind,
774}
775
776#[derive(Debug)]
777pub struct Range {
778    pub min: Expression,
779    pub max: Expression,
780    pub mode: RangeMode,
781}
782
783#[derive(Debug)]
784pub enum PostfixKind {
785    StructField(StructTypeRef, usize),
786    ArrayIndex(ArrayTypeRef, Expression),
787    ArrayRangeIndex(ArrayTypeRef, Range),
788    StringIndex(Expression),
789    StringRangeIndex(Range),
790    MapIndex(MapTypeRef, Expression),
791    ExternalTypeIndexRef(ExternalTypeRef, Expression),
792    MemberCall(FunctionRef, Vec<ArgumentExpressionOrLocation>),
793    FunctionCall(Vec<ArgumentExpressionOrLocation>),
794    OptionUnwrap, // ? operator
795    NoneCoalesce(Expression),
796
797    IntrinsicCallEx(IntrinsicFunction, Vec<ArgumentExpressionOrLocation>),
798    IntrinsicCall(IntrinsicFunction, Vec<Expression>),
799}
800
801#[derive(Debug)]
802pub enum LocationAccessKind {
803    FieldIndex(StructTypeRef, usize),
804    ArrayIndex(ArrayTypeRef, Expression),
805    ArrayRange(ArrayTypeRef, Range),
806    StringIndex(Expression),
807    StringRange(Range),
808    MapIndex(MapTypeRef, Expression),
809    MapIndexInsertIfNonExisting(MapTypeRef, Expression),
810    ExternalTypeIndex(ExternalTypeRef, Expression),
811}
812
813#[derive(Debug)]
814pub struct LocationAccess {
815    pub node: Node,
816    pub ty: Type,
817    pub kind: LocationAccessKind,
818}
819
820#[derive(Debug)]
821pub struct SingleLocationExpression {
822    pub kind: SingleLocationExpressionKind,
823    pub node: Node,
824    pub ty: Type,
825
826    pub starting_variable: VariableRef,
827    pub access_chain: Vec<LocationAccess>,
828}
829
830#[derive(Debug)]
831pub struct SingleMutLocationExpression(pub SingleLocationExpression);
832
833#[derive(Debug)]
834pub enum SingleLocationExpressionKind {
835    MutVariableRef,
836    MutStructFieldRef(StructTypeRef, usize),
837    MutArrayIndexRef(ArrayTypeRef),
838    MutMapIndexRef(MapTypeRef),
839    MutExternalTypeIndexRef(ExternalTypeRef),
840}
841
842#[derive(Debug)]
843pub struct SliceLocationExpression {
844    pub start: Box<Expression>,
845    pub range_start: Box<Expression>,
846    pub range_end: Box<Expression>,
847    pub mode: RangeMode,
848    pub ty: Type,
849}
850
851#[derive(Debug)]
852pub struct MutOrImmutableExpression {
853    pub expression_or_location: ArgumentExpressionOrLocation,
854    pub is_mutable: Option<Node>,
855}
856
857impl MutOrImmutableExpression {}
858
859impl MutOrImmutableExpression {
860    pub fn expect_immutable(self) -> Result<Expression, SemanticError> {
861        match self.expression_or_location {
862            ArgumentExpressionOrLocation::Expression(expr) => Ok(expr),
863            ArgumentExpressionOrLocation::Location(_) => Err(SemanticError::WasNotImmutable),
864        }
865    }
866
867    pub fn expect_immutable_ref(&self) -> Result<&Expression, SemanticError> {
868        match &self.expression_or_location {
869            ArgumentExpressionOrLocation::Expression(expr) => Ok(expr),
870            ArgumentExpressionOrLocation::Location(_) => Err(SemanticError::WasNotImmutable),
871        }
872    }
873
874    pub fn ty(&self) -> &Type {
875        match &self.expression_or_location {
876            ArgumentExpressionOrLocation::Expression(expr) => &expr.ty,
877            ArgumentExpressionOrLocation::Location(loc) => &loc.ty,
878        }
879    }
880}
881
882#[derive(Debug)]
883pub enum ArgumentExpressionOrLocation {
884    Expression(Expression),
885    Location(SingleLocationExpression),
886}
887
888#[derive()]
889pub struct Expression {
890    pub ty: Type,
891    pub node: Node,
892    pub kind: ExpressionKind,
893}
894
895impl Debug for Expression {
896    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
897        write!(f, "{:?}{},{:?}", self.node, self.ty, self.kind)
898    }
899}
900
901#[derive(Debug)]
902pub struct WhenBinding {
903    pub variable: VariableRef,
904    pub expr: MutOrImmutableExpression,
905}
906
907#[derive(Debug)]
908pub enum ExpressionKind {
909    // Access Lookup values
910    ConstantAccess(ConstantRef),
911    VariableAccess(VariableRef),
912    FieldAccess(Box<Expression>, usize),
913    ArrayAccess(
914        Box<Expression>,
915        ArrayTypeRef,
916        Box<Expression>, // int index lookup
917    ), // Read from an array: arr[3]
918    MapIndexAccess(Box<Expression>, MapTypeRef, Box<Expression>),
919    StringRangeAccess(Box<Expression>, Box<Range>),
920    ArrayRangeAccess(Box<Expression>, Box<Range>),
921
922    // ----
923    InternalFunctionAccess(InternalFunctionDefinitionRef),
924    ExternalFunctionAccess(ExternalFunctionDefinitionRef),
925
926    // Adding to a collection
927    MapAssignment(
928        Box<SingleMutLocationExpression>,
929        Box<Expression>,
930        Box<Expression>,
931    ), // Motivation: Can not use location since adding is more complex
932
933    // Operators
934    BinaryOp(BinaryOperator),
935    UnaryOp(UnaryOperator),
936    PostfixChain(Box<Expression>, Vec<Postfix>),
937
938    // Conversion
939    // the `?` operator. unwraps the value, unless it is none
940    //NoneCoalesceOperator(Box<Expression>, Box<Expression>),
941    CoerceOptionToBool(Box<Expression>),
942
943    // Calls
944
945    // For calls from returned function values
946    FunctionCall(
947        Signature,
948        Box<Expression>,
949        Vec<ArgumentExpressionOrLocation>,
950    ),
951
952    MemberCall(MemberCall),
953    InterpolatedString(Vec<StringPart>),
954
955    // Constructing
956    VariableDefinition(VariableRef, Box<MutOrImmutableExpression>), // First time assignment
957    VariableReassignment(VariableRef, Box<MutOrImmutableExpression>),
958
959    StructInstantiation(StructInstantiation),
960    Array(ArrayInstantiation),
961    Tuple(Vec<Expression>),
962    Literal(Literal),
963    Option(Option<Box<Expression>>), // Wrapping an expression in `Some()`
964    Range(Box<Expression>, Box<Expression>, RangeMode),
965
966    // Control
967    ForLoop(ForPattern, Iterable, Box<Expression>),
968    WhileLoop(BooleanExpression, Box<Expression>),
969    Return(Option<Box<Expression>>),
970    Break,
971    Continue, //
972
973    Block(Vec<Expression>),
974
975    // Match and compare
976    Match(Match),
977    Guard(Vec<Guard>),
978    If(BooleanExpression, Box<Expression>, Option<Box<Expression>>),
979
980    When(Vec<WhenBinding>, Box<Expression>, Option<Box<Expression>>),
981
982    TupleDestructuring(Vec<VariableRef>, TupleTypeRef, Box<Expression>),
983
984    Assignment(Box<SingleMutLocationExpression>, Box<Expression>),
985    AssignmentSlice(Box<SliceLocationExpression>, Box<Expression>),
986    CompoundAssignment(
987        SingleMutLocationExpression,
988        CompoundOperatorKind,
989        Box<Expression>,
990    ),
991
992    // --------------------------------------------------------------------
993    // Built In members
994    // --------------------------------------------------------------------
995    IntrinsicCallMut(
996        IntrinsicFunction,
997        SingleMutLocationExpression,
998        Vec<Expression>,
999    ),
1000
1001    // Sparse Built in
1002    SparseNew(ExternalTypeRef, Type), // item type
1003}
1004
1005#[derive(Debug)]
1006pub struct StringConst(pub Node);
1007
1008#[derive(Debug)]
1009pub enum Literal {
1010    FloatLiteral(Fp),
1011    NoneLiteral,
1012    IntLiteral(i32),
1013    StringLiteral(String),
1014    BoolLiteral(bool),
1015
1016    EnumVariantLiteral(EnumVariantTypeRef, EnumLiteralData),
1017    TupleLiteral(TupleTypeRef, Vec<Expression>),
1018    Array(ArrayTypeRef, Vec<Expression>),
1019    Map(MapTypeRef, Vec<(Expression, Expression)>),
1020}
1021
1022#[derive(Debug)]
1023pub struct ArrayInstantiation {
1024    pub expressions: Vec<Expression>,
1025    pub item_type: Type,
1026    pub array_type: Type,
1027    pub array_type_ref: ArrayTypeRef,
1028}
1029
1030#[derive(Debug)]
1031pub enum ForPattern {
1032    Single(VariableRef),
1033    Pair(VariableRef, VariableRef),
1034}
1035
1036impl ForPattern {
1037    #[must_use]
1038    pub fn is_mutable(&self) -> bool {
1039        match self {
1040            Self::Single(variable) => variable.is_mutable(),
1041            Self::Pair(a, b) => a.is_mutable() || b.is_mutable(),
1042        }
1043    }
1044}
1045
1046impl Display for ForPattern {
1047    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1048        write!(f, "resolved_for_pattern")
1049    }
1050}
1051
1052#[derive(Debug, Eq, PartialEq)]
1053pub struct ModulePathItem(pub Node);
1054
1055pub type StructTypeRef = Rc<RefCell<StructType>>;
1056
1057pub fn same_struct_ref(a: &StructTypeRef, b: &StructTypeRef) -> bool {
1058    Rc::ptr_eq(a, b)
1059}
1060
1061pub type TypeNumber = u32;
1062
1063#[derive(Debug, Clone, Eq, PartialEq)]
1064pub struct LocalTypeIdentifier(pub Node);
1065
1066#[derive(Debug)]
1067pub struct Constant {
1068    pub name: Node,
1069    pub assigned_name: String,
1070    pub id: ConstantId,
1071    pub expr: Expression,
1072    pub resolved_type: Type,
1073}
1074pub type ConstantRef = Rc<Constant>;
1075
1076#[derive(Debug)]
1077pub struct AliasType {
1078    pub name: Node,
1079    pub assigned_name: String,
1080    pub referenced_type: Type,
1081}
1082pub type AliasTypeRef = Rc<AliasType>;
1083
1084#[derive(Eq, PartialEq)]
1085pub struct StructType {
1086    pub name: Node,
1087    pub assigned_name: String,
1088    pub anon_struct_type: AnonymousStructType,
1089
1090    //
1091    pub functions: SeqMap<String, FunctionRef>,
1092}
1093
1094impl Debug for StructType {
1095    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1096        write!(f, "struct {:?}", self.assigned_name)
1097    }
1098}
1099
1100impl StructType {
1101    pub fn new(name: Node, assigned_name: &str, anon_struct_type: AnonymousStructType) -> Self {
1102        Self {
1103            //defined_in_module,
1104            anon_struct_type,
1105            name,
1106            assigned_name: assigned_name.to_string(),
1107            functions: SeqMap::default(),
1108        }
1109    }
1110
1111    pub fn field_index(&self, field_name: &str) -> Option<usize> {
1112        self.anon_struct_type
1113            .defined_fields
1114            .get_index(&field_name.to_string())
1115    }
1116
1117    pub fn name(&self) -> &Node {
1118        &self.name
1119    }
1120
1121    pub fn add_external_member_function(
1122        &mut self,
1123        external_func: ExternalFunctionDefinitionRef,
1124    ) -> Result<(), SeqMapError> {
1125        let name = external_func.assigned_name.clone();
1126        let func = Function::External(external_func);
1127        self.functions.insert(name, func.into())?;
1128        Ok(())
1129    }
1130
1131    pub fn get_member_function(&self, function_name: &str) -> Option<&FunctionRef> {
1132        self.functions.get(&function_name.to_string())
1133    }
1134
1135    pub fn get_internal_member_function(
1136        &self,
1137        function_name: &str,
1138    ) -> Option<InternalFunctionDefinitionRef> {
1139        let func = self.functions.get(&function_name.to_string())?;
1140        match &**func {
1141            Function::Internal(fn_def) => Some(fn_def.clone()),
1142            _ => None,
1143        }
1144    }
1145}
1146
1147pub type OptionTypeRef = Rc<crate::OptionType>;
1148
1149#[derive(Debug)]
1150pub struct OptionType {
1151    pub item_type: Type,
1152}
1153
1154pub type ArrayTypeRef = Rc<ArrayType>;
1155
1156pub fn same_array_ref(a: &ArrayTypeRef, b: &ArrayTypeRef) -> bool {
1157    Rc::ptr_eq(a, b)
1158}
1159
1160#[derive(Debug, Eq, PartialEq)]
1161pub struct ArrayType {
1162    pub item_type: Type,
1163}
1164
1165pub type MapTypeRef = Rc<MapType>;
1166
1167#[derive(Debug, Eq, PartialEq)]
1168pub struct MapType {
1169    pub key_type: Type,
1170    pub value_type: Type,
1171}
1172
1173pub type EnumVariantStructTypeRef = Rc<EnumVariantStructType>;
1174
1175#[derive(Clone, Eq, PartialEq)]
1176pub struct AnonymousStructType {
1177    pub defined_fields: SeqMap<String, StructTypeField>,
1178}
1179
1180impl Debug for AnonymousStructType {
1181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
1182        write!(f, "{}", comma_seq(&self.defined_fields))
1183    }
1184}
1185
1186#[derive(Debug, Eq, PartialEq)]
1187pub struct EnumVariantStructType {
1188    pub common: EnumVariantCommon,
1189    pub anon_struct: AnonymousStructType,
1190}
1191
1192pub type EnumVariantTupleTypeRef = Rc<EnumVariantTupleType>;
1193
1194#[derive(Debug, Eq, PartialEq)]
1195pub struct EnumVariantTupleType {
1196    pub common: EnumVariantCommon,
1197    pub fields_in_order: Vec<Type>,
1198}
1199
1200pub type TupleTypeRef = Rc<TupleType>;
1201
1202#[derive(Debug, Eq, PartialEq)]
1203pub struct TupleType(pub Vec<Type>);
1204
1205impl TupleType {
1206    pub fn new(types: Vec<Type>) -> Self {
1207        Self(types)
1208    }
1209}
1210
1211pub type EnumTypeRef = Rc<RefCell<EnumType>>;
1212
1213#[derive(Eq, PartialEq)]
1214pub struct EnumType {
1215    pub name: LocalTypeIdentifier,
1216    pub assigned_name: String,
1217    pub module_path: Vec<String>,
1218    pub number: TypeNumber,
1219
1220    pub variants: SeqMap<String, EnumVariantTypeRef>,
1221}
1222
1223impl Debug for EnumType {
1224    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
1225        write!(f, "{}", self.assigned_name)?;
1226        let s = comma(
1227            &self
1228                .variants
1229                .iter()
1230                .map(|(name, _variant)| name)
1231                .collect::<Vec<&String>>(),
1232        );
1233        write!(f, "{{ {s} }}")
1234    }
1235}
1236
1237impl EnumType {
1238    #[must_use]
1239    pub fn new(
1240        name: LocalTypeIdentifier,
1241        assigned_name: &str,
1242        module_path: Vec<String>,
1243        number: TypeNumber,
1244    ) -> Self {
1245        Self {
1246            name,
1247            assigned_name: assigned_name.to_string(),
1248            module_path,
1249            number,
1250            variants: SeqMap::new(),
1251        }
1252    }
1253
1254    #[must_use]
1255    pub const fn name(&self) -> &LocalTypeIdentifier {
1256        &self.name
1257    }
1258
1259    pub fn get_variant(&self, name: &str) -> Option<&EnumVariantTypeRef> {
1260        self.variants.get(&name.to_string())
1261    }
1262
1263    pub fn get_variant_from_index(&self, index: usize) -> Option<&EnumVariantTypeRef> {
1264        Some(self.variants.values().collect::<Vec<_>>()[index])
1265    }
1266}
1267
1268pub type EnumVariantTypeRef = Rc<EnumVariantType>;
1269
1270#[derive(Eq, PartialEq, Clone)]
1271pub struct EnumVariantCommon {
1272    pub name: LocalTypeIdentifier,
1273    pub assigned_name: String,
1274    pub number: TypeNumber,
1275    pub container_index: u8,
1276    pub owner: EnumTypeRef,
1277}
1278
1279impl Debug for EnumVariantCommon {
1280    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
1281        write!(
1282            f,
1283            "<{}>{}::{}",
1284            self.number,
1285            self.owner.borrow().assigned_name,
1286            self.assigned_name
1287        )
1288    }
1289}
1290
1291pub type EnumVariantStructFieldTypeRef = Rc<EnumVariantStructFieldType>;
1292
1293#[derive(Debug)]
1294pub struct EnumVariantStructFieldType {
1295    pub name: LocalIdentifier,
1296    pub enum_variant: EnumVariantTypeRef,
1297    pub resolved_type: Type,
1298
1299    pub field_index: usize,
1300}
1301
1302pub type EnumVariantTupleFieldTypeRef = Rc<EnumVariantTupleFieldType>;
1303
1304#[derive(Debug, Eq, PartialEq)]
1305pub struct EnumVariantTupleFieldType {
1306    pub name: LocalIdentifier,
1307    pub enum_variant: EnumVariantTypeRef,
1308    pub resolved_type: Type,
1309
1310    pub field_index: usize,
1311}
1312
1313#[derive(Debug, Clone, Eq, PartialEq)]
1314pub struct EnumVariantSimpleType {
1315    pub common: EnumVariantCommon,
1316}
1317
1318pub type EnumVariantSimpleTypeRef = Rc<EnumVariantSimpleType>;
1319
1320#[derive(Clone, Eq, PartialEq)]
1321pub enum EnumVariantType {
1322    Struct(EnumVariantStructTypeRef),
1323    Tuple(EnumVariantTupleTypeRef),
1324    Nothing(EnumVariantSimpleTypeRef),
1325}
1326impl EnumVariantType {
1327    pub fn common(&self) -> &EnumVariantCommon {
1328        match self {
1329            Self::Tuple(tuple) => &tuple.common,
1330            Self::Struct(c) => &c.common,
1331            Self::Nothing(c) => &c.common,
1332        }
1333    }
1334}
1335
1336impl Debug for EnumVariantType {
1337    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
1338        match self {
1339            Self::Struct(x) => write!(f, "{{ {x:?} }}"),
1340            Self::Tuple(x) => write!(f, "({x:?})"),
1341            Self::Nothing(_x) => Ok(()),
1342        }
1343    }
1344}
1345
1346#[derive(Debug)]
1347pub struct ImplMember {}
1348
1349#[derive(Debug)]
1350pub enum UseItem {
1351    Identifier(Node),
1352    TypeIdentifier(Node),
1353}
1354
1355#[derive(Debug)]
1356pub struct Use {
1357    pub path: Vec<Node>,
1358    pub items: Vec<UseItem>,
1359}
1360
1361// Mutable part
1362#[derive(Debug)]
1363pub struct ProgramState {
1364    pub array_types: Vec<ArrayTypeRef>,
1365    pub number: TypeNumber,
1366    pub external_function_number: ExternalFunctionId,
1367    // It is just so we don't have to do another dendency check of the
1368    // modules, we know that these constants have been
1369    // evaluated in order already
1370    pub constants_in_dependency_order: Vec<ConstantRef>,
1371}
1372
1373impl ProgramState {
1374    pub fn new() -> Self {
1375        Self {
1376            array_types: Vec::new(),
1377            number: 0,
1378            external_function_number: 0,
1379            constants_in_dependency_order: Vec::new(),
1380        }
1381    }
1382
1383    pub fn allocate_number(&mut self) -> TypeNumber {
1384        self.number += 1;
1385        self.number
1386    }
1387
1388    pub fn allocate_external_function_id(&mut self) -> ExternalFunctionId {
1389        self.external_function_number += 1;
1390        self.external_function_number
1391    }
1392}
1393
1394#[derive()]
1395pub enum EnumLiteralData {
1396    Nothing,
1397    Tuple(Vec<Expression>),
1398    Struct(Vec<(usize, Expression)>),
1399}
1400
1401impl Debug for EnumLiteralData {
1402    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
1403        match self {
1404            Self::Nothing => Ok(()),
1405            Self::Tuple(x) => write!(f, "{x:?}"),
1406            Self::Struct(s) => write!(f, "{s:?}"),
1407        }
1408    }
1409}