methcall.nut 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*translation of the methcall test from The Great Computer Language Shootout
  2. */
  3. class Toggle {
  4. bool=null
  5. }
  6. function Toggle::constructor(startstate) {
  7. bool = startstate
  8. }
  9. function Toggle::value() {
  10. return bool;
  11. }
  12. function Toggle::activate() {
  13. bool = !bool;
  14. return this;
  15. }
  16. class NthToggle extends Toggle {
  17. count_max=null
  18. count=0
  19. }
  20. function NthToggle::constructor(start_state,max_counter)
  21. {
  22. base.constructor(start_state);
  23. count_max = max_counter
  24. }
  25. function NthToggle::activate ()
  26. {
  27. ++count;
  28. if (count >= count_max ) {
  29. base.activate();
  30. count = 0;
  31. }
  32. return this;
  33. }
  34. function main() {
  35. local n = vargv.len()!=0?vargv[0].tointeger():1
  36. local val = 1;
  37. local toggle = Toggle(val);
  38. local i = n;
  39. while(i--) {
  40. val = toggle.activate().value();
  41. }
  42. print(toggle.value() ? "true\n" : "false\n");
  43. val = 1;
  44. local ntoggle = NthToggle(val, 3);
  45. i = n;
  46. while(i--) {
  47. val = ntoggle.activate().value();
  48. }
  49. print(ntoggle.value() ? "true\n" : "false\n");
  50. }
  51. local start=clock();
  52. main();
  53. print("TIME="+(clock()-start)+"\n");