The Squirrel interpreter. See http://www.squirrel-lang.org/
samples/methcall.nut@0:97a4f8cc534c, 2014-12-16 (annotated)
- Committer:
- jhnwkmn
- Date:
- Tue Dec 16 10:20:34 2014 +0000
- Revision:
- 0:97a4f8cc534c
Initial import of Squirrel.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jhnwkmn | 0:97a4f8cc534c | 1 | /*translation of the methcall test from The Great Computer Language Shootout |
jhnwkmn | 0:97a4f8cc534c | 2 | */ |
jhnwkmn | 0:97a4f8cc534c | 3 | |
jhnwkmn | 0:97a4f8cc534c | 4 | class Toggle { |
jhnwkmn | 0:97a4f8cc534c | 5 | bool=null |
jhnwkmn | 0:97a4f8cc534c | 6 | } |
jhnwkmn | 0:97a4f8cc534c | 7 | |
jhnwkmn | 0:97a4f8cc534c | 8 | function Toggle::constructor(startstate) { |
jhnwkmn | 0:97a4f8cc534c | 9 | bool = startstate |
jhnwkmn | 0:97a4f8cc534c | 10 | } |
jhnwkmn | 0:97a4f8cc534c | 11 | |
jhnwkmn | 0:97a4f8cc534c | 12 | function Toggle::value() { |
jhnwkmn | 0:97a4f8cc534c | 13 | return bool; |
jhnwkmn | 0:97a4f8cc534c | 14 | } |
jhnwkmn | 0:97a4f8cc534c | 15 | |
jhnwkmn | 0:97a4f8cc534c | 16 | function Toggle::activate() { |
jhnwkmn | 0:97a4f8cc534c | 17 | bool = !bool; |
jhnwkmn | 0:97a4f8cc534c | 18 | return this; |
jhnwkmn | 0:97a4f8cc534c | 19 | } |
jhnwkmn | 0:97a4f8cc534c | 20 | |
jhnwkmn | 0:97a4f8cc534c | 21 | class NthToggle extends Toggle { |
jhnwkmn | 0:97a4f8cc534c | 22 | count_max=null |
jhnwkmn | 0:97a4f8cc534c | 23 | count=0 |
jhnwkmn | 0:97a4f8cc534c | 24 | } |
jhnwkmn | 0:97a4f8cc534c | 25 | |
jhnwkmn | 0:97a4f8cc534c | 26 | function NthToggle::constructor(start_state,max_counter) |
jhnwkmn | 0:97a4f8cc534c | 27 | { |
jhnwkmn | 0:97a4f8cc534c | 28 | base.constructor(start_state); |
jhnwkmn | 0:97a4f8cc534c | 29 | count_max = max_counter |
jhnwkmn | 0:97a4f8cc534c | 30 | } |
jhnwkmn | 0:97a4f8cc534c | 31 | |
jhnwkmn | 0:97a4f8cc534c | 32 | function NthToggle::activate () |
jhnwkmn | 0:97a4f8cc534c | 33 | { |
jhnwkmn | 0:97a4f8cc534c | 34 | ++count; |
jhnwkmn | 0:97a4f8cc534c | 35 | if (count >= count_max ) { |
jhnwkmn | 0:97a4f8cc534c | 36 | base.activate(); |
jhnwkmn | 0:97a4f8cc534c | 37 | count = 0; |
jhnwkmn | 0:97a4f8cc534c | 38 | } |
jhnwkmn | 0:97a4f8cc534c | 39 | return this; |
jhnwkmn | 0:97a4f8cc534c | 40 | } |
jhnwkmn | 0:97a4f8cc534c | 41 | |
jhnwkmn | 0:97a4f8cc534c | 42 | |
jhnwkmn | 0:97a4f8cc534c | 43 | function main() { |
jhnwkmn | 0:97a4f8cc534c | 44 | local n = vargv.len()!=0?vargv[0].tointeger():1 |
jhnwkmn | 0:97a4f8cc534c | 45 | |
jhnwkmn | 0:97a4f8cc534c | 46 | |
jhnwkmn | 0:97a4f8cc534c | 47 | |
jhnwkmn | 0:97a4f8cc534c | 48 | local val = 1; |
jhnwkmn | 0:97a4f8cc534c | 49 | local toggle = Toggle(val); |
jhnwkmn | 0:97a4f8cc534c | 50 | local i = n; |
jhnwkmn | 0:97a4f8cc534c | 51 | while(i--) { |
jhnwkmn | 0:97a4f8cc534c | 52 | val = toggle.activate().value(); |
jhnwkmn | 0:97a4f8cc534c | 53 | |
jhnwkmn | 0:97a4f8cc534c | 54 | } |
jhnwkmn | 0:97a4f8cc534c | 55 | print(toggle.value() ? "true\n" : "false\n"); |
jhnwkmn | 0:97a4f8cc534c | 56 | |
jhnwkmn | 0:97a4f8cc534c | 57 | val = 1; |
jhnwkmn | 0:97a4f8cc534c | 58 | local ntoggle = NthToggle(val, 3); |
jhnwkmn | 0:97a4f8cc534c | 59 | i = n; |
jhnwkmn | 0:97a4f8cc534c | 60 | while(i--) { |
jhnwkmn | 0:97a4f8cc534c | 61 | val = ntoggle.activate().value(); |
jhnwkmn | 0:97a4f8cc534c | 62 | } |
jhnwkmn | 0:97a4f8cc534c | 63 | print(ntoggle.value() ? "true\n" : "false\n"); |
jhnwkmn | 0:97a4f8cc534c | 64 | |
jhnwkmn | 0:97a4f8cc534c | 65 | } |
jhnwkmn | 0:97a4f8cc534c | 66 | local start=clock(); |
jhnwkmn | 0:97a4f8cc534c | 67 | main(); |
jhnwkmn | 0:97a4f8cc534c | 68 | print("TIME="+(clock()-start)+"\n"); |