summaryrefslogtreecommitdiff
path: root/test/examples
diff options
context:
space:
mode:
Diffstat (limited to 'test/examples')
-rw-r--r--test/examples/complex/complex.lua82
-rw-r--r--test/examples/complex/mandel.lua25
-rw-r--r--test/examples/www/README10
-rw-r--r--test/examples/www/db.lua46
-rw-r--r--test/examples/www/staff.lua37
-rw-r--r--test/examples/www/template.html29
6 files changed, 122 insertions, 107 deletions
diff --git a/test/examples/complex/complex.lua b/test/examples/complex/complex.lua
deleted file mode 100644
index a213a746..00000000
--- a/test/examples/complex/complex.lua
+++ /dev/null
@@ -1,82 +0,0 @@
--- complex.lua
--- complex arithmetic package for lua
--- Luiz Henrique de Figueiredo (lhf@csg.uwaterloo.ca)
--- 24 Oct 95
-$debug
-
-Complex={type="package"}
-
-function complex(x,y)
- return { re=x, im=y, type="complex" }
-end
-
-function Complex.conj(x,y)
- return complex(x.re,-x.im)
-end
-
-function Complex.norm2(x)
- local n=Complex.mul(x,Complex.conj(x))
- return n.re
-end
-
-function Complex.abs(x)
- return sqrt(Complex.norm2(x))
-end
-
-function Complex.add(x,y)
- return complex(x.re+y.re,x.im+y.im)
-end
-
-function Complex.sub(x,y)
- return complex(x.re-y.re,x.im-y.im)
-end
-
-function Complex.mul(x,y)
- return complex(x.re*y.re-x.im*y.im,x.re*y.im+x.im*y.re)
-end
-
-function Complex.div(x,y)
- local z=x*Complex.conj(y)
- local t=Complex.norm2(y)
- z.re=z.re/t
- z.im=z.im/t
- return z
-end
-
-function Complex.fallback(x,y,op)
- if type(x)=="number" then x=complex(x,0) end
- if type(y)=="number" then y=complex(y,0) end
- if type(x)=="complex" and type(y)=="complex" then
- return Complex[op](x,y)
- else
- return Complex.oldfallback(x,y)
- end
-end
-
-function Complex.newtype(x)
- local t=Complex.oldtype(x)
- if t=="table" and x.type then return x.type else return t end
-end
-
-function Complex.print(x)
- if type(x)~="complex" then
- Complex.oldprint(x)
- else
- local s
- if x.im>=0 then s="+" else s="" end
- Complex.oldprint(x.re..s..x.im.."I")
- end
-end
-
-function Complex.newabs(x)
- if type(x)~="complex" then
- return Complex.oldabs(x)
- else
- return Complex.abs(x)
- end
-end
-
-Complex.oldfallback=setfallback("arith",Complex.fallback)
-Complex.oldtype=type type=Complex.newtype
-Complex.oldprint=print print=Complex.print
-Complex.oldabs=abs abs=Complex.abs
diff --git a/test/examples/complex/mandel.lua b/test/examples/complex/mandel.lua
deleted file mode 100644
index 5e3d3eb6..00000000
--- a/test/examples/complex/mandel.lua
+++ /dev/null
@@ -1,25 +0,0 @@
-dofile("complex.lua")
-
-xmin=-2 xmax=2 ymin=-2 ymax=2
-d=.125
-
-function level(x,y)
- local c=complex(x,y)
- local l=0
- local z=c
- repeat
- z=z*z+c
- l=l+1
- until abs(z)>2 or l>255
- return l-1
-end
-
-x=xmin
-while x<xmax do
- y=ymin
- while y<ymax do
- print(level(x,y))
- y=y+d
- end
- x=x+d
-end
diff --git a/test/examples/www/README b/test/examples/www/README
new file mode 100644
index 00000000..0c249a43
--- /dev/null
+++ b/test/examples/www/README
@@ -0,0 +1,10 @@
+This directory contains a database for a fake web site.
+Standard web page for the persons listed in db.lua are
+creared automatically from template.html with staff.lua.
+(See http://www.cos.ufrj.br for a real web site was created in this way.)
+
+To run, type lua db.lua.
+
+This example is meant to show the power of gsub and Lua declarative constructs,
+which have been combined here into a "mail-merge" application.
+
diff --git a/test/examples/www/db.lua b/test/examples/www/db.lua
new file mode 100644
index 00000000..837afd29
--- /dev/null
+++ b/test/examples/www/db.lua
@@ -0,0 +1,46 @@
+dofile("staff.lua")
+
+global{
+ ROOT='http://www.tecgraf.puc-rio.br',
+ EMAIL="|LOGIN|@tecgraf.puc-rio.br",
+ WWW="|ROOT|/~|LOGIN|",
+ webmast='|ROOT|/webmaster.html',
+ TECGRAF='<A HREF="http://www.tecgraf.puc-rio.br">TeCGraf</A>',
+ CS='<A HREF="http://www.inf.puc-rio.br">Computer Science Department</A>',
+ PUCRIO='<A HREF="http://www.puc-rio.br">PUC-Rio</A>',
+}
+
+staff{
+DI="inf.puc-rio.br",
+ LOGIN="roberto",
+ NAME="Roberto Ierusalimschy",
+ TITLE="D.Sc., |PUCRIO|, 1990",
+ POSITION="Associate Professor, |CS|, |PUCRIO|",
+ AREAS="Programming Languages, Object Oriented Programming",
+ EMAIL="|LOGIN|@|DI|",
+ WWW="http://www.|DI|/~|LOGIN|",
+}
+
+staff{
+PCG="http://www.graphics.cornell.edu",
+ LOGIN="celes",
+ NAME="Waldemar Celes",
+ TITLE="D.Sc., |PUCRIO|, 1995",
+ POSITION="Postdoctoral Associate at "..
+'<A HREF="|PCG|">Program of Computer Graphics</A>, '..
+'<A HREF="http://www.cornell.edu">Cornell University</A>',
+ WWW="|PCG|/~celes/",
+ AREAS="Image segmentation and 3D reconstruction; "
+.."Physical simulation and educational software;"
+.."Geometric modeling and topological data structures;"
+.."Extension languages and customizable applications"
+}
+
+staff{
+ LOGIN="lhf",
+ NAME="Luiz Henrique de Figueiredo",
+ TITLE='D.Sc., <A HREF="http://www.impa.br">IMPA</A>, 1992',
+ POSITION='Visiting Research fellow at <A HREF="http://www.lncc.br">LNCC</A>; Consultant at |TECGRAF|',
+ AREAS="Geometric modeling; Software tools",
+ WWW="http://www2.lncc.br/~lhf/",
+}
diff --git a/test/examples/www/staff.lua b/test/examples/www/staff.lua
new file mode 100644
index 00000000..7fa5b810
--- /dev/null
+++ b/test/examples/www/staff.lua
@@ -0,0 +1,37 @@
+$debug
+
+readfrom("template.html")
+TEMPLATE=read(".*")
+readfrom()
+
+PAT="|(%a%a*)|"
+
+GLOBAL={
+ DATE=date("%d/%m/%Y %T"),
+}
+
+function get(i)
+ if LOCAL[i] then return LOCAL[i]
+ elseif GLOBAL[i] then return GLOBAL[i]
+ else return "<BLINK>?"..i.."?</BLINK>" end
+end
+
+function global(t)
+ local i,v=next(t,nil)
+ while i do
+ GLOBAL[i]=v
+ i,v=next(t,i)
+ end
+end
+
+function staff(t)
+ LOCAL=t
+ if t.AREAS then t.AREAS=gsub(t.AREAS,"[;,] *","\n<LI>") end
+ local p,n=TEMPLATE
+ if t.WWW=="" then p=gsub(p,'<A HREF="|WWW|">|NAME|</A>',"|NAME|") end
+ repeat p,n=gsub(p,PAT,get) until n==0
+ write(t.LOGIN,"\n")
+ writeto(t.LOGIN..".html")
+ write(p)
+ writeto()
+end
diff --git a/test/examples/www/template.html b/test/examples/www/template.html
new file mode 100644
index 00000000..c8494b76
--- /dev/null
+++ b/test/examples/www/template.html
@@ -0,0 +1,29 @@
+<HTML>
+<HEAD>
+<TITLE>
+Imaginary web site:Staff:|LOGIN|
+</TITLE>
+</HEAD>
+
+<BODY>
+<H1><A HREF="|WWW|">|NAME|</A></H1>
+<A HREF="mailto:|EMAIL|">|EMAIL|</A>
+<P>
+
+|POSITION|<BR>
+|TITLE|<P>
+
+<H2>Research areas</H2>
+<UL>
+<LI>|AREAS|
+</UL>
+<P>
+
+<HR>
+Last update in |DATE| by
+<A HREF="|webmast|">websmaster</A>.
+<P>
+
+</BODY>
+
+</HTML>