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
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

//! The build stage of the ucg compiler.
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet, VecDeque};
use std::convert::From;
use std::env;
use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::fs::File;
use std::io::Read;
use std::ops::Deref;
use std::path::PathBuf;
use std::rc::Rc;
use std::string::ToString;

use ast::*;
use error;
use format;
use parse::parse;
use tokenizer::Span;

impl MacroDef {
    /// Expands a ucg Macro using the given arguments into a new Tuple.
    pub fn eval(
        &self,
        root: PathBuf,
        env: Rc<Val>,
        mut args: Vec<Rc<Val>>,
    ) -> Result<Vec<(Positioned<String>, Rc<Val>)>, Box<Error>> {
        // Error conditions. If the args don't match the length and types of the argdefs then this is
        // macro call error.
        if args.len() > self.argdefs.len() {
            return Err(Box::new(error::Error::new(
                format!(
                    "Macro called with too many args in file: {}",
                    root.to_string_lossy()
                ),
                error::ErrorType::BadArgLen,
                self.pos.clone(),
            )));
        }
        // If the args don't match the types required by the expressions then that is a TypeFail.
        // If the expressions reference Symbols not defined in the MacroDef that is also an error.
        // TODO(jwall): We should probably enforce that the Expression Symbols must be in argdefs rules
        // at Macro definition time not evaluation time.
        let mut scope = HashMap::<Positioned<String>, Rc<Val>>::new();
        for (i, arg) in args.drain(0..).enumerate() {
            scope.entry(self.argdefs[i].clone()).or_insert(arg.clone());
        }
        let b = Builder::new_with_env_and_scope(root, scope, env);
        let mut result: Vec<(Positioned<String>, Rc<Val>)> = Vec::new();
        for &(ref key, ref expr) in self.fields.iter() {
            // We clone the expressions here because this macro may be consumed
            // multiple times in the future.
            let val = try!(b.eval_expr(expr));
            result.push((key.into(), val.clone()));
        }
        Ok(result)
    }
}

/// The result of a build.
type BuildResult = Result<(), Box<Error>>;

/// The Intermediate representation of a compiled UCG AST.
#[derive(PartialEq, Debug, Clone)]
pub enum Val {
    Empty,
    Boolean(bool),
    Int(i64),
    Float(f64),
    Str(String),
    List(Vec<Rc<Val>>),
    Tuple(Vec<(Positioned<String>, Rc<Val>)>),
    Macro(MacroDef),
}

impl Val {
    /// Returns the Type of a Val as a string.
    pub fn type_name(&self) -> String {
        match self {
            &Val::Empty => "EmptyValue".to_string(),
            &Val::Boolean(_) => "Boolean".to_string(),
            &Val::Int(_) => "Integer".to_string(),
            &Val::Float(_) => "Float".to_string(),
            &Val::Str(_) => "String".to_string(),
            &Val::List(_) => "List".to_string(),
            &Val::Tuple(_) => "Tuple".to_string(),
            &Val::Macro(_) => "Macro".to_string(),
        }
    }

    /// Returns true if called with a Val of the same type as itself.
    pub fn type_equal(&self, target: &Self) -> bool {
        enum_type_equality!(
            self,
            target,
            &Val::Empty,
            &Val::Boolean(_),
            &Val::Int(_),
            &Val::Float(_),
            &Val::Str(_),
            &Val::List(_),
            &Val::Tuple(_),
            &Val::Macro(_)
        )
    }

    pub fn equal(
        &self,
        target: &Self,
        file_name: &str,
        pos: Position,
    ) -> Result<bool, error::Error> {
        // first we do a type equality comparison
        match (self, target) {
            // Empty values are always equal.
            (&Val::Empty, &Val::Empty) => Ok(true),
            (&Val::Int(ref i), &Val::Int(ref ii)) => Ok(i == ii),
            (&Val::Float(ref f), &Val::Float(ref ff)) => Ok(f == ff),
            (&Val::Boolean(ref b), &Val::Boolean(ref bb)) => Ok(b == bb),
            (&Val::Str(ref s), &Val::Str(ref ss)) => Ok(s == ss),
            (&Val::List(ref ldef), &Val::List(ref rdef)) => {
                if ldef.len() != rdef.len() {
                    Ok(false)
                } else {
                    for (i, lv) in ldef.iter().enumerate() {
                        try!(lv.equal(rdef[i].as_ref(), file_name, pos.clone()));
                    }
                    Ok(true)
                }
            }
            (&Val::Tuple(ref ldef), &Val::Tuple(ref rdef)) => {
                if ldef.len() != rdef.len() {
                    Ok(false)
                } else {
                    for (i, lv) in ldef.iter().enumerate() {
                        let field_target = &rdef[i];
                        if lv.0.val != field_target.0.val {
                            // field name equality
                            return Ok(false);
                        } else {
                            // field value equality.
                            if !try!(lv.1.equal(
                                field_target.1.as_ref(),
                                file_name,
                                lv.0.pos.clone()
                            )) {
                                return Ok(false);
                            }
                        }
                    }
                    Ok(true)
                }
            }
            (&Val::Macro(_), &Val::Macro(_)) => Err(error::Error::new(
                format!("Macros are not comparable in file: {}", file_name),
                error::ErrorType::TypeFail,
                pos,
            )),
            (me, tgt) => Err(error::Error::new(
                format!("Types differ for {}, {} in file: {}", me, tgt, file_name),
                error::ErrorType::TypeFail,
                pos,
            )),
        }
    }

