forked from veged/ometa-js-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOMeta_WJS_Mods.js
110 lines (101 loc) · 2.46 KB
/
OMeta_WJS_Mods.js
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
OMeta._or = function() {
for (var idx = 0; idx < arguments.length; idx++) {
var ok = true
in thisWorld.sprout() {
try { return arguments[idx]() }
catch (f) {
ok = false
if (f != fail)
throw f
}
finally { if (ok) thisWorld.commit() }
}
}
throw fail
}
OMeta._many = function(x) {
var ans = arguments[1] != undefined ? [arguments[1]] : []
while (true) {
in thisWorld.sprout() {
try {
ans.push(x())
//print("committing " + ans.toString())
thisWorld.commit()
}
catch (f) {
if (f != fail)
throw f
break
}
}
}
return ans
}
OMeta._not = function(x) {
in thisWorld.sprout() {
try { x() }
catch (f) {
if (f != fail)
throw f
return true
}
}
throw fail
}
/*
OMeta._lookahead = function(x) {
in thisWorld.sprout() {
var r = x()
//print("la = " + r.toString())
return x
}
}
*/
/*
OMeta._apply = function(rule) {
var memoRec = this.input.memo[rule]
if (memoRec == undefined) {
var origInput = this.input,
failer = new Failer()
this.input.memo[rule] = failer
this.input.memo[rule] = memoRec = {ans: this[rule].apply(this), nextInput: this.input}
if (failer.used) {
var sentinel = this.input
while (true) {
try {
this.input = origInput
var ans = this[rule].apply(this)
if (this.input == sentinel)
throw fail
memoRec.ans = ans
memoRec.nextInput = this.input
}
catch (f) {
if (f != fail)
throw f
break
}
}
}
}
else if (memoRec instanceof Failer) {
memoRec.used = true
throw fail
}
this.input = memoRec.nextInput
return memoRec.ans
}
*/
print("defining example 1")
eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = (1 -> 2)* }", "srcElem"), "trans"))
print("running example 1")
print(M.matchAll([1, 1, 1, 1], "ones"))
print("defining example 2")
eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { foo = &(:x) anything*:ys -> [x, ys] }", "srcElem"), "trans"))
print("running example 2")
print(M.matchAll([1, 2, 3, 4], "foo"))
print("defining example 3")
eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = {count=0} ({count++} 1 -> 2)* }", "srcElem"), "trans"))
print("running example 3")
print(M.matchAll([1, 1, 1, 1], "ones"))
print("count = " + count)