ldpl 0.1.0

LDPL 4.4 compiler in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
//! The Compiler generates a String of C++ code from parsed LDPL code.

use crate::{
    parser::{LDPLParser, Parser, Rule},
    LDPLResult, LDPLType, LPM_LOCATION,
};
use pest::iterators::{Pair, Pairs};
use std::{
    collections::HashMap,
    fmt,
    sync::atomic::{AtomicUsize, Ordering},
};

////
// CONSTANTS

/// Track indentation depth.
static DEPTH: AtomicUsize = AtomicUsize::new(0);

/// Include LDPL C++ internal functions in our output.
const CPP_HEADER: &'static str = include_str!("../lib/ldpl_header.cpp");

/// Setup the C++ main() function
const MAIN_HEADER: &'static str = r#"
int main(int argc, char* argv[]) {
    cout.precision(numeric_limits<ldpl_number>::digits10);
    for(int i = 1; i < argc; ++i) VAR_ARGV.inner_collection.push_back(argv[i]);

"#;
const MAIN_FOOTER: &'static str = r#"
    return 0;
}
"#;

////
// DATA

/// State of our LDPL program, including variables and defined
/// sub-procedures. Eventually we'll move this into a Parser so we can
/// have multiple emitters (for different languages).
#[derive(Default)]
pub struct Compiler {
    /// Body of the the main() function. _HEADER and _FOOTER get
    /// inserted automatically when we're done.
    pub main: Vec<String>,

    /// Sub-procedure definitions.
    pub subs: Vec<String>,

    /// Global variable declarations.
    pub vars: Vec<String>,

    /// C++ Extension files to include when building.
    /// Add with `add_extension()`.
    pub exts: Vec<String>,

    /// Compiler flags to build with.
    pub flags: Vec<String>,

    /// Forward function declarations.
    forwards: Vec<String>,

    /// EXTERNAL variables
    extern_vars: HashMap<String, bool>,

    /// Global variables defined in the DATA: section. Used for error
    /// checking.
    globals: HashMap<String, LDPLType>,

    /// Local variables, re-defined for each sub-procedure.
    locals: HashMap<String, LDPLType>,

    /// Sub definitions. name => params
    defs: HashMap<String, Vec<LDPLType>>,

    /// Path of the file we're currently compiling, if any.
    path: Option<String>,

    /// When a sub is called before it's defined, we stick it in this
    /// list. When it's defined we remove it from the list. If the
    /// list isn't empty when we're done, we have an error.
    pub expected_defs: HashMap<String, bool>,

    /// User-defined statements created with CREATE STATEMENT.
    /// The same statement can reference multiple SUBs based on the
    /// param types, so we use a vec.
    user_stmts: HashMap<String, Vec<String>>,

    // in a sub-procedure? RETURN doesn't work outside of one.
    in_sub: bool,

    // in a loop? BREAK/CONTINUE only work in loops. Vec for nesting.
    in_loop: Vec<bool>,

    // counter for tmp variables
    tmp_id: usize,
}

////
// MACROS

/// Call when an unexpected Pair/Rule is encountered.
macro_rules! unexpected {
    ($rule:expr) => {
        return error!("Unexpected rule: {:?}", $rule);
    };
}

/// Produce a single line with indentation. Used to build multi-line responses.
macro_rules! emit_line {
    ($msg:expr) => {
        format!("{}{}\n", "    ".repeat(DEPTH.load(Ordering::SeqCst)), $msg)
    };
    ($fmt:expr, $($args:expr),*) => {
        emit_line!(format!($fmt, $($args),*))
    };
    ($fmt:expr, $($args:expr,)*) => {
        emit_line!(format!($fmt, $($args,)*))
    };
}

/// Produce a line of code at the current indentation level with a
/// trailing newline.
macro_rules! emit {
    ($msg:expr) => {
        Ok(emit_line!($msg))
    };
    ($fmt:expr, $($args:expr),*) => {
        emit!(format!($fmt, $($args),*))
    };
    ($fmt:expr, $($args:expr,)*) => {
        emit!(format!($fmt, $($args,)*))
    };
}

/// Increase indentation level (depth)
macro_rules! indent {
    () => {
        DEPTH.fetch_add(1, Ordering::SeqCst);
    };
}

/// Decrease indentation level
macro_rules! dedent {
    () => {
        if DEPTH.load(Ordering::SeqCst) > 0 {
            DEPTH.fetch_sub(1, Ordering::SeqCst);
        }
    };
}

////
// FUNCTIONS

/// Turns a string of LDPL code into C++ code.
pub fn compile(code: &str) -> LDPLResult<Compiler> {
    let mut compiler = Compiler::default();
    compiler.compile(code)?;
    Ok(compiler)
}

/// Turns parsed LDPL code into C++ code.
pub fn compile_ast(ast: Pairs<Rule>) -> LDPLResult<Compiler> {
    let mut compiler = Compiler::default();
    compiler.compile_ast(ast)?;
    Ok(compiler)
}

/// Turns LDPL code on disk into C++ code.
pub fn load_and_compile(path: &str) -> LDPLResult<Compiler> {
    let mut compiler = Compiler::default();
    compiler.load_and_compile(path)?;
    Ok(compiler)
}

/// Create a fresh compiler.
pub fn new() -> Compiler {
    Compiler::default()
}

/// Treating the compiler as a string produces the compiled C++.
impl fmt::Display for Compiler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}{}{}{}{}{}",
            CPP_HEADER,
            self.forwards.join(""),
            self.vars.join("\n"),
            self.subs.join(""),
            MAIN_HEADER,
            self.main.join(""),
            MAIN_FOOTER
        )
    }
}

impl Compiler {
    /// Add a C++ file to include when building.
    pub fn add_extension(&mut self, ext_file: String) -> LDPLResult<()> {
        self.exts.push(ext_file);
        Ok(())
    }

    /// Add a C++ flag to include when building.
    pub fn add_flag(&mut self, flag: String) -> LDPLResult<()> {
        self.flags.push(flag);
        Ok(())
    }

