OMeta_WJS_Mods.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. OMeta._or = function() {
  2. for (var idx = 0; idx < arguments.length; idx++) {
  3. var ok = true
  4. in thisWorld.sprout() {
  5. try { return arguments[idx]() }
  6. catch (f) {
  7. ok = false
  8. if (f != fail)
  9. throw f
  10. }
  11. finally { if (ok) thisWorld.commit() }
  12. }
  13. }
  14. throw fail
  15. }
  16. OMeta._many = function(x) {
  17. var ans = arguments[1] != undefined ? [arguments[1]] : []
  18. while (true) {
  19. in thisWorld.sprout() {
  20. try {
  21. ans.push(x())
  22. //print("committing " + ans.toString())
  23. thisWorld.commit()
  24. }
  25. catch (f) {
  26. if (f != fail)
  27. throw f
  28. break
  29. }
  30. }
  31. }
  32. return ans
  33. }
  34. OMeta._not = function(x) {
  35. in thisWorld.sprout() {
  36. try { x() }
  37. catch (f) {
  38. if (f != fail)
  39. throw f
  40. return true
  41. }
  42. }
  43. throw fail
  44. }
  45. /*
  46. OMeta._lookahead = function(x) {
  47. in thisWorld.sprout() {
  48. var r = x()
  49. //print("la = " + r.toString())
  50. return x
  51. }
  52. }
  53. */
  54. /*
  55. OMeta._apply = function(rule) {
  56. var memoRec = this.input.memo[rule]
  57. if (memoRec == undefined) {
  58. var origInput = this.input,
  59. failer = new Failer()
  60. this.input.memo[rule] = failer
  61. this.input.memo[rule] = memoRec = {ans: this[rule].apply(this), nextInput: this.input}
  62. if (failer.used) {
  63. var sentinel = this.input
  64. while (true) {
  65. try {
  66. this.input = origInput
  67. var ans = this[rule].apply(this)
  68. if (this.input == sentinel)
  69. throw fail
  70. memoRec.ans = ans
  71. memoRec.nextInput = this.input
  72. }
  73. catch (f) {
  74. if (f != fail)
  75. throw f
  76. break
  77. }
  78. }
  79. }
  80. }
  81. else if (memoRec instanceof Failer) {
  82. memoRec.used = true
  83. throw fail
  84. }
  85. this.input = memoRec.nextInput
  86. return memoRec.ans
  87. }
  88. */
  89. print("defining example 1")
  90. eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = (1 -> 2)* }", "srcElem"), "trans"))
  91. print("running example 1")
  92. print(M.matchAll([1, 1, 1, 1], "ones"))
  93. print("defining example 2")
  94. eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { foo = &(:x) anything*:ys -> [x, ys] }", "srcElem"), "trans"))
  95. print("running example 2")
  96. print(M.matchAll([1, 2, 3, 4], "foo"))
  97. print("defining example 3")
  98. eval(BSOMetaJSTranslator.match(BSOMetaJSParser.matchAll("ometa M { ones = {count=0} ({count++} 1 -> 2)* }", "srcElem"), "trans"))
  99. print("running example 3")
  100. print(M.matchAll([1, 1, 1, 1], "ones"))
  101. print("count = " + count)