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
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
use std::{
    collections::BTreeMap,
    fmt::{Display, Error, Formatter},
    fs::read_to_string,
    path::PathBuf,
    process::exit,
};

use crate::{
    hir::{
        HirConstant, HirDeclaration, HirExpression, HirFunction, HirProgram, HirStatement,
        HirStructure, HirType,
    },
    parse, Identifier, StringLiteral, Target,
};

#[derive(Clone, Debug)]
pub enum TirError {
    /// A copy method must have a very specific type signature:
    /// fn copy(self: &T) -> T
    /// This is so that the compiler can properly place
    /// copy and drop method calls for automatic memory management.
    InvalidCopyTypeSignature(Identifier),
    /// A drop method must have a very specific type signature:
    /// fn drop(self: &T) -> void
    /// This is so that the compiler can properly place
    /// copy and drop method calls for automatic memory management.
    InvalidDropTypeSignature(Identifier),
    /// Does a structure use a member with an undefined type?
    /// If so, then this error is thrown.
    StructureNotDefined(Identifier),
    /// The user may NOT call the `.copy()` method explicitly
    /// The compiler is only allowed to call this method.
    /// This is to prevent memory leaks.
    ExplicitCopy,
}

impl Display for TirError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        match self {
            Self::InvalidCopyTypeSignature(type_name) => write!(
                f,
                "invalid copy constructor type signature for type '{}'",
                type_name
            ),
            Self::InvalidDropTypeSignature(type_name) => write!(
                f,
                "invalid drop destructor type signature for type '{}'",
                type_name
            ),
            Self::StructureNotDefined(type_name) => {
                write!(f, "type '{}' is not defined", type_name)
            }
            Self::ExplicitCopy => write!(f, "cannot explicitly call copy constructors"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct TirProgram(Vec<TirDeclaration>, i32);

impl TirProgram {
    pub fn new(decls: Vec<TirDeclaration>, memory_size: i32) -> Self {
        Self(decls, memory_size)
    }

    pub fn get_declarations(&mut self) -> &mut Vec<TirDeclaration> { &mut self.0 }

    /// Add a prefix to every include statement in this program.
    /// This is used to include files in other directories.
    pub fn set_include_dir(&mut self, include_dir: &PathBuf) -> &mut Self {
        for decl in self.get_declarations() {
            if let TirDeclaration::Include(filename) = decl {
                // Join the include directive argument with the include directory
                let new_path = include_dir.join(filename.clone());
                // Replace the directive's argument with the new path
                *filename = new_path.to_str().unwrap().to_string()
            }
        }
        self
    }

    pub fn compile(&mut self, cwd: &PathBuf) -> Result<HirProgram, TirError> {
        let mut hir_decls = vec![];

        for (i, decl) in self.get_declarations().iter().enumerate() {
            if let TirDeclaration::Include(filename) = decl {
                // This takes the path of the file in the `include` flag
                // and appends it to the directory of the file which is
                // including it.
                //
                // So, if `src/main.ok` includes "lib/all.ok",
                // `file_path` will be equal to "src/lib/all.ok"
                let file_path = cwd.join(filename.clone());
                if let Ok(contents) = read_to_string(file_path.clone()) {
                    // Get the directory of the included file.

                    // If `src/main.ok` includes "lib/all.ok",
                    // `include_path` will be equal to "src/lib/"
                    let include_path = if let Some(dir) = file_path.parent() {
                        PathBuf::from(dir)
                    } else {
                        PathBuf::from("./")
                    };

                    // Remove the include directive so it does not get computed again
                    self.get_declarations().remove(i);
                    
                    // Add the contents of the included file to this file
                    self.get_declarations().extend(
                        parse(contents)
                            // The included file might be in a different folder.
                            // So, compile the included file with the file's folder
                            // as the working directory.
                            .set_include_dir(&match include_path.strip_prefix(cwd) {
                                Ok(path) => path.to_path_buf(),
                                Err(_) => include_path
                            })
                            .get_declarations()
                            .clone()
                    );

                    // Use recursion to deal with new include directives
                    return self.compile(cwd)
                } else {
                    eprintln!("error: could not include file '{:?}'", file_path);
                    exit(1);
                }
            }
        }

        for decl in &self.0 {
            hir_decls.push(decl.to_hir_decl(cwd, &self.0)?);
        }

        Ok(HirProgram::new(hir_decls, self.1))
    }
}

/// This is purely a standin for HIR's declaration
/// type. However, if a `macro` flag is added, it
/// should be added here.
#[derive(Clone, Debug)]
pub enum TirDeclaration {
    DocumentHeader(String),
    Constant(Option<String>, Identifier, TirConstant),
    Function(TirFunction),
    Structure(TirStructure),
    Assert(TirConstant),
    If(TirConstant, TirProgram),
    IfElse(TirConstant, TirProgram, TirProgram),
    Error(String),
    Extern(String),
    /// This is the only flag that is computed in TIR. This
    /// copies and pastes another Oak file in place of this declaration.
    Include(String),
    Memory(i32),
    RequireStd,
    NoStd,
}

impl TirDeclaration {
    fn to_hir_decl(&self, cwd: &PathBuf, decls: &Vec<TirDeclaration>) -> Result<HirDeclaration, TirError> {
        Ok(match self {
            Self::DocumentHeader(header) => HirDeclaration::DocumentHeader(header.clone()),
            Self::Constant(doc, name, constant) => {
                HirDeclaration::Constant(doc.clone(), name.clone(), constant.to_hir_const(decls)?)
            }
            Self::Function(func) => HirDeclaration::Function(func.to_hir_fn(decls)?),
            Self::Structure(structure) => {
                HirDeclaration::Structure(structure.clone().to_hir_struct(decls)?)
            }

            Self::Assert(constant) => HirDeclaration::Assert(constant.to_hir_const(decls)?),

            Self::Error(msg) => HirDeclaration::Error(msg.clone()),

            Self::Extern(file) => HirDeclaration::Extern(file.clone()),

            /// In HIR, do nothing in place of an include statement
            Self::Include(file) => HirDeclaration::Pass,

            Self::Memory(n) => HirDeclaration::Memory(*n),

            Self::RequireStd => HirDeclaration::RequireStd,
            Self::NoStd => HirDeclaration::NoStd,

            Self::If(constant, program) => {
                HirDeclaration::If(constant.to_hir_const(decls)?, program.clone().compile(cwd)?)
            }

            Self::IfElse(constant, then_prog, else_prog) => HirDeclaration::IfElse(
                constant.to_hir_const(decls)?,
                then_prog.clone().compile(cwd)?,
                else_prog.clone().compile(cwd)?,
            ),
        })
    }
}

/// This enum represents a type name in an expression.
/// Take for example the declaration `fn test(x: num) -> &void`.
/// `num` and `&void` are both `TirType` instances.
#[derive(Clone, Debug, PartialEq)]
pub enum TirType {
    Pointer(Box<Self>),
    Void,
    Float,
    Boolean,
    Character,
    Structure(Identifier),
}

impl TirType {
    /// Is this type a structure?
    fn is_structure(&self) -> bool {
        match self {
            Self::Structure(_) => true,
            _ => false,
        }
    }

    /// Can this type be moved without making a new copy?
    fn is_movable(&self, decls: &Vec<TirDeclaration>) -> Result<bool, TirError> {
        if let Self::Structure(name) = self {
            for decl in decls {
                if let TirDeclaration::Structure(structure) = decl {
                    // Find the structure with this type's name,
                    // and return if it is movable
                    if name == structure.get_name() {
                        return Ok(structure.is_movable(decls)?);
                    }
                }
            }
            // If the structure is not defined, then this type is not defined
            return Err(TirError::StructureNotDefined(name.clone()));
        } else {
            // If this type is not a structure,
            // it is movable.
            return Ok(true);
        }
    }

    /// Add a reference to this type
    fn refer(&self) -> Self {
        Self::Pointer(Box::new(self.clone()))
    }

    /// Convert this type to an HIR type
    fn to_hir_type(&self) -> Result<HirType, TirError> {
        Ok(match self {
            Self::Pointer(inner) => HirType::Pointer(Box::new(inner.to_hir_type()?)),
            Self::Void => HirType::Void,
            Self::Float => HirType::Float,
            Self::Boolean => HirType::Boolean,
            Self::Character => HirType::Character,
            Self::Structure(name) => HirType::Structure(name.clone()),
        })
    }
}

/// The type that represents a function definition.
#[derive(Clone, Debug)]
pub struct TirFunction {
    /// The function's optional docstring
    doc: Option<String>,
    /// The function's name
    name: Identifier,
    /// The function's parameters
    args: Vec<(Identifier, TirType)>,
    /// The function's return type
    return_type: TirType,
    /// The function's body statements
    body: Vec<TirStatement>,
}

impl TirFunction {
    pub fn new(
        doc: Option<String>,
        name: Identifier,
        args: Vec<(Identifier, TirType)>,
        return_type: TirType,
        body: Vec<TirStatement>,
    ) -> Self {
        Self {
            doc,
            name,
            args,
            return_type,
            body,
        }
    }

    /// A structure in Oak is actually syntactic
    /// sugar for a method. Take for example the
    /// following structure definition:
    /// ```
    /// struct Date {
    ///     let month: num,
    ///         day: num,
    ///         year: num;
    /// }
    /// ```
    /// This structure gets converted to the following HIR structure
    /// ```
    /// struct Date(sizeof(num) + sizeof(num) + sizeof(num)) {
    ///     fn month(self: &Date) -> &num { return self as &num}
    ///     fn day(self: &Date) -> &num { return (self + sizeof(num)) as &num}
    ///     fn year(self: &Date) -> &num { return (self + sizeof(num) + sizeof(num)) as &num}
    /// }
    /// ```
    fn member_method(
        // The type of the structure the method is being defined for
        self_type: &Identifier,
        // The list of members that came before this member. This is
        // to determine the offset of the member in the structure's memory.
        previous_member_types: &Vec<TirType>,
        // The name of this member
        member_name: &Identifier,
        // This member's type
        member_type: &TirType,
    ) -> Self {
        // Add the size of all the previous members to the self pointer
        // to get the address of this member.
        let mut fn_return = TirExpression::Variable(Identifier::from("self"));
        for t in previous_member_types {
            fn_return = TirExpression::Add(
                Box::new(fn_return.clone()),
                Box::new(TirExpression::SizeOf(t.clone())),
            );
        }

        Self::new(
            None,
            member_name.clone(),
            vec![(
                Identifier::from("self"),
                TirType::Pointer(Box::new(TirType::Structure(self_type.clone()))),
            )],
            member_type.refer().clone(),
            // Then, typecast the address of the member as the member's type.
            vec![TirStatement::Return(vec![TirExpression::TypeCast(
                Box::new(fn_return),
                member_type.refer().clone(),
            )])],
        )
    }

    /// Generate a copy constructor for a type.
    fn copy_constructor(members: &Vec<(Identifier, TirType)>, structure: &Identifier) -> Self {
        let struct_t = TirType::Structure(structure.clone());
        let mut result = vec![];

        if members.len() == 1 {
            // If the number of members is one, then
            // the returned value NEEDS to be cast to pass MIR typechecks.
            let member_name = members[0].0.clone();

            // This generates the following code:
            // ```
            // return (*self) as T
            // ```
            result = vec![TirExpression::TypeCast(
                Box::new(TirExpression::Deref(Box::new(TirExpression::Method(
                    Box::new(TirExpression::Variable(Identifier::from("self"))),
                    member_name,
                    vec![],
                )))),
                TirType::Structure(structure.clone()),
            )]
        } else {
            // If the number of members greater than one, then
            // the typechecks will pass without casting any members.

            // This generates the following code:
            // ```
            // return [self->member_1, self->member_2, ...];
            // ```
            for (member, _) in members {
                result.push(TirExpression::Deref(Box::new(TirExpression::Method(
                    Box::new(TirExpression::Variable(Identifier::from("self"))),
                    member.clone(),
                    vec![],
                ))))
            }
        }

        // fn copy(self: &T) -> T { ... }
        Self::new(
            None,
            Identifier::from("copy"),
            vec![(Identifier::from("self"), struct_t.refer())],
            struct_t,
            vec![TirStatement::Return(result)],
        )
    }

    /// Generate a drop destructor for a type
    fn drop_destructor(members: &Vec<(Identifier, TirType)>, structure: &Identifier) -> Self {
        // Convert a structure to its TIR type
        let struct_t = TirType::Structure(structure.clone());
        let mut result = vec![];
        for (member, t) in members {
            // If the type of the member is a structure, call its drop method.
            // If the object is a pointer or is primitive, then the drop method
            // must not be called.
            if t.is_structure() {
                // Generate `self->member.drop();`
                result.push(TirStatement::Expression(TirExpression::Method(
                    Box::new(TirExpression::Method(
                        Box::new(TirExpression::Variable(Identifier::from("self"))),
                        member.clone(),
                        vec![],
                    )),
                    Identifier::from("drop"),
                    vec![],
                )))
            }
        }

        Self::new(
            None,
            Identifier::from("drop"),
            vec![(Identifier::from("self"), struct_t.refer())],
            TirType::Void,
            result,
        )
    }

    /// Is the type signature of this function a valid copy constructor for a given type?
    fn is_valid_copy(&self, structure: &Identifier) -> Result<bool, TirError> {
        // The method name must be `copy`
        if &self.name == "copy" {
            let struct_t = TirType::Structure(structure.clone());
            // If the number of parameters is one,
            // and the parameter type is &T,
            // and the result is T, then the type signature is good!
            if self.args.len() == 1
                && self.args[0].1 == struct_t.refer()
                && self.return_type == struct_t
            {
                return Ok(true);
            } else {
                // Otherwise, throw an error about the copy constructors type signature
                return Err(TirError::InvalidCopyTypeSignature(structure.clone()));
            }
        }
        return Ok(false);
    }

    /// Is the type signature of this function a valid drop destructor for a given type?
    fn is_valid_drop(&self, structure: &Identifier) -> Result<bool, TirError> {
        // The method name must be `drop`
        if &self.name == "drop" {
            let struct_t = TirType::Structure(structure.clone());
            // If the number of parameters is one,
            // and the parameter type is &T,
            // and the result is void, then the type signature is good!
            if self.args.len() == 1
                && self.args[0].1 == struct_t.refer()
                && self.return_type == TirType::Void
            {
                return Ok(true);
            } else {
                // Otherwise, throw an error about the drop destructors type signature
                return Err(TirError::InvalidDropTypeSignature(structure.clone()));
            }
        }
        return Ok(false);
    }

    /// Convert this function into an HIR function
    fn to_hir_fn(&self, decls: &Vec<TirDeclaration>) -> Result<HirFunction, TirError> {
        // Convert the parameter types to HIR types
        let mut args = vec![];
        for (arg, t) in &self.args {
            args.push((arg.clone(), t.to_hir_type()?))
        }

        // Convert the function statements to HIR statements
        let mut body = vec![];
        for stmt in &self.body {
            body.push(stmt.to_hir_stmt(decls)?)
        }

        Ok(HirFunction::new(
            self.doc.clone(),
            self.name.clone(),
            args,
            self.return_type.to_hir_type()?,
            body,
        ))
    }
}

/// The type that represents a structure definition.
#[derive(Clone, Debug)]
pub struct TirStructure {
    /// The optional docstring for the structure
    doc: Option<String>,
    /// The name of the structure
    name: Identifier,
    /// The structure's members
    members: Vec<(Identifier, TirType)>,
    /// The structure's methods
    methods: Vec<TirFunction>,
}

impl TirStructure {
    pub fn new(
        doc: Option<String>,
        name: Identifier,
        members: Vec<(Identifier, TirType)>,
        methods: Vec<TirFunction>,
    ) -> Self {
        Self {
            doc,
            name,
            members,
            methods,
        }
    }

    /// Get the name of the structure
    fn get_name(&self) -> &Identifier {
        &self.name
    }

    /// Can this type be moved without making a new copy?
    fn is_movable(&self, decls: &Vec<TirDeclaration>) -> Result<bool, TirError> {
        /// Does this type manually implement copy and drop?
        let mut default_copy = true;
        let mut default_drop = true;
        for method in &self.methods {
            // If the method is a copy constructor, mark `default_copy` as false
            if method.is_valid_copy(&self.name)? {
                default_copy = false;
            }

            // If the method is a drop destructor, mark `default_drop` as false
            if method.is_valid_drop(&self.name)? {
                default_drop = false;
            }
        }

        for (_, t) in &self.members {
            // If any of the structure's members are not movable,
            // then this structure cannot be movable.
            if !t.is_movable(decls)? {
                return Ok(false);
            }
        }
        // If either a `copy` or `drop` is implemented manually,
        // then the object cannot be movable.
        Ok(default_copy && default_drop)
    }

    fn to_hir_struct(&mut self, decls: &Vec<TirDeclaration>) -> Result<HirStructure, TirError> {
        // Check if the structure is movable BEFORE the copy
        // and drop functions are automatically added. If the
        // copy and drop methods are added before the movability is checked,
        // then `is_movable` will automatically be false.
        let is_movable = self.is_movable(decls)?;
        // Add the object's `copy` and `drop` methods.
        self.add_copy_and_drop()?;

        // Create the list of methods for the new HIR structure
        let mut methods = vec![];

        // Store all the previous member's types for each member
        // to create a getter/setter method for each member.
        let mut previous_member_types = vec![];

        // Keep track of the size of the structure
        let mut size = HirConstant::Float(0.0);

        for (name, t) in &self.members {
            // Add the member function to the list of methods
            methods.push(
                TirFunction::member_method(&self.name, &previous_member_types, name, t)
                    .to_hir_fn(decls)?,
            );
            // Add the size of the member to the size of the structure
            size = HirConstant::Add(
                Box::new(size.clone()),
                Box::new(HirConstant::SizeOf(t.to_hir_type()?)),
            );
            // Add this member's type to the list of
            // previous member's types.
            previous_member_types.push(t.clone())
        }

        // In addition to the member methods,
        // add each of the structures explicit methods
        for method in &self.methods {
            methods.push(method.to_hir_fn(decls)?)
        }

        Ok(HirStructure::new(
            self.doc.clone(),
            self.name.clone(),
            size,
            methods,
            is_movable,
        ))
    }

    /// Add the default copy and drop methods to this structure
    fn add_copy_and_drop(&mut self) -> Result<(), TirError> {
        // To prevent multiple method definitions,
        // determine whether or not the copy and
        // drop methods have already been defined.
        let mut has_copy = false;
        let mut has_drop = false;
        for method in &self.methods {
            if method.is_valid_copy(&self.name)? {
                has_copy = true;
            } else if method.is_valid_drop(&self.name)? {
                has_drop = true;
            }
        }

        // If the structure does not have a copy method,
        // add a default copy constructor to the list of methods.
        if !has_copy {
            self.methods
                .push(TirFunction::copy_constructor(&self.members, &self.name));
        }

        // If the structure does not have a drop method,
        // add a default drop destructor to the list of methods.
        if !has_drop {
            self.methods
                .push(TirFunction::drop_destructor(&self.members, &self.name));
        }

        Ok(())
    }
}

/// The type that represents a constant TIR  expression.
#[derive(Clone, Debug)]
pub enum TirConstant {
    /// A float constant
    Float(f64),
    /// A character constant
    Character(char),
    /// True constant
    True,
    /// False constant
    False,

    /// Add two constants
    Add(Box<Self>, Box<Self>),
    /// Subtract two constants
    Subtract(Box<Self>, Box<Self>),
    /// Multiply two constants
    Multiply(Box<Self>, Box<Self>),
    /// Divide two constants
    Divide(Box<Self>, Box<Self>),

    /// And two constants
    And(Box<Self>, Box<Self>),
    /// Or two constants
    Or(Box<Self>, Box<Self>),
    /// Not a constant
    Not(Box<Self>),

    /// Compare two constants with the `>` operator
    Greater(Box<Self>, Box<Self>),
    /// Compare two constants with the `<` operator
    Less(Box<Self>, Box<Self>),
    /// Compare two constants with the `>=` operator
    GreaterEqual(Box<Self>, Box<Self>),
    /// Compare two constants with the `<=` operator
    LessEqual(Box<Self>, Box<Self>),
    /// Compare two constants with the `==` operator
    Equal(Box<Self>, Box<Self>),
    /// Compare two constants with the `!=` operator
    NotEqual(Box<Self>, Box<Self>),

    /// Get a constant by its name
    Constant(Identifier),
    /// Is a constant defined?
    IsDefined(String),
    /// Is a type movable?
    IsMovable(TirType),
    /// What's the size of a type?
    SizeOf(TirType),
    /// A constant expression that is contingent on another constant expression
    Conditional(Box<Self>, Box<Self>, Box<Self>)
}

impl TirConstant {
    pub fn to_hir_const(&self, decls: &Vec<TirDeclaration>) -> Result<HirConstant, TirError> {
        Ok(match self {
            Self::Conditional(cond, then, otherwise) => HirConstant::Conditional(
                Box::new(cond.to_hir_const(decls)?),
                Box::new(then.to_hir_const(decls)?),
                Box::new(otherwise.to_hir_const(decls)?),
            ),

            Self::Float(n) => HirConstant::Float(*n),
            Self::Character(ch) => HirConstant::Character(*ch),
            Self::True => HirConstant::True,
            Self::False => HirConstant::False,

            Self::Add(lhs, rhs) => HirConstant::Add(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Subtract(lhs, rhs) => HirConstant::Subtract(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Multiply(lhs, rhs) => HirConstant::Multiply(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Divide(lhs, rhs) => HirConstant::Divide(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),

            Self::Greater(lhs, rhs) => HirConstant::Greater(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Less(lhs, rhs) => HirConstant::Less(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::GreaterEqual(lhs, rhs) => HirConstant::GreaterEqual(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::LessEqual(lhs, rhs) => HirConstant::LessEqual(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Equal(lhs, rhs) => HirConstant::Equal(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::NotEqual(lhs, rhs) => HirConstant::NotEqual(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),

            Self::Constant(name) => HirConstant::Constant(name.clone()),
            Self::IsDefined(name) => HirConstant::IsDefined(name.clone()),
            Self::IsMovable(t) => HirConstant::Float(t.is_movable(decls)? as i32 as f64),
            Self::SizeOf(t) => HirConstant::SizeOf(t.to_hir_type()?),

            Self::And(lhs, rhs) => HirConstant::And(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Or(lhs, rhs) => HirConstant::Or(
                Box::new(lhs.to_hir_const(decls)?),
                Box::new(rhs.to_hir_const(decls)?),
            ),
            Self::Not(expr) => HirConstant::Not(Box::new(expr.to_hir_const(decls)?)),
        })
    }
}

#[derive(Clone, Debug)]
pub enum TirStatement {
    /// An HIR let expression with a manually assigned type
    Define(Identifier, TirType, TirExpression),
    /// An HIR let expression with an automatically assigned type
    AutoDefine(Identifier, TirExpression),
    /// A variable assignment
    AssignVariable(Identifier, TirExpression),
    /// Add to a variable
    AddAssignVariable(Identifier, TirExpression),
    /// Subtract from a variable
    SubtractAssignVariable(Identifier, TirExpression),
    /// Multiply to a variable
    MultiplyAssignVariable(Identifier, TirExpression),
    /// Divide from a variable
    DivideAssignVariable(Identifier, TirExpression),
    /// An assignment to a dereferenced address
    AssignAddress(TirExpression, TirExpression),
    /// Add to the value a pointer points to
    AddAssignAddress(TirExpression, TirExpression),
    /// Subtract from the value a pointer points to
    SubtractAssignAddress(TirExpression, TirExpression),
    /// Multiply the value a pointer points to
    MultiplyAssignAddress(TirExpression, TirExpression),
    /// Divide the value a pointer points to
    DivideAssignAddress(TirExpression, TirExpression),

    /// An HIR for loop `for (let i=0; i<10; i=i+1) {...}`
    For(Box<Self>, TirExpression, Box<Self>, Vec<Self>),
    /// An HIR for loop `for i in 0..10 {...}`
    ForRange(Identifier, TirExpression, TirExpression, Vec<Self>),

    /// An HIR while loop
    While(TirExpression, Vec<Self>),
    /// An HIR if statement
    If(TirExpression, Vec<Self>),
    /// An HIR if statement with an else clause
    IfElse(TirExpression, Vec<Self>, Vec<Self>),
    /// An HIR if statement with an else clause
    IfElifElse(
        TirExpression,
        Vec<Self>,
        Vec<(TirExpression, Vec<Self>)>,
        Vec<Self>,
    ),

    /// An HIR free statement to deallocate memory
    Free(TirExpression, TirExpression),
    /// Return one or more values at the end of a function
    Return(Vec<TirExpression>),

    /// Any expression
    Expression(TirExpression),
}

impl TirStatement {
    fn to_hir_stmt(&self, decls: &Vec<TirDeclaration>) -> Result<HirStatement, TirError> {
        Ok(match self {
            Self::Define(name, t, expr) => {
                HirStatement::Define(name.clone(), t.to_hir_type()?, expr.to_hir_expr(decls)?)
            }
            Self::AutoDefine(name, expr) => {
                HirStatement::AutoDefine(name.clone(), expr.to_hir_expr(decls)?)
            }
            Self::AssignVariable(name, expr) => {
                HirStatement::AssignVariable(name.clone(), expr.to_hir_expr(decls)?)
            }
            Self::AddAssignVariable(name, expr) => HirStatement::AssignVariable(
                name.clone(),
                HirExpression::Add(
                    Box::new(HirExpression::Variable(name.clone())),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::SubtractAssignVariable(name, expr) => HirStatement::AssignVariable(
                name.clone(),
                HirExpression::Subtract(
                    Box::new(HirExpression::Variable(name.clone())),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::MultiplyAssignVariable(name, expr) => HirStatement::AssignVariable(
                name.clone(),
                HirExpression::Multiply(
                    Box::new(HirExpression::Variable(name.clone())),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::DivideAssignVariable(name, expr) => HirStatement::AssignVariable(
                name.clone(),
                HirExpression::Divide(
                    Box::new(HirExpression::Variable(name.clone())),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::AssignAddress(addr, expr) => {
                HirStatement::AssignAddress(addr.to_hir_expr(decls)?, expr.to_hir_expr(decls)?)
            }
            Self::AddAssignAddress(addr, expr) => HirStatement::AssignAddress(
                addr.to_hir_expr(decls)?,
                HirExpression::Add(
                    Box::new(HirExpression::Deref(Box::new(addr.to_hir_expr(decls)?))),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::SubtractAssignAddress(addr, expr) => HirStatement::AssignAddress(
                addr.to_hir_expr(decls)?,
                HirExpression::Subtract(
                    Box::new(HirExpression::Deref(Box::new(addr.to_hir_expr(decls)?))),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::MultiplyAssignAddress(addr, expr) => HirStatement::AssignAddress(
                addr.to_hir_expr(decls)?,
                HirExpression::Multiply(
                    Box::new(HirExpression::Deref(Box::new(addr.to_hir_expr(decls)?))),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),
            Self::DivideAssignAddress(addr, expr) => HirStatement::AssignAddress(
                addr.to_hir_expr(decls)?,
                HirExpression::Divide(
                    Box::new(HirExpression::Deref(Box::new(addr.to_hir_expr(decls)?))),
                    Box::new(expr.to_hir_expr(decls)?),
                ),
            ),

            Self::For(pre, cond, post, body) => HirStatement::For(
                Box::new(pre.to_hir_stmt(decls)?),
                cond.to_hir_expr(decls)?,
                Box::new(post.to_hir_stmt(decls)?),
                {
                    let mut result = vec![];
                    for stmt in body {
                        result.push(stmt.to_hir_stmt(decls)?)
                    }
                    result
                },
            ),

            Self::ForRange(var, from, to, body) => HirStatement::For(
                Box::new(HirStatement::Define(
                    var.clone(),
                    HirType::Float,
                    from.to_hir_expr(decls)?,
                )),
                HirExpression::Less(
                    Box::new(HirExpression::Variable(var.clone())),
                    Box::new(to.to_hir_expr(decls)?),
                ),
                Box::new(HirStatement::AssignVariable(
                    var.clone(),
                    HirExpression::Add(
                        Box::new(HirExpression::Variable(var.clone())),
                        Box::new(HirExpression::Constant(HirConstant::Float(1.0))),
                    ),
                )),
                {
                    let mut result = vec![];
                    for stmt in body {
                        result.push(stmt.to_hir_stmt(decls)?)
                    }
                    result
                },
            ),

            Self::While(cond, body) => HirStatement::While(cond.to_hir_expr(decls)?, {
                let mut result = vec![];
                for stmt in body {
                    result.push(stmt.to_hir_stmt(decls)?)
                }
                result
            }),

            Self::If(cond, body) => HirStatement::If(cond.to_hir_expr(decls)?, {
                let mut result = vec![];
                for stmt in body {
                    result.push(stmt.to_hir_stmt(decls)?)
                }
                result
            }),

            Self::IfElse(cond, then_body, else_body) => HirStatement::IfElse(
                cond.to_hir_expr(decls)?,
                {
                    let mut result = vec![];
                    for stmt in then_body {
                        result.push(stmt.to_hir_stmt(decls)?)
                    }
                    result
                },
                {
                    let mut result = vec![];
                    for stmt in else_body {
                        result.push(stmt.to_hir_stmt(decls)?)
                    }
                    result
                },
            ),

            Self::IfElifElse(cond, then_body, elifs, else_body) => {
                let mut else_branch = else_body.clone();
                for (elif_cond, elif_body) in elifs {
                    else_branch = vec![Self::IfElse(
                        elif_cond.clone(),
                        elif_body.clone(),
                        else_branch.clone(),
                    )];
                }
                Self::IfElse(cond.clone(), then_body.clone(), else_branch).to_hir_stmt(decls)?
            }

            Self::Free(addr, size) => {
                HirStatement::Free(addr.to_hir_expr(decls)?, size.to_hir_expr(decls)?)
            }
            Self::Return(exprs) => HirStatement::Return({
                let mut result = vec![];
                for expr in exprs {
                    result.push(expr.to_hir_expr(decls)?)
                }
                result
            }),

            Self::Expression(expr) => HirStatement::Expression(expr.to_hir_expr(decls)?),
        })
    }
}

#[derive(Clone, Debug)]
pub enum TirExpression {
    IsMovable(TirType),
    SizeOf(TirType),
    Constant(TirConstant),
    Move(Box<Self>),

    Add(Box<Self>, Box<Self>),
    Subtract(Box<Self>, Box<Self>),
    Multiply(Box<Self>, Box<Self>),
    Divide(Box<Self>, Box<Self>),

    Not(Box<Self>),
    And(Box<Self>, Box<Self>),
    Or(Box<Self>, Box<Self>),

    Greater(Box<Self>, Box<Self>),
    Less(Box<Self>, Box<Self>),
    GreaterEqual(Box<Self>, Box<Self>),
    LessEqual(Box<Self>, Box<Self>),
    Equal(Box<Self>, Box<Self>),
    NotEqual(Box<Self>, Box<Self>),

    Refer(Identifier),
    Deref(Box<Self>),

    Void,
    True,
    False,
    Character(char),
    String(StringLiteral),
    Variable(Identifier),

    TypeCast(Box<Self>, TirType),
    Alloc(Box<Self>),

    Call(Identifier, Vec<Self>),
    ForeignCall(Identifier, Vec<Self>),
    Method(Box<Self>, Identifier, Vec<Self>),
    Index(Box<Self>, Box<Self>),
    Conditional(Box<Self>, Box<Self>, Box<Self>),
}

impl TirExpression {
    pub fn to_hir_expr(&self, decls: &Vec<TirDeclaration>) -> Result<HirExpression, TirError> {
        Ok(match self {
            Self::IsMovable(t) => {
                if t.is_movable(decls)? {
                    HirExpression::True
                } else {
                    HirExpression::False
                }
            }
            Self::Void => HirExpression::Void,
            Self::True => HirExpression::True,
            Self::False => HirExpression::False,
            Self::Character(ch) => HirExpression::Character(*ch),
            Self::String(s) => HirExpression::String(s.clone()),
            Self::Variable(name) => HirExpression::Variable(name.clone()),

            Self::Move(expr) => HirExpression::Move(Box::new(expr.to_hir_expr(decls)?)),
            Self::SizeOf(t) => HirExpression::SizeOf(t.to_hir_type()?),
            Self::Constant(constant) => HirExpression::Constant(constant.to_hir_const(decls)?),

            Self::And(lhs, rhs) => HirExpression::And(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Or(lhs, rhs) => HirExpression::Or(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Not(expr) => HirExpression::Not(Box::new(expr.to_hir_expr(decls)?)),

            Self::Add(lhs, rhs) => HirExpression::Add(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Subtract(lhs, rhs) => HirExpression::Subtract(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Multiply(lhs, rhs) => HirExpression::Multiply(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Divide(lhs, rhs) => HirExpression::Divide(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Greater(lhs, rhs) => HirExpression::Greater(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Less(lhs, rhs) => HirExpression::Less(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::GreaterEqual(lhs, rhs) => HirExpression::GreaterEqual(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::LessEqual(lhs, rhs) => HirExpression::LessEqual(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Equal(lhs, rhs) => HirExpression::Equal(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::NotEqual(lhs, rhs) => HirExpression::NotEqual(
                Box::new(lhs.to_hir_expr(decls)?),
                Box::new(rhs.to_hir_expr(decls)?),
            ),

            Self::Refer(name) => HirExpression::Refer(name.clone()),
            Self::Deref(ptr) => HirExpression::Deref(Box::new(ptr.to_hir_expr(decls)?)),

            Self::TypeCast(expr, t) => {
                HirExpression::TypeCast(Box::new(expr.to_hir_expr(decls)?), t.to_hir_type()?)
            }

            Self::Alloc(expr) => HirExpression::Alloc(Box::new(expr.to_hir_expr(decls)?)),

            Self::Call(name, args) => HirExpression::Call(name.clone(), {
                let mut result = vec![];
                for arg in args {
                    result.push(arg.to_hir_expr(decls)?)
                }
                result
            }),

            Self::ForeignCall(name, args) => HirExpression::ForeignCall(name.clone(), {
                let mut result = vec![];
                for arg in args {
                    result.push(arg.to_hir_expr(decls)?)
                }
                result
            }),

            Self::Method(instance, name, args) => {
                if name == "copy" {
                    return Err(TirError::ExplicitCopy);
                }

                HirExpression::Method(Box::new(instance.to_hir_expr(decls)?), name.clone(), {
                    let mut result = vec![];
                    for arg in args {
                        result.push(arg.to_hir_expr(decls)?)
                    }
                    result
                })
            }

            Self::Index(ptr, idx) => HirExpression::Index(
                Box::new(ptr.to_hir_expr(decls)?),
                Box::new(idx.to_hir_expr(decls)?),
            ),

            Self::Conditional(cond, then, otherwise) => HirExpression::Conditional(
                Box::new(cond.to_hir_expr(decls)?),
                Box::new(then.to_hir_expr(decls)?),
                Box::new(otherwise.to_hir_expr(decls)?),
            ),
        })
    }
}