summaryrefslogtreecommitdiff
path: root/test/matchoverwild.myr
blob: e34e8a13a2063d3956dc03b4267822816a3dde29 (plain)
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
use std

type ast = struct
	body : op[:]
;;
type op = union
	`Go int
	`Set int
	`Add int
	`Mul (int, int)
	`Read
	`Write
	`Loop ast
;;

const combine = {lhs : op, rhs : op
	match (lhs, rhs)
	| (`Add x, `Add y): -> `std.Some `Add (x + y)
	| (`Go x, `Go y): -> `std.Some `Go (x + y)
	| (`Go 0, _): -> `std.Some rhs
	| (`Set x, `Add y): -> `std.Some `Set(x + y)
	| (`Add _, `Set y): -> `std.Some rhs
	| (`Add _, `Read): -> `std.Some rhs
	| (`Set _, `Read): -> `std.Some rhs
	| (`Loop _, `Loop _): -> `std.Some lhs
	| _: -> `std.None
	;;
}

const main = {
	match combine(`Add 123, `Set 246)
	| `std.Some `Set 246:
		/* ok */
	| r:
		std.fatal("bad combine {}\n", r)
	;;
	match combine(`Add 123, `Add 246)
	| `std.Some `Add 369:
		/* ok */
	| r:
		std.fatal("bad combine {}\n", r)
	;;
}