    /// Returns the fields if this Val is a tuple. None otherwise.
    pub fn get_fields(&self) -> Option<&Vec<(Positioned<String>, Rc<Val>)>> {
        if let &Val::Tuple(ref fs) = self {
            Some(fs)
        } else {
            None
        }
    }

    pub fn is_int(&self) -> bool {
        if let &Val::Int(_) = self {
            return true;
        }
        return false;
    }

    pub fn is_empty(&self) -> bool {
        if let &Val::Empty = self {
            return true;
        }
        return false;
    }

    pub fn is_float(&self) -> bool {
        if let &Val::Float(_) = self {
            return true;
        }
        return false;
    }

    pub fn is_string(&self) -> bool {
        if let &Val::Str(_) = self {
            return true;
        }
        return false;
    }

    pub fn is_tuple(&self) -> bool {
        if let &Val::Tuple(_) = self {
            return true;
        }
        return false;
    }

    pub fn is_list(&self) -> bool {
        if let &Val::List(_) = self {
            return true;
        }
        return false;
    }

    pub fn is_macro(&self) -> bool {
        if let &Val::Macro(_) = self {
            return true;
        }
        return false;
    }
}

impl Display for Val {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            &Val::Boolean(b) => write!(f, "Boolean({})", b),
            &Val::Empty => write!(f, "EmptyValue"),
            &Val::Float(ref ff) => write!(f, "Float({})", ff),
            &Val::Int(ref i) => write!(f, "Int({})", i),
            &Val::Str(ref s) => write!(f, "String({})", s),
            &Val::List(ref def) => {
                try!(write!(f, "[\n"));
                for v in def.iter() {
                    try!(write!(f, "\t{},\n", v));
                }
                write!(f, "]")
            }
            &Val::Macro(_) => write!(f, "Macro(..)"),
            &Val::Tuple(ref def) => {
                try!(write!(f, "Tuple(\n"));
                for v in def.iter() {
                    try!(write!(f, "\t{} = {},\n", v.0.val, v.1));
                }
                write!(f, ")")
            }
        }
    }
}

impl From<Val> for String {
    fn from(v: Val) -> String {
        match v {
            Val::Int(ref i) => format!("{}", i),
            Val::Float(ref f) => format!("{}", f),
            Val::Str(ref s) => s.to_string(),
            val => format!("<{}>", val),
        }
    }
}

impl From<String> for Val {
    fn from(s: String) -> Val {
        Val::Str(s)
    }
}

/// Defines a set of values in a parsed file.
type ValueMap = HashMap<Positioned<String>, Rc<Val>>;

pub struct AssertCollector {
    pub success: bool,
    pub summary: String,
    pub failures: String,
}

/// Handles building ucg code.
pub struct Builder {
    root: PathBuf,
    validate_mode: bool,
    assert_collector: AssertCollector,
    env: Rc<Val>,
    /// assets are other parsed files from import statements. They
    /// are keyed by the normalized import path. This acts as a cache
    /// so multiple imports of the same file don't have to be parsed
    /// multiple times.
    assets: ValueMap,
    // List of file paths we have already parsed.
    files: HashSet<String>,
    /// out is our built output.
    out: ValueMap,
    /// last is the result of the last statement.
    pub last: Option<Rc<Val>>,
}

macro_rules! eval_binary_expr {
    ($case:pat, $pos:ident, $rside:ident, $result:expr, $msg:expr) => {
        match $rside.as_ref() {
            $case => {
                return Ok(Rc::new($result));
            }
            val => {
                return Err(Box::new(error::Error::new(
                    format!("Expected {} but got {}", $msg, val),
                    error::ErrorType::TypeFail,
                    $pos.clone(),
                )));
            }
        }
    };
}

