Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
52119866
Commit
52119866
authored
Dec 01, 2011
by
Megax
Browse files
bridge from LuaInterface(capresti) to LuaInterface (megax)
parent
99dc49f7
Changes
129
Hide whitespace changes
Inline
Side-by-side
lua-5.1.4/test/luac.lua
deleted
100644 → 0
View file @
99dc49f7
-- bare-bones luac in Lua
-- usage: lua luac.lua file.lua
assert
(
arg
[
1
]
~=
nil
and
arg
[
2
]
==
nil
,
"usage: lua luac.lua file.lua"
)
f
=
assert
(
io.open
(
"luac.out"
,
"wb"
))
assert
(
f
:
write
(
string.dump
(
assert
(
loadfile
(
arg
[
1
])))))
assert
(
f
:
close
())
lua-5.1.4/test/printf.lua
deleted
100644 → 0
View file @
99dc49f7
-- an implementation of printf
function
printf
(
...
)
io.write
(
string.format
(
...
))
end
printf
(
"Hello %s from %s on %s\n"
,
os.getenv
"USER"
or
"there"
,
_VERSION
,
os.date
())
lua-5.1.4/test/readonly.lua
deleted
100644 → 0
View file @
99dc49f7
-- make global variables readonly
local
f
=
function
(
t
,
i
)
error
(
"cannot redefine global variable `"
..
i
..
"'"
,
2
)
end
local
g
=
{}
local
G
=
getfenv
()
setmetatable
(
g
,{
__index
=
G
,
__newindex
=
f
})
setfenv
(
1
,
g
)
-- an example
rawset
(
g
,
"x"
,
3
)
x
=
2
y
=
1
-- cannot redefine `y'
lua-5.1.4/test/sieve.lua
deleted
100644 → 0
View file @
99dc49f7
-- the sieve of of Eratosthenes programmed with coroutines
-- typical usage: lua -e N=1000 sieve.lua | column
-- generate all the numbers from 2 to n
function
gen
(
n
)
return
coroutine.wrap
(
function
()
for
i
=
2
,
n
do
coroutine.yield
(
i
)
end
end
)
end
-- filter the numbers generated by `g', removing multiples of `p'
function
filter
(
p
,
g
)
return
coroutine.wrap
(
function
()
while
1
do
local
n
=
g
()
if
n
==
nil
then
return
end
if
math
.
mod
(
n
,
p
)
~=
0
then
coroutine.yield
(
n
)
end
end
end
)
end
N
=
N
or
1000
-- from command line
x
=
gen
(
N
)
-- generate primes up to N
while
1
do
local
n
=
x
()
-- pick a number until done
if
n
==
nil
then
break
end
print
(
n
)
-- must be a prime number
x
=
filter
(
n
,
x
)
-- now remove its multiples
end
lua-5.1.4/test/sort.lua
deleted
100644 → 0
View file @
99dc49f7
-- two implementations of a sort function
-- this is an example only. Lua has now a built-in function "sort"
-- extracted from Programming Pearls, page 110
function
qsort
(
x
,
l
,
u
,
f
)
if
l
<
u
then
local
m
=
math.random
(
u
-
(
l
-
1
))
+
l
-
1
-- choose a random pivot in range l..u
x
[
l
],
x
[
m
]
=
x
[
m
],
x
[
l
]
-- swap pivot to first position
local
t
=
x
[
l
]
-- pivot value
m
=
l
local
i
=
l
+
1
while
i
<=
u
do
-- invariant: x[l+1..m] < t <= x[m+1..i-1]
if
f
(
x
[
i
],
t
)
then
m
=
m
+
1
x
[
m
],
x
[
i
]
=
x
[
i
],
x
[
m
]
-- swap x[i] and x[m]
end
i
=
i
+
1
end
x
[
l
],
x
[
m
]
=
x
[
m
],
x
[
l
]
-- swap pivot to a valid place
-- x[l+1..m-1] < x[m] <= x[m+1..u]
qsort
(
x
,
l
,
m
-
1
,
f
)
qsort
(
x
,
m
+
1
,
u
,
f
)
end
end
function
selectionsort
(
x
,
n
,
f
)
local
i
=
1
while
i
<=
n
do
local
m
,
j
=
i
,
i
+
1
while
j
<=
n
do
if
f
(
x
[
j
],
x
[
m
])
then
m
=
j
end
j
=
j
+
1
end
x
[
i
],
x
[
m
]
=
x
[
m
],
x
[
i
]
-- swap x[i] and x[m]
i
=
i
+
1
end
end
function
show
(
m
,
x
)
io.write
(
m
,
"
\n\t
"
)
local
i
=
1
while
x
[
i
]
do
io.write
(
x
[
i
])
i
=
i
+
1
if
x
[
i
]
then
io.write
(
","
)
end
end
io.write
(
"
\n
"
)
end
function
testsorts
(
x
)
local
n
=
1
while
x
[
n
]
do
n
=
n
+
1
end
;
n
=
n
-
1
-- count elements
show
(
"original"
,
x
)
qsort
(
x
,
1
,
n
,
function
(
x
,
y
)
return
x
<
y
end
)
show
(
"after quicksort"
,
x
)
selectionsort
(
x
,
n
,
function
(
x
,
y
)
return
x
>
y
end
)
show
(
"after reverse selection sort"
,
x
)
qsort
(
x
,
1
,
n
,
function
(
x
,
y
)
return
x
<
y
end
)
show
(
"after quicksort again"
,
x
)
end
-- array to be sorted
x
=
{
"Jan"
,
"Feb"
,
"Mar"
,
"Apr"
,
"May"
,
"Jun"
,
"Jul"
,
"Aug"
,
"Sep"
,
"Oct"
,
"Nov"
,
"Dec"
}
testsorts
(
x
)
lua-5.1.4/test/table.lua
deleted
100644 → 0
View file @
99dc49f7
-- make table, grouping all data for the same item
-- input is 2 columns (item, data)
local
A
while
1
do
local
l
=
io.read
()
if
l
==
nil
then
break
end
local
_
,
_
,
a
,
b
=
string.find
(
l
,
'"?([_%w]+)"?%s*(.*)$'
)
if
a
~=
A
then
A
=
a
io.write
(
"
\n
"
,
a
,
":"
)
end
io.write
(
" "
,
b
)
end
io.write
(
"
\n
"
)
lua-5.1.4/test/trace-calls.lua
deleted
100644 → 0
View file @
99dc49f7
-- trace calls
-- example: lua -ltrace-calls bisect.lua
local
level
=
0
local
function
hook
(
event
)
local
t
=
debug.getinfo
(
3
)
io.write
(
level
,
" >>> "
,
string.rep
(
" "
,
level
))
if
t
~=
nil
and
t
.
currentline
>=
0
then
io.write
(
t
.
short_src
,
":"
,
t
.
currentline
,
" "
)
end
t
=
debug.getinfo
(
2
)
if
event
==
"call"
then
level
=
level
+
1
else
level
=
level
-
1
if
level
<
0
then
level
=
0
end
end
if
t
.
what
==
"main"
then
if
event
==
"call"
then
io.write
(
"begin "
,
t
.
short_src
)
else
io.write
(
"end "
,
t
.
short_src
)
end
elseif
t
.
what
==
"Lua"
then
-- table.foreach(t,print)
io.write
(
event
,
" "
,
t
.
name
or
"(Lua)"
,
" <"
,
t
.
linedefined
,
":"
,
t
.
short_src
,
">"
)
else
io.write
(
event
,
" "
,
t
.
name
or
"(C)"
,
" ["
,
t
.
what
,
"] "
)
end
io.write
(
"
\n
"
)
end
debug.sethook
(
hook
,
"cr"
)
level
=
0
lua-5.1.4/test/trace-globals.lua
deleted
100644 → 0
View file @
99dc49f7
-- trace assigments to global variables
do
-- a tostring that quotes strings. note the use of the original tostring.
local
_tostring
=
tostring
local
tostring
=
function
(
a
)
if
type
(
a
)
==
"string"
then
return
string.format
(
"%q"
,
a
)
else
return
_tostring
(
a
)
end
end
local
log
=
function
(
name
,
old
,
new
)
local
t
=
debug.getinfo
(
3
,
"Sl"
)
local
line
=
t
.
currentline
io.write
(
t
.
short_src
)
if
line
>=
0
then
io.write
(
":"
,
line
)
end
io.write
(
": "
,
name
,
" is now "
,
tostring
(
new
),
" (was "
,
tostring
(
old
),
")"
,
"
\n
"
)
end
local
g
=
{}
local
set
=
function
(
t
,
name
,
value
)
log
(
name
,
g
[
name
],
value
)
g
[
name
]
=
value
end
setmetatable
(
getfenv
(),{
__index
=
g
,
__newindex
=
set
})
end
-- an example
a
=
1
b
=
2
a
=
10
b
=
20
b
=
nil
b
=
200
print
(
a
,
b
,
c
)
lua-5.1.4/test/xd.lua
deleted
100644 → 0
View file @
99dc49f7
-- hex dump
-- usage: lua xd.lua < file
local
offset
=
0
while
true
do
local
s
=
io.read
(
16
)
if
s
==
nil
then
return
end
io.write
(
string.format
(
"%08X "
,
offset
))
string.gsub
(
s
,
"(.)"
,
function
(
c
)
io.write
(
string.format
(
"%02X "
,
string.byte
(
c
)))
end
)
io.write
(
string.rep
(
" "
,
3
*
(
16
-
string.len
(
s
))))
io.write
(
" "
,
string.gsub
(
s
,
"%c"
,
"."
),
"
\n
"
)
offset
=
offset
+
16
end
Prev
1
…
3
4
5
6
7
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment