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
mod expansion;
mod mel_expr;
mod syntax;

/// Count the number of instructions in a [MelExpr].
pub use mel_expr::count_insts;

use crate::{
    optimize,
    types::{Expr, MelExpr, Symbol},
};
use expansion::Evaluator;
use mel_expr::MemoryMap;

/// Module-level aggregate error type. Unifies all parser-type errors.
#[derive(Debug)]
pub enum ParseError<E> {
    /// Error deriving from syntax parsing.
    Syntax(nom::Err<E>),
    /// Error during macro expansion of the AST.
    Expansion(ParseErr),
}

/// A macro definition type.
/// Macros are not part of an [Expr] because they are only defined at the beginning of a program,
/// and cannot be nested.
type Defn = (Symbol, (Vec<Symbol>, Expr));

/// Number of reserved locations on an execution heap, enumerated from 0.
pub const NUM_RESERVED: i32 = 32;

/// Parse a string into the low-level abstract syntax tree, [MelExpr],
/// which can be directly compiled to bytes.
pub fn parse(input: &str) -> Result<MelExpr, ParseError<nom::error::VerboseError<&str>>> {
    // First pass AST
    syntax::root(input)
        .map_err(ParseError::Syntax)
        // Expand AST
        .and_then(|(remaining, (fn_defs, ast))| {
            if !remaining.trim().is_empty() {
                return Err(ParseError::Expansion(ParseErr("leftover stuff".into())));
            }
            //println!("{:?}\n\n{:?}\n", fn_defs, ast);
            let env = expansion::Env::new(fn_defs);
            env.expand_fns(&ast).map_err(ParseError::Expansion)
        })
        // Low-level MelExpr
        .map(|expanded| {
            let expanded = optimize::let_useonce(expanded);
            let mut mem = MemoryMap::new();
            mem.unrolled_to_mel(expanded)
        })
}

pub fn parse_no_optimize(input: &str) -> Result<MelExpr, ParseError<nom::error::VerboseError<&str>>> {
    // First pass AST
    syntax::root(input)
        .map_err(ParseError::Syntax)
        // Expand AST
        .and_then(|(remaining, (fn_defs, ast))| {
            if !remaining.trim().is_empty() {
                return Err(ParseError::Expansion(ParseErr("leftover stuff".into())));
            }
            //println!("{:?}\n\n{:?}\n", fn_defs, ast);
            let env = expansion::Env::new(fn_defs);
            env.expand_fns(&ast).map_err(ParseError::Expansion)
        })
        // Low-level MelExpr
        .map(|expanded| {
            let mut mem = MemoryMap::new();
            mem.unrolled_to_mel(expanded)
        })
}

/// Syntax parser error type. May become may intricate later.
#[derive(Debug, PartialEq, Eq)]
pub struct ParseErr(pub String);

/// Short hand for a Result<_, ParseErr> type given the error string and args.
#[macro_export]
macro_rules! PErr {
    ($msg:expr) => {
        Err(ParseErr($msg.to_string()))
    };
    ($msg:expr, $($var:expr),+) => {
        Err(ParseErr(format!($msg, $($var),+)))
    }
}

/// Try to extract values from results in vector. Short circuit on the first failure. Note this
/// does not return an iterator (because it folds).
// TODO: For efficiency: fold_results(v: Vec<O>, f: Fn(O) -> Result<O,E>), map and fold in one pass
fn fold_results<O, E>(v: Vec<Result<O, E>>) -> Result<Vec<O>, E> {
    v.into_iter().try_fold(vec![], |mut inner_vec, r| {
        //inner_vec.push(v)))
        let v = r?;
        inner_vec.push(v);
        Ok(inner_vec)
    })
    //.map(|mut v| { v.reverse(); v })
}