    /// Load a file from disk, parse it, and generate C++ code.
    pub fn load_and_compile(&mut self, path: &str) -> LDPLResult<()> {
        // info!("Loading {}", path);
        let old_path = self.path.clone();
        self.path = Some(path.to_string());

        let source =
            std::fs::read_to_string(&path).map_err(|err| Err(format!("{}: {}", path, err)))?;
        // info!("Parsing {}", path);
        let out = self.compile(&source);
        self.path = old_path;
        out
    }

    /// Turns a string of LDPL code into C++ code.
    pub fn compile(&mut self, source: &str) -> LDPLResult<()> {
        let ast = LDPLParser::parse(Rule::program, &source)?;
        self.compile_ast(ast)
    }

    /// Turns parsed LDPL code into C++ code.
    pub fn compile_ast(&mut self, ast: Pairs<Rule>) -> LDPLResult<()> {
        // Predeclared vars
        if self.globals.is_empty() {
            self.vars.push("ldpl_list<chText> VAR_ARGV;".into());
            self.globals
                .insert("ARGV".into(), LDPLType::List(Box::new(LDPLType::Text)));
            self.globals.insert("ERRORCODE".into(), LDPLType::Number);
            self.globals.insert("ERRORTEXT".into(), LDPLType::Text);
        }

        for pair in ast {
            match pair.as_rule() {
                Rule::header_stmt => self.compile_header(pair)?,
                Rule::data_section => {
                    let data = self.compile_data(pair, false)?;
                    self.vars.push(data);
                }
                Rule::EOI => break,

                Rule::procedure_section => {
                    for proc_stmt in pair.into_inner() {
                        match proc_stmt.as_rule() {
                            Rule::create_stmt_stmt => self.add_user_stmt(proc_stmt)?,
                            Rule::sub_def_stmt => {
                                let sub = self.compile_sub_def_stmt(proc_stmt)?;
                                self.subs.push(sub);
                            }
                            _ => {
                                indent!();
                                let stmt = self.compile_subproc_stmt(proc_stmt)?;
                                self.main.push(stmt);
                                dedent!();
                            }
                        }
                    }
                }

                _ => unexpected!(pair),
            }
        }

        Ok(())
    }

    /// Process INCLUDE, EXTENSION, and FLAGs in the header above
    /// the DATA: and PROCEDURE: sections.
    fn compile_header(&mut self, pair: Pair<Rule>) -> LDPLResult<()> {
        let stmt = pair.into_inner().next().unwrap();
        match stmt.as_rule() {
            Rule::include_stmt => {
                let file = stmt.into_inner().next().unwrap().as_str();
                self.load_and_compile(&self.expand_path(unquote(file)))?;
            }
            Rule::extension_stmt => {
                let ext_file = unquote(stmt.into_inner().next().unwrap().as_str());
                self.add_extension(self.expand_path(ext_file))?;
            }
            Rule::flag_stmt => {
                let flag = unquote(stmt.into_inner().next().unwrap().as_str());
                self.add_flag(flag.into())?;
            }
            Rule::using_stmt => {
                let name = stmt.into_inner().next().unwrap().as_str().to_lowercase();
                let mut path = format!("{}{}/{}.ldpl", LPM_LOCATION, name, name);
                if path.contains('~') {
                    path = path.replace("~", env!("HOME"));
                }
                self.load_and_compile(&path)?;
            }
            _ => unexpected!(stmt),
        }
        Ok(())
    }

    /// Convert `name IS TEXT` into a C++ variable declaration.
    /// Used by DATA: and LOCAL DATA: sections.
    fn compile_data(&mut self, pair: Pair<Rule>, local: bool) -> LDPLResult<String> {
        let mut out = vec![];

        for def in pair.into_inner() {
            let is_extern = def.as_rule() == Rule::external_type_def;

            let mut parts = def.into_inner();
            let ident = parts.next().unwrap().as_str();
            let typename = parts.next().unwrap().as_str();
            let varname = ident.to_uppercase();
            let mut var: String;

            if is_extern {
                self.extern_vars.insert(varname.clone(), true);
                var = format!("extern {} {}", compile_type(typename), mangle_extern(ident));
            } else {
                var = format!("{} {}", compile_type(typename), mangle_var(ident));
                if typename == "number" {
                    var.push_str(" = 0");
                } else if typename == "text" {
                    var.push_str(r#" = """#);
                }
            }

            let ldpltype = LDPLType::from(typename);
            if local {
                if self.locals.contains_key(&varname) {
                    return error!("Duplicate declaration for variable: {}", ident);
                }
                self.locals.insert(varname, ldpltype);
            } else {
                if self.globals.contains_key(&varname) {
                    return error!("Duplicate declaration for variable: {}", ident);
                }
                self.globals.insert(varname, ldpltype);
            };

            var.push(';');
            out.push(emit_line!(var));
        }

        Ok(format!("{}\n", out.join("")))
    }

    /// Convert a param list into a vector of param types and a C++
    /// function signature params list.
    fn compile_params(&mut self, pair: Pair<Rule>) -> LDPLResult<(Vec<LDPLType>, String)> {
        let mut out = vec![];
        let mut types = vec![];

        for def in pair.into_inner() {
            assert!(def.as_rule() == Rule::type_def);
            let mut parts = def.into_inner();
            let ident = parts.next().unwrap().as_str();
            let typename = parts.next().unwrap().as_str();
            let typetype = LDPLType::from(typename);
            types.push(typetype.clone());
            self.locals.insert(ident.to_uppercase(), typetype);
            out.push(format!("{}& {}", compile_type(typename), mangle_var(ident)));
        }

        Ok((types, out.join(", ")))
    }

    /// Function definition.
    fn compile_sub_def_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let mut params = String::new();
        let mut param_types = vec![];
        let mut vars = String::new();
        let mut body: Vec<String> = vec![];
        let mut is_extern = false;
        let ident;

        self.locals.clear();
        self.in_sub = true;
        indent!();

        let first = iter.next().unwrap();
        if first.as_rule() == Rule::external {
            is_extern = true;
            ident = iter.next().unwrap().as_str();
        } else {
            ident = first.as_str();
        }

        let ident_upper = ident.to_uppercase();

        if self.defs.contains_key(&ident_upper) {
            return error!("Redefining existing SUB-PROCEDURE: {}", ident);
        }

        if self.expected_defs.contains_key(&ident_upper) {
            self.expected_defs.remove(&ident_upper);
        }

        let mut node = iter.next().unwrap();

        if node.as_rule() == Rule::sub_param_section {
            let (types, string) = self.compile_params(node)?;
            params = string;
            param_types = types;
            node = iter.next().unwrap();
        }

        if node.as_rule() == Rule::sub_data_section {
            vars = self.compile_data(node, true)?;
            node = iter.next().unwrap();
        }

        // done with the header, register this SUB so we
        // can call it recursively in the body.
        self.defs.insert(ident.to_uppercase(), param_types);

        loop {
            body.push(self.compile_subproc_stmt(node)?);
            let node_opt = iter.next();
            if node_opt.is_none() {
                break;
            } else {
                node = node_opt.unwrap();
            }
        }
        dedent!();
        self.in_sub = false;

        let mangled = if is_extern {
            mangle_extern(ident)
        } else {
            mangle_sub(ident)
        };

        emit!(
            "void {}({}) {{\n{}{}}}\n",
            mangled,
            params,
            vars,
            body.join(""),
        )
    }

    /// Read CREATE STATEMENT and add mapping as a user_stmt
    /// CREATE STATEMENT <text> EXECUTING <ident>
    fn add_user_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<()> {
        let mut iter = pair.into_inner();
        let stmt = unquote(iter.next().unwrap().as_str()).to_uppercase();
        let ident = iter.next().unwrap().as_str().to_uppercase();

        if !self.defs.contains_key(&ident) {
            return error!(
                "CREATE STATEMENT used with unknown sub-procedure: {}",
                ident
            );
        }

        if let Some(subs) = self.user_stmts.get_mut(&stmt) {
            subs.push(ident);
        } else {
            self.user_stmts.insert(stmt, vec![ident]);
        }

        Ok(())
    }

    /// Translate a user-defined STATEMENT into a SUB call.
    fn compile_user_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let iter = pair.into_inner();

        // we can't just take pair.as_str() because that returns the
        // trailing comments. maybe a pest bug?
        let stmt = iter
            .clone()
            .map(|part| part.as_str())
            .collect::<Vec<_>>()
            .join(" ")
            .to_uppercase();

        let types_iter = iter.clone(); // for inferring types of stmt parts
        let call_parts: Vec<_> = stmt.split(" ").map(|p| p.to_uppercase()).collect();
        let mut matched = false;
        let mut sub_name = String::new();

        // args is list of (index, type)
        let mut args: Vec<(usize, LDPLType)> = vec![];

        'outer: for (pattern, subs) in &self.user_stmts {
            let mut def_parts: Vec<_> = pattern.split(" ").collect();

            // don't bother if the patterns aren't the same length
            if def_parts.len() != call_parts.len() {
                continue;
            }

            let mut types_iter = types_iter.clone(); // re-use each loop
                                                     // compare each word in the pattern
            for (i, call_part) in call_parts.iter().enumerate() {
                let node = types_iter.next().unwrap();
                let def_part = def_parts.remove(0); // safe - we checked size
                if def_part == "$" {
                    args.push((i, self.scalar_type_of_expr(node)?.clone()));
                } else if *call_part != def_part {
                    continue 'outer;
                }
            }

            // if we got here, we may have found a match.
            // now we need to compare arity and param types to find
            // the specific sub-procedure to call.
            let call_params: Vec<LDPLType> = args.iter().map(|t| t.1.clone()).collect();
            for sub in subs {
                if let Some(sub_params) = self.defs.get(sub) {
                    if *sub_params == call_params {
                        sub_name = sub.clone();
                        matched = true;
                        break 'outer;
                    }
                }
            }

