man_or_boy.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var RTL$ = {
  2. extend: function extend(methods){
  3. function Type(){
  4. for(var m in methods)
  5. this[m] = methods[m];
  6. }
  7. Type.prototype = this.prototype;
  8. var result = methods.init;
  9. result.prototype = new Type(); // inherit this.prototype
  10. result.prototype.constructor = result; // to see constructor name in diagnostic
  11. result.extend = extend;
  12. return result;
  13. }
  14. };
  15. var test = function (JS){
  16. var State = RTL$.extend({
  17. init: function State(){
  18. this.f = null;
  19. this.k = 0;
  20. this.x1 = null;
  21. this.x2 = null;
  22. this.x3 = null;
  23. this.x4 = null;
  24. this.x5 = null;
  25. }
  26. });
  27. var pB = null;
  28. function call(s/*PState*/){
  29. return s.f(s);
  30. }
  31. function makeEmptyState(f/*Function*/){
  32. var result = null;
  33. result = new State();
  34. result.f = f;
  35. return result;
  36. }
  37. function makeState(f/*Function*/, k/*INTEGER*/, x1/*PState*/, x2/*PState*/, x3/*PState*/, x4/*PState*/, x5/*PState*/){
  38. var result = null;
  39. result = makeEmptyState(f);
  40. result.k = k;
  41. result.x1 = x1;
  42. result.x2 = x2;
  43. result.x3 = x3;
  44. result.x4 = x4;
  45. result.x5 = x5;
  46. return result;
  47. }
  48. function F0(s/*PState*/){
  49. return 0;
  50. }
  51. function F1(s/*PState*/){
  52. return 1;
  53. }
  54. function Fn1(s/*PState*/){
  55. return -1;
  56. }
  57. function A(s/*PState*/){
  58. var res = 0;
  59. if (s.k <= 0){
  60. res = call(s.x4) + call(s.x5) | 0;
  61. }
  62. else {
  63. res = call(makeState(pB, s.k, s.x1, s.x2, s.x3, s.x4, s.x5));
  64. }
  65. return res;
  66. }
  67. function B(s/*PState*/){
  68. --s.k;
  69. return call(makeState(A, s.k, s, s.x1, s.x2, s.x3, s.x4));
  70. }
  71. pB = B;
  72. JS.alert(call(makeState(A, 10, makeEmptyState(F1), makeEmptyState(Fn1), makeEmptyState(Fn1), makeEmptyState(F1), makeEmptyState(F0))));
  73. }(this);