The Squirrel interpreter. See http://www.squirrel-lang.org/
samples/metamethods.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 | |
jhnwkmn | 0:97a4f8cc534c | 2 | local base_vec={ |
jhnwkmn | 0:97a4f8cc534c | 3 | function _add(n) |
jhnwkmn | 0:97a4f8cc534c | 4 | { |
jhnwkmn | 0:97a4f8cc534c | 5 | return { |
jhnwkmn | 0:97a4f8cc534c | 6 | x=x+n.x, |
jhnwkmn | 0:97a4f8cc534c | 7 | y=y+n.y, |
jhnwkmn | 0:97a4f8cc534c | 8 | z=z+n.z, |
jhnwkmn | 0:97a4f8cc534c | 9 | } |
jhnwkmn | 0:97a4f8cc534c | 10 | } |
jhnwkmn | 0:97a4f8cc534c | 11 | function _sub(n) |
jhnwkmn | 0:97a4f8cc534c | 12 | { |
jhnwkmn | 0:97a4f8cc534c | 13 | return { |
jhnwkmn | 0:97a4f8cc534c | 14 | x=x-n.x, |
jhnwkmn | 0:97a4f8cc534c | 15 | y=y-n.y, |
jhnwkmn | 0:97a4f8cc534c | 16 | z=z-n.z, |
jhnwkmn | 0:97a4f8cc534c | 17 | } |
jhnwkmn | 0:97a4f8cc534c | 18 | } |
jhnwkmn | 0:97a4f8cc534c | 19 | function _div(n) |
jhnwkmn | 0:97a4f8cc534c | 20 | { |
jhnwkmn | 0:97a4f8cc534c | 21 | return { |
jhnwkmn | 0:97a4f8cc534c | 22 | x=x/n.x, |
jhnwkmn | 0:97a4f8cc534c | 23 | y=y/n.y, |
jhnwkmn | 0:97a4f8cc534c | 24 | z=z/n.z, |
jhnwkmn | 0:97a4f8cc534c | 25 | } |
jhnwkmn | 0:97a4f8cc534c | 26 | } |
jhnwkmn | 0:97a4f8cc534c | 27 | function _mul(n) |
jhnwkmn | 0:97a4f8cc534c | 28 | { |
jhnwkmn | 0:97a4f8cc534c | 29 | return { |
jhnwkmn | 0:97a4f8cc534c | 30 | x=x*n.x, |
jhnwkmn | 0:97a4f8cc534c | 31 | y=y*n.y, |
jhnwkmn | 0:97a4f8cc534c | 32 | z=z*n.z, |
jhnwkmn | 0:97a4f8cc534c | 33 | } |
jhnwkmn | 0:97a4f8cc534c | 34 | } |
jhnwkmn | 0:97a4f8cc534c | 35 | function _modulo(n) |
jhnwkmn | 0:97a4f8cc534c | 36 | { |
jhnwkmn | 0:97a4f8cc534c | 37 | return { |
jhnwkmn | 0:97a4f8cc534c | 38 | x=x%n, |
jhnwkmn | 0:97a4f8cc534c | 39 | y=y%n, |
jhnwkmn | 0:97a4f8cc534c | 40 | z=z%n, |
jhnwkmn | 0:97a4f8cc534c | 41 | } |
jhnwkmn | 0:97a4f8cc534c | 42 | } |
jhnwkmn | 0:97a4f8cc534c | 43 | function _typeof() {return "vector";} |
jhnwkmn | 0:97a4f8cc534c | 44 | function _get(key) |
jhnwkmn | 0:97a4f8cc534c | 45 | { |
jhnwkmn | 0:97a4f8cc534c | 46 | if(key==100) |
jhnwkmn | 0:97a4f8cc534c | 47 | { |
jhnwkmn | 0:97a4f8cc534c | 48 | return test_field; |
jhnwkmn | 0:97a4f8cc534c | 49 | } |
jhnwkmn | 0:97a4f8cc534c | 50 | }, |
jhnwkmn | 0:97a4f8cc534c | 51 | function _set(key,val) |
jhnwkmn | 0:97a4f8cc534c | 52 | { |
jhnwkmn | 0:97a4f8cc534c | 53 | ::print("key = "+key+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 54 | ::print("val = "+val+"\n") |
jhnwkmn | 0:97a4f8cc534c | 55 | if(key==100) |
jhnwkmn | 0:97a4f8cc534c | 56 | { |
jhnwkmn | 0:97a4f8cc534c | 57 | return test_field=val; |
jhnwkmn | 0:97a4f8cc534c | 58 | } |
jhnwkmn | 0:97a4f8cc534c | 59 | } |
jhnwkmn | 0:97a4f8cc534c | 60 | test_field="nothing" |
jhnwkmn | 0:97a4f8cc534c | 61 | } |
jhnwkmn | 0:97a4f8cc534c | 62 | |
jhnwkmn | 0:97a4f8cc534c | 63 | function vector(_x,_y,_z) |
jhnwkmn | 0:97a4f8cc534c | 64 | { |
jhnwkmn | 0:97a4f8cc534c | 65 | return {x=_x,y=_y,z=_z }.setdelegate(base_vec); |
jhnwkmn | 0:97a4f8cc534c | 66 | } |
jhnwkmn | 0:97a4f8cc534c | 67 | //////////////////////////////////////////////////////////// |
jhnwkmn | 0:97a4f8cc534c | 68 | |
jhnwkmn | 0:97a4f8cc534c | 69 | local v1=vector(1.5,2.5,3.5); |
jhnwkmn | 0:97a4f8cc534c | 70 | local v2=vector(1.5,2.5,3.5); |
jhnwkmn | 0:97a4f8cc534c | 71 | |
jhnwkmn | 0:97a4f8cc534c | 72 | local r=v1+v2; |
jhnwkmn | 0:97a4f8cc534c | 73 | |
jhnwkmn | 0:97a4f8cc534c | 74 | |
jhnwkmn | 0:97a4f8cc534c | 75 | foreach(i,val in r) |
jhnwkmn | 0:97a4f8cc534c | 76 | { |
jhnwkmn | 0:97a4f8cc534c | 77 | print(i+" = "+val+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 78 | } |
jhnwkmn | 0:97a4f8cc534c | 79 | |
jhnwkmn | 0:97a4f8cc534c | 80 | r=v1*v2; |
jhnwkmn | 0:97a4f8cc534c | 81 | |
jhnwkmn | 0:97a4f8cc534c | 82 | foreach(i,val in r) |
jhnwkmn | 0:97a4f8cc534c | 83 | { |
jhnwkmn | 0:97a4f8cc534c | 84 | print(i+" = "+val+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 85 | } |
jhnwkmn | 0:97a4f8cc534c | 86 | |
jhnwkmn | 0:97a4f8cc534c | 87 | r=v1/v2; |
jhnwkmn | 0:97a4f8cc534c | 88 | |
jhnwkmn | 0:97a4f8cc534c | 89 | foreach(i,val in r) |
jhnwkmn | 0:97a4f8cc534c | 90 | { |
jhnwkmn | 0:97a4f8cc534c | 91 | print(i+" = "+val+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 92 | } |
jhnwkmn | 0:97a4f8cc534c | 93 | |
jhnwkmn | 0:97a4f8cc534c | 94 | r=v1-v2; |
jhnwkmn | 0:97a4f8cc534c | 95 | |
jhnwkmn | 0:97a4f8cc534c | 96 | foreach(i,val in r) |
jhnwkmn | 0:97a4f8cc534c | 97 | { |
jhnwkmn | 0:97a4f8cc534c | 98 | print(i+" = "+val+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 99 | } |
jhnwkmn | 0:97a4f8cc534c | 100 | |
jhnwkmn | 0:97a4f8cc534c | 101 | r=v1%2; |
jhnwkmn | 0:97a4f8cc534c | 102 | |
jhnwkmn | 0:97a4f8cc534c | 103 | foreach(i,val in r) |
jhnwkmn | 0:97a4f8cc534c | 104 | { |
jhnwkmn | 0:97a4f8cc534c | 105 | print(i+" = "+val+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 106 | } |
jhnwkmn | 0:97a4f8cc534c | 107 | |
jhnwkmn | 0:97a4f8cc534c | 108 | print(v1[100]+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 109 | v1[100]="set SUCCEEDED"; |
jhnwkmn | 0:97a4f8cc534c | 110 | print(v1[100]+"\n"); |
jhnwkmn | 0:97a4f8cc534c | 111 | |
jhnwkmn | 0:97a4f8cc534c | 112 | if(typeof v1=="vector") |
jhnwkmn | 0:97a4f8cc534c | 113 | print("<SUCCEEDED>\n"); |
jhnwkmn | 0:97a4f8cc534c | 114 | else |
jhnwkmn | 0:97a4f8cc534c | 115 | print("<FAILED>\n"); |