The Squirrel interpreter. See http://www.squirrel-lang.org/
Diff: samples/methcall.nut
- Revision:
- 0:97a4f8cc534c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/methcall.nut Tue Dec 16 10:20:34 2014 +0000 @@ -0,0 +1,68 @@ +/*translation of the methcall test from The Great Computer Language Shootout +*/ + +class Toggle { + bool=null +} + +function Toggle::constructor(startstate) { + bool = startstate +} + +function Toggle::value() { + return bool; +} + +function Toggle::activate() { + bool = !bool; + return this; +} + +class NthToggle extends Toggle { + count_max=null + count=0 +} + +function NthToggle::constructor(start_state,max_counter) +{ + base.constructor(start_state); + count_max = max_counter +} + +function NthToggle::activate () +{ + ++count; + if (count >= count_max ) { + base.activate(); + count = 0; + } + return this; +} + + +function main() { + local n = vargv.len()!=0?vargv[0].tointeger():1 + + + + local val = 1; + local toggle = Toggle(val); + local i = n; + while(i--) { + val = toggle.activate().value(); + + } + print(toggle.value() ? "true\n" : "false\n"); + + val = 1; + local ntoggle = NthToggle(val, 3); + i = n; + while(i--) { + val = ntoggle.activate().value(); + } + print(ntoggle.value() ? "true\n" : "false\n"); + +} +local start=clock(); +main(); +print("TIME="+(clock()-start)+"\n"); \ No newline at end of file