oridb / mc

Myrddin Compiler
MIT License
387 stars 34 forks source link

Support or-pattern #202

Closed typeless closed 4 years ago

typeless commented 4 years ago

Examples:

type foo = union
        `Black
        `Blue
        `Green
        `Red
        `Yellow
        `White
;;
match `Green
| `Black || `White: std.put("monochrome\n")
| `Blue || `Green || `Red: std.put("color\n")
| _: std.put("others")  
;;
// output: color
match `std.Some 100
| `std.Some (100 ||200 ||300): std.put("hundreds\n")
| `std.Some _: std.put("other values\n")
| _: std.put("no value\n")
;;
// output: hundreds
match `std.Some (`std.Some 333, 123, 789)
| `std.Some (`std.Some (101||451||789||333), _, _): std.put("good\n")
| `std.Some (`std.Some (100||200), 222, 333): std.put("bad\n")
| `std.Some _: std.put("Some _\n")
| `std.None: std.put("None\n")
;;
// output: good
typeless commented 4 years ago

The or-patterns with variables are implemented now.

type bar = union
    `A int
    `B int
    `C int
    `D (byte[:], int)
    `E (byte[:], int)
    `F (int, std.option(int))
    `G (int, std.option(int))
;;

match `A 123
| `A x || `B x: std.put("good #4 {}\n", x)
| _: std.exit(1)
;;
// output: good #4 123

match `G (223, `std.Some 556)
| `F (x, `std.Some y) || `G (x, `std.Some y): std.put("good #5 x={} y={}\n", x, y)
| _: std.exit(1)
;;
// output good #5 x=223 y=556
oridb commented 4 years ago

This is looking pretty good. I'm going to poke a bit more at it, but I like the idea.