impl Builder {
    // FIXME(jwall): This needs some unit tests.
    fn tuple_to_val(&self, fields: &Vec<(Token, Expression)>) -> Result<Rc<Val>, Box<Error>> {
        let mut new_fields = Vec::<(Positioned<String>, Rc<Val>)>::new();
        for &(ref name, ref expr) in fields.iter() {
            let val = try!(self.eval_expr(expr));
            new_fields.push((name.into(), val));
        }
        Ok(Rc::new(Val::Tuple(new_fields)))
    }

    fn list_to_val(&self, def: &ListDef) -> Result<Rc<Val>, Box<Error>> {
        let mut vals = Vec::new();
        for expr in def.elems.iter() {
            vals.push(try!(self.eval_expr(expr)));
        }
        Ok(Rc::new(Val::List(vals)))
    }

    fn value_to_val(&self, v: &Value) -> Result<Rc<Val>, Box<Error>> {
        match v {
            &Value::Empty(_) => Ok(Rc::new(Val::Empty)),
            &Value::Boolean(ref b) => Ok(Rc::new(Val::Boolean(b.val))),
            &Value::Int(ref i) => Ok(Rc::new(Val::Int(i.val))),
            &Value::Float(ref f) => Ok(Rc::new(Val::Float(f.val))),
            &Value::Str(ref s) => Ok(Rc::new(Val::Str(s.val.to_string()))),
            &Value::Symbol(ref s) => self.lookup_sym(&(s.into())).ok_or(Box::new(
                error::Error::new(
                    format!(
                        "Unable to find {} in file: {}",
                        s.val,
                        self.root.to_string_lossy()
                    ),
                    error::ErrorType::NoSuchSymbol,
                    v.pos().clone(),
                ),
            )),
            &Value::List(ref def) => self.list_to_val(def),
            &Value::Tuple(ref tuple) => self.tuple_to_val(&tuple.val),
            &Value::Selector(ref selector_list_node) => {
                self.lookup_selector(&selector_list_node.sel)
            }
        }
    }

    /// Constructs a new Builder.
    pub fn new<P: Into<PathBuf>>(root: P) -> Self {
        Self::new_with_scope(root, HashMap::new())
    }

    /// Constructs a new Builder with a provided scope.
    pub fn new_with_scope<P: Into<PathBuf>>(root: P, scope: ValueMap) -> Self {
        let env_vars: Vec<(Positioned<String>, Rc<Val>)> = env::vars()
            .map(|t| (Positioned::new(t.0, 0, 0), Rc::new(t.1.into())))
            .collect();
        Self::new_with_env_and_scope(root, scope, Rc::new(Val::Tuple(env_vars)))
    }

    pub fn new_with_env_and_scope<P: Into<PathBuf>>(
        root: P,
        scope: ValueMap,
        env: Rc<Val>,
    ) -> Self {
        Builder {
            root: root.into(),
            validate_mode: false,
            assert_collector: AssertCollector {
                success: true,
                summary: String::new(),
                failures: String::new(),
            },
            env: env,
            assets: HashMap::new(),
            files: HashSet::new(),
            out: scope,
            last: None,
        }
    }

    /// Returns a Val by name from previously built UCG.
    pub fn get_out_by_name(&self, name: &str) -> Option<Rc<Val>> {
        let key = Positioned {
            pos: Position::new(0, 0),
            val: name.to_string(),
        };
        self.lookup_sym(&key)
    }

    /// Puts the builder in validation mode.
    ///
    /// Among other things this means that assertions will be evaluated and their results
    /// will be saved in a report for later output.
    pub fn enable_validate_mode(&mut self) {
        self.validate_mode = true;
    }

    /// Builds a list of parsed UCG Statements.
    pub fn build(&mut self, ast: &Vec<Statement>) -> BuildResult {
        for stmt in ast.iter() {
            try!(self.build_stmt(stmt));
        }
        Ok(())
    }

    pub fn eval_string(&mut self, input: &str) -> Result<Rc<Val>, Box<Error>> {
        match parse(Span::new(input)) {
            Ok(stmts) => {
                //panic!("Successfully parsed {}", input);
                let mut out: Option<Rc<Val>> = None;
                for stmt in stmts.iter() {
                    out = Some(try!(self.build_stmt(stmt)));
                }
                match out {
                    None => return Ok(Rc::new(Val::Empty)),
                    Some(val) => Ok(val),
                }
            }
            Err(err) => Err(Box::new(error::Error::new_with_cause(
                format!("Error while parsing file: {}", self.root.to_string_lossy()),
                error::ErrorType::ParseError,
                err,
            ))),
        }
    }

