summaryrefslogtreecommitdiff
path: root/src/3rdparty/squirrel/samples/methcall.nut
blob: 131d262288a3a0e22124a1ee15111581aee3cccf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*translation of the methcall test from The Great Computer Language Shootout
*/

Toggle <- {
	bool=null
}

function Toggle::value() {
	return bool;
}

function Toggle::activate() {
	bool = !bool;
	return this;
}

function Toggle::new(startstate) {
	local newo=clone this;
	newo.bool = startstate;
	return newo;
}

NthToggle <- {
	count_max=null
	count=0
}

function NthToggle::new(start_state,max_counter)
{
	local newo=delegate ::Toggle.new(start_state) : clone this;
	newo.count_max <- max_counter
	return newo;
}

function NthToggle::activate ()
{
	count+=1
    if (count >= count_max) {
      bool = !bool;
      count = 0;
    }
    return this;
}


local n = ARGS.len()!=0?ARGS[0].tointeger():1

local val = 1;
local toggle = Toggle.new(val);
for (local i=0; i<n; i+=1) {
  val = toggle.activate().value();

}
print(toggle.value() ? "true\n" : "false\n");

val = 1;
local ntoggle = NthToggle.new(val, 3);
for (local i=0; i<n; i+=1) {
  val = ntoggle.activate().value();
}
print(ntoggle.value() ? "true\n" : "false\n");