Tamar 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/.gitmodules +3 -0
  2. data/HISTORY +22 -5
  3. data/Tamar.gemspec +114 -1
  4. data/VERSION +1 -1
  5. data/src/lua/CMakeLists.txt +176 -0
  6. data/src/lua/COPYRIGHT +34 -0
  7. data/src/lua/FindReadline.cmake +25 -0
  8. data/src/lua/HISTORY +183 -0
  9. data/src/lua/INSTALL +99 -0
  10. data/src/lua/Makefile +128 -0
  11. data/src/lua/README +46 -0
  12. data/src/lua/dist.cmake +450 -0
  13. data/src/lua/dist.info +10 -0
  14. data/src/lua/doc/amazon.gif +0 -0
  15. data/src/lua/doc/contents.html +499 -0
  16. data/src/lua/doc/cover.png +0 -0
  17. data/src/lua/doc/logo.gif +0 -0
  18. data/src/lua/doc/lua.1 +163 -0
  19. data/src/lua/doc/lua.css +41 -0
  20. data/src/lua/doc/lua.html +172 -0
  21. data/src/lua/doc/luac.1 +136 -0
  22. data/src/lua/doc/luac.html +145 -0
  23. data/src/lua/doc/manual.css +13 -0
  24. data/src/lua/doc/manual.html +8801 -0
  25. data/src/lua/doc/readme.html +40 -0
  26. data/src/lua/etc/Makefile +44 -0
  27. data/src/lua/etc/README +37 -0
  28. data/src/lua/etc/all.c +38 -0
  29. data/src/lua/etc/lua.hpp +9 -0
  30. data/src/lua/etc/lua.ico +0 -0
  31. data/src/lua/etc/lua.pc +31 -0
  32. data/src/lua/etc/luavs.bat +28 -0
  33. data/src/lua/etc/min.c +39 -0
  34. data/src/lua/etc/noparser.c +50 -0
  35. data/src/lua/etc/strict.lua +41 -0
  36. data/src/lua/src/Makefile +182 -0
  37. data/src/lua/src/lapi.c +1087 -0
  38. data/src/lua/src/lapi.h +16 -0
  39. data/src/lua/src/lauxlib.c +652 -0
  40. data/src/lua/src/lauxlib.h +174 -0
  41. data/src/lua/src/lbaselib.c +653 -0
  42. data/src/lua/src/lcode.c +831 -0
  43. data/src/lua/src/lcode.h +76 -0
  44. data/src/lua/src/ldblib.c +398 -0
  45. data/src/lua/src/ldebug.c +638 -0
  46. data/src/lua/src/ldebug.h +33 -0
  47. data/src/lua/src/ldo.c +518 -0
  48. data/src/lua/src/ldo.h +57 -0
  49. data/src/lua/src/ldump.c +164 -0
  50. data/src/lua/src/lfunc.c +174 -0
  51. data/src/lua/src/lfunc.h +34 -0
  52. data/src/lua/src/lgc.c +711 -0
  53. data/src/lua/src/lgc.h +110 -0
  54. data/src/lua/src/linit.c +38 -0
  55. data/src/lua/src/liolib.c +556 -0
  56. data/src/lua/src/llex.c +463 -0
  57. data/src/lua/src/llex.h +81 -0
  58. data/src/lua/src/llimits.h +128 -0
  59. data/src/lua/src/lmathlib.c +263 -0
  60. data/src/lua/src/lmem.c +86 -0
  61. data/src/lua/src/lmem.h +49 -0
  62. data/src/lua/src/loadlib.c +666 -0
  63. data/src/lua/src/loadlib_rel.c +719 -0
  64. data/src/lua/src/lobject.c +214 -0
  65. data/src/lua/src/lobject.h +381 -0
  66. data/src/lua/src/lopcodes.c +102 -0
  67. data/src/lua/src/lopcodes.h +268 -0
  68. data/src/lua/src/loslib.c +243 -0
  69. data/src/lua/src/lparser.c +1339 -0
  70. data/src/lua/src/lparser.h +82 -0
  71. data/src/lua/src/lstate.c +214 -0
  72. data/src/lua/src/lstate.h +169 -0
  73. data/src/lua/src/lstring.c +111 -0
  74. data/src/lua/src/lstring.h +31 -0
  75. data/src/lua/src/lstrlib.c +871 -0
  76. data/src/lua/src/ltable.c +588 -0
  77. data/src/lua/src/ltable.h +40 -0
  78. data/src/lua/src/ltablib.c +287 -0
  79. data/src/lua/src/ltm.c +75 -0
  80. data/src/lua/src/ltm.h +54 -0
  81. data/src/lua/src/lua.c +392 -0
  82. data/src/lua/src/lua.def +131 -0
  83. data/src/lua/src/lua.h +388 -0
  84. data/src/lua/src/lua.rc +28 -0
  85. data/src/lua/src/lua_dll.rc +26 -0
  86. data/src/lua/src/luac.c +200 -0
  87. data/src/lua/src/luac.rc +1 -0
  88. data/src/lua/src/luaconf.h.in +724 -0
  89. data/src/lua/src/luaconf.h.orig +763 -0
  90. data/src/lua/src/lualib.h +53 -0
  91. data/src/lua/src/lundump.c +227 -0
  92. data/src/lua/src/lundump.h +36 -0
  93. data/src/lua/src/lvm.c +766 -0
  94. data/src/lua/src/lvm.h +36 -0
  95. data/src/lua/src/lzio.c +82 -0
  96. data/src/lua/src/lzio.h +67 -0
  97. data/src/lua/src/print.c +227 -0
  98. data/src/lua/test/README +26 -0
  99. data/src/lua/test/bisect.lua +27 -0
  100. data/src/lua/test/cf.lua +16 -0
  101. data/src/lua/test/echo.lua +5 -0
  102. data/src/lua/test/env.lua +7 -0
  103. data/src/lua/test/factorial.lua +32 -0
  104. data/src/lua/test/fib.lua +40 -0
  105. data/src/lua/test/fibfor.lua +13 -0
  106. data/src/lua/test/globals.lua +13 -0
  107. data/src/lua/test/hello.lua +3 -0
  108. data/src/lua/test/life.lua +111 -0
  109. data/src/lua/test/luac.lua +7 -0
  110. data/src/lua/test/printf.lua +7 -0
  111. data/src/lua/test/readonly.lua +12 -0
  112. data/src/lua/test/sieve.lua +29 -0
  113. data/src/lua/test/sort.lua +66 -0
  114. data/src/lua/test/table.lua +12 -0
  115. data/src/lua/test/trace-calls.lua +32 -0
  116. data/src/lua/test/trace-globals.lua +38 -0
  117. data/src/lua/test/xd.lua +14 -0
  118. metadata +115 -2