            // if we're here, we didn't find a match
            return error!(
                "Statement arguments didn't match any sub-procedures: {}",
                stmt
            );
        }

        if matched {
            let iter = iter
                .enumerate()
                .filter(|(i, _rule)| args.iter().any(|(idx, _)| idx == i))
                .map(|(_, rule)| rule);
            let (prefix, args) = self.compile_arg_list(iter)?;
            return Ok(format!(
                "{}\n{}",
                prefix,
                emit_line!("{}({});", mangle_sub(&sub_name), args)
            ));
        }

        error!("Unknown statement: {}", stmt)
    }

    /// Used in CALL and when calling user-defined statements.
    /// Wants either Pairs<Rule> or an Iterator you built of
    /// Rule::expr items exclusively. Like from an expr_list.
    ///
    /// Returns a tuple of (PREFIX, ARGS) where:
    /// - PREFIX is the setup code that should be inserted before a
    ///   function call is made.
    /// - ARGS are the list of args to be used in the function call,
    ///   as prepared by PREFIX.
    fn compile_arg_list<'p, I: Iterator<Item = Pair<'p, Rule>>>(
        &mut self,
        mut iter: I,
    ) -> LDPLResult<(String, String)> {
        let mut prefix = vec![];
        let mut args = vec![];

        while let Some(arg) = iter.next() {
            match arg.as_rule() {
                Rule::number => {
                    let var = format!("LPVAR_{}", self.tmp_id);
                    self.tmp_id += 1;
                    prefix.push(emit_line!(
                        "ldpl_number {} = {};",
                        var,
                        self.compile_expr(arg)?
                    ));
                    args.push(var);
                }
                Rule::text | Rule::linefeed => {
                    let var = format!("LPVAR_{}", self.tmp_id);
                    self.tmp_id += 1;
                    prefix.push(emit_line!("chText {} = {};", var, self.compile_expr(arg)?));
                    args.push(var);
                }
                Rule::var => args.push(self.compile_expr(arg)?),
                _ => unexpected!(arg),
            }
        }

        Ok((prefix.join(""), args.join(", ")))
    }

    /// Emit a stmt from the PROCEDURE: section of a file or function.
    fn compile_subproc_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut out = vec![];

        out.push(match pair.as_rule() {
            // control flow
            Rule::call_stmt => self.compile_call_stmt(pair)?,
            Rule::if_stmt => self.compile_if_stmt(pair)?,
            Rule::else_stmt => return error!("unexpected ELSE statement"),
            Rule::while_stmt => self.compile_while_stmt(pair)?,
            Rule::for_each_stmt => self.compile_for_each_stmt(pair)?,
            Rule::for_stmt => self.compile_for_stmt(pair)?,
            Rule::loop_kw_stmt => self.compile_loop_kw_stmt(pair)?,
            Rule::return_stmt => self.compile_return_stmt(pair)?,
            Rule::goto_stmt => self.compile_goto_stmt(pair)?,
            Rule::label_stmt => self.compile_label_stmt(pair)?,
            Rule::exit_stmt => self.compile_exit_stmt(pair)?,
            Rule::wait_stmt => self.compile_wait_stmt(pair)?,
            Rule::store_quote_stmt => self.compile_store_quote_stmt(pair)?,
            Rule::store_stmt => self.compile_store_stmt(pair)?,

            // math
            Rule::solve_stmt => self.compile_solve_stmt(pair)?,
            Rule::floor_stmt => self.compile_floor_stmt(pair)?,
            Rule::modulo_stmt => self.compile_modulo_stmt(pair)?,

            // text
            Rule::join_stmt => self.compile_join_stmt(pair)?,
            Rule::old_join_stmt => self.compile_old_join_stmt(pair)?,
            Rule::replace_stmt => self.compile_replace_stmt(pair)?,
            Rule::split_stmt => self.compile_split_stmt(pair)?,
            Rule::get_char_stmt => self.compile_get_char_stmt(pair)?,
            Rule::get_ascii_stmt => self.compile_get_ascii_stmt(pair)?,
            Rule::get_char_code_stmt => self.compile_get_char_code_stmt(pair)?,
            Rule::get_index_stmt => self.compile_get_index_stmt(pair)?,
            Rule::count_stmt => self.compile_count_stmt(pair)?,
            Rule::substr_stmt => self.compile_substring_stmt(pair)?,
            Rule::trim_stmt => self.compile_trim_stmt(pair)?,

            // list
            Rule::push_stmt => self.compile_push_stmt(pair)?,
            Rule::delete_stmt => self.compile_delete_stmt(pair)?,

            // map
            Rule::get_keys_count_stmt => self.compile_get_keys_count_stmt(pair)?,
            Rule::get_keys_stmt => self.compile_get_keys_stmt(pair)?,

            // list + map
            Rule::clear_stmt => self.compile_clear_stmt(pair)?,
            Rule::copy_stmt => self.compile_copy_stmt(pair)?,

            // list + text
            Rule::get_length_stmt => self.compile_get_length_stmt(pair)?,

            // io
            Rule::display_stmt => self.compile_display_stmt(pair)?,
            Rule::load_stmt => self.compile_load_stmt(pair)?,
            Rule::write_stmt => self.compile_write_stmt(pair)?,
            Rule::append_stmt => self.compile_append_stmt(pair)?,
            Rule::accept_stmt => self.compile_accept_stmt(pair)?,
            Rule::execute_stmt => self.compile_execute_stmt(pair)?,

            // user-defined statement (made via CREATE STATEMENT)
            Rule::user_stmt => self.compile_user_stmt(pair)?,

            _ => unexpected!(pair),
        });

        Ok(out.join(""))
    }

    ////
    // CONTROL FLOW

    /// STORE _ IN _
    fn compile_store_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();

        let expr = iter.next().unwrap();
        let var = iter.next().unwrap();
        let val = self.compile_expr_for_type(expr, self.type_of_var(var.clone())?)?;

        emit!("{} = {};", self.compile_var(var)?, val)
    }

    /// STORE QUOTE IN _
    fn compile_store_quote_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let var = self.compile_var(iter.next().unwrap())?;
        let txt = iter.next().unwrap().as_str();
        // remove extra preceeding \n from txt. parser limitation.
        if !txt.is_empty() {
            emit!(
                r#"{} = "{}";"#,
                var,
                &txt[1..].replace("\n", "\\\n\\n").replace("\"", "\\\"")
            )
        } else {
            emit!("{} = \"\";", var)
        }
    }

    /// RETURN
    fn compile_return_stmt(&self, _pair: Pair<Rule>) -> LDPLResult<String> {
        if !self.in_sub {
            return error!("RETURN can't be used outside of SUB-PROCEDURE");
        }
        emit!("return;")
    }

    /// BREAK / CONTINUE
    fn compile_loop_kw_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        if self.in_loop.is_empty() {
            return error!("{} can't be used without FOR/WHILE loop", pair.as_str());
        }
        emit!("{};", pair.as_str())
    }

    /// GOTO _
    fn compile_goto_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let label = pair.into_inner().next().unwrap();
        emit!("goto label_{};", mangle(label.as_str()))
    }

    /// LABEL _
    fn compile_label_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let label = pair.into_inner().next().unwrap();
        // no indenting
        Ok(format!("label_{}:\n", mangle(label.as_str())))
    }

    /// WAIT _ MILLISECONDS
    fn compile_wait_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let count = self.compile_expr(pair.into_inner().next().unwrap())?;
        emit!(
            "std::this_thread::sleep_for(std::chrono::milliseconds((long int){}));",
            count
        )
    }

    /// EXIT
    fn compile_exit_stmt(&self, _pair: Pair<Rule>) -> LDPLResult<String> {
        emit!("exit(0);")
    }

    /// CALL _ WITH _ ...
    fn compile_call_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let call_stmt = pair.into_inner().next().unwrap();
        let is_extern = call_stmt.as_rule() == Rule::call_external_stmt;
        let mut iter = call_stmt.into_inner();
        let ident = iter.next().unwrap().as_str();

        if !is_extern && !self.defs.contains_key(&ident.to_uppercase()) {
            self.expected_defs.insert(ident.to_uppercase(), true);
        }

        let (prefix, params) = self.compile_arg_list(iter)?;

        let mangled = if is_extern {
            mangle_extern(ident)
        } else {
            mangle_sub(ident)
        };

        let fwd_decl = format!("void {}();", mangled);
        if !self.forwards.contains(&fwd_decl) {
            self.forwards.push(format!("void {}();\n", mangled));
        }

        Ok(format!(
            "{}{}",
            prefix,
            emit_line!("{}({});", mangled, params)
        ))
    }

    /// IF and WHILE test statement
    fn compile_test_stmt(&self, test: Pair<Rule>) -> LDPLResult<String> {
        let mut out = vec![];
        match test.as_rule() {
            Rule::test_expr => {
                for t in test.into_inner() {
                    out.push(self.compile_test_stmt(t)?);
                }
            }
            Rule::or_test_expr => {
                let mut iter = test.into_inner();
                let left = self.compile_test_stmt(iter.next().unwrap())?;
                let right = self.compile_test_stmt(iter.next().unwrap())?;
                out.push(format!("({} || {})", left, right));
            }
            Rule::and_test_expr => {
                let mut iter = test.into_inner();
                let left = self.compile_test_stmt(iter.next().unwrap())?;
                let right = self.compile_test_stmt(iter.next().unwrap())?;
                out.push(format!("({} && {})", left, right));
            }
            Rule::one_test_expr => out.push(self.compile_test_expr(test)?),
            _ => unexpected!(test),
        }
        Ok(out.join(" "))
    }

    /// Single test expression. Use _stmt for expressions with OR / AND.
    fn compile_test_expr(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let left = self.compile_expr(iter.next().unwrap())?;
        let mid = iter.next().unwrap();
        let sign = match mid.as_rule() {
            Rule::equal_expr => "==",
            Rule::not_equal_expr => "!=",
            Rule::gt_expr => ">",
            Rule::lt_expr => "<",
            Rule::gte_expr => ">=",
            Rule::lte_expr => "<=",
            _ => unexpected!(mid),
        };
        let right = self.compile_expr(iter.next().unwrap())?;
        Ok(format!("({} {} {})", left, sign, right))
    }

    /// Coerce Number -> Text and Text -> Number.
    fn compile_expr_for_type(&self, expr: Pair<Rule>, typename: &LDPLType) -> LDPLResult<String> {
        let expr_type = self.type_of_expr(expr.clone())?;

        if typename.is_text() && expr.as_rule() == Rule::number {
            // 45 => "45"
            Ok(format!(r#""{}""#, self.compile_expr(expr)?))
        } else if typename.is_number() && (expr_type.is_text() || expr_type.is_text_collection()) {
            // "123" => to_number("123")
            Ok(format!("to_number({})", self.compile_expr(expr)?))
        } else if typename.is_text() && (expr_type.is_number() || expr_type.is_number_collection())
        {
            // txt_var => to_ldpl_string(txt_var)
            Ok(format!("to_ldpl_string({})", self.compile_expr(expr)?))
        } else {
            self.compile_expr(expr)
        }
    }

    /// Variable, Number, or Text
    fn compile_expr(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        Ok(match pair.as_rule() {
            Rule::var => self.compile_var(pair)?,
            Rule::number => self.compile_number(pair)?,
            Rule::text => pair.as_str().to_string(),
            Rule::linefeed => "\"\\n\"".to_string(),
            Rule::ident => self.mangle_var(pair.as_str()),
            _ => return error!("UNIMPLEMENTED: {:?}", pair),
        })
    }

    /// Normalize a number literal.
    /// Ex: -000.0 => 0
    fn compile_number(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let num = pair.as_str();
        if let Ok(parsed) = num.parse::<f64>() {
            Ok(parsed.to_string())
        } else {
            error!("Can't parse number: {}", num)
        }
    }

    /// Turn an ident/lookup pair into a C++ friendly ident.
    fn compile_var(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        assert!(
            pair.as_rule() == Rule::var,
            "Expected Rule::var, got {:?}",
            pair.as_rule()
        );

        let inner = pair.into_inner().next().unwrap();
        match inner.as_rule() {
            Rule::ident => Ok(self.mangle_var(inner.as_str())),
            Rule::lookup => self.compile_lookup_from_iter(inner.into_inner()),
            _ => unexpected!(inner),
        }
    }

    /// Expects the iterator from an IDENT.into_inner() call.
    /// Knows the difference between a:b:1 where b is a container
    /// (a[b[1]]) and where b is a scalar (a[b][1]).
    fn compile_lookup_from_iter(&self, mut iter: Pairs<Rule>) -> LDPLResult<String> {
        let basevar = iter.next().unwrap();
        let mut parts = vec![self.compile_expr(basevar)?];
        let mut copy = iter.clone();
        while let Some(part) = iter.next() {
            // If it's an ident AND a variable AND a
            // container, then end this lookup and nest the
            // new one
            if part.as_rule() == Rule::ident {
                if let Ok(t) = self.type_of_var(part.clone()) {
                    if t.is_collection() {
                        parts.push(format!("[{}]", self.compile_lookup_from_iter(copy)?));
                        break;
                    }
                }
            }
            copy.next(); // copy should be 1 step behind iter, to
                         // capture the current variable

            // otherwise just keep adding index operations
            parts.push(format!("[{}]", self.compile_expr(part)?));
        }
        Ok(parts.join(""))
    }

    /// WHILE _ DO / REPEAT
    fn compile_while_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let test = iter.next().unwrap();
        let test = self.compile_test_stmt(test)?;

        self.in_loop.push(true);
        let mut body = vec![];
        indent!();
        for node in iter {
            body.push(self.compile_subproc_stmt(node)?);
        }
        dedent!();
        self.in_loop.pop();

        Ok(format!(
            "{}{}{}",
            emit_line!("while {} {{", test),
            body.join(""),
            emit_line!("}")
        ))
    }

    /// IF _ THEN / END IF
    fn compile_if_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let test = iter.next().unwrap();
        let test = self.compile_test_stmt(test)?;

        let mut body = vec![];
        indent!();
        for node in iter {
            match node.as_rule() {
                Rule::else_stmt => body.push(self.compile_else_stmt(node)?),
                _ => body.push(self.compile_subproc_stmt(node)?),
            }
        }
        dedent!();

        Ok(format!(
            "{}{}{}",
            emit_line!("if {} {{", test),
            body.join(""),
            emit_line!("}")
        ))
    }

    /// ELSE IF _ THEN
    fn compile_else_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();

        let test = if let Some(test_expr) = iter.next() {
            Some(self.compile_test_stmt(test_expr)?)
        } else {
            None
        };

        dedent!();
        let out = if test.is_some() {
            emit!("}} else if {} {{", test.unwrap())
        } else {
            emit!("} else {")
        };
        indent!();
        out
    }

    /// FOR _ IN _ TO _ STEP _ DO / REPEAT
    fn compile_for_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let var = mangle_var(iter.next().unwrap().as_str());
        let from = self.compile_expr(iter.next().unwrap())?;
        let to = self.compile_expr(iter.next().unwrap())?;
        let step = self.compile_expr(iter.next().unwrap())?;

        self.in_loop.push(true);
        indent!();
        let mut body = vec![];
        for node in iter {
            body.push(self.compile_subproc_stmt(node)?);
        }
        dedent!();
        self.in_loop.pop();

        let init = format!("{} = {}", var, from);
        let test = format!(
            "{step} >= 0 ? {var} < {to} : {var} > {to}",
            step = step,
            var = var,
            to = to
        );
        let incr = format!("{} += {}", var, step);

        Ok(format!(
            "{}{}{}",
            emit_line!("for({}; {}; {}) {{", init, test, incr),
            body.join(""),
            emit_line!("}")
        ))
    }

    /// FOR EACH _ IN _ DO / REPEAT
    fn compile_for_each_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let ident = mangle_var(iter.next().unwrap().as_str());
        let collection = iter.next().unwrap();

        let range_var = format!("RVAR_{}", self.tmp_id);
        self.tmp_id += 1;

        let method = if self.type_of_expr(collection.clone())?.is_map() {
            ".second"
        } else {
            ""
        };

        self.in_loop.push(true);
        indent!();
        let mut body = vec![emit_line!("{} = {}{};", ident, range_var, method)];
        for node in iter {
            body.push(self.compile_subproc_stmt(node)?);
        }
        dedent!();
        self.in_loop.pop();

        Ok(format!(
            "{}{}{}",
            emit_line!(
                "for (auto& {} : {}.inner_collection) {{",
                range_var,
                self.compile_expr(collection)?
            ),
            body.join(""),
            emit_line!("}")
        ))
    }

    ////
    // ARITHMETIC

    /// MODULO _ BY _ IN _
    fn compile_modulo_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let base = self.compile_expr(iter.next().unwrap())?;
        let by = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;

        emit!("{} = modulo({}, {});", var, base, by)
    }

    /// FLOOR _
    /// FLOOR _ IN _
    /// TODO: only FLOOR _ in 4.4
    fn compile_floor_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let stmt = pair.into_inner().next().unwrap();
        let rule = stmt.as_rule();
        let mut iter = stmt.into_inner();
        let left = self.compile_expr(iter.next().unwrap())?;
        let mut right = left.clone();
        match rule {
            Rule::floor_in_stmt => right = self.compile_var(iter.next().unwrap())?,
            Rule::floor_mut_stmt => {}
            _ => unexpected!(rule),
        }

        emit!("{} = floor({});", left, right)
    }

    /// IN _ SOLVE X
    fn compile_solve_stmt(&mut self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let ident = iter.next().unwrap();

        emit!(
            "{} = {};",
            self.compile_var(ident)?,
            self.compile_solve_expr(iter.next().unwrap())?
        )
    }

    // Math expression part of a SOLVE statement
    fn compile_solve_expr(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut parts = vec![];

        for part in pair.into_inner() {
            match part.as_rule() {
                Rule::var | Rule::number | Rule::text => parts.push(self.compile_expr(part)?),
                Rule::solve_expr => parts.push(self.compile_solve_expr(part)?),
                Rule::math_op => parts.push(part.as_str().to_string()),
                _ => return error!("unexpected rule: {:?}", part),
            }
        }

        Ok(parts.join(" "))
    }

    ////
    // TEXT

    /// SPLIT _ BY _ IN _
    fn compile_split_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let text = self.compile_expr(iter.next().unwrap())?;
        let splitter = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = utf8_split_list({}, {});", var, text, splitter)
    }

    /// REPLACE _ FROM _ WITH _ IN _
    /// replace_stmt = { ^"REPLACE" ~ expr ~ ^"FROM" ~ expr ~ ^"WITH" ~ expr ~ ^"IN" ~ var }
    fn compile_replace_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let search = self.compile_expr(iter.next().unwrap())?;
        let text = self.compile_expr(iter.next().unwrap())?;
        let replacement = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;

        emit!("{} = str_replace(((chText){}).str_rep(), ((chText){}).str_rep(), ((chText){}).str_rep());",
            var, text, search, replacement)
    }

    /// IN _ JOIN _ _...
    fn compile_join_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let var = self.compile_var(iter.next().unwrap())?;

        let mut out = vec![emit_line!(r#"joinvar = "";"#)];
        for expr in iter {
            out.push(emit_line!(
                "join(joinvar, {}, joinvar);",
                self.compile_expr_for_type(expr, &LDPLType::Text)?
            ));
        }
        out.push(emit_line!("{} = joinvar;", var));

        Ok(format!("{}", out.join("")))
    }

    /// JOIN _ AND _ IN _
    fn compile_old_join_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let left = self.compile_expr_for_type(iter.next().unwrap(), &LDPLType::Text)?;
        let right = self.compile_expr_for_type(iter.next().unwrap(), &LDPLType::Text)?;
        let var = self.compile_var(iter.next().unwrap())?;

        emit!("join({}, {}, {});", left, right, var)
    }

    /// TRIM _ IN _
    fn compile_trim_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = trimCopy({});", var, expr)
    }

    /// COUNT _ FROM _ IN _
    fn compile_count_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let search = self.compile_expr(iter.next().unwrap())?;
        let text = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = utf8Count({}, {});", var, text, search)
    }

    /// SUBSTRING _ FROM _ LENGTH _ IN _
    fn compile_substring_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let text = self.compile_expr(iter.next().unwrap())?;
        let search = self.compile_expr(iter.next().unwrap())?;
        let length = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;

        Ok(format!(
            "{}{}",
            emit_line!("joinvar = {};", text),
            emit_line!("{} = joinvar.substr({}, {});", var, search, length)
        ))
    }

    /// GET INDEX OF _ FROM _ IN _
    fn compile_get_index_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let search = self.compile_expr(iter.next().unwrap())?;
        let text = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = utf8GetIndexOf({}, {});", var, text, search)
    }

    /// GET CHARACTER CODE OF _ IN _
    fn compile_get_char_code_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = get_char_num({});", var, expr)
    }

    /// GET ASCII CHARACTER _ IN _
    fn compile_get_ascii_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let chr = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = (char)({});", var, chr)
    }

    /// GET CHARACTER AT _ FROM _ IN _
    fn compile_get_char_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let at = self.compile_expr(iter.next().unwrap())?;
        let from = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = charat({}, {});", var, from, at)
    }

    ////
    // LIST + TEXT

    // GET LENGTH OF _ IN _
    fn compile_get_length_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = iter.next().unwrap();
        let var = self.compile_var(iter.next().unwrap())?;
        let expr_type = self.type_of_expr(expr.clone())?;
        let expr = self.compile_expr(expr)?;

        if expr_type.is_text() {
            emit!("{} = ((chText){}).size();", var, expr)
        } else if expr_type.is_list() {
            emit!("{} = {}.inner_collection.size();", var, expr)
        } else {
            unexpected!(expr_type)
        }
    }

    ////
    // LIST

    /// PUSH _ TO _
    fn compile_push_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = self.compile_expr(iter.next().unwrap())?;
        let list = self.compile_var(iter.next().unwrap())?;
        emit!("{}.inner_collection.push_back({});", list, expr)
    }

    /// DELETE LAST ELEMENT OF _
    fn compile_delete_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let list = self.compile_var(iter.next().unwrap())?;
        emit!(format!(
            "if({list}.inner_collection.size() > 0) {list}.inner_collection.pop_back();",
            list = list
        ))
    }

    ////
    // MAP

    /// GET KEYS COUNT OF _ IN _
    fn compile_get_keys_count_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let map = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("{} = {}.inner_collection.size();", var, map)
    }

    /// GET KEYS OF _ IN _
    fn compile_get_keys_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let map = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("get_indices({}, {});", var, map)
    }

    ////
    // MAP + LIST

    /// COPY _ TO _
    fn compile_copy_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let from = self.compile_expr(iter.next().unwrap())?;
        let to = self.compile_var(iter.next().unwrap())?;
        emit!("{}.inner_collection = {}.inner_collection;", to, from)
    }

    /// CLEAR _
    fn compile_clear_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let collection = self.compile_var(iter.next().unwrap())?;
        emit!("{}.inner_collection.clear();", collection)
    }

    ////
    // IO

    /// DISPLAY _...
    fn compile_display_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut parts = vec!["cout".to_string()];
        for node in pair.into_inner() {
            parts.push(self.compile_expr(node)?);
        }
        parts.push("flush".into());
        emit!("{};", parts.join(" << "))
    }

    /// ACCEPT _
    /// ACCEPT _ UNTIL EOF
    fn compile_accept_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let stmt = pair.into_inner().next().unwrap();

        let eof = stmt.as_rule() == Rule::accept_eof_stmt;
        let ident = stmt.into_inner().next().unwrap();
        let vartype = self.type_of_var(ident.clone())?;

        let fun = if eof {
            "input_until_eof()"
        } else if vartype.is_text() {
            "input_string()"
        } else if vartype.is_number() {
            "input_number()"
        } else {
            unexpected!(ident);
        };

        emit!("{} = {};", self.compile_var(ident)?, fun)
    }

    /// LOAD FILE _ IN _
    fn compile_load_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let path = self.compile_expr(iter.next().unwrap())?;
        let var = self.compile_var(iter.next().unwrap())?;
        emit!("load_file({}, {});", path, var)
    }

    /// WRITE _ TO FILE _
    fn compile_write_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = self.compile_expr(iter.next().unwrap())?;
        let path = self.compile_expr(iter.next().unwrap())?;

        Ok(format!("{}{}{}",
            emit_line!("file_writing_stream.open(expandHomeDirectory(((chText){}).str_rep()), ios_base::out);", path),
            emit_line!("file_writing_stream << {};", expr),
            emit_line!("file_writing_stream.close();")
        ))
    }

    /// APPEND _ TO FILE _
    fn compile_append_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let mut iter = pair.into_inner();
        let expr = self.compile_expr(iter.next().unwrap())?;
        let path = self.compile_expr(iter.next().unwrap())?;

        Ok(format!("{}{}{}",
            emit_line!("file_writing_stream.open(expandHomeDirectory(((chText){}).str_rep()), ios_base::app);", path),
            emit_line!("file_writing_stream << {};", expr),
            emit_line!("file_writing_stream.close();")
        ))
    }

    /// EXECUTE _
    /// EXECUTE _ AND STORE EXIT CODE IN _
    /// EXECUTE _ AND STORE OUTPUT IN _
    fn compile_execute_stmt(&self, pair: Pair<Rule>) -> LDPLResult<String> {
        let pair = pair.into_inner().next().unwrap();
        let rule = pair.as_rule();
        let mut iter = pair.into_inner();
        match rule {
            Rule::execute_expr_stmt => emit!(
                "system({});",
                self.compile_c_char_array(iter.next().unwrap())?
            ),
            Rule::execute_output_stmt => {
                let expr = self.compile_c_char_array(iter.next().unwrap())?;
                let var = self.compile_var(iter.next().unwrap())?;
                emit!("{} = exec({});", var, expr)
            }
            Rule::execute_exit_code_stmt => {
                let expr = self.compile_c_char_array(iter.next().unwrap())?;
                let var = self.compile_var(iter.next().unwrap())?;
                emit!(
                    "{} = (system({}) >> 8) & 0xff;", //shift wait() val and get lowest 2
                    var,
                    expr
                )
            }
            _ => unexpected!(rule),
        }
    }
}

