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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
use std::collections::HashMap;

use codespan_reporting::diagnostic::Diagnostic;

use arret_syntax::datum::Datum;
use arret_syntax::span::{FileId, Span};

use crate::ty;
use crate::ty::purity;
use crate::CompileCtx;

use crate::context::ModuleImports;
use crate::hir::destruc;
use crate::hir::error::{Error, ErrorKind, ExpectedSym, Result};
use crate::hir::exports::Exports;
use crate::hir::import;
use crate::hir::macros::{expand_macro, lower_macro_rules};
use crate::hir::ns::{Ident, NsDataIter, NsDatum};
use crate::hir::prim::Prim;
use crate::hir::records::lower_record;
use crate::hir::scope::{Binding, Scope};
use crate::hir::types::{lower_poly, lower_polymorphic_var_set, try_lower_purity};
use crate::hir::util::{expect_one_arg, expect_spanned_ns_ident, try_take_rest_arg};
use crate::hir::var_id::{ExportId, LocalIdAlloc};
use crate::hir::Lowered;
use crate::hir::{
    App, Cond, DeclPurity, DeclTy, Def, Expr, ExprKind, FieldAccessor, Fun, Let, LocalId, Recur,
};

#[cfg(test)]
use crate::source::EMPTY_SPAN;

/// Module lowered to HIR
pub struct LoweredModule {
    /// Defs in the order the were lowered
    pub defs: Vec<Def<Lowered>>,

    pub exports: Exports,
    pub main_local_id: Option<LocalId>,
}

struct DeferredDef {
    span: Span,
    macro_invocation_span: Option<Span>,
    destruc: destruc::Destruc<Lowered>,
    value_datum: NsDatum,
}

struct DeferredExport {
    span: Span,
    ident: Ident,
}

enum DeferredModulePrim {
    Def(DeferredDef),
    Exports(Vec<DeferredExport>),
}

impl DeferredModulePrim {
    fn with_macro_invocation_span(self, span: Span) -> DeferredModulePrim {
        match self {
            DeferredModulePrim::Def(deferred_def) => DeferredModulePrim::Def(DeferredDef {
                macro_invocation_span: Some(span),
                ..deferred_def
            }),
            other => other,
        }
    }
}

pub(crate) enum LoweredReplDatum {
    /// One or more modules were imported
    Import(ModuleImports),
    /// An evaluable definition
    EvaluableDef(Def<Lowered>),
    /// A non-evalable definition handled by HIR lowering
    NonEvaluableDef,
    /// An expression
    Expr(Expr<Lowered>),
}

// This would be less ugly as Result<!> once it's stabilised
fn lower_user_compile_error(span: Span, arg_iter: NsDataIter) -> Error {
    match expect_one_arg(span, arg_iter) {
        Ok(NsDatum::Str(_, user_message)) => Error::new(span, ErrorKind::UserError(user_message)),
        Ok(other) => Error::new(
            other.span(),
            ErrorKind::ExpectedCompileErrorString(other.description()),
        ),
        Err(error) => error,
    }
}

fn lower_macro(
    scope: &mut Scope<'_>,
    self_datum: NsDatum,
    transformer_spec: NsDatum,
) -> Result<()> {
    let (self_span, self_ident) = expect_spanned_ns_ident(self_datum, "new macro name")?;

    let macro_rules_data = if let NsDatum::List(span, vs) = transformer_spec {
        let mut transformer_data = vs.into_vec();

        let macro_type_datum = if let Some(macro_type_datum) = transformer_data.first() {
            macro_type_datum
        } else {
            return Err(Error::new(span, ErrorKind::NoMacroType));
        };

        if let Some(Binding::Prim(Prim::MacroRules)) = scope.get_datum(macro_type_datum) {
        } else {
            return Err(Error::new(macro_type_datum.span(), ErrorKind::BadMacroType));
        }

        transformer_data.remove(0);
        transformer_data
    } else {
        return Err(Error::new(
            transformer_spec.span(),
            ErrorKind::ExpectedMacroSpecList(transformer_spec.description()),
        ));
    };

    let mac = lower_macro_rules(scope, &self_ident, macro_rules_data)?;
    scope.insert_binding(self_span, self_ident, Binding::Macro(None, mac))?;

    Ok(())
}

fn lower_defmacro(scope: &mut Scope<'_>, span: Span, mut arg_iter: NsDataIter) -> Result<()> {
    if arg_iter.len() != 2 {
        return Err(Error::new(
            span,
            ErrorKind::WrongDefLikeArgCount("defmacro"),
        ));
    }

    let self_datum = arg_iter.next().unwrap();
    let transformer_spec = arg_iter.next().unwrap();

    lower_macro(scope, self_datum, transformer_spec)
}

