rain-lang 0.0.1

An implementation of an RVSDG in Rust with a concept of lifetimes
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
/*!
A simple parser, AST and prettyprinter for a textual representation of `rain` programs
*/
use nom::{
    IResult,
    multi::{many0, many0_count, separated_list, separated_nonempty_list},
    sequence::{preceded, terminated, delimited, tuple, separated_pair},
    combinator::{opt, map, complete, recognize},
    branch::alt,
    bytes::complete::{tag, is_not, is_a},
    bytes::streaming::take_until,
    character::complete::{not_line_ending, digit1, hex_digit1, oct_digit1},
    character::streaming::multispace0,
};
use num::BigUint;

pub mod ast;
use ast::{
    Path, Expr, Pattern, Let, Scope, SimpleAssignment, Parametrized, Lambda, Pi, Phi, Gamma, Sexpr
};
use crate::value::primitive::{
    logical::{LogicalOp, Binary, Unary, Bool},
    binary::{Natural, BinaryDisplay}
};
pub mod symbol_table;
pub mod builder;

macro_rules! special_chars { () => (" \t\r\n(){}[]|:.=;#,'\"") }
macro_rules! digits { () => ("0123456789") }

const LET: &'static str = "let";
const DEFEQ: &'static str = "=";
const TERM: &'static str = ";";

/// Parse a single line `rain` comment
pub fn parse_single_comment(input: &str) -> IResult<&str, &str> {
    preceded(tag("//"), not_line_ending)(input)
}

/// Parse a multi line `rain` comment
pub fn parse_multi_comment(input: &str) -> IResult<&str, &str> {
    preceded(
        tag("/*"),
        take_until("*/")
    )(input)
}

/// Parse a `rain` comment. TODO: distinguish documentation comments, and don't ignore those
pub fn parse_comment(input: &str) -> IResult<&str, &str> {
    alt((
        parse_single_comment,
        parse_multi_comment
    ))(input)
}

/// Parse whitespace, including ignored comments. Returns the count of comments.
pub fn whitespace(input: &str) -> IResult<&str, usize> {
    terminated(
        many0_count(preceded(multispace0, parse_comment)),
        multispace0
    )(input)
}

/// Parse a `rain` identifier, which is composed of any non-special character.
/// Does *not* accept whitespace before the identifier, and does *not* consume whitespace after it.
pub fn parse_ident(input: &str) -> IResult<&str, &str> {
    recognize(
        tuple((is_not(concat!(special_chars!(), digits!())), opt(is_not(special_chars!()))))
    )(input)
}

/// Parse a path separator
pub fn path_separator(input: &str) -> IResult<&str, &str> { tag(".")(input) }

/// Parse a `rain` path.
/// Does *not* accept whitespace before the path and does *not* consume whitespace after it.
pub fn parse_path(input: &str) -> IResult<&str, Path> {
    map(
        separated_nonempty_list(path_separator, parse_ident),
        |names| { names.into() }
    )(input)
}

/// Parse a binary logical operation
pub fn parse_binary_logical_op(input: &str) -> IResult<&str, Binary> {
    use Binary::*;
    preceded(whitespace, alt((
        map(tag(And.get_string()), |_| And),
        map(tag(Or.get_string()), |_| Or),
        map(tag(Xor.get_string()), |_| Xor),
        map(tag(Nand.get_string()), |_| Nand),
        map(tag(Nor.get_string()), |_| Nor),
        map(tag(Eq.get_string()), |_| Eq),
        map(tag(Implies.get_string()), |_| Implies),
        map(tag(ImpliedBy.get_string()), |_| ImpliedBy)
    )))(input)
}

/// Parse a unary logical operation
pub fn parse_unary_logical_op(input: &str) -> IResult<&str, Unary> {
    use Unary::*;
    preceded(whitespace, alt((
        map(tag(Id.get_string()), |_| Id),
        map(tag(Not.get_string()), |_| Not),
        map(tag(Constant(true).get_string()), |_| Constant(true)),
        map(tag(Constant(false).get_string()), |_| Constant(false)),
    )))(input)
}

