Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

117 lines (107 sloc) 2.622 kb
#pragma NativePrelude
#pragma DefaultVisibility=public
inherits _prelude
def 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 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(nothing, [])
}
}
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)
}
}
Jump to Line
Something went wrong with that request. Please try again.