fn lower_letmacro(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    lower_let_like(lia, scope, span, arg_iter, lower_macro, |expr, _| expr)
}

fn lower_type(scope: &mut Scope<'_>, self_datum: NsDatum, ty_datum: NsDatum) -> Result<()> {
    let (span, ident) = expect_spanned_ns_ident(self_datum, "new type name")?;
    let ty = lower_poly(scope, ty_datum)?;

    scope.insert_binding(span, ident, Binding::Ty(ty))?;
    Ok(())
}

fn lower_deftype(scope: &mut Scope<'_>, span: Span, mut arg_iter: NsDataIter) -> Result<()> {
    if arg_iter.len() != 2 {
        return Err(Error::new(span, ErrorKind::WrongDefLikeArgCount("deftype")));
    }

    let self_datum = arg_iter.next().unwrap();
    let ty_datum = arg_iter.next().unwrap();

    lower_type(scope, self_datum, ty_datum)
}

fn lower_lettype(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    lower_let_like(lia, scope, span, arg_iter, lower_type, |expr, _| expr)
}

fn lower_defrecord(scope: &mut Scope<'_>, span: Span, mut arg_iter: NsDataIter) -> Result<()> {
    if arg_iter.len() != 2 {
        return Err(Error::new(span, ErrorKind::WrongDefRecordArgCount));
    }

    let ty_cons_datum = arg_iter.next().unwrap();
    let value_cons_datum = arg_iter.next().unwrap();

    lower_record(scope, ty_cons_datum, value_cons_datum)
}

fn lower_letrecord(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    lower_let_like(lia, scope, span, arg_iter, lower_record, |expr, _| expr)
}

/// Lowers an identifier in to a scalar destruc with the passed type
fn lower_ident_destruc(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    span: Span,
    ident: Ident,
    decl_ty: DeclTy,
) -> Result<destruc::Scalar<Lowered>> {
    if ident.is_underscore() {
        Ok(destruc::Scalar::new(None, ident.into_name(), decl_ty))
    } else {
        let local_id = lia.alloc();
        let source_name = ident.name().clone();

        scope.insert_local(span, ident, local_id)?;

        Ok(destruc::Scalar::new(Some(local_id), source_name, decl_ty))
    }
}

fn lower_scalar_destruc(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    destruc_datum: NsDatum,
) -> Result<destruc::Scalar<Lowered>> {
    match destruc_datum {
        NsDatum::Ident(span, ident) => lower_ident_destruc(lia, scope, span, ident, DeclTy::Free),
        NsDatum::Vector(span, vs) => {
            let mut data = vs.into_vec();

            if data.len() != 2 {
                return Err(Error::new(span, ErrorKind::NoVecDestruc));
            }

            let ty = lower_poly(scope, data.pop().unwrap())?;

            let (span, ident) = expect_spanned_ns_ident(data.pop().unwrap(), "new variable name")?;
            lower_ident_destruc(lia, scope, span, ident, ty.into())
        }
        _ => Err(Error::new(destruc_datum.span(), ErrorKind::BadRestDestruc)),
    }
}

fn lower_list_destruc(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    mut data_iter: NsDataIter,
) -> Result<destruc::List<Lowered>> {
    let rest = try_take_rest_arg(&mut data_iter);

    let fixed_destrucs = data_iter
        .map(|v| lower_destruc(lia, scope, v))
        .collect::<Result<Vec<destruc::Destruc<Lowered>>>>()?;

    let rest_destruc = match rest {
        Some(rest) => Some(Box::new(lower_scalar_destruc(lia, scope, rest)?)),
        None => None,
    };

    Ok(destruc::List::new(fixed_destrucs, rest_destruc))
}

fn lower_destruc(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    destruc_datum: NsDatum,
) -> Result<destruc::Destruc<Lowered>> {
    match destruc_datum {
        NsDatum::Ident(span, _) | NsDatum::Vector(span, _) => {
            lower_scalar_destruc(lia, scope, destruc_datum)
                .map(|scalar| destruc::Destruc::Scalar(span, scalar))
        }

        NsDatum::List(span, vs) => lower_list_destruc(lia, scope, vs.into_vec().into_iter())
            .map(|list_destruc| destruc::Destruc::List(span, list_destruc)),

        NsDatum::Keyword(span, _) => Err(Error::new(
            span,
            ErrorKind::ExpectedSym(
                ExpectedSym {
                    found: "keyword",
                    usage: "new variable name",
                }
                .into(),
            ),
        )),
        _ => Err(Error::new(destruc_datum.span(), ErrorKind::BadListDestruc)),
    }
}

