forked from mwh/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardPrelude.grace
133 lines (124 loc) · 2.85 KB
/
StandardPrelude.grace
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
#pragma NativePrelude
#pragma DefaultVisibility=public
inherits _prelude
var isStandardPrelude := true
class SuccessfulMatch.new(result', bindings') {
inherits true
def result = result'
def bindings = bindings'
method asString {
"SuccessfulMatch(result = {result}, bindings = {bindings})"
}
}
class FailedMatch.new(result') {
inherits false
def result = result'
def bindings = []
method asString {
"FailedMatch(result = {result})"
}
}
type Extractable = {
extract
}
class BasicPattern.new {
method &(o) {
AndPattern.new(self, o)
}
method |(o) {
OrPattern.new(self, o)
}
}
class MatchAndDestructuringPattern.new(pat, items') {
def pattern = pat
def items = items'
method match(o) {
def m = pat.match(o)
if (m) then{
var mbindings := m.bindings
def bindings = []
if (mbindings.size < items.size) then {
if (Extractable.match(o)) then {
mbindings := o.extract
} else {
return FailedMatch.new(o)
}
}
for (items.indices) do {i->
def b = items[i].match(mbindings[i])
if (!b) then {
return FailedMatch.new(o)
}
for (b.bindings) do {bb->
bindings.push(bb)
}
}
SuccessfulMatch.new(o, bindings)
} else {
FailedMatch.new(o)
}
}
}
class VariablePattern.new(nm) {
method match(o) {
SuccessfulMatch.new(o, [o])
}
}
class BindingPattern.new(pat) {
method match(o) {
def bindings = [o]
def m = pat.match(o)
if (!m) then {
return m
}
for (m.bindings) do {b->
bindings.push(b)
}
SuccessfulMatch.new(o, bindings)
}
}
class WildcardPattern.new {
method match(o) {
SuccessfulMatch.new(done, [])
}
}
class AndPattern.new(p1, p2) {
method match(o) {
def m1 = p1.match(o)
if (!m1) then {
return m1
}
def m2 = p2.match(o)
if (!m2) then {
return m2
}
def bindings = []
for (m1.bindings) do {b->
bindings.push(b)
}
for (m2.bindings) do {b->
bindings.push(b)
}
SuccessfulMatch.new(o, bindings)
}
}
class OrPattern.new(p1, p2) {
method match(o) {
if (p1.match(o)) then {
return SuccessfulMatch.new(o, [])
}
if (p2.match(o)) then {
return SuccessfulMatch.new(o, [])
}
FailedMatch.new(o)
}
}
def _standardPrelude = self
def BasicGrace = object {
method new {
_prelude.clone(_standardPrelude)
}
}
method new {
_prelude.clone(self)
}