data/src/lua/src/lvm.h ADDED
@@ -0,0 +1,36 @@
1
+ /*
2
+ ** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Lua virtual machine
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+ #ifndef lvm_h
8
+ #define lvm_h
9
+
10
+
11
+ #include "ldo.h"
12
+ #include "lobject.h"
13
+ #include "ltm.h"
14
+
15
+
16
+ #define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
17
+
18
+ #define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
19
+ (((o) = luaV_tonumber(o,n)) != NULL))
20
+
21
+ #define equalobj(L,o1,o2) \
22
+ (ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
23
+
24
+
25
+ LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
26
+ LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
27
+ LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
28
+ LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
29
+ LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
30
+ StkId val);
31
+ LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
32
+ StkId val);
33
+ LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
34
+ LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
35
+
36
+ #endif
@@ -0,0 +1,82 @@
1
+ /*
2
+ ** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** a generic input stream interface
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #include <string.h>
9
+
10
+ #define lzio_c
11
+ #define LUA_CORE
12
+
13
+ #include "lua.h"
14
+
15
+ #include "llimits.h"
16
+ #include "lmem.h"
17
+ #include "lstate.h"
18
+ #include "lzio.h"
19
+
20
+
21
+ int luaZ_fill (ZIO *z) {
22
+ size_t size;
23
+ lua_State *L = z->L;
24
+ const char *buff;
25
+ lua_unlock(L);
26
+ buff = z->reader(L, z->data, &size);
27
+ lua_lock(L);
28
+ if (buff == NULL || size == 0) return EOZ;
29
+ z->n = size - 1;
30
+ z->p = buff;
31
+ return char2int(*(z->p++));
32
+ }
33
+
34
+
35
+ int luaZ_lookahead (ZIO *z) {
36
+ if (z->n == 0) {
37
+ if (luaZ_fill(z) == EOZ)
38
+ return EOZ;
39
+ else {
40
+ z->n++; /* luaZ_fill removed first byte; put back it */
41
+ z->p--;
42
+ }
43
+ }
44
+ return char2int(*z->p);
45
+ }
46
+
47
+
48
+ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
49
+ z->L = L;
50
+ z->reader = reader;
51
+ z->data = data;
52
+ z->n = 0;
53
+ z->p = NULL;
54
+ }
55
+
56
+
57
+ /* --------------------------------------------------------------- read --- */
58
+ size_t luaZ_read (ZIO *z, void *b, size_t n) {
59
+ while (n) {
60
+ size_t m;
61
+ if (luaZ_lookahead(z) == EOZ)
62
+ return n; /* return number of missing bytes */
63
+ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
64
+ memcpy(b, z->p, m);
65
+ z->n -= m;
66
+ z->p += m;
67
+ b = (char *)b + m;
68
+ n -= m;
69
+ }
70
+ return 0;
71
+ }
72
+
73
+ /* ------------------------------------------------------------------------ */
74
+ char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
75
+ if (n > buff->buffsize) {
76
+ if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
77
+ luaZ_resizebuffer(L, buff, n);
78
+ }
79
+ return buff->buffer;
80
+ }
81
+
82
+
@@ -0,0 +1,67 @@
1
+ /*
2
+ ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
3
+ ** Buffered streams
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+
8
+ #ifndef lzio_h
9
+ #define lzio_h
10
+
11
+ #include "lua.h"
12
+
13
+ #include "lmem.h"
14
+
15
+
16
+ #define EOZ (-1) /* end of stream */
17
+
18
+ typedef struct Zio ZIO;
19
+
20
+ #define char2int(c) cast(int, cast(unsigned char, (c)))
21
+
22
+ #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
23
+
24
+ typedef struct Mbuffer {
25
+ char *buffer;
26
+ size_t n;
27
+ size_t buffsize;
28
+ } Mbuffer;
29
+
30
+ #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
31
+
32
+ #define luaZ_buffer(buff) ((buff)->buffer)
33
+ #define luaZ_sizebuffer(buff) ((buff)->buffsize)
34
+ #define luaZ_bufflen(buff) ((buff)->n)
35
+
36
+ #define luaZ_resetbuffer(buff) ((buff)->n = 0)
37
+
38
+
39
+ #define luaZ_resizebuffer(L, buff, size) \
40
+ (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
41
+ (buff)->buffsize = size)
42
+
43
+ #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
44
+
45
+
46
+ LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
47
+ LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
48
+ void *data);
49
+ LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
50
+ LUAI_FUNC int luaZ_lookahead (ZIO *z);
51
+
52
+
53
+
54
+ /* --------- Private Part ------------------ */
55
+
56
+ struct Zio {
57
+ size_t n; /* bytes still unread */
58
+ const char *p; /* current position in buffer */
59
+ lua_Reader reader;
60
+ void* data; /* additional data */
61
+ lua_State *L; /* Lua state (for reader) */
62
+ };
63
+
64
+
65
+ LUAI_FUNC int luaZ_fill (ZIO *z);
66
+
67
+ #endif
@@ -0,0 +1,227 @@
1
+ /*
2
+ ** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
3
+ ** print bytecodes
4
+ ** See Copyright Notice in lua.h
5
+ */
6
+
7
+ #include <ctype.h>
8
+ #include <stdio.h>
9
+
10
+ #define luac_c
11
+ #define LUA_CORE
12
+
13
+ #include "ldebug.h"
14
+ #include "lobject.h"
15
+ #include "lopcodes.h"
16
+ #include "lundump.h"
17
+
18
+ #define PrintFunction luaU_print
19
+
20
+ #define Sizeof(x) ((int)sizeof(x))
21
+ #define VOID(p) ((const void*)(p))
22
+
23
+ static void PrintString(const TString* ts)
24
+ {
25
+ const char* s=getstr(ts);
26
+ size_t i,n=ts->tsv.len;
27
+ putchar('"');
28
+ for (i=0; i<n; i++)
29
+ {
30
+ int c=s[i];
31
+ switch (c)
32
+ {
33
+ case '"': printf("\\\""); break;
34
+ case '\\': printf("\\\\"); break;
35
+ case '\a': printf("\\a"); break;
36
+ case '\b': printf("\\b"); break;
37
+ case '\f': printf("\\f"); break;
38
+ case '\n': printf("\\n"); break;
39
+ case '\r': printf("\\r"); break;
40
+ case '\t': printf("\\t"); break;
41
+ case '\v': printf("\\v"); break;
42
+ default: if (isprint((unsigned char)c))
43
+ putchar(c);
44
+ else
45
+ printf("\\%03u",(unsigned char)c);
46
+ }
47
+ }
48
+ putchar('"');
49
+ }
50
+
51
+ static void PrintConstant(const Proto* f, int i)
52
+ {
53
+ const TValue* o=&f->k[i];
54
+ switch (ttype(o))
55
+ {
56
+ case LUA_TNIL:
57
+ printf("nil");
58
+ break;
59
+ case LUA_TBOOLEAN:
60
+ printf(bvalue(o) ? "true" : "false");
61
+ break;
62
+ case LUA_TNUMBER:
63
+ printf(LUA_NUMBER_FMT,nvalue(o));
64
+ break;
65
+ case LUA_TSTRING:
66
+ PrintString(rawtsvalue(o));
67
+ break;
68
+ default: /* cannot happen */
69
+ printf("? type=%d",ttype(o));
70
+ break;
71
+ }
72
+ }
73
+
74
+ static void PrintCode(const Proto* f)
75
+ {
76
+ const Instruction* code=f->code;
77
+ int pc,n=f->sizecode;
78
+ for (pc=0; pc<n; pc++)
79
+ {
80
+ Instruction i=code[pc];
81
+ OpCode o=GET_OPCODE(i);
82
+ int a=GETARG_A(i);
83
+ int b=GETARG_B(i);
84
+ int c=GETARG_C(i);
85
+ int bx=GETARG_Bx(i);
86
+ int sbx=GETARG_sBx(i);
87
+ int line=getline(f,pc);
88
+ printf("\t%d\t",pc+1);
89
+ if (line>0) printf("[%d]\t",line); else printf("[-]\t");
90
+ printf("%-9s\t",luaP_opnames[o]);
91
+ switch (getOpMode(o))
92
+ {
93
+ case iABC:
94
+ printf("%d",a);
95
+ if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
96
+ if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
97
+ break;
98
+ case iABx:
99
+ if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
100
+ break;
101
+ case iAsBx:
102
+ if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
103
+ break;
104
+ }
105
+ switch (o)
106
+ {
107
+ case OP_LOADK:
108
+ printf("\t; "); PrintConstant(f,bx);
109
+ break;
110
+ case OP_GETUPVAL:
111
+ case OP_SETUPVAL:
112
+ printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
113
+ break;
114
+ case OP_GETGLOBAL:
115
+ case OP_SETGLOBAL:
116
+ printf("\t; %s",svalue(&f->k[bx]));
117
+ break;
118
+ case OP_GETTABLE:
119
+ case OP_SELF:
120
+ if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
121
+ break;
122
+ case OP_SETTABLE:
123
+ case OP_ADD:
124
+ case OP_SUB:
125
+ case OP_MUL:
126
+ case OP_DIV:
127
+ case OP_POW:
128
+ case OP_EQ:
129
+ case OP_LT:
130
+ case OP_LE:
131
+ if (ISK(b) || ISK(c))
132
+ {
133
+ printf("\t; ");
134
+ if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
135
+ printf(" ");
136
+ if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
137
+ }
138
+ break;
139
+ case OP_JMP:
140
+ case OP_FORLOOP:
141
+ case OP_FORPREP:
142
+ printf("\t; to %d",sbx+pc+2);
143
+ break;
144
+ case OP_CLOSURE:
145
+ printf("\t; %p",VOID(f->p[bx]));
146
+ break;
147
+ case OP_SETLIST:
148
+ if (c==0) printf("\t; %d",(int)code[++pc]);
149
+ else printf("\t; %d",c);
150
+ break;
151
+ default:
152
+ break;
153
+ }
154
+ printf("\n");
155
+ }
156
+ }
157
+
158
+ #define SS(x) (x==1)?"":"s"
159
+ #define S(x) x,SS(x)
160
+
161
+ static void PrintHeader(const Proto* f)
162
+ {
163
+ const char* s=getstr(f->source);
164
+ if (*s=='@' || *s=='=')
165
+ s++;
166
+ else if (*s==LUA_SIGNATURE[0])
167
+ s="(bstring)";
168
+ else
169
+ s="(string)";
170
+ printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
171
+ (f->linedefined==0)?"main":"function",s,
172
+ f->linedefined,f->lastlinedefined,
173
+ S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
174
+ printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
175
+ f->numparams,f->is_vararg?"+":"",SS(f->numparams),
176
+ S(f->maxstacksize),S(f->nups));
177
+ printf("%d local%s, %d constant%s, %d function%s\n",
178
+ S(f->sizelocvars),S(f->sizek),S(f->sizep));
179
+ }
180
+
181
+ static void PrintConstants(const Proto* f)
182
+ {
183
+ int i,n=f->sizek;
184
+ printf("constants (%d) for %p:\n",n,VOID(f));
185
+ for (i=0; i<n; i++)
186
+ {
187
+ printf("\t%d\t",i+1);
188
+ PrintConstant(f,i);
189
+ printf("\n");
190
+ }
191
+ }
192
+
193
+ static void PrintLocals(const Proto* f)
194
+ {
195
+ int i,n=f->sizelocvars;
196
+ printf("locals (%d) for %p:\n",n,VOID(f));
197
+ for (i=0; i<n; i++)
198
+ {
199
+ printf("\t%d\t%s\t%d\t%d\n",
200
+ i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
201
+ }
202
+ }
203
+
204
+ static void PrintUpvalues(const Proto* f)
205
+ {
206
+ int i,n=f->sizeupvalues;
207
+ printf("upvalues (%d) for %p:\n",n,VOID(f));
208
+ if (f->upvalues==NULL) return;
209
+ for (i=0; i<n; i++)
210
+ {
211
+ printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
212
+ }
213
+ }
214
+
215
+ void PrintFunction(const Proto* f, int full)
216
+ {
217
+ int i,n=f->sizep;
218
+ PrintHeader(f);
219
+ PrintCode(f);
220
+ if (full)
221
+ {
222
+ PrintConstants(f);
223
+ PrintLocals(f);
224
+ PrintUpvalues(f);
225
+ }
226
+ for (i=0; i<n; i++) PrintFunction(f->p[i],full);
227
+ }
@@ -0,0 +1,26 @@
1
+ These are simple tests for Lua. Some of them contain useful code.
2
+ They are meant to be run to make sure Lua is built correctly and also
3
+ to be read, to see how Lua programs look.
4
+
5
+ Here is a one-line summary of each program:
6
+
7
+ bisect.lua bisection method for solving non-linear equations
8
+ cf.lua temperature conversion table (celsius to farenheit)
9
+ echo.lua echo command line arguments
10
+ env.lua environment variables as automatic global variables
11
+ factorial.lua factorial without recursion
12
+ fib.lua fibonacci function with cache
13
+ fibfor.lua fibonacci numbers with coroutines and generators
14
+ globals.lua report global variable usage
15
+ hello.lua the first program in every language
16
+ life.lua Conway's Game of Life
17
+ luac.lua bare-bones luac
18
+ printf.lua an implementation of printf
19
+ readonly.lua make global variables readonly
20
+ sieve.lua the sieve of of Eratosthenes programmed with coroutines
21
+ sort.lua two implementations of a sort function
22
+ table.lua make table, grouping all data for the same item
23
+ trace-calls.lua trace calls
24
+ trace-globals.lua trace assigments to global variables
25
+ xd.lua hex dump
26
+
@@ -0,0 +1,27 @@
1
+ -- bisection method for solving non-linear equations
2
+
3
+ delta=1e-6 -- tolerance
4
+
5
+ function bisect(f,a,b,fa,fb)
6
+ local c=(a+b)/2
7
+ io.write(n," c=",c," a=",a," b=",b,"\n")
8
+ if c==a or c==b or math.abs(a-b)<delta then return c,b-a end
9
+ n=n+1
10
+ local fc=f(c)
11
+ if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
12
+ end
13
+
14
+ -- find root of f in the inverval [a,b]. needs f(a)*f(b)<0
15
+ function solve(f,a,b)
16
+ n=0
17
+ local z,e=bisect(f,a,b,f(a),f(b))
18
+ io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z)))
19
+ end
20
+
21
+ -- our function
22
+ function f(x)
23
+ return x*x*x-x-1
24
+ end
25
+
26
+ -- find zero in [1,2]
27
+ solve(f,1,2)
@@ -0,0 +1,16 @@
1
+ -- temperature conversion table (celsius to farenheit)
2
+
3
+ for c0=-20,50-1,10 do
4
+ io.write("C ")
5
+ for c=c0,c0+10-1 do
6
+ io.write(string.format("%3.0f ",c))
7
+ end
8
+ io.write("\n")
9
+
10
+ io.write("F ")
11
+ for c=c0,c0+10-1 do
12
+ f=(9/5)*c+32
13
+ io.write(string.format("%3.0f ",f))
14
+ end
15
+ io.write("\n\n")
16
+ end
@@ -0,0 +1,5 @@
1
+ -- echo command line arguments
2
+
3
+ for i=0,table.getn(arg) do
4
+ print(i,arg[i])
5
+ end
@@ -0,0 +1,7 @@
1
+ -- read environment variables as if they were global variables
2
+
3
+ local f=function (t,i) return os.getenv(i) end
4
+ setmetatable(getfenv(),{__index=f})
5
+
6
+ -- an example
7
+ print(a,USER,PATH)
@@ -0,0 +1,32 @@
1
+ -- function closures are powerful
2
+
3
+ -- traditional fixed-point operator from functional programming
4
+ Y = function (g)
5
+ local a = function (f) return f(f) end
6
+ return a(function (f)
7
+ return g(function (x)
8
+ local c=f(f)
9
+ return c(x)
10
+ end)
11
+ end)
12
+ end
13
+
14
+
15
+ -- factorial without recursion
16
+ F = function (f)
17
+ return function (n)
18
+ if n == 0 then return 1
19
+ else return n*f(n-1) end
20
+ end
21
+ end
22
+
23
+ factorial = Y(F) -- factorial is the fixed point of F
24
+
25
+ -- now test it
26
+ function test(x)
27
+ io.write(x,"! = ",factorial(x),"\n")
28
+ end
29
+
30
+ for n=0,16 do
31
+ test(n)
32
+ end
@@ -0,0 +1,40 @@
1
+ -- fibonacci function with cache
2
+
3
+ -- very inefficient fibonacci function
4
+ function fib(n)
5
+ N=N+1
6
+ if n<2 then
7
+ return n
8
+ else
9
+ return fib(n-1)+fib(n-2)
10
+ end
11
+ end
12
+
13
+ -- a general-purpose value cache
14
+ function cache(f)
15
+ local c={}
16
+ return function (x)
17
+ local y=c[x]
18
+ if not y then
19
+ y=f(x)
20
+ c[x]=y
21
+ end
22
+ return y
23
+ end
24
+ end
25
+
26
+ -- run and time it
27
+ function test(s,f)
28
+ N=0
29
+ local c=os.clock()
30
+ local v=f(n)
31
+ local t=os.clock()-c
32
+ print(s,n,v,t,N)
33
+ end
34
+
35
+ n=arg[1] or 24 -- for other values, do lua fib.lua XX
36
+ n=tonumber(n)
37
+ print("","n","value","time","evals")
38
+ test("plain",fib)
39
+ fib=cache(fib)
40
+ test("cached",fib)
@@ -0,0 +1,13 @@
1
+ -- example of for with generator functions
2
+
3
+ function generatefib (n)
4
+ return coroutine.wrap(function ()
5
+ local a,b = 1, 1
6
+ while a <= n do
7
+ coroutine.yield(a)
8
+ a, b = b, a+b
9
+ end
10
+ end)
11
+ end
12
+
13
+ for i in generatefib(1000) do print(i) end
@@ -0,0 +1,13 @@
1
+ -- reads luac listings and reports global variable usage
2
+ -- lines where a global is written to are marked with "*"
3
+ -- typical usage: luac -p -l file.lua | lua globals.lua | sort | lua table.lua
4
+
5
+ while 1 do
6
+ local s=io.read()
7
+ if s==nil then break end
8
+ local ok,_,l,op,g=string.find(s,"%[%-?(%d*)%]%s*([GS])ETGLOBAL.-;%s+(.*)$")
9
+ if ok then
10
+ if op=="S" then op="*" else op="" end
11
+ io.write(g,"\t",l,op,"\n")
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ -- the first program in every language
2
+
3
+ io.write("Hello world, from ",_VERSION,"!\n")