fn lower_let_like<B, C, O>(
    lia: &LocalIdAlloc,
    outer_scope: &Scope<'_>,
    span: Span,
    mut arg_iter: NsDataIter,
    binder: B,
    fold_output: C,
) -> Result<Expr<Lowered>>
where
    B: Fn(&mut Scope<'_>, NsDatum, NsDatum) -> Result<O>,
    C: Fn(Expr<Lowered>, O) -> Expr<Lowered>,
{
    let bindings_datum = arg_iter
        .next()
        .ok_or_else(|| Error::new(span, ErrorKind::NoBindingVec))?;

    let bindings_data = if let NsDatum::Vector(_, vs) = bindings_datum {
        vs.into_vec()
    } else {
        return Err(Error::new(
            bindings_datum.span(),
            ErrorKind::BindingsNotVec(bindings_datum.description()),
        ));
    };

    let mut scope = outer_scope.child();
    let mut outputs = Vec::<O>::with_capacity(bindings_data.len() / 2);

    let mut bindings_iter = bindings_data.into_iter();
    while let Some(target_datum) = bindings_iter.next() {
        let value_datum = bindings_iter
            .next()
            .ok_or_else(|| Error::new(target_datum.span(), ErrorKind::UnevenBindingVec))?;

        outputs.push(binder(&mut scope, target_datum, value_datum)?);
    }

    let body_expr = lower_body(lia, &scope, arg_iter)?;

    // This is to build nested `Let` expressions. Types/macros don't need this
    Ok(outputs.into_iter().rfold(body_expr, fold_output))
}

fn lower_body(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    body_data: NsDataIter,
) -> Result<Expr<Lowered>> {
    let mut flattened_exprs = vec![];

    for body_datum in body_data {
        match lower_expr(lia, scope, body_datum)? {
            Expr {
                kind: ExprKind::Do(mut exprs),
                ..
            } => {
                flattened_exprs.append(&mut exprs);
            }
            other => {
                flattened_exprs.push(other);
            }
        }
    }

    if flattened_exprs.len() == 1 {
        Ok(flattened_exprs.pop().unwrap())
    } else {
        Ok(ExprKind::Do(flattened_exprs).into())
    }
}

fn lower_let(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    lower_let_like(
        lia,
        scope,
        span,
        arg_iter,
        |scope, target_datum, value_datum| {
            let value_expr = lower_expr(lia, scope, value_datum)?;
            let destruc = lower_destruc(lia, scope, target_datum)?;
            Ok((destruc, value_expr))
        },
        |body_expr, (destruc, value_expr)| {
            ExprKind::Let(Box::new(Let {
                span,
                destruc,
                value_expr,
                body_expr,
            }))
            .into()
        },
    )
}

fn lower_fun(
    lia: &LocalIdAlloc,
    outer_scope: &Scope<'_>,
    span: Span,
    mut arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    let mut fun_scope = outer_scope.child();

    let mut next_datum = arg_iter
        .next()
        .ok_or_else(|| Error::new(span, ErrorKind::NoParamDecl))?;

    // We can either begin with a set of type variables or a list of parameters
    let (pvars, tvars) = if let NsDatum::Set(_, vs) = next_datum {
        next_datum = arg_iter
            .next()
            .ok_or_else(|| Error::new(span, ErrorKind::NoParamDecl))?;

        lower_polymorphic_var_set(outer_scope, &mut fun_scope, vs.into_vec().into_iter())?
    } else {
        (purity::PVars::new(), ty::TVars::new())
    };

    // Pull out our params
    let params = match next_datum {
        NsDatum::List(_, vs) => lower_list_destruc(lia, &mut fun_scope, vs.into_vec().into_iter())?,
        other => {
            return Err(Error::new(
                other.span(),
                ErrorKind::ExpectedParamList(other.description()),
            ));
        }
    };

    // Determine if we have a purity and return type after the parameters, eg (param) -> RetTy
    let mut purity = DeclPurity::Free;
    let mut ret_ty = DeclTy::Free;
    let mut ret_ty_span = None;

    if arg_iter.len() >= 2 {
        if let Some(poly_purity) = try_lower_purity(&fun_scope, &arg_iter.as_slice()[0]) {
            arg_iter.next();
            purity = poly_purity.into();

            match arg_iter.next().unwrap() {
                NsDatum::Ident(_, ref ident) if ident.is_underscore() => {}
                ret_datum => {
                    ret_ty_span = Some(ret_datum.span());
                    ret_ty = lower_poly(&fun_scope, ret_datum)?.into();
                }
            }
        }
    }

    // Extract the body
    let body_expr = lower_body(lia, &fun_scope, arg_iter)?;

    Ok(ExprKind::Fun(Box::new(Fun {
        span,
        pvars,
        tvars,
        purity,
        params,
        ret_ty,
        ret_ty_span,
        body_expr,
    }))
    .into())
}

fn lower_expr_prim_apply(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    prim: Prim,
    mut arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    match prim {
        Prim::Def | Prim::DefMacro | Prim::DefType | Prim::ImportPlaceholder | Prim::DefRecord => {
            Err(Error::new(span, ErrorKind::DefOutsideBody))
        }
        Prim::Let => lower_let(lia, scope, span, arg_iter),
        Prim::LetMacro => lower_letmacro(lia, scope, span, arg_iter),
        Prim::LetType => lower_lettype(lia, scope, span, arg_iter),
        Prim::LetRecord => lower_letrecord(lia, scope, span, arg_iter),
        Prim::Export => Err(Error::new(span, ErrorKind::ExportOutsideModule)),
        Prim::Quote => {
            let literal_datum = expect_one_arg(span, arg_iter)?;
            Ok(literal_datum.into_syntax_datum().into())
        }
        Prim::Fun => lower_fun(lia, scope, span, arg_iter),
        Prim::If => {
            if arg_iter.len() != 3 {
                return Err(Error::new(span, ErrorKind::WrongCondArgCount));
            }

            Ok(ExprKind::Cond(Box::new(Cond {
                span,
                test_expr: lower_expr(lia, scope, arg_iter.next().unwrap())?,
                true_expr: lower_expr(lia, scope, arg_iter.next().unwrap())?,
                false_expr: lower_expr(lia, scope, arg_iter.next().unwrap())?,
            }))
            .into())
        }
        Prim::Do => lower_body(lia, scope, arg_iter),
        Prim::Recur => lower_recur(lia, scope, span, arg_iter),
        Prim::CompileError => Err(lower_user_compile_error(span, arg_iter)),
        Prim::MacroRules | Prim::All => {
            Err(Error::new(span, ErrorKind::ExpectedValue("primitive")))
        }
    }
}

fn lower_expr_apply(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    fun_expr: Expr<Lowered>,
    mut arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    let rest_arg_datum = try_take_rest_arg(&mut arg_iter);

    let fixed_arg_exprs = arg_iter
        .map(|arg_datum| lower_expr(lia, scope, arg_datum))
        .collect::<Result<Vec<Expr<Lowered>>>>()?;

    let rest_arg_expr = match rest_arg_datum {
        Some(rest_arg_datum) => Some(lower_expr(lia, scope, rest_arg_datum)?),
        None => None,
    };

    Ok(ExprKind::App(Box::new(App {
        span,
        fun_expr,
        ty_args: (),
        fixed_arg_exprs,
        rest_arg_expr,
    }))
    .into())
}

fn lower_recur(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    span: Span,
    mut arg_iter: NsDataIter,
) -> Result<Expr<Lowered>> {
    let rest_arg_datum = try_take_rest_arg(&mut arg_iter);

    let fixed_arg_exprs = arg_iter
        .map(|arg_datum| lower_expr(lia, scope, arg_datum))
        .collect::<Result<Vec<Expr<Lowered>>>>()?;

    let rest_arg_expr = match rest_arg_datum {
        Some(rest_arg_datum) => Some(lower_expr(lia, scope, rest_arg_datum)?),
        None => None,
    };

    Ok(ExprKind::Recur(Box::new(Recur {
        span,
        fixed_arg_exprs,
        rest_arg_expr,
    }))
    .into())
}

fn lower_expr(lia: &LocalIdAlloc, scope: &Scope<'_>, datum: NsDatum) -> Result<Expr<Lowered>> {
    match datum {
        NsDatum::Ident(span, ident) => match scope.get_or_err(span, &ident)? {
            Binding::Var(Some(module_id), local_id) => {
                Ok(ExprKind::ExportRef(span, ExportId::new(*module_id, *local_id)).into())
            }
            Binding::Var(None, local_id) => Ok(ExprKind::LocalRef(span, *local_id).into()),
            Binding::TyPred(test_ty) => Ok(ExprKind::TyPred(span, test_ty.clone()).into()),
            Binding::EqPred => Ok(ExprKind::EqPred(span).into()),
            Binding::RecordValueCons(record_cons) => {
                Ok(ExprKind::RecordCons(span, record_cons.clone()).into())
            }
            Binding::FieldAccessor(record_cons, field_index) => {
                Ok(ExprKind::FieldAccessor(Box::new(FieldAccessor {
                    span,
                    record_cons: record_cons.clone(),
                    field_index: *field_index,
                }))
                .into())
            }
            other => Err(Error::new(
                span,
                ErrorKind::ExpectedValue(other.description()),
            )),
        },
        NsDatum::List(span, vs) => {
            let mut data_iter = vs.into_vec().into_iter();

            let fn_datum = if let Some(fn_datum) = data_iter.next() {
                fn_datum
            } else {
                return Ok(Datum::List(span, Box::new([])).into());
            };

            if let NsDatum::Ident(fn_span, ref ident) = fn_datum {
                match scope.get_or_err(fn_span, ident)? {
                    Binding::Prim(prim) => {
                        return lower_expr_prim_apply(lia, scope, span, *prim, data_iter);
                    }
                    Binding::Macro(module_id, mac) => {
                        let mut macro_scope = scope.child();

                        let expanded_datum = expand_macro(
                            &mut macro_scope,
                            span,
                            *module_id,
                            mac,
                            data_iter.as_slice(),
                        )?;

                        return lower_expr(lia, &macro_scope, expanded_datum)
                            .map(|expr| ExprKind::MacroExpand(span, Box::new(expr)).into())
                            .map_err(|e| e.with_macro_invocation_span(span));
                    }
                    _ => {}
                }
            }

            let fn_expr = lower_expr(lia, scope, fn_datum)?;
            lower_expr_apply(lia, scope, span, fn_expr, data_iter)
        }
        other => Ok(other.into_syntax_datum().into()),
    }
}

fn lower_module_prim_apply(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    span: Span,
    prim: Prim,
    mut arg_iter: NsDataIter,
) -> Result<Option<DeferredModulePrim>, Vec<Error>> {
    match prim {
        Prim::Export => {
            let deferred_exports = arg_iter
                .map(|datum| {
                    let (span, ident) = expect_spanned_ns_ident(datum, "identifier to export")?;
                    Ok(DeferredExport { span, ident })
                })
                .collect::<Result<Vec<DeferredExport>>>()?;

            Ok(Some(DeferredModulePrim::Exports(deferred_exports)))
        }
        Prim::Def => {
            if arg_iter.len() != 2 {
                return Err(vec![Error::new(
                    span,
                    ErrorKind::WrongDefLikeArgCount("def"),
                )]);
            }

            let destruc_datum = arg_iter.next().unwrap();
            let destruc = lower_destruc(lia, scope, destruc_datum)?;

            let value_datum = arg_iter.next().unwrap();

            let deferred_def = DeferredDef {
                span,
                macro_invocation_span: None,
                destruc,
                value_datum,
            };

            Ok(Some(DeferredModulePrim::Def(deferred_def)))
        }
        Prim::DefMacro => Ok(lower_defmacro(scope, span, arg_iter).map(|_| None)?),
        Prim::DefType => Ok(lower_deftype(scope, span, arg_iter).map(|_| None)?),
        Prim::DefRecord => Ok(lower_defrecord(scope, span, arg_iter).map(|_| None)?),
        Prim::CompileError => Err(vec![lower_user_compile_error(span, arg_iter)]),
        _ => Err(vec![Error::new(span, ErrorKind::NonDefInsideModule)]),
    }
}

fn lower_module_def(
    lia: &LocalIdAlloc,
    scope: &mut Scope<'_>,
    datum: NsDatum,
) -> Result<Option<DeferredModulePrim>, Vec<Error>> {
    let span = datum.span();

    if let NsDatum::List(span, vs) = datum {
        let mut data_iter = vs.into_vec().into_iter();

        if let Some(NsDatum::Ident(fn_span, ref ident)) = data_iter.next() {
            match scope.get_or_err(fn_span, ident)? {
                Binding::Prim(prim) => {
                    let prim = *prim;
                    return lower_module_prim_apply(lia, scope, span, prim, data_iter);
                }
                Binding::Macro(module_id, mac) => {
                    let module_id = *module_id;
                    let mac = &mac.clone();

                    let expanded_datum =
                        expand_macro(scope, span, module_id, mac, data_iter.as_slice())?;

                    return lower_module_def(lia, scope, expanded_datum)
                        .map(|def| def.map(|def| def.with_macro_invocation_span(span)))
                        .map_err(|errs| {
                            errs.into_iter()
                                .map(|e| e.with_macro_invocation_span(span))
                                .collect()
                        });
                }
                _ => {
                    // Non-def
                }
            }
        }
    }

    Err(vec![Error::new(span, ErrorKind::NonDefInsideModule)])
}

fn insert_import_bindings(
    imports: &ModuleImports,
    scope: &mut Scope<'_>,
    arg_data: &[Datum],
) -> Result<(), Vec<Error>> {
    for arg_datum in arg_data {
        let span = arg_datum.span();

        let parsed_import = import::parse_import_set(arg_datum)?;
        let import_module = &imports[parsed_import.module_name()];

        let exports = import::filter_imported_exports(parsed_import, &import_module.exports)?;

        scope.insert_bindings(
            span,
            exports.into_iter().map(|(name, binding)| {
                (
                    Ident::new(Scope::root_ns_id(), name),
                    binding.import_from(import_module.module_id),
                )
            }),
        )?;
    }

    Ok(())
}

pub(crate) fn lower_data(
    imports: &ModuleImports,
    data: &[Datum],
) -> Result<LoweredModule, Vec<Error>> {
    let lia = LocalIdAlloc::new();
    let mut scope = Scope::root();

    // Build up a list of errors to return at once
    let mut errors: Vec<Error> = vec![];

    // Extract all of our definitions.
    //
    // This occurs in two passes:
    // - Imports, types and macros are resolved immediately and cannot refer to bindings later
    //   in the body
    // - Definitions are resolved after the module has been loaded
    let mut deferred_exports = Vec::<DeferredExport>::new();
    let mut deferred_defs = Vec::<DeferredDef>::new();

    for input_datum in data {
        if let Some(arg_data) = import::try_extract_import_set(input_datum) {
            if let Err(mut new_errors) = insert_import_bindings(imports, &mut scope, arg_data) {
                errors.append(&mut new_errors);
            }

            continue;
        }

        let ns_datum = NsDatum::from_syntax_datum(input_datum);
        match lower_module_def(&lia, &mut scope, ns_datum) {
            Ok(Some(DeferredModulePrim::Exports(mut exports))) => {
                deferred_exports.append(&mut exports);
            }
            Ok(Some(DeferredModulePrim::Def(deferred_def))) => {
                deferred_defs.push(deferred_def);
            }
            Ok(None) => {}
            Err(mut new_errors) => {
                errors.append(&mut new_errors);
            }
        };
    }

    // Process any exports
    let mut exports = HashMap::with_capacity(deferred_exports.len());
    for deferred_export in deferred_exports {
        let DeferredExport { span, ident } = deferred_export;
        match scope.get_or_err(span, &ident) {
            Ok(binding) => {
                exports.insert(ident.into_name(), binding.clone());
            }
            Err(err) => {
                errors.push(err);
            }
        };
    }

    // And now process any deferred defs
    let mut defs = Vec::with_capacity(deferred_defs.len());
    for deferred_def in deferred_defs {
        match resolve_deferred_def(&lia, &scope, deferred_def) {
            Ok(def) => {
                defs.push(def);
            }
            Err(error) => {
                errors.push(error);
            }
        }
    }

    // Try to find `main!`. If we're not the entry module this will be ignored.
    let main_ident = Ident::new(Scope::root_ns_id(), "main!".into());
    let main_local_id = if let Some(Binding::Var(None, local_id)) = scope.get(&main_ident) {
        Some(*local_id)
    } else {
        None
    };

    if errors.is_empty() {
        Ok(LoweredModule {
            defs,
            exports,
            main_local_id,
        })
    } else {
        Err(errors)
    }
}

fn resolve_deferred_def(
    lia: &LocalIdAlloc,
    scope: &Scope<'_>,
    deferred_def: DeferredDef,
) -> Result<Def<Lowered>> {
    let DeferredDef {
        span,
        macro_invocation_span,
        destruc,
        value_datum,
    } = deferred_def;

    lower_expr(lia, scope, value_datum).map(|value_expr| Def {
        span,
        macro_invocation_span,
        destruc,
        value_expr,
    })
}

// REPL interface
pub(crate) fn lower_repl_datum(
    ccx: &CompileCtx,
    scope: &mut Scope<'_>,
    datum: &Datum,
) -> Result<LoweredReplDatum, Vec<Diagnostic<FileId>>> {
    use crate::reporting::errors_to_diagnostics;

    // For the purposes of type inference every datum is a new module
    let lia = LocalIdAlloc::new();

    if let Some(arg_data) = import::try_extract_import_set(datum) {
        let imports = ccx.imports_for_data(std::iter::once(datum))?;

        insert_import_bindings(&imports, scope, arg_data).map_err(errors_to_diagnostics)?;

        return Ok(LoweredReplDatum::Import(imports));
    }

    // Try interpreting this as a module def
    let ns_datum = NsDatum::from_syntax_datum(datum);
    match lower_module_def(&lia, scope, ns_datum.clone()) {
        Ok(Some(DeferredModulePrim::Def(deferred_def))) => {
            let def =
                resolve_deferred_def(&lia, scope, deferred_def).map_err(|err| vec![err.into()])?;

            Ok(LoweredReplDatum::EvaluableDef(def))
        }
        Ok(Some(DeferredModulePrim::Exports(_))) => {
            Err(vec![
                Error::new(datum.span(), ErrorKind::ExportInsideRepl).into()
            ])
        }
        Ok(None) => Ok(LoweredReplDatum::NonEvaluableDef),
        Err(mut errs) => {
            // `NonDefInsideModule` doesn't apply because we allow non-defs in the REPL
            errs.retain(|err| err.kind() != &ErrorKind::NonDefInsideModule);

            if errs.is_empty() {
                // Re-interpret as an expression
                let expr = lower_expr(&lia, scope, ns_datum).map_err(|err| vec![err.into()])?;

                Ok(LoweredReplDatum::Expr(expr))
            } else {
                Err(errors_to_diagnostics(errs))
            }
        }
    }
}

////
#[cfg(test)]
fn import_statement_for_module(names: &[&'static str]) -> Datum {
    Datum::List(
        EMPTY_SPAN,
        Box::new([
            Datum::Sym(EMPTY_SPAN, "import".into()),
            Datum::Vector(
                EMPTY_SPAN,
                names
                    .iter()
                    .map(|&n| Datum::Sym(EMPTY_SPAN, n.into()))
                    .collect(),
            ),
        ]),
    )
}

#[cfg(test)]
fn module_for_str(data_str: &str) -> Result<LoweredModule> {
    use std::iter;
    use std::sync::Arc;

    use arret_syntax::parser::data_from_str;

    use crate::context;
    use crate::hir::exports;
    use crate::hir::loader::ModuleName;

    let mut program_data = vec![];
    let mut imports: ModuleImports = HashMap::new();

    for (terminal_name, exports) in iter::once(("primitives", exports::prims_exports()))
        .chain(iter::once(("types", exports::tys_exports())))
    {
        program_data.push(import_statement_for_module(&[
            "arret",
            "internal",
            terminal_name,
        ]));

        imports.insert(
            ModuleName::new(
                "arret".into(),
                vec!["internal".into()],
                terminal_name.into(),
            ),
            Arc::new(context::prims_to_module(exports)),
        );
    }

    let mut test_data = data_from_str(None, data_str).unwrap();
    program_data.append(&mut test_data);

    imports.insert(
        ModuleName::new("arret".into(), vec!["internal".into()], "types".into()),
        Arc::new(context::prims_to_module(exports::tys_exports())),
    );

    lower_data(&imports, &program_data).map_err(|mut errors| errors.remove(0))
}

#[cfg(test)]
pub fn expr_for_str(data_str: &str) -> Expr<Lowered> {
    use arret_syntax::parser::datum_from_str;

    let lia = LocalIdAlloc::new();
    let scope = Scope::new_with_primitives();

    let test_datum = datum_from_str(None, data_str).unwrap();
    let test_nsdatum = NsDatum::from_syntax_datum(&test_datum);

    lower_expr(&lia, &scope, test_nsdatum).unwrap()
}

#[allow(clippy::many_single_char_names)]
#[cfg(test)]
mod test {
    use super::*;

    use arret_syntax::span::t2s;

    use crate::ty::purity::Purity;
    use crate::ty::Ty;

    #[test]
    fn self_quoting_bool() {
        let j = "false";
        let t = "^^^^^";

        let expected: Expr<_> = Datum::Bool(t2s(t), false).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn self_quoting_empty_list() {
        let j = "()";
        let t = "^^";

        let expected: Expr<_> = Datum::List(t2s(t), Box::new([])).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn quoted_datum_shorthand() {
        let j = "'foo";
        let t = " ^^^";

        let expected: Expr<_> = Datum::Sym(t2s(t), "foo".into()).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn quoted_datum_explicit() {
        let j = "(quote foo)";
        let t = "       ^^^ ";

        let expected: Expr<_> = Datum::Sym(t2s(t), "foo".into()).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn self_evaluating_keyword() {
        let j = ":foo";
        let t = "^^^^";

        let expected: Expr<_> = Datum::Sym(t2s(t), ":foo".into()).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn wildcard_let() {
        let j = "(let [_ 1])";
        let t = "      ^    ";
        let u = "^^^^^^^^^^^";
        let v = "        ^  ";

        let destruc =
            destruc::Destruc::Scalar(t2s(t), destruc::Scalar::new(None, "_".into(), DeclTy::Free));

        let expected: Expr<_> = ExprKind::Let(Box::new(Let {
            span: t2s(u),
            destruc,
            value_expr: Datum::Int(t2s(v), 1).into(),
            body_expr: ExprKind::Do(vec![]).into(),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn empty_fn() {
        let j = "(fn ())";
        let t = "^^^^^^^";

        let expected: Expr<_> = ExprKind::Fun(Box::new(Fun {
            span: t2s(t),
            pvars: purity::PVars::new(),
            tvars: ty::TVars::new(),
            purity: DeclPurity::Free,
            params: destruc::List::new(vec![], None),
            ret_ty: DeclTy::Free,
            ret_ty_span: None,
            body_expr: ExprKind::Do(vec![]).into(),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn empty_fn_with_purity() {
        let j = "(fn () -> _ 1)";
        let t = "^^^^^^^^^^^^^^";
        let u = "            ^ ";

        let expected: Expr<_> = ExprKind::Fun(Box::new(Fun {
            span: t2s(t),
            pvars: purity::PVars::new(),
            tvars: ty::TVars::new(),
            purity: Purity::Pure.into(),
            params: destruc::List::new(vec![], None),
            ret_ty: DeclTy::Free,
            ret_ty_span: None,
            body_expr: Datum::Int(t2s(u), 1).into(),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn empty_fn_with_ret_ty() {
        let j = "(fn () -> Int 1)";
        let t = "^^^^^^^^^^^^^^^^";
        let u = "          ^^^   ";
        let v = "              ^ ";

        let expected: Expr<_> = ExprKind::Fun(Box::new(Fun {
            span: t2s(t),
            pvars: purity::PVars::new(),
            tvars: ty::TVars::new(),
            purity: Purity::Pure.into(),
            params: destruc::List::new(vec![], None),
            ret_ty: Ty::Int.into(),
            ret_ty_span: Some(t2s(u)),
            body_expr: Datum::Int(t2s(v), 1).into(),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn fixed_expr_apply() {
        let j = "(1 2 3)";
        let t = "^^^^^^^";
        let u = " ^     ";
        let v = "   ^   ";
        let w = "     ^ ";

        let expected: Expr<_> = ExprKind::App(Box::new(App {
            span: t2s(t),
            fun_expr: Datum::Int(t2s(u), 1).into(),
            ty_args: (),
            fixed_arg_exprs: vec![Datum::Int(t2s(v), 2).into(), Datum::Int(t2s(w), 3).into()],
            rest_arg_expr: None,
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn rest_expr_apply() {
        let j = "(1 2 & 3)";
        let t = "^^^^^^^^^";
        let u = " ^       ";
        let v = "   ^     ";
        let w = "       ^ ";

        let expected: Expr<_> = ExprKind::App(Box::new(App {
            span: t2s(t),
            fun_expr: Datum::Int(t2s(u), 1).into(),
            ty_args: (),
            fixed_arg_exprs: vec![Datum::Int(t2s(v), 2).into()],
            rest_arg_expr: Some(Datum::Int(t2s(w), 3).into()),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn recur_expr() {
        let j = "(recur 1 2 3)";
        let t = "^^^^^^^^^^^^^";
        let u = "       ^     ";
        let v = "         ^   ";
        let w = "           ^ ";

        let expected: Expr<_> = ExprKind::Recur(Box::new(Recur {
            span: t2s(t),
            fixed_arg_exprs: vec![
                Datum::Int(t2s(u), 1).into(),
                Datum::Int(t2s(v), 2).into(),
                Datum::Int(t2s(w), 3).into(),
            ],
            rest_arg_expr: None,
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn if_expr() {
        let j = "(if true 1 2)";
        let t = "^^^^^^^^^^^^^";
        let u = "    ^^^^     ";
        let v = "         ^   ";
        let w = "           ^ ";

        let expected: Expr<_> = ExprKind::Cond(Box::new(Cond {
            span: t2s(t),
            test_expr: ExprKind::Lit(Datum::Bool(t2s(u), true)).into(),
            true_expr: ExprKind::Lit(Datum::Int(t2s(v), 1)).into(),
            false_expr: ExprKind::Lit(Datum::Int(t2s(w), 2)).into(),
        }))
        .into();

        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn expand_trivial_macro() {
        let j = "(letmacro [one (macro-rules [() 1])] (one))";
        let t = "                                     ^^^^^ ";
        let u = "                                ^          ";

        let expected: Expr<_> =
            ExprKind::MacroExpand(t2s(t), Box::new(Datum::Int(t2s(u), 1).into())).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn mutual_module_def() {
        let j1 = "(export x y)";
        let j2 = "(def x y)";
        let j3 = "(def y x)";

        let j = &[j1, j2, j3].join("");

        let module = module_for_str(j).unwrap();
        assert_eq!(2, module.exports.len());
    }

    #[test]
    fn type_predicate() {
        let j = "bool?";
        let t = "^^^^^";

        let expected: Expr<_> = ExprKind::TyPred(t2s(t), ty::pred::TestTy::Bool).into();
        assert_eq!(expected, expr_for_str(j));
    }

    #[test]
    fn equality_predicate() {
        let j = "=";
        let t = "^";

        let expected: Expr<_> = ExprKind::EqPred(t2s(t)).into();
        assert_eq!(expected, expr_for_str(j));
    }
}