    /// Builds a string of ucg syntax.
    pub fn build_file_string(&mut self, input: String) -> BuildResult {
        self.last = Some(try!(self.eval_string(&input)));
        Ok(())
    }

    /// Builds a ucg file at the named path.
    pub fn build_file(&mut self, name: &str) -> BuildResult {
        let mut f = try!(File::open(name));
        let mut s = String::new();
        try!(f.read_to_string(&mut s));
        self.build_file_string(s)
    }

    fn build_import(&mut self, def: &ImportDef) -> Result<Rc<Val>, Box<Error>> {
        let sym = &def.name;
        let positioned_sym = sym.into();
        let mut normalized = self.root.to_path_buf();
        normalized.push(&def.path.fragment);
        let key = normalized.to_str().unwrap().to_string();
        if !self.files.contains(&key) {
            // Only parse the file once on import.
            if self.assets.get(&positioned_sym).is_none() {
                let mut b = Self::new(normalized);
                try!(b.build_file(&def.path.fragment));
                let fields: Vec<(Positioned<String>, Rc<Val>)> = b.out.drain().collect();
                let result = Rc::new(Val::Tuple(fields));
                self.assets.entry(positioned_sym).or_insert(result.clone());
                self.files.insert(def.path.fragment.clone());
                return Ok(result);
            } else {
                return Ok(self.assets.get(&positioned_sym).unwrap().clone());
            }
        } else {
            return match self.assets.get(&positioned_sym) {
                None => {
                    // some kind of error here I think.
                    Err(Box::new(error::Error::new(
                        format!(
                            "Unknown Error processing import in file: {}",
                            self.root.to_string_lossy()
                        ),
                        error::ErrorType::Unsupported,
                        def.name.pos.clone(),
                    )))
                }
                Some(val) => Ok(val.clone()),
            };
        }
    }

    fn build_let(&mut self, def: &LetDef) -> Result<Rc<Val>, Box<Error>> {
        let val = try!(self.eval_expr(&def.value));
        let name = &def.name;
        match self.out.entry(name.into()) {
            Entry::Occupied(e) => {
                return Err(Box::new(error::Error::new(
                    format!(
                        "Let binding \
                         for {:?} already \
                         exists in file: {}",
                        e.key(),
                        self.root.to_string_lossy(),
                    ),
                    error::ErrorType::DuplicateBinding,
                    def.name.pos.clone(),
                )));
            }
            Entry::Vacant(e) => {
                e.insert(val.clone());
            }
        }
        Ok(val)
    }

    fn build_stmt(&mut self, stmt: &Statement) -> Result<Rc<Val>, Box<Error>> {
        match stmt {
            &Statement::Assert(ref expr) => self.build_assert(&expr),
            &Statement::Let(ref def) => self.build_let(def),
            &Statement::Import(ref def) => self.build_import(def),
            &Statement::Expression(ref expr) => self.eval_expr(expr),
        }
    }

    fn lookup_sym(&self, sym: &Positioned<String>) -> Option<Rc<Val>> {
        if &sym.val == "env" {
            return Some(self.env.clone());
        }
        if self.out.contains_key(sym) {
            return Some(self.out[sym].clone());
        }
        if self.assets.contains_key(sym) {
            return Some(self.assets[sym].clone());
        }
        None
    }

    fn find_in_fieldlist(target: &str, fs: &Vec<(Positioned<String>, Rc<Val>)>) -> Option<Rc<Val>> {
        for (key, val) in fs.iter().cloned() {
            if target == &key.val {
                return Some(val.clone());
            }
        }
        return None;
    }

    fn lookup_in_tuple(
        &self,
        stack: &mut VecDeque<Rc<Val>>,
        sl: &SelectorList,
        next: (&Position, &str),
        fs: &Vec<(Positioned<String>, Rc<Val>)>,
    ) -> Result<(), Box<Error>> {
        if let Some(vv) = Self::find_in_fieldlist(next.1, fs) {
            stack.push_back(vv.clone());
        } else {
            return Err(Box::new(error::Error::new(
                format!(
                    "Unable to \
                     match element {} in selector \
                     path [{}] in file: {}",
                    next.1,
                    sl,
                    self.root.to_string_lossy(),
                ),
                error::ErrorType::NoSuchSymbol,
                next.0.clone(),
            )));
        }
        Ok(())
    }

    fn lookup_in_list(
        &self,
        stack: &mut VecDeque<Rc<Val>>,
        sl: &SelectorList,
        next: (&Position, &str),
        elems: &Vec<Rc<Val>>,
    ) -> Result<(), Box<Error>> {
        let idx = try!(next.1.parse::<usize>());
        if idx < elems.len() {
            stack.push_back(elems[idx].clone());
        } else {
            return Err(Box::new(error::Error::new(
                format!(
                    "Unable to \
                     match element {} in selector \
                     path [{}] in file: {}",
                    next.1,
                    sl,
                    self.root.to_string_lossy(),
                ),
                error::ErrorType::NoSuchSymbol,
                next.0.clone(),
            )));
        }
        Ok(())
    }

