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
use std::str::FromStr;

use pest::iterators::Pair;
use pest::prec_climber::{Assoc, Operator, PrecClimber};
use pest::Parser;
use yolol_number::YololNumber;

use crate::ast::{InfixOp, PrefixOp, YolkNode};
use crate::error::ParseError;

#[cfg(test)]
mod tests;

lazy_static! {
    static ref PREC_CLIMBER: PrecClimber<Rule> = build_prec_climber();
}

fn build_prec_climber() -> PrecClimber<Rule> {
    PrecClimber::new(vec![
        Operator::new(Rule::logical_or, Assoc::Left),
        Operator::new(Rule::logical_and, Assoc::Left),
        Operator::new(Rule::equal, Assoc::Left) | Operator::new(Rule::not_equal, Assoc::Left),
        Operator::new(Rule::less_than, Assoc::Left)
            | Operator::new(Rule::less_equal, Assoc::Left)
            | Operator::new(Rule::greater_than, Assoc::Left)
            | Operator::new(Rule::greater_equal, Assoc::Left),
        Operator::new(Rule::plus, Assoc::Left) | Operator::new(Rule::minus, Assoc::Left),
        Operator::new(Rule::multiply, Assoc::Left)
            | Operator::new(Rule::divide, Assoc::Left)
            | Operator::new(Rule::modulo, Assoc::Left),
        Operator::new(Rule::exponent, Assoc::Right),
    ])
}

#[derive(Parser)]
#[grammar = "grammar/yolk.pest"]
pub struct YolkParser;

/// Parses Yolk statements from source text.
pub fn parse(source: &str) -> Result<Vec<YolkNode>, ParseError> {
    let mut ast = vec![];
    let pairs = YolkParser::parse(Rule::program, source)
        .map_err(|e| ParseError::BadSyntax(e.to_string()))?;
    for pair in pairs {
        match pair.as_rule() {
            Rule::import_stmt => ast.push(parse_import_stmt(pair)),
            Rule::define_stmt => ast.push(parse_define_stmt(pair)),
            Rule::let_stmt => ast.push(parse_let_stmt(pair)),
            Rule::export_stmt => ast.push(parse_export_stmt(pair)),
            Rule::comment => (),
            Rule::EOI => (),
            _ => panic!("expected rule statement, but got: {:?}", pair),
        }
    }
    Ok(ast)
}

fn parse_import_stmt(stmt: Pair<Rule>) -> YolkNode {
    let mut pairs = stmt.into_inner();
    let ident = pairs.next().expect("failed to unwrap ident from pair");
    YolkNode::ImportStmt {
        ident: ident.as_str().to_string(),
    }
}

fn parse_define_stmt(stmt: Pair<Rule>) -> YolkNode {
    let mut pairs = stmt.into_inner();
    let ident = pairs.next().expect("failed to unwrap ident from pair");
    let params = pairs.next().expect("failed to unwrap params from pair");
    let body = pairs.next().expect("failed to unwrap body from pair");
    YolkNode::DefineStmt {
        ident: ident.as_str().to_string(),
        params: params
            .into_inner()
            .map(|x| x.as_str().to_string())
            .collect(),
        body: Box::new(parse_expr(body)),
    }
}

fn parse_let_stmt(stmt: Pair<Rule>) -> YolkNode {
    let mut pairs = stmt.into_inner();
    let ident = pairs.next().expect("failed to unwrap ident from pair");
    let expr = pairs.next().expect("failed to unwrap expr from pair");
    YolkNode::LetStmt {
        ident: ident.as_str().to_string(),
        expr: Box::new(parse_expr(expr)),
    }
}

fn parse_export_stmt(stmt: Pair<Rule>) -> YolkNode {
    let mut pairs = stmt.into_inner();
    let ident = pairs.next().expect("failed to unwrap ident from pair");
    YolkNode::ExportStmt {
        ident: ident.as_str().to_string(),
    }
}

fn parse_expr(expr: Pair<Rule>) -> YolkNode {
    match expr.as_rule() {
        Rule::prefix_expr => {
            let mut pairs = expr.into_inner();
            let op = pairs.next().expect("failed to unwrap op from pair");
            let expr = pairs.next().expect("failed to unwrap expr from pair");
            YolkNode::PrefixExpr {
                op: match op.as_rule() {
                    Rule::logical_not => PrefixOp::Not,
                    Rule::abs => PrefixOp::Abs,
                    Rule::sqrt => PrefixOp::Sqrt,
                    Rule::sin => PrefixOp::Sin,
                    Rule::cos => PrefixOp::Cos,
                    Rule::tan => PrefixOp::Tan,
                    Rule::asin => PrefixOp::Asin,
                    Rule::acos => PrefixOp::Acos,
                    Rule::atan => PrefixOp::Atan,
                    _ => panic!("expected prefix op, but got: {:?}", op),
                },
                expr: Box::new(parse_expr(expr)),
            }
        }
        Rule::builtin_expr => {
            let mut pairs = expr.into_inner();
            let ident = pairs.next().expect("failed to unwrap ident from pair");
            let args = pairs.next().expect("failed to unwrap args from pair");
            YolkNode::BuiltinExpr {
                ident: ident.as_str().to_string(),
                args: args.into_inner().map(parse_expr).collect(),
            }
        }
        Rule::call_expr => {
            let mut pairs = expr.into_inner();
            let ident = pairs.next().expect("failed to unwrap ident from pair");
            let args = pairs.next().expect("failed to unwrap args from pair");
            YolkNode::CallExpr {
                ident: ident.as_str().to_string(),
                args: args.into_inner().map(parse_expr).collect(),
            }
        }
        Rule::infix_expr => PREC_CLIMBER.climb(
            expr.into_inner(),
            |pair: Pair<Rule>| parse_expr(pair),
            |lhs: YolkNode, op: Pair<Rule>, rhs: YolkNode| YolkNode::InfixExpr {
                lhs: Box::new(lhs),
                op: match op.as_rule() {
                    Rule::plus => InfixOp::Add,
                    Rule::minus => InfixOp::Sub,
                    Rule::multiply => InfixOp::Mul,
                    Rule::divide => InfixOp::Div,
                    Rule::modulo => InfixOp::Mod,
                    Rule::exponent => InfixOp::Exp,
                    Rule::less_than => InfixOp::LessThan,
                    Rule::less_equal => InfixOp::LessEqual,
                    Rule::greater_than => InfixOp::GreaterThan,
                    Rule::greater_equal => InfixOp::GreaterEqual,
                    Rule::equal => InfixOp::Equal,
                    Rule::not_equal => InfixOp::NotEqual,
                    Rule::logical_and => InfixOp::And,
                    Rule::logical_or => InfixOp::Or,
                    _ => panic!("expected infix op, but got: {:?}", op),
                },
                rhs: Box::new(rhs),
            },
        ),
        Rule::ident => YolkNode::Ident(expr.as_str().to_string()),
        Rule::literal => YolkNode::Literal(
            YololNumber::from_str(expr.as_str())
                .unwrap_or_else(|e| panic!("failed to parse YololNumber from string: {}", e)),
        ),
        Rule::array => {
            let exprs: Vec<YolkNode> = expr.into_inner().map(parse_expr).collect();
            YolkNode::Array(exprs)
        }
        _ => panic!("expected rule expression, but got: {:?}", expr),
    }
}