////
// HELPERS

impl Compiler {
    /// Find the scalar type for an expression. Map(List(Text)) will
    /// just give us Text.
    fn scalar_type_of_expr(&self, expr: Pair<Rule>) -> LDPLResult<LDPLType> {
        if let Ok(t) = self.type_of_expr(expr.clone()) {
            self.scalar_type_of_collection(t.clone())
        } else {
            error!("Can't infer scalar type for expression: {:?}", expr)
        }
    }

    /// Map(List(Text)) => Text
    fn scalar_type_of_collection<'a>(&self, t: LDPLType) -> LDPLResult<LDPLType> {
        Ok(match t {
            LDPLType::List(inner) | LDPLType::Map(inner) => {
                return self.scalar_type_of_collection(*inner);
            }
            _ => t,
        })
    }

    /// Find the type for an expression.
    fn type_of_expr(&self, expr: Pair<Rule>) -> LDPLResult<&LDPLType> {
        match expr.as_rule() {
            Rule::var => self.type_of_var(expr),
            Rule::ident => self.type_of_var(expr),
            Rule::number => Ok(&LDPLType::Number),
            Rule::text | Rule::linefeed => Ok(&LDPLType::Text),
            _ => unexpected!(expr),
        }
    }

    /// Find the LDPLType for a variable, local or global.
    fn type_of_var(&self, var: Pair<Rule>) -> LDPLResult<&LDPLType> {
        match var.as_rule() {
            Rule::var => self.type_of_var(var.into_inner().next().unwrap()),
            Rule::ident => {
                if let Some(t) = self.locals.get(&var.as_str().to_uppercase()) {
                    Ok(t)
                } else if let Some(t) = self.globals.get(&var.as_str().to_uppercase()) {
                    Ok(t)
                } else {
                    error!("No type found for {}", var.as_str())
                }
            }
            Rule::lookup => {
                let mut iter = var.into_inner();
                let base = iter.next().unwrap();
                let out = self.type_of_var(base);
                out
            }
            _ => unexpected!(var),
        }
    }

    /// Expand a relative file path into a full one, based on the
    /// current file we're compiling.
    fn expand_path(&self, file: &str) -> String {
        if let Some(current) = &self.path {
            if let Some(cwd) = std::path::Path::new(current).parent() {
                let root = cwd.to_string_lossy();
                if !root.is_empty() {
                    return format!("{}/{}", root, file);
                }
            }
        }

        file.to_string()
    }

    /// Like the freestanding mangle_var(), but also works with
    /// external variables. Use this when you want to reference a
    /// variable that can be either global, local, or external.
    fn mangle_var(&self, ident: &str) -> String {
        let ident = ident.to_uppercase();
        if self.extern_vars.contains_key(&ident) {
            mangle_extern(&ident)
        } else {
            mangle_var(&ident)
        }
    }

    /// Compile TEXT as a c char array, mostly for EXECUTE and friends.
    fn compile_c_char_array(&self, node: Pair<Rule>) -> LDPLResult<String> {
        Ok(match node.as_rule() {
            Rule::var => format!("{}.str_rep().c_str()", mangle_var(node.as_str())),
            Rule::text => node.as_str().to_string(),
            _ => unexpected!(node),
        })
    }
}

