diff options
author | rubidium <rubidium@openttd.org> | 2009-02-25 01:21:50 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-02-25 01:21:50 +0000 |
commit | a9dd6dc2f595bf8ed3ddfb2949b3e93e0cdfa66d (patch) | |
tree | 0955433146cc59335ddaac1d6e08eb177a9ead61 /src/3rdparty/squirrel/samples | |
parent | b2def946078e4e6f8f0158eb61a52f1863871570 (diff) | |
download | openttd-a9dd6dc2f595bf8ed3ddfb2949b3e93e0cdfa66d.tar.xz |
(svn r15578) -Change: unexternalise squirrel.
Diffstat (limited to 'src/3rdparty/squirrel/samples')
-rw-r--r-- | src/3rdparty/squirrel/samples/ackermann.nut | 22 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/array.nut | 28 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/class.nut | 49 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/classattributes.nut | 35 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/coroutines.nut | 25 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/delegation.nut | 52 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/fibonacci.nut | 15 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/flow.nut | 33 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/generators.nut | 42 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/hello.nut | 1 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/list.nut | 39 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/loops.nut | 32 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/matrix.nut | 44 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/metamethods.nut | 115 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/methcall.nut | 61 | ||||
-rw-r--r-- | src/3rdparty/squirrel/samples/tailstate.nut | 24 |
16 files changed, 617 insertions, 0 deletions
diff --git a/src/3rdparty/squirrel/samples/ackermann.nut b/src/3rdparty/squirrel/samples/ackermann.nut new file mode 100644 index 000000000..c8d056082 --- /dev/null +++ b/src/3rdparty/squirrel/samples/ackermann.nut @@ -0,0 +1,22 @@ +/* +* +* Original Javascript version by David Hedbor(http://www.bagley.org/~doug/shootout/) +* +*/ + +function Ack(M, N) { + if (M == 0) return( N + 1 ); + if (N == 0) return( Ack(M - 1, 1) ); + return( Ack(M - 1, Ack(M, (N - 1))) ); +} + +local n; + +if(ARGS.len()!=0) { + n = ARGS[0].tointeger(); + if(n < 1) n = 1; +} else { + n = 1; +} +print("n="+n+"\n"); +print("Ack(3,"+ n+ "):"+ Ack(3, n)); diff --git a/src/3rdparty/squirrel/samples/array.nut b/src/3rdparty/squirrel/samples/array.nut new file mode 100644 index 000000000..58f160b12 --- /dev/null +++ b/src/3rdparty/squirrel/samples/array.nut @@ -0,0 +1,28 @@ +/* +* +* Original Javascript version by David Hedbor(http://www.bagley.org/~doug/shootout/) +* +*/ +local n, i, k; + +if(ARGS.len()!=0) { + n = ARGS[0].tointeger(); + if(n < 1) n = 1; +} else { + n = 1; +} + +local x = []; x.resize(n); +local y = []; y.resize(n); + +for (i = 0; i < n; i+=1) { + x[i] = i + 1; + y[i] = 0; +} + +for (k = 0 ; k < n; k+=1) { + for (i = n-1; i >= 0; i-=1) { + y[i] = y[i]+ x[i]; + } +} +print(y[0].tostring()+" "+y[n-1]); diff --git a/src/3rdparty/squirrel/samples/class.nut b/src/3rdparty/squirrel/samples/class.nut new file mode 100644 index 000000000..babe54f0b --- /dev/null +++ b/src/3rdparty/squirrel/samples/class.nut @@ -0,0 +1,49 @@ +////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////// +class BaseVector { + constructor(...) + { + if(vargc >= 3) { + x = vargv[0]; + y = vargv[1]; + z = vargv[2]; + } + } + + + x = 0; + y = 0; + z = 0; +} + +class Vector3 extends BaseVector { + function _add(other) + { + if(other instanceof this.getclass()) + return ::Vector3(x+other.x,y+other.y,z+other.z); + else + throw "wrong parameter"; + } + function Print() + { + ::print(x+","+y+","+z+"\n"); + } +} + +local v0 = Vector3(1,2,3) +local v1 = Vector3(11,12,13) +local v2 = v0 + v1; +v2.Print(); + +FakeNamespace <- { + Utils = {} +} + +class FakeNamespace.Utils.SuperClass { + constructor() + { + ::print("FakeNamespace.Utils.SuperClass") + } +} + +local testy = FakeNamespace.Utils.SuperClass(); diff --git a/src/3rdparty/squirrel/samples/classattributes.nut b/src/3rdparty/squirrel/samples/classattributes.nut new file mode 100644 index 000000000..01232002c --- /dev/null +++ b/src/3rdparty/squirrel/samples/classattributes.nut @@ -0,0 +1,35 @@ +class Foo { + //constructor + constructor(a) + { + testy = ["stuff",1,2,3]; + } + //attributes of PrintTesty + </ test = "freakin attribute"/> + function PrintTesty() + { + foreach(i,val in testy) + { + ::print("idx = "+i+" = "+val+" \n"); + } + } + //attributes of testy + </ flippy = 10 , second = [1,2,3] /> + testy = null; + +} + +foreach(member,val in Foo) +{ + ::print(member+"\n"); + local attr; + if((attr = Foo.getattributes(member)) != null) { + foreach(i,v in attr) + { + ::print("\t"+i+" = "+(typeof v)+"\n"); + } + } + else { + ::print("\t<no attributes>\n") + } +} diff --git a/src/3rdparty/squirrel/samples/coroutines.nut b/src/3rdparty/squirrel/samples/coroutines.nut new file mode 100644 index 000000000..c7e4583c1 --- /dev/null +++ b/src/3rdparty/squirrel/samples/coroutines.nut @@ -0,0 +1,25 @@ +function coroutine_test(a,b) +{ + ::print(a+" "+b+"\n"); + local ret = ::suspend("suspend 1"); + ::print("the coroutine says "+ret+"\n"); + ret = ::suspend("suspend 2"); + ::print("the coroutine says "+ret+"\n"); + ret = ::suspend("suspend 3"); + ::print("the coroutine says "+ret+"\n"); + return "I'm done" +} + +local coro = ::newthread(coroutine_test); + +local susparam = coro.call("test","coroutine"); //starts the coroutine + +local i = 1; +do +{ + ::print("suspend passed ["+susparam+"]\n") + susparam = coro.wakeup("ciao "+i); + ++i; +}while(coro.getstatus()=="suspended") + +::print("return passed ["+susparam+"]\n") diff --git a/src/3rdparty/squirrel/samples/delegation.nut b/src/3rdparty/squirrel/samples/delegation.nut new file mode 100644 index 000000000..e9e86ca9a --- /dev/null +++ b/src/3rdparty/squirrel/samples/delegation.nut @@ -0,0 +1,52 @@ + +PEntity <- { + name="noname" + pos={x=0,y=0,z=0} + type="entity" + //methamethod + _typeof=function() + { + return type; + } +} + +function PEntity::PrintPos() +{ + ::print("x="+pos.x+" y="+pos.y+" z="+pos.z+"\n"); +} + +function PEntity::new(name,pos) +{ + local newentity=clone ::PEntity; + if(name) + newentity.name=name; + if(pos) + newentity.pos=pos; + return newentity; +} + +PPlayer <- { + model="warrior.mdl" + weapon="fist" + health=100 + armor=0 + //overrides the parent type + type="player" +} + +function PPlayer::new(name,pos) +{ + local newplayer=delegate ::PEntity.new(name,pos) : clone ::PPlayer; + return newplayer; +} + +local player=PPlayer.new("godzilla",{x=10,y=20,z=30}); + +::print("PLAYER NAME"+player.name+"\n"); +::print("ENTITY TYPE"+typeof player+"\n"); + +player.PrintPos(); + +player.pos.x=123; + +player.PrintPos(); diff --git a/src/3rdparty/squirrel/samples/fibonacci.nut b/src/3rdparty/squirrel/samples/fibonacci.nut new file mode 100644 index 000000000..1db79423e --- /dev/null +++ b/src/3rdparty/squirrel/samples/fibonacci.nut @@ -0,0 +1,15 @@ +/* +* +* Original Javascript version by David Hedbor(http://www.bagley.org/~doug/shootout/) +* +*/ + +function fib(n) +{ + if (n < 2) return 1 + return fib(n-2) + fib(n-1) +} + +local n = ARGS.len()!=0?ARGS[0].tointeger():1 + +print(fib(n)+"\n") diff --git a/src/3rdparty/squirrel/samples/flow.nut b/src/3rdparty/squirrel/samples/flow.nut new file mode 100644 index 000000000..1cd7cfcf4 --- /dev/null +++ b/src/3rdparty/squirrel/samples/flow.nut @@ -0,0 +1,33 @@ +function min(x,y) + return x<y?x:y; + +function max(x,y) + return x>y?x:y; + +if(min(100,200)>max(50,20)) + print("I'm useless statement just to show up the if/else\n"); +else + print("squirrel!!\n"); + +print("\n") + +function typy(obj) +{ + switch(typeof obj) + { + case "integer": + case "float": + return "is a number"; + case "table": + case "array": + return "is a container"; + default: + return "is other stuff" + } +} + +local a=1,b={},c=function(a,b){return a+b;} + +print("a "+typy(a)+"\n"); +print("b "+typy(b)+"\n"); +print("c "+typy(c)+"\n"); diff --git a/src/3rdparty/squirrel/samples/generators.nut b/src/3rdparty/squirrel/samples/generators.nut new file mode 100644 index 000000000..9853e0964 --- /dev/null +++ b/src/3rdparty/squirrel/samples/generators.nut @@ -0,0 +1,42 @@ +/* +*Random number function from The Great Computer Language shootout +*converted to a generator func +*/ + +function gen_random(max) { + local last=42 + local IM = 139968; + local IA = 3877; + local IC = 29573; + for(;;){ //loops forever + yield (max * (last = (last * IA + IC) % IM) / IM); + } +} + +local randtor=gen_random(100); + +print("RAND NUMBERS \n") + +for(local i=0;i<10;i+=1) + print(">"+resume randtor+"\n"); + +print("FIBONACCI \n") +function fiboz(n) +{ + local prev=0; + local curr=1; + yield 1; + + for(local i=0;i<n-1;i+=1) + { + local res=prev+curr; + prev=curr; + yield curr=res; + } + return prev+curr; +} + +foreach(val in fiboz(10)) +{ + ::print(">"+val+"\n"); +} diff --git a/src/3rdparty/squirrel/samples/hello.nut b/src/3rdparty/squirrel/samples/hello.nut new file mode 100644 index 000000000..1dc45ac13 --- /dev/null +++ b/src/3rdparty/squirrel/samples/hello.nut @@ -0,0 +1 @@ +print("Hello World!")
\ No newline at end of file diff --git a/src/3rdparty/squirrel/samples/list.nut b/src/3rdparty/squirrel/samples/list.nut new file mode 100644 index 000000000..8ab1ab517 --- /dev/null +++ b/src/3rdparty/squirrel/samples/list.nut @@ -0,0 +1,39 @@ +/*translation of the list test from The Great Computer Language Shootout +*/ + +function compare_arr(a1,a2) +{ + foreach(i,val in a1) + if(val!=a2[i])return null; + return 1; +} + +function test() +{ + local size=10000 + local l1=[]; l1.resize(size); + for(local i=0;i<size;i+=1) l1[i]=i; + local l2=clone l1; + local l3=[] + + l2.reverse(); + while(l2.len()>0) + l3.append(l2.pop()); + while(l3.len()>0) + l2.append(l3.pop()); + l1.reverse(); + + if(compare_arr(l1,l2)) + return l1.len(); + return null; +} + +local n = ARGS.len()!=0?ARGS[0].tointeger():1 +for(local i=0;i<n;i+=1) + if(!test()) + { + print("failed"); + return; + } + +print("oki doki"); diff --git a/src/3rdparty/squirrel/samples/loops.nut b/src/3rdparty/squirrel/samples/loops.nut new file mode 100644 index 000000000..50074c935 --- /dev/null +++ b/src/3rdparty/squirrel/samples/loops.nut @@ -0,0 +1,32 @@ +local arr=["one","two","three"] + +::print("FOREACH\n"); + +foreach(i,val in arr) +{ + ::print("index ["+i+"]="+val+"\n"); +} + +::print("FOR\n"); + +for(local i=0;i<arr.len();i+=1) +{ + ::print("index ["+i+"]="+arr[i]+"\n"); +} + +::print("WHILE\n"); + +local i=0; +while(i<arr.len()) +{ + ::print("index ["+i+"]="+arr[i]+"\n"); + i+=1; +} +::print("DO WHILE\n"); + +local i=0; +do +{ + ::print("index ["+i+"]="+arr[i]+"\n"); + i+=1; +}while(i<arr.len()); diff --git a/src/3rdparty/squirrel/samples/matrix.nut b/src/3rdparty/squirrel/samples/matrix.nut new file mode 100644 index 000000000..c8b16c65a --- /dev/null +++ b/src/3rdparty/squirrel/samples/matrix.nut @@ -0,0 +1,44 @@ +/* +* +* Original Javascript version by David Hedbor(http://www.bagley.org/~doug/shootout/) +* +*/ +local SIZE=30; + +function mkmatrix(rows, cols) { + local i, j, count = 1; + local m = []; m.resize(rows); + for (i = 0; i < rows; i+=1) { + m[i] = [];m[i].resize(cols) + for (j = 0; j < cols; j+=1) { + m[i][j] = count+=1; + } + } + return m; +} + +function mmult(rows, cols, m1, m2, m3) { + local i, j, k, val; + for (i = 0; i < rows; i+=1) { + for (j = 0; j < cols; j+=1) { + val = 0; + for (k = 0; k < cols; k+=1) { + val += m1[i][k] * m2[k][j]; + } + m3[i][j] = val; + } + } + return m3; +} + +local n = ARGS.len()!=0?ARGS[0].tointeger():1 + +local m1 = mkmatrix(SIZE, SIZE); +local m2 = mkmatrix(SIZE, SIZE); +local mm = mkmatrix(SIZE, SIZE); + +for (local i = 0; i < n; i+=1) { + mmult(SIZE, SIZE, m1, m2, mm); +} + +print(mm[0][0]+" "+mm[2][3]+" "+mm[3][2]+" "+mm[4][4]); diff --git a/src/3rdparty/squirrel/samples/metamethods.nut b/src/3rdparty/squirrel/samples/metamethods.nut new file mode 100644 index 000000000..53439520e --- /dev/null +++ b/src/3rdparty/squirrel/samples/metamethods.nut @@ -0,0 +1,115 @@ + +local base_vec={ + function _add(n) + { + return { + x=x+n.x, + y=y+n.y, + z=z+n.z, + } + } + function _sub(n) + { + return { + x=x-n.x, + y=y-n.y, + z=z-n.z, + } + } + function _div(n) + { + return { + x=x/n.x, + y=y/n.y, + z=z/n.z, + } + } + function _mul(n) + { + return { + x=x*n.x, + y=y*n.y, + z=z*n.z, + } + } + function _modulo(n) + { + return { + x=x%n, + y=y%n, + z=z%n, + } + } + function _typeof() {return "vector";} + function _get(key) + { + if(key==100) + { + return test_field; + } + }, + function _set(key,val) + { + ::print("key = "+key+"\n"); + ::print("val = "+val+"\n") + if(key==100) + { + return test_field=val; + } + } + test_field="nothing" +} + +function vector(_x,_y,_z):(base_vec) +{ + return delegate base_vec : {x=_x,y=_y,z=_z } +} +//////////////////////////////////////////////////////////// + +local v1=vector(1.5,2.5,3.5); +local v2=vector(1.5,2.5,3.5); + +local r=v1+v2; + + +foreach(i,val in r) +{ + print(i+" = "+val+"\n"); +} + +r=v1*v2; + +foreach(i,val in r) +{ + print(i+" = "+val+"\n"); +} + +r=v1/v2; + +foreach(i,val in r) +{ + print(i+" = "+val+"\n"); +} + +r=v1-v2; + +foreach(i,val in r) +{ + print(i+" = "+val+"\n"); +} + +r=v1%2; + +foreach(i,val in r) +{ + print(i+" = "+val+"\n"); +} + +print(v1[100]+"\n"); +v1[100]="set SUCCEEDED"; +print(v1[100]+"\n"); + +if(typeof v1=="vector") + print("<SUCCEEDED>\n"); +else + print("<FAILED>\n"); diff --git a/src/3rdparty/squirrel/samples/methcall.nut b/src/3rdparty/squirrel/samples/methcall.nut new file mode 100644 index 000000000..131d26228 --- /dev/null +++ b/src/3rdparty/squirrel/samples/methcall.nut @@ -0,0 +1,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"); diff --git a/src/3rdparty/squirrel/samples/tailstate.nut b/src/3rdparty/squirrel/samples/tailstate.nut new file mode 100644 index 000000000..59fb962ef --- /dev/null +++ b/src/3rdparty/squirrel/samples/tailstate.nut @@ -0,0 +1,24 @@ +function state1() +{ + ::suspend("state1"); + return state2(); +} + +function state2() +{ + ::suspend("state2"); + return state3(); +} + +function state3() +{ + ::suspend("state3"); + return state1(); +} + +local statethread = ::newthread(state1) + +::print(statethread.call()+"\n"); + +for(local i = 0; i < 10000; i++) + ::print(statethread.wakeup()+"\n"); |