    fn lookup_selector(&self, sl: &SelectorList) -> Result<Rc<Val>, Box<Error>> {
        let first = try!(self.eval_expr(&sl.head));
        // First we ensure that the result is a tuple or a list.
        let mut stack = VecDeque::new();
        match first.as_ref() {
            &Val::Tuple(_) => {
                stack.push_back(first.clone());
            }
            &Val::List(_) => {
                stack.push_back(first.clone());
            }
            _ => {
                // noop
            }
        }

        if let &Some(ref tail) = &sl.tail {
            if tail.len() == 0 {
                return Ok(first);
            }
            let mut it = tail.iter().peekable();
            loop {
                let vref = stack.pop_front().unwrap();
                if it.peek().is_none() {
                    return Ok(vref.clone());
                }
                // This unwrap is safe because we already checked for
                // None above.
                let next = it.next().unwrap();
                match vref.as_ref() {
                    &Val::Tuple(ref fs) => {
                        try!(self.lookup_in_tuple(&mut stack, sl, (&next.pos, &next.fragment), fs));
                        continue;
                    }
                    &Val::List(ref elems) => {
                        try!(self.lookup_in_list(
                            &mut stack,
                            sl,
                            (&next.pos, &next.fragment),
                            elems
                        ));
                        continue;
                    }
                    _ => {
                        return Err(Box::new(error::Error::new(
                            format!("{} is not a Tuple or List", vref),
                            error::ErrorType::TypeFail,
                            next.pos.clone(),
                        )));
                    }
                }
            }
        } else {
            return Ok(first);
        }
    }