/// LDPL Type => C++ Type
fn compile_type(ldpl_type: &str) -> &str {
    match ldpl_type.to_lowercase().as_ref() {
        "number" => "ldpl_number",
        "number list" => "ldpl_list<ldpl_number>",
        "number map" | "number vector" => "ldpl_map<ldpl_number>",
        "text" => "chText",
        "text list" => "ldpl_list<chText>",
        "text map" | "text vector" => "ldpl_map<chText>",
        _ => "UNKNOWN_TYPE",
    }
}

/// Mangle a variable name for C++.
fn mangle_var(ident: &str) -> String {
    format!("VAR_{}", mangle(ident))
}

/// Mangle a subprocedure name.
fn mangle_sub(ident: &str) -> String {
    format!("SUBPR_{}", mangle(ident))
}

/// Convert an ident to a C++-friendly ident by stripping illegal
/// characters and whatnot.
/// https://docs.ldpl-lang.org/naming/
/// Based on `fix_identifier()` in ldpl.cpp
fn mangle(ident: &str) -> String {
    let mut mangled = String::with_capacity(ident.len() + 10);

    for c in ident.to_uppercase().chars() {
        if c.is_alphanumeric() || c == '_' {
            mangled.push(c);
        } else {
            mangled.push_str(&format!("c{}_", c as u16));
        }
    }

    mangled
}

/// External functions have simpler conversion rules.
/// http://docs.ldpl-lang.org/naming/#external-identifier-naming-schemes
fn mangle_extern(ident: &str) -> String {
    let mut mangled = String::with_capacity(ident.len() + 10);

    for c in ident.to_uppercase().chars() {
        if c.is_alphanumeric() || c == '_' {
            mangled.push(c);
        } else {
            mangled.push('_');
        }
    }

    mangled.to_uppercase()
}

/// Remove "quotes" from a literal text string.
fn unquote(text: &str) -> &str {
    &text[1..text.len() - 1]
}