/// Parse a logical operation
pub fn parse_logical_op(input: &str) -> IResult<&str, LogicalOp> {
    use LogicalOp::*;
    alt((
        map(parse_binary_logical_op, Binary),
        map(parse_unary_logical_op, Unary)
    ))(input)
}

/// Parse the Boolean type
pub fn parse_bool_type(input: &str) -> IResult<&str, Bool> {
    map(preceded(whitespace, tag("#bool")), |_| Bool)(input)
}

/// Parse a boolean
pub fn parse_bool(input: &str) -> IResult<&str, bool> {
    preceded(whitespace, alt((
        map(tag("#true"), |_| true),
        map(tag("#false"), |_| false)
    )))(input)
}

/// Parse a natural number
pub fn parse_natural(input: &str) -> IResult<&str, Natural> {
    use BinaryDisplay::*;
    alt((
        map(
            preceded(tag("0b"), is_a("01")),
            |bytes: &str| Natural(BigUint::parse_bytes(bytes.as_bytes(), 2).unwrap(), Bin)
        ),
        map(
            preceded(tag("0o"), oct_digit1),
            |bytes: &str| Natural(BigUint::parse_bytes(bytes.as_bytes(), 8).unwrap(), Oct)
        ),
        map(
            preceded(tag("0x"), hex_digit1),
            |bytes: &str| Natural(BigUint::parse_bytes(bytes.as_bytes(), 2).unwrap(), Hex)
        ),
        map(
            digit1,
            |bytes: &str| Natural(BigUint::parse_bytes(bytes.as_bytes(), 10).unwrap(), Dec)
        ),
    ))(input)
}

/// Parse an atomic `rain` expression
/// Does *not* accept whitespace before the expression, and does *not* consume whitespace after it.
pub fn parse_atom(input: &str) -> IResult<&str, Expr> {
    alt((
        map(parse_natural, Expr::Natural), // Natural values
        map(parse_bool, Expr::Bool), // Boolean values
        map(parse_logical_op, Expr::LogicalOp), // Binary logical operations
        map(parse_bool_type, Expr::BoolTy), // Parse the boolean type
        map(parse_phi, Expr::Phi), // Phis
        map(parse_lambda, Expr::Lambda), // Lambdas
        map(parse_gamma, Expr::Gamma), // Match statements
        map(parse_pi, Expr::Pi), // Pi types
        map(parse_path, Expr::Path), // Paths
        map(parse_scope, Expr::Scope), // Scopes
        delimited(tag("("), parse_expr, preceded(whitespace, tag(")")))
    ))(input)
}

/// Parse a `rain` expression, which is either an atom or an application of them
/// Accepts whitespace before the expression
pub fn parse_expr(input: &str) -> IResult<&str, Expr> {
    map(
        tuple((
            preceded(whitespace, parse_atom),
            many0(preceded(complete(whitespace), map(parse_atom, Box::new)))
        )),
        |(first, mut ops)| {
            if ops.len() == 0 { first }
            else { ops.reverse(); ops.push(Box::new(first)); Expr::Sexpr(Sexpr { ops })}
        }
    )(input)
}

/// Parse a type bound
pub fn parse_type_bound(input: &str) -> IResult<&str, Expr> {
    preceded(preceded(whitespace, tag(":")), parse_expr)(input)
}

/// Parse a simple assignment. Accepts whitespace before it
pub fn parse_simple_assignment(input: &str) -> IResult<&str, SimpleAssignment> {
    map(
        tuple((
            whitespace,
            parse_ident,
            opt(parse_type_bound)
        )),
        |(_, name, ty)| SimpleAssignment { name, ty }
    )(input)
}

/// Parse a pattern for assignment
/// Accepts whitespace before the pattern
pub fn parse_pattern(input: &str) -> IResult<&str, Pattern> {
    map(preceded(whitespace, parse_simple_assignment), Pattern::Simple)(input) //TODO: this
}