    fn add_vals(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        match *left {
            Val::Int(i) => {
                eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i + ii), "Integer")
            }
            Val::Float(f) => {
                eval_binary_expr!(&Val::Float(ff), pos, right, Val::Float(f + ff), "Float")
            }
            Val::Str(ref s) => match right.as_ref() {
                &Val::Str(ref ss) => {
                    return Ok(Rc::new(Val::Str([s.to_string(), ss.clone()].concat())))
                }
                val => {
                    return Err(Box::new(error::Error::new(
                        format!(
                            "Expected \
                             String \
                             but got \
                             {:?}",
                            val
                        ),
                        error::ErrorType::TypeFail,
                        pos.clone(),
                    )))
                }
            },
            Val::List(ref l) => match right.as_ref() {
                &Val::List(ref r) => {
                    let mut new_vec = Vec::new();
                    new_vec.extend(l.iter().cloned());
                    new_vec.extend(r.iter().cloned());
                    return Ok(Rc::new(Val::List(new_vec)));
                }
                val => {
                    return Err(Box::new(error::Error::new(
                        format!(
                            "Expected \
                             List \
                             but got \
                             {:?}",
                            val
                        ),
                        error::ErrorType::TypeFail,
                        pos.clone(),
                    )))
                }
            },
            ref expr => {
                return Err(Box::new(error::Error::new(
                    format!("{} does not support the '+' operation", expr.type_name()),
                    error::ErrorType::Unsupported,
                    pos.clone(),
                )))
            }
        }
    }

    fn subtract_vals(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        match *left {
            Val::Int(i) => {
                eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i - ii), "Integer")
            }
            Val::Float(f) => {
                eval_binary_expr!(&Val::Float(ff), pos, right, Val::Float(f - ff), "Float")
            }
            ref expr => {
                return Err(Box::new(error::Error::new(
                    format!("{} does not support the '-' operation", expr.type_name()),
                    error::ErrorType::Unsupported,
                    pos.clone(),
                )))
            }
        }
    }

    fn multiply_vals(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        match *left {
            Val::Int(i) => {
                eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i * ii), "Integer")
            }
            Val::Float(f) => {
                eval_binary_expr!(&Val::Float(ff), pos, right, Val::Float(f * ff), "Float")
            }
            ref expr => {
                return Err(Box::new(error::Error::new(
                    format!("{} does not support the '*' operation", expr.type_name()),
                    error::ErrorType::Unsupported,
                    pos.clone(),
                )))
            }
        }
    }

    fn divide_vals(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        match *left {
            Val::Int(i) => {
                eval_binary_expr!(&Val::Int(ii), pos, right, Val::Int(i / ii), "Integer")
            }
            Val::Float(f) => {
                eval_binary_expr!(&Val::Float(ff), pos, right, Val::Float(f / ff), "Float")
            }
            ref expr => {
                return Err(Box::new(error::Error::new(
                    format!("{} does not support the '*' operation", expr.type_name()),
                    error::ErrorType::Unsupported,
                    pos.clone(),
                )))
            }
        }
    }

    fn do_deep_equal(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        Ok(Rc::new(Val::Boolean(try!(
            left.equal(right.as_ref(), &self.root.to_string_lossy(), pos.clone())
        ))))
    }

    fn do_not_deep_equal(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        Ok(Rc::new(Val::Boolean(!try!(
            left.equal(right.as_ref(), &self.root.to_string_lossy(), pos.clone())
        ))))
    }

    fn do_gt(&self, pos: &Position, left: Rc<Val>, right: Rc<Val>) -> Result<Rc<Val>, Box<Error>> {
        // first ensure that left and right are numeric vals of the same type.
        if let &Val::Int(ref l) = left.as_ref() {
            if let &Val::Int(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l > r)));
            }
        }
        if let &Val::Float(ref l) = left.as_ref() {
            if let &Val::Float(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l > r)));
            }
        }
        Err(Box::new(error::Error::new(
            format!(
                "Incompatible types for numeric comparison {} with {}",
                left.type_name(),
                right.type_name()
            ),
            error::ErrorType::TypeFail,
            pos.clone(),
        )))
    }

    fn do_lt(&self, pos: &Position, left: Rc<Val>, right: Rc<Val>) -> Result<Rc<Val>, Box<Error>> {
        // first ensure that left and right are numeric vals of the same type.
        if let &Val::Int(ref l) = left.as_ref() {
            if let &Val::Int(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l < r)));
            }
        }
        if let &Val::Float(ref l) = left.as_ref() {
            if let &Val::Float(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l < r)));
            }
        }
        Err(Box::new(error::Error::new(
            format!(
                "Incompatible types for numeric comparison {} with {}",
                left.type_name(),
                right.type_name()
            ),
            error::ErrorType::TypeFail,
            pos.clone(),
        )))
    }

    fn do_ltequal(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        if let &Val::Int(ref l) = left.as_ref() {
            if let &Val::Int(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l <= r)));
            }
        }
        if let &Val::Float(ref l) = left.as_ref() {
            if let &Val::Float(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l <= r)));
            }
        }
        Err(Box::new(error::Error::new(
            format!(
                "Incompatible types for numeric comparison {} with {}",
                left.type_name(),
                right.type_name()
            ),
            error::ErrorType::TypeFail,
            pos.clone(),
        )))
    }

    fn do_gtequal(
        &self,
        pos: &Position,
        left: Rc<Val>,
        right: Rc<Val>,
    ) -> Result<Rc<Val>, Box<Error>> {
        if let &Val::Int(ref l) = left.as_ref() {
            if let &Val::Int(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l >= r)));
            }
        }
        if let &Val::Float(ref l) = left.as_ref() {
            if let &Val::Float(ref r) = right.as_ref() {
                return Ok(Rc::new(Val::Boolean(l >= r)));
            }
        }
        Err(Box::new(error::Error::new(
            format!(
                "Incompatible types for numeric comparison {} with {}",
                left.type_name(),
                right.type_name()
            ),
            error::ErrorType::TypeFail,
            pos.clone(),
        )))
    }

    fn eval_binary(&self, def: &BinaryOpDef) -> Result<Rc<Val>, Box<Error>> {
        let kind = &def.kind;
        let left = try!(self.eval_expr(&def.left));
        let right = try!(self.eval_expr(&def.right));
        match kind {
            &BinaryExprType::Add => self.add_vals(&def.pos, left, right),
            &BinaryExprType::Sub => self.subtract_vals(&def.pos, left, right),
            &BinaryExprType::Mul => self.multiply_vals(&def.pos, left, right),
            &BinaryExprType::Div => self.divide_vals(&def.pos, left, right),
        }
    }

    fn eval_compare(&self, def: &ComparisonDef) -> Result<Rc<Val>, Box<Error>> {
        let kind = &def.kind;
        let left = try!(self.eval_expr(&def.left));
        let right = try!(self.eval_expr(&def.right));
        match kind {
            &CompareType::Equal => self.do_deep_equal(&def.pos, left, right),
            &CompareType::GT => self.do_gt(&def.pos, left, right),
            &CompareType::LT => self.do_lt(&def.pos, left, right),
            &CompareType::GTEqual => self.do_gtequal(&def.pos, left, right),
            &CompareType::LTEqual => self.do_ltequal(&def.pos, left, right),
            &CompareType::NotEqual => self.do_not_deep_equal(&def.pos, left, right),
        }
    }

    fn eval_copy(&self, def: &CopyDef) -> Result<Rc<Val>, Box<Error>> {
        let v = try!(self.lookup_selector(&def.selector.sel));
        if let Val::Tuple(ref src_fields) = *v {
            let mut m = HashMap::<Positioned<String>, (i32, Rc<Val>)>::new();
            // loop through fields and build  up a hashmap
            let mut count = 0;
            for &(ref key, ref val) in src_fields.iter() {
                if let Entry::Vacant(v) = m.entry(key.clone()) {
                    v.insert((count, val.clone()));
                    count += 1;
                } else {
                    return Err(Box::new(error::Error::new(
                        format!(
                            "Duplicate \
                             field: {} in \
                             tuple",
                            key.val
                        ),
                        error::ErrorType::TypeFail,
                        key.pos.clone(),
                    )));
                }
            }
            for &(ref key, ref val) in def.fields.iter() {
                let expr_result = try!(self.eval_expr(val));
                match m.entry(key.into()) {
                    // brand new field here.
                    Entry::Vacant(v) => {
                        v.insert((count, expr_result));
                        count += 1;
                    }
                    Entry::Occupied(mut v) => {
                        // overriding field here.
                        // Ensure that the new type matches the old type.
                        let src_val = v.get().clone();
                        if src_val.1.type_equal(&expr_result) {
                            v.insert((src_val.0, expr_result));
                        } else {
                            return Err(Box::new(error::Error::new(
                                format!(
                                    "Expected type {} for field {} but got {}",
                                    src_val.1.type_name(),
                                    key.fragment,
                                    expr_result.type_name()
                                ),
                                error::ErrorType::TypeFail,
                                key.pos.clone(),
                            )));
                        }
                    }
                };
            }
            let mut new_fields: Vec<(Positioned<String>, (i32, Rc<Val>))> = m.drain().collect();
            // We want to maintain our order for the fields to make comparing tuples
            // easier in later code. So we sort by the field order before constructing a new tuple.
            new_fields.sort_by(|a, b| {
                let ta = a.1.clone();
                let tb = b.1.clone();
                ta.0.cmp(&tb.0)
            });
            return Ok(Rc::new(Val::Tuple(
                new_fields
                    .iter()
                    .map(|a| {
                        let first = a.0.clone();
                        let t = a.1.clone();
                        (first, t.1)
                    })
                    .collect(),
            )));
        }
        Err(Box::new(error::Error::new(
            format!("Expected Tuple got {}", v),
            error::ErrorType::TypeFail,
            def.selector.pos.clone(),
        )))
    }

    fn eval_format(&self, def: &FormatDef) -> Result<Rc<Val>, Box<Error>> {
        let tmpl = &def.template;
        let args = &def.args;
        let mut vals = Vec::new();
        for v in args.iter() {
            let rcv = try!(self.eval_expr(v));
            vals.push(rcv.deref().clone());
        }
        let formatter = format::Formatter::new(tmpl.clone(), vals);
        Ok(Rc::new(Val::Str(try!(formatter.render(&def.pos)))))
    }

    fn eval_call(&self, def: &CallDef) -> Result<Rc<Val>, Box<Error>> {
        let sel = &def.macroref;
        let args = &def.arglist;
        let v = try!(self.lookup_selector(&sel.sel));
        if let &Val::Macro(ref m) = v.deref() {
            // Congratulations this is actually a macro.
            let mut argvals: Vec<Rc<Val>> = Vec::new();
            for arg in args.iter() {
                argvals.push(try!(self.eval_expr(arg)));
            }
            let fields = try!(m.eval(self.root.clone(), self.env.clone(), argvals));
            return Ok(Rc::new(Val::Tuple(fields)));
        }
        Err(Box::new(error::Error::new(
            // We should pretty print the selectors here.
            format!("{} is not a Macro", v),
            error::ErrorType::TypeFail,
            def.pos.clone(),
        )))
    }

    fn eval_macro_def(&self, def: &MacroDef) -> Result<Rc<Val>, Box<Error>> {
        match def.validate_symbols() {
            Ok(()) => Ok(Rc::new(Val::Macro(def.clone()))),
            Err(set) => Err(Box::new(error::Error::new(
                format!(
                    "Macro has the following \
                     undefined symbols: {:?}",
                    set
                ),
                error::ErrorType::NoSuchSymbol,
                def.pos.clone(),
            ))),
        }
    }

    fn eval_select(&self, def: &SelectDef) -> Result<Rc<Val>, Box<Error>> {
        let target = &def.val;
        let def_expr = &def.default;
        let fields = &def.tuple;
        // First resolve the target expression.
        let v = try!(self.eval_expr(target));
        // Second ensure that the expression resolves to a string.
        if let &Val::Str(ref name) = v.deref() {
            // Third find the field with that name in the tuple.
            for &(ref fname, ref val_expr) in fields.iter() {
                if &fname.fragment == name {
                    // Fourth return the result of evaluating that field.
                    return self.eval_expr(val_expr);
                }
            }
            // Otherwise return the default.
            return self.eval_expr(def_expr);
        } else {
            return Err(Box::new(error::Error::new(
                format!(
                    "Expected String but got \
                     {} in Select expression",
                    v.type_name()
                ),
                error::ErrorType::TypeFail,
                def.pos.clone(),
            )));
        }
    }

    fn eval_list_op(&self, def: &ListOpDef) -> Result<Rc<Val>, Box<Error>> {
        let l = &def.target.elems;
        let mac = &def.mac;
        if let &Val::Macro(ref macdef) = try!(self.lookup_selector(&mac.sel)).as_ref() {
            let mut out = Vec::new();
            for expr in l.iter() {
                let argvals = vec![try!(self.eval_expr(expr))];
                let fields = try!(macdef.eval(self.root.clone(), self.env.clone(), argvals));
                if let Some(v) = Self::find_in_fieldlist(&def.field, &fields) {
                    match def.typ {
                        ListOpType::Map => {
                            out.push(v.clone());
                        }
                        ListOpType::Filter => {
                            if let &Val::Empty = v.as_ref() {
                                // noop
                                continue;
                            }
                            out.push(v.clone());
                        }
                    }
                }
            }
            return Ok(Rc::new(Val::List(out)));
        }
        return Err(Box::new(error::Error::new(
            format!("Expected macro but got {:?}", mac),
            error::ErrorType::TypeFail,
            def.pos.clone(),
        )));
    }

    fn build_assert(&mut self, tok: &Token) -> Result<Rc<Val>, Box<Error>> {
        if !self.validate_mode {
            // we are not in validate_mode then build_asserts are noops.
            return Ok(Rc::new(Val::Empty));
        }
        // FIXME(jwall): We need to append a semicolon to the expr.
        let mut expr_as_stmt = String::new();
        let expr = &tok.fragment;
        expr_as_stmt.push_str(expr);
        expr_as_stmt.push_str(";");
        let ok = match self.eval_string(&expr_as_stmt) {
            Ok(v) => v,
            Err(e) => {
                return Err(Box::new(error::Error::new(
                    format!("Assertion Evaluation of [{}] failed: {}", expr, e),
                    error::ErrorType::AssertError,
                    tok.pos.clone(),
                )));
            }
        };

        if let &Val::Boolean(b) = ok.as_ref() {
            // record the assertion result.
            if b {
                // success!
                let msg = format!(
                    "OK - '{}' at line: {} column: {}\n",
                    expr, tok.pos.line, tok.pos.column
                );
                self.assert_collector.summary.push_str(&msg);
            } else {
                // failure!
                let msg = format!(
                    "NOT OK - '{}' at line: {} column: {}\n",
                    expr, tok.pos.line, tok.pos.column
                );
                self.assert_collector.summary.push_str(&msg);
                self.assert_collector.failures.push_str(&msg);
                self.assert_collector.success = false;
            }
        } else {
            // record an assertion type-failure result.
            let msg = format!(
                "TYPE FAIL - '{}' at line: {} column: {}\n",
                expr, tok.pos.line, tok.pos.column
            );
            self.assert_collector.summary.push_str(&msg);
        }
        Ok(ok)
    }

    // Evals a single Expression in the context of a running Builder.
    // It does not mutate the builders collected state at all.
    pub fn eval_expr(&self, expr: &Expression) -> Result<Rc<Val>, Box<Error>> {
        match expr {
            &Expression::Simple(ref val) => self.value_to_val(val),
            &Expression::Binary(ref def) => self.eval_binary(def),
            &Expression::Compare(ref def) => self.eval_compare(def),
            &Expression::Copy(ref def) => self.eval_copy(def),
            &Expression::Grouped(ref expr) => self.eval_expr(expr),
            &Expression::Format(ref def) => self.eval_format(def),
            &Expression::Call(ref def) => self.eval_call(def),
            &Expression::Macro(ref def) => self.eval_macro_def(def),
            &Expression::Select(ref def) => self.eval_select(def),
            &Expression::ListOp(ref def) => self.eval_list_op(def),
        }
    }
}

#[cfg(test)]
mod compile_test;

#[cfg(test)]
mod test;