/// Parse an equality separator, optionally preceded by whitespace
pub fn defeq(input: &str) -> IResult<&str, &str> { preceded(whitespace, tag(DEFEQ))(input) }

/// Parse a terminator, optionally preceded by whitespace
pub fn terminator(input: &str) -> IResult<&str, &str> { preceded(whitespace, tag(TERM))(input) }

/// Parse a `let`-statement
pub fn parse_statement(input: &str) -> IResult<&str, Let> {
    delimited(
        preceded(whitespace, tag(LET)),
        map(
            separated_pair(parse_pattern, defeq, parse_expr),
            |(pattern, expr)| { Let { pattern, expr } }
        ),
        terminator
    )(input)
}

/// Parse a scope, consuming all whitespace before it
pub fn parse_scope(input: &str) -> IResult<&str, Scope> {
    map(
        delimited(
            preceded(whitespace, tag("{")),
            tuple((
                many0(parse_statement),
                opt(parse_expr)
            )),
            preceded(whitespace, tag("}"))
        ),
        |(definitions, value)| Scope { definitions, value: value.map(Box::new) }
    )(input)
}

/// Parse a phi node, consuming all whitespace beforeit
pub fn parse_phi(input: &str) -> IResult<&str, Phi> {
    map(
        preceded(
            preceded(whitespace, tag("#phi")),
            parse_scope
        ),
        Phi
    )(input)
}

/// Parse an optional ident
pub fn parse_opt_ident(input: &str) -> IResult<&str, Option<&str>> {
    alt((
        map(tag("_"), |_| None),
        map(parse_ident, Some)
    ))(input)
}

/// Parse a list of typed identifiers
pub fn parse_typed_idents(input: &str) -> IResult<&str, Vec<(Option<&str>, Expr)>> {
    separated_nonempty_list(
        delimited(whitespace, tag(","), whitespace),
        tuple((parse_opt_ident, parse_type_bound))
    )(input)
}

/// Parse a list of typed arguments
pub fn parse_typed_args(input: &str) -> IResult<&str, Vec<(Option<&str>, Expr)>> {
    delimited(
        preceded(whitespace, tag("|")),
        parse_typed_idents,
        preceded(whitespace, tag("|"))
    )(input)
}

/// Parse a function type arrow
pub fn parse_type_arrow(input: &str) -> IResult<&str, Expr> {
    preceded(
        delimited(whitespace, tag("=>"), whitespace),
        parse_atom
    )(input)
}

/// Parse a lambda function, consuming all whitespace before it
pub fn parse_lambda(input: &str) -> IResult<&str, Lambda> {
    map(
        preceded(
            preceded(whitespace, tag("#lambda")),
            parse_parametrized
        ),
        |p| Lambda(p)
    )(input)
}

/// Parse a pi type, consuming all whitespace before it
pub fn parse_pi(input: &str) -> IResult<&str, Pi> {
    map(
        preceded(
            preceded(whitespace, tag("#pi")),
            parse_parametrized
        ),
        |p| Pi(p)
    )(input)
}

/// Parse a parametrized expression
pub fn parse_parametrized(input: &str) -> IResult<&str, Parametrized> {
    map(
        tuple((parse_typed_args, opt(parse_type_arrow), parse_expr)),
        |(args, ret_ty, result)| Parametrized {
            args, result: Box::new(result), ret_ty: ret_ty.map(|r| Box::new(r))
        }
    )(input)
}

/// Parse a gamma node, consuming all whitespace before it
pub fn parse_gamma(input: &str) -> IResult<&str, Gamma> {
    map(
        preceded(
            preceded(whitespace, tag("#match")),
            parse_pattern_matches
        ),
        |branches| Gamma { branches }
    )(input)
}

/// Parse a set of pattern matches
pub fn parse_pattern_matches(input: &str) -> IResult<&str, Vec<(Pattern, Expr)>> {
    delimited(
        preceded(whitespace, tag("{")),
        separated_list(
            preceded(whitespace, tag(",")),
            parse_pattern_match
        ),
        preceded(delimited(whitespace, opt(tag(",")), whitespace), tag("}"))
    )(input)
}

/// Parse a pattern match
pub fn parse_pattern_match(input: &str) -> IResult<&str, (Pattern, Expr)> {
    separated_pair(parse_pattern, preceded(whitespace, tag("=>")), parse_expr)(input)
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! parse_tester {
        ($parser:expr, $string:expr, $correct:expr, $tail:expr) => {{
            let parser = $parser;
            let string = $string;
            let correct = $correct;
            let tail: Option<&str> = $tail;
            let (rest, parsed) = match parser(string) {
                Ok(result) => result,
                Err(err) => {
                    panic!("Error {:?} parsing input string {:?}", err, string)
                }
            };
            if let Some(correct) = correct { assert_eq!(parsed, correct, "Input parses wrong!"); }
            if let Some(tail) = tail { assert_eq!(rest, tail, "Invalid tail on input!"); }
            let displayed = format!("{}", parsed);
            let (rest, d_parsed) = match parser(&displayed) {
                Ok(result) => result,
                Err(err) => {
                    panic!(
                        "Error {:?} parsing display string {:?} (input = {:?})",
                        err, displayed, string
                    )
                }
            };
            assert_eq!(
                d_parsed, parsed,
                "Display output parses to the wrong result! (input = {:?}, displayed = {:?})",
                string, displayed
            );
            assert_eq!(
                rest, "",
                "Unparsed display output (input = {:?}, displayed = {:?})!", string, displayed
            );
            assert_eq!(displayed, format!("{}", d_parsed));
        }}
    }

    macro_rules! assert_parses {
        ($parser:expr, $string:expr) => { parse_tester!($parser, $string, None, Some("")) }
    }

    macro_rules! assert_parses_to {
        ($parser:expr, $string:expr, $correct:expr, $tail:expr) => {
            parse_tester!($parser, $string, Some($correct), Some($tail))
        }
    }

    #[test]
    fn idents_parse_properly() {
        assert_parses_to!(parse_ident, "hello world", "hello", " world");
        assert_parses_to!(parse_ident, "h3110 w0rld", "h3110", " w0rld");
        assert_parses_to!(parse_ident, "helloworld", "helloworld", "");
        assert_parses_to!(parse_ident, "hello() world", "hello", "() world");
        assert!(parse_ident(" helloworld").is_err());
        assert!(parse_ident(".helloworld").is_err());
        assert!(parse_ident(".12345").is_err());
        assert!(parse_ident("").is_err());
    }

    #[test]
    fn nested_exprs_dont_merge_improperly() {
        assert_eq!(
            &(format!("{:?}", parse_expr("a (b c) (d e)").unwrap())),
            "(\"\", (a (b c) (d e)))"
        );
    }

    #[test]
    fn paths_parse_properly() {
        assert_parses_to!(parse_path, "hello world", Path::ident("hello"), " world");
        assert_parses_to!(parse_path, "hello.world", Path::from(vec!["hello", "world"]), "");
        assert_parses_to!(parse_path, "hello.", Path::ident("hello"), ".");
        assert_parses_to!(parse_path, "h3110.w0rld", Path::from(vec!["h3110", "w0rld"]), "");
        assert!(parse_path(" helloworld").is_err());
        assert!(parse_path(".helloworld").is_err());
        assert!(parse_path(".12345").is_err());
        assert!(parse_path("").is_err());
    }

    #[test]
    fn atoms_parse_properly() {
        assert_parses_to!(parse_atom, "x y", Expr::ident("x"), " y");
        assert_parses_to!(parse_atom, "x.y", Expr::Path(Path::from(vec!["x", "y"])), "");
        assert_parses_to!(parse_atom, "(x) y", Expr::ident("x"), " y");
        assert_parses_to!(parse_atom, "(x.y) z", Expr::Path(Path::from(vec!["x", "y"])), " z");
    }

    #[test]
    fn simple_exprs_parse_properly() {
        assert_parses_to!(parse_expr, "(x y)", Expr::Sexpr(vec![
            Expr::ident("y").into(), Expr::ident("x").into()
        ].into()), "");
        assert_parses_to!(parse_expr, "(x.y)", Expr::Path(Path::from(vec!["x", "y"])), "");
        assert_parses_to!(parse_expr, "((x) y)", Expr::Sexpr(vec![
            Expr::ident("y").into(), Expr::ident("x").into()
        ].into()), "");
        assert_parses_to!(parse_expr, "((x.y) z)", Expr::Sexpr(vec![
            Expr::ident("z").into(),
            Expr::Path(Path::from(vec!["x", "y"])).into()
        ].into()), "");
    }

    #[test]
    fn nested_exprs_parse_properly() {
        let yz = Box::new(
            Expr::Sexpr(vec![Expr::ident("z").into(), Expr::ident("y").into()].into())
        );
        let xyz = Box::new(Expr::Sexpr(
            vec![
            Expr::Sexpr(vec![Expr::ident("z").into(), Expr::ident("y").into()].into()).into(),
            Expr::ident("x").into()
            ]
        .into()));
        assert_parses_to!(parse_expr, "(x (y z) (x (y z)) ((y z) w))", Expr::Sexpr(vec![
            Expr::Sexpr(vec![Expr::ident("w").into(), yz.clone()].into()).into(),
            xyz,
            yz.clone(),
            Expr::ident("x").into()
        ].into()), "")
    }

    #[test]
    fn simple_let_statements_parse_properly() {
        let statements = [
            "let x = y;",
            "let x = x.y;",
            "let hello = (world y) z;",
            "let z = 43 (54 2);"
        ];
        for statement in statements.iter() { assert_parses!(parse_statement, statement) }
    }

    #[test]
    fn simple_scopes_parse_properly() {
        let scopes = [
            "{}",
            "{ /* my variable x*/ x }",
            "{ let x = y; x }",
            "{ let hello = (world y) z; let x = world hello; hello x }"
        ];
        for scope in scopes.iter() { assert_parses!(parse_scope, scope) }
    }

    #[test]
    fn lambda_arguments_parse_properly() {
        let args = [
            "|x : A|",
            "|x : A|",
            "|y : B, z : C|",
            "|world: F, y: C|"
        ];
        for arg in args.iter() {
            assert!(parse_typed_args(arg).is_ok(), "Failed to parse {}", arg)
        }
    }

    #[test]
    fn type_arrows_parse_properly() {
        let args = [
            "=> x",
        ];
        for arg in args.iter() {
            match parse_type_arrow(arg) {
                Ok(_) => {},
                Err(err) => panic!("Failed to parse {:?}, got {:?}", arg, err)
            }
        }
    }

    #[test]
    fn simple_functions_parse_properly() {
        let fns = [
            "#lambda |x : A| {}",
            "#lambda |x : A| x",
            "#lambda |_ : A| x",
            "#lambda |x : A| /*some comment*/ x",
            "#lambda |x : A| { /* my variable x*/ x }",
            "#lambda |y : B, z : C| { let x = y; x }",
            "#lambda |world: F, y: C| { let hello = (world y) z; let x = world hello; hello x }",
            "#lambda |world: F, y: C| => T { let hello = (world y) z; let x = world hello; hello x }"
        ];
        for func in fns.iter() { assert_parses!(parse_lambda, func) }
    }

    #[test]
    fn phi_nodes_parse_properly() {
        let phis = [
            "#phi { let f = #lambda |x : A| { g x }; let g = #lambda |x : A| { f x }; }",
            "#phi { let x = 6; let y = 43; let z = 341; }"
        ];
        for phi in phis.iter() { assert_parses!(parse_phi, phi) }
    }

    #[test]
    fn gamma_nodes_parse_properly() {
        let gammas = [
            "#match {}",
            "#match { x => y }"
        ];
        for gamma in gammas.iter() { assert_parses!(parse_gamma, gamma) }
    }
}