mirror of
https://github.com/lua/lua.git
synced 2026-07-27 16:39:06 +00:00
Compare commits
1152 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dbde9f8817 | ||
|
|
857a1204d8 | ||
|
|
d742b88fa1 | ||
|
|
eb822c314a | ||
|
|
ab7aceb980 | ||
|
|
72b12e0956 | ||
|
|
5ed3bcd4ea | ||
|
|
7508fed673 | ||
|
|
aab4fde468 | ||
|
|
6ec4a0ef76 | ||
|
|
42ca3105ac | ||
|
|
6030d5fed4 | ||
|
|
91c003dcc2 | ||
|
|
620d22f2a0 | ||
|
|
f45cba42b5 | ||
|
|
2628a42480 | ||
|
|
0b3b6850c9 | ||
|
|
5d033f33eb | ||
|
|
e143fed484 | ||
|
|
01ce1ce48c | ||
|
|
024ed4218f | ||
|
|
c1f725ba4a | ||
|
|
5d69960734 | ||
|
|
d1c351857d | ||
|
|
4ef14ba280 | ||
|
|
b8d57092eb | ||
|
|
0f59e3d7a7 | ||
|
|
55c3bc2bcb | ||
|
|
8060193702 | ||
|
|
2779e81fbb | ||
|
|
30f6e658d2 | ||
|
|
787a78f83e | ||
|
|
70c8a31092 | ||
|
|
2018380e9f | ||
|
|
6e80c1cde1 | ||
|
|
f67f324377 | ||
|
|
eebc9729e4 | ||
|
|
6990b06467 | ||
|
|
cad91499dd | ||
|
|
ebdd4e8451 | ||
|
|
9a21e81907 | ||
|
|
f0b3cd1d6f | ||
|
|
fb5e6d5ac4 | ||
|
|
d63afba9d3 | ||
|
|
100bfec39a | ||
|
|
a290b84c67 | ||
|
|
1022b3c85e | ||
|
|
a1d3e001b9 | ||
|
|
10b4799ce5 | ||
|
|
a0de89d62a | ||
|
|
21dc77b2af | ||
|
|
2d3ebba537 | ||
|
|
a97f29f154 | ||
|
|
4135f4f586 | ||
|
|
808525e417 | ||
|
|
48a968e6b5 | ||
|
|
58adbde216 | ||
|
|
9e74ca62d7 | ||
|
|
d396562b5e | ||
|
|
9d60598260 | ||
|
|
4e56c0d514 | ||
|
|
ac12f4db4b | ||
|
|
b691d4344b | ||
|
|
9fdf73bc9a | ||
|
|
f1fd9b5c2c | ||
|
|
3723d790ae | ||
|
|
b800c38b69 | ||
|
|
5c0e79847c | ||
|
|
e662e0f1cd | ||
|
|
c85162be27 | ||
|
|
bd39db46ed | ||
|
|
90fb2e18e8 | ||
|
|
4365c31c89 | ||
|
|
44eb7d91ac | ||
|
|
ddc8d94a08 | ||
|
|
5d9cbdadfb | ||
|
|
58453dc1e1 | ||
|
|
f555e493f0 | ||
|
|
d1ffd1a1c5 | ||
|
|
9e84bf18db | ||
|
|
daf09c476f | ||
|
|
e238efc536 | ||
|
|
fdd04e7a7f | ||
|
|
fb55c96706 | ||
|
|
c39bec81dc | ||
|
|
5f22f8961c | ||
|
|
a7c1390ffa | ||
|
|
0802a9df9e | ||
|
|
f90bc248b3 | ||
|
|
d9e61e8cea | ||
|
|
397905ef86 | ||
|
|
435f587ed0 | ||
|
|
ae55f3eead | ||
|
|
cfba572076 | ||
|
|
aa01d2568d | ||
|
|
014a09c509 | ||
|
|
b622282973 | ||
|
|
7f867eb0b9 | ||
|
|
5362426ffa | ||
|
|
906434011f | ||
|
|
c07cebbbf6 | ||
|
|
afef009fce | ||
|
|
b69e712713 | ||
|
|
f517759507 | ||
|
|
52062684e1 | ||
|
|
83e3ec0117 | ||
|
|
feb2083730 | ||
|
|
8d2baf719c | ||
|
|
88657809ac | ||
|
|
2e1b32d873 | ||
|
|
89a3ec08f3 | ||
|
|
e3ea307434 | ||
|
|
18b0e8270d | ||
|
|
094880ef9e | ||
|
|
1de5587184 | ||
|
|
8ca9534d04 | ||
|
|
8bcf622876 | ||
|
|
dbbf6c073b | ||
|
|
292c953018 | ||
|
|
c542aac0b9 | ||
|
|
dbfb810267 | ||
|
|
9c14b5e416 | ||
|
|
6b9bf49265 | ||
|
|
989ad7232a | ||
|
|
190c3be739 | ||
|
|
7e30900def | ||
|
|
f63d7753b8 | ||
|
|
50a82ec1b9 | ||
|
|
ed1751bc32 | ||
|
|
8e617985fa | ||
|
|
c39345fba3 | ||
|
|
ed94a85f0c | ||
|
|
58fbdc76d5 | ||
|
|
a301304612 | ||
|
|
677313da32 | ||
|
|
ef62b340e0 | ||
|
|
5c2dd7a9e0 | ||
|
|
93d93a0bfb | ||
|
|
9e1f94fc1c | ||
|
|
8714cc02d6 | ||
|
|
69b45bb4e9 | ||
|
|
843f84f4ce | ||
|
|
b803c0600e | ||
|
|
8ac0bbf64b | ||
|
|
dc1e4f5073 | ||
|
|
330e51bed3 | ||
|
|
44b71ca816 | ||
|
|
bad6365540 | ||
|
|
91f34fb05c | ||
|
|
11a7022067 | ||
|
|
35a6ed2838 | ||
|
|
cfe6ca819b | ||
|
|
fe5c37ae95 | ||
|
|
c31f02948f | ||
|
|
c9f91c6fe3 | ||
|
|
66d620674c | ||
|
|
84790bb953 | ||
|
|
534c3a64d3 | ||
|
|
b9c98cd4d9 | ||
|
|
0e8855e171 | ||
|
|
2ae9c856cf | ||
|
|
c4409f69ab | ||
|
|
89a9c36281 | ||
|
|
8f2fba5877 | ||
|
|
62824137d6 | ||
|
|
71219ccc39 | ||
|
|
870f61d299 | ||
|
|
c845ec777a | ||
|
|
e408cf61a2 | ||
|
|
4e90768635 | ||
|
|
c1666a13e3 | ||
|
|
e02750ec60 | ||
|
|
0b56646baf | ||
|
|
634c3d57e9 | ||
|
|
4aa9ad6514 | ||
|
|
d76b1a0eef | ||
|
|
e7c8393682 | ||
|
|
ceaa97ff5b | ||
|
|
2bb77cdaed | ||
|
|
f9cf402fbd | ||
|
|
0c3fe2c44b | ||
|
|
0433e42023 | ||
|
|
0810bc707f | ||
|
|
c3b73cbeb8 | ||
|
|
1eabd0549e | ||
|
|
54dd5cc7fd | ||
|
|
031978798c | ||
|
|
14251c5a56 | ||
|
|
42130ed712 | ||
|
|
d615e78e08 | ||
|
|
c6965ce551 | ||
|
|
22329e4cdf | ||
|
|
9f734094f9 | ||
|
|
4e7e9e8de5 | ||
|
|
3e45496295 | ||
|
|
e30327728c | ||
|
|
36e1390631 | ||
|
|
8f0f54ec38 | ||
|
|
556a89e537 | ||
|
|
e2c60eda16 | ||
|
|
a69356e9e0 | ||
|
|
b53dc0c485 | ||
|
|
63e230a16b | ||
|
|
102a1be615 | ||
|
|
4c03196697 | ||
|
|
54633d7c7d | ||
|
|
13578517c7 | ||
|
|
213e9febc8 | ||
|
|
47b4bf5964 | ||
|
|
415ee1a24f | ||
|
|
5f3c5ace95 | ||
|
|
441469f803 | ||
|
|
1157c2a7e4 | ||
|
|
374cc888f4 | ||
|
|
bb9605bbc9 | ||
|
|
6759299880 | ||
|
|
da7eb34cd6 | ||
|
|
2c8e28d75a | ||
|
|
4fb8e93c36 | ||
|
|
7f77431950 | ||
|
|
87367e97f3 | ||
|
|
06f08f5634 | ||
|
|
3860c5934e | ||
|
|
f0b697e01c | ||
|
|
73aa465a8e | ||
|
|
3d0577f4b9 | ||
|
|
977b8f65f0 | ||
|
|
e3cf93ddb6 | ||
|
|
88b306f495 | ||
|
|
563de491be | ||
|
|
29e0193425 | ||
|
|
61f97a8ace | ||
|
|
d1ea38580a | ||
|
|
3c9d999424 | ||
|
|
f7840a3e0b | ||
|
|
1780e2c977 | ||
|
|
fa14cbf8e8 | ||
|
|
4d4e6f07c0 | ||
|
|
5cecf0ea9f | ||
|
|
3bc925138e | ||
|
|
39e1f079bd | ||
|
|
075da266e5 | ||
|
|
228a62f799 | ||
|
|
7c2fb15bec | ||
|
|
1f01998516 | ||
|
|
c929376d59 | ||
|
|
3afe85b2ce | ||
|
|
52aad0ab59 | ||
|
|
74f1c3d025 | ||
|
|
1f691a4fcd | ||
|
|
d6b9f49aaf | ||
|
|
d83c2a8455 | ||
|
|
d11e5adf55 | ||
|
|
99e340b2ba | ||
|
|
9744255ae9 | ||
|
|
512b15b601 | ||
|
|
33d3504889 | ||
|
|
2877bad4c2 | ||
|
|
27163f032e | ||
|
|
eab57ed6fd | ||
|
|
ea45f3eb28 | ||
|
|
46ec57cbc6 | ||
|
|
62787f1b1f | ||
|
|
dcb7bb514e | ||
|
|
b3aaa048b0 | ||
|
|
aee07c6599 | ||
|
|
bcdbdaccc3 | ||
|
|
5cafe5af02 | ||
|
|
607012ece1 | ||
|
|
0652906e7a | ||
|
|
4d1b815b60 | ||
|
|
4ebc183e56 | ||
|
|
298d0abff7 | ||
|
|
4fbe775154 | ||
|
|
aabefe9d68 | ||
|
|
fb60283974 | ||
|
|
acdb0b741e | ||
|
|
5b08fcd5a1 | ||
|
|
b1b0c219f5 | ||
|
|
be3212de78 | ||
|
|
2eaf9a00ae | ||
|
|
6c79a0a80d | ||
|
|
3daeabb606 | ||
|
|
cfdebfbc53 | ||
|
|
d61d5bef9d | ||
|
|
4450efc97e | ||
|
|
de65253f2d | ||
|
|
12b45c2df2 | ||
|
|
1b15206cf9 | ||
|
|
e6d56cd2d8 | ||
|
|
c16c63cc59 | ||
|
|
dea54a7c71 | ||
|
|
544eeb1f9c | ||
|
|
b3fe203c36 | ||
|
|
9c142740c1 | ||
|
|
7621cc2aa5 | ||
|
|
ba1f504970 | ||
|
|
617be66015 | ||
|
|
f356eb010b | ||
|
|
3e38fd2463 | ||
|
|
533beedad2 | ||
|
|
968ad49da6 | ||
|
|
1fdb445e7d | ||
|
|
c7119a93f0 | ||
|
|
3c9e29f56d | ||
|
|
72afb6debb | ||
|
|
b097076678 | ||
|
|
8223ff473f | ||
|
|
fe237ad808 | ||
|
|
3181dfefee | ||
|
|
764e095d7f | ||
|
|
2b3ea61342 | ||
|
|
49af08e3e7 | ||
|
|
c5fa0895e9 | ||
|
|
d015f1fc02 | ||
|
|
790690a223 | ||
|
|
33b8a01032 | ||
|
|
d29ce75737 | ||
|
|
878ef96127 | ||
|
|
e5743adb21 | ||
|
|
514783de9d | ||
|
|
7d1499ba88 | ||
|
|
29ede6aa13 | ||
|
|
951897c093 | ||
|
|
a34677e4f4 | ||
|
|
573372665a | ||
|
|
a6755e2f1a | ||
|
|
a4628a02cf | ||
|
|
0c725b2492 | ||
|
|
d915cf4f9d | ||
|
|
53fb65d394 | ||
|
|
a7fa7bafc8 | ||
|
|
cde179b369 | ||
|
|
80b39d83c3 | ||
|
|
5a48255c9f | ||
|
|
bbab974717 | ||
|
|
910836fb53 | ||
|
|
8e7451512f | ||
|
|
4e9f2d13d5 | ||
|
|
b6ebbb2fee | ||
|
|
c5fee7615e | ||
|
|
cca78b5c71 | ||
|
|
d435b7acd8 | ||
|
|
63ccf42397 | ||
|
|
1942b58609 | ||
|
|
4343420d4d | ||
|
|
1f7103e05d | ||
|
|
dcc10accea | ||
|
|
e2fc5aa684 | ||
|
|
d53e2d2386 | ||
|
|
cf9a22396c | ||
|
|
17374d2daa | ||
|
|
04265655a8 | ||
|
|
e10788b2ff | ||
|
|
7eb2f18621 | ||
|
|
a8a75fb7ab | ||
|
|
ae3ecc2d4a | ||
|
|
2e13cd77ab | ||
|
|
ff9c0da783 | ||
|
|
f7fe04d4b7 | ||
|
|
82699d0c4f | ||
|
|
88866208f0 | ||
|
|
7d365a5c7a | ||
|
|
52a206366d | ||
|
|
b2c89ed2d2 | ||
|
|
2a03170ebd | ||
|
|
05f55cc062 | ||
|
|
c787dccd9b | ||
|
|
b44e35b773 | ||
|
|
21843f022a | ||
|
|
900257e814 | ||
|
|
a82c8185bc | ||
|
|
4bbe0679a8 | ||
|
|
438598aeea | ||
|
|
e9a670695a | ||
|
|
16024861bd | ||
|
|
3f43aaa23f | ||
|
|
3b4c831ca9 | ||
|
|
3fdab3981b | ||
|
|
88b65da4d7 | ||
|
|
521b38532a | ||
|
|
36b6fe8d17 | ||
|
|
d4dce57f5c | ||
|
|
5c19ed2a13 | ||
|
|
5caf7f4a33 | ||
|
|
3b533ea7c7 | ||
|
|
995775e1cb | ||
|
|
abfc885079 | ||
|
|
613b60c156 | ||
|
|
cdd0fe9946 | ||
|
|
bc8619342a | ||
|
|
48326500d0 | ||
|
|
c8d219798a | ||
|
|
34695d4f4a | ||
|
|
924bbe020b | ||
|
|
3aa500b524 | ||
|
|
f1861ee210 | ||
|
|
705eae9fe4 | ||
|
|
6eb1399a1c | ||
|
|
c390f73e3b | ||
|
|
73308c7605 | ||
|
|
288fa05602 | ||
|
|
7808ea3a5f | ||
|
|
732741b62f | ||
|
|
cc0f635ef7 | ||
|
|
e64dbc390a | ||
|
|
a0fd8d1787 | ||
|
|
d9d04a9274 | ||
|
|
b4ad600b93 | ||
|
|
cb7f027380 | ||
|
|
0bbd96bd5f | ||
|
|
4eb67aa710 | ||
|
|
0133610315 | ||
|
|
de04533dc0 | ||
|
|
7c9aee64c2 | ||
|
|
e0ff4e5d22 | ||
|
|
bf7f85d609 | ||
|
|
a775a2d81a | ||
|
|
e9aa98d594 | ||
|
|
3e9c6a8a24 | ||
|
|
1f4e2ba7b2 | ||
|
|
d6ff06751a | ||
|
|
7a11c7f8e4 | ||
|
|
c454dc7bdd | ||
|
|
82ad0d5770 | ||
|
|
256d1bea08 | ||
|
|
f2d35bdc78 | ||
|
|
2679461637 | ||
|
|
0870a2d1d8 | ||
|
|
78edc241e9 | ||
|
|
e907c711c0 | ||
|
|
5a8bb00df4 | ||
|
|
677188de8a | ||
|
|
6233d21c9d | ||
|
|
ab8ea5c38a | ||
|
|
ae9fd122fa | ||
|
|
da18ec5d54 | ||
|
|
038848eccd | ||
|
|
b678e465a1 | ||
|
|
72d675aba7 | ||
|
|
ba57f7d946 | ||
|
|
e63b542c9b | ||
|
|
6a853fcb8b | ||
|
|
31bea2190b | ||
|
|
4b954e9b2e | ||
|
|
055823c04d | ||
|
|
26d1e21c89 | ||
|
|
24a2c08145 | ||
|
|
9d7bae0b6a | ||
|
|
082aded149 | ||
|
|
aa9c75c06e | ||
|
|
f04c83e075 | ||
|
|
c364e9f97e | ||
|
|
e3a02e6a9c | ||
|
|
d5feffdb60 | ||
|
|
bb5627f3a4 | ||
|
|
21107d7c2c | ||
|
|
b5cd7d426f | ||
|
|
bf6d2ccf92 | ||
|
|
b82ff713e3 | ||
|
|
77113ee02f | ||
|
|
ad6c7b0dd4 | ||
|
|
8b2d97d187 | ||
|
|
fb1cf6ab2d | ||
|
|
19ca2087de | ||
|
|
7bdbd833b5 | ||
|
|
b22baf386d | ||
|
|
8fdd06ba3c | ||
|
|
028ec00ab9 | ||
|
|
1dcf1c9cbd | ||
|
|
76179a1014 | ||
|
|
bdfab46c22 | ||
|
|
5687949560 | ||
|
|
19de5b2205 | ||
|
|
cbc58af260 | ||
|
|
80001ab0eb | ||
|
|
ae29ab9858 | ||
|
|
27407fc1f5 | ||
|
|
1a17da2ff9 | ||
|
|
50248e440a | ||
|
|
0f0079f394 | ||
|
|
68267ed878 | ||
|
|
fd25d4ad85 | ||
|
|
2431534f10 | ||
|
|
fd7d0774e5 | ||
|
|
57ffc3f009 | ||
|
|
4a13f513f8 | ||
|
|
13ad46b67d | ||
|
|
1b45e967b4 | ||
|
|
933bead92e | ||
|
|
3314f49ec4 | ||
|
|
bc930aa5ff | ||
|
|
67b44c9493 | ||
|
|
758a381644 | ||
|
|
eec31aaca5 | ||
|
|
595738f6fe | ||
|
|
b5eb4f3126 | ||
|
|
3fecf187ff | ||
|
|
54840fb256 | ||
|
|
e87fddf1ad | ||
|
|
dea400bc1d | ||
|
|
fb663f768d | ||
|
|
e03767b3eb | ||
|
|
8396027516 | ||
|
|
e24f7fd2d2 | ||
|
|
8081f39dab | ||
|
|
3cc4ca821e | ||
|
|
01772cefa5 | ||
|
|
dc90d4bce3 | ||
|
|
f5bc671030 | ||
|
|
d7294c6de8 | ||
|
|
63a752f961 | ||
|
|
03d38b66fd | ||
|
|
b9c9ccfbb4 | ||
|
|
b94110a68f | ||
|
|
8278468041 | ||
|
|
4fbb2531b3 | ||
|
|
59f8e6fb77 | ||
|
|
05d89b5c05 | ||
|
|
fe5c41fb8a | ||
|
|
9a45543841 | ||
|
|
766e67ef3b | ||
|
|
4c94d8cc2c | ||
|
|
d2de2d5eda | ||
|
|
96a7695275 | ||
|
|
63166c0ca0 | ||
|
|
a881abfd1e | ||
|
|
d3ac7075a2 | ||
|
|
0c9080c7a9 | ||
|
|
b8fcb7b151 | ||
|
|
5d6de9075d | ||
|
|
21cff3015a | ||
|
|
5ca2709ba0 | ||
|
|
bb1cb7b9f1 | ||
|
|
c64f36ab2b | ||
|
|
e4830ddce3 | ||
|
|
758e330d6e | ||
|
|
8e3bd752bb | ||
|
|
a84bca67fc | ||
|
|
4ccfb2f9bc | ||
|
|
ce9609296c | ||
|
|
b1450721be | ||
|
|
b04294d3d8 | ||
|
|
22c2704842 | ||
|
|
ee22af5ced | ||
|
|
cc117253c8 | ||
|
|
8e226e6a09 | ||
|
|
1d420c2c11 | ||
|
|
5378331f2d | ||
|
|
894a264671 | ||
|
|
e1a127245d | ||
|
|
afb5ef72e1 | ||
|
|
1d8edd347d | ||
|
|
41d9ea948c | ||
|
|
ee912e5a7f | ||
|
|
ad446a0eb0 | ||
|
|
176cb39feb | ||
|
|
64ad009fb2 | ||
|
|
dcb1a08906 | ||
|
|
1788501eed | ||
|
|
bee1a5aeb2 | ||
|
|
994aba062b | ||
|
|
e869d17eb1 | ||
|
|
9a0221ef58 | ||
|
|
07008b5d45 | ||
|
|
8f31eda649 | ||
|
|
da94130160 | ||
|
|
468fbdbde7 | ||
|
|
eb45f8b631 | ||
|
|
df0df08bc5 | ||
|
|
9618aaf07d | ||
|
|
bec9bc4154 | ||
|
|
955a811aa1 | ||
|
|
c9902be294 | ||
|
|
112c9d53ab | ||
|
|
0789451458 | ||
|
|
d97af0de26 | ||
|
|
1917149fdd | ||
|
|
0845e73b6a | ||
|
|
7dfa952091 | ||
|
|
02134b4a87 | ||
|
|
bdb1db4d37 | ||
|
|
02a6891939 | ||
|
|
741c6f5006 | ||
|
|
6152973f9c | ||
|
|
243a808067 | ||
|
|
62c36a6056 | ||
|
|
74719afc33 | ||
|
|
7e59a8901d | ||
|
|
abc6eac404 | ||
|
|
054e0b888a | ||
|
|
da252eeff7 | ||
|
|
9890bedaab | ||
|
|
0a0c9593b8 | ||
|
|
d470792517 | ||
|
|
439236773b | ||
|
|
2a2b64d6ac | ||
|
|
daa937c043 | ||
|
|
21455162b5 | ||
|
|
99cc4b20f2 | ||
|
|
0969a971cd | ||
|
|
be6d215f67 | ||
|
|
e74817f8aa | ||
|
|
043c2ac258 | ||
|
|
88a2023c32 | ||
|
|
5ef1989c4b | ||
|
|
f380d627f8 | ||
|
|
aafa106d10 | ||
|
|
29b7b8e52c | ||
|
|
a9dd2c6717 | ||
|
|
aee3f97acb | ||
|
|
46968b8ffa | ||
|
|
6cdf0d8768 | ||
|
|
07ff251a17 | ||
|
|
b3b7cf7335 | ||
|
|
8622dc18bf | ||
|
|
d22e2644dd | ||
|
|
f529a22ca5 | ||
|
|
783ba75129 | ||
|
|
d49e4dd752 | ||
|
|
981fddea02 | ||
|
|
81b953f27e | ||
|
|
b9acf4b4af | ||
|
|
44ace0aefd | ||
|
|
5981161360 | ||
|
|
763c64be9b | ||
|
|
f0dffaa209 | ||
|
|
77a6836fef | ||
|
|
9f043e8017 | ||
|
|
6ac047afc4 | ||
|
|
0e1058cfdd | ||
|
|
26679b1a48 | ||
|
|
e04c2b9aa8 | ||
|
|
0c031dcc8b | ||
|
|
c332c4e927 | ||
|
|
964c503a63 | ||
|
|
90d87e3a78 | ||
|
|
f76bca23ef | ||
|
|
a5fd7d722c | ||
|
|
4e0bf95622 | ||
|
|
498a934abf | ||
|
|
ce53872684 | ||
|
|
da96eb2cce | ||
|
|
fada8efd01 | ||
|
|
d916487d7c | ||
|
|
1bf762ba38 | ||
|
|
541e722360 | ||
|
|
807ba6301c | ||
|
|
03f3f9e707 | ||
|
|
a78eecee48 | ||
|
|
43461d267f | ||
|
|
fae0b52825 | ||
|
|
22439a7511 | ||
|
|
7ecc3ce827 | ||
|
|
4e91384e14 | ||
|
|
de79e7fc58 | ||
|
|
8b5b42563c | ||
|
|
502343b402 | ||
|
|
82d09fbf0d | ||
|
|
9be85d1648 | ||
|
|
45e533599f | ||
|
|
94144a7821 | ||
|
|
4daae2165d | ||
|
|
cdd261f332 | ||
|
|
034f16892e | ||
|
|
c759520bc8 | ||
|
|
80b3d28f4a | ||
|
|
69d97712ec | ||
|
|
5d89dad9b8 | ||
|
|
525a91fed3 | ||
|
|
868d16dee0 | ||
|
|
3393fd7f25 | ||
|
|
00c122cc29 | ||
|
|
03160920cf | ||
|
|
b42cc6a4d2 | ||
|
|
a6ad644bf2 | ||
|
|
39fd5bb9b0 | ||
|
|
5482992dec | ||
|
|
024528e0c2 | ||
|
|
ef37c87e93 | ||
|
|
9e029f98b9 | ||
|
|
e962330df9 | ||
|
|
b291e50006 | ||
|
|
9ae0c082a3 | ||
|
|
accd7bc253 | ||
|
|
6153200bc2 | ||
|
|
2e7595522d | ||
|
|
b79ffdc4ce | ||
|
|
592a3f289b | ||
|
|
9cdeb275e7 | ||
|
|
c957b270d2 | ||
|
|
92791b9dd6 | ||
|
|
45cad43c3f | ||
|
|
dad5a01fb0 | ||
|
|
66713181c1 | ||
|
|
7135803cc8 | ||
|
|
b7567b6673 | ||
|
|
f8c95fa9e8 | ||
|
|
9c965d0ffb | ||
|
|
6103dca8ee | ||
|
|
18cd7adac6 | ||
|
|
41223a01ec | ||
|
|
e78cf96c97 | ||
|
|
0cb3843956 | ||
|
|
907368ead5 | ||
|
|
81489beea1 | ||
|
|
ac30aad09b | ||
|
|
2c89651fc6 | ||
|
|
3a89c973ff | ||
|
|
52d5e8032c | ||
|
|
19c178fa14 | ||
|
|
45ccb0e881 | ||
|
|
4be18fa889 | ||
|
|
7c261a13b5 | ||
|
|
2bb94d9e22 | ||
|
|
a3235ad270 | ||
|
|
f6a9cc9a67 | ||
|
|
28d47a0aaa | ||
|
|
eb617df2d8 | ||
|
|
a580480b07 | ||
|
|
0dd6d1080e | ||
|
|
3c820d622e | ||
|
|
d6c867ea50 | ||
|
|
2079cfe8fa | ||
|
|
dfe03c7abe | ||
|
|
8cd67ac676 | ||
|
|
9828893f7e | ||
|
|
6990da0057 | ||
|
|
d985dc0629 | ||
|
|
451124005b | ||
|
|
2f1fa3d427 | ||
|
|
189d64409b | ||
|
|
60cc473bcf | ||
|
|
43a2ee6ea1 | ||
|
|
4b91e9cde6 | ||
|
|
26c5f56ad1 | ||
|
|
daa858ef27 | ||
|
|
ea169d2083 | ||
|
|
c31aa863ac | ||
|
|
ff08b0f406 | ||
|
|
c1801e623f | ||
|
|
a404f6e0e6 | ||
|
|
2d2440a753 | ||
|
|
0c4ed2b3dc | ||
|
|
b945fae40e | ||
|
|
dadba4d6ed | ||
|
|
d600a6b5b3 | ||
|
|
75ac0d2172 | ||
|
|
9f3785a2f3 | ||
|
|
84e92e0976 | ||
|
|
b8a049abed | ||
|
|
e18f681333 | ||
|
|
dd1aa28390 | ||
|
|
abbf14cd32 | ||
|
|
e8292f076d | ||
|
|
3037dccaf6 | ||
|
|
a7793468aa | ||
|
|
caa987faad | ||
|
|
0892f0e5b7 | ||
|
|
1d7857bc63 | ||
|
|
72a1d81b51 | ||
|
|
2c580a0afb | ||
|
|
05e8b0ae80 | ||
|
|
16dd77e8d9 | ||
|
|
0600f968c3 | ||
|
|
971b1d557d | ||
|
|
11d97c34d5 | ||
|
|
66be42549e | ||
|
|
067db30d71 | ||
|
|
da4dbe65b2 | ||
|
|
4321fde2a7 | ||
|
|
8f3df1d471 | ||
|
|
1a17211707 | ||
|
|
d56e3a6481 | ||
|
|
7820a47184 | ||
|
|
88b185ada1 | ||
|
|
ef3c45d523 | ||
|
|
20a48a818f | ||
|
|
c9e6ed1d9f | ||
|
|
56fdab67d8 | ||
|
|
3a98c8670d | ||
|
|
da585783e3 | ||
|
|
e81f184164 | ||
|
|
3e43275308 | ||
|
|
f97307b548 | ||
|
|
6402bfb1f8 | ||
|
|
27b6bbdcf3 | ||
|
|
5a198de3c1 | ||
|
|
c5d7ba7347 | ||
|
|
f84c2ebc4a | ||
|
|
c8897f2b08 | ||
|
|
6bb2cac3db | ||
|
|
88f020b626 | ||
|
|
a38f093f05 | ||
|
|
b6adfdd01b | ||
|
|
b90b4bbd3a | ||
|
|
f6d95140ae | ||
|
|
eca17b24ea | ||
|
|
6b61582e47 | ||
|
|
60bdc370d9 | ||
|
|
b9e31f73ee | ||
|
|
ea98620d98 | ||
|
|
6b78040840 | ||
|
|
e931c7c0f6 | ||
|
|
70160320b1 | ||
|
|
bfbf56f15a | ||
|
|
732ef82c72 | ||
|
|
2a1da64872 | ||
|
|
3c5d71cf77 | ||
|
|
1848bcc15b | ||
|
|
4d2de484f6 | ||
|
|
312f78d925 | ||
|
|
e4b69d6c9c | ||
|
|
c9a2dfeb2c | ||
|
|
9fe5be3acf | ||
|
|
611680af08 | ||
|
|
cb1d8f0fa0 | ||
|
|
33f4fef410 | ||
|
|
e77534c08f | ||
|
|
a22b54bbb6 | ||
|
|
414ebe8ce1 | ||
|
|
33ace03b5a | ||
|
|
085181a08a | ||
|
|
dd22ea4da5 | ||
|
|
5fdcfeb353 | ||
|
|
d68d8287aa | ||
|
|
e1249970c2 | ||
|
|
9d6f4e48a6 | ||
|
|
b546a042e8 | ||
|
|
bd9e68cfcd | ||
|
|
9747f3c87a | ||
|
|
12d9731a49 | ||
|
|
4aade854bb | ||
|
|
369dd65318 | ||
|
|
209602ac31 | ||
|
|
6251d889ca | ||
|
|
faec0b0824 | ||
|
|
64d93e7cf9 | ||
|
|
d73c5bcfe7 | ||
|
|
e78c1c54aa | ||
|
|
42049b42f1 | ||
|
|
a8cd072c76 | ||
|
|
83e9897cf0 | ||
|
|
f93704728f | ||
|
|
c9e3d32182 | ||
|
|
00050b8a6b | ||
|
|
19a1e19ae1 | ||
|
|
3a9516ffc8 | ||
|
|
42fa305649 | ||
|
|
9319735744 | ||
|
|
1ea6e8064c | ||
|
|
c1746340c3 | ||
|
|
d0339e7b72 | ||
|
|
075b7918c3 | ||
|
|
7c99149a76 | ||
|
|
27d95f1880 | ||
|
|
f0d523887d | ||
|
|
f29fbf2bf6 | ||
|
|
4355e1afcd | ||
|
|
cae1eff901 | ||
|
|
f83bc86e51 | ||
|
|
59744f0311 | ||
|
|
bb2e2bec18 | ||
|
|
efaaf99c42 | ||
|
|
f8a571ee35 | ||
|
|
397e32399b | ||
|
|
373363cb69 | ||
|
|
ad5574c4c9 | ||
|
|
264f8c5e7b | ||
|
|
9e9e2ea287 | ||
|
|
03d917f229 | ||
|
|
c3488f5115 | ||
|
|
6fef372fb8 | ||
|
|
052a1cc46c | ||
|
|
ae067dcddd | ||
|
|
5d60470508 | ||
|
|
88d7ffb0d0 | ||
|
|
47527d2d3c | ||
|
|
1444d28476 | ||
|
|
2de803c250 | ||
|
|
fa08b42dd8 | ||
|
|
eea734aa88 | ||
|
|
b6d91e24e2 | ||
|
|
a82ab0852e | ||
|
|
b8af9c56c9 | ||
|
|
c3c0b52a1f | ||
|
|
ec422ec7d0 | ||
|
|
a8a15ff1bd | ||
|
|
131d66efd2 | ||
|
|
bbf1b3060a | ||
|
|
8b7f271ea2 | ||
|
|
bc323435ee | ||
|
|
6769f34817 | ||
|
|
0b110f7922 | ||
|
|
205ee1ec84 | ||
|
|
b48847c5fa | ||
|
|
1143bf9286 | ||
|
|
d9ecc13545 | ||
|
|
f00d692445 | ||
|
|
d8b0bbb2ad | ||
|
|
1f4ee4a4d2 | ||
|
|
6a9efa8b8e | ||
|
|
65459e7938 | ||
|
|
42359b8b13 | ||
|
|
169870e37d | ||
|
|
78e454d864 | ||
|
|
dbfe28e199 | ||
|
|
d59c52753f | ||
|
|
62e1a4c84d | ||
|
|
81411e8913 | ||
|
|
62aa717f7e | ||
|
|
a5614eae3c | ||
|
|
536bae5871 | ||
|
|
679eddf296 | ||
|
|
d991def36c | ||
|
|
8b195533d2 | ||
|
|
3ccdd57c26 | ||
|
|
a103455dda | ||
|
|
60242e1930 | ||
|
|
a0e9bfbb48 | ||
|
|
2f19e0ba16 | ||
|
|
ab7fdcbbed | ||
|
|
48cf1de356 | ||
|
|
8d50122af0 | ||
|
|
fd379b38f7 | ||
|
|
aa4d865077 | ||
|
|
3e94febfc1 | ||
|
|
243b3a1a47 | ||
|
|
389e808c60 | ||
|
|
450465c4d4 | ||
|
|
2f44cc9f4d | ||
|
|
d106f3f43c | ||
|
|
bf3091d94f | ||
|
|
4dbf7285a8 | ||
|
|
a1e41e3a12 | ||
|
|
9d0044ce53 | ||
|
|
37bf74efb7 | ||
|
|
8c37d3b9d6 | ||
|
|
0af581f0bf | ||
|
|
2a506ea9d2 | ||
|
|
e5ec547eb3 | ||
|
|
6d383202dc | ||
|
|
7b8166d7b3 | ||
|
|
3636bbad3a | ||
|
|
82f9f3e552 | ||
|
|
c96ad1c945 | ||
|
|
5b9fbfa006 | ||
|
|
f0cc2d5506 | ||
|
|
d289ac81d3 | ||
|
|
15791f93fe | ||
|
|
d763b69740 | ||
|
|
36dd1af92d | ||
|
|
25b6dae7c0 | ||
|
|
1630c2533a | ||
|
|
1d373d77de | ||
|
|
f025b0d160 | ||
|
|
cc02b4729b | ||
|
|
2bb3830fc1 | ||
|
|
7a38bdd4b3 | ||
|
|
7614b17e85 | ||
|
|
6dfdb76538 | ||
|
|
9a3c51cff1 | ||
|
|
6336d2f9e1 | ||
|
|
ec6677e551 | ||
|
|
20cbca699a | ||
|
|
3211a9648a | ||
|
|
0baa915343 | ||
|
|
5cddb264d4 | ||
|
|
9863223fbf | ||
|
|
9a1948e67d | ||
|
|
f9deeac632 | ||
|
|
29f0021837 | ||
|
|
7acddb871d | ||
|
|
a7ca46405d | ||
|
|
0e2297afaa | ||
|
|
0a1891f6a0 | ||
|
|
1936a9e53b | ||
|
|
820ec63bdf | ||
|
|
01ea523b80 | ||
|
|
88cf0836fc | ||
|
|
3ec9ee0d0f | ||
|
|
21c9ebf4a9 | ||
|
|
4fb77c4308 | ||
|
|
bced00ab9e | ||
|
|
25116a3065 | ||
|
|
eadbb9cff4 | ||
|
|
42b947296b | ||
|
|
f37e65d1cb | ||
|
|
0ef5cf2289 | ||
|
|
fed9408ab5 | ||
|
|
ce23901f04 | ||
|
|
df1ee1fb1c | ||
|
|
f1d0276684 | ||
|
|
7ecc2ea597 | ||
|
|
7a35f23c16 | ||
|
|
9284742a11 | ||
|
|
9704ff4cb1 | ||
|
|
e3c0ce9a69 | ||
|
|
85b76bcc01 | ||
|
|
a275d9a25b | ||
|
|
7e0be1fbde | ||
|
|
54ba642cc3 | ||
|
|
8826eb7918 | ||
|
|
e701a86385 | ||
|
|
3e1f731826 | ||
|
|
f86c1367db | ||
|
|
58fd8aa851 | ||
|
|
3226ac2da8 | ||
|
|
3e9daa7416 | ||
|
|
7236df875a | ||
|
|
675e608325 | ||
|
|
1dc0e82aeb | ||
|
|
c2eb02aaf6 | ||
|
|
2fee7e42c9 | ||
|
|
281db390e8 | ||
|
|
df8cf53cc9 | ||
|
|
40306b10db | ||
|
|
5eff5d3eac | ||
|
|
8ad8426c43 | ||
|
|
3cab7cd025 | ||
|
|
bb26efbbec | ||
|
|
5c0e5fd36d | ||
|
|
621322a305 | ||
|
|
e33a3b8e0d | ||
|
|
9a6cccb08c | ||
|
|
b58225e93b | ||
|
|
852b919465 | ||
|
|
ef94999647 | ||
|
|
6f30fa98d8 | ||
|
|
74102bd716 | ||
|
|
8d82aa821a | ||
|
|
cec1ffb80b | ||
|
|
870967ca77 | ||
|
|
66fc0f554a | ||
|
|
d6e4c29733 | ||
|
|
3e42969979 | ||
|
|
712ac505e0 | ||
|
|
f935d3397e | ||
|
|
30dd3a2dbc | ||
|
|
b04f88d581 | ||
|
|
b3c10c8c41 | ||
|
|
5c1bd89a1c | ||
|
|
15f3ab09eb | ||
|
|
c7e834f424 | ||
|
|
8c1a9899d4 | ||
|
|
05caf09a36 | ||
|
|
168a865e60 | ||
|
|
15c17c24fa | ||
|
|
45cf24485d | ||
|
|
c56e2b2e30 | ||
|
|
d1608c597e | ||
|
|
0f4903a5d7 | ||
|
|
772f25d3dd | ||
|
|
f1a1eda7c5 | ||
|
|
41259bff31 | ||
|
|
afaa98a666 | ||
|
|
73be918285 | ||
|
|
ca412214cb | ||
|
|
801722825d | ||
|
|
3abc25fa54 | ||
|
|
f4d67761f1 | ||
|
|
369c5fe3c0 | ||
|
|
7918c6cf11 | ||
|
|
826d70fcba | ||
|
|
bbb23048e3 | ||
|
|
5a3a1fe458 | ||
|
|
56fb06b6f5 | ||
|
|
995a9f7188 | ||
|
|
a0ef046ef1 | ||
|
|
5fa51fc426 | ||
|
|
15057aa0a4 | ||
|
|
1431b52e76 | ||
|
|
98fe770cab | ||
|
|
43382ce5a2 | ||
|
|
abfebf1e21 | ||
|
|
b1c02c7f00 | ||
|
|
84df3ac267 | ||
|
|
55a70c9719 | ||
|
|
0d50b87aa4 | ||
|
|
19290a8e92 | ||
|
|
d845963349 | ||
|
|
8dae4657a1 | ||
|
|
ca7be1cfeb | ||
|
|
445872a6e2 | ||
|
|
3681d025ac | ||
|
|
2998049f51 | ||
|
|
24ccc7c038 | ||
|
|
be48c4d91e | ||
|
|
a19f9056f3 | ||
|
|
5b71ab780c | ||
|
|
481bafd581 | ||
|
|
e74b250d71 | ||
|
|
cd54c95ee1 | ||
|
|
bf006eeaf5 | ||
|
|
b2afc410fa | ||
|
|
19cfa32393 | ||
|
|
27ae8432b6 | ||
|
|
415ee250b5 | ||
|
|
f188e1000b | ||
|
|
07d64e78b6 | ||
|
|
fa649fbc26 | ||
|
|
0c3e0fd95d | ||
|
|
3bb6443131 | ||
|
|
f57afd6e32 | ||
|
|
5f664a4516 | ||
|
|
87fe07c0d4 | ||
|
|
f9a9bd77e4 | ||
|
|
63b8a6fd20 | ||
|
|
024f2374ab | ||
|
|
9d9f9c48ff | ||
|
|
15d48576ea | ||
|
|
39b071f7b1 | ||
|
|
9efc257d9d | ||
|
|
fa71304e54 | ||
|
|
b5745d11cd | ||
|
|
ebcf546a55 | ||
|
|
2b45f8967c | ||
|
|
a66404aca6 | ||
|
|
d80659759b | ||
|
|
d24253d92f | ||
|
|
2cffb08a5c | ||
|
|
15f40fddca | ||
|
|
970995c3f2 | ||
|
|
b17c76817d | ||
|
|
b074306267 | ||
|
|
3c75b75516 | ||
|
|
36a7fda014 | ||
|
|
1bb3fb73cc | ||
|
|
7e01348658 | ||
|
|
28b3017baf | ||
|
|
ae808860ae | ||
|
|
a47e8c7dd0 | ||
|
|
79ce619876 | ||
|
|
233f0b0cc7 | ||
|
|
025589f772 | ||
|
|
68f337dfa6 | ||
|
|
f132ac03bc | ||
|
|
ec785a1d65 | ||
|
|
e0621e6115 | ||
|
|
38411aa102 | ||
|
|
3ec4f4eb86 | ||
|
|
367139c6d9 | ||
|
|
457bac94ce | ||
|
|
bcf46ee83b | ||
|
|
97b2fd1ba1 | ||
|
|
e13753e2fb | ||
|
|
ec79f25286 | ||
|
|
18ea2eff80 | ||
|
|
8156604823 | ||
|
|
36b6fdda83 | ||
|
|
3c67d2595b | ||
|
|
2043a0ca30 |
200
bugs
Normal file
200
bugs
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
|
||||||
|
** lua.stx / llex.c
|
||||||
|
Tue Dec 2 10:45:48 EDT 1997
|
||||||
|
>> BUG: "lastline" was not reset on function entry, so debug information
|
||||||
|
>> started only in the 2nd line of a function.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=================================================================
|
||||||
|
--- Version 3.1 alpha
|
||||||
|
|
||||||
|
** lua.c
|
||||||
|
Thu Jan 15 14:34:58 EDT 1998
|
||||||
|
>> must include "stdlib.h" (for "exit()").
|
||||||
|
|
||||||
|
** lbuiltin.c / lobject.h
|
||||||
|
Thu Jan 15 14:34:58 EDT 1998
|
||||||
|
>> MAX_WORD may be bigger than MAX_INT
|
||||||
|
(by lhf)
|
||||||
|
|
||||||
|
** llex.c
|
||||||
|
Mon Jan 19 18:17:18 EDT 1998
|
||||||
|
>> wrong line number (+1) in error report when file starts with "#..."
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Tue Jan 27 15:27:49 EDT 1998
|
||||||
|
>> formats like "%020d" were considered too big (3 digits); moreover,
|
||||||
|
>> some sistems limit printf to at most 500 chars, so we can limit sizes
|
||||||
|
>> to 2 digits (99).
|
||||||
|
|
||||||
|
** lapi.c
|
||||||
|
Tue Jan 27 17:12:36 EDT 1998
|
||||||
|
>> "lua_getstring" may create a new string, so should check GC
|
||||||
|
|
||||||
|
** lstring.c / ltable.c
|
||||||
|
Wed Jan 28 14:48:12 EDT 1998
|
||||||
|
>> tables can become full of "empty" slots, and keep growing without limits.
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Mon Mar 9 15:26:09 EST 1998
|
||||||
|
>> gsub('a', '(b?)%1*' ...) loops (because the capture is empty).
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Mon May 18 19:20:00 EST 1998
|
||||||
|
>> arguments for "format" 'x', 'X', 'o' and 'u' must be unsigned int.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=================================================================
|
||||||
|
--- Version 3.1
|
||||||
|
|
||||||
|
** liolib.c / lauxlib.c
|
||||||
|
Mon Sep 7 15:57:02 EST 1998
|
||||||
|
>> function "luaL_argerror" prints wrong argument number (from a user's point
|
||||||
|
of view) when functions have upvalues.
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Tue Nov 10 17:29:36 EDT 1998
|
||||||
|
>> gsub/strfind do not check whether captures are properly finished.
|
||||||
|
(by roberto/tomas)
|
||||||
|
|
||||||
|
** lbuiltin.c
|
||||||
|
Fri Dec 18 11:22:55 EDT 1998
|
||||||
|
>> "tonumber" goes crazy with negative numbers in other bases (not 10),
|
||||||
|
because "strtol" returns long, not unsigned long.
|
||||||
|
(by Visual C++)
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Mon Jan 4 10:41:40 EDT 1999
|
||||||
|
>> "format" does not check size of format item (such as "%00000...00000d").
|
||||||
|
|
||||||
|
** lapi.c
|
||||||
|
Wed Feb 3 14:40:21 EDT 1999
|
||||||
|
>> getlocal cannot return the local itself, since lua_isstring and
|
||||||
|
lua_isnumber can modify it.
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Thu Feb 4 17:08:50 EDT 1999
|
||||||
|
>> format "%s" may break limit of "sprintf" on some machines.
|
||||||
|
(by Marcelo Sales)
|
||||||
|
|
||||||
|
** lzio.c
|
||||||
|
Thu Mar 4 11:49:37 EST 1999
|
||||||
|
>> file stream cannot call fread after EOF.
|
||||||
|
(by lhf)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=================================================================
|
||||||
|
--- Version 3.2 (beta)
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Fri Apr 30 11:10:20 EST 1999
|
||||||
|
>> '$' at end of pattern was matching regular '$', too.
|
||||||
|
(by anna; since 2.5)
|
||||||
|
|
||||||
|
** lbuiltin.c
|
||||||
|
Fri May 21 17:15:11 EST 1999
|
||||||
|
>> foreach, foreachi, foreachvar points to function in stack when stack
|
||||||
|
can be reallocated.
|
||||||
|
(by tomas; since 3.2 beta)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Wed Jun 16 10:32:46 EST 1999
|
||||||
|
>> cannot assign to unlimited variables, because it causes overflow in
|
||||||
|
the number of returns of a function.
|
||||||
|
(since 3.1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=================================================================
|
||||||
|
--- Version 3.2
|
||||||
|
|
||||||
|
** lmathlib.c
|
||||||
|
Wed Aug 18 11:28:38 EST 1999
|
||||||
|
>> random(0) and random(x,0) are wrong (0 is read as no argument!).
|
||||||
|
(by Dave Bollinger; since 3.1)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Thu Sep 2 10:07:20 EST 1999
|
||||||
|
>> in the (old) expression << ls->fs->f->consts[checkname(ls)] >>, checkname
|
||||||
|
could realloc f->consts.
|
||||||
|
(by Supratik Champati; since 3.2 beta)
|
||||||
|
|
||||||
|
** lobject.c / lbuiltin.c
|
||||||
|
Wed Sep 8 17:41:54 EST 1999
|
||||||
|
>> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.
|
||||||
|
(since 3.1)
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Thu Nov 11 14:36:30 EDT 1999
|
||||||
|
>> `strfind' does not handle \0 in plain search.
|
||||||
|
(by Jon Kleiser; since 3.1)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Wed Dec 29 16:05:43 EDT 1999
|
||||||
|
>> return gives wrong line in debug information
|
||||||
|
(by lhf; since 3.2 [at least])
|
||||||
|
|
||||||
|
** ldo.c
|
||||||
|
Thu Dec 30 16:39:33 EDT 1999
|
||||||
|
>> cannot reopen stdin (for binary mode)
|
||||||
|
(by lhf & roberto; since 3.1)
|
||||||
|
|
||||||
|
** lapi.c
|
||||||
|
Thu Mar 2 09:41:53 EST 2000
|
||||||
|
>> lua_settable should check stack space (it could call a T.M.)
|
||||||
|
(by lhf & celes; since 3.2; it was already fixed by fixed stack)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Mon Apr 3 09:59:06 EST 2000
|
||||||
|
>> '%' should be in expfollow
|
||||||
|
(by Edgar Toernig; since 3.1; it was already fixed)
|
||||||
|
|
||||||
|
** lbuiltin.c
|
||||||
|
Mon Apr 3 10:05:05 EST 2000
|
||||||
|
>> tostring() without arguments gives seg. fault.
|
||||||
|
(by Edgar Toernig; since 3.0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=================================================================
|
||||||
|
--- Version 4.0 alpha
|
||||||
|
|
||||||
|
Tested with full test suites (as locked in Mon Apr 24 14:23:11 EST 2000)
|
||||||
|
in the following platforms:
|
||||||
|
* Linux - gcc, g++
|
||||||
|
* AIX - gcc
|
||||||
|
* Solaris - gcc, cc
|
||||||
|
* IRIX - cc, cc-purify
|
||||||
|
* Windows - Visual C++ (.c e .cpp, warning level=4)
|
||||||
|
|
||||||
|
|
||||||
|
** lstrlib.c
|
||||||
|
Tue May 2 15:27:58 EST 2000
|
||||||
|
>> `strfind' gets wrong subject length when there is an offset
|
||||||
|
(by Jon Kleiser; since 4.0a)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Fri May 12 15:11:12 EST 2000
|
||||||
|
>> first element in a list constructor is not adjusted to one value
|
||||||
|
>> (e.g. «a = {gsub('a','a','')}»)
|
||||||
|
(by Tomas; since 4.0a)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Wed May 24 14:50:16 EST 2000
|
||||||
|
>> record-constructor starting with an upvalue name gets an error
|
||||||
|
>> (e.g. «local a; function f() x = {a=1} end»)
|
||||||
|
(by Edgar Toernig; since 3.1)
|
||||||
|
|
||||||
|
** lparser.c
|
||||||
|
Tue Aug 29 15:56:05 EST 2000
|
||||||
|
>> error message for `for' uses `while'
|
||||||
|
(since 4.0a; already corrected)
|
||||||
|
|
||||||
|
** lgc.c
|
||||||
|
Tue Aug 29 15:57:41 EST 2000
|
||||||
|
>> gc tag method for nil could call line hook
|
||||||
|
(by ry; since ?)
|
||||||
|
|
||||||
171
fallback.c
171
fallback.c
@@ -1,171 +0,0 @@
|
|||||||
/*
|
|
||||||
** fallback.c
|
|
||||||
** TecCGraf - PUC-Rio
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_fallback="$Id: fallback.c,v 1.10 1994/12/20 21:20:36 roberto Exp $";
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "fallback.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
|
|
||||||
static void errorFB (void);
|
|
||||||
static void indexFB (void);
|
|
||||||
static void gettableFB (void);
|
|
||||||
static void arithFB (void);
|
|
||||||
static void concatFB (void);
|
|
||||||
static void orderFB (void);
|
|
||||||
static void GDFB (void);
|
|
||||||
static void funcFB (void);
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Warning: This list must be in the same order as the #define's
|
|
||||||
*/
|
|
||||||
struct FB luaI_fallBacks[] = {
|
|
||||||
{"error", {LUA_T_CFUNCTION, errorFB}},
|
|
||||||
{"index", {LUA_T_CFUNCTION, indexFB}},
|
|
||||||
{"gettable", {LUA_T_CFUNCTION, gettableFB}},
|
|
||||||
{"arith", {LUA_T_CFUNCTION, arithFB}},
|
|
||||||
{"order", {LUA_T_CFUNCTION, orderFB}},
|
|
||||||
{"concat", {LUA_T_CFUNCTION, concatFB}},
|
|
||||||
{"settable", {LUA_T_CFUNCTION, gettableFB}},
|
|
||||||
{"gc", {LUA_T_CFUNCTION, GDFB}},
|
|
||||||
{"function", {LUA_T_CFUNCTION, funcFB}}
|
|
||||||
};
|
|
||||||
|
|
||||||
#define N_FB (sizeof(luaI_fallBacks)/sizeof(struct FB))
|
|
||||||
|
|
||||||
void luaI_setfallback (void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *name = lua_getstring(lua_getparam(1));
|
|
||||||
lua_Object func = lua_getparam(2);
|
|
||||||
if (name == NULL || !(lua_isfunction(func) || lua_iscfunction(func)))
|
|
||||||
{
|
|
||||||
lua_pushnil();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (i=0; i<N_FB; i++)
|
|
||||||
{
|
|
||||||
if (strcmp(luaI_fallBacks[i].kind, name) == 0)
|
|
||||||
{
|
|
||||||
luaI_pushobject(&luaI_fallBacks[i].function);
|
|
||||||
luaI_fallBacks[i].function = *luaI_Address(func);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* name not found */
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void errorFB (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
if (lua_isstring(o))
|
|
||||||
fprintf (stderr, "lua: %s\n", lua_getstring(o));
|
|
||||||
else
|
|
||||||
fprintf(stderr, "lua: unknown error\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void indexFB (void)
|
|
||||||
{
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void gettableFB (void)
|
|
||||||
{
|
|
||||||
lua_reportbug("indexed expression not a table");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void arithFB (void)
|
|
||||||
{
|
|
||||||
lua_reportbug("unexpected type at conversion to number");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void concatFB (void)
|
|
||||||
{
|
|
||||||
lua_reportbug("unexpected type at conversion to string");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void orderFB (void)
|
|
||||||
{
|
|
||||||
lua_reportbug("unexpected type at comparison");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void GDFB (void) { }
|
|
||||||
|
|
||||||
static void funcFB (void)
|
|
||||||
{
|
|
||||||
lua_reportbug("call expression not a function");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Lock routines
|
|
||||||
*/
|
|
||||||
|
|
||||||
static Object *lockArray = NULL;
|
|
||||||
static Word lockSize = 0;
|
|
||||||
|
|
||||||
int luaI_lock (Object *object)
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
Word oldSize;
|
|
||||||
if (tag(object) == LUA_T_NIL)
|
|
||||||
return -1;
|
|
||||||
for (i=0; i<lockSize; i++)
|
|
||||||
if (tag(&lockArray[i]) == LUA_T_NIL)
|
|
||||||
{
|
|
||||||
lockArray[i] = *object;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
/* no more empty spaces */
|
|
||||||
oldSize = lockSize;
|
|
||||||
if (lockArray == NULL)
|
|
||||||
{
|
|
||||||
lockSize = 10;
|
|
||||||
lockArray = newvector(lockSize, Object);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lockSize = 3*oldSize/2 + 5;
|
|
||||||
lockArray = growvector(lockArray, lockSize, Object);
|
|
||||||
}
|
|
||||||
for (i=oldSize; i<lockSize; i++)
|
|
||||||
tag(&lockArray[i]) = LUA_T_NIL;
|
|
||||||
lockArray[oldSize] = *object;
|
|
||||||
return oldSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void lua_unlock (int ref)
|
|
||||||
{
|
|
||||||
tag(&lockArray[ref]) = LUA_T_NIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Object *luaI_getlocked (int ref)
|
|
||||||
{
|
|
||||||
return &lockArray[ref];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaI_travlock (void (*fn)(Object *))
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
for (i=0; i<lockSize; i++)
|
|
||||||
fn(&lockArray[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
31
fallback.h
31
fallback.h
@@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
** $Id: fallback.h,v 1.6 1994/11/21 13:30:15 roberto Exp roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef fallback_h
|
|
||||||
#define fallback_h
|
|
||||||
|
|
||||||
#include "opcode.h"
|
|
||||||
|
|
||||||
extern struct FB {
|
|
||||||
char *kind;
|
|
||||||
Object function;
|
|
||||||
} luaI_fallBacks[];
|
|
||||||
|
|
||||||
#define FB_ERROR 0
|
|
||||||
#define FB_INDEX 1
|
|
||||||
#define FB_GETTABLE 2
|
|
||||||
#define FB_ARITH 3
|
|
||||||
#define FB_ORDER 4
|
|
||||||
#define FB_CONCAT 5
|
|
||||||
#define FB_SETTABLE 6
|
|
||||||
#define FB_GC 7
|
|
||||||
#define FB_FUNCTION 8
|
|
||||||
|
|
||||||
void luaI_setfallback (void);
|
|
||||||
int luaI_lock (Object *object);
|
|
||||||
Object *luaI_getlocked (int ref);
|
|
||||||
void luaI_travlock (void (*fn)(Object *));
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
336
hash.c
336
hash.c
@@ -1,336 +0,0 @@
|
|||||||
/*
|
|
||||||
** hash.c
|
|
||||||
** hash manager for lua
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_hash="$Id: hash.c,v 2.23 1995/01/12 14:19:04 roberto Exp $";
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#define streq(s1,s2) (s1 == s2 || (*(s1) == *(s2) && strcmp(s1,s2)==0))
|
|
||||||
|
|
||||||
#define nhash(t) ((t)->nhash)
|
|
||||||
#define nuse(t) ((t)->nuse)
|
|
||||||
#define markarray(t) ((t)->mark)
|
|
||||||
#define nodevector(t) ((t)->node)
|
|
||||||
#define node(t,i) (&(t)->node[i])
|
|
||||||
#define ref(n) (&(n)->ref)
|
|
||||||
#define val(n) (&(n)->val)
|
|
||||||
|
|
||||||
|
|
||||||
#define REHASH_LIMIT 0.70 /* avoid more than this % full */
|
|
||||||
|
|
||||||
|
|
||||||
static Hash *listhead = NULL;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* hash dimensions values */
|
|
||||||
static Word dimensions[] =
|
|
||||||
{3, 5, 7, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421,
|
|
||||||
12853, 25717, 51437, 65521, 0}; /* 65521 == last prime < MAX_WORD */
|
|
||||||
|
|
||||||
static Word redimension (Word nhash)
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
for (i=0; dimensions[i]!=0; i++)
|
|
||||||
{
|
|
||||||
if (dimensions[i] > nhash)
|
|
||||||
return dimensions[i];
|
|
||||||
}
|
|
||||||
lua_error("table overflow");
|
|
||||||
return 0; /* to avoid warnings */
|
|
||||||
}
|
|
||||||
|
|
||||||
static Word hashindex (Hash *t, Object *ref) /* hash function */
|
|
||||||
{
|
|
||||||
switch (tag(ref))
|
|
||||||
{
|
|
||||||
case LUA_T_NIL:
|
|
||||||
lua_reportbug ("unexpected type to index table");
|
|
||||||
return -1; /* UNREACHEABLE */
|
|
||||||
case LUA_T_NUMBER:
|
|
||||||
return (((Word)nvalue(ref))%nhash(t));
|
|
||||||
case LUA_T_STRING:
|
|
||||||
{
|
|
||||||
unsigned long h = tsvalue(ref)->hash;
|
|
||||||
if (h == 0)
|
|
||||||
{
|
|
||||||
char *name = svalue(ref);
|
|
||||||
while (*name)
|
|
||||||
h = ((h<<5)-h)^(unsigned char)*(name++);
|
|
||||||
tsvalue(ref)->hash = h;
|
|
||||||
}
|
|
||||||
return (Word)h%nhash(t); /* make it a valid index */
|
|
||||||
}
|
|
||||||
case LUA_T_FUNCTION:
|
|
||||||
return (((IntPoint)bvalue(ref))%nhash(t));
|
|
||||||
case LUA_T_CFUNCTION:
|
|
||||||
return (((IntPoint)fvalue(ref))%nhash(t));
|
|
||||||
case LUA_T_ARRAY:
|
|
||||||
return (((IntPoint)avalue(ref))%nhash(t));
|
|
||||||
default: /* user data */
|
|
||||||
return (((IntPoint)uvalue(ref))%nhash(t));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Bool lua_equalObj (Object *t1, Object *t2)
|
|
||||||
{
|
|
||||||
if (tag(t1) != tag(t2)) return 0;
|
|
||||||
switch (tag(t1))
|
|
||||||
{
|
|
||||||
case LUA_T_NIL: return 1;
|
|
||||||
case LUA_T_NUMBER: return nvalue(t1) == nvalue(t2);
|
|
||||||
case LUA_T_STRING: return streq(svalue(t1), svalue(t2));
|
|
||||||
case LUA_T_ARRAY: return avalue(t1) == avalue(t2);
|
|
||||||
case LUA_T_FUNCTION: return bvalue(t1) == bvalue(t2);
|
|
||||||
case LUA_T_CFUNCTION: return fvalue(t1) == fvalue(t2);
|
|
||||||
default: return uvalue(t1) == uvalue(t2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Word present (Hash *t, Object *ref)
|
|
||||||
{
|
|
||||||
Word h = hashindex(t, ref);
|
|
||||||
while (tag(ref(node(t, h))) != LUA_T_NIL)
|
|
||||||
{
|
|
||||||
if (lua_equalObj(ref, ref(node(t, h))))
|
|
||||||
return h;
|
|
||||||
h = (h+1) % nhash(t);
|
|
||||||
}
|
|
||||||
return h;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Alloc a vector node
|
|
||||||
*/
|
|
||||||
static Node *hashnodecreate (Word nhash)
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
Node *v = newvector (nhash, Node);
|
|
||||||
for (i=0; i<nhash; i++)
|
|
||||||
tag(ref(&v[i])) = LUA_T_NIL;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Create a new hash. Return the hash pointer or NULL on error.
|
|
||||||
*/
|
|
||||||
static Hash *hashcreate (Word nhash)
|
|
||||||
{
|
|
||||||
Hash *t = new(Hash);
|
|
||||||
nhash = redimension((Word)((float)nhash/REHASH_LIMIT));
|
|
||||||
nodevector(t) = hashnodecreate(nhash);
|
|
||||||
nhash(t) = nhash;
|
|
||||||
nuse(t) = 0;
|
|
||||||
markarray(t) = 0;
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Delete a hash
|
|
||||||
*/
|
|
||||||
static void hashdelete (Hash *t)
|
|
||||||
{
|
|
||||||
luaI_free (nodevector(t));
|
|
||||||
luaI_free(t);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Mark a hash and check its elements
|
|
||||||
*/
|
|
||||||
void lua_hashmark (Hash *h)
|
|
||||||
{
|
|
||||||
if (markarray(h) == 0)
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
markarray(h) = 1;
|
|
||||||
for (i=0; i<nhash(h); i++)
|
|
||||||
{
|
|
||||||
Node *n = node(h,i);
|
|
||||||
if (tag(ref(n)) != LUA_T_NIL)
|
|
||||||
{
|
|
||||||
lua_markobject(&n->ref);
|
|
||||||
lua_markobject(&n->val);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void call_fallbacks (void)
|
|
||||||
{
|
|
||||||
Hash *curr_array;
|
|
||||||
Object t;
|
|
||||||
tag(&t) = LUA_T_ARRAY;
|
|
||||||
for (curr_array = listhead; curr_array; curr_array = curr_array->next)
|
|
||||||
if (markarray(curr_array) != 1)
|
|
||||||
{
|
|
||||||
avalue(&t) = curr_array;
|
|
||||||
luaI_gcFB(&t);
|
|
||||||
}
|
|
||||||
tag(&t) = LUA_T_NIL;
|
|
||||||
luaI_gcFB(&t); /* end of list */
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Garbage collection to arrays
|
|
||||||
** Delete all unmarked arrays.
|
|
||||||
*/
|
|
||||||
Long lua_hashcollector (void)
|
|
||||||
{
|
|
||||||
Hash *curr_array = listhead, *prev = NULL;
|
|
||||||
Long counter = 0;
|
|
||||||
call_fallbacks();
|
|
||||||
while (curr_array != NULL)
|
|
||||||
{
|
|
||||||
Hash *next = curr_array->next;
|
|
||||||
if (markarray(curr_array) != 1)
|
|
||||||
{
|
|
||||||
if (prev == NULL) listhead = next;
|
|
||||||
else prev->next = next;
|
|
||||||
hashdelete(curr_array);
|
|
||||||
++counter;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
markarray(curr_array) = 0;
|
|
||||||
prev = curr_array;
|
|
||||||
}
|
|
||||||
curr_array = next;
|
|
||||||
}
|
|
||||||
return counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Create a new array
|
|
||||||
** This function inserts the new array in the array list. It also
|
|
||||||
** executes garbage collection if the number of arrays created
|
|
||||||
** exceed a pre-defined range.
|
|
||||||
*/
|
|
||||||
Hash *lua_createarray (Word nhash)
|
|
||||||
{
|
|
||||||
Hash *array;
|
|
||||||
lua_pack();
|
|
||||||
array = hashcreate(nhash);
|
|
||||||
array->next = listhead;
|
|
||||||
listhead = array;
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Re-hash
|
|
||||||
*/
|
|
||||||
static void rehash (Hash *t)
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
Word nold = nhash(t);
|
|
||||||
Node *vold = nodevector(t);
|
|
||||||
nhash(t) = redimension(nhash(t));
|
|
||||||
nodevector(t) = hashnodecreate(nhash(t));
|
|
||||||
for (i=0; i<nold; i++)
|
|
||||||
{
|
|
||||||
Node *n = vold+i;
|
|
||||||
if (tag(ref(n)) != LUA_T_NIL && tag(val(n)) != LUA_T_NIL)
|
|
||||||
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */
|
|
||||||
}
|
|
||||||
luaI_free(vold);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** If the hash node is present, return its pointer, otherwise return
|
|
||||||
** null.
|
|
||||||
*/
|
|
||||||
Object *lua_hashget (Hash *t, Object *ref)
|
|
||||||
{
|
|
||||||
Word h = present(t, ref);
|
|
||||||
if (tag(ref(node(t, h))) != LUA_T_NIL) return val(node(t, h));
|
|
||||||
else return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** If the hash node is present, return its pointer, otherwise create a new
|
|
||||||
** node for the given reference and also return its pointer.
|
|
||||||
*/
|
|
||||||
Object *lua_hashdefine (Hash *t, Object *ref)
|
|
||||||
{
|
|
||||||
Word h;
|
|
||||||
Node *n;
|
|
||||||
h = present(t, ref);
|
|
||||||
n = node(t, h);
|
|
||||||
if (tag(ref(n)) == LUA_T_NIL)
|
|
||||||
{
|
|
||||||
nuse(t)++;
|
|
||||||
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT)
|
|
||||||
{
|
|
||||||
rehash(t);
|
|
||||||
h = present(t, ref);
|
|
||||||
n = node(t, h);
|
|
||||||
}
|
|
||||||
*ref(n) = *ref;
|
|
||||||
tag(val(n)) = LUA_T_NIL;
|
|
||||||
}
|
|
||||||
return (val(n));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function to manipulate arrays.
|
|
||||||
** Given an array object and a reference value, return the next element
|
|
||||||
** in the hash.
|
|
||||||
** This function pushs the element value and its reference to the stack.
|
|
||||||
*/
|
|
||||||
static void hashnext (Hash *t, Word i)
|
|
||||||
{
|
|
||||||
if (i >= nhash(t))
|
|
||||||
{
|
|
||||||
lua_pushnil(); lua_pushnil();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while (tag(ref(node(t,i))) == LUA_T_NIL || tag(val(node(t,i))) == LUA_T_NIL)
|
|
||||||
{
|
|
||||||
if (++i >= nhash(t))
|
|
||||||
{
|
|
||||||
lua_pushnil(); lua_pushnil();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
luaI_pushobject(ref(node(t,i)));
|
|
||||||
luaI_pushobject(val(node(t,i)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void lua_next (void)
|
|
||||||
{
|
|
||||||
Hash *t;
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
lua_Object r = lua_getparam(2);
|
|
||||||
if (o == LUA_NOOBJECT || r == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `next'");
|
|
||||||
if (lua_getparam(3) != LUA_NOOBJECT)
|
|
||||||
lua_error ("too many arguments to function `next'");
|
|
||||||
if (!lua_istable(o))
|
|
||||||
lua_error ("first argument of function `next' is not a table");
|
|
||||||
t = avalue(luaI_Address(o));
|
|
||||||
if (lua_isnil(r))
|
|
||||||
{
|
|
||||||
hashnext(t, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Word h = present (t, luaI_Address(r));
|
|
||||||
hashnext(t, h+1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
37
hash.h
37
hash.h
@@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
** hash.h
|
|
||||||
** hash manager for lua
|
|
||||||
** Luiz Henrique de Figueiredo - 17 Aug 90
|
|
||||||
** $Id: hash.h,v 2.7 1994/12/20 21:20:36 roberto Exp roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef hash_h
|
|
||||||
#define hash_h
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
typedef struct node
|
|
||||||
{
|
|
||||||
Object ref;
|
|
||||||
Object val;
|
|
||||||
} Node;
|
|
||||||
|
|
||||||
typedef struct Hash
|
|
||||||
{
|
|
||||||
struct Hash *next;
|
|
||||||
char mark;
|
|
||||||
Word nhash;
|
|
||||||
Word nuse;
|
|
||||||
Node *node;
|
|
||||||
} Hash;
|
|
||||||
|
|
||||||
|
|
||||||
Bool lua_equalObj (Object *t1, Object *t2);
|
|
||||||
Hash *lua_createarray (Word nhash);
|
|
||||||
void lua_hashmark (Hash *h);
|
|
||||||
Long lua_hashcollector (void);
|
|
||||||
Object *lua_hashget (Hash *t, Object *ref);
|
|
||||||
Object *lua_hashdefine (Hash *t, Object *ref);
|
|
||||||
void lua_next (void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
294
inout.c
294
inout.c
@@ -1,294 +0,0 @@
|
|||||||
/*
|
|
||||||
** inout.c
|
|
||||||
** Provide function to realise the input/output function and debugger
|
|
||||||
** facilities.
|
|
||||||
** Also provides some predefined lua functions.
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_inout="$Id: inout.c,v 2.15 1994/12/16 15:55:04 roberto Exp roberto $";
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "tree.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
/* Exported variables */
|
|
||||||
Word lua_linenumber;
|
|
||||||
Bool lua_debug;
|
|
||||||
Word lua_debugline = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/* Internal variables */
|
|
||||||
|
|
||||||
#ifndef MAXFUNCSTACK
|
|
||||||
#define MAXFUNCSTACK 100
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef struct FuncStackNode {
|
|
||||||
struct FuncStackNode *next;
|
|
||||||
char *file;
|
|
||||||
Word function;
|
|
||||||
Word line;
|
|
||||||
} FuncStackNode;
|
|
||||||
|
|
||||||
static FuncStackNode *funcStack = NULL;
|
|
||||||
static Word nfuncstack=0;
|
|
||||||
|
|
||||||
static FILE *fp;
|
|
||||||
static char *st;
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to get the next character from the input file
|
|
||||||
*/
|
|
||||||
static int fileinput (void)
|
|
||||||
{
|
|
||||||
return fgetc (fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to get the next character from the input string
|
|
||||||
*/
|
|
||||||
static int stringinput (void)
|
|
||||||
{
|
|
||||||
return *st++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to open a file to be input unit.
|
|
||||||
** Return 0 on success or error message on error.
|
|
||||||
*/
|
|
||||||
char *lua_openfile (char *fn)
|
|
||||||
{
|
|
||||||
lua_linenumber = 1;
|
|
||||||
lua_setinput (fileinput);
|
|
||||||
fp = fopen (fn, "r");
|
|
||||||
if (fp == NULL)
|
|
||||||
{
|
|
||||||
static char buff[32];
|
|
||||||
sprintf(buff, "unable to open file %.10s", fn);
|
|
||||||
return buff;
|
|
||||||
}
|
|
||||||
return lua_addfile (fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to close an opened file
|
|
||||||
*/
|
|
||||||
void lua_closefile (void)
|
|
||||||
{
|
|
||||||
if (fp != NULL)
|
|
||||||
{
|
|
||||||
lua_delfile();
|
|
||||||
fclose (fp);
|
|
||||||
fp = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to open a string to be input unit
|
|
||||||
*/
|
|
||||||
char *lua_openstring (char *s)
|
|
||||||
{
|
|
||||||
lua_linenumber = 1;
|
|
||||||
lua_setinput (stringinput);
|
|
||||||
st = s;
|
|
||||||
{
|
|
||||||
char sn[64];
|
|
||||||
sprintf (sn, "String: %10.10s...", s);
|
|
||||||
return lua_addfile (sn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Function to close an opened string
|
|
||||||
*/
|
|
||||||
void lua_closestring (void)
|
|
||||||
{
|
|
||||||
lua_delfile();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Called to execute SETFUNCTION opcode, this function pushs a function into
|
|
||||||
** function stack.
|
|
||||||
*/
|
|
||||||
void lua_pushfunction (char *file, Word function)
|
|
||||||
{
|
|
||||||
FuncStackNode *newNode;
|
|
||||||
if (nfuncstack++ >= MAXFUNCSTACK)
|
|
||||||
{
|
|
||||||
lua_reportbug("function stack overflow");
|
|
||||||
}
|
|
||||||
newNode = new(FuncStackNode);
|
|
||||||
newNode->function = function;
|
|
||||||
newNode->file = file;
|
|
||||||
newNode->line= lua_debugline;
|
|
||||||
newNode->next = funcStack;
|
|
||||||
funcStack = newNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Called to execute RESET opcode, this function pops a function from
|
|
||||||
** function stack.
|
|
||||||
*/
|
|
||||||
void lua_popfunction (void)
|
|
||||||
{
|
|
||||||
FuncStackNode *temp = funcStack;
|
|
||||||
if (temp == NULL) return;
|
|
||||||
--nfuncstack;
|
|
||||||
lua_debugline = temp->line;
|
|
||||||
funcStack = temp->next;
|
|
||||||
luaI_free(temp);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Report bug building a message and sending it to lua_error function.
|
|
||||||
*/
|
|
||||||
void lua_reportbug (char *s)
|
|
||||||
{
|
|
||||||
char msg[MAXFUNCSTACK*80];
|
|
||||||
strcpy (msg, s);
|
|
||||||
if (lua_debugline != 0)
|
|
||||||
{
|
|
||||||
if (funcStack)
|
|
||||||
{
|
|
||||||
FuncStackNode *func = funcStack;
|
|
||||||
int line = lua_debugline;
|
|
||||||
sprintf (strchr(msg,0), "\n\tactive stack:\n");
|
|
||||||
do
|
|
||||||
{
|
|
||||||
sprintf (strchr(msg,0),
|
|
||||||
"\t-> function \"%s\" at file \"%s\":%u\n",
|
|
||||||
lua_constant[func->function]->str, func->file, line);
|
|
||||||
line = func->line;
|
|
||||||
func = func->next;
|
|
||||||
lua_popfunction();
|
|
||||||
} while (func);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sprintf (strchr(msg,0),
|
|
||||||
"\n\tin statement begining at line %u of file \"%s\"",
|
|
||||||
lua_debugline, lua_filename());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lua_error (msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: do a string
|
|
||||||
*/
|
|
||||||
void lua_internaldostring (void)
|
|
||||||
{
|
|
||||||
lua_Object obj = lua_getparam (1);
|
|
||||||
if (lua_isstring(obj) && !lua_dostring(lua_getstring(obj)))
|
|
||||||
lua_pushnumber(1);
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: do a file
|
|
||||||
*/
|
|
||||||
void lua_internaldofile (void)
|
|
||||||
{
|
|
||||||
lua_Object obj = lua_getparam (1);
|
|
||||||
if (lua_isstring(obj) && !lua_dofile(lua_getstring(obj)))
|
|
||||||
lua_pushnumber(1);
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: print object values
|
|
||||||
*/
|
|
||||||
void lua_print (void)
|
|
||||||
{
|
|
||||||
int i=1;
|
|
||||||
lua_Object obj;
|
|
||||||
while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
|
|
||||||
{
|
|
||||||
if (lua_isnumber(obj)) printf("%g\n",lua_getnumber(obj));
|
|
||||||
else if (lua_isstring(obj)) printf("%s\n",lua_getstring(obj));
|
|
||||||
else if (lua_isfunction(obj)) printf("function: %p\n",bvalue(luaI_Address(obj)));
|
|
||||||
else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
|
|
||||||
);
|
|
||||||
else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata(obj));
|
|
||||||
else if (lua_istable(obj)) printf("table: %p\n",avalue(luaI_Address(obj)));
|
|
||||||
else if (lua_isnil(obj)) printf("nil\n");
|
|
||||||
else printf("invalid value to print\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: return an object type.
|
|
||||||
*/
|
|
||||||
void luaI_type (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error("no parameter to function 'type'");
|
|
||||||
switch (lua_type(o))
|
|
||||||
{
|
|
||||||
case LUA_T_NIL :
|
|
||||||
lua_pushliteral("nil");
|
|
||||||
break;
|
|
||||||
case LUA_T_NUMBER :
|
|
||||||
lua_pushliteral("number");
|
|
||||||
break;
|
|
||||||
case LUA_T_STRING :
|
|
||||||
lua_pushliteral("string");
|
|
||||||
break;
|
|
||||||
case LUA_T_ARRAY :
|
|
||||||
lua_pushliteral("table");
|
|
||||||
break;
|
|
||||||
case LUA_T_FUNCTION :
|
|
||||||
lua_pushliteral("function");
|
|
||||||
break;
|
|
||||||
case LUA_T_CFUNCTION :
|
|
||||||
lua_pushliteral("cfunction");
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
lua_pushliteral("userdata");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: convert an object to a number
|
|
||||||
*/
|
|
||||||
void lua_obj2number (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
if (lua_isnumber(o))
|
|
||||||
lua_pushobject(o);
|
|
||||||
else if (lua_isstring(o))
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
float f;
|
|
||||||
if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
|
|
||||||
lua_pushnumber(f);
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void luaI_error (void)
|
|
||||||
{
|
|
||||||
char *s = lua_getstring(lua_getparam(1));
|
|
||||||
if (s == NULL) s = "(no message)";
|
|
||||||
lua_reportbug(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
30
inout.h
30
inout.h
@@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
** $Id: inout.h,v 1.6 1994/11/21 21:41:09 roberto Stab roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef inout_h
|
|
||||||
#define inout_h
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
extern Word lua_linenumber;
|
|
||||||
extern Bool lua_debug;
|
|
||||||
extern Word lua_debugline;
|
|
||||||
|
|
||||||
char *lua_openfile (char *fn);
|
|
||||||
void lua_closefile (void);
|
|
||||||
char *lua_openstring (char *s);
|
|
||||||
void lua_closestring (void);
|
|
||||||
void lua_pushfunction (char *file, Word function);
|
|
||||||
void lua_popfunction (void);
|
|
||||||
void lua_reportbug (char *s);
|
|
||||||
|
|
||||||
void lua_internaldofile (void);
|
|
||||||
void lua_internaldostring (void);
|
|
||||||
void lua_print (void);
|
|
||||||
void luaI_type (void);
|
|
||||||
void lua_obj2number (void);
|
|
||||||
void luaI_error (void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
615
iolib.c
615
iolib.c
@@ -1,615 +0,0 @@
|
|||||||
/*
|
|
||||||
** iolib.c
|
|
||||||
** Input/output library to LUA
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_iolib="$Id: iolib.c,v 1.20 1995/02/02 18:54:58 roberto Exp roberto $";
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lualib.h"
|
|
||||||
|
|
||||||
static FILE *in=stdin, *out=stdout;
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open a file to read.
|
|
||||||
** LUA interface:
|
|
||||||
** status = readfrom (filename)
|
|
||||||
** where:
|
|
||||||
** status = 1 -> success
|
|
||||||
** status = 0 -> error
|
|
||||||
*/
|
|
||||||
static void io_readfrom (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT) /* restore standart input */
|
|
||||||
{
|
|
||||||
if (in != stdin)
|
|
||||||
{
|
|
||||||
fclose (in);
|
|
||||||
in = stdin;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'readfrom`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
FILE *fp = fopen (lua_getstring(o),"r");
|
|
||||||
if (fp == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (in != stdin) fclose (in);
|
|
||||||
in = fp;
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open a file to write.
|
|
||||||
** LUA interface:
|
|
||||||
** status = writeto (filename)
|
|
||||||
** where:
|
|
||||||
** status = 1 -> success
|
|
||||||
** status = 0 -> error
|
|
||||||
*/
|
|
||||||
static void io_writeto (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT) /* restore standart output */
|
|
||||||
{
|
|
||||||
if (out != stdout)
|
|
||||||
{
|
|
||||||
fclose (out);
|
|
||||||
out = stdout;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'writeto`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
FILE *fp = fopen (lua_getstring(o),"w");
|
|
||||||
if (fp == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (out != stdout) fclose (out);
|
|
||||||
out = fp;
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open a file to write appended.
|
|
||||||
** LUA interface:
|
|
||||||
** status = appendto (filename)
|
|
||||||
** where:
|
|
||||||
** status = 2 -> success (already exist)
|
|
||||||
** status = 1 -> success (new file)
|
|
||||||
** status = 0 -> error
|
|
||||||
*/
|
|
||||||
static void io_appendto (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT) /* restore standart output */
|
|
||||||
{
|
|
||||||
if (out != stdout)
|
|
||||||
{
|
|
||||||
fclose (out);
|
|
||||||
out = stdout;
|
|
||||||
}
|
|
||||||
lua_pushnumber (1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (!lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'appendto`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int r;
|
|
||||||
FILE *fp;
|
|
||||||
struct stat st;
|
|
||||||
if (stat(lua_getstring(o), &st) == -1) r = 1;
|
|
||||||
else r = 2;
|
|
||||||
fp = fopen (lua_getstring(o),"a");
|
|
||||||
if (fp == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (out != stdout) fclose (out);
|
|
||||||
out = fp;
|
|
||||||
lua_pushnumber (r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Read a variable. On error put nil on stack.
|
|
||||||
** LUA interface:
|
|
||||||
** variable = read ([format])
|
|
||||||
**
|
|
||||||
** O formato pode ter um dos seguintes especificadores:
|
|
||||||
**
|
|
||||||
** s ou S -> para string
|
|
||||||
** f ou F, g ou G, e ou E -> para reais
|
|
||||||
** i ou I -> para inteiros
|
|
||||||
**
|
|
||||||
** Estes especificadores podem vir seguidos de numero que representa
|
|
||||||
** o numero de campos a serem lidos.
|
|
||||||
*/
|
|
||||||
static void io_read (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT || !lua_isstring(o)) /* free format */
|
|
||||||
{
|
|
||||||
int c;
|
|
||||||
char s[256];
|
|
||||||
while (isspace(c=fgetc(in)))
|
|
||||||
;
|
|
||||||
if (c == '\"')
|
|
||||||
{
|
|
||||||
int n=0;
|
|
||||||
while((c = fgetc(in)) != '\"')
|
|
||||||
{
|
|
||||||
if (c == EOF)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s[n++] = c;
|
|
||||||
}
|
|
||||||
s[n] = 0;
|
|
||||||
}
|
|
||||||
else if (c == '\'')
|
|
||||||
{
|
|
||||||
int n=0;
|
|
||||||
while((c = fgetc(in)) != '\'')
|
|
||||||
{
|
|
||||||
if (c == EOF)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
s[n++] = c;
|
|
||||||
}
|
|
||||||
s[n] = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
ungetc (c, in);
|
|
||||||
if (fscanf (in, "%s", s) != 1)
|
|
||||||
{
|
|
||||||
lua_pushnil ();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (sscanf(s, "%lf %*c", &d) == 1)
|
|
||||||
{
|
|
||||||
lua_pushnumber (d);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lua_pushstring (s);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else /* formatted */
|
|
||||||
{
|
|
||||||
char *e = lua_getstring(o);
|
|
||||||
char t;
|
|
||||||
int m=0;
|
|
||||||
while (isspace(*e)) e++;
|
|
||||||
t = *e++;
|
|
||||||
while (isdigit(*e))
|
|
||||||
m = m*10 + (*e++ - '0');
|
|
||||||
|
|
||||||
if (m > 0)
|
|
||||||
{
|
|
||||||
char f[80];
|
|
||||||
char s[256];
|
|
||||||
sprintf (f, "%%%ds", m);
|
|
||||||
if (fgets (s, m, in) == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnil();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (s[strlen(s)-1] == '\n')
|
|
||||||
s[strlen(s)-1] = 0;
|
|
||||||
}
|
|
||||||
switch (tolower(t))
|
|
||||||
{
|
|
||||||
case 'i':
|
|
||||||
{
|
|
||||||
long int l;
|
|
||||||
sscanf (s, "%ld", &l);
|
|
||||||
lua_pushnumber(l);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'f': case 'g': case 'e':
|
|
||||||
{
|
|
||||||
float fl;
|
|
||||||
sscanf (s, "%f", &fl);
|
|
||||||
lua_pushnumber(fl);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
lua_pushstring(s);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
switch (tolower(t))
|
|
||||||
{
|
|
||||||
case 'i':
|
|
||||||
{
|
|
||||||
long int l;
|
|
||||||
if (fscanf (in, "%ld", &l) == EOF)
|
|
||||||
lua_pushnil();
|
|
||||||
else lua_pushnumber(l);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'f': case 'g': case 'e':
|
|
||||||
{
|
|
||||||
float f;
|
|
||||||
if (fscanf (in, "%f", &f) == EOF)
|
|
||||||
lua_pushnil();
|
|
||||||
else lua_pushnumber(f);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
char s[256];
|
|
||||||
if (fscanf (in, "%s", s) == EOF)
|
|
||||||
lua_pushnil();
|
|
||||||
else lua_pushstring(s);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Read characters until a given one. The delimiter is not read.
|
|
||||||
*/
|
|
||||||
static void io_readuntil (void)
|
|
||||||
{
|
|
||||||
int n=255,m=0;
|
|
||||||
int c,d;
|
|
||||||
char *s;
|
|
||||||
lua_Object lo = lua_getparam(1);
|
|
||||||
if (!lua_isstring(lo))
|
|
||||||
d = EOF;
|
|
||||||
else
|
|
||||||
d = *lua_getstring(lo);
|
|
||||||
|
|
||||||
s = (char *)malloc(n+1);
|
|
||||||
while((c = fgetc(in)) != EOF && c != d)
|
|
||||||
{
|
|
||||||
if (m==n)
|
|
||||||
{
|
|
||||||
n *= 2;
|
|
||||||
s = (char *)realloc(s, n+1);
|
|
||||||
}
|
|
||||||
s[m++] = c;
|
|
||||||
}
|
|
||||||
if (c != EOF) ungetc(c,in);
|
|
||||||
s[m] = 0;
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Write a variable. On error put 0 on stack, otherwise put 1.
|
|
||||||
** LUA interface:
|
|
||||||
** status = write (variable [,format])
|
|
||||||
**
|
|
||||||
** O formato pode ter um dos seguintes especificadores:
|
|
||||||
**
|
|
||||||
** s ou S -> para string
|
|
||||||
** f ou F, g ou G, e ou E -> para reais
|
|
||||||
** i ou I -> para inteiros
|
|
||||||
**
|
|
||||||
** Estes especificadores podem vir seguidos de:
|
|
||||||
**
|
|
||||||
** [?][m][.n]
|
|
||||||
**
|
|
||||||
** onde:
|
|
||||||
** ? -> indica justificacao
|
|
||||||
** < = esquerda
|
|
||||||
** | = centro
|
|
||||||
** > = direita (default)
|
|
||||||
** m -> numero maximo de campos (se exceder estoura)
|
|
||||||
** n -> indica precisao para
|
|
||||||
** reais -> numero de casas decimais
|
|
||||||
** inteiros -> numero minimo de digitos
|
|
||||||
** string -> nao se aplica
|
|
||||||
*/
|
|
||||||
static char *buildformat (char *e, lua_Object o)
|
|
||||||
{
|
|
||||||
static char buffer[2048];
|
|
||||||
static char f[80];
|
|
||||||
char *string = &buffer[255];
|
|
||||||
char *fstart=e, *fspace, *send;
|
|
||||||
char t, j='r';
|
|
||||||
int m=0, n=-1, l;
|
|
||||||
while (isspace(*e)) e++;
|
|
||||||
fspace = e;
|
|
||||||
t = *e++;
|
|
||||||
if (*e == '<' || *e == '|' || *e == '>') j = *e++;
|
|
||||||
while (isdigit(*e))
|
|
||||||
m = m*10 + (*e++ - '0');
|
|
||||||
if (*e == '.') e++; /* skip point */
|
|
||||||
while (isdigit(*e))
|
|
||||||
if (n < 0) n = (*e++ - '0');
|
|
||||||
else n = n*10 + (*e++ - '0');
|
|
||||||
|
|
||||||
sprintf(f,"%%");
|
|
||||||
if (j == '<' || j == '|') sprintf(strchr(f,0),"-");
|
|
||||||
if (m > 0) sprintf(strchr(f,0),"%d", m);
|
|
||||||
if (n >= 0) sprintf(strchr(f,0),".%d", n);
|
|
||||||
switch (t)
|
|
||||||
{
|
|
||||||
case 'i': case 'I': t = 'd';
|
|
||||||
sprintf(strchr(f,0), "%c", t);
|
|
||||||
sprintf (string, f, (long int)lua_getnumber(o));
|
|
||||||
break;
|
|
||||||
case 'f': case 'g': case 'e': case 'G': case 'E':
|
|
||||||
sprintf(strchr(f,0), "%c", t);
|
|
||||||
sprintf (string, f, (float)lua_getnumber(o));
|
|
||||||
break;
|
|
||||||
case 'F': t = 'f';
|
|
||||||
sprintf(strchr(f,0), "%c", t);
|
|
||||||
sprintf (string, f, (float)lua_getnumber(o));
|
|
||||||
break;
|
|
||||||
case 's': case 'S': t = 's';
|
|
||||||
sprintf(strchr(f,0), "%c", t);
|
|
||||||
sprintf (string, f, lua_getstring(o));
|
|
||||||
break;
|
|
||||||
default: return "";
|
|
||||||
}
|
|
||||||
l = strlen(string);
|
|
||||||
send = string+l;
|
|
||||||
if (m!=0 && l>m)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for (i=0; i<m; i++)
|
|
||||||
string[i] = '*';
|
|
||||||
string[i] = 0;
|
|
||||||
}
|
|
||||||
else if (m!=0 && j=='|')
|
|
||||||
{
|
|
||||||
int k;
|
|
||||||
int i=l-1;
|
|
||||||
while (isspace(string[i]) || string[i]==0) i--;
|
|
||||||
string -= (m-i)/2;
|
|
||||||
for(k=0; k<(m-i)/2; k++)
|
|
||||||
string[k] = ' ';
|
|
||||||
}
|
|
||||||
/* add space characteres */
|
|
||||||
while (fspace != fstart)
|
|
||||||
{
|
|
||||||
string--;
|
|
||||||
fspace--;
|
|
||||||
*string = *fspace;
|
|
||||||
}
|
|
||||||
while (isspace(*e)) *send++ = *e++;
|
|
||||||
*send = 0;
|
|
||||||
return string;
|
|
||||||
}
|
|
||||||
static void io_write (void)
|
|
||||||
{
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
if (o1 == LUA_NOOBJECT) /* new line */
|
|
||||||
{
|
|
||||||
fprintf (out, "\n");
|
|
||||||
lua_pushnumber(1);
|
|
||||||
}
|
|
||||||
else if (o2 == LUA_NOOBJECT) /* free format */
|
|
||||||
{
|
|
||||||
int status=0;
|
|
||||||
if (lua_isnumber(o1))
|
|
||||||
status = fprintf (out, "%g", lua_getnumber(o1));
|
|
||||||
else if (lua_isstring(o1))
|
|
||||||
status = fprintf (out, "%s", lua_getstring(o1));
|
|
||||||
lua_pushnumber(status);
|
|
||||||
}
|
|
||||||
else /* formated */
|
|
||||||
{
|
|
||||||
if (!lua_isstring(o2))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect format to function `write'");
|
|
||||||
lua_pushnumber(0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
lua_pushnumber(fprintf (out, "%s", buildformat(lua_getstring(o2),o1)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Execute a executable program using "system".
|
|
||||||
** Return the result of execution.
|
|
||||||
*/
|
|
||||||
static void io_execute (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT || !lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int res = system(lua_getstring(o));
|
|
||||||
lua_pushnumber (res);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Remove a file.
|
|
||||||
** On error put 0 on stack, otherwise put 1.
|
|
||||||
*/
|
|
||||||
static void io_remove (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT || !lua_isstring (o))
|
|
||||||
{
|
|
||||||
lua_error ("incorrect argument to function 'execute`");
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (remove(lua_getstring(o)) == 0)
|
|
||||||
lua_pushnumber (1);
|
|
||||||
else
|
|
||||||
lua_pushnumber (0);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** To get a environment variables
|
|
||||||
*/
|
|
||||||
static void io_getenv (void)
|
|
||||||
{
|
|
||||||
lua_Object s = lua_getparam(1);
|
|
||||||
if (!lua_isstring(s))
|
|
||||||
lua_pushnil();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
char *env = getenv(lua_getstring(s));
|
|
||||||
if (env == NULL) lua_pushnil();
|
|
||||||
else lua_pushstring(env);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return time: hour, min, sec
|
|
||||||
*/
|
|
||||||
static void io_time (void)
|
|
||||||
{
|
|
||||||
time_t t;
|
|
||||||
struct tm *s;
|
|
||||||
|
|
||||||
time(&t);
|
|
||||||
s = localtime(&t);
|
|
||||||
lua_pushnumber(s->tm_hour);
|
|
||||||
lua_pushnumber(s->tm_min);
|
|
||||||
lua_pushnumber(s->tm_sec);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return date: dd, mm, yyyy
|
|
||||||
*/
|
|
||||||
static void io_date (void)
|
|
||||||
{
|
|
||||||
time_t t;
|
|
||||||
struct tm *s;
|
|
||||||
|
|
||||||
time(&t);
|
|
||||||
s = localtime(&t);
|
|
||||||
lua_pushnumber(s->tm_mday);
|
|
||||||
lua_pushnumber(s->tm_mon+1);
|
|
||||||
lua_pushnumber(s->tm_year+1900);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Beep
|
|
||||||
*/
|
|
||||||
static void io_beep (void)
|
|
||||||
{
|
|
||||||
printf("\a");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** To exit
|
|
||||||
*/
|
|
||||||
static void io_exit (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
if (lua_isstring(o))
|
|
||||||
printf("%s\n", lua_getstring(o));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** To debug a lua program. Start a dialog with the user, interpreting
|
|
||||||
lua commands until an 'cont'.
|
|
||||||
*/
|
|
||||||
static void io_debug (void)
|
|
||||||
{
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
char buffer[250];
|
|
||||||
fprintf(stderr, "lua_debug> ");
|
|
||||||
if (gets(buffer) == 0) return;
|
|
||||||
if (strcmp(buffer, "cont") == 0) return;
|
|
||||||
lua_dostring(buffer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open io library
|
|
||||||
*/
|
|
||||||
void iolib_open (void)
|
|
||||||
{
|
|
||||||
lua_register ("readfrom", io_readfrom);
|
|
||||||
lua_register ("writeto", io_writeto);
|
|
||||||
lua_register ("appendto", io_appendto);
|
|
||||||
lua_register ("read", io_read);
|
|
||||||
lua_register ("readuntil",io_readuntil);
|
|
||||||
lua_register ("write", io_write);
|
|
||||||
lua_register ("execute", io_execute);
|
|
||||||
lua_register ("remove", io_remove);
|
|
||||||
lua_register ("getenv", io_getenv);
|
|
||||||
lua_register ("time", io_time);
|
|
||||||
lua_register ("date", io_date);
|
|
||||||
lua_register ("beep", io_beep);
|
|
||||||
lua_register ("exit", io_exit);
|
|
||||||
lua_register ("debug", io_debug);
|
|
||||||
}
|
|
||||||
452
lapi.c
Normal file
452
lapi.c
Normal file
@@ -0,0 +1,452 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lapi.c,v 1.98 2000/09/14 14:09:31 roberto Exp roberto $
|
||||||
|
** Lua API
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lapi.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lgc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
#include "lvm.h"
|
||||||
|
|
||||||
|
|
||||||
|
const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n"
|
||||||
|
"$Authors: " LUA_AUTHORS " $";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define Index(L,i) ((i) >= 0 ? (L->Cbase+((i)-1)) : (L->top+(i)))
|
||||||
|
|
||||||
|
#define api_incr_top(L) incr_top
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TObject *luaA_index (lua_State *L, int index) {
|
||||||
|
return Index(L, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaA_pushobject (lua_State *L, const TObject *o) {
|
||||||
|
*L->top = *o;
|
||||||
|
incr_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_stackspace (lua_State *L) {
|
||||||
|
return (L->stack_last - L->top);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** basic stack manipulation
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
int lua_gettop (lua_State *L) {
|
||||||
|
return (L->top - L->Cbase);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_settop (lua_State *L, int index) {
|
||||||
|
if (index >= 0)
|
||||||
|
luaD_adjusttop(L, L->Cbase, index);
|
||||||
|
else
|
||||||
|
L->top = L->top+index+1; /* index is negative */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_remove (lua_State *L, int index) {
|
||||||
|
StkId p = Index(L, index);
|
||||||
|
while (++p < L->top) *(p-1) = *p;
|
||||||
|
L->top--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_insert (lua_State *L, int index) {
|
||||||
|
StkId p = Index(L, index);
|
||||||
|
StkId q;
|
||||||
|
for (q = L->top; q>p; q--)
|
||||||
|
*q = *(q-1);
|
||||||
|
*p = *L->top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushvalue (lua_State *L, int index) {
|
||||||
|
*L->top = *Index(L, index);
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** access functions (stack -> C)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define btest(L,i,value,default) { \
|
||||||
|
StkId o; \
|
||||||
|
if ((i) >= 0) { \
|
||||||
|
o = L->Cbase+((i)-1); \
|
||||||
|
if (o >= L->top) return (default); \
|
||||||
|
} \
|
||||||
|
else o = L->top+(i); \
|
||||||
|
return (value); }
|
||||||
|
|
||||||
|
|
||||||
|
#define access(L,i,test,default,value) { \
|
||||||
|
StkId o; \
|
||||||
|
if ((i) >= 0) { \
|
||||||
|
o = L->Cbase+((i)-1); \
|
||||||
|
if (o >= L->top) return (default); \
|
||||||
|
} \
|
||||||
|
else o = L->top+(i); \
|
||||||
|
return ((test) ? (value) : (default)); }
|
||||||
|
|
||||||
|
|
||||||
|
const char *lua_type (lua_State *L, int index) {
|
||||||
|
btest(L, index, luaO_typename(o), "NO VALUE");
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_iscfunction (lua_State *L, int index) {
|
||||||
|
btest(L, index, (ttype(o) == TAG_CCLOSURE), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_isnumber (lua_State *L, int index) {
|
||||||
|
btest(L, index, (tonumber(Index(L, index)) == 0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_tag (lua_State *L, int index) {
|
||||||
|
btest(L, index,
|
||||||
|
((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag :
|
||||||
|
luaT_effectivetag(L, o)), -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_equal (lua_State *L, int index1, int index2) {
|
||||||
|
StkId o1 = Index(L, index1);
|
||||||
|
StkId o2 = Index(L, index2);
|
||||||
|
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
||||||
|
else return luaO_equalObj(o1, o2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int lua_lessthan (lua_State *L, int index1, int index2) {
|
||||||
|
StkId o1 = Index(L, index1);
|
||||||
|
StkId o2 = Index(L, index2);
|
||||||
|
if (o1 >= L->top || o2 >= L->top) return 0; /* index out-of-range */
|
||||||
|
else return luaV_lessthan(L, o1, o2, L->top);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
double lua_tonumber (lua_State *L, int index) {
|
||||||
|
access(L, index, (tonumber(o) == 0), 0.0, nvalue(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *lua_tostring (lua_State *L, int index) {
|
||||||
|
luaC_checkGC(L); /* `tostring' may create a new string */
|
||||||
|
access(L, index, (tostring(L, o) == 0), NULL, svalue(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t lua_strlen (lua_State *L, int index) {
|
||||||
|
access(L, index, (tostring(L, o) == 0), 0, tsvalue(o)->u.s.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_CFunction lua_tocfunction (lua_State *L, int index) {
|
||||||
|
access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *lua_touserdata (lua_State *L, int index) {
|
||||||
|
access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const void *lua_topointer (lua_State *L, int index) {
|
||||||
|
StkId o = Index(L, index);
|
||||||
|
switch (ttype(o)) {
|
||||||
|
case TAG_TABLE:
|
||||||
|
return hvalue(o);
|
||||||
|
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||||
|
return clvalue(o);
|
||||||
|
default: return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** push functions (C -> stack)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushnil (lua_State *L) {
|
||||||
|
ttype(L->top) = TAG_NIL;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushnumber (lua_State *L, double n) {
|
||||||
|
ttype(L->top) = TAG_NUMBER;
|
||||||
|
nvalue(L->top) = n;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushlstring (lua_State *L, const char *s, size_t len) {
|
||||||
|
luaC_checkGC(L);
|
||||||
|
tsvalue(L->top) = luaS_newlstr(L, s, len);
|
||||||
|
ttype(L->top) = TAG_STRING;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushstring (lua_State *L, const char *s) {
|
||||||
|
if (s == NULL)
|
||||||
|
lua_pushnil(L);
|
||||||
|
else
|
||||||
|
lua_pushlstring(L, s, strlen(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
|
||||||
|
luaC_checkGC(L);
|
||||||
|
luaV_Cclosure(L, fn, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
|
||||||
|
luaC_checkGC(L);
|
||||||
|
if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
|
||||||
|
luaO_verror(L, "invalid tag for a userdata (%d)", tag);
|
||||||
|
tsvalue(L->top) = luaS_createudata(L, u, tag);
|
||||||
|
ttype(L->top) = TAG_USERDATA;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get functions (Lua -> stack)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
void lua_getglobal (lua_State *L, const char *name) {
|
||||||
|
StkId top = L->top;
|
||||||
|
*top = *luaV_getglobal(L, luaS_new(L, name));
|
||||||
|
L->top = top;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_gettable (lua_State *L, int index) {
|
||||||
|
StkId t = Index(L, index);
|
||||||
|
StkId top = L->top;
|
||||||
|
*(top-1) = *luaV_gettable(L, t);
|
||||||
|
L->top = top; /* tag method may change top */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_rawget (lua_State *L, int index) {
|
||||||
|
StkId t = Index(L, index);
|
||||||
|
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||||
|
*(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_rawgeti (lua_State *L, int index, int n) {
|
||||||
|
StkId o = Index(L, index);
|
||||||
|
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
|
||||||
|
*L->top = *luaH_getnum(hvalue(o), n);
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_getglobals (lua_State *L) {
|
||||||
|
hvalue(L->top) = L->gt;
|
||||||
|
ttype(L->top) = TAG_TABLE;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_getref (lua_State *L, int ref) {
|
||||||
|
if (ref == LUA_REFNIL)
|
||||||
|
ttype(L->top) = TAG_NIL;
|
||||||
|
else if (0 <= ref && ref < L->refSize &&
|
||||||
|
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD))
|
||||||
|
*L->top = L->refArray[ref].o;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
api_incr_top(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_newtable (lua_State *L) {
|
||||||
|
luaC_checkGC(L);
|
||||||
|
hvalue(L->top) = luaH_new(L, 0);
|
||||||
|
ttype(L->top) = TAG_TABLE;
|
||||||
|
api_incr_top(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** set functions (stack -> Lua)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
void lua_setglobal (lua_State *L, const char *name) {
|
||||||
|
StkId top = L->top;
|
||||||
|
luaV_setglobal(L, luaS_new(L, name));
|
||||||
|
L->top = top-1; /* remove element from the top */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_settable (lua_State *L, int index) {
|
||||||
|
StkId t = Index(L, index);
|
||||||
|
StkId top = L->top;
|
||||||
|
luaV_settable(L, t, top-2);
|
||||||
|
L->top = top-2; /* pop index and value */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_rawset (lua_State *L, int index) {
|
||||||
|
StkId t = Index(L, index);
|
||||||
|
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||||
|
*luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
|
||||||
|
L->top -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_rawseti (lua_State *L, int index, int n) {
|
||||||
|
StkId o = Index(L, index);
|
||||||
|
LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected");
|
||||||
|
*luaH_setint(L, hvalue(o), n) = *(L->top-1);
|
||||||
|
L->top--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_setglobals (lua_State *L) {
|
||||||
|
StkId newtable = --L->top;
|
||||||
|
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected");
|
||||||
|
L->gt = hvalue(newtable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_ref (lua_State *L, int lock) {
|
||||||
|
int ref;
|
||||||
|
if (ttype(L->top-1) == TAG_NIL)
|
||||||
|
ref = LUA_REFNIL;
|
||||||
|
else {
|
||||||
|
if (L->refFree != NONEXT) { /* is there a free place? */
|
||||||
|
ref = L->refFree;
|
||||||
|
L->refFree = L->refArray[ref].st;
|
||||||
|
}
|
||||||
|
else { /* no more free places */
|
||||||
|
luaM_growvector(L, L->refArray, L->refSize, 1, struct Ref,
|
||||||
|
"reference table overflow", MAX_INT);
|
||||||
|
ref = L->refSize++;
|
||||||
|
}
|
||||||
|
L->refArray[ref].o = *(L->top-1);
|
||||||
|
L->refArray[ref].st = lock ? LOCK : HOLD;
|
||||||
|
}
|
||||||
|
L->top--;
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** "do" functions (run Lua code)
|
||||||
|
** (most of them are in ldo.c)
|
||||||
|
*/
|
||||||
|
|
||||||
|
void lua_rawcall (lua_State *L, int nargs, int nresults) {
|
||||||
|
luaD_call(L, L->top-(nargs+1), nresults);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** miscellaneous functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
void lua_settag (lua_State *L, int tag) {
|
||||||
|
luaT_realtag(L, tag);
|
||||||
|
switch (ttype(L->top-1)) {
|
||||||
|
case TAG_TABLE:
|
||||||
|
hvalue(L->top-1)->htag = tag;
|
||||||
|
break;
|
||||||
|
case TAG_USERDATA:
|
||||||
|
tsvalue(L->top-1)->u.d.tag = tag;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
luaO_verror(L, "cannot change the tag of a %.20s",
|
||||||
|
luaO_typename(L->top-1));
|
||||||
|
}
|
||||||
|
L->top--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_unref (lua_State *L, int ref) {
|
||||||
|
if (ref >= 0) {
|
||||||
|
LUA_ASSERT(ref < L->refSize && L->refArray[ref].st < 0, "invalid ref");
|
||||||
|
L->refArray[ref].st = L->refFree;
|
||||||
|
L->refFree = ref;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_next (lua_State *L, int index) {
|
||||||
|
StkId t = Index(L, index);
|
||||||
|
Node *n;
|
||||||
|
LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected");
|
||||||
|
n = luaH_next(L, hvalue(t), Index(L, -1));
|
||||||
|
if (n) {
|
||||||
|
*(L->top-1) = *key(n);
|
||||||
|
*L->top = *val(n);
|
||||||
|
api_incr_top(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else { /* no more elements */
|
||||||
|
L->top -= 1; /* remove key */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_getn (lua_State *L, int index) {
|
||||||
|
Hash *h = hvalue(Index(L, index));
|
||||||
|
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */
|
||||||
|
if (ttype(value) == TAG_NUMBER)
|
||||||
|
return (int)nvalue(value);
|
||||||
|
else {
|
||||||
|
Number max = 0;
|
||||||
|
int i = h->size;
|
||||||
|
Node *n = h->node;
|
||||||
|
while (i--) {
|
||||||
|
if (ttype(key(n)) == TAG_NUMBER &&
|
||||||
|
ttype(val(n)) != TAG_NIL &&
|
||||||
|
nvalue(key(n)) > max)
|
||||||
|
max = nvalue(key(n));
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return (int)max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_concat (lua_State *L, int n) {
|
||||||
|
StkId top = L->top;
|
||||||
|
luaV_strconc(L, n, top);
|
||||||
|
L->top = top-(n-1);
|
||||||
|
}
|
||||||
|
|
||||||
17
lapi.h
Normal file
17
lapi.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lapi.h,v 1.19 2000/08/28 17:57:04 roberto Exp roberto $
|
||||||
|
** Auxiliary functions from Lua API
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lapi_h
|
||||||
|
#define lapi_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
|
TObject *luaA_index (lua_State *L, int index);
|
||||||
|
void luaA_pushobject (lua_State *L, const TObject *o);
|
||||||
|
|
||||||
|
#endif
|
||||||
219
lauxlib.c
Normal file
219
lauxlib.c
Normal file
@@ -0,0 +1,219 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lauxlib.c,v 1.35 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
** Auxiliary functions for building Lua libraries
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/* This file uses only the official API of Lua.
|
||||||
|
** Any function declared here could be written as an application function.
|
||||||
|
** With care, these functions can be used by other libraries.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int luaL_findstring (const char *name, const char *const list[]) {
|
||||||
|
int i;
|
||||||
|
for (i=0; list[i]; i++)
|
||||||
|
if (strcmp(list[i], name) == 0)
|
||||||
|
return i;
|
||||||
|
return -1; /* name not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaL_argerror (lua_State *L, int narg, const char *extramsg) {
|
||||||
|
lua_Debug ar;
|
||||||
|
lua_getstack(L, 0, &ar);
|
||||||
|
lua_getinfo(L, "n", &ar);
|
||||||
|
if (ar.name == NULL)
|
||||||
|
ar.name = "?";
|
||||||
|
luaL_verror(L, "bad argument #%d to `%.50s' (%.100s)",
|
||||||
|
narg, ar.name, extramsg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void type_error (lua_State *L, int narg, const char *type_name) {
|
||||||
|
char buff[100];
|
||||||
|
const char *rt = lua_type(L, narg);
|
||||||
|
if (*rt == 'N') rt = "no value";
|
||||||
|
sprintf(buff, "%.10s expected, got %.10s", type_name, rt);
|
||||||
|
luaL_argerror(L, narg, buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_checkstack (lua_State *L, int space, const char *mes) {
|
||||||
|
if (space > lua_stackspace(L))
|
||||||
|
luaL_verror(L, "stack overflow (%.30s)", mes);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** use the 3rd letter of type names for testing:
|
||||||
|
** nuMber, niL, stRing, fuNction, usErdata, taBle, anY
|
||||||
|
*/
|
||||||
|
void luaL_checktype(lua_State *L, int narg, const char *tname) {
|
||||||
|
const char *rt = lua_type(L, narg);
|
||||||
|
if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2])))
|
||||||
|
type_error(L, narg, tname);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *checkstr (lua_State *L, int narg, size_t *len) {
|
||||||
|
const char *s = lua_tostring(L, narg);
|
||||||
|
if (!s) type_error(L, narg, "string");
|
||||||
|
if (len) *len = lua_strlen(L, narg);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
|
||||||
|
return checkstr(L, narg, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *luaL_opt_lstr (lua_State *L, int narg, const char *def,
|
||||||
|
size_t *len) {
|
||||||
|
if (lua_isnull(L, narg)) {
|
||||||
|
if (len) *len = def ? strlen(def) : 0;
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
else return checkstr(L, narg, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
double luaL_check_number (lua_State *L, int narg) {
|
||||||
|
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
|
||||||
|
return lua_tonumber(L, narg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
double luaL_opt_number (lua_State *L, int narg, double def) {
|
||||||
|
if (lua_isnull(L, narg)) return def;
|
||||||
|
else {
|
||||||
|
if (!lua_isnumber(L, narg)) type_error(L, narg, "number");
|
||||||
|
return lua_tonumber(L, narg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
lua_register(L, l[i].name, l[i].func);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_verror (lua_State *L, const char *fmt, ...) {
|
||||||
|
char buff[500];
|
||||||
|
va_list argp;
|
||||||
|
va_start(argp, fmt);
|
||||||
|
vsprintf(buff, fmt, argp);
|
||||||
|
va_end(argp);
|
||||||
|
lua_error(L, buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Generic Buffer manipulation
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define buffempty(B) ((B)->p == (B)->buffer)
|
||||||
|
#define bufflen(B) ((B)->p - (B)->buffer)
|
||||||
|
#define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
|
||||||
|
|
||||||
|
#define LIMIT (LUA_MINSTACK/2)
|
||||||
|
|
||||||
|
|
||||||
|
static int emptybuffer (luaL_Buffer *B) {
|
||||||
|
size_t l = bufflen(B);
|
||||||
|
if (l == 0) return 0; /* put nothing on stack */
|
||||||
|
else {
|
||||||
|
lua_pushlstring(B->L, B->buffer, l);
|
||||||
|
B->p = B->buffer;
|
||||||
|
B->level++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void adjuststack (luaL_Buffer *B) {
|
||||||
|
if (B->level > 1) {
|
||||||
|
lua_State *L = B->L;
|
||||||
|
int toget = 1; /* number of levels to concat */
|
||||||
|
size_t toplen = lua_strlen(L, -1);
|
||||||
|
do {
|
||||||
|
size_t l = lua_strlen(L, -(toget+1));
|
||||||
|
if (B->level - toget + 1 >= LIMIT || toplen > l) {
|
||||||
|
toplen += l;
|
||||||
|
toget++;
|
||||||
|
}
|
||||||
|
else break;
|
||||||
|
} while (toget < B->level);
|
||||||
|
if (toget >= 2) {
|
||||||
|
lua_concat(L, toget);
|
||||||
|
B->level = B->level - toget + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *luaL_prepbuffer (luaL_Buffer *B) {
|
||||||
|
if (emptybuffer(B))
|
||||||
|
adjuststack(B);
|
||||||
|
return B->buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
|
||||||
|
while (l--)
|
||||||
|
luaL_putchar(B, *s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_addstring (luaL_Buffer *B, const char *s) {
|
||||||
|
luaL_addlstring(B, s, strlen(s));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_pushresult (luaL_Buffer *B) {
|
||||||
|
emptybuffer(B);
|
||||||
|
if (B->level == 0)
|
||||||
|
lua_pushlstring(B->L, NULL, 0);
|
||||||
|
else if (B->level > 1)
|
||||||
|
lua_concat(B->L, B->level);
|
||||||
|
B->level = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_addvalue (luaL_Buffer *B) {
|
||||||
|
lua_State *L = B->L;
|
||||||
|
size_t vl = lua_strlen(L, -1);
|
||||||
|
if (vl <= bufffree(B)) { /* fit into buffer? */
|
||||||
|
memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
|
||||||
|
B->p += vl;
|
||||||
|
lua_pop(L, 1); /* remove from stack */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (emptybuffer(B))
|
||||||
|
lua_insert(L, -2); /* put buffer before new value */
|
||||||
|
B->level++; /* add new value into B stack */
|
||||||
|
adjuststack(B);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
|
||||||
|
B->L = L;
|
||||||
|
B->p = B->buffer;
|
||||||
|
B->level = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
93
lauxlib.h
Normal file
93
lauxlib.h
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lauxlib.h,v 1.24 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
** Auxiliary functions for building Lua libraries
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef lauxlib_h
|
||||||
|
#define lauxlib_h
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
struct luaL_reg {
|
||||||
|
const char *name;
|
||||||
|
lua_CFunction func;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n);
|
||||||
|
void luaL_argerror (lua_State *L, int numarg, const char *extramsg);
|
||||||
|
const char *luaL_check_lstr (lua_State *L, int numArg, size_t *len);
|
||||||
|
const char *luaL_opt_lstr (lua_State *L, int numArg, const char *def,
|
||||||
|
size_t *len);
|
||||||
|
double luaL_check_number (lua_State *L, int numArg);
|
||||||
|
double luaL_opt_number (lua_State *L, int numArg, double def);
|
||||||
|
|
||||||
|
void luaL_checkstack (lua_State *L, int space, const char *msg);
|
||||||
|
void luaL_checktype (lua_State *L, int narg, const char *tname);
|
||||||
|
|
||||||
|
void luaL_verror (lua_State *L, const char *fmt, ...);
|
||||||
|
int luaL_findstring (const char *name, const char *const list[]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** ===============================================================
|
||||||
|
** some useful macros
|
||||||
|
** ===============================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define luaL_arg_check(L, cond,numarg,extramsg) if (!(cond)) \
|
||||||
|
luaL_argerror(L, numarg,extramsg)
|
||||||
|
#define luaL_check_string(L,n) (luaL_check_lstr(L, (n), NULL))
|
||||||
|
#define luaL_opt_string(L,n,d) (luaL_opt_lstr(L, (n), (d), NULL))
|
||||||
|
#define luaL_check_int(L,n) ((int)luaL_check_number(L, n))
|
||||||
|
#define luaL_check_long(L,n) ((long)luaL_check_number(L, n))
|
||||||
|
#define luaL_opt_int(L,n,d) ((int)luaL_opt_number(L, n,d))
|
||||||
|
#define luaL_opt_long(L,n,d) ((long)luaL_opt_number(L, n,d))
|
||||||
|
#define luaL_openl(L,a) luaL_openlib(L, a, (sizeof(a)/sizeof(a[0])))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Generic Buffer manipulation
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define LUAL_BUFFERSIZE BUFSIZ
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct luaL_Buffer {
|
||||||
|
char *p; /* current position in buffer */
|
||||||
|
int level;
|
||||||
|
lua_State *L;
|
||||||
|
char buffer[LUAL_BUFFERSIZE];
|
||||||
|
} luaL_Buffer;
|
||||||
|
|
||||||
|
#define luaL_putchar(B,c) \
|
||||||
|
((void)((B)->p < &(B)->buffer[LUAL_BUFFERSIZE] || luaL_prepbuffer(B)), \
|
||||||
|
(*(B)->p++ = (char)(c)))
|
||||||
|
|
||||||
|
#define luaL_addsize(B,n) ((B)->p += (n))
|
||||||
|
|
||||||
|
void luaL_buffinit (lua_State *L, luaL_Buffer *B);
|
||||||
|
char *luaL_prepbuffer (luaL_Buffer *B);
|
||||||
|
void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
|
||||||
|
void luaL_addstring (luaL_Buffer *B, const char *s);
|
||||||
|
void luaL_addvalue (luaL_Buffer *B);
|
||||||
|
void luaL_pushresult (luaL_Buffer *B);
|
||||||
|
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
636
lbaselib.c
Normal file
636
lbaselib.c
Normal file
@@ -0,0 +1,636 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lbaselib.c,v 1.5 2000/09/14 14:09:31 roberto Exp roberto $
|
||||||
|
** Basic library
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If your system does not support `stderr', redefine this function, or
|
||||||
|
** redefine _ERRORMESSAGE so that it won't need _ALERT.
|
||||||
|
*/
|
||||||
|
static int luaB__ALERT (lua_State *L) {
|
||||||
|
fputs(luaL_check_string(L, 1), stderr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Basic implementation of _ERRORMESSAGE.
|
||||||
|
** The library `liolib' redefines _ERRORMESSAGE for better error information.
|
||||||
|
*/
|
||||||
|
static int luaB__ERRORMESSAGE (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "string");
|
||||||
|
lua_getglobal(L, LUA_ALERT);
|
||||||
|
if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */
|
||||||
|
lua_Debug ar;
|
||||||
|
lua_pushstring(L, "error: ");
|
||||||
|
lua_pushvalue(L, 1);
|
||||||
|
if (lua_getstack(L, 1, &ar)) {
|
||||||
|
lua_getinfo(L, "Sl", &ar);
|
||||||
|
if (ar.source && ar.currentline > 0) {
|
||||||
|
char buff[100];
|
||||||
|
sprintf(buff, "\n <%.70s: line %d>", ar.short_src, ar.currentline);
|
||||||
|
lua_pushstring(L, buff);
|
||||||
|
lua_concat(L, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushstring(L, "\n");
|
||||||
|
lua_concat(L, 3);
|
||||||
|
lua_rawcall(L, 1, 0);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If your system does not support `stdout', you can just remove this function.
|
||||||
|
** If you need, you can define your own `print' function, following this
|
||||||
|
** model but changing `fputs' to put the strings at a proper place
|
||||||
|
** (a console window or a log file, for instance).
|
||||||
|
*/
|
||||||
|
static int luaB_print (lua_State *L) {
|
||||||
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
|
int i;
|
||||||
|
lua_getglobal(L, "tostring");
|
||||||
|
for (i=1; i<=n; i++) {
|
||||||
|
const char *s;
|
||||||
|
lua_pushvalue(L, -1); /* function to be called */
|
||||||
|
lua_pushvalue(L, i); /* value to print */
|
||||||
|
lua_rawcall(L, 1, 1);
|
||||||
|
s = lua_tostring(L, -1); /* get result */
|
||||||
|
if (s == NULL)
|
||||||
|
lua_error(L, "`tostring' must return a string to `print'");
|
||||||
|
if (i>1) fputs("\t", stdout);
|
||||||
|
fputs(s, stdout);
|
||||||
|
lua_pop(L, 1); /* pop result */
|
||||||
|
}
|
||||||
|
fputs("\n", stdout);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_tonumber (lua_State *L) {
|
||||||
|
int base = luaL_opt_int(L, 2, 10);
|
||||||
|
if (base == 10) { /* standard conversion */
|
||||||
|
luaL_checktype(L, 1, "any");
|
||||||
|
if (lua_isnumber(L, 1)) {
|
||||||
|
lua_pushnumber(L, lua_tonumber(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const char *s1 = luaL_check_string(L, 1);
|
||||||
|
char *s2;
|
||||||
|
unsigned long n;
|
||||||
|
luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range");
|
||||||
|
n = strtoul(s1, &s2, base);
|
||||||
|
if (s1 != s2) { /* at least one valid digit? */
|
||||||
|
while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
|
||||||
|
if (*s2 == '\0') { /* no invalid trailing characters? */
|
||||||
|
lua_pushnumber(L, n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushnil(L); /* else not a number */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_error (lua_State *L) {
|
||||||
|
lua_error(L, luaL_opt_string(L, 1, NULL));
|
||||||
|
return 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_setglobal (lua_State *L) {
|
||||||
|
luaL_checktype(L, 2, "any");
|
||||||
|
lua_setglobal(L, luaL_check_string(L, 1));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_getglobal (lua_State *L) {
|
||||||
|
lua_getglobal(L, luaL_check_string(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_tag (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "any");
|
||||||
|
lua_pushnumber(L, lua_tag(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_settag (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
lua_pushvalue(L, 1); /* push table */
|
||||||
|
lua_settag(L, luaL_check_int(L, 2));
|
||||||
|
lua_pop(L, 1); /* remove second argument */
|
||||||
|
return 1; /* return first argument */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_newtag (lua_State *L) {
|
||||||
|
lua_pushnumber(L, lua_newtag(L));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_copytagmethods (lua_State *L) {
|
||||||
|
lua_pushnumber(L, lua_copytagmethods(L, luaL_check_int(L, 1),
|
||||||
|
luaL_check_int(L, 2)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_globals (lua_State *L) {
|
||||||
|
lua_getglobals(L); /* value to be returned */
|
||||||
|
if (!lua_isnull(L, 1)) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
lua_pushvalue(L, 1); /* new table of globals */
|
||||||
|
lua_setglobals(L);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_rawget (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
luaL_checktype(L, 2, "any");
|
||||||
|
lua_rawget(L, -2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_rawset (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
luaL_checktype(L, 2, "any");
|
||||||
|
luaL_checktype(L, 3, "any");
|
||||||
|
lua_rawset(L, -3);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_settagmethod (lua_State *L) {
|
||||||
|
int tag = (int)luaL_check_int(L, 1);
|
||||||
|
const char *event = luaL_check_string(L, 2);
|
||||||
|
luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
|
||||||
|
"function or nil expected");
|
||||||
|
lua_pushnil(L); /* to get its tag */
|
||||||
|
if (strcmp(event, "gc") == 0 && tag != lua_tag(L, -1))
|
||||||
|
lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
|
||||||
|
lua_pop(L, 1); /* remove the nil */
|
||||||
|
lua_settagmethod(L, tag, event);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_gettagmethod (lua_State *L) {
|
||||||
|
lua_gettagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_collectgarbage (lua_State *L) {
|
||||||
|
lua_pushnumber(L, lua_collectgarbage(L, luaL_opt_int(L, 1, 0)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_type (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "any");
|
||||||
|
lua_pushstring(L, lua_type(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_next (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
||||||
|
if (lua_next(L, 1))
|
||||||
|
return 2;
|
||||||
|
else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int passresults (lua_State *L, int status, int oldtop) {
|
||||||
|
static const char *const errornames[] =
|
||||||
|
{"ok", "run-time error", "file error", "syntax error", "memory error"};
|
||||||
|
if (status == 0) {
|
||||||
|
int nresults = lua_gettop(L) - oldtop;
|
||||||
|
if (nresults > 0)
|
||||||
|
return nresults; /* results are already on the stack */
|
||||||
|
else {
|
||||||
|
lua_pushuserdata(L, NULL); /* at least one result to signal no errors */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { /* error */
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, errornames[status]); /* error code */
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_dostring (lua_State *L) {
|
||||||
|
int oldtop = lua_gettop(L);
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
if (*s == '\27') /* binary files start with ESC... */
|
||||||
|
lua_error(L, "`dostring' cannot run pre-compiled code");
|
||||||
|
return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_dofile (lua_State *L) {
|
||||||
|
int oldtop = lua_gettop(L);
|
||||||
|
const char *fname = luaL_opt_string(L, 1, NULL);
|
||||||
|
return passresults(L, lua_dofile(L, fname), oldtop);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_call (lua_State *L) {
|
||||||
|
int oldtop;
|
||||||
|
const char *options = luaL_opt_string(L, 3, "");
|
||||||
|
int err = 0; /* index of old error method */
|
||||||
|
int i, status;
|
||||||
|
int n;
|
||||||
|
luaL_checktype(L, 2, "table");
|
||||||
|
n = lua_getn(L, 2);
|
||||||
|
if (!lua_isnull(L, 4)) { /* set new error method */
|
||||||
|
lua_getglobal(L, LUA_ERRORMESSAGE);
|
||||||
|
err = lua_gettop(L); /* get index */
|
||||||
|
lua_pushvalue(L, 4);
|
||||||
|
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||||
|
}
|
||||||
|
oldtop = lua_gettop(L); /* top before function-call preparation */
|
||||||
|
/* push function */
|
||||||
|
lua_pushvalue(L, 1);
|
||||||
|
luaL_checkstack(L, n, "too many arguments");
|
||||||
|
for (i=0; i<n; i++) /* push arg[1...n] */
|
||||||
|
lua_rawgeti(L, 2, i+1);
|
||||||
|
status = lua_call(L, n, LUA_MULTRET);
|
||||||
|
if (err != 0) { /* restore old error method */
|
||||||
|
lua_pushvalue(L, err);
|
||||||
|
lua_setglobal(L, LUA_ERRORMESSAGE);
|
||||||
|
}
|
||||||
|
if (status != 0) { /* error in call? */
|
||||||
|
if (strchr(options, 'x'))
|
||||||
|
lua_pushnil(L); /* return nil to signal the error */
|
||||||
|
else
|
||||||
|
lua_error(L, NULL); /* propagate error without additional messages */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (strchr(options, 'p')) /* pack results? */
|
||||||
|
lua_error(L, "deprecated option `p' in `call'");
|
||||||
|
return lua_gettop(L) - oldtop; /* results are already on the stack */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_tostring (lua_State *L) {
|
||||||
|
char buff[64];
|
||||||
|
switch (lua_type(L, 1)[2]) {
|
||||||
|
case 'm': /* nuMber */
|
||||||
|
lua_pushstring(L, lua_tostring(L, 1));
|
||||||
|
return 1;
|
||||||
|
case 'r': /* stRing */
|
||||||
|
lua_pushvalue(L, 1);
|
||||||
|
return 1;
|
||||||
|
case 'b': /* taBle */
|
||||||
|
sprintf(buff, "table: %p", lua_topointer(L, 1));
|
||||||
|
break;
|
||||||
|
case 'n': /* fuNction */
|
||||||
|
sprintf(buff, "function: %p", lua_topointer(L, 1));
|
||||||
|
break;
|
||||||
|
case 'e': /* usErdata */
|
||||||
|
sprintf(buff, "userdata(%d): %p", lua_tag(L, 1), lua_touserdata(L, 1));
|
||||||
|
break;
|
||||||
|
case 'l': /* niL */
|
||||||
|
lua_pushstring(L, "nil");
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
luaL_argerror(L, 1, "value expected");
|
||||||
|
}
|
||||||
|
lua_pushstring(L, buff);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_foreachi (lua_State *L) {
|
||||||
|
int n, i;
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
luaL_checktype(L, 2, "function");
|
||||||
|
n = lua_getn(L, 1);
|
||||||
|
for (i=1; i<=n; i++) {
|
||||||
|
lua_pushvalue(L, 2); /* function */
|
||||||
|
lua_pushnumber(L, i); /* 1st argument */
|
||||||
|
lua_rawgeti(L, 1, i); /* 2nd argument */
|
||||||
|
lua_rawcall(L, 2, 1);
|
||||||
|
if (!lua_isnil(L, -1))
|
||||||
|
return 1;
|
||||||
|
lua_pop(L, 1); /* remove nil result */
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_foreach (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
luaL_checktype(L, 2, "function");
|
||||||
|
lua_pushnil(L); /* first index */
|
||||||
|
for (;;) {
|
||||||
|
if (lua_next(L, 1) == 0)
|
||||||
|
return 0;
|
||||||
|
lua_pushvalue(L, 2); /* function */
|
||||||
|
lua_pushvalue(L, -3); /* key */
|
||||||
|
lua_pushvalue(L, -3); /* value */
|
||||||
|
lua_rawcall(L, 2, 1);
|
||||||
|
if (!lua_isnil(L, -1))
|
||||||
|
return 1;
|
||||||
|
lua_pop(L, 2); /* remove value and result */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_assert (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "any");
|
||||||
|
if (lua_isnil(L, 1))
|
||||||
|
luaL_verror(L, "assertion failed! %.90s", luaL_opt_string(L, 2, ""));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_getn (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
lua_pushnumber(L, lua_getn(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_tinsert (lua_State *L) {
|
||||||
|
int v = lua_gettop(L); /* last argument: to be inserted */
|
||||||
|
int n, pos;
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
n = lua_getn(L, 1);
|
||||||
|
if (v == 2) /* called with only 2 arguments */
|
||||||
|
pos = n+1;
|
||||||
|
else
|
||||||
|
pos = luaL_check_int(L, 2); /* 2nd argument is the position */
|
||||||
|
lua_pushstring(L, "n");
|
||||||
|
lua_pushnumber(L, n+1);
|
||||||
|
lua_rawset(L, 1); /* t.n = n+1 */
|
||||||
|
for (; n>=pos; n--) {
|
||||||
|
lua_rawgeti(L, 1, n);
|
||||||
|
lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */
|
||||||
|
}
|
||||||
|
lua_pushvalue(L, v);
|
||||||
|
lua_rawseti(L, 1, pos); /* t[pos] = v */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaB_tremove (lua_State *L) {
|
||||||
|
int pos, n;
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
n = lua_getn(L, 1);
|
||||||
|
pos = luaL_opt_int(L, 2, n);
|
||||||
|
if (n <= 0) return 0; /* table is "empty" */
|
||||||
|
lua_rawgeti(L, 1, pos); /* result = t[pos] */
|
||||||
|
for ( ;pos<n; pos++) {
|
||||||
|
lua_rawgeti(L, 1, pos+1);
|
||||||
|
lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */
|
||||||
|
}
|
||||||
|
lua_pushstring(L, "n");
|
||||||
|
lua_pushnumber(L, n-1);
|
||||||
|
lua_rawset(L, 1); /* t.n = n-1 */
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_rawseti(L, 1, n); /* t[n] = nil */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Quicksort
|
||||||
|
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
|
||||||
|
** Addison-Wesley, 1993.)
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
static void swap (lua_State *L, int i, int j) {
|
||||||
|
lua_rawgeti(L, 1, i);
|
||||||
|
lua_rawgeti(L, 1, j);
|
||||||
|
lua_rawseti(L, 1, i);
|
||||||
|
lua_rawseti(L, 1, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int sort_comp (lua_State *L, int n, int r) {
|
||||||
|
/* WARNING: the caller (auxsort) must ensure stack space */
|
||||||
|
int res;
|
||||||
|
if (!lua_isnil(L, 2)) { /* function? */
|
||||||
|
lua_pushvalue(L, 2);
|
||||||
|
if (r) {
|
||||||
|
lua_rawgeti(L, 1, n); /* a[n] */
|
||||||
|
lua_pushvalue(L, -3); /* pivot */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushvalue(L, -2); /* pivot */
|
||||||
|
lua_rawgeti(L, 1, n); /* a[n] */
|
||||||
|
}
|
||||||
|
lua_rawcall(L, 2, 1);
|
||||||
|
res = !lua_isnil(L, -1);
|
||||||
|
}
|
||||||
|
else { /* a < b? */
|
||||||
|
lua_rawgeti(L, 1, n); /* a[n] */
|
||||||
|
if (r)
|
||||||
|
res = lua_lessthan(L, -1, -2);
|
||||||
|
else
|
||||||
|
res = lua_lessthan(L, -2, -1);
|
||||||
|
}
|
||||||
|
lua_pop(L, 1);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void auxsort (lua_State *L, int l, int u) {
|
||||||
|
while (l < u) { /* for tail recursion */
|
||||||
|
int i, j;
|
||||||
|
luaL_checkstack(L, 4, "array too large");
|
||||||
|
/* sort elements a[l], a[(l+u)/2] and a[u] */
|
||||||
|
lua_rawgeti(L, 1, u);
|
||||||
|
if (sort_comp(L, l, 0)) /* a[u] < a[l]? */
|
||||||
|
swap(L, l, u);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
if (u-l == 1) break; /* only 2 elements */
|
||||||
|
i = (l+u)/2;
|
||||||
|
lua_rawgeti(L, 1, i); /* Pivot = a[i] */
|
||||||
|
if (sort_comp(L, l, 0)) /* a[i]<a[l]? */
|
||||||
|
swap(L, l, i);
|
||||||
|
else {
|
||||||
|
if (sort_comp(L, u, 1)) /* a[u]<a[i]? */
|
||||||
|
swap(L, i, u);
|
||||||
|
}
|
||||||
|
lua_pop(L, 1); /* pop old a[i] */
|
||||||
|
if (u-l == 2) break; /* only 3 elements */
|
||||||
|
lua_rawgeti(L, 1, i); /* Pivot */
|
||||||
|
swap(L, i, u-1); /* put median element as pivot (a[u-1]) */
|
||||||
|
/* a[l] <= P == a[u-1] <= a[u], only needs to sort from l+1 to u-2 */
|
||||||
|
i = l; j = u-1;
|
||||||
|
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
|
||||||
|
/* repeat i++ until a[i] >= P */
|
||||||
|
while (sort_comp(L, ++i, 1))
|
||||||
|
if (i>u) lua_error(L, "invalid order function for sorting");
|
||||||
|
/* repeat j-- until a[j] <= P */
|
||||||
|
while (sort_comp(L, --j, 0))
|
||||||
|
if (j<l) lua_error(L, "invalid order function for sorting");
|
||||||
|
if (j<i) break;
|
||||||
|
swap(L, i, j);
|
||||||
|
}
|
||||||
|
swap(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */
|
||||||
|
/* a[l..i-1] <= a[i] == P <= a[i+1..u] */
|
||||||
|
/* adjust so that smaller "half" is in [j..i] and larger one in [l..u] */
|
||||||
|
if (i-l < u-i) {
|
||||||
|
j=l; i=i-1; l=i+2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
j=i+1; i=u; u=j-2;
|
||||||
|
}
|
||||||
|
lua_pop(L, 1); /* remove pivot from stack */
|
||||||
|
auxsort(L, j, i); /* call recursively the smaller one */
|
||||||
|
} /* repeat the routine for the larger one */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int luaB_sort (lua_State *L) {
|
||||||
|
int n;
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
n = lua_getn(L, 1);
|
||||||
|
if (!lua_isnull(L, 2)) /* is there a 2nd argument? */
|
||||||
|
luaL_checktype(L, 2, "function");
|
||||||
|
lua_settop(L, 2); /* make sure there is two arguments */
|
||||||
|
auxsort(L, 1, n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Deprecated functions to manipulate global environment.
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define num_deprecated 4
|
||||||
|
|
||||||
|
static const struct luaL_reg deprecated_names [num_deprecated] = {
|
||||||
|
{"foreachvar", luaB_foreach},
|
||||||
|
{"nextvar", luaB_next},
|
||||||
|
{"rawgetglobal", luaB_rawget},
|
||||||
|
{"rawsetglobal", luaB_rawset}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef LUA_DEPRECATETFUNCS
|
||||||
|
|
||||||
|
/*
|
||||||
|
** call corresponding function inserting `globals' as first argument
|
||||||
|
*/
|
||||||
|
static int deprecated_func (lua_State *L) {
|
||||||
|
lua_insert(L, 1); /* upvalue is the function to be called */
|
||||||
|
lua_getglobals(L);
|
||||||
|
lua_insert(L, 2); /* table of globals is 1o argument */
|
||||||
|
lua_rawcall(L, lua_gettop(L)-1, LUA_MULTRET);
|
||||||
|
return lua_gettop(L); /* return all results */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void deprecated_funcs (lua_State *L) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<num_deprecated; i++) {
|
||||||
|
lua_pushcfunction(L, deprecated_names[i].func);
|
||||||
|
lua_pushcclosure(L, deprecated_func, 1);
|
||||||
|
lua_setglobal(L, deprecated_names[i].name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
/*
|
||||||
|
** gives an explicit error in any attempt to call a deprecated function
|
||||||
|
*/
|
||||||
|
static int deprecated_func (lua_State *L) {
|
||||||
|
luaL_verror(L, "function `%.20s' is deprecated", lua_tostring(L, -1));
|
||||||
|
return 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void deprecated_funcs (lua_State *L) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<num_deprecated; i++) {
|
||||||
|
lua_pushstring(L, deprecated_names[i].name);
|
||||||
|
lua_pushcclosure(L, deprecated_func, 1);
|
||||||
|
lua_setglobal(L, deprecated_names[i].name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
static const struct luaL_reg base_funcs[] = {
|
||||||
|
{LUA_ALERT, luaB__ALERT},
|
||||||
|
{LUA_ERRORMESSAGE, luaB__ERRORMESSAGE},
|
||||||
|
{"call", luaB_call},
|
||||||
|
{"collectgarbage", luaB_collectgarbage},
|
||||||
|
{"copytagmethods", luaB_copytagmethods},
|
||||||
|
{"dofile", luaB_dofile},
|
||||||
|
{"dostring", luaB_dostring},
|
||||||
|
{"error", luaB_error},
|
||||||
|
{"foreach", luaB_foreach},
|
||||||
|
{"foreachi", luaB_foreachi},
|
||||||
|
{"getglobal", luaB_getglobal},
|
||||||
|
{"gettagmethod", luaB_gettagmethod},
|
||||||
|
{"globals", luaB_globals},
|
||||||
|
{"newtag", luaB_newtag},
|
||||||
|
{"next", luaB_next},
|
||||||
|
{"print", luaB_print},
|
||||||
|
{"rawget", luaB_rawget},
|
||||||
|
{"rawset", luaB_rawset},
|
||||||
|
{"rawgettable", luaB_rawget}, /* for compatibility */
|
||||||
|
{"rawsettable", luaB_rawset}, /* for compatibility */
|
||||||
|
{"setglobal", luaB_setglobal},
|
||||||
|
{"settag", luaB_settag},
|
||||||
|
{"settagmethod", luaB_settagmethod},
|
||||||
|
{"tag", luaB_tag},
|
||||||
|
{"tonumber", luaB_tonumber},
|
||||||
|
{"tostring", luaB_tostring},
|
||||||
|
{"type", luaB_type},
|
||||||
|
{"assert", luaB_assert},
|
||||||
|
{"getn", luaB_getn},
|
||||||
|
{"sort", luaB_sort},
|
||||||
|
{"tinsert", luaB_tinsert},
|
||||||
|
{"tremove", luaB_tremove}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void lua_baselibopen (lua_State *L) {
|
||||||
|
luaL_openl(L, base_funcs);
|
||||||
|
lua_pushstring(L, LUA_VERSION);
|
||||||
|
lua_setglobal(L, "_VERSION");
|
||||||
|
deprecated_funcs(L);
|
||||||
|
}
|
||||||
|
|
||||||
700
lcode.c
Normal file
700
lcode.c
Normal file
@@ -0,0 +1,700 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lcode.c,v 1.49 2000/08/15 13:18:28 roberto Exp roberto $
|
||||||
|
** Code generator for Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "stdlib.h"
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lcode.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "llex.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lparser.h"
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_error (LexState *ls, const char *msg) {
|
||||||
|
luaX_error(ls, msg, ls->t.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Returns the the previous instruction, for optimizations.
|
||||||
|
** If there is a jump target between this and the current instruction,
|
||||||
|
** returns a dummy instruction to avoid wrong optimizations.
|
||||||
|
*/
|
||||||
|
static Instruction previous_instruction (FuncState *fs) {
|
||||||
|
if (fs->pc > fs->lasttarget) /* no jumps to current position? */
|
||||||
|
return fs->f->code[fs->pc-1]; /* returns previous instruction */
|
||||||
|
else
|
||||||
|
return CREATE_0(OP_END); /* no optimizations after an `END' */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaK_jump (FuncState *fs) {
|
||||||
|
int j = luaK_code1(fs, OP_JMP, NO_JUMP);
|
||||||
|
if (j == fs->lasttarget) { /* possible jumps to this jump? */
|
||||||
|
luaK_concat(fs, &j, fs->jlt); /* keep them on hold */
|
||||||
|
fs->jlt = NO_JUMP;
|
||||||
|
}
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaK_fixjump (FuncState *fs, int pc, int dest) {
|
||||||
|
Instruction *jmp = &fs->f->code[pc];
|
||||||
|
if (dest == NO_JUMP)
|
||||||
|
SETARG_S(*jmp, NO_JUMP); /* point to itself to represent end of list */
|
||||||
|
else { /* jump is relative to position following jump instruction */
|
||||||
|
int offset = dest-(pc+1);
|
||||||
|
if (abs(offset) > MAXARG_S)
|
||||||
|
luaK_error(fs->ls, "control structure too long");
|
||||||
|
SETARG_S(*jmp, offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaK_getjump (FuncState *fs, int pc) {
|
||||||
|
int offset = GETARG_S(fs->f->code[pc]);
|
||||||
|
if (offset == NO_JUMP) /* point to itself represents end of list */
|
||||||
|
return NO_JUMP; /* end of list */
|
||||||
|
else
|
||||||
|
return (pc+1)+offset; /* turn offset into absolute position */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** returns current `pc' and marks it as a jump target (to avoid wrong
|
||||||
|
** optimizations with consecutive instructions not in the same basic block).
|
||||||
|
** discharge list of jumps to last target.
|
||||||
|
*/
|
||||||
|
int luaK_getlabel (FuncState *fs) {
|
||||||
|
if (fs->pc != fs->lasttarget) {
|
||||||
|
int lasttarget = fs->lasttarget;
|
||||||
|
fs->lasttarget = fs->pc;
|
||||||
|
luaK_patchlist(fs, fs->jlt, lasttarget); /* discharge old list `jlt' */
|
||||||
|
fs->jlt = NO_JUMP; /* nobody jumps to this new label (yet) */
|
||||||
|
}
|
||||||
|
return fs->pc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_deltastack (FuncState *fs, int delta) {
|
||||||
|
fs->stacklevel += delta;
|
||||||
|
if (fs->stacklevel > fs->f->maxstacksize) {
|
||||||
|
if (fs->stacklevel > MAXSTACK)
|
||||||
|
luaK_error(fs->ls, "function or expression too complex");
|
||||||
|
fs->f->maxstacksize = fs->stacklevel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_kstr (LexState *ls, int c) {
|
||||||
|
luaK_code1(ls->fs, OP_PUSHSTRING, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int number_constant (FuncState *fs, Number r) {
|
||||||
|
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
|
||||||
|
Proto *f = fs->f;
|
||||||
|
int c = f->nknum;
|
||||||
|
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
|
||||||
|
while (--c >= lim)
|
||||||
|
if (f->knum[c] == r) return c;
|
||||||
|
/* not found; create a new entry */
|
||||||
|
luaM_growvector(fs->L, f->knum, f->nknum, 1, Number,
|
||||||
|
"constant table overflow", MAXARG_U);
|
||||||
|
c = f->nknum++;
|
||||||
|
f->knum[c] = r;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_number (FuncState *fs, Number f) {
|
||||||
|
if (f <= (Number)MAXARG_S && (Number)(int)f == f)
|
||||||
|
luaK_code1(fs, OP_PUSHINT, (int)f); /* f has a short integer value */
|
||||||
|
else
|
||||||
|
luaK_code1(fs, OP_PUSHNUM, number_constant(fs, f));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_adjuststack (FuncState *fs, int n) {
|
||||||
|
if (n > 0)
|
||||||
|
luaK_code1(fs, OP_POP, n);
|
||||||
|
else
|
||||||
|
luaK_code1(fs, OP_PUSHNIL, -n);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaK_lastisopen (FuncState *fs) {
|
||||||
|
/* check whether last instruction is an open function call */
|
||||||
|
Instruction i = previous_instruction(fs);
|
||||||
|
if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET)
|
||||||
|
return 1;
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_setcallreturns (FuncState *fs, int nresults) {
|
||||||
|
if (luaK_lastisopen(fs)) { /* expression is an open function call? */
|
||||||
|
SETARG_B(fs->f->code[fs->pc-1], nresults); /* set number of results */
|
||||||
|
luaK_deltastack(fs, nresults); /* push results */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int discharge (FuncState *fs, expdesc *var) {
|
||||||
|
switch (var->k) {
|
||||||
|
case VLOCAL:
|
||||||
|
luaK_code1(fs, OP_GETLOCAL, var->u.index);
|
||||||
|
break;
|
||||||
|
case VGLOBAL:
|
||||||
|
luaK_code1(fs, OP_GETGLOBAL, var->u.index);
|
||||||
|
break;
|
||||||
|
case VINDEXED:
|
||||||
|
luaK_code0(fs, OP_GETTABLE);
|
||||||
|
break;
|
||||||
|
case VEXP:
|
||||||
|
return 0; /* nothing to do */
|
||||||
|
}
|
||||||
|
var->k = VEXP;
|
||||||
|
var->u.l.t = var->u.l.f = NO_JUMP;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void discharge1 (FuncState *fs, expdesc *var) {
|
||||||
|
discharge(fs, var);
|
||||||
|
/* if it has jumps then it is already discharged */
|
||||||
|
if (var->u.l.t == NO_JUMP && var->u.l.f == NO_JUMP)
|
||||||
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_storevar (LexState *ls, const expdesc *var) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
switch (var->k) {
|
||||||
|
case VLOCAL:
|
||||||
|
luaK_code1(fs, OP_SETLOCAL, var->u.index);
|
||||||
|
break;
|
||||||
|
case VGLOBAL:
|
||||||
|
luaK_code1(fs, OP_SETGLOBAL, var->u.index);
|
||||||
|
break;
|
||||||
|
case VINDEXED: /* table is at top-3; pop 3 elements after operation */
|
||||||
|
luaK_code2(fs, OP_SETTABLE, 3, 3);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LUA_INTERNALERROR("invalid var kind to store");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static OpCode invertjump (OpCode op) {
|
||||||
|
switch (op) {
|
||||||
|
case OP_JMPNE: return OP_JMPEQ;
|
||||||
|
case OP_JMPEQ: return OP_JMPNE;
|
||||||
|
case OP_JMPLT: return OP_JMPGE;
|
||||||
|
case OP_JMPLE: return OP_JMPGT;
|
||||||
|
case OP_JMPGT: return OP_JMPLE;
|
||||||
|
case OP_JMPGE: return OP_JMPLT;
|
||||||
|
case OP_JMPT: case OP_JMPONT: return OP_JMPF;
|
||||||
|
case OP_JMPF: case OP_JMPONF: return OP_JMPT;
|
||||||
|
default:
|
||||||
|
LUA_INTERNALERROR("invalid jump instruction");
|
||||||
|
return OP_END; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaK_patchlistaux (FuncState *fs, int list, int target,
|
||||||
|
OpCode special, int special_target) {
|
||||||
|
Instruction *code = fs->f->code;
|
||||||
|
while (list != NO_JUMP) {
|
||||||
|
int next = luaK_getjump(fs, list);
|
||||||
|
Instruction *i = &code[list];
|
||||||
|
OpCode op = GET_OPCODE(*i);
|
||||||
|
if (op == special) /* this `op' already has a value */
|
||||||
|
luaK_fixjump(fs, list, special_target);
|
||||||
|
else {
|
||||||
|
luaK_fixjump(fs, list, target); /* do the patch */
|
||||||
|
if (op == OP_JMPONT) /* remove eventual values */
|
||||||
|
SET_OPCODE(*i, OP_JMPT);
|
||||||
|
else if (op == OP_JMPONF)
|
||||||
|
SET_OPCODE(*i, OP_JMPF);
|
||||||
|
}
|
||||||
|
list = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_patchlist (FuncState *fs, int list, int target) {
|
||||||
|
if (target == fs->lasttarget) /* same target that list `jlt'? */
|
||||||
|
luaK_concat(fs, &fs->jlt, list); /* delay fixing */
|
||||||
|
else
|
||||||
|
luaK_patchlistaux(fs, list, target, OP_END, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int need_value (FuncState *fs, int list, OpCode hasvalue) {
|
||||||
|
/* check whether list has a jump without a value */
|
||||||
|
for (; list != NO_JUMP; list = luaK_getjump(fs, list))
|
||||||
|
if (GET_OPCODE(fs->f->code[list]) != hasvalue) return 1;
|
||||||
|
return 0; /* not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_concat (FuncState *fs, int *l1, int l2) {
|
||||||
|
if (*l1 == NO_JUMP)
|
||||||
|
*l1 = l2;
|
||||||
|
else {
|
||||||
|
int list = *l1;
|
||||||
|
for (;;) { /* traverse `l1' */
|
||||||
|
int next = luaK_getjump(fs, list);
|
||||||
|
if (next == NO_JUMP) { /* end of list? */
|
||||||
|
luaK_fixjump(fs, list, l2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
list = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaK_testgo (FuncState *fs, expdesc *v, int invert, OpCode jump) {
|
||||||
|
int prevpos; /* position of last instruction */
|
||||||
|
Instruction *previous;
|
||||||
|
int *golist, *exitlist;
|
||||||
|
if (!invert) {
|
||||||
|
golist = &v->u.l.f; /* go if false */
|
||||||
|
exitlist = &v->u.l.t; /* exit if true */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
golist = &v->u.l.t; /* go if true */
|
||||||
|
exitlist = &v->u.l.f; /* exit if false */
|
||||||
|
}
|
||||||
|
discharge1(fs, v);
|
||||||
|
prevpos = fs->pc-1;
|
||||||
|
previous = &fs->f->code[prevpos];
|
||||||
|
LUA_ASSERT(*previous==previous_instruction(fs), "no jump allowed here");
|
||||||
|
if (!ISJUMP(GET_OPCODE(*previous)))
|
||||||
|
prevpos = luaK_code1(fs, jump, NO_JUMP);
|
||||||
|
else { /* last instruction is already a jump */
|
||||||
|
if (invert)
|
||||||
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
||||||
|
}
|
||||||
|
luaK_concat(fs, exitlist, prevpos); /* insert last jump in `exitlist' */
|
||||||
|
luaK_patchlist(fs, *golist, luaK_getlabel(fs));
|
||||||
|
*golist = NO_JUMP;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue) {
|
||||||
|
luaK_testgo(fs, v, 1, keepvalue ? OP_JMPONF : OP_JMPF);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaK_goiffalse (FuncState *fs, expdesc *v, int keepvalue) {
|
||||||
|
luaK_testgo(fs, v, 0, keepvalue ? OP_JMPONT : OP_JMPT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int code_label (FuncState *fs, OpCode op, int arg) {
|
||||||
|
luaK_getlabel(fs); /* those instructions may be jump targets */
|
||||||
|
return luaK_code1(fs, op, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_tostack (LexState *ls, expdesc *v, int onlyone) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
if (!discharge(fs, v)) { /* `v' is an expression? */
|
||||||
|
OpCode previous = GET_OPCODE(fs->f->code[fs->pc-1]);
|
||||||
|
if (!ISJUMP(previous) && v->u.l.f == NO_JUMP && v->u.l.t == NO_JUMP) {
|
||||||
|
/* expression has no jumps */
|
||||||
|
if (onlyone)
|
||||||
|
luaK_setcallreturns(fs, 1); /* call must return 1 value */
|
||||||
|
}
|
||||||
|
else { /* expression has jumps */
|
||||||
|
int final; /* position after whole expression */
|
||||||
|
int j = NO_JUMP; /* eventual jump over values */
|
||||||
|
int p_nil = NO_JUMP; /* position of an eventual PUSHNIL */
|
||||||
|
int p_1 = NO_JUMP; /* position of an eventual PUSHINT */
|
||||||
|
if (ISJUMP(previous) || need_value(fs, v->u.l.f, OP_JMPONF)
|
||||||
|
|| need_value(fs, v->u.l.t, OP_JMPONT)) {
|
||||||
|
/* expression needs values */
|
||||||
|
if (ISJUMP(previous))
|
||||||
|
luaK_concat(fs, &v->u.l.t, fs->pc-1); /* put `previous' in t. list */
|
||||||
|
else {
|
||||||
|
j = code_label(fs, OP_JMP, NO_JUMP); /* to jump over both pushes */
|
||||||
|
/* correct stack for compiler and symbolic execution */
|
||||||
|
luaK_adjuststack(fs, 1);
|
||||||
|
}
|
||||||
|
p_nil = code_label(fs, OP_PUSHNILJMP, 0);
|
||||||
|
p_1 = code_label(fs, OP_PUSHINT, 1);
|
||||||
|
luaK_patchlist(fs, j, luaK_getlabel(fs));
|
||||||
|
}
|
||||||
|
final = luaK_getlabel(fs);
|
||||||
|
luaK_patchlistaux(fs, v->u.l.f, p_nil, OP_JMPONF, final);
|
||||||
|
luaK_patchlistaux(fs, v->u.l.t, p_1, OP_JMPONT, final);
|
||||||
|
v->u.l.f = v->u.l.t = NO_JUMP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
if (op == OPR_MINUS) {
|
||||||
|
luaK_tostack(ls, v, 1);
|
||||||
|
luaK_code0(fs, OP_MINUS);
|
||||||
|
}
|
||||||
|
else { /* op == NOT */
|
||||||
|
Instruction *previous;
|
||||||
|
discharge1(fs, v);
|
||||||
|
previous = &fs->f->code[fs->pc-1];
|
||||||
|
if (ISJUMP(GET_OPCODE(*previous)))
|
||||||
|
SET_OPCODE(*previous, invertjump(GET_OPCODE(*previous)));
|
||||||
|
else
|
||||||
|
luaK_code0(fs, OP_NOT);
|
||||||
|
/* interchange true and false lists */
|
||||||
|
{ int temp = v->u.l.f; v->u.l.f = v->u.l.t; v->u.l.t = temp; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_infix (LexState *ls, BinOpr op, expdesc *v) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
switch (op) {
|
||||||
|
case OPR_AND:
|
||||||
|
luaK_goiftrue(fs, v, 1);
|
||||||
|
break;
|
||||||
|
case OPR_OR:
|
||||||
|
luaK_goiffalse(fs, v, 1);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
luaK_tostack(ls, v, 1); /* all other binary operators need a value */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static const struct {
|
||||||
|
OpCode opcode; /* opcode for each binary operator */
|
||||||
|
int arg; /* default argument for the opcode */
|
||||||
|
} codes[] = { /* ORDER OPR */
|
||||||
|
{OP_ADD, 0}, {OP_SUB, 0}, {OP_MULT, 0}, {OP_DIV, 0},
|
||||||
|
{OP_POW, 0}, {OP_CONCAT, 2},
|
||||||
|
{OP_JMPNE, NO_JUMP}, {OP_JMPEQ, NO_JUMP},
|
||||||
|
{OP_JMPLT, NO_JUMP}, {OP_JMPLE, NO_JUMP},
|
||||||
|
{OP_JMPGT, NO_JUMP}, {OP_JMPGE, NO_JUMP}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2) {
|
||||||
|
FuncState *fs = ls->fs;
|
||||||
|
switch (op) {
|
||||||
|
case OPR_AND: {
|
||||||
|
LUA_ASSERT(v1->u.l.t == NO_JUMP, "list must be closed");
|
||||||
|
discharge1(fs, v2);
|
||||||
|
v1->u.l.t = v2->u.l.t;
|
||||||
|
luaK_concat(fs, &v1->u.l.f, v2->u.l.f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OPR_OR: {
|
||||||
|
LUA_ASSERT(v1->u.l.f == NO_JUMP, "list must be closed");
|
||||||
|
discharge1(fs, v2);
|
||||||
|
v1->u.l.f = v2->u.l.f;
|
||||||
|
luaK_concat(fs, &v1->u.l.t, v2->u.l.t);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
luaK_tostack(ls, v2, 1); /* `v2' must be a value */
|
||||||
|
luaK_code1(fs, codes[op].opcode, codes[op].arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void codelineinfo (FuncState *fs) {
|
||||||
|
LexState *ls = fs->ls;
|
||||||
|
if (ls->lastline > fs->lastline) {
|
||||||
|
luaM_growvector(fs->L, fs->f->lineinfo, fs->nlineinfo, 2, int,
|
||||||
|
"line info overflow", MAX_INT);
|
||||||
|
if (ls->lastline > fs->lastline+1)
|
||||||
|
fs->f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
|
||||||
|
fs->f->lineinfo[fs->nlineinfo++] = fs->pc;
|
||||||
|
fs->lastline = ls->lastline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaK_code0 (FuncState *fs, OpCode o) {
|
||||||
|
return luaK_code2(fs, o, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaK_code1 (FuncState *fs, OpCode o, int arg1) {
|
||||||
|
return luaK_code2(fs, o, arg1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
|
||||||
|
Instruction i = previous_instruction(fs);
|
||||||
|
int delta = luaK_opproperties[o].push - luaK_opproperties[o].pop;
|
||||||
|
int optm = 0; /* 1 when there is an optimization */
|
||||||
|
switch (o) {
|
||||||
|
case OP_CLOSURE: {
|
||||||
|
delta = -arg2+1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETTABLE: {
|
||||||
|
delta = -arg2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETLIST: {
|
||||||
|
if (arg2 == 0) return NO_JUMP; /* nothing to do */
|
||||||
|
delta = -arg2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETMAP: {
|
||||||
|
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||||
|
delta = -2*arg1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_RETURN: {
|
||||||
|
if (GET_OPCODE(i) == OP_CALL && GETARG_B(i) == MULT_RET) {
|
||||||
|
SET_OPCODE(i, OP_TAILCALL);
|
||||||
|
SETARG_B(i, arg1);
|
||||||
|
optm = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNIL: {
|
||||||
|
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||||
|
delta = arg1;
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_PUSHNIL: SETARG_U(i, GETARG_U(i)+arg1); optm = 1; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_POP: {
|
||||||
|
if (arg1 == 0) return NO_JUMP; /* nothing to do */
|
||||||
|
delta = -arg1;
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_SETTABLE: SETARG_B(i, GETARG_B(i)+arg1); optm = 1; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETTABLE: {
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_PUSHSTRING: /* `t.x' */
|
||||||
|
SET_OPCODE(i, OP_GETDOTTED);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
case OP_GETLOCAL: /* `t[i]' */
|
||||||
|
SET_OPCODE(i, OP_GETINDEXED);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_ADD: {
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_PUSHINT: SET_OPCODE(i, OP_ADDI); optm = 1; break; /* `a+k' */
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SUB: {
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_PUSHINT: /* `a-k' */
|
||||||
|
i = CREATE_S(OP_ADDI, -GETARG_S(i));
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CONCAT: {
|
||||||
|
delta = -arg1+1;
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_CONCAT: /* `a..b..c' */
|
||||||
|
SETARG_U(i, GETARG_U(i)+1);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_MINUS: {
|
||||||
|
switch(GET_OPCODE(i)) {
|
||||||
|
case OP_PUSHINT: /* `-k' */
|
||||||
|
SETARG_S(i, -GETARG_S(i));
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
case OP_PUSHNUM: /* `-k' */
|
||||||
|
SET_OPCODE(i, OP_PUSHNEGNUM);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPNE: {
|
||||||
|
if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a~=nil' */
|
||||||
|
i = CREATE_S(OP_JMPT, NO_JUMP);
|
||||||
|
optm = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPEQ: {
|
||||||
|
if (i == CREATE_U(OP_PUSHNIL, 1)) { /* `a==nil' */
|
||||||
|
i = CREATE_0(OP_NOT);
|
||||||
|
delta = -1; /* just undo effect of previous PUSHNIL */
|
||||||
|
optm = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPT:
|
||||||
|
case OP_JMPONT: {
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_NOT: {
|
||||||
|
i = CREATE_S(OP_JMPF, NO_JUMP);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHINT: {
|
||||||
|
if (o == OP_JMPT) { /* JMPONT must keep original integer value */
|
||||||
|
i = CREATE_S(OP_JMP, NO_JUMP);
|
||||||
|
optm = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNIL: {
|
||||||
|
if (GETARG_U(i) == 1) {
|
||||||
|
fs->pc--; /* erase previous instruction */
|
||||||
|
luaK_deltastack(fs, -1); /* correct stack */
|
||||||
|
return NO_JUMP;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPF:
|
||||||
|
case OP_JMPONF: {
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_NOT: {
|
||||||
|
i = CREATE_S(OP_JMPT, NO_JUMP);
|
||||||
|
optm = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHINT: { /* `while 1 do ...' */
|
||||||
|
fs->pc--; /* erase previous instruction */
|
||||||
|
luaK_deltastack(fs, -1); /* correct stack */
|
||||||
|
return NO_JUMP;
|
||||||
|
}
|
||||||
|
case OP_PUSHNIL: { /* `repeat ... until nil' */
|
||||||
|
if (GETARG_U(i) == 1) {
|
||||||
|
i = CREATE_S(OP_JMP, NO_JUMP);
|
||||||
|
optm = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETDOTTED:
|
||||||
|
case OP_GETINDEXED:
|
||||||
|
case OP_TAILCALL:
|
||||||
|
case OP_ADDI: {
|
||||||
|
LUA_INTERNALERROR("instruction used only for optimizations");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
LUA_ASSERT(delta != VD, "invalid delta");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaK_deltastack(fs, delta);
|
||||||
|
if (optm) { /* optimize: put instruction in place of last one */
|
||||||
|
fs->f->code[fs->pc-1] = i; /* change previous instruction */
|
||||||
|
return fs->pc-1; /* do not generate new instruction */
|
||||||
|
}
|
||||||
|
/* else build new instruction */
|
||||||
|
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||||
|
case iO: i = CREATE_0(o); break;
|
||||||
|
case iU: i = CREATE_U(o, arg1); break;
|
||||||
|
case iS: i = CREATE_S(o, arg1); break;
|
||||||
|
case iAB: i = CREATE_AB(o, arg1, arg2); break;
|
||||||
|
}
|
||||||
|
codelineinfo(fs);
|
||||||
|
/* put new instruction in code array */
|
||||||
|
luaM_growvector(fs->L, fs->f->code, fs->pc, 1, Instruction,
|
||||||
|
"code size overflow", MAX_INT);
|
||||||
|
fs->f->code[fs->pc] = i;
|
||||||
|
return fs->pc++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const struct OpProperties luaK_opproperties[NUM_OPCODES] = {
|
||||||
|
{iO, 0, 0}, /* OP_END */
|
||||||
|
{iU, 0, 0}, /* OP_RETURN */
|
||||||
|
{iAB, 0, 0}, /* OP_CALL */
|
||||||
|
{iAB, 0, 0}, /* OP_TAILCALL */
|
||||||
|
{iU, VD, 0}, /* OP_PUSHNIL */
|
||||||
|
{iU, VD, 0}, /* OP_POP */
|
||||||
|
{iS, 1, 0}, /* OP_PUSHINT */
|
||||||
|
{iU, 1, 0}, /* OP_PUSHSTRING */
|
||||||
|
{iU, 1, 0}, /* OP_PUSHNUM */
|
||||||
|
{iU, 1, 0}, /* OP_PUSHNEGNUM */
|
||||||
|
{iU, 1, 0}, /* OP_PUSHUPVALUE */
|
||||||
|
{iU, 1, 0}, /* OP_GETLOCAL */
|
||||||
|
{iU, 1, 0}, /* OP_GETGLOBAL */
|
||||||
|
{iO, 1, 2}, /* OP_GETTABLE */
|
||||||
|
{iU, 1, 1}, /* OP_GETDOTTED */
|
||||||
|
{iU, 1, 1}, /* OP_GETINDEXED */
|
||||||
|
{iU, 2, 1}, /* OP_PUSHSELF */
|
||||||
|
{iU, 1, 0}, /* OP_CREATETABLE */
|
||||||
|
{iU, 0, 1}, /* OP_SETLOCAL */
|
||||||
|
{iU, 0, 1}, /* OP_SETGLOBAL */
|
||||||
|
{iAB, VD, 0}, /* OP_SETTABLE */
|
||||||
|
{iAB, VD, 0}, /* OP_SETLIST */
|
||||||
|
{iU, VD, 0}, /* OP_SETMAP */
|
||||||
|
{iO, 1, 2}, /* OP_ADD */
|
||||||
|
{iS, 1, 1}, /* OP_ADDI */
|
||||||
|
{iO, 1, 2}, /* OP_SUB */
|
||||||
|
{iO, 1, 2}, /* OP_MULT */
|
||||||
|
{iO, 1, 2}, /* OP_DIV */
|
||||||
|
{iO, 1, 2}, /* OP_POW */
|
||||||
|
{iU, VD, 0}, /* OP_CONCAT */
|
||||||
|
{iO, 1, 1}, /* OP_MINUS */
|
||||||
|
{iO, 1, 1}, /* OP_NOT */
|
||||||
|
{iS, 0, 2}, /* OP_JMPNE */
|
||||||
|
{iS, 0, 2}, /* OP_JMPEQ */
|
||||||
|
{iS, 0, 2}, /* OP_JMPLT */
|
||||||
|
{iS, 0, 2}, /* OP_JMPLE */
|
||||||
|
{iS, 0, 2}, /* OP_JMPGT */
|
||||||
|
{iS, 0, 2}, /* OP_JMPGE */
|
||||||
|
{iS, 0, 1}, /* OP_JMPT */
|
||||||
|
{iS, 0, 1}, /* OP_JMPF */
|
||||||
|
{iS, 0, 1}, /* OP_JMPONT */
|
||||||
|
{iS, 0, 1}, /* OP_JMPONF */
|
||||||
|
{iS, 0, 0}, /* OP_JMP */
|
||||||
|
{iO, 0, 0}, /* OP_PUSHNILJMP */
|
||||||
|
{iS, 0, 0}, /* OP_FORPREP */
|
||||||
|
{iS, 0, 3}, /* OP_FORLOOP */
|
||||||
|
{iS, 2, 0}, /* OP_LFORPREP */
|
||||||
|
{iS, 0, 3}, /* OP_LFORLOOP */
|
||||||
|
{iAB, VD, 0} /* OP_CLOSURE */
|
||||||
|
};
|
||||||
|
|
||||||
70
lcode.h
Normal file
70
lcode.h
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lcode.h,v 1.15 2000/06/28 20:20:36 roberto Exp roberto $
|
||||||
|
** Code generator for Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lcode_h
|
||||||
|
#define lcode_h
|
||||||
|
|
||||||
|
#include "llex.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lparser.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Marks the end of a patch list. It is an invalid value both as an absolute
|
||||||
|
** address, and as a list link (would link an element to itself).
|
||||||
|
*/
|
||||||
|
#define NO_JUMP (-1)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** grep "ORDER OPR" if you change these enums
|
||||||
|
*/
|
||||||
|
typedef enum BinOpr {
|
||||||
|
OPR_ADD, OPR_SUB, OPR_MULT, OPR_DIV, OPR_POW,
|
||||||
|
OPR_CONCAT,
|
||||||
|
OPR_NE, OPR_EQ, OPR_LT, OPR_LE, OPR_GT, OPR_GE,
|
||||||
|
OPR_AND, OPR_OR,
|
||||||
|
OPR_NOBINOPR
|
||||||
|
} BinOpr;
|
||||||
|
|
||||||
|
typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_NOUNOPR } UnOpr;
|
||||||
|
|
||||||
|
|
||||||
|
enum Mode {iO, iU, iS, iAB}; /* instruction format */
|
||||||
|
|
||||||
|
#define VD 100 /* flag for variable delta */
|
||||||
|
|
||||||
|
extern const struct OpProperties {
|
||||||
|
char mode;
|
||||||
|
unsigned char push;
|
||||||
|
unsigned char pop;
|
||||||
|
} luaK_opproperties[];
|
||||||
|
|
||||||
|
|
||||||
|
void luaK_error (LexState *ls, const char *msg);
|
||||||
|
int luaK_code0 (FuncState *fs, OpCode o);
|
||||||
|
int luaK_code1 (FuncState *fs, OpCode o, int arg1);
|
||||||
|
int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2);
|
||||||
|
int luaK_jump (FuncState *fs);
|
||||||
|
void luaK_patchlist (FuncState *fs, int list, int target);
|
||||||
|
void luaK_concat (FuncState *fs, int *l1, int l2);
|
||||||
|
void luaK_goiftrue (FuncState *fs, expdesc *v, int keepvalue);
|
||||||
|
int luaK_getlabel (FuncState *fs);
|
||||||
|
void luaK_deltastack (FuncState *fs, int delta);
|
||||||
|
void luaK_kstr (LexState *ls, int c);
|
||||||
|
void luaK_number (FuncState *fs, Number f);
|
||||||
|
void luaK_adjuststack (FuncState *fs, int n);
|
||||||
|
int luaK_lastisopen (FuncState *fs);
|
||||||
|
void luaK_setcallreturns (FuncState *fs, int nresults);
|
||||||
|
void luaK_tostack (LexState *ls, expdesc *v, int onlyone);
|
||||||
|
void luaK_storevar (LexState *ls, const expdesc *var);
|
||||||
|
void luaK_prefix (LexState *ls, UnOpr op, expdesc *v);
|
||||||
|
void luaK_infix (LexState *ls, BinOpr op, expdesc *v);
|
||||||
|
void luaK_posfix (LexState *ls, BinOpr op, expdesc *v1, expdesc *v2);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
180
ldblib.c
Normal file
180
ldblib.c
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ldblib.c,v 1.20 2000/09/05 19:33:32 roberto Exp roberto $
|
||||||
|
** Interface from Lua to its debug API
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void settabss (lua_State *L, const char *i, const char *v) {
|
||||||
|
lua_pushstring(L, i);
|
||||||
|
lua_pushstring(L, v);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void settabsi (lua_State *L, const char *i, int v) {
|
||||||
|
lua_pushstring(L, i);
|
||||||
|
lua_pushnumber(L, v);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int getinfo (lua_State *L) {
|
||||||
|
lua_Debug ar;
|
||||||
|
const char *options = luaL_opt_string(L, 2, "flnSu");
|
||||||
|
char buff[20];
|
||||||
|
if (lua_isnumber(L, 1)) {
|
||||||
|
if (!lua_getstack(L, (int)lua_tonumber(L, 1), &ar)) {
|
||||||
|
lua_pushnil(L); /* level out of range */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (lua_isfunction(L, 1)) {
|
||||||
|
lua_pushvalue(L, 1);
|
||||||
|
sprintf(buff, ">%.10s", options);
|
||||||
|
options = buff;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
luaL_argerror(L, 1, "function or level expected");
|
||||||
|
if (!lua_getinfo(L, options, &ar))
|
||||||
|
luaL_argerror(L, 2, "invalid option");
|
||||||
|
lua_newtable(L);
|
||||||
|
for (; *options; options++) {
|
||||||
|
switch (*options) {
|
||||||
|
case 'S':
|
||||||
|
settabss(L, "source", ar.source);
|
||||||
|
if (ar.source)
|
||||||
|
settabss(L, "short_src", ar.short_src);
|
||||||
|
settabsi(L, "linedefined", ar.linedefined);
|
||||||
|
settabss(L, "what", ar.what);
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
settabsi(L, "currentline", ar.currentline);
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
settabsi(L, "nups", ar.nups);
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
settabss(L, "name", ar.name);
|
||||||
|
settabss(L, "namewhat", ar.namewhat);
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
lua_pushstring(L, "func");
|
||||||
|
lua_pushvalue(L, -3);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1; /* return table */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int getlocal (lua_State *L) {
|
||||||
|
lua_Debug ar;
|
||||||
|
const char *name;
|
||||||
|
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||||
|
luaL_argerror(L, 1, "level out of range");
|
||||||
|
name = lua_getlocal(L, &ar, luaL_check_int(L, 2));
|
||||||
|
if (name) {
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
lua_pushvalue(L, -2);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int setlocal (lua_State *L) {
|
||||||
|
lua_Debug ar;
|
||||||
|
if (!lua_getstack(L, luaL_check_int(L, 1), &ar)) /* level out of range? */
|
||||||
|
luaL_argerror(L, 1, "level out of range");
|
||||||
|
luaL_checktype(L, 3, "any");
|
||||||
|
lua_pushstring(L, lua_setlocal(L, &ar, luaL_check_int(L, 2)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** because of these variables, this module is not reentrant, and should
|
||||||
|
** not be used in multiple states
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int linehook = LUA_NOREF; /* Lua reference to line hook function */
|
||||||
|
static int callhook = LUA_NOREF; /* Lua reference to call hook function */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void linef (lua_State *L, lua_Debug *ar) {
|
||||||
|
if (linehook != LUA_NOREF) {
|
||||||
|
lua_getref(L, linehook);
|
||||||
|
lua_pushnumber(L, ar->currentline);
|
||||||
|
lua_call(L, 1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void callf (lua_State *L, lua_Debug *ar) {
|
||||||
|
if (callhook != LUA_NOREF) {
|
||||||
|
lua_getref(L, callhook);
|
||||||
|
lua_pushstring(L, ar->event);
|
||||||
|
lua_call(L, 1, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int setcallhook (lua_State *L) {
|
||||||
|
lua_unref(L, callhook);
|
||||||
|
if (lua_isnull(L, 1)) {
|
||||||
|
callhook = LUA_NOREF;
|
||||||
|
lua_setcallhook(L, NULL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callhook = lua_ref(L, 1);
|
||||||
|
lua_setcallhook(L, callf);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int setlinehook (lua_State *L) {
|
||||||
|
lua_unref(L, linehook);
|
||||||
|
if (lua_isnull(L, 1)) {
|
||||||
|
linehook = LUA_NOREF;
|
||||||
|
lua_setlinehook(L, NULL);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
linehook = lua_ref(L, 1);
|
||||||
|
lua_setlinehook(L, linef);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg dblib[] = {
|
||||||
|
{"getlocal", getlocal},
|
||||||
|
{"getinfo", getinfo},
|
||||||
|
{"setcallhook", setcallhook},
|
||||||
|
{"setlinehook", setlinehook},
|
||||||
|
{"setlocal", setlocal}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void lua_dblibopen (lua_State *L) {
|
||||||
|
luaL_openl(L, dblib);
|
||||||
|
}
|
||||||
|
|
||||||
460
ldebug.c
Normal file
460
ldebug.c
Normal file
@@ -0,0 +1,460 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ldebug.c,v 1.41 2000/09/12 18:38:02 roberto Exp roberto $
|
||||||
|
** Debug Interface
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lapi.h"
|
||||||
|
#include "lcode.h"
|
||||||
|
#include "ldebug.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
|
||||||
|
|
||||||
|
static const char *getfuncname (lua_State *L, StkId f, const char **name);
|
||||||
|
|
||||||
|
|
||||||
|
static void setnormalized (TObject *d, const TObject *s) {
|
||||||
|
switch (s->ttype) {
|
||||||
|
case TAG_CMARK: {
|
||||||
|
clvalue(d) = clvalue(s);
|
||||||
|
ttype(d) = TAG_CCLOSURE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TAG_LMARK: {
|
||||||
|
clvalue(d) = infovalue(s)->func;
|
||||||
|
ttype(d) = TAG_LCLOSURE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: *d = *s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func) {
|
||||||
|
lua_Hook oldhook = L->callhook;
|
||||||
|
L->callhook = func;
|
||||||
|
return oldhook;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) {
|
||||||
|
lua_Hook oldhook = L->linehook;
|
||||||
|
L->linehook = func;
|
||||||
|
return oldhook;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
|
||||||
|
int i;
|
||||||
|
for (i = (top-1) - L->stack; i>=0; i--) {
|
||||||
|
if (is_T_MARK(L->stack[i].ttype)) {
|
||||||
|
if (level == 0)
|
||||||
|
return L->stack+i;
|
||||||
|
level--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_getstack (lua_State *L, int level, lua_Debug *ar) {
|
||||||
|
StkId f = aux_stackedfunction(L, level, L->top);
|
||||||
|
if (f == NULL) return 0; /* there is no such level */
|
||||||
|
else {
|
||||||
|
ar->_func = f;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int lua_nups (StkId f) {
|
||||||
|
switch (ttype(f)) {
|
||||||
|
case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK:
|
||||||
|
return clvalue(f)->nupvalues;
|
||||||
|
case TAG_LMARK:
|
||||||
|
return infovalue(f)->func->nupvalues;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
|
||||||
|
int refi;
|
||||||
|
if (lineinfo == NULL) return -1; /* no line info */
|
||||||
|
else if (pc == -1) return refline; /* function preamble */
|
||||||
|
refi = prefi ? *prefi : 0;
|
||||||
|
if (lineinfo[refi] < 0)
|
||||||
|
refline += -lineinfo[refi++];
|
||||||
|
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||||
|
while (lineinfo[refi] > pc) {
|
||||||
|
refline--;
|
||||||
|
refi--;
|
||||||
|
if (lineinfo[refi] < 0)
|
||||||
|
refline -= -lineinfo[refi--];
|
||||||
|
LUA_ASSERT(lineinfo[refi] >= 0, "invalid line info");
|
||||||
|
}
|
||||||
|
for (;;) {
|
||||||
|
int nextline = refline + 1;
|
||||||
|
int nextref = refi + 1;
|
||||||
|
if (lineinfo[nextref] < 0)
|
||||||
|
nextline += -lineinfo[nextref++];
|
||||||
|
LUA_ASSERT(lineinfo[nextref] >= 0, "invalid line info");
|
||||||
|
if (lineinfo[nextref] > pc)
|
||||||
|
break;
|
||||||
|
refline = nextline;
|
||||||
|
refi = nextref;
|
||||||
|
}
|
||||||
|
if (prefi) *prefi = refi;
|
||||||
|
return refline;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int lua_currentpc (StkId f) {
|
||||||
|
CallInfo *ci = infovalue(f);
|
||||||
|
LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc");
|
||||||
|
return (*ci->pc - ci->func->f.l->code) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int lua_currentline (StkId f) {
|
||||||
|
if (ttype(f) != TAG_LMARK)
|
||||||
|
return -1; /* only active lua functions have current-line information */
|
||||||
|
else {
|
||||||
|
CallInfo *ci = infovalue(f);
|
||||||
|
int *lineinfo = ci->func->f.l->lineinfo;
|
||||||
|
return luaG_getline(lineinfo, lua_currentpc(f), 1, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static Proto *getluaproto (StkId f) {
|
||||||
|
return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum) {
|
||||||
|
const char *name;
|
||||||
|
StkId f = ar->_func;
|
||||||
|
Proto *fp = getluaproto(f);
|
||||||
|
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||||
|
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
|
||||||
|
if (!name) return NULL;
|
||||||
|
luaA_pushobject(L, (f+1)+(localnum-1)); /* push value */
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum) {
|
||||||
|
const char *name;
|
||||||
|
StkId f = ar->_func;
|
||||||
|
Proto *fp = getluaproto(f);
|
||||||
|
L->top--; /* pop new value */
|
||||||
|
if (!fp) return NULL; /* `f' is not a Lua function? */
|
||||||
|
name = luaF_getlocalname(fp, localnum, lua_currentpc(f));
|
||||||
|
if (!name || name[0] == '*') return NULL; /* `*' starts private locals */
|
||||||
|
*((f+1)+(localnum-1)) = *L->top;
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void infoLproto (lua_Debug *ar, Proto *f) {
|
||||||
|
ar->source = f->source->str;
|
||||||
|
ar->linedefined = f->lineDefined;
|
||||||
|
ar->what = "Lua";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void lua_funcinfo (lua_Debug *ar, StkId func) {
|
||||||
|
switch (ttype(func)) {
|
||||||
|
case TAG_LCLOSURE:
|
||||||
|
infoLproto(ar, clvalue(func)->f.l);
|
||||||
|
break;
|
||||||
|
case TAG_LMARK:
|
||||||
|
infoLproto(ar, infovalue(func)->func->f.l);
|
||||||
|
break;
|
||||||
|
case TAG_CCLOSURE: case TAG_CMARK:
|
||||||
|
ar->source = "(C)";
|
||||||
|
ar->linedefined = -1;
|
||||||
|
ar->what = "C";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LUA_INTERNALERROR("invalid `func' value");
|
||||||
|
}
|
||||||
|
luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
|
||||||
|
if (ar->linedefined == 0)
|
||||||
|
ar->what = "main";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *travtagmethods (lua_State *L, const TObject *o) {
|
||||||
|
int e;
|
||||||
|
for (e=0; e<IM_N; e++) {
|
||||||
|
int t;
|
||||||
|
for (t=0; t<=L->last_tag; t++)
|
||||||
|
if (luaO_equalObj(o, luaT_getim(L, t,e)))
|
||||||
|
return luaT_eventname[e];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *travglobals (lua_State *L, const TObject *o) {
|
||||||
|
Hash *g = L->gt;
|
||||||
|
int i;
|
||||||
|
for (i=0; i<g->size; i++) {
|
||||||
|
if (luaO_equalObj(o, val(node(g, i))) &&
|
||||||
|
ttype(key(node(g, i))) == TAG_STRING)
|
||||||
|
return tsvalue(key(node(g, i)))->str;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void lua_getname (lua_State *L, StkId f, lua_Debug *ar) {
|
||||||
|
TObject o;
|
||||||
|
setnormalized(&o, f);
|
||||||
|
/* try to find a name for given function */
|
||||||
|
if ((ar->name = travglobals(L, &o)) != NULL)
|
||||||
|
ar->namewhat = "global";
|
||||||
|
/* not found: try tag methods */
|
||||||
|
else if ((ar->name = travtagmethods(L, &o)) != NULL)
|
||||||
|
ar->namewhat = "tag-method";
|
||||||
|
else ar->namewhat = ""; /* not found at all */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
|
||||||
|
StkId func;
|
||||||
|
int isactive = (*what != '>');
|
||||||
|
if (isactive)
|
||||||
|
func = ar->_func;
|
||||||
|
else {
|
||||||
|
what++; /* skip the '>' */
|
||||||
|
func = L->top - 1;
|
||||||
|
}
|
||||||
|
for (; *what; what++) {
|
||||||
|
switch (*what) {
|
||||||
|
case 'S': {
|
||||||
|
lua_funcinfo(ar, func);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'l': {
|
||||||
|
ar->currentline = lua_currentline(func);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'u': {
|
||||||
|
ar->nups = lua_nups(func);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'n': {
|
||||||
|
ar->namewhat = (isactive) ? getfuncname(L, func, &ar->name) : NULL;
|
||||||
|
if (ar->namewhat == NULL)
|
||||||
|
lua_getname(L, func, ar);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'f': {
|
||||||
|
setnormalized(L->top, func);
|
||||||
|
incr_top; /* push function */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: return 0; /* invalid option */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isactive) L->top--; /* pop function */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Symbolic Execution
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
static int pushpc (int *stack, int pc, int top, int n) {
|
||||||
|
while (n--)
|
||||||
|
stack[top++] = pc-1;
|
||||||
|
return top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) {
|
||||||
|
int stack[MAXSTACK]; /* stores last instruction that changed a stack entry */
|
||||||
|
const Instruction *code = pt->code;
|
||||||
|
int top = pt->numparams;
|
||||||
|
int pc = 0;
|
||||||
|
if (pt->is_vararg) /* varargs? */
|
||||||
|
top++; /* `arg' */
|
||||||
|
while (pc < lastpc) {
|
||||||
|
const Instruction i = code[pc++];
|
||||||
|
LUA_ASSERT(0 <= top && top <= pt->maxstacksize, "wrong stack");
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_RETURN: {
|
||||||
|
LUA_ASSERT(top >= GETARG_U(i), "wrong stack");
|
||||||
|
top = GETARG_U(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_TAILCALL: {
|
||||||
|
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||||
|
top = GETARG_B(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CALL: {
|
||||||
|
int nresults = GETARG_B(i);
|
||||||
|
if (nresults == MULT_RET) nresults = 1;
|
||||||
|
LUA_ASSERT(top >= GETARG_A(i), "wrong stack");
|
||||||
|
top = pushpc(stack, pc, GETARG_A(i), nresults);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNIL: {
|
||||||
|
top = pushpc(stack, pc, top, GETARG_U(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_POP: {
|
||||||
|
top -= GETARG_U(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETTABLE:
|
||||||
|
case OP_SETLIST: {
|
||||||
|
top -= GETARG_B(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETMAP: {
|
||||||
|
top -= 2*GETARG_U(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CONCAT: {
|
||||||
|
top -= GETARG_U(i);
|
||||||
|
stack[top++] = pc-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CLOSURE: {
|
||||||
|
top -= GETARG_B(i);
|
||||||
|
stack[top++] = pc-1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPONT:
|
||||||
|
case OP_JMPONF: {
|
||||||
|
int newpc = pc + GETARG_S(i);
|
||||||
|
/* jump is forward and do not skip `lastpc'? */
|
||||||
|
if (pc < newpc && newpc <= lastpc) {
|
||||||
|
stack[top-1] = pc-1; /* value comes from `and'/`or' */
|
||||||
|
pc = newpc; /* do the jump */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
top--; /* do not jump; pop value */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
OpCode op = GET_OPCODE(i);
|
||||||
|
LUA_ASSERT(luaK_opproperties[op].push != VD,
|
||||||
|
"invalid opcode for default");
|
||||||
|
top -= luaK_opproperties[op].pop;
|
||||||
|
LUA_ASSERT(top >= 0, "wrong stack");
|
||||||
|
top = pushpc(stack, pc, top, luaK_opproperties[op].push);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return code[stack[stackpos]];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *getobjname (lua_State *L, StkId obj, const char **name) {
|
||||||
|
StkId func = aux_stackedfunction(L, 0, obj);
|
||||||
|
if (func == NULL || ttype(func) != TAG_LMARK)
|
||||||
|
return NULL; /* not a Lua function */
|
||||||
|
else {
|
||||||
|
Proto *p = infovalue(func)->func->f.l;
|
||||||
|
int pc = lua_currentpc(func);
|
||||||
|
int stackpos = obj - (func+1); /* func+1 == function base */
|
||||||
|
Instruction i = luaG_symbexec(p, pc, stackpos);
|
||||||
|
LUA_ASSERT(pc != -1, "function must be active");
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_GETGLOBAL: {
|
||||||
|
*name = p->kstr[GETARG_U(i)]->str;
|
||||||
|
return "global";
|
||||||
|
}
|
||||||
|
case OP_GETLOCAL: {
|
||||||
|
*name = luaF_getlocalname(p, GETARG_U(i)+1, pc);
|
||||||
|
LUA_ASSERT(*name, "local must exist");
|
||||||
|
return "local";
|
||||||
|
}
|
||||||
|
case OP_PUSHSELF:
|
||||||
|
case OP_GETDOTTED: {
|
||||||
|
*name = p->kstr[GETARG_U(i)]->str;
|
||||||
|
return "field";
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return NULL; /* no useful name found */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *getfuncname (lua_State *L, StkId f, const char **name) {
|
||||||
|
StkId func = aux_stackedfunction(L, 0, f); /* calling function */
|
||||||
|
if (func == NULL || ttype(func) != TAG_LMARK)
|
||||||
|
return NULL; /* not a Lua function */
|
||||||
|
else {
|
||||||
|
Proto *p = infovalue(func)->func->f.l;
|
||||||
|
int pc = lua_currentpc(func);
|
||||||
|
Instruction i;
|
||||||
|
if (pc == -1) return NULL; /* function is not activated */
|
||||||
|
i = p->code[pc];
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_CALL: case OP_TAILCALL:
|
||||||
|
return getobjname(L, (func+1)+GETARG_A(i), name);
|
||||||
|
default:
|
||||||
|
return NULL; /* no useful name found */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
void luaG_typeerror (lua_State *L, StkId o, const char *op) {
|
||||||
|
const char *name;
|
||||||
|
const char *kind = getobjname(L, o, &name);
|
||||||
|
const char *t = luaO_typename(o);
|
||||||
|
if (kind)
|
||||||
|
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
|
||||||
|
op, kind, name, t);
|
||||||
|
else
|
||||||
|
luaO_verror(L, "attempt to %.30s a %.10s value", op, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op) {
|
||||||
|
if (ttype(p1) == t) p1++;
|
||||||
|
LUA_ASSERT(ttype(p1) != t, "must be an error");
|
||||||
|
luaG_typeerror(L, p1, op);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaG_ordererror (lua_State *L, StkId top) {
|
||||||
|
const char *t1 = luaO_typename(top-2);
|
||||||
|
const char *t2 = luaO_typename(top-1);
|
||||||
|
if (t1[2] == t2[2])
|
||||||
|
luaO_verror(L, "attempt to compare two %.10s values", t1);
|
||||||
|
else
|
||||||
|
luaO_verror(L, "attempt to compare %.10s with %.10s", t1, t2);
|
||||||
|
}
|
||||||
|
|
||||||
21
ldebug.h
Normal file
21
ldebug.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ldebug.h,v 1.4 2000/08/10 19:50:47 roberto Exp roberto $
|
||||||
|
** Auxiliary functions from Debug Interface module
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ldebug_h
|
||||||
|
#define ldebug_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
|
||||||
|
|
||||||
|
void luaG_typeerror (lua_State *L, StkId o, const char *op);
|
||||||
|
void luaG_binerror (lua_State *L, StkId p1, lua_Type t, const char *op);
|
||||||
|
int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
|
||||||
|
void luaG_ordererror (lua_State *L, StkId top);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
341
ldo.c
Normal file
341
ldo.c
Normal file
@@ -0,0 +1,341 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ldo.c,v 1.95 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
** Stack and Call structure of Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "ldebug.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lgc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lparser.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
#include "lundump.h"
|
||||||
|
#include "lvm.h"
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* space to handle stack overflow errors */
|
||||||
|
#define EXTRA_STACK (2*LUA_MINSTACK)
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_init (lua_State *L, int stacksize) {
|
||||||
|
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
|
||||||
|
L->stack_last = L->stack+(stacksize-1);
|
||||||
|
L->stacksize = stacksize;
|
||||||
|
L->Cbase = L->top = L->stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_checkstack (lua_State *L, int n) {
|
||||||
|
if (L->stack_last-L->top <= n) { /* stack overflow? */
|
||||||
|
if (L->stack_last-L->stack > (L->stacksize-1)) {
|
||||||
|
/* overflow while handling overflow: do what?? */
|
||||||
|
L->top -= EXTRA_STACK;
|
||||||
|
lua_error(L, "BAD STACK OVERFLOW! DATA CORRUPTED!");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
L->stack_last += EXTRA_STACK; /* to be used by error message */
|
||||||
|
lua_error(L, "stack overflow");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void restore_stack_limit (lua_State *L) {
|
||||||
|
if (L->top - L->stack < L->stacksize - 1)
|
||||||
|
L->stack_last = L->stack + (L->stacksize-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Adjust stack. Set top to base+extra, pushing NILs if needed.
|
||||||
|
** (we cannot add base+extra unless we are sure it fits in the stack;
|
||||||
|
** otherwise the result of such operation on pointers is undefined)
|
||||||
|
*/
|
||||||
|
void luaD_adjusttop (lua_State *L, StkId base, int extra) {
|
||||||
|
int diff = extra-(L->top-base);
|
||||||
|
if (diff <= 0)
|
||||||
|
L->top = base+extra;
|
||||||
|
else {
|
||||||
|
luaD_checkstack(L, diff);
|
||||||
|
while (diff--)
|
||||||
|
ttype(L->top++) = TAG_NIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Open a hole inside the stack at `pos'
|
||||||
|
*/
|
||||||
|
static void luaD_openstack (lua_State *L, StkId pos) {
|
||||||
|
int i = L->top-pos;
|
||||||
|
while (i--) pos[i+1] = pos[i];
|
||||||
|
incr_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
|
||||||
|
StkId old_Cbase = L->Cbase;
|
||||||
|
StkId old_top = L->Cbase = L->top;
|
||||||
|
luaD_checkstack(L, LUA_MINSTACK); /* assures minimum stack size */
|
||||||
|
L->allowhooks = 0; /* cannot call hooks inside a hook */
|
||||||
|
(*hook)(L, ar);
|
||||||
|
LUA_ASSERT(L->allowhooks == 0, "invalid allow");
|
||||||
|
L->allowhooks = 1;
|
||||||
|
L->top = old_top;
|
||||||
|
L->Cbase = old_Cbase;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook) {
|
||||||
|
if (L->allowhooks) {
|
||||||
|
lua_Debug ar;
|
||||||
|
ar._func = func;
|
||||||
|
ar.event = "line";
|
||||||
|
ar.currentline = line;
|
||||||
|
dohook(L, &ar, linehook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
|
||||||
|
const char *event) {
|
||||||
|
if (L->allowhooks) {
|
||||||
|
lua_Debug ar;
|
||||||
|
ar._func = func;
|
||||||
|
ar.event = event;
|
||||||
|
dohook(L, &ar, callhook);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
|
||||||
|
int nup = cl->nupvalues; /* number of upvalues */
|
||||||
|
StkId old_Cbase = L->Cbase;
|
||||||
|
int n;
|
||||||
|
L->Cbase = base; /* new base for C function */
|
||||||
|
luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */
|
||||||
|
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
|
||||||
|
*(L->top++) = cl->upvalue[n];
|
||||||
|
n = (*cl->f.c)(L); /* do the actual call */
|
||||||
|
L->Cbase = old_Cbase; /* restore old C base */
|
||||||
|
return L->top - n; /* return index of first result */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) {
|
||||||
|
StkId base = L->top - nParams;
|
||||||
|
luaD_openstack(L, base);
|
||||||
|
*base = *f;
|
||||||
|
luaD_call(L, base, nResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Call a function (C or Lua). The function to be called is at *func.
|
||||||
|
** The arguments are on the stack, right after the function.
|
||||||
|
** When returns, the results are on the stack, starting at the original
|
||||||
|
** function position.
|
||||||
|
** The number of results is nResults, unless nResults=LUA_MULTRET.
|
||||||
|
*/
|
||||||
|
void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||||
|
StkId firstResult;
|
||||||
|
lua_Hook callhook = L->callhook;
|
||||||
|
retry: /* for `function' tag method */
|
||||||
|
switch (ttype(func)) {
|
||||||
|
case TAG_LCLOSURE: {
|
||||||
|
CallInfo ci;
|
||||||
|
ci.func = clvalue(func);
|
||||||
|
ci.line = 0;
|
||||||
|
ttype(func) = TAG_LMARK;
|
||||||
|
infovalue(func) = &ci;
|
||||||
|
if (callhook)
|
||||||
|
luaD_callHook(L, func, callhook, "call");
|
||||||
|
firstResult = luaV_execute(L, ci.func, func+1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TAG_CCLOSURE: {
|
||||||
|
ttype(func) = TAG_CMARK;
|
||||||
|
if (callhook)
|
||||||
|
luaD_callHook(L, func, callhook, "call");
|
||||||
|
firstResult = callCclosure(L, clvalue(func), func+1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: { /* `func' is not a function; check the `function' tag method */
|
||||||
|
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
|
||||||
|
if (ttype(im) == TAG_NIL)
|
||||||
|
luaG_typeerror(L, func, "call");
|
||||||
|
luaD_openstack(L, func);
|
||||||
|
*func = *im; /* tag method is the new function to be called */
|
||||||
|
goto retry; /* retry the call */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (callhook) /* same hook that was active at entry */
|
||||||
|
luaD_callHook(L, func, callhook, "return");
|
||||||
|
/* adjust the number of results */
|
||||||
|
if (nResults == LUA_MULTRET)
|
||||||
|
nResults = L->top - firstResult;
|
||||||
|
else
|
||||||
|
luaD_adjusttop(L, firstResult, nResults);
|
||||||
|
/* move results to `func' (to erase parameters and function) */
|
||||||
|
while (nResults) {
|
||||||
|
*func++ = *(L->top - nResults);
|
||||||
|
nResults--;
|
||||||
|
}
|
||||||
|
L->top = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void message (lua_State *L, const char *s) {
|
||||||
|
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
|
||||||
|
if (*luaO_typename(em) == 'f') {
|
||||||
|
*L->top = *em;
|
||||||
|
incr_top;
|
||||||
|
lua_pushstring(L, s);
|
||||||
|
luaD_call(L, L->top-2, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_breakrun (lua_State *L, int errcode) {
|
||||||
|
if (L->errorJmp) {
|
||||||
|
L->errorJmp->status = errcode;
|
||||||
|
longjmp(L->errorJmp->b, 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (errcode != LUA_ERRMEM)
|
||||||
|
message(L, "unable to recover; exiting\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Reports an error, and jumps up to the available recovery label
|
||||||
|
*/
|
||||||
|
void lua_error (lua_State *L, const char *s) {
|
||||||
|
if (s) message(L, s);
|
||||||
|
luaD_breakrun(L, LUA_ERRRUN);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void chain_longjmp (lua_State *L, struct lua_longjmp *lj) {
|
||||||
|
lj->status = 0;
|
||||||
|
lj->base = L->Cbase;
|
||||||
|
lj->previous = L->errorJmp;
|
||||||
|
L->errorJmp = lj;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int restore_longjmp (lua_State *L, struct lua_longjmp *lj) {
|
||||||
|
L->Cbase = lj->base;
|
||||||
|
L->errorJmp = lj->previous;
|
||||||
|
return lj->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Execute a protected call.
|
||||||
|
*/
|
||||||
|
int lua_call (lua_State *L, int nargs, int nresults) {
|
||||||
|
StkId func = L->top - (nargs+1); /* function to be called */
|
||||||
|
struct lua_longjmp myErrorJmp;
|
||||||
|
chain_longjmp(L, &myErrorJmp);
|
||||||
|
if (setjmp(myErrorJmp.b) == 0) {
|
||||||
|
luaD_call(L, func, nresults);
|
||||||
|
}
|
||||||
|
else { /* an error occurred: restore the state */
|
||||||
|
L->top = func; /* remove garbage from the stack */
|
||||||
|
restore_stack_limit(L);
|
||||||
|
}
|
||||||
|
return restore_longjmp(L, &myErrorJmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int protectedparser (lua_State *L, ZIO *z, int bin) {
|
||||||
|
struct lua_longjmp myErrorJmp;
|
||||||
|
unsigned long old_blocks;
|
||||||
|
luaC_checkGC(L);
|
||||||
|
old_blocks = L->nblocks;
|
||||||
|
chain_longjmp(L, &myErrorJmp);
|
||||||
|
if (setjmp(myErrorJmp.b) == 0) {
|
||||||
|
Proto *tf = bin ? luaU_undump(L, z) : luaY_parser(L, z);
|
||||||
|
luaV_Lclosure(L, tf, 0);
|
||||||
|
}
|
||||||
|
else { /* an error occurred: correct error code */
|
||||||
|
if (myErrorJmp.status == LUA_ERRRUN)
|
||||||
|
myErrorJmp.status = LUA_ERRSYNTAX;
|
||||||
|
}
|
||||||
|
/* add new memory to threshould (as it probably will stay) */
|
||||||
|
L->GCthreshold += (L->nblocks - old_blocks);
|
||||||
|
return restore_longjmp(L, &myErrorJmp); /* error code */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int parse_file (lua_State *L, const char *filename) {
|
||||||
|
ZIO z;
|
||||||
|
char source[MAXFILENAME];
|
||||||
|
int status;
|
||||||
|
int bin; /* flag for file mode */
|
||||||
|
int c; /* look ahead char */
|
||||||
|
FILE *f = (filename == NULL) ? stdin : fopen(filename, "r");
|
||||||
|
if (f == NULL) return LUA_ERRFILE; /* unable to open file */
|
||||||
|
if (filename == NULL) filename = "(stdin)";
|
||||||
|
sprintf(source, "@%.*s", (int)sizeof(source)-2, filename);
|
||||||
|
c = fgetc(f);
|
||||||
|
ungetc(c, f);
|
||||||
|
bin = (c == ID_CHUNK);
|
||||||
|
if (bin && f != stdin) {
|
||||||
|
f = freopen(filename, "rb", f); /* set binary mode */
|
||||||
|
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
|
||||||
|
}
|
||||||
|
luaZ_Fopen(&z, f, source);
|
||||||
|
status = protectedparser(L, &z, bin);
|
||||||
|
if (f != stdin)
|
||||||
|
fclose(f);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_dofile (lua_State *L, const char *filename) {
|
||||||
|
int status = parse_file(L, filename);
|
||||||
|
if (status == 0) /* parse OK? */
|
||||||
|
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int parse_buffer (lua_State *L, const char *buff, size_t size,
|
||||||
|
const char *name) {
|
||||||
|
ZIO z;
|
||||||
|
if (!name) name = "?";
|
||||||
|
luaZ_mopen(&z, buff, size, name);
|
||||||
|
return protectedparser(L, &z, buff[0]==ID_CHUNK);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||||
|
const char *name) {
|
||||||
|
int status = parse_buffer(L, buff, size, name);
|
||||||
|
if (status == 0) /* parse OK? */
|
||||||
|
status = lua_call(L, 0, LUA_MULTRET); /* call main */
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_dostring (lua_State *L, const char *str) {
|
||||||
|
return lua_dobuffer(L, str, strlen(str), str);
|
||||||
|
}
|
||||||
|
|
||||||
31
ldo.h
Normal file
31
ldo.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ldo.h,v 1.23 2000/08/28 17:57:04 roberto Exp roberto $
|
||||||
|
** Stack and Call structure of Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ldo_h
|
||||||
|
#define ldo_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** macro to increment stack top.
|
||||||
|
** There must be always an empty slot at the L->stack.top
|
||||||
|
*/
|
||||||
|
#define incr_top {if (L->top == L->stack_last) luaD_checkstack(L, 1); L->top++;}
|
||||||
|
|
||||||
|
|
||||||
|
void luaD_init (lua_State *L, int stacksize);
|
||||||
|
void luaD_adjusttop (lua_State *L, StkId base, int extra);
|
||||||
|
void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
|
||||||
|
void luaD_call (lua_State *L, StkId func, int nResults);
|
||||||
|
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults);
|
||||||
|
void luaD_breakrun (lua_State *L, int errcode);
|
||||||
|
void luaD_checkstack (lua_State *L, int n);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
271
lex.c
271
lex.c
@@ -1,271 +0,0 @@
|
|||||||
char *rcs_lex = "$Id: lex.c,v 2.13 1994/12/20 21:20:36 roberto Exp celes $";
|
|
||||||
|
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "tree.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "parser.h"
|
|
||||||
#include "ugly.h"
|
|
||||||
|
|
||||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
|
||||||
|
|
||||||
#define next() { current = input(); }
|
|
||||||
#define save(x) { *yytextLast++ = (x); }
|
|
||||||
#define save_and_next() { save(current); next(); }
|
|
||||||
|
|
||||||
static int current;
|
|
||||||
static char yytext[256];
|
|
||||||
static char *yytextLast;
|
|
||||||
|
|
||||||
static Input input;
|
|
||||||
|
|
||||||
void lua_setinput (Input fn)
|
|
||||||
{
|
|
||||||
current = ' ';
|
|
||||||
input = fn;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *lua_lasttext (void)
|
|
||||||
{
|
|
||||||
*yytextLast = 0;
|
|
||||||
return yytext;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* The reserved words must be listed in lexicographic order */
|
|
||||||
static struct
|
|
||||||
{
|
|
||||||
char *name;
|
|
||||||
int token;
|
|
||||||
} reserved [] = {
|
|
||||||
{"and", AND},
|
|
||||||
{"do", DO},
|
|
||||||
{"else", ELSE},
|
|
||||||
{"elseif", ELSEIF},
|
|
||||||
{"end", END},
|
|
||||||
{"function", FUNCTION},
|
|
||||||
{"if", IF},
|
|
||||||
{"local", LOCAL},
|
|
||||||
{"nil", NIL},
|
|
||||||
{"not", NOT},
|
|
||||||
{"or", OR},
|
|
||||||
{"repeat", REPEAT},
|
|
||||||
{"return", RETURN},
|
|
||||||
{"then", THEN},
|
|
||||||
{"until", UNTIL},
|
|
||||||
{"while", WHILE} };
|
|
||||||
|
|
||||||
|
|
||||||
#define RESERVEDSIZE (sizeof(reserved)/sizeof(reserved[0]))
|
|
||||||
|
|
||||||
|
|
||||||
static int findReserved (char *name)
|
|
||||||
{
|
|
||||||
int l = 0;
|
|
||||||
int h = RESERVEDSIZE - 1;
|
|
||||||
while (l <= h)
|
|
||||||
{
|
|
||||||
int m = (l+h)/2;
|
|
||||||
int comp = lua_strcmp(name, reserved[m].name);
|
|
||||||
if (comp < 0)
|
|
||||||
h = m-1;
|
|
||||||
else if (comp == 0)
|
|
||||||
return reserved[m].token;
|
|
||||||
else
|
|
||||||
l = m+1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int yylex (void)
|
|
||||||
{
|
|
||||||
float a;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
yytextLast = yytext;
|
|
||||||
#if 0
|
|
||||||
fprintf(stderr,"'%c' %d\n",current,current);
|
|
||||||
#endif
|
|
||||||
switch (current)
|
|
||||||
{
|
|
||||||
case EOF:
|
|
||||||
case 0:
|
|
||||||
return 0;
|
|
||||||
case '\n': lua_linenumber++;
|
|
||||||
case ' ':
|
|
||||||
case '\t':
|
|
||||||
next();
|
|
||||||
continue;
|
|
||||||
|
|
||||||
case '$':
|
|
||||||
next();
|
|
||||||
while (isalnum(current) || current == '_')
|
|
||||||
save_and_next();
|
|
||||||
*yytextLast = 0;
|
|
||||||
if (lua_strcmp(yytext, "debug") == 0)
|
|
||||||
{
|
|
||||||
yylval.vInt = 1;
|
|
||||||
return DEBUG;
|
|
||||||
}
|
|
||||||
else if (lua_strcmp(yytext, "nodebug") == 0)
|
|
||||||
{
|
|
||||||
yylval.vInt = 0;
|
|
||||||
return DEBUG;
|
|
||||||
}
|
|
||||||
return WRONGTOKEN;
|
|
||||||
|
|
||||||
case '-':
|
|
||||||
save_and_next();
|
|
||||||
if (current != '-') return '-';
|
|
||||||
do { next(); } while (current != '\n' && current != 0);
|
|
||||||
continue;
|
|
||||||
|
|
||||||
case '=':
|
|
||||||
save_and_next();
|
|
||||||
if (current != '=') return '=';
|
|
||||||
else { save_and_next(); return EQ; }
|
|
||||||
|
|
||||||
case '<':
|
|
||||||
save_and_next();
|
|
||||||
if (current != '=') return '<';
|
|
||||||
else { save_and_next(); return LE; }
|
|
||||||
|
|
||||||
case '>':
|
|
||||||
save_and_next();
|
|
||||||
if (current != '=') return '>';
|
|
||||||
else { save_and_next(); return GE; }
|
|
||||||
|
|
||||||
case '~':
|
|
||||||
save_and_next();
|
|
||||||
if (current != '=') return '~';
|
|
||||||
else { save_and_next(); return NE; }
|
|
||||||
|
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
{
|
|
||||||
int del = current;
|
|
||||||
next(); /* skip the delimiter */
|
|
||||||
while (current != del)
|
|
||||||
{
|
|
||||||
switch (current)
|
|
||||||
{
|
|
||||||
case EOF:
|
|
||||||
case 0:
|
|
||||||
case '\n':
|
|
||||||
return WRONGTOKEN;
|
|
||||||
case '\\':
|
|
||||||
next(); /* do not save the '\' */
|
|
||||||
switch (current)
|
|
||||||
{
|
|
||||||
case 'n': save('\n'); next(); break;
|
|
||||||
case 't': save('\t'); next(); break;
|
|
||||||
case 'r': save('\r'); next(); break;
|
|
||||||
default : save(current); next(); break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
save_and_next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next(); /* skip the delimiter */
|
|
||||||
*yytextLast = 0;
|
|
||||||
yylval.vWord = luaI_findconstant(lua_constcreate(yytext));
|
|
||||||
return STRING;
|
|
||||||
}
|
|
||||||
|
|
||||||
case 'a': case 'b': case 'c': case 'd': case 'e':
|
|
||||||
case 'f': case 'g': case 'h': case 'i': case 'j':
|
|
||||||
case 'k': case 'l': case 'm': case 'n': case 'o':
|
|
||||||
case 'p': case 'q': case 'r': case 's': case 't':
|
|
||||||
case 'u': case 'v': case 'w': case 'x': case 'y':
|
|
||||||
case 'z':
|
|
||||||
case 'A': case 'B': case 'C': case 'D': case 'E':
|
|
||||||
case 'F': case 'G': case 'H': case 'I': case 'J':
|
|
||||||
case 'K': case 'L': case 'M': case 'N': case 'O':
|
|
||||||
case 'P': case 'Q': case 'R': case 'S': case 'T':
|
|
||||||
case 'U': case 'V': case 'W': case 'X': case 'Y':
|
|
||||||
case 'Z':
|
|
||||||
case '_':
|
|
||||||
{
|
|
||||||
Word res;
|
|
||||||
do { save_and_next(); } while (isalnum(current) || current == '_');
|
|
||||||
*yytextLast = 0;
|
|
||||||
res = findReserved(yytext);
|
|
||||||
if (res) return res;
|
|
||||||
yylval.pNode = lua_constcreate(yytext);
|
|
||||||
return NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
case '.':
|
|
||||||
save_and_next();
|
|
||||||
if (current == '.')
|
|
||||||
{
|
|
||||||
save_and_next();
|
|
||||||
return CONC;
|
|
||||||
}
|
|
||||||
else if (!isdigit(current)) return '.';
|
|
||||||
/* current is a digit: goes through to number */
|
|
||||||
a=0.0;
|
|
||||||
goto fraction;
|
|
||||||
|
|
||||||
case '0': case '1': case '2': case '3': case '4':
|
|
||||||
case '5': case '6': case '7': case '8': case '9':
|
|
||||||
a=0.0;
|
|
||||||
do { a=10*a+current-'0'; save_and_next(); } while (isdigit(current));
|
|
||||||
if (current == '.') save_and_next();
|
|
||||||
fraction:
|
|
||||||
{ float da=0.1;
|
|
||||||
while (isdigit(current))
|
|
||||||
{a+=(current-'0')*da; da/=10.0; save_and_next()};
|
|
||||||
if (current == 'e' || current == 'E')
|
|
||||||
{
|
|
||||||
int e=0;
|
|
||||||
int neg;
|
|
||||||
float ea;
|
|
||||||
save_and_next();
|
|
||||||
neg=(current=='-');
|
|
||||||
if (current == '+' || current == '-') save_and_next();
|
|
||||||
if (!isdigit(current)) return WRONGTOKEN;
|
|
||||||
do { e=10*e+current-'0'; save_and_next(); } while (isdigit(current));
|
|
||||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
|
||||||
{
|
|
||||||
if (e & 1) a*=ea;
|
|
||||||
ea*=ea;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
yylval.vFloat = a;
|
|
||||||
return NUMBER;
|
|
||||||
}
|
|
||||||
|
|
||||||
case U_and: case U_do: case U_else: case U_elseif: case U_end:
|
|
||||||
case U_function: case U_if: case U_local: case U_nil: case U_not:
|
|
||||||
case U_or: case U_repeat: case U_return: case U_then:
|
|
||||||
case U_until: case U_while:
|
|
||||||
{
|
|
||||||
int old = current;
|
|
||||||
next();
|
|
||||||
return reserved[old-U_and].token;
|
|
||||||
}
|
|
||||||
|
|
||||||
case U_eq: next(); return EQ;
|
|
||||||
case U_le: next(); return LE;
|
|
||||||
case U_ge: next(); return GE;
|
|
||||||
case U_ne: next(); return NE;
|
|
||||||
case U_sc: next(); return CONC;
|
|
||||||
|
|
||||||
default: /* also end of file */
|
|
||||||
{
|
|
||||||
save_and_next();
|
|
||||||
return yytext[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
88
lfunc.c
Normal file
88
lfunc.c
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lfunc.c,v 1.29 2000/08/09 19:16:57 roberto Exp roberto $
|
||||||
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
#define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto))
|
||||||
|
#define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Closure *luaF_newclosure (lua_State *L, int nelems) {
|
||||||
|
Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) +
|
||||||
|
(lint32)sizeof(TObject)*(nelems-1));
|
||||||
|
c->next = L->rootcl;
|
||||||
|
L->rootcl = c;
|
||||||
|
c->mark = c;
|
||||||
|
c->nupvalues = nelems;
|
||||||
|
L->nblocks += gcsizeclosure(L, c);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Proto *luaF_newproto (lua_State *L) {
|
||||||
|
Proto *f = luaM_new(L, Proto);
|
||||||
|
f->code = NULL;
|
||||||
|
f->lineinfo = NULL;
|
||||||
|
f->lineDefined = 0;
|
||||||
|
f->source = NULL;
|
||||||
|
f->kstr = NULL;
|
||||||
|
f->nkstr = 0;
|
||||||
|
f->knum = NULL;
|
||||||
|
f->nknum = 0;
|
||||||
|
f->kproto = NULL;
|
||||||
|
f->nkproto = 0;
|
||||||
|
f->locvars = NULL;
|
||||||
|
f->nlocvars = 0;
|
||||||
|
f->next = L->rootproto;
|
||||||
|
L->rootproto = f;
|
||||||
|
f->marked = 0;
|
||||||
|
L->nblocks += gcsizeproto(L, f);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaF_freeproto (lua_State *L, Proto *f) {
|
||||||
|
L->nblocks -= gcsizeproto(L, f);
|
||||||
|
luaM_free(L, f->code);
|
||||||
|
luaM_free(L, f->locvars);
|
||||||
|
luaM_free(L, f->kstr);
|
||||||
|
luaM_free(L, f->knum);
|
||||||
|
luaM_free(L, f->kproto);
|
||||||
|
luaM_free(L, f->lineinfo);
|
||||||
|
luaM_free(L, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaF_freeclosure (lua_State *L, Closure *c) {
|
||||||
|
L->nblocks -= gcsizeclosure(L, c);
|
||||||
|
luaM_free(L, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Look for n-th local variable at line `line' in function `func'.
|
||||||
|
** Returns NULL if not found.
|
||||||
|
*/
|
||||||
|
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) {
|
||||||
|
if (pc < f->locvars[i].endpc) { /* is variable active? */
|
||||||
|
local_number--;
|
||||||
|
if (local_number == 0)
|
||||||
|
return f->locvars[i].varname->str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL; /* not found */
|
||||||
|
}
|
||||||
|
|
||||||
23
lfunc.h
Normal file
23
lfunc.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lfunc.h,v 1.11 2000/03/10 18:37:44 roberto Exp roberto $
|
||||||
|
** Auxiliary functions to manipulate prototypes and closures
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lfunc_h
|
||||||
|
#define lfunc_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Proto *luaF_newproto (lua_State *L);
|
||||||
|
Closure *luaF_newclosure (lua_State *L, int nelems);
|
||||||
|
void luaF_freeproto (lua_State *L, Proto *f);
|
||||||
|
void luaF_freeclosure (lua_State *L, Closure *c);
|
||||||
|
|
||||||
|
const char *luaF_getlocalname (const Proto *func, int local_number, int pc);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
358
lgc.c
Normal file
358
lgc.c
Normal file
@@ -0,0 +1,358 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lgc.c,v 1.65 2000/09/11 17:38:42 roberto Exp roberto $
|
||||||
|
** Garbage Collector
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lgc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct GCState {
|
||||||
|
Hash *tmark; /* list of marked tables to be visited */
|
||||||
|
Closure *cmark; /* list of marked closures to be visited */
|
||||||
|
} GCState;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int markobject (GCState *st, TObject *o);
|
||||||
|
|
||||||
|
|
||||||
|
/* mark a string; marks larger than 1 cannot be changed */
|
||||||
|
#define strmark(s) {if ((s)->marked == 0) (s)->marked = 1;}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void protomark (Proto *f) {
|
||||||
|
if (!f->marked) {
|
||||||
|
int i;
|
||||||
|
f->marked = 1;
|
||||||
|
strmark(f->source);
|
||||||
|
for (i=0; i<f->nkstr; i++)
|
||||||
|
strmark(f->kstr[i]);
|
||||||
|
for (i=0; i<f->nkproto; i++)
|
||||||
|
protomark(f->kproto[i]);
|
||||||
|
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
|
||||||
|
strmark(f->locvars[i].varname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void markstack (lua_State *L, GCState *st) {
|
||||||
|
StkId o;
|
||||||
|
for (o=L->stack; o<L->top; o++)
|
||||||
|
markobject(st, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void marklock (lua_State *L, GCState *st) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<L->refSize; i++) {
|
||||||
|
if (L->refArray[i].st == LOCK)
|
||||||
|
markobject(st, &L->refArray[i].o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void marktagmethods (lua_State *L, GCState *st) {
|
||||||
|
int e;
|
||||||
|
for (e=0; e<IM_N; e++) {
|
||||||
|
int t;
|
||||||
|
for (t=0; t<=L->last_tag; t++)
|
||||||
|
markobject(st, luaT_getim(L, t,e));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int markobject (GCState *st, TObject *o) {
|
||||||
|
switch (ttype(o)) {
|
||||||
|
case TAG_USERDATA: case TAG_STRING:
|
||||||
|
strmark(tsvalue(o));
|
||||||
|
break;
|
||||||
|
case TAG_TABLE: {
|
||||||
|
if (!ismarked(hvalue(o))) {
|
||||||
|
hvalue(o)->mark = st->tmark; /* chain it in list of marked */
|
||||||
|
st->tmark = hvalue(o);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TAG_LMARK: {
|
||||||
|
Closure *cl = infovalue(o)->func;
|
||||||
|
if (!ismarked(cl)) {
|
||||||
|
protomark(cl->f.l);
|
||||||
|
cl->mark = st->cmark; /* chain it for later traversal */
|
||||||
|
st->cmark = cl;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case TAG_LCLOSURE:
|
||||||
|
protomark(clvalue(o)->f.l);
|
||||||
|
/* go through */
|
||||||
|
case TAG_CCLOSURE: case TAG_CMARK:
|
||||||
|
if (!ismarked(clvalue(o))) {
|
||||||
|
clvalue(o)->mark = st->cmark; /* chain it for later traversal */
|
||||||
|
st->cmark = clvalue(o);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default: break; /* numbers, etc */
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void markall (lua_State *L) {
|
||||||
|
GCState st;
|
||||||
|
st.cmark = NULL;
|
||||||
|
st.tmark = L->gt; /* put table of globals in mark list */
|
||||||
|
L->gt->mark = NULL;
|
||||||
|
marktagmethods(L, &st); /* mark tag methods */
|
||||||
|
markstack(L, &st); /* mark stack objects */
|
||||||
|
marklock(L, &st); /* mark locked objects */
|
||||||
|
for (;;) { /* mark tables and closures */
|
||||||
|
if (st.cmark) {
|
||||||
|
int i;
|
||||||
|
Closure *f = st.cmark; /* get first closure from list */
|
||||||
|
st.cmark = f->mark; /* remove it from list */
|
||||||
|
for (i=0; i<f->nupvalues; i++) /* mark its upvalues */
|
||||||
|
markobject(&st, &f->upvalue[i]);
|
||||||
|
}
|
||||||
|
else if (st.tmark) {
|
||||||
|
int i;
|
||||||
|
Hash *h = st.tmark; /* get first table from list */
|
||||||
|
st.tmark = h->mark; /* remove it from list */
|
||||||
|
for (i=0; i<h->size; i++) {
|
||||||
|
Node *n = node(h, i);
|
||||||
|
if (ttype(key(n)) != TAG_NIL) {
|
||||||
|
if (ttype(val(n)) == TAG_NIL)
|
||||||
|
luaH_remove(h, key(n)); /* dead element; try to remove it */
|
||||||
|
markobject(&st, &n->key);
|
||||||
|
markobject(&st, &n->val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else break; /* nothing else to mark */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int hasmark (const TObject *o) {
|
||||||
|
/* valid only for locked objects */
|
||||||
|
switch (o->ttype) {
|
||||||
|
case TAG_STRING: case TAG_USERDATA:
|
||||||
|
return tsvalue(o)->marked;
|
||||||
|
case TAG_TABLE:
|
||||||
|
return ismarked(hvalue(o));
|
||||||
|
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||||
|
return ismarked(clvalue(o)->mark);
|
||||||
|
default: /* number */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* macro for internal debugging; check if a link of free refs is valid */
|
||||||
|
#define VALIDLINK(L, st,n) (NONEXT <= (st) && (st) < (n))
|
||||||
|
|
||||||
|
static void invalidaterefs (lua_State *L) {
|
||||||
|
int n = L->refSize;
|
||||||
|
int i;
|
||||||
|
for (i=0; i<n; i++) {
|
||||||
|
struct Ref *r = &L->refArray[i];
|
||||||
|
if (r->st == HOLD && !hasmark(&r->o))
|
||||||
|
r->st = COLLECTED;
|
||||||
|
LUA_ASSERT((r->st == LOCK && hasmark(&r->o)) ||
|
||||||
|
(r->st == HOLD && hasmark(&r->o)) ||
|
||||||
|
r->st == COLLECTED ||
|
||||||
|
r->st == NONEXT ||
|
||||||
|
(r->st < n && VALIDLINK(L, L->refArray[r->st].st, n)),
|
||||||
|
"inconsistent ref table");
|
||||||
|
}
|
||||||
|
LUA_ASSERT(VALIDLINK(L, L->refFree, n), "inconsistent ref table");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void collectproto (lua_State *L) {
|
||||||
|
Proto **p = &L->rootproto;
|
||||||
|
Proto *next;
|
||||||
|
while ((next = *p) != NULL) {
|
||||||
|
if (next->marked) {
|
||||||
|
next->marked = 0;
|
||||||
|
p = &next->next;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*p = next->next;
|
||||||
|
luaF_freeproto(L, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void collectclosure (lua_State *L) {
|
||||||
|
Closure **p = &L->rootcl;
|
||||||
|
Closure *next;
|
||||||
|
while ((next = *p) != NULL) {
|
||||||
|
if (ismarked(next)) {
|
||||||
|
next->mark = next; /* unmark */
|
||||||
|
p = &next->next;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*p = next->next;
|
||||||
|
luaF_freeclosure(L, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void collecttable (lua_State *L) {
|
||||||
|
Hash **p = &L->roottable;
|
||||||
|
Hash *next;
|
||||||
|
while ((next = *p) != NULL) {
|
||||||
|
if (ismarked(next)) {
|
||||||
|
next->mark = next; /* unmark */
|
||||||
|
p = &next->next;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
*p = next->next;
|
||||||
|
luaH_free(L, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void checktab (lua_State *L, stringtable *tb) {
|
||||||
|
if (tb->nuse < (lint32)(tb->size/4) && tb->size > 10)
|
||||||
|
luaS_resize(L, tb, tb->size/2); /* table is too big */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** collect all elements with `marked' <= `limit'.
|
||||||
|
** with limit=0, that means all unmarked elements;
|
||||||
|
** with limit=MAX_INT, that means all elements.
|
||||||
|
*/
|
||||||
|
static void collectstringtab (lua_State *L, int limit) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<L->strt.size; i++) { /* for each list */
|
||||||
|
TString **p = &L->strt.hash[i];
|
||||||
|
TString *next;
|
||||||
|
while ((next = *p) != NULL) {
|
||||||
|
if ((int)next->marked > limit) { /* preserve? */
|
||||||
|
if (next->marked < FIXMARK) /* does not change FIXMARKs */
|
||||||
|
next->marked = 0;
|
||||||
|
p = &next->nexthash;
|
||||||
|
}
|
||||||
|
else { /* collect */
|
||||||
|
*p = next->nexthash;
|
||||||
|
L->strt.nuse--;
|
||||||
|
L->nblocks -= gcsizestring(L, next->u.s.len);
|
||||||
|
luaM_free(L, next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checktab(L, &L->strt);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void collectudatatab (lua_State *L, int all) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<L->udt.size; i++) { /* for each list */
|
||||||
|
TString **p = &L->udt.hash[i];
|
||||||
|
TString *next;
|
||||||
|
while ((next = *p) != NULL) {
|
||||||
|
LUA_ASSERT(next->marked <= 1, "udata cannot be fixed");
|
||||||
|
if ((int)next->marked > all) { /* preserve? */
|
||||||
|
next->marked = 0;
|
||||||
|
p = &next->nexthash;
|
||||||
|
}
|
||||||
|
else { /* collect */
|
||||||
|
int tag = next->u.d.tag;
|
||||||
|
if (tag > L->last_tag) tag = TAG_USERDATA;
|
||||||
|
*p = next->nexthash;
|
||||||
|
next->nexthash = L->IMtable[tag].collected; /* chain udata */
|
||||||
|
L->IMtable[tag].collected = next;
|
||||||
|
L->nblocks -= gcsizeudata;
|
||||||
|
L->udt.nuse--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
checktab(L, &L->udt);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void callgcTM (lua_State *L, const TObject *o) {
|
||||||
|
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
||||||
|
if (ttype(im) != TAG_NIL) {
|
||||||
|
luaD_checkstack(L, 2);
|
||||||
|
*(L->top) = *im;
|
||||||
|
*(L->top+1) = *o;
|
||||||
|
L->top += 2;
|
||||||
|
luaD_call(L, L->top-2, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void callgcTMudata (lua_State *L) {
|
||||||
|
int tag;
|
||||||
|
TObject o;
|
||||||
|
ttype(&o) = TAG_USERDATA;
|
||||||
|
for (tag=L->last_tag; tag>=0; tag--) {
|
||||||
|
TString *udata = L->IMtable[tag].collected;
|
||||||
|
L->IMtable[tag].collected = NULL;
|
||||||
|
while (udata) {
|
||||||
|
TString *next = udata->nexthash;
|
||||||
|
tsvalue(&o) = udata;
|
||||||
|
callgcTM(L, &o);
|
||||||
|
luaM_free(L, udata);
|
||||||
|
udata = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaC_collect (lua_State *L, int all) {
|
||||||
|
int oldah = L->allowhooks;
|
||||||
|
L->allowhooks = 0; /* stop debug hooks during GC */
|
||||||
|
L->GCthreshold *= 4; /* to avoid GC during GC */
|
||||||
|
collectudatatab(L, all);
|
||||||
|
callgcTMudata(L);
|
||||||
|
collectstringtab(L, all?MAX_INT:0);
|
||||||
|
collecttable(L);
|
||||||
|
collectproto(L);
|
||||||
|
collectclosure(L);
|
||||||
|
L->allowhooks = oldah; /* restore hooks */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define MINBUFFER 256
|
||||||
|
|
||||||
|
long lua_collectgarbage (lua_State *L, long limit) {
|
||||||
|
unsigned long recovered = L->nblocks; /* to subtract `nblocks' after gc */
|
||||||
|
markall(L);
|
||||||
|
invalidaterefs(L);
|
||||||
|
luaC_collect(L, 0);
|
||||||
|
recovered = recovered - L->nblocks;
|
||||||
|
L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit;
|
||||||
|
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
|
||||||
|
L->Mbuffsize /= 2; /* still larger than MINBUFFER */
|
||||||
|
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
||||||
|
}
|
||||||
|
callgcTM(L, &luaO_nilobject);
|
||||||
|
return recovered;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaC_checkGC (lua_State *L) {
|
||||||
|
if (L->nblocks >= L->GCthreshold)
|
||||||
|
lua_collectgarbage(L, 0);
|
||||||
|
}
|
||||||
|
|
||||||
18
lgc.h
Normal file
18
lgc.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lgc.h,v 1.6 1999/10/04 17:51:04 roberto Exp roberto $
|
||||||
|
** Garbage Collector
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lgc_h
|
||||||
|
#define lgc_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
|
void luaC_checkGC (lua_State *L);
|
||||||
|
void luaC_collect (lua_State *L, int all);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
8
linit.c
Normal file
8
linit.c
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/*
|
||||||
|
** $Id: linit.c,v 1.5 2000/06/16 17:22:43 roberto Exp roberto $
|
||||||
|
** Initialization of libraries for lua.c
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
>>>>>>> This module is now obsolete, and is not part of Lua. <<<<<<<<<
|
||||||
|
|
||||||
728
liolib.c
Normal file
728
liolib.c
Normal file
@@ -0,0 +1,728 @@
|
|||||||
|
/*
|
||||||
|
** $Id: liolib.c,v 1.83 2000/09/13 20:12:14 roberto Exp roberto $
|
||||||
|
** Standard I/O (and system) library
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef OLD_ANSI
|
||||||
|
#include <errno.h>
|
||||||
|
#include <locale.h>
|
||||||
|
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||||
|
#define free(b) if (b) (free)(b)
|
||||||
|
#else
|
||||||
|
/* no support for locale and for strerror: fake them */
|
||||||
|
#define setlocale(a,b) ((void)a, strcmp((b),"C")==0?"C":NULL)
|
||||||
|
#define LC_ALL 0
|
||||||
|
#define LC_COLLATE 0
|
||||||
|
#define LC_CTYPE 0
|
||||||
|
#define LC_MONETARY 0
|
||||||
|
#define LC_NUMERIC 0
|
||||||
|
#define LC_TIME 0
|
||||||
|
#define strerror(e) "generic I/O error"
|
||||||
|
#define errno (-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef POPEN
|
||||||
|
/* FILE *popen();
|
||||||
|
int pclose(); */
|
||||||
|
#define CLOSEFILE(L, f) ((pclose(f) == -1) ? fclose(f) : 0)
|
||||||
|
#else
|
||||||
|
/* no support for popen */
|
||||||
|
#define popen(x,y) NULL /* that is, popen always fails */
|
||||||
|
#define CLOSEFILE(L, f) (fclose(f))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#define INFILE 0
|
||||||
|
#define OUTFILE 1
|
||||||
|
|
||||||
|
typedef struct IOCtrl {
|
||||||
|
int ref[2]; /* ref for strings _INPUT/_OUTPUT */
|
||||||
|
int iotag; /* tag for file handles */
|
||||||
|
int closedtag; /* tag for closed handles */
|
||||||
|
} IOCtrl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static const char *const filenames[] = {"_INPUT", "_OUTPUT"};
|
||||||
|
|
||||||
|
|
||||||
|
static int pushresult (lua_State *L, int i) {
|
||||||
|
if (i) {
|
||||||
|
lua_pushuserdata(L, NULL);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, strerror(errno));
|
||||||
|
lua_pushnumber(L, errno);
|
||||||
|
return 3;;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** FILE Operations
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) {
|
||||||
|
void *p = lua_touserdata(L, f);
|
||||||
|
if (p != NULL) { /* is `f' a userdata ? */
|
||||||
|
int ftag = lua_tag(L, f);
|
||||||
|
if (ftag == ctrl->iotag) /* does it have the correct tag? */
|
||||||
|
return (FILE *)p;
|
||||||
|
else if (ftag == ctrl->closedtag)
|
||||||
|
lua_error(L, "cannot access a closed file");
|
||||||
|
/* else go through */
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) {
|
||||||
|
FILE *f = gethandle(L, ctrl, arg);
|
||||||
|
luaL_arg_check(L, f, arg, "invalid file handle");
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static FILE *getfilebyref (lua_State *L, IOCtrl *ctrl, int inout) {
|
||||||
|
FILE *f;
|
||||||
|
lua_getglobals(L);
|
||||||
|
lua_getref(L, ctrl->ref[inout]);
|
||||||
|
lua_rawget(L, -2);
|
||||||
|
f = gethandle(L, ctrl, -1);
|
||||||
|
if (f == NULL)
|
||||||
|
luaL_verror(L, "global variable `%.10s' is not a file handle",
|
||||||
|
filenames[inout]);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void setfilebyname (lua_State *L, IOCtrl *ctrl, FILE *f,
|
||||||
|
const char *name) {
|
||||||
|
lua_pushusertag(L, f, ctrl->iotag);
|
||||||
|
lua_setglobal(L, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define setfile(L,ctrl,f,inout) (setfilebyname(L,ctrl,f,filenames[inout]))
|
||||||
|
|
||||||
|
|
||||||
|
static int setreturn (lua_State *L, IOCtrl *ctrl, FILE *f, int inout) {
|
||||||
|
if (f == NULL)
|
||||||
|
return pushresult(L, 0);
|
||||||
|
else {
|
||||||
|
setfile(L, ctrl, f, inout);
|
||||||
|
lua_pushusertag(L, f, ctrl->iotag);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int closefile (lua_State *L, IOCtrl *ctrl, FILE *f) {
|
||||||
|
if (f == stdin || f == stdout || f == stderr)
|
||||||
|
return 1;
|
||||||
|
else {
|
||||||
|
lua_pushusertag(L, f, ctrl->iotag);
|
||||||
|
lua_settag(L, ctrl->closedtag);
|
||||||
|
return (CLOSEFILE(L, f) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_close (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
return pushresult(L, closefile(L, ctrl, getnonullfile(L, ctrl, 1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int file_collect (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
if (ctrl == (IOCtrl *)lua_touserdata(L, 1)) {
|
||||||
|
/* collecting `ctrl' itself */
|
||||||
|
lua_unref(L, ctrl->ref[INFILE]);
|
||||||
|
lua_unref(L, ctrl->ref[OUTFILE]);
|
||||||
|
free(ctrl);
|
||||||
|
}
|
||||||
|
else { /* collecting a file: Close it */
|
||||||
|
FILE *f = getnonullfile(L, ctrl, 1);
|
||||||
|
if (f != stdin && f != stdout && f != stderr)
|
||||||
|
CLOSEFILE(L, f);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_open (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
FILE *f;
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
f = fopen(luaL_check_string(L, 1), luaL_check_string(L, 2));
|
||||||
|
if (f) {
|
||||||
|
lua_pushusertag(L, f, ctrl->iotag);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return pushresult(L, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int io_fromto (lua_State *L, int inout, const char *mode) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
FILE *current;
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
if (lua_isnull(L, 1)) {
|
||||||
|
closefile(L, ctrl, getfilebyref(L, ctrl, inout));
|
||||||
|
current = (inout == 0) ? stdin : stdout;
|
||||||
|
}
|
||||||
|
else if (lua_tag(L, 1) == ctrl->iotag) /* deprecated option */
|
||||||
|
current = (FILE *)lua_touserdata(L, 1);
|
||||||
|
else {
|
||||||
|
const char *s = luaL_check_string(L, 1);
|
||||||
|
current = (*s == '|') ? popen(s+1, mode) : fopen(s, mode);
|
||||||
|
}
|
||||||
|
return setreturn(L, ctrl, current, inout);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_readfrom (lua_State *L) {
|
||||||
|
return io_fromto(L, INFILE, "r");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_writeto (lua_State *L) {
|
||||||
|
return io_fromto(L, OUTFILE, "w");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_appendto (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
FILE *current;
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
current = fopen(luaL_check_string(L, 1), "a");
|
||||||
|
return setreturn(L, ctrl, current, OUTFILE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** READ
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef LUA_COMPAT_READPATTERN
|
||||||
|
|
||||||
|
/*
|
||||||
|
** We cannot lookahead without need, because this can lock stdin.
|
||||||
|
** This flag signals when we need to read a next char.
|
||||||
|
*/
|
||||||
|
#define NEED_OTHER (EOF-1) /* just some flag different from EOF */
|
||||||
|
|
||||||
|
|
||||||
|
static int read_pattern (lua_State *L, FILE *f, const char *p) {
|
||||||
|
int inskip = 0; /* {skip} level */
|
||||||
|
int c = NEED_OTHER;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
while (*p != '\0') {
|
||||||
|
switch (*p) {
|
||||||
|
case '{':
|
||||||
|
inskip++;
|
||||||
|
p++;
|
||||||
|
continue;
|
||||||
|
case '}':
|
||||||
|
if (!inskip) lua_error(L, "unbalanced braces in read pattern");
|
||||||
|
inskip--;
|
||||||
|
p++;
|
||||||
|
continue;
|
||||||
|
default: {
|
||||||
|
const char *ep = luaI_classend(L, p); /* get what is next */
|
||||||
|
int m; /* match result */
|
||||||
|
if (c == NEED_OTHER) c = getc(f);
|
||||||
|
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
|
||||||
|
if (m) {
|
||||||
|
if (!inskip) luaL_putchar(&b, c);
|
||||||
|
c = NEED_OTHER;
|
||||||
|
}
|
||||||
|
switch (*ep) {
|
||||||
|
case '+': /* repetition (1 or more) */
|
||||||
|
if (!m) goto break_while; /* pattern fails? */
|
||||||
|
/* else go through */
|
||||||
|
case '*': /* repetition (0 or more) */
|
||||||
|
while (m) { /* reads the same item until it fails */
|
||||||
|
c = getc(f);
|
||||||
|
m = (c==EOF) ? 0 : luaI_singlematch(c, p, ep);
|
||||||
|
if (m && !inskip) luaL_putchar(&b, c);
|
||||||
|
}
|
||||||
|
/* go through to continue reading the pattern */
|
||||||
|
case '?': /* optional */
|
||||||
|
p = ep+1; /* continues reading the pattern */
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
if (!m) goto break_while; /* pattern fails? */
|
||||||
|
p = ep; /* else continues reading the pattern */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} break_while:
|
||||||
|
if (c != NEED_OTHER) ungetc(c, f);
|
||||||
|
luaL_pushresult(&b); /* close buffer */
|
||||||
|
return (*p == '\0');
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define read_pattern(L, f, p) (lua_error(L, "read patterns are deprecated"), 0)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static int read_number (lua_State *L, FILE *f) {
|
||||||
|
double d;
|
||||||
|
if (fscanf(f, "%lf", &d) == 1) {
|
||||||
|
lua_pushnumber(L, d);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else return 0; /* read fails */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int read_word (lua_State *L, FILE *f) {
|
||||||
|
int c;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
do { c = fgetc(f); } while (isspace(c)); /* skip spaces */
|
||||||
|
while (c != EOF && !isspace(c)) {
|
||||||
|
luaL_putchar(&b, c);
|
||||||
|
c = fgetc(f);
|
||||||
|
}
|
||||||
|
ungetc(c, f);
|
||||||
|
luaL_pushresult(&b); /* close buffer */
|
||||||
|
return (lua_strlen(L, 1) > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int read_line (lua_State *L, FILE *f) {
|
||||||
|
int n = 0;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
for (;;) {
|
||||||
|
char *p = luaL_prepbuffer(&b);
|
||||||
|
if (!fgets(p, LUAL_BUFFERSIZE, f)) /* read fails? */
|
||||||
|
break;
|
||||||
|
n = strlen(p);
|
||||||
|
if (p[n-1] != '\n')
|
||||||
|
luaL_addsize(&b, n);
|
||||||
|
else {
|
||||||
|
luaL_addsize(&b, n-1); /* do not add the `\n' */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaL_pushresult(&b); /* close buffer */
|
||||||
|
return (n > 0); /* read something? */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void read_file (lua_State *L, FILE *f) {
|
||||||
|
size_t len = 0;
|
||||||
|
size_t size = BUFSIZ;
|
||||||
|
char *buffer = NULL;
|
||||||
|
for (;;) {
|
||||||
|
buffer = (char *)realloc(buffer, size);
|
||||||
|
if (buffer == NULL)
|
||||||
|
lua_error(L, "not enough memory to read a file");
|
||||||
|
len += fread(buffer+len, sizeof(char), size-len, f);
|
||||||
|
if (len < size) break; /* did not read all it could */
|
||||||
|
size *= 2;
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buffer, len);
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int read_chars (lua_State *L, FILE *f, size_t n) {
|
||||||
|
char *buffer;
|
||||||
|
size_t n1;
|
||||||
|
char statbuff[BUFSIZ];
|
||||||
|
if (n <= BUFSIZ)
|
||||||
|
buffer = statbuff;
|
||||||
|
else {
|
||||||
|
buffer = (char *)malloc(n);
|
||||||
|
if (buffer == NULL)
|
||||||
|
lua_error(L, "not enough memory to read a file");
|
||||||
|
}
|
||||||
|
n1 = fread(buffer, sizeof(char), n, f);
|
||||||
|
lua_pushlstring(L, buffer, n1);
|
||||||
|
if (buffer != statbuff) free(buffer);
|
||||||
|
return (n1 > 0 || n == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_read (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
int lastarg = lua_gettop(L) - 1;
|
||||||
|
int firstarg = 1;
|
||||||
|
FILE *f = gethandle(L, ctrl, firstarg);
|
||||||
|
int n;
|
||||||
|
if (f) firstarg++;
|
||||||
|
else f = getfilebyref(L, ctrl, INFILE); /* get _INPUT */
|
||||||
|
lua_pop(L, 1);
|
||||||
|
if (firstarg > lastarg) { /* no arguments? */
|
||||||
|
lua_settop(L, 0); /* erase upvalue and other eventual garbage */
|
||||||
|
firstarg = lastarg = 1; /* correct indices */
|
||||||
|
lua_pushstring(L, "*l"); /* push default argument */
|
||||||
|
}
|
||||||
|
else /* ensure stack space for all results and for auxlib's buffer */
|
||||||
|
luaL_checkstack(L, lastarg-firstarg+1+LUA_MINSTACK, "too many arguments");
|
||||||
|
for (n = firstarg; n<=lastarg; n++) {
|
||||||
|
int success;
|
||||||
|
if (lua_isnumber(L, n))
|
||||||
|
success = read_chars(L, f, (size_t)lua_tonumber(L, n));
|
||||||
|
else {
|
||||||
|
const char *p = luaL_check_string(L, n);
|
||||||
|
if (p[0] != '*')
|
||||||
|
success = read_pattern(L, f, p); /* deprecated! */
|
||||||
|
else {
|
||||||
|
switch (p[1]) {
|
||||||
|
case 'n': /* number */
|
||||||
|
if (!read_number(L, f)) goto endloop; /* read fails */
|
||||||
|
continue; /* number is already pushed; avoid the "pushstring" */
|
||||||
|
case 'l': /* line */
|
||||||
|
success = read_line(L, f);
|
||||||
|
break;
|
||||||
|
case 'a': /* file */
|
||||||
|
read_file(L, f);
|
||||||
|
success = 1; /* always success */
|
||||||
|
break;
|
||||||
|
case 'w': /* word */
|
||||||
|
success = read_word(L, f);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
luaL_argerror(L, n, "invalid format");
|
||||||
|
success = 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!success) {
|
||||||
|
lua_pop(L, 1); /* remove last result */
|
||||||
|
break; /* read fails */
|
||||||
|
}
|
||||||
|
} endloop:
|
||||||
|
return n - firstarg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
static int io_write (lua_State *L) {
|
||||||
|
int lastarg = lua_gettop(L) - 1;
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
int arg = 1;
|
||||||
|
int status = 1;
|
||||||
|
FILE *f = gethandle(L, ctrl, arg);
|
||||||
|
if (f) arg++;
|
||||||
|
else f = getfilebyref(L, ctrl, OUTFILE); /* get _OUTPUT */
|
||||||
|
for (; arg <= lastarg; arg++) {
|
||||||
|
if (lua_type(L, arg)[2] == 'm') { /* nuMber? */ /* LUA_NUMBER */
|
||||||
|
/* optimization: could be done exactly as for strings */
|
||||||
|
status = status && fprintf(f, "%.16g", lua_tonumber(L, arg)) > 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, arg, &l);
|
||||||
|
status = status && (fwrite(s, sizeof(char), l, f) == l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pushresult(L, status);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_seek (lua_State *L) {
|
||||||
|
static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
|
||||||
|
static const char *const modenames[] = {"set", "cur", "end", NULL};
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
FILE *f;
|
||||||
|
int op;
|
||||||
|
long offset;
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
f = getnonullfile(L, ctrl, 1);
|
||||||
|
op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
|
||||||
|
offset = luaL_opt_long(L, 3, 0);
|
||||||
|
luaL_arg_check(L, op != -1, 2, "invalid mode");
|
||||||
|
op = fseek(f, offset, mode[op]);
|
||||||
|
if (op)
|
||||||
|
return pushresult(L, 0); /* error */
|
||||||
|
else {
|
||||||
|
lua_pushnumber(L, ftell(f));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_flush (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
|
||||||
|
FILE *f;
|
||||||
|
lua_pop(L, 1); /* remove upvalue */
|
||||||
|
f = gethandle(L, ctrl, 1);
|
||||||
|
luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
|
||||||
|
return pushresult(L, fflush(f) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Other O.S. Operations
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int io_execute (lua_State *L) {
|
||||||
|
lua_pushnumber(L, system(luaL_check_string(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_remove (lua_State *L) {
|
||||||
|
return pushresult(L, remove(luaL_check_string(L, 1)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_rename (lua_State *L) {
|
||||||
|
return pushresult(L, rename(luaL_check_string(L, 1),
|
||||||
|
luaL_check_string(L, 2)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_tmpname (lua_State *L) {
|
||||||
|
lua_pushstring(L, tmpnam(NULL));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int io_getenv (lua_State *L) {
|
||||||
|
lua_pushstring(L, getenv(luaL_check_string(L, 1))); /* if NULL push nil */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_clock (lua_State *L) {
|
||||||
|
lua_pushnumber(L, ((double)clock())/CLOCKS_PER_SEC);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_date (lua_State *L) {
|
||||||
|
char b[256];
|
||||||
|
const char *s = luaL_opt_string(L, 1, "%c");
|
||||||
|
struct tm *stm;
|
||||||
|
time_t t;
|
||||||
|
time(&t); stm = localtime(&t);
|
||||||
|
if (strftime(b, sizeof(b), s, stm))
|
||||||
|
lua_pushstring(L, b);
|
||||||
|
else
|
||||||
|
lua_error(L, "invalid `date' format");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int setloc (lua_State *L) {
|
||||||
|
static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
|
||||||
|
LC_NUMERIC, LC_TIME};
|
||||||
|
static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
|
||||||
|
"numeric", "time", NULL};
|
||||||
|
int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
|
||||||
|
luaL_arg_check(L, op != -1, 2, "invalid option");
|
||||||
|
lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int io_exit (lua_State *L) {
|
||||||
|
exit(luaL_opt_int(L, 1, EXIT_SUCCESS));
|
||||||
|
return 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int io_debug (lua_State *L) {
|
||||||
|
for (;;) {
|
||||||
|
char buffer[250];
|
||||||
|
fprintf(stderr, "lua_debug> ");
|
||||||
|
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
|
||||||
|
strcmp(buffer, "cont\n") == 0)
|
||||||
|
return 0;
|
||||||
|
lua_dostring(L, buffer);
|
||||||
|
lua_settop(L, 0); /* remove eventual returns */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define LEVELS1 12 /* size of the first part of the stack */
|
||||||
|
#define LEVELS2 10 /* size of the second part of the stack */
|
||||||
|
|
||||||
|
static int errorfb (lua_State *L) {
|
||||||
|
int level = 1; /* skip level 0 (it's this function) */
|
||||||
|
int firstpart = 1; /* still before eventual `...' */
|
||||||
|
lua_Debug ar;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
luaL_addstring(&b, "error: ");
|
||||||
|
luaL_addstring(&b, luaL_check_string(L, 1));
|
||||||
|
luaL_addstring(&b, "\n");
|
||||||
|
while (lua_getstack(L, level++, &ar)) {
|
||||||
|
char buff[120]; /* enough to fit following `sprintf's */
|
||||||
|
if (level == 2)
|
||||||
|
luaL_addstring(&b, "stack traceback:\n");
|
||||||
|
else if (level > LEVELS1 && firstpart) {
|
||||||
|
/* no more than `LEVELS2' more levels? */
|
||||||
|
if (!lua_getstack(L, level+LEVELS2, &ar))
|
||||||
|
level--; /* keep going */
|
||||||
|
else {
|
||||||
|
luaL_addstring(&b, " ...\n"); /* too many levels */
|
||||||
|
while (lua_getstack(L, level+LEVELS2, &ar)) /* find last levels */
|
||||||
|
level++;
|
||||||
|
}
|
||||||
|
firstpart = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sprintf(buff, "%4d: ", level-1);
|
||||||
|
luaL_addstring(&b, buff);
|
||||||
|
lua_getinfo(L, "Snl", &ar);
|
||||||
|
switch (*ar.namewhat) {
|
||||||
|
case 'g': case 'l': /* global, local */
|
||||||
|
sprintf(buff, "function `%.50s'", ar.name);
|
||||||
|
break;
|
||||||
|
case 'f': /* field */
|
||||||
|
sprintf(buff, "method `%.50s'", ar.name);
|
||||||
|
break;
|
||||||
|
case 't': /* tag method */
|
||||||
|
sprintf(buff, "`%.50s' tag method", ar.name);
|
||||||
|
break;
|
||||||
|
default: {
|
||||||
|
if (*ar.what == 'm') /* main? */
|
||||||
|
sprintf(buff, "main of %.70s", ar.short_src);
|
||||||
|
else if (*ar.what == 'C') /* C function? */
|
||||||
|
sprintf(buff, "%.70s", ar.short_src);
|
||||||
|
else
|
||||||
|
sprintf(buff, "function <%d:%.70s>", ar.linedefined, ar.short_src);
|
||||||
|
ar.source = NULL; /* do not print source again */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaL_addstring(&b, buff);
|
||||||
|
if (ar.currentline > 0) {
|
||||||
|
sprintf(buff, " at line %d", ar.currentline);
|
||||||
|
luaL_addstring(&b, buff);
|
||||||
|
}
|
||||||
|
if (ar.source) {
|
||||||
|
sprintf(buff, " [%.70s]", ar.short_src);
|
||||||
|
luaL_addstring(&b, buff);
|
||||||
|
}
|
||||||
|
luaL_addstring(&b, "\n");
|
||||||
|
}
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
lua_getglobal(L, LUA_ALERT);
|
||||||
|
if (lua_isfunction(L, -1)) { /* avoid loop if _ALERT is not defined */
|
||||||
|
lua_pushvalue(L, -2); /* error message */
|
||||||
|
lua_rawcall(L, 1, 0);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg iolib[] = {
|
||||||
|
{LUA_ERRORMESSAGE, errorfb},
|
||||||
|
{"clock", io_clock},
|
||||||
|
{"date", io_date},
|
||||||
|
{"debug", io_debug},
|
||||||
|
{"execute", io_execute},
|
||||||
|
{"exit", io_exit},
|
||||||
|
{"getenv", io_getenv},
|
||||||
|
{"remove", io_remove},
|
||||||
|
{"rename", io_rename},
|
||||||
|
{"setlocale", setloc},
|
||||||
|
{"tmpname", io_tmpname}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg iolibtag[] = {
|
||||||
|
{"appendto", io_appendto},
|
||||||
|
{"closefile", io_close},
|
||||||
|
{"flush", io_flush},
|
||||||
|
{"openfile", io_open},
|
||||||
|
{"read", io_read},
|
||||||
|
{"readfrom", io_readfrom},
|
||||||
|
{"seek", io_seek},
|
||||||
|
{"write", io_write},
|
||||||
|
{"writeto", io_writeto}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static void openwithcontrol (lua_State *L) {
|
||||||
|
IOCtrl *ctrl = (IOCtrl *)malloc(sizeof(IOCtrl));
|
||||||
|
unsigned int i;
|
||||||
|
int ctrltag = lua_newtag(L);
|
||||||
|
ctrl->iotag = lua_newtag(L);
|
||||||
|
ctrl->closedtag = lua_newtag(L);
|
||||||
|
for (i=0; i<sizeof(iolibtag)/sizeof(iolibtag[0]); i++) {
|
||||||
|
/* put `ctrl' as upvalue for these functions */
|
||||||
|
lua_pushusertag(L, ctrl, ctrltag);
|
||||||
|
lua_pushcclosure(L, iolibtag[i].func, 1);
|
||||||
|
lua_setglobal(L, iolibtag[i].name);
|
||||||
|
}
|
||||||
|
/* create references to variable names */
|
||||||
|
lua_pushstring(L, filenames[INFILE]);
|
||||||
|
ctrl->ref[INFILE] = lua_ref(L, 1);
|
||||||
|
lua_pushstring(L, filenames[OUTFILE]);
|
||||||
|
ctrl->ref[OUTFILE] = lua_ref(L, 1);
|
||||||
|
/* predefined file handles */
|
||||||
|
setfile(L, ctrl, stdin, INFILE);
|
||||||
|
setfile(L, ctrl, stdout, OUTFILE);
|
||||||
|
setfilebyname(L, ctrl, stdin, "_STDIN");
|
||||||
|
setfilebyname(L, ctrl, stdout, "_STDOUT");
|
||||||
|
setfilebyname(L, ctrl, stderr, "_STDERR");
|
||||||
|
/* delete `ctrl' when collected */
|
||||||
|
lua_pushusertag(L, ctrl, ctrltag);
|
||||||
|
lua_pushcclosure(L, file_collect, 1);
|
||||||
|
lua_settagmethod(L, ctrltag, "gc");
|
||||||
|
/* close files when collected */
|
||||||
|
lua_copytagmethods(L, ctrl->iotag, ctrltag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_iolibopen (lua_State *L) {
|
||||||
|
luaL_openl(L, iolib);
|
||||||
|
openwithcontrol(L);
|
||||||
|
}
|
||||||
|
|
||||||
380
llex.c
Normal file
380
llex.c
Normal file
@@ -0,0 +1,380 @@
|
|||||||
|
/*
|
||||||
|
** $Id: llex.c,v 1.69 2000/09/11 17:38:42 roberto Exp roberto $
|
||||||
|
** Lexical Analyzer
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "llex.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lparser.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define next(LS) (LS->current = zgetc(LS->z))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ORDER RESERVED */
|
||||||
|
static const char *const token2string [] = {
|
||||||
|
"and", "break", "do", "else", "elseif", "end", "for",
|
||||||
|
"function", "if", "local", "nil", "not", "or", "repeat", "return", "then",
|
||||||
|
"until", "while", "", "..", "...", "==", ">=", "<=", "~=", "", "", "<eof>"};
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_init (lua_State *L) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<NUM_RESERVED; i++) {
|
||||||
|
TString *ts = luaS_new(L, token2string[i]);
|
||||||
|
ts->marked = (unsigned char)(RESERVEDMARK+i); /* reserved word */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define MAXSRC 80
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
|
||||||
|
if (val > limit) {
|
||||||
|
char buff[100];
|
||||||
|
sprintf(buff, "too many %.50s (limit=%d)", msg, limit);
|
||||||
|
luaX_error(ls, buff, ls->t.token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_syntaxerror (LexState *ls, const char *s, const char *token) {
|
||||||
|
char buff[MAXSRC];
|
||||||
|
luaO_chunkid(buff, ls->source->str, sizeof(buff));
|
||||||
|
luaO_verror(ls->L, "%.100s;\n last token read: `%.50s' at line %d in %.80s",
|
||||||
|
s, token, ls->linenumber, buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_error (LexState *ls, const char *s, int token) {
|
||||||
|
char buff[TOKEN_LEN];
|
||||||
|
luaX_token2str(token, buff);
|
||||||
|
if (buff[0] == '\0')
|
||||||
|
luaX_syntaxerror(ls, s, ls->L->Mbuffer);
|
||||||
|
else
|
||||||
|
luaX_syntaxerror(ls, s, buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_token2str (int token, char *s) {
|
||||||
|
if (token < 256) {
|
||||||
|
s[0] = (char)token;
|
||||||
|
s[1] = '\0';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
strcpy(s, token2string[token-FIRST_RESERVED]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaX_invalidchar (LexState *ls, int c) {
|
||||||
|
char buff[8];
|
||||||
|
sprintf(buff, "0x%02X", c);
|
||||||
|
luaX_syntaxerror(ls, "invalid control char", buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void inclinenumber (LexState *LS) {
|
||||||
|
next(LS); /* skip '\n' */
|
||||||
|
++LS->linenumber;
|
||||||
|
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
|
||||||
|
LS->L = L;
|
||||||
|
LS->lookahead.token = TK_EOS; /* no look-ahead token */
|
||||||
|
LS->z = z;
|
||||||
|
LS->fs = NULL;
|
||||||
|
LS->linenumber = 1;
|
||||||
|
LS->lastline = 1;
|
||||||
|
LS->source = source;
|
||||||
|
next(LS); /* read first char */
|
||||||
|
if (LS->current == '#') {
|
||||||
|
do { /* skip first line */
|
||||||
|
next(LS);
|
||||||
|
} while (LS->current != '\n' && LS->current != EOZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** =======================================================
|
||||||
|
** LEXICAL ANALYZER
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* use Mbuffer to store names, literal strings and numbers */
|
||||||
|
|
||||||
|
#define EXTRABUFF 128
|
||||||
|
#define checkbuffer(L, n, len) if ((len)+(n) > L->Mbuffsize) \
|
||||||
|
luaO_openspace(L, (len)+(n)+EXTRABUFF)
|
||||||
|
|
||||||
|
#define save(L, c, l) (L->Mbuffer[l++] = (char)c)
|
||||||
|
#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
|
||||||
|
|
||||||
|
|
||||||
|
static const char *readname (LexState *LS) {
|
||||||
|
lua_State *L = LS->L;
|
||||||
|
size_t l = 0;
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
do {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
} while (isalnum(LS->current) || LS->current == '_');
|
||||||
|
save(L, '\0', l);
|
||||||
|
return L->Mbuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* LUA_NUMBER */
|
||||||
|
static void read_number (LexState *LS, int comma) {
|
||||||
|
lua_State *L = LS->L;
|
||||||
|
size_t l = 0;
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
if (comma) save(L, '.', l);
|
||||||
|
while (isdigit(LS->current)) {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
if (LS->current == '.') {
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
if (LS->current == '.') {
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
save(L, '\0', l);
|
||||||
|
luaX_error(LS, "ambiguous syntax"
|
||||||
|
" (decimal point x string concatenation)", TK_NUMBER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (isdigit(LS->current)) {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
if (LS->current == 'e' || LS->current == 'E') {
|
||||||
|
save_and_next(L, LS, l); /* read 'E' */
|
||||||
|
if (LS->current == '+' || LS->current == '-')
|
||||||
|
save_and_next(L, LS, l); /* optional exponent sign */
|
||||||
|
while (isdigit(LS->current)) {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
save(L, '\0', l);
|
||||||
|
if (!luaO_str2d(L->Mbuffer, &LS->t.seminfo.r))
|
||||||
|
luaX_error(LS, "malformed number", TK_NUMBER);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void read_long_string (LexState *LS) {
|
||||||
|
lua_State *L = LS->L;
|
||||||
|
int cont = 0;
|
||||||
|
size_t l = 0;
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save(L, '[', l); /* save first '[' */
|
||||||
|
save_and_next(L, LS, l); /* pass the second '[' */
|
||||||
|
for (;;) {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
switch (LS->current) {
|
||||||
|
case EOZ:
|
||||||
|
save(L, '\0', l);
|
||||||
|
luaX_error(LS, "unfinished long string", TK_STRING);
|
||||||
|
break; /* to avoid warnings */
|
||||||
|
case '[':
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
if (LS->current == '[') {
|
||||||
|
cont++;
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
case ']':
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
if (LS->current == ']') {
|
||||||
|
if (cont == 0) goto endloop;
|
||||||
|
cont--;
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
case '\n':
|
||||||
|
save(L, '\n', l);
|
||||||
|
inclinenumber(LS);
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
} endloop:
|
||||||
|
save_and_next(L, LS, l); /* skip the second ']' */
|
||||||
|
save(L, '\0', l);
|
||||||
|
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+2, l-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void read_string (LexState *LS, int del) {
|
||||||
|
lua_State *L = LS->L;
|
||||||
|
size_t l = 0;
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
while (LS->current != del) {
|
||||||
|
checkbuffer(L, 10, l);
|
||||||
|
switch (LS->current) {
|
||||||
|
case EOZ: case '\n':
|
||||||
|
save(L, '\0', l);
|
||||||
|
luaX_error(LS, "unfinished string", TK_STRING);
|
||||||
|
break; /* to avoid warnings */
|
||||||
|
case '\\':
|
||||||
|
next(LS); /* do not save the '\' */
|
||||||
|
switch (LS->current) {
|
||||||
|
case 'a': save(L, '\a', l); next(LS); break;
|
||||||
|
case 'b': save(L, '\b', l); next(LS); break;
|
||||||
|
case 'f': save(L, '\f', l); next(LS); break;
|
||||||
|
case 'n': save(L, '\n', l); next(LS); break;
|
||||||
|
case 'r': save(L, '\r', l); next(LS); break;
|
||||||
|
case 't': save(L, '\t', l); next(LS); break;
|
||||||
|
case 'v': save(L, '\v', l); next(LS); break;
|
||||||
|
case '\n': save(L, '\n', l); inclinenumber(LS); break;
|
||||||
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
|
case '5': case '6': case '7': case '8': case '9': {
|
||||||
|
int c = 0;
|
||||||
|
int i = 0;
|
||||||
|
do {
|
||||||
|
c = 10*c + (LS->current-'0');
|
||||||
|
next(LS);
|
||||||
|
} while (++i<3 && isdigit(LS->current));
|
||||||
|
if (c != (unsigned char)c) {
|
||||||
|
save(L, '\0', l);
|
||||||
|
luaX_error(LS, "escape sequence too large", TK_STRING);
|
||||||
|
}
|
||||||
|
save(L, c, l);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: /* handles \\, \", \', and \? */
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
save_and_next(L, LS, l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
save_and_next(L, LS, l); /* skip delimiter */
|
||||||
|
save(L, '\0', l);
|
||||||
|
LS->t.seminfo.ts = luaS_newlstr(L, L->Mbuffer+1, l-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaX_lex (LexState *LS) {
|
||||||
|
for (;;) {
|
||||||
|
switch (LS->current) {
|
||||||
|
|
||||||
|
case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
|
||||||
|
next(LS);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case '\n':
|
||||||
|
inclinenumber(LS);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case '$':
|
||||||
|
luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '-':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '-') return '-';
|
||||||
|
do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
|
||||||
|
continue;
|
||||||
|
|
||||||
|
case '[':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '[') return '[';
|
||||||
|
else {
|
||||||
|
read_long_string(LS);
|
||||||
|
return TK_STRING;
|
||||||
|
}
|
||||||
|
|
||||||
|
case '=':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '=') return '=';
|
||||||
|
else { next(LS); return TK_EQ; }
|
||||||
|
|
||||||
|
case '<':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '=') return '<';
|
||||||
|
else { next(LS); return TK_LE; }
|
||||||
|
|
||||||
|
case '>':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '=') return '>';
|
||||||
|
else { next(LS); return TK_GE; }
|
||||||
|
|
||||||
|
case '~':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current != '=') return '~';
|
||||||
|
else { next(LS); return TK_NE; }
|
||||||
|
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
read_string(LS, LS->current);
|
||||||
|
return TK_STRING;
|
||||||
|
|
||||||
|
case '.':
|
||||||
|
next(LS);
|
||||||
|
if (LS->current == '.') {
|
||||||
|
next(LS);
|
||||||
|
if (LS->current == '.') {
|
||||||
|
next(LS);
|
||||||
|
return TK_DOTS; /* ... */
|
||||||
|
}
|
||||||
|
else return TK_CONCAT; /* .. */
|
||||||
|
}
|
||||||
|
else if (!isdigit(LS->current)) return '.';
|
||||||
|
else {
|
||||||
|
read_number(LS, 1);
|
||||||
|
return TK_NUMBER;
|
||||||
|
}
|
||||||
|
|
||||||
|
case '0': case '1': case '2': case '3': case '4':
|
||||||
|
case '5': case '6': case '7': case '8': case '9':
|
||||||
|
read_number(LS, 0);
|
||||||
|
return TK_NUMBER;
|
||||||
|
|
||||||
|
case EOZ:
|
||||||
|
return TK_EOS;
|
||||||
|
|
||||||
|
case '_': goto tname;
|
||||||
|
|
||||||
|
default:
|
||||||
|
if (!isalpha(LS->current)) {
|
||||||
|
int c = LS->current;
|
||||||
|
if (iscntrl(c))
|
||||||
|
luaX_invalidchar(LS, c);
|
||||||
|
next(LS);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
tname: { /* identifier or reserved word */
|
||||||
|
TString *ts = luaS_new(LS->L, readname(LS));
|
||||||
|
if (ts->marked >= RESERVEDMARK) /* reserved word? */
|
||||||
|
return ts->marked-RESERVEDMARK+FIRST_RESERVED;
|
||||||
|
LS->t.seminfo.ts = ts;
|
||||||
|
return TK_NAME;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
68
llex.h
Normal file
68
llex.h
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
** $Id: llex.h,v 1.29 2000/06/19 18:05:14 roberto Exp roberto $
|
||||||
|
** Lexical Analyzer
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef llex_h
|
||||||
|
#define llex_h
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define FIRST_RESERVED 257
|
||||||
|
|
||||||
|
/* maximum length of a reserved word (+1 for final 0) */
|
||||||
|
#define TOKEN_LEN 15
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* WARNING: if you change the order of this enumeration,
|
||||||
|
* grep "ORDER RESERVED"
|
||||||
|
*/
|
||||||
|
enum RESERVED {
|
||||||
|
/* terminal symbols denoted by reserved words */
|
||||||
|
TK_AND = FIRST_RESERVED, TK_BREAK,
|
||||||
|
TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FOR, TK_FUNCTION, TK_IF, TK_LOCAL,
|
||||||
|
TK_NIL, TK_NOT, TK_OR, TK_REPEAT, TK_RETURN, TK_THEN, TK_UNTIL, TK_WHILE,
|
||||||
|
/* other terminal symbols */
|
||||||
|
TK_NAME, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
|
||||||
|
TK_STRING, TK_EOS
|
||||||
|
};
|
||||||
|
|
||||||
|
/* number of reserved words */
|
||||||
|
#define NUM_RESERVED ((int)(TK_WHILE-FIRST_RESERVED+1))
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Token {
|
||||||
|
int token;
|
||||||
|
union {
|
||||||
|
Number r;
|
||||||
|
TString *ts;
|
||||||
|
} seminfo; /* semantics information */
|
||||||
|
} Token;
|
||||||
|
|
||||||
|
typedef struct LexState {
|
||||||
|
int current; /* current character */
|
||||||
|
Token t; /* current token */
|
||||||
|
Token lookahead; /* look ahead token */
|
||||||
|
struct FuncState *fs; /* `FuncState' is private to the parser */
|
||||||
|
struct lua_State *L;
|
||||||
|
struct zio *z; /* input stream */
|
||||||
|
int linenumber; /* input line counter */
|
||||||
|
int lastline; /* line of last token `consumed' */
|
||||||
|
TString *source; /* current source name */
|
||||||
|
} LexState;
|
||||||
|
|
||||||
|
|
||||||
|
void luaX_init (lua_State *L);
|
||||||
|
void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source);
|
||||||
|
int luaX_lex (LexState *LS);
|
||||||
|
void luaX_checklimit (LexState *ls, int val, int limit, const char *msg);
|
||||||
|
void luaX_syntaxerror (LexState *ls, const char *s, const char *token);
|
||||||
|
void luaX_error (LexState *ls, const char *s, int token);
|
||||||
|
void luaX_token2str (int token, char *s);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
204
llimits.h
Normal file
204
llimits.h
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
/*
|
||||||
|
** $Id: llimits.h,v 1.13 2000/08/28 17:57:04 roberto Exp roberto $
|
||||||
|
** Limits, basic types, and some other "installation-dependent" definitions
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef llimits_h
|
||||||
|
#define llimits_h
|
||||||
|
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** try to find number of bits in an integer
|
||||||
|
*/
|
||||||
|
#ifndef BITS_INT
|
||||||
|
/* avoid overflows in comparison */
|
||||||
|
#if INT_MAX-20 < 32760
|
||||||
|
#define BITS_INT 16
|
||||||
|
#else
|
||||||
|
#if INT_MAX > 2147483640L
|
||||||
|
/* machine has at least 32 bits */
|
||||||
|
#define BITS_INT 32
|
||||||
|
#else
|
||||||
|
#error "you must define BITS_INT with number of bits in an integer"
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Define the type `number' of Lua
|
||||||
|
** GREP LUA_NUMBER to change that
|
||||||
|
*/
|
||||||
|
#ifndef LUA_NUM_TYPE
|
||||||
|
#define LUA_NUM_TYPE double
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef LUA_NUM_TYPE Number;
|
||||||
|
|
||||||
|
|
||||||
|
typedef unsigned long lint32; /* unsigned int with at least 32 bits */
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_SIZET ((size_t)(~(size_t)0)-2)
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */
|
||||||
|
|
||||||
|
/*
|
||||||
|
** conversion of pointer to int (for hashing only)
|
||||||
|
** (the shift removes bits that are usually 0 because of alignment)
|
||||||
|
*/
|
||||||
|
#define IntPoint(p) (((unsigned long)(p)) >> 3)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** number of `blocks' for garbage collection: each reference to other
|
||||||
|
** objects count 1, and each 32 bytes of `raw' memory count 1; we add
|
||||||
|
** 2 to the total as a minimum (and also to count the overhead of malloc)
|
||||||
|
*/
|
||||||
|
#define numblocks(L, o,b) ((o)+((b)>>5)+2)
|
||||||
|
|
||||||
|
|
||||||
|
#define MINPOWER2 4 /* minimum size for "growing" vectors */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef DEFAULT_STACK_SIZE
|
||||||
|
#define DEFAULT_STACK_SIZE 1024
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** type for virtual-machine instructions
|
||||||
|
** must be an unsigned with 4 bytes (see details in lopcodes.h)
|
||||||
|
** For a very small machine, you may change that to 2 bytes (and adjust
|
||||||
|
** the following limits accordingly)
|
||||||
|
*/
|
||||||
|
typedef unsigned long Instruction;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** size and position of opcode arguments.
|
||||||
|
** For an instruction with 2 bytes, size is 16, and size_b can be 5
|
||||||
|
** (accordingly, size_u will be 10, and size_a will be 5)
|
||||||
|
*/
|
||||||
|
#define SIZE_INSTRUCTION 32
|
||||||
|
#define SIZE_B 9
|
||||||
|
|
||||||
|
#define SIZE_OP 6
|
||||||
|
#define SIZE_U (SIZE_INSTRUCTION-SIZE_OP)
|
||||||
|
#define POS_U SIZE_OP
|
||||||
|
#define POS_B SIZE_OP
|
||||||
|
#define SIZE_A (SIZE_INSTRUCTION-(SIZE_OP+SIZE_B))
|
||||||
|
#define POS_A (SIZE_OP+SIZE_B)
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** limits for opcode arguments.
|
||||||
|
** we use (signed) int to manipulate most arguments,
|
||||||
|
** so they must fit in BITS_INT-1 bits (-1 for signal)
|
||||||
|
*/
|
||||||
|
#if SIZE_U < BITS_INT-1
|
||||||
|
#define MAXARG_U ((1<<SIZE_U)-1)
|
||||||
|
#define MAXARG_S (MAXARG_U>>1) /* `S' is signed */
|
||||||
|
#else
|
||||||
|
#define MAXARG_U MAX_INT
|
||||||
|
#define MAXARG_S MAX_INT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if SIZE_A < BITS_INT-1
|
||||||
|
#define MAXARG_A ((1<<SIZE_A)-1)
|
||||||
|
#else
|
||||||
|
#define MAXARG_A MAX_INT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if SIZE_B < BITS_INT-1
|
||||||
|
#define MAXARG_B ((1<<SIZE_B)-1)
|
||||||
|
#else
|
||||||
|
#define MAXARG_B MAX_INT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum stack size in a function */
|
||||||
|
#ifndef MAXSTACK
|
||||||
|
#define MAXSTACK 250
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MAXSTACK > MAXARG_B
|
||||||
|
#undef MAXSTACK
|
||||||
|
#define MAXSTACK MAXARG_B
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum number of local variables */
|
||||||
|
#ifndef MAXLOCALS
|
||||||
|
#define MAXLOCALS 200 /* arbitrary limit (<MAXSTACK) */
|
||||||
|
#endif
|
||||||
|
#if MAXLOCALS>=MAXSTACK
|
||||||
|
#undef MAXLOCALS
|
||||||
|
#define MAXLOCALS (MAXSTACK-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum number of upvalues */
|
||||||
|
#ifndef MAXUPVALUES
|
||||||
|
#define MAXUPVALUES 32 /* arbitrary limit (<=MAXARG_B) */
|
||||||
|
#endif
|
||||||
|
#if MAXUPVALUES>MAXARG_B
|
||||||
|
#undef MAXUPVALUES
|
||||||
|
#define MAXUPVALUES MAXARG_B
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum number of variables in the left side of an assignment */
|
||||||
|
#ifndef MAXVARSLH
|
||||||
|
#define MAXVARSLH 100 /* arbitrary limit (<MULT_RET) */
|
||||||
|
#endif
|
||||||
|
#if MAXVARSLH>=MULT_RET
|
||||||
|
#undef MAXVARSLH
|
||||||
|
#define MAXVARSLH (MULT_RET-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum number of parameters in a function */
|
||||||
|
#ifndef MAXPARAMS
|
||||||
|
#define MAXPARAMS 100 /* arbitrary limit (<MAXLOCALS) */
|
||||||
|
#endif
|
||||||
|
#if MAXPARAMS>=MAXLOCALS
|
||||||
|
#undef MAXPARAMS
|
||||||
|
#define MAXPARAMS (MAXLOCALS-1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* number of list items to accumulate before a SETLIST instruction */
|
||||||
|
#define LFIELDS_PER_FLUSH 64
|
||||||
|
#if LFIELDS_PER_FLUSH>(MAXSTACK/4)
|
||||||
|
#undef LFIELDS_PER_FLUSH
|
||||||
|
#define LFIELDS_PER_FLUSH (MAXSTACK/4)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* number of record items to accumulate before a SETMAP instruction */
|
||||||
|
/* (each item counts 2 elements on the stack: an index and a value) */
|
||||||
|
#define RFIELDS_PER_FLUSH (LFIELDS_PER_FLUSH/2)
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum lookback to find a real constant (for code generation) */
|
||||||
|
#ifndef LOOKBACKNUMS
|
||||||
|
#define LOOKBACKNUMS 20 /* arbitrary constant */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum part of a file name kept for error messages */
|
||||||
|
#ifndef MAXFILENAME
|
||||||
|
#define MAXFILENAME 260
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
239
lmathlib.c
Normal file
239
lmathlib.c
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lmathlib.c,v 1.27 2000/08/28 17:57:04 roberto Exp roberto $
|
||||||
|
** Standard mathematical library
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
#undef PI
|
||||||
|
#define PI (3.14159265358979323846)
|
||||||
|
#define RADIANS_PER_DEGREE (PI/180.0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** If you want Lua to operate in radians (instead of degrees),
|
||||||
|
** define RADIANS
|
||||||
|
*/
|
||||||
|
#ifdef RADIANS
|
||||||
|
#define FROMRAD(a) (a)
|
||||||
|
#define TORAD(a) (a)
|
||||||
|
#else
|
||||||
|
#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
|
||||||
|
#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static int math_abs (lua_State *L) {
|
||||||
|
lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_sin (lua_State *L) {
|
||||||
|
lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_cos (lua_State *L) {
|
||||||
|
lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_tan (lua_State *L) {
|
||||||
|
lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_asin (lua_State *L) {
|
||||||
|
lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_acos (lua_State *L) {
|
||||||
|
lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_atan (lua_State *L) {
|
||||||
|
lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_atan2 (lua_State *L) {
|
||||||
|
lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_ceil (lua_State *L) {
|
||||||
|
lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_floor (lua_State *L) {
|
||||||
|
lua_pushnumber(L, floor(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_mod (lua_State *L) {
|
||||||
|
lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_sqrt (lua_State *L) {
|
||||||
|
lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_pow (lua_State *L) {
|
||||||
|
lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_log (lua_State *L) {
|
||||||
|
lua_pushnumber(L, log(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_log10 (lua_State *L) {
|
||||||
|
lua_pushnumber(L, log10(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_exp (lua_State *L) {
|
||||||
|
lua_pushnumber(L, exp(luaL_check_number(L, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_deg (lua_State *L) {
|
||||||
|
lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_rad (lua_State *L) {
|
||||||
|
lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_frexp (lua_State *L) {
|
||||||
|
int e;
|
||||||
|
lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
|
||||||
|
lua_pushnumber(L, e);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int math_ldexp (lua_State *L) {
|
||||||
|
lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int math_min (lua_State *L) {
|
||||||
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
|
double dmin = luaL_check_number(L, 1);
|
||||||
|
int i;
|
||||||
|
for (i=2; i<=n; i++) {
|
||||||
|
double d = luaL_check_number(L, i);
|
||||||
|
if (d < dmin)
|
||||||
|
dmin = d;
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, dmin);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int math_max (lua_State *L) {
|
||||||
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
|
double dmax = luaL_check_number(L, 1);
|
||||||
|
int i;
|
||||||
|
for (i=2; i<=n; i++) {
|
||||||
|
double d = luaL_check_number(L, i);
|
||||||
|
if (d > dmax)
|
||||||
|
dmax = d;
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, dmax);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int math_random (lua_State *L) {
|
||||||
|
/* the '%' avoids the (rare) case of r==1, and is needed also because on
|
||||||
|
some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
|
||||||
|
double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
|
||||||
|
switch (lua_gettop(L)) { /* check number of arguments */
|
||||||
|
case 0: { /* no arguments */
|
||||||
|
lua_pushnumber(L, r); /* Number between 0 and 1 */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: { /* only upper limit */
|
||||||
|
int u = luaL_check_int(L, 1);
|
||||||
|
luaL_arg_check(L, 1<=u, 1, "interval is empty");
|
||||||
|
lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: { /* lower and upper limits */
|
||||||
|
int l = luaL_check_int(L, 1);
|
||||||
|
int u = luaL_check_int(L, 2);
|
||||||
|
luaL_arg_check(L, l<=u, 2, "interval is empty");
|
||||||
|
lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: lua_error(L, "wrong number of arguments");
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int math_randomseed (lua_State *L) {
|
||||||
|
srand(luaL_check_int(L, 1));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg mathlib[] = {
|
||||||
|
{"abs", math_abs},
|
||||||
|
{"sin", math_sin},
|
||||||
|
{"cos", math_cos},
|
||||||
|
{"tan", math_tan},
|
||||||
|
{"asin", math_asin},
|
||||||
|
{"acos", math_acos},
|
||||||
|
{"atan", math_atan},
|
||||||
|
{"atan2", math_atan2},
|
||||||
|
{"ceil", math_ceil},
|
||||||
|
{"floor", math_floor},
|
||||||
|
{"mod", math_mod},
|
||||||
|
{"frexp", math_frexp},
|
||||||
|
{"ldexp", math_ldexp},
|
||||||
|
{"sqrt", math_sqrt},
|
||||||
|
{"min", math_min},
|
||||||
|
{"max", math_max},
|
||||||
|
{"log", math_log},
|
||||||
|
{"log10", math_log10},
|
||||||
|
{"exp", math_exp},
|
||||||
|
{"deg", math_deg},
|
||||||
|
{"rad", math_rad},
|
||||||
|
{"random", math_random},
|
||||||
|
{"randomseed", math_randomseed}
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Open math library
|
||||||
|
*/
|
||||||
|
void lua_mathlibopen (lua_State *L) {
|
||||||
|
luaL_openl(L, mathlib);
|
||||||
|
lua_pushnumber(L, 0); /* to get its tag */
|
||||||
|
lua_pushcfunction(L, math_pow);
|
||||||
|
lua_settagmethod(L, lua_tag(L, -2), "pow");
|
||||||
|
lua_pop(L, 1); /* remove number */
|
||||||
|
lua_pushnumber(L, PI); lua_setglobal(L, "PI");
|
||||||
|
}
|
||||||
|
|
||||||
153
lmem.c
Normal file
153
lmem.c
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lmem.c,v 1.35 2000/08/04 19:38:35 roberto Exp roberto $
|
||||||
|
** Interface to Memory Manager
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Real ISO (ANSI) systems do not need these tests;
|
||||||
|
** but some systems (Sun OS) are not that ISO...
|
||||||
|
*/
|
||||||
|
#ifdef OLD_ANSI
|
||||||
|
#define realloc(b,s) ((b) == NULL ? malloc(s) : (realloc)(b, s))
|
||||||
|
#define free(b) if (b) (free)(b)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
/*
|
||||||
|
** {======================================================================
|
||||||
|
** Controlled version for realloc.
|
||||||
|
** =======================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#undef realloc
|
||||||
|
#undef malloc
|
||||||
|
#undef free
|
||||||
|
#define realloc(b, s) debug_realloc(b, s)
|
||||||
|
#define malloc(b) debug_realloc(NULL, 0)
|
||||||
|
#define free(b) debug_realloc(b, 0)
|
||||||
|
|
||||||
|
|
||||||
|
/* ensures maximum alignment for HEADER */
|
||||||
|
union L_U { double d; char *s; long l; };
|
||||||
|
#define HEADER (sizeof(union L_U))
|
||||||
|
|
||||||
|
#define MARKSIZE 16
|
||||||
|
#define MARK 0x55 /* 01010101 (a nice pattern) */
|
||||||
|
|
||||||
|
|
||||||
|
#define blocksize(b) ((unsigned long *)((char *)(b) - HEADER))
|
||||||
|
|
||||||
|
unsigned long memdebug_numblocks = 0;
|
||||||
|
unsigned long memdebug_total = 0;
|
||||||
|
unsigned long memdebug_maxmem = 0;
|
||||||
|
unsigned long memdebug_memlimit = LONG_MAX;
|
||||||
|
|
||||||
|
|
||||||
|
static void *checkblock (void *block) {
|
||||||
|
unsigned long *b = blocksize(block);
|
||||||
|
unsigned long size = *b;
|
||||||
|
int i;
|
||||||
|
for (i=0;i<MARKSIZE;i++)
|
||||||
|
assert(*(((char *)b)+HEADER+size+i) == MARK+i); /* corrupted block? */
|
||||||
|
memdebug_numblocks--;
|
||||||
|
memdebug_total -= size;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void freeblock (void *block) {
|
||||||
|
if (block) {
|
||||||
|
size_t size = *blocksize(block);
|
||||||
|
block = checkblock(block);
|
||||||
|
memset(block, -1, size+HEADER+MARKSIZE); /* erase block */
|
||||||
|
(free)(block); /* free original block */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void *debug_realloc (void *block, size_t size) {
|
||||||
|
if (size == 0) {
|
||||||
|
freeblock(block);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else if (memdebug_total+size > memdebug_memlimit)
|
||||||
|
return NULL; /* to test memory allocation errors */
|
||||||
|
else {
|
||||||
|
size_t realsize = HEADER+size+MARKSIZE;
|
||||||
|
char *newblock = (char *)(malloc)(realsize); /* alloc a new block */
|
||||||
|
int i;
|
||||||
|
if (realsize < size) return NULL; /* overflow! */
|
||||||
|
if (newblock == NULL) return NULL;
|
||||||
|
if (block) {
|
||||||
|
size_t oldsize = *blocksize(block);
|
||||||
|
if (oldsize > size) oldsize = size;
|
||||||
|
memcpy(newblock+HEADER, block, oldsize);
|
||||||
|
freeblock(block); /* erase (and check) old copy */
|
||||||
|
}
|
||||||
|
memdebug_total += size;
|
||||||
|
if (memdebug_total > memdebug_maxmem) memdebug_maxmem = memdebug_total;
|
||||||
|
memdebug_numblocks++;
|
||||||
|
*(unsigned long *)newblock = size;
|
||||||
|
for (i=0;i<MARKSIZE;i++)
|
||||||
|
*(newblock+HEADER+size+i) = (char)(MARK+i);
|
||||||
|
return newblock+HEADER;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* }====================================================================== */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||||
|
int inc, size_t size, const char *errormsg, size_t limit) {
|
||||||
|
size_t newn = nelems+inc;
|
||||||
|
if (nelems >= limit-inc) lua_error(L, errormsg);
|
||||||
|
if ((newn ^ nelems) <= nelems || /* still the same power-of-2 limit? */
|
||||||
|
(nelems > 0 && newn < MINPOWER2)) /* or block already is MINPOWER2? */
|
||||||
|
return block; /* do not need to reallocate */
|
||||||
|
else /* it crossed a power-of-2 boundary; grow to next power */
|
||||||
|
return luaM_realloc(L, block, luaO_power2(newn)*size);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** generic allocation routine.
|
||||||
|
*/
|
||||||
|
void *luaM_realloc (lua_State *L, void *block, lint32 size) {
|
||||||
|
if (size == 0) {
|
||||||
|
free(block); /* block may be NULL; that is OK for free */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else if (size >= MAX_SIZET)
|
||||||
|
lua_error(L, "memory allocation error: block too big");
|
||||||
|
block = realloc(block, size);
|
||||||
|
if (block == NULL) {
|
||||||
|
if (L)
|
||||||
|
luaD_breakrun(L, LUA_ERRMEM); /* break run without error message */
|
||||||
|
else return NULL; /* error before creating state! */
|
||||||
|
}
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
42
lmem.h
Normal file
42
lmem.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lmem.h,v 1.14 2000/05/24 13:54:49 roberto Exp roberto $
|
||||||
|
** Interface to Memory Manager
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lmem_h
|
||||||
|
#define lmem_h
|
||||||
|
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "llimits.h"
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
void *luaM_realloc (lua_State *L, void *oldblock, lint32 size);
|
||||||
|
void *luaM_growaux (lua_State *L, void *block, size_t nelems,
|
||||||
|
int inc, size_t size, const char *errormsg,
|
||||||
|
size_t limit);
|
||||||
|
|
||||||
|
#define luaM_free(L, b) luaM_realloc(L, (b), 0)
|
||||||
|
#define luaM_malloc(L, t) luaM_realloc(L, NULL, (t))
|
||||||
|
#define luaM_new(L, t) ((t *)luaM_malloc(L, sizeof(t)))
|
||||||
|
#define luaM_newvector(L, n,t) ((t *)luaM_malloc(L, (n)*(lint32)sizeof(t)))
|
||||||
|
|
||||||
|
#define luaM_growvector(L, v,nelems,inc,t,e,l) \
|
||||||
|
((v)=(t *)luaM_growaux(L, v,nelems,inc,sizeof(t),e,l))
|
||||||
|
|
||||||
|
#define luaM_reallocvector(L, v,n,t) \
|
||||||
|
((v)=(t *)luaM_realloc(L, v,(n)*(lint32)sizeof(t)))
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
extern unsigned long memdebug_numblocks;
|
||||||
|
extern unsigned long memdebug_total;
|
||||||
|
extern unsigned long memdebug_maxmem;
|
||||||
|
extern unsigned long memdebug_memlimit;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
160
lobject.c
Normal file
160
lobject.c
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lobject.c,v 1.47 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
** Some generic functions over Lua objects
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** you can use the fact that the 3rd letter or each name is always different
|
||||||
|
** (e-m-r-b-n-l) to compare and switch these strings
|
||||||
|
*/
|
||||||
|
const char *const luaO_typenames[] = { /* ORDER LUA_T */
|
||||||
|
"userdata", "number", "string", "table", "function", "function", "nil",
|
||||||
|
"function", "function"
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
const TObject luaO_nilobject = {TAG_NIL, {NULL}};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
||||||
|
*/
|
||||||
|
lint32 luaO_power2 (lint32 n) {
|
||||||
|
lint32 p = MINPOWER2;
|
||||||
|
while (p<=n) p<<=1;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
||||||
|
if (ttype(t1) != ttype(t2)) return 0;
|
||||||
|
switch (ttype(t1)) {
|
||||||
|
case TAG_NUMBER:
|
||||||
|
return nvalue(t1) == nvalue(t2);
|
||||||
|
case TAG_STRING: case TAG_USERDATA:
|
||||||
|
return tsvalue(t1) == tsvalue(t2);
|
||||||
|
case TAG_TABLE:
|
||||||
|
return hvalue(t1) == hvalue(t2);
|
||||||
|
case TAG_CCLOSURE: case TAG_LCLOSURE:
|
||||||
|
return clvalue(t1) == clvalue(t2);
|
||||||
|
default:
|
||||||
|
LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
|
||||||
|
return 1; /* TAG_NIL */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
char *luaO_openspace (lua_State *L, size_t n) {
|
||||||
|
if (n > L->Mbuffsize) {
|
||||||
|
luaM_reallocvector(L, L->Mbuffer, n, char);
|
||||||
|
L->Mbuffsize = n;
|
||||||
|
}
|
||||||
|
return L->Mbuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static double expten (unsigned int e) {
|
||||||
|
double exp = 10.0;
|
||||||
|
double res = 1.0;
|
||||||
|
for (; e; e>>=1) {
|
||||||
|
if (e & 1) res *= exp;
|
||||||
|
exp *= exp;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
||||||
|
double a = 0.0;
|
||||||
|
int point = 0; /* number of decimal digits */
|
||||||
|
int sig;
|
||||||
|
while (isspace((unsigned char)*s)) s++;
|
||||||
|
sig = 0;
|
||||||
|
switch (*s) {
|
||||||
|
case '-': sig = 1; /* go through */
|
||||||
|
case '+': s++;
|
||||||
|
}
|
||||||
|
if (! (isdigit((unsigned char)*s) ||
|
||||||
|
(*s == '.' && isdigit((unsigned char)*(s+1)))))
|
||||||
|
return 0; /* not (at least one digit before or after the point) */
|
||||||
|
while (isdigit((unsigned char)*s))
|
||||||
|
a = 10.0*a + (*(s++)-'0');
|
||||||
|
if (*s == '.') {
|
||||||
|
s++;
|
||||||
|
while (isdigit((unsigned char)*s)) {
|
||||||
|
a = 10.0*a + (*(s++)-'0');
|
||||||
|
point++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sig) a = -a;
|
||||||
|
if (*s == 'e' || *s == 'E') {
|
||||||
|
int e = 0;
|
||||||
|
s++;
|
||||||
|
sig = 0;
|
||||||
|
switch (*s) {
|
||||||
|
case '-': sig = 1; /* go through */
|
||||||
|
case '+': s++;
|
||||||
|
}
|
||||||
|
if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
|
||||||
|
do {
|
||||||
|
e = 10*e + (*(s++)-'0');
|
||||||
|
} while (isdigit((unsigned char)*s));
|
||||||
|
if (sig) e = -e;
|
||||||
|
point -= e;
|
||||||
|
}
|
||||||
|
while (isspace((unsigned char)*s)) s++;
|
||||||
|
if (*s != '\0') return 0; /* invalid trailing characters? */
|
||||||
|
if (point != 0) {
|
||||||
|
if (point > 0) a /= expten(point);
|
||||||
|
else a *= expten(-point);
|
||||||
|
}
|
||||||
|
*result = a;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* this function needs to handle only '%d' and '%.XXXs' formats */
|
||||||
|
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
||||||
|
char buff[500];
|
||||||
|
va_list argp;
|
||||||
|
va_start(argp, fmt);
|
||||||
|
vsprintf(buff, fmt, argp);
|
||||||
|
va_end(argp);
|
||||||
|
lua_error(L, buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define EXTRALEN sizeof("string \"...\"0")
|
||||||
|
|
||||||
|
void luaO_chunkid (char *out, const char *source, int len) {
|
||||||
|
if (*source == '(') {
|
||||||
|
strncpy(out, source+1, len-1); /* remove first char */
|
||||||
|
out[len-1] = '\0'; /* make sure `out' has an end */
|
||||||
|
out[strlen(out)-1] = '\0'; /* remove last char */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
len -= EXTRALEN;
|
||||||
|
if (*source == '@')
|
||||||
|
sprintf(out, "file `%.*s'", len, source+1);
|
||||||
|
else {
|
||||||
|
const char *b = strchr(source , '\n'); /* stop at first new line */
|
||||||
|
int lim = (b && (b-source)<len) ? b-source : len;
|
||||||
|
sprintf(out, "string \"%.*s\"", lim, source);
|
||||||
|
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
205
lobject.h
Normal file
205
lobject.h
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lobject.h,v 1.75 2000/09/11 17:38:42 roberto Exp roberto $
|
||||||
|
** Type definitions for Lua objects
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lobject_h
|
||||||
|
#define lobject_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "llimits.h"
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#undef NDEBUG
|
||||||
|
#include <assert.h>
|
||||||
|
#define LUA_INTERNALERROR(s) assert(((void)s,0))
|
||||||
|
#define LUA_ASSERT(c,s) assert(((void)s,(c)))
|
||||||
|
#else
|
||||||
|
#define LUA_INTERNALERROR(s) /* empty */
|
||||||
|
#define LUA_ASSERT(c,s) /* empty */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
/* to avoid warnings, and make sure value is really unused */
|
||||||
|
#define UNUSED(x) (x=0, (void)(x))
|
||||||
|
#else
|
||||||
|
#define UNUSED(x) ((void)(x)) /* to avoid warnings */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Lua TYPES
|
||||||
|
** WARNING: if you change the order of this enumeration,
|
||||||
|
** grep "ORDER LUA_T"
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
TAG_USERDATA = 0, /* default tag for userdata */
|
||||||
|
TAG_NUMBER, /* fixed tag for numbers */
|
||||||
|
TAG_STRING, /* fixed tag for strings */
|
||||||
|
TAG_TABLE, /* default tag for tables */
|
||||||
|
TAG_LCLOSURE, /* fixed tag for Lua closures */
|
||||||
|
TAG_CCLOSURE, /* fixed tag for C closures */
|
||||||
|
TAG_NIL, /* last "pre-defined" tag */
|
||||||
|
|
||||||
|
TAG_LMARK, /* mark for Lua closures */
|
||||||
|
TAG_CMARK /* mark for C closures */
|
||||||
|
|
||||||
|
} lua_Type;
|
||||||
|
|
||||||
|
/* tags for values visible from Lua == first user-created tag */
|
||||||
|
#define NUM_TAGS 7
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** check whether `t' is a mark
|
||||||
|
*/
|
||||||
|
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
|
||||||
|
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
struct TString *ts; /* TAG_STRING, TAG_USERDATA */
|
||||||
|
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */
|
||||||
|
struct Hash *a; /* TAG_TABLE */
|
||||||
|
struct CallInfo *i; /* TAG_LMARK */
|
||||||
|
Number n; /* TAG_NUMBER */
|
||||||
|
} Value;
|
||||||
|
|
||||||
|
|
||||||
|
/* Macros to access values */
|
||||||
|
#define ttype(o) ((o)->ttype)
|
||||||
|
#define nvalue(o) ((o)->value.n)
|
||||||
|
#define tsvalue(o) ((o)->value.ts)
|
||||||
|
#define clvalue(o) ((o)->value.cl)
|
||||||
|
#define hvalue(o) ((o)->value.a)
|
||||||
|
#define infovalue(o) ((o)->value.i)
|
||||||
|
#define svalue(o) (tsvalue(o)->str)
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct lua_TObject {
|
||||||
|
lua_Type ttype;
|
||||||
|
Value value;
|
||||||
|
} TObject;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** String headers for string table
|
||||||
|
*/
|
||||||
|
typedef struct TString {
|
||||||
|
union {
|
||||||
|
struct { /* for strings */
|
||||||
|
unsigned long hash;
|
||||||
|
size_t len;
|
||||||
|
int constindex; /* hint to reuse constants */
|
||||||
|
} s;
|
||||||
|
struct { /* for userdata */
|
||||||
|
int tag;
|
||||||
|
void *value;
|
||||||
|
} d;
|
||||||
|
} u;
|
||||||
|
struct TString *nexthash; /* chain for hash table */
|
||||||
|
unsigned char marked;
|
||||||
|
char str[1]; /* variable length string!! must be the last field! */
|
||||||
|
} TString;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Function Prototypes
|
||||||
|
*/
|
||||||
|
typedef struct Proto {
|
||||||
|
Number *knum; /* Number numbers used by the function */
|
||||||
|
int nknum; /* size of `knum' */
|
||||||
|
struct TString **kstr; /* strings used by the function */
|
||||||
|
int nkstr; /* size of `kstr' */
|
||||||
|
struct Proto **kproto; /* functions defined inside the function */
|
||||||
|
int nkproto; /* size of `kproto' */
|
||||||
|
Instruction *code; /* ends with opcode ENDCODE */
|
||||||
|
int numparams;
|
||||||
|
int is_vararg;
|
||||||
|
int maxstacksize;
|
||||||
|
struct Proto *next;
|
||||||
|
int marked;
|
||||||
|
/* debug information */
|
||||||
|
int *lineinfo; /* map from opcodes to source lines */
|
||||||
|
int nlocvars;
|
||||||
|
struct LocVar *locvars; /* information about local variables */
|
||||||
|
int lineDefined;
|
||||||
|
TString *source;
|
||||||
|
} Proto;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct LocVar {
|
||||||
|
TString *varname;
|
||||||
|
int startpc; /* first point where variable is active */
|
||||||
|
int endpc; /* first point where variable is dead */
|
||||||
|
} LocVar;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Closures
|
||||||
|
*/
|
||||||
|
typedef struct Closure {
|
||||||
|
union {
|
||||||
|
lua_CFunction c; /* C functions */
|
||||||
|
struct Proto *l; /* Lua functions */
|
||||||
|
} f;
|
||||||
|
struct Closure *next;
|
||||||
|
struct Closure *mark; /* marked closures (point to itself when not marked) */
|
||||||
|
int nupvalues;
|
||||||
|
TObject upvalue[1];
|
||||||
|
} Closure;
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
TObject key;
|
||||||
|
TObject val;
|
||||||
|
struct Node *next; /* for chaining */
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct Hash {
|
||||||
|
Node *node;
|
||||||
|
int htag;
|
||||||
|
int size;
|
||||||
|
Node *firstfree; /* this position is free; all positions after it are full */
|
||||||
|
struct Hash *next;
|
||||||
|
struct Hash *mark; /* marked tables (point to itself when not marked) */
|
||||||
|
} Hash;
|
||||||
|
|
||||||
|
|
||||||
|
/* unmarked tables and closures are represented by pointing `mark' to
|
||||||
|
** themselves
|
||||||
|
*/
|
||||||
|
#define ismarked(x) ((x)->mark != (x))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** informations about a call (for debugging)
|
||||||
|
*/
|
||||||
|
typedef struct CallInfo {
|
||||||
|
struct Closure *func; /* function being called */
|
||||||
|
const Instruction **pc; /* current pc of called function */
|
||||||
|
int lastpc; /* last pc traced */
|
||||||
|
int line; /* current line */
|
||||||
|
int refi; /* current index in `lineinfo' */
|
||||||
|
} CallInfo;
|
||||||
|
|
||||||
|
|
||||||
|
extern const char *const luaO_typenames[];
|
||||||
|
extern const TObject luaO_nilobject;
|
||||||
|
|
||||||
|
#define luaO_typename(o) luaO_typenames[ttype(o)]
|
||||||
|
|
||||||
|
lint32 luaO_power2 (lint32 n);
|
||||||
|
char *luaO_openspace (lua_State *L, size_t n);
|
||||||
|
|
||||||
|
int luaO_equalObj (const TObject *t1, const TObject *t2);
|
||||||
|
int luaO_str2d (const char *s, Number *result);
|
||||||
|
|
||||||
|
void luaO_verror (lua_State *L, const char *fmt, ...);
|
||||||
|
void luaO_chunkid (char *out, const char *source, int len);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
168
lopcodes.h
Normal file
168
lopcodes.h
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lopcodes.h,v 1.66 2000/08/15 18:28:48 roberto Exp roberto $
|
||||||
|
** Opcodes for Lua virtual machine
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lopcodes_h
|
||||||
|
#define lopcodes_h
|
||||||
|
|
||||||
|
#include "llimits.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*===========================================================================
|
||||||
|
We assume that instructions are unsigned numbers.
|
||||||
|
All instructions have an opcode in the first 6 bits. Moreover,
|
||||||
|
an instruction can have 0, 1, or 2 arguments. Instructions can
|
||||||
|
have the following types:
|
||||||
|
type 0: no arguments
|
||||||
|
type 1: 1 unsigned argument in the higher bits (called `U')
|
||||||
|
type 2: 1 signed argument in the higher bits (`S')
|
||||||
|
type 3: 1st unsigned argument in the higher bits (`A')
|
||||||
|
2nd unsigned argument in the middle bits (`B')
|
||||||
|
|
||||||
|
A signed argument is represented in excess K; that is, the number
|
||||||
|
value is the unsigned value minus K. K is exactly the maximum value
|
||||||
|
for that argument (so that -max is represented by 0, and +max is
|
||||||
|
represented by 2*max), which is half the maximum for the corresponding
|
||||||
|
unsigned argument.
|
||||||
|
|
||||||
|
The size of each argument is defined in `llimits.h'. The usual is an
|
||||||
|
instruction with 32 bits, U arguments with 26 bits (32-6), B arguments
|
||||||
|
with 9 bits, and A arguments with 17 bits (32-6-9). For small
|
||||||
|
installations, the instruction size can be 16, so U has 10 bits,
|
||||||
|
and A and B have 5 bits each.
|
||||||
|
===========================================================================*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* creates a mask with `n' 1 bits at position `p' */
|
||||||
|
#define MASK1(n,p) ((~((~(Instruction)0)<<n))<<p)
|
||||||
|
|
||||||
|
/* creates a mask with `n' 0 bits at position `p' */
|
||||||
|
#define MASK0(n,p) (~MASK1(n,p))
|
||||||
|
|
||||||
|
/*
|
||||||
|
** the following macros help to manipulate instructions
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define CREATE_0(o) ((Instruction)(o))
|
||||||
|
#define GET_OPCODE(i) ((OpCode)((i)&MASK1(SIZE_OP,0)))
|
||||||
|
#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,0)) | (Instruction)(o)))
|
||||||
|
|
||||||
|
#define CREATE_U(o,u) ((Instruction)(o) | ((Instruction)(u)<<POS_U))
|
||||||
|
#define GETARG_U(i) ((int)((i)>>POS_U))
|
||||||
|
#define SETARG_U(i,u) ((i) = (((i)&MASK0(SIZE_U,POS_U)) | \
|
||||||
|
((Instruction)(u)<<POS_U)))
|
||||||
|
|
||||||
|
#define CREATE_S(o,s) CREATE_U((o),(s)+MAXARG_S)
|
||||||
|
#define GETARG_S(i) (GETARG_U(i)-MAXARG_S)
|
||||||
|
#define SETARG_S(i,s) SETARG_U((i),(s)+MAXARG_S)
|
||||||
|
|
||||||
|
|
||||||
|
#define CREATE_AB(o,a,b) ((Instruction)(o) | ((Instruction)(a)<<POS_A) \
|
||||||
|
| ((Instruction)(b)<<POS_B))
|
||||||
|
#define GETARG_A(i) ((int)((i)>>POS_A))
|
||||||
|
#define SETARG_A(i,a) ((i) = (((i)&MASK0(SIZE_A,POS_A)) | \
|
||||||
|
((Instruction)(a)<<POS_A)))
|
||||||
|
#define GETARG_B(i) ((int)(((i)>>POS_B) & MASK1(SIZE_B,0)))
|
||||||
|
#define SETARG_B(i,b) ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
|
||||||
|
((Instruction)(b)<<POS_B)))
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** K = U argument used as index to `kstr'
|
||||||
|
** J = S argument used as jump offset (relative to pc of next instruction)
|
||||||
|
** L = unsigned argument used as index of local variable
|
||||||
|
** N = U argument used as index to `knum'
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
/*----------------------------------------------------------------------
|
||||||
|
name args stack before stack after side effects
|
||||||
|
------------------------------------------------------------------------*/
|
||||||
|
OP_END,/* - - (return) no results */
|
||||||
|
OP_RETURN,/* U v_n-v_x(at u) (return) returns v_x-v_n */
|
||||||
|
|
||||||
|
OP_CALL,/* A B v_n-v_1 f(at a) r_b-r_1 f(v1,...,v_n) */
|
||||||
|
OP_TAILCALL,/* A B v_n-v_1 f(at a) (return) f(v1,...,v_n) */
|
||||||
|
|
||||||
|
OP_PUSHNIL,/* U - nil_1-nil_u */
|
||||||
|
OP_POP,/* U a_u-a_1 - */
|
||||||
|
|
||||||
|
OP_PUSHINT,/* S - (Number)s */
|
||||||
|
OP_PUSHSTRING,/* K - KSTR[k] */
|
||||||
|
OP_PUSHNUM,/* N - KNUM[n] */
|
||||||
|
OP_PUSHNEGNUM,/* N - -KNUM[n] */
|
||||||
|
|
||||||
|
OP_PUSHUPVALUE,/* U - Closure[u] */
|
||||||
|
|
||||||
|
OP_GETLOCAL,/* L - LOC[l] */
|
||||||
|
OP_GETGLOBAL,/* K - VAR[KSTR[k]] */
|
||||||
|
|
||||||
|
OP_GETTABLE,/* - i t t[i] */
|
||||||
|
OP_GETDOTTED,/* K t t[KSTR[k]] */
|
||||||
|
OP_GETINDEXED,/* L t t[LOC[l]] */
|
||||||
|
OP_PUSHSELF,/* K t t t[KSTR[k]] */
|
||||||
|
|
||||||
|
OP_CREATETABLE,/* U - newarray(size = u) */
|
||||||
|
|
||||||
|
OP_SETLOCAL,/* L x - LOC[l]=x */
|
||||||
|
OP_SETGLOBAL,/* K x - VAR[KSTR[k]]=x */
|
||||||
|
OP_SETTABLE,/* A B v a_a-a_1 i t (pops b values) t[i]=v */
|
||||||
|
|
||||||
|
OP_SETLIST,/* A B v_b-v_1 t t t[i+a*FPF]=v_i */
|
||||||
|
OP_SETMAP,/* U v_u k_u - v_1 k_1 t t t[k_i]=v_i */
|
||||||
|
|
||||||
|
OP_ADD,/* - y x x+y */
|
||||||
|
OP_ADDI,/* S x x+s */
|
||||||
|
OP_SUB,/* - y x x-y */
|
||||||
|
OP_MULT,/* - y x x*y */
|
||||||
|
OP_DIV,/* - y x x/y */
|
||||||
|
OP_POW,/* - y x x^y */
|
||||||
|
OP_CONCAT,/* U v_u-v_1 v1..-..v_u */
|
||||||
|
OP_MINUS,/* - x -x */
|
||||||
|
OP_NOT,/* - x (x==nil)? 1 : nil */
|
||||||
|
|
||||||
|
OP_JMPNE,/* J y x - (x~=y)? PC+=s */
|
||||||
|
OP_JMPEQ,/* J y x - (x==y)? PC+=s */
|
||||||
|
OP_JMPLT,/* J y x - (x<y)? PC+=s */
|
||||||
|
OP_JMPLE,/* J y x - (x<y)? PC+=s */
|
||||||
|
OP_JMPGT,/* J y x - (x>y)? PC+=s */
|
||||||
|
OP_JMPGE,/* J y x - (x>=y)? PC+=s */
|
||||||
|
|
||||||
|
OP_JMPT,/* J x - (x~=nil)? PC+=s */
|
||||||
|
OP_JMPF,/* J x - (x==nil)? PC+=s */
|
||||||
|
OP_JMPONT,/* J x (x~=nil)? x : - (x~=nil)? PC+=s */
|
||||||
|
OP_JMPONF,/* J x (x==nil)? x : - (x==nil)? PC+=s */
|
||||||
|
OP_JMP,/* J - - PC+=s */
|
||||||
|
|
||||||
|
OP_PUSHNILJMP,/* - - nil PC++; */
|
||||||
|
|
||||||
|
OP_FORPREP,/* J */
|
||||||
|
OP_FORLOOP,/* J */
|
||||||
|
|
||||||
|
OP_LFORPREP,/* J */
|
||||||
|
OP_LFORLOOP,/* J */
|
||||||
|
|
||||||
|
OP_CLOSURE/* A B v_b-v_1 closure(KPROTO[a], v_1-v_b) */
|
||||||
|
|
||||||
|
} OpCode;
|
||||||
|
|
||||||
|
#define NUM_OPCODES ((int)OP_CLOSURE+1)
|
||||||
|
|
||||||
|
|
||||||
|
#define ISJUMP(o) (OP_JMPNE <= (o) && (o) <= OP_JMP)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* special code to fit a LUA_MULTRET inside an argB */
|
||||||
|
#define MULT_RET 255 /* (<=MAXARG_B) */
|
||||||
|
#if MULT_RET>MAXARG_B
|
||||||
|
#undef MULT_RET
|
||||||
|
#define MULT_RET MAXARG_B
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
61
lparser.h
Normal file
61
lparser.h
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lparser.h,v 1.23 2000/08/22 17:44:17 roberto Exp roberto $
|
||||||
|
** LL(1) Parser and code generator for Lua
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lparser_h
|
||||||
|
#define lparser_h
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Expression descriptor
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
VGLOBAL,
|
||||||
|
VLOCAL,
|
||||||
|
VINDEXED,
|
||||||
|
VEXP
|
||||||
|
} expkind;
|
||||||
|
|
||||||
|
typedef struct expdesc {
|
||||||
|
expkind k;
|
||||||
|
union {
|
||||||
|
int index; /* VGLOBAL: `kstr' index of global name; VLOCAL: stack index */
|
||||||
|
struct {
|
||||||
|
int t; /* patch list of `exit when true' */
|
||||||
|
int f; /* patch list of `exit when false' */
|
||||||
|
} l;
|
||||||
|
} u;
|
||||||
|
} expdesc;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* state needed to generate code for a given function */
|
||||||
|
typedef struct FuncState {
|
||||||
|
Proto *f; /* current function header */
|
||||||
|
struct FuncState *prev; /* enclosing function */
|
||||||
|
struct LexState *ls; /* lexical state */
|
||||||
|
struct lua_State *L; /* copy of the Lua state */
|
||||||
|
int pc; /* next position to code */
|
||||||
|
int lasttarget; /* `pc' of last `jump target' */
|
||||||
|
int jlt; /* list of jumps to `lasttarget' */
|
||||||
|
int stacklevel; /* number of values on activation register */
|
||||||
|
int nactloc; /* number of active local variables */
|
||||||
|
int nupvalues; /* number of upvalues */
|
||||||
|
int lastline; /* line where last `lineinfo' was generated */
|
||||||
|
int nlineinfo; /* index of next `lineinfo' to be generated */
|
||||||
|
struct Breaklabel *bl; /* chain of breakable blocks */
|
||||||
|
expdesc upvalues[MAXUPVALUES]; /* upvalues */
|
||||||
|
int actloc[MAXLOCALS]; /* local-variable stack (indices to locvars) */
|
||||||
|
} FuncState;
|
||||||
|
|
||||||
|
|
||||||
|
Proto *luaY_parser (lua_State *L, ZIO *z);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
103
lstate.c
Normal file
103
lstate.c
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lstate.c,v 1.39 2000/09/12 18:42:32 roberto Exp roberto $
|
||||||
|
** Global State
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lgc.h"
|
||||||
|
#include "llex.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
extern lua_State *lua_state;
|
||||||
|
void luaB_opentests (lua_State *L);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** built-in implementation for ERRORMESSAGE. In a "correct" environment
|
||||||
|
** ERRORMESSAGE should have an external definition, and so this function
|
||||||
|
** would not be used.
|
||||||
|
*/
|
||||||
|
static int errormessage (lua_State *L) {
|
||||||
|
const char *s = lua_tostring(L, 1);
|
||||||
|
if (s == NULL) s = "(no message)";
|
||||||
|
fprintf(stderr, "error: %s\n", s);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
lua_State *lua_open (int stacksize) {
|
||||||
|
struct lua_longjmp myErrorJmp;
|
||||||
|
lua_State *L = luaM_new(NULL, lua_State);
|
||||||
|
if (L == NULL) return NULL; /* memory allocation error */
|
||||||
|
L->stack = NULL;
|
||||||
|
L->strt.size = L->udt.size = 0;
|
||||||
|
L->strt.nuse = L->udt.nuse = 0;
|
||||||
|
L->strt.hash = NULL;
|
||||||
|
L->udt.hash = NULL;
|
||||||
|
L->Mbuffer = NULL;
|
||||||
|
L->Mbuffsize = 0;
|
||||||
|
L->rootproto = NULL;
|
||||||
|
L->rootcl = NULL;
|
||||||
|
L->roottable = NULL;
|
||||||
|
L->IMtable = NULL;
|
||||||
|
L->last_tag = -1;
|
||||||
|
L->refArray = NULL;
|
||||||
|
L->refSize = 0;
|
||||||
|
L->refFree = NONEXT;
|
||||||
|
L->nblocks = 0;
|
||||||
|
L->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
|
||||||
|
L->callhook = NULL;
|
||||||
|
L->linehook = NULL;
|
||||||
|
L->allowhooks = 1;
|
||||||
|
L->errorJmp = &myErrorJmp;
|
||||||
|
if (setjmp(myErrorJmp.b) == 0) { /* to catch memory allocation errors */
|
||||||
|
L->gt = luaH_new(L, 10);
|
||||||
|
luaD_init(L, (stacksize == 0) ? DEFAULT_STACK_SIZE :
|
||||||
|
stacksize+LUA_MINSTACK);
|
||||||
|
luaS_init(L);
|
||||||
|
luaX_init(L);
|
||||||
|
luaT_init(L);
|
||||||
|
lua_register(L, LUA_ERRORMESSAGE, errormessage);
|
||||||
|
#ifdef DEBUG
|
||||||
|
luaB_opentests(L);
|
||||||
|
#endif
|
||||||
|
L->GCthreshold = L->nblocks*4;
|
||||||
|
L->errorJmp = NULL;
|
||||||
|
return L;
|
||||||
|
}
|
||||||
|
else { /* memory allocation error: free partial state */
|
||||||
|
lua_close(L);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_close (lua_State *L) {
|
||||||
|
luaC_collect(L, 1); /* collect all elements */
|
||||||
|
LUA_ASSERT(L->rootproto == NULL, "list should be empty");
|
||||||
|
LUA_ASSERT(L->rootcl == NULL, "list should be empty");
|
||||||
|
LUA_ASSERT(L->roottable == NULL, "list should be empty");
|
||||||
|
luaS_freeall(L);
|
||||||
|
luaM_free(L, L->stack);
|
||||||
|
luaM_free(L, L->IMtable);
|
||||||
|
luaM_free(L, L->refArray);
|
||||||
|
luaM_free(L, L->Mbuffer);
|
||||||
|
LUA_ASSERT(L->nblocks == 0, "wrong count for nblocks");
|
||||||
|
luaM_free(L, L);
|
||||||
|
LUA_ASSERT(L != lua_state || memdebug_numblocks == 0, "memory leak!");
|
||||||
|
LUA_ASSERT(L != lua_state || memdebug_total == 0,"memory leak!");
|
||||||
|
}
|
||||||
|
|
||||||
86
lstate.h
Normal file
86
lstate.h
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lstate.h,v 1.37 2000/08/28 17:57:04 roberto Exp roberto $
|
||||||
|
** Global State
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lstate_h
|
||||||
|
#define lstate_h
|
||||||
|
|
||||||
|
#include <setjmp.h>
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lua.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef TObject *StkId; /* index to stack elements */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** marks for Reference array
|
||||||
|
*/
|
||||||
|
#define NONEXT -1 /* to end the free list */
|
||||||
|
#define HOLD -2
|
||||||
|
#define COLLECTED -3
|
||||||
|
#define LOCK -4
|
||||||
|
|
||||||
|
|
||||||
|
struct Ref {
|
||||||
|
TObject o;
|
||||||
|
int st; /* can be LOCK, HOLD, COLLECTED, or next (for free list) */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** chain list of long jumps
|
||||||
|
*/
|
||||||
|
struct lua_longjmp {
|
||||||
|
jmp_buf b;
|
||||||
|
struct lua_longjmp *previous;
|
||||||
|
volatile int status; /* error code */
|
||||||
|
StkId base;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct stringtable {
|
||||||
|
int size;
|
||||||
|
lint32 nuse; /* number of elements */
|
||||||
|
TString **hash;
|
||||||
|
} stringtable;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct lua_State {
|
||||||
|
/* thread-specific state */
|
||||||
|
StkId top; /* first free slot in the stack */
|
||||||
|
StkId stack; /* stack base */
|
||||||
|
StkId stack_last; /* last free slot in the stack */
|
||||||
|
int stacksize;
|
||||||
|
StkId Cbase; /* base for current C function */
|
||||||
|
struct lua_longjmp *errorJmp; /* current error recover point */
|
||||||
|
char *Mbuffer; /* global buffer */
|
||||||
|
size_t Mbuffsize; /* size of Mbuffer */
|
||||||
|
/* global state */
|
||||||
|
Proto *rootproto; /* list of all prototypes */
|
||||||
|
Closure *rootcl; /* list of all closures */
|
||||||
|
Hash *roottable; /* list of all tables */
|
||||||
|
stringtable strt; /* hash table for strings */
|
||||||
|
stringtable udt; /* hash table for udata */
|
||||||
|
Hash *gt; /* table for globals */
|
||||||
|
struct IM *IMtable; /* table for tag methods */
|
||||||
|
int last_tag; /* last used tag in IMtable */
|
||||||
|
struct Ref *refArray; /* locked objects */
|
||||||
|
int refSize; /* size of refArray */
|
||||||
|
int refFree; /* list of free positions in refArray */
|
||||||
|
unsigned long GCthreshold;
|
||||||
|
unsigned long nblocks; /* number of `blocks' currently allocated */
|
||||||
|
lua_Hook callhook;
|
||||||
|
lua_Hook linehook;
|
||||||
|
int allowhooks;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
132
lstring.c
Normal file
132
lstring.c
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lstring.c,v 1.41 2000/08/04 19:38:35 roberto Exp roberto $
|
||||||
|
** String table (keeps all strings handled by Lua)
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void luaS_init (lua_State *L) {
|
||||||
|
L->strt.hash = luaM_newvector(L, 1, TString *);
|
||||||
|
L->udt.hash = luaM_newvector(L, 1, TString *);
|
||||||
|
L->strt.size = L->udt.size = 1;
|
||||||
|
L->strt.nuse = L->udt.nuse = 0;
|
||||||
|
L->strt.hash[0] = L->udt.hash[0] = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaS_freeall (lua_State *L) {
|
||||||
|
LUA_ASSERT(L->strt.nuse==0, "non-empty string table");
|
||||||
|
luaM_free(L, L->strt.hash);
|
||||||
|
LUA_ASSERT(L->udt.nuse==0, "non-empty udata table");
|
||||||
|
luaM_free(L, L->udt.hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static unsigned long hash_s (const char *s, size_t l) {
|
||||||
|
unsigned long h = l; /* seed */
|
||||||
|
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
|
||||||
|
for (; l>=step; l-=step)
|
||||||
|
h = h ^ ((h<<5)+(h>>2)+(unsigned char)*(s++));
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaS_resize (lua_State *L, stringtable *tb, int newsize) {
|
||||||
|
TString **newhash = luaM_newvector(L, newsize, TString *);
|
||||||
|
int i;
|
||||||
|
for (i=0; i<newsize; i++) newhash[i] = NULL;
|
||||||
|
/* rehash */
|
||||||
|
for (i=0; i<tb->size; i++) {
|
||||||
|
TString *p = tb->hash[i];
|
||||||
|
while (p) { /* for each node in the list */
|
||||||
|
TString *next = p->nexthash; /* save next */
|
||||||
|
unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
|
||||||
|
int h1 = h&(newsize-1); /* new position */
|
||||||
|
LUA_ASSERT(h%newsize == (h&(newsize-1)),
|
||||||
|
"a&(x-1) == a%x, for x power of 2");
|
||||||
|
p->nexthash = newhash[h1]; /* chain it in new position */
|
||||||
|
newhash[h1] = p;
|
||||||
|
p = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaM_free(L, tb->hash);
|
||||||
|
tb->size = newsize;
|
||||||
|
tb->hash = newhash;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void newentry (lua_State *L, stringtable *tb, TString *ts, int h) {
|
||||||
|
ts->nexthash = tb->hash[h]; /* chain new entry */
|
||||||
|
tb->hash[h] = ts;
|
||||||
|
tb->nuse++;
|
||||||
|
if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT/2) /* too crowded? */
|
||||||
|
luaS_resize(L, tb, tb->size*2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
|
||||||
|
unsigned long h = hash_s(str, l);
|
||||||
|
int h1 = h&(L->strt.size-1);
|
||||||
|
TString *ts;
|
||||||
|
for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash) {
|
||||||
|
if (ts->u.s.len == l && (memcmp(str, ts->str, l) == 0))
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
/* not found */
|
||||||
|
ts = (TString *)luaM_malloc(L, sizeof(TString)+(lint32)l*sizeof(char));
|
||||||
|
ts->marked = 0;
|
||||||
|
ts->nexthash = NULL;
|
||||||
|
ts->u.s.len = l;
|
||||||
|
ts->u.s.hash = h;
|
||||||
|
ts->u.s.constindex = 0;
|
||||||
|
memcpy(ts->str, str, l);
|
||||||
|
ts->str[l] = 0; /* ending 0 */
|
||||||
|
L->nblocks += gcsizestring(L, l);
|
||||||
|
newentry(L, &L->strt, ts, h1); /* insert it on table */
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TString *luaS_createudata (lua_State *L, void *udata, int tag) {
|
||||||
|
unsigned long h = IntPoint(udata);
|
||||||
|
int h1 = h&(L->udt.size-1);
|
||||||
|
TString *ts;
|
||||||
|
for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash) {
|
||||||
|
if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == LUA_ANYTAG))
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
/* not found */
|
||||||
|
ts = luaM_new(L, TString);
|
||||||
|
ts->marked = 0;
|
||||||
|
ts->nexthash = NULL;
|
||||||
|
ts->u.d.value = udata;
|
||||||
|
ts->u.d.tag = (tag == LUA_ANYTAG) ? 0 : tag;
|
||||||
|
L->nblocks += gcsizeudata;
|
||||||
|
newentry(L, &L->udt, ts, h1); /* insert it on table */
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TString *luaS_new (lua_State *L, const char *str) {
|
||||||
|
return luaS_newlstr(L, str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TString *luaS_newfixed (lua_State *L, const char *str) {
|
||||||
|
TString *ts = luaS_new(L, str);
|
||||||
|
if (ts->marked == 0) ts->marked = FIXMARK; /* avoid GC */
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
36
lstring.h
Normal file
36
lstring.h
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lstring.h,v 1.20 2000/05/10 16:33:20 roberto Exp roberto $
|
||||||
|
** String table (keep all strings handled by Lua)
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lstring_h
|
||||||
|
#define lstring_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** any TString with mark>=FIXMARK is never collected.
|
||||||
|
** Marks>=RESERVEDMARK are used to identify reserved words.
|
||||||
|
*/
|
||||||
|
#define FIXMARK 2
|
||||||
|
#define RESERVEDMARK 3
|
||||||
|
|
||||||
|
|
||||||
|
#define gcsizestring(L, l) numblocks(L, 0, sizeof(TString)+l)
|
||||||
|
#define gcsizeudata gcsizestring(L, 0)
|
||||||
|
|
||||||
|
|
||||||
|
void luaS_init (lua_State *L);
|
||||||
|
void luaS_resize (lua_State *L, stringtable *tb, int newsize);
|
||||||
|
TString *luaS_createudata (lua_State *L, void *udata, int tag);
|
||||||
|
void luaS_freeall (lua_State *L);
|
||||||
|
TString *luaS_newlstr (lua_State *L, const char *str, size_t l);
|
||||||
|
TString *luaS_new (lua_State *L, const char *str);
|
||||||
|
TString *luaS_newfixed (lua_State *L, const char *str);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
621
lstrlib.c
Normal file
621
lstrlib.c
Normal file
@@ -0,0 +1,621 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lstrlib.c,v 1.52 2000/09/11 17:38:42 roberto Exp roberto $
|
||||||
|
** Standard library for string operations and pattern-matching
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int str_len (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
luaL_check_lstr(L, 1, &l);
|
||||||
|
lua_pushnumber(L, l);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static long posrelat (long pos, size_t len) {
|
||||||
|
/* relative string position: negative means back from end */
|
||||||
|
return (pos>=0) ? pos : (long)len+pos+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_sub (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
long start = posrelat(luaL_check_long(L, 2), l);
|
||||||
|
long end = posrelat(luaL_opt_long(L, 3, -1), l);
|
||||||
|
if (start < 1) start = 1;
|
||||||
|
if (end > (long)l) end = l;
|
||||||
|
if (start <= end)
|
||||||
|
lua_pushlstring(L, s+start-1, end-start+1);
|
||||||
|
else lua_pushstring(L, "");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_lower (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
size_t i;
|
||||||
|
luaL_Buffer b;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
for (i=0; i<l; i++)
|
||||||
|
luaL_putchar(&b, tolower((unsigned char)(s[i])));
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_upper (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
size_t i;
|
||||||
|
luaL_Buffer b;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
for (i=0; i<l; i++)
|
||||||
|
luaL_putchar(&b, toupper((unsigned char)(s[i])));
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int str_rep (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
luaL_Buffer b;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
int n = luaL_check_int(L, 2);
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
while (n-- > 0)
|
||||||
|
luaL_addlstring(&b, s, l);
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_byte (lua_State *L) {
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l);
|
||||||
|
long pos = posrelat(luaL_opt_long(L, 2, 1), l);
|
||||||
|
luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range");
|
||||||
|
lua_pushnumber(L, (unsigned char)s[pos-1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_char (lua_State *L) {
|
||||||
|
int n = lua_gettop(L); /* number of arguments */
|
||||||
|
int i;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
for (i=1; i<=n; i++) {
|
||||||
|
int c = luaL_check_int(L, i);
|
||||||
|
luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
|
||||||
|
luaL_putchar(&b, (unsigned char)c);
|
||||||
|
}
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** PATTERN MATCHING
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MAX_CAPTURES
|
||||||
|
#define MAX_CAPTURES 32 /* arbitrary limit */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
struct Capture {
|
||||||
|
const char *src_end; /* end ('\0') of source string */
|
||||||
|
int level; /* total number of captures (finished or unfinished) */
|
||||||
|
struct {
|
||||||
|
const char *init;
|
||||||
|
long len; /* -1 signals unfinished capture */
|
||||||
|
} capture[MAX_CAPTURES];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define ESC '%'
|
||||||
|
#define SPECIALS "^$*+?.([%-"
|
||||||
|
|
||||||
|
|
||||||
|
static int check_capture (lua_State *L, int l, struct Capture *cap) {
|
||||||
|
l -= '1';
|
||||||
|
if (!(0 <= l && l < cap->level && cap->capture[l].len != -1))
|
||||||
|
lua_error(L, "invalid capture index");
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int capture_to_close (lua_State *L, struct Capture *cap) {
|
||||||
|
int level = cap->level;
|
||||||
|
for (level--; level>=0; level--)
|
||||||
|
if (cap->capture[level].len == -1) return level;
|
||||||
|
lua_error(L, "invalid pattern capture");
|
||||||
|
return 0; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const char *luaI_classend (lua_State *L, const char *p) {
|
||||||
|
switch (*p++) {
|
||||||
|
case ESC:
|
||||||
|
if (*p == '\0') lua_error(L, "malformed pattern (ends with `%')");
|
||||||
|
return p+1;
|
||||||
|
case '[':
|
||||||
|
if (*p == '^') p++;
|
||||||
|
do { /* look for a ']' */
|
||||||
|
if (*p == '\0') lua_error(L, "malformed pattern (missing `]')");
|
||||||
|
if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */
|
||||||
|
} while (*p != ']');
|
||||||
|
return p+1;
|
||||||
|
default:
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int match_class (int c, int cl) {
|
||||||
|
int res;
|
||||||
|
switch (tolower(cl)) {
|
||||||
|
case 'a' : res = isalpha(c); break;
|
||||||
|
case 'c' : res = iscntrl(c); break;
|
||||||
|
case 'd' : res = isdigit(c); break;
|
||||||
|
case 'l' : res = islower(c); break;
|
||||||
|
case 'p' : res = ispunct(c); break;
|
||||||
|
case 's' : res = isspace(c); break;
|
||||||
|
case 'u' : res = isupper(c); break;
|
||||||
|
case 'w' : res = isalnum(c); break;
|
||||||
|
case 'x' : res = isxdigit(c); break;
|
||||||
|
case 'z' : res = (c == '\0'); break;
|
||||||
|
default: return (cl == c);
|
||||||
|
}
|
||||||
|
return (islower(cl) ? res : !res);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int matchbracketclass (int c, const char *p, const char *endclass) {
|
||||||
|
int sig = 1;
|
||||||
|
if (*(p+1) == '^') {
|
||||||
|
sig = 0;
|
||||||
|
p++; /* skip the '^' */
|
||||||
|
}
|
||||||
|
while (++p < endclass) {
|
||||||
|
if (*p == ESC) {
|
||||||
|
p++;
|
||||||
|
if (match_class(c, (unsigned char)*p))
|
||||||
|
return sig;
|
||||||
|
}
|
||||||
|
else if ((*(p+1) == '-') && (p+2 < endclass)) {
|
||||||
|
p+=2;
|
||||||
|
if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p)
|
||||||
|
return sig;
|
||||||
|
}
|
||||||
|
else if ((int)(unsigned char)*p == c) return sig;
|
||||||
|
}
|
||||||
|
return !sig;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int luaI_singlematch (int c, const char *p, const char *ep) {
|
||||||
|
switch (*p) {
|
||||||
|
case '.': /* matches any char */
|
||||||
|
return 1;
|
||||||
|
case ESC:
|
||||||
|
return match_class(c, (unsigned char)*(p+1));
|
||||||
|
case '[':
|
||||||
|
return matchbracketclass(c, p, ep-1);
|
||||||
|
default:
|
||||||
|
return ((unsigned char)*p == c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *match (lua_State *L, const char *s, const char *p,
|
||||||
|
struct Capture *cap);
|
||||||
|
|
||||||
|
|
||||||
|
static const char *matchbalance (lua_State *L, const char *s, const char *p,
|
||||||
|
struct Capture *cap) {
|
||||||
|
if (*p == 0 || *(p+1) == 0)
|
||||||
|
lua_error(L, "unbalanced pattern");
|
||||||
|
if (*s != *p) return NULL;
|
||||||
|
else {
|
||||||
|
int b = *p;
|
||||||
|
int e = *(p+1);
|
||||||
|
int cont = 1;
|
||||||
|
while (++s < cap->src_end) {
|
||||||
|
if (*s == e) {
|
||||||
|
if (--cont == 0) return s+1;
|
||||||
|
}
|
||||||
|
else if (*s == b) cont++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL; /* string ends out of balance */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *max_expand (lua_State *L, const char *s, const char *p,
|
||||||
|
const char *ep, struct Capture *cap) {
|
||||||
|
long i = 0; /* counts maximum expand for item */
|
||||||
|
while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep))
|
||||||
|
i++;
|
||||||
|
/* keeps trying to match with the maximum repetitions */
|
||||||
|
while (i>=0) {
|
||||||
|
const char *res = match(L, (s+i), ep+1, cap);
|
||||||
|
if (res) return res;
|
||||||
|
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *min_expand (lua_State *L, const char *s, const char *p,
|
||||||
|
const char *ep, struct Capture *cap) {
|
||||||
|
for (;;) {
|
||||||
|
const char *res = match(L, s, ep+1, cap);
|
||||||
|
if (res != NULL)
|
||||||
|
return res;
|
||||||
|
else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep))
|
||||||
|
s++; /* try with one more repetition */
|
||||||
|
else return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *start_capture (lua_State *L, const char *s, const char *p,
|
||||||
|
struct Capture *cap) {
|
||||||
|
const char *res;
|
||||||
|
int level = cap->level;
|
||||||
|
if (level >= MAX_CAPTURES) lua_error(L, "too many captures");
|
||||||
|
cap->capture[level].init = s;
|
||||||
|
cap->capture[level].len = -1;
|
||||||
|
cap->level = level+1;
|
||||||
|
if ((res=match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||||
|
cap->level--; /* undo capture */
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *end_capture (lua_State *L, const char *s, const char *p,
|
||||||
|
struct Capture *cap) {
|
||||||
|
int l = capture_to_close(L, cap);
|
||||||
|
const char *res;
|
||||||
|
cap->capture[l].len = s - cap->capture[l].init; /* close capture */
|
||||||
|
if ((res = match(L, s, p+1, cap)) == NULL) /* match failed? */
|
||||||
|
cap->capture[l].len = -1; /* undo capture */
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *match_capture (lua_State *L, const char *s, int level,
|
||||||
|
struct Capture *cap) {
|
||||||
|
int l = check_capture(L, level, cap);
|
||||||
|
size_t len = cap->capture[l].len;
|
||||||
|
if ((size_t)(cap->src_end-s) >= len &&
|
||||||
|
memcmp(cap->capture[l].init, s, len) == 0)
|
||||||
|
return s+len;
|
||||||
|
else return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const char *match (lua_State *L, const char *s, const char *p,
|
||||||
|
struct Capture *cap) {
|
||||||
|
init: /* using goto's to optimize tail recursion */
|
||||||
|
switch (*p) {
|
||||||
|
case '(': /* start capture */
|
||||||
|
return start_capture(L, s, p, cap);
|
||||||
|
case ')': /* end capture */
|
||||||
|
return end_capture(L, s, p, cap);
|
||||||
|
case ESC: /* may be %[0-9] or %b */
|
||||||
|
if (isdigit((unsigned char)(*(p+1)))) { /* capture? */
|
||||||
|
s = match_capture(L, s, *(p+1), cap);
|
||||||
|
if (s == NULL) return NULL;
|
||||||
|
p+=2; goto init; /* else return match(L, s, p+2, cap) */
|
||||||
|
}
|
||||||
|
else if (*(p+1) == 'b') { /* balanced string? */
|
||||||
|
s = matchbalance(L, s, p+2, cap);
|
||||||
|
if (s == NULL) return NULL;
|
||||||
|
p+=4; goto init; /* else return match(L, s, p+4, cap); */
|
||||||
|
}
|
||||||
|
else goto dflt; /* case default */
|
||||||
|
case '\0': /* end of pattern */
|
||||||
|
return s; /* match succeeded */
|
||||||
|
case '$':
|
||||||
|
if (*(p+1) == '\0') /* is the '$' the last char in pattern? */
|
||||||
|
return (s == cap->src_end) ? s : NULL; /* check end of string */
|
||||||
|
else goto dflt;
|
||||||
|
default: dflt: { /* it is a pattern item */
|
||||||
|
const char *ep = luaI_classend(L, p); /* points to what is next */
|
||||||
|
int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep);
|
||||||
|
switch (*ep) {
|
||||||
|
case '?': { /* optional */
|
||||||
|
const char *res;
|
||||||
|
if (m && ((res=match(L, s+1, ep+1, cap)) != NULL))
|
||||||
|
return res;
|
||||||
|
p=ep+1; goto init; /* else return match(L, s, ep+1, cap); */
|
||||||
|
}
|
||||||
|
case '*': /* 0 or more repetitions */
|
||||||
|
return max_expand(L, s, p, ep, cap);
|
||||||
|
case '+': /* 1 or more repetitions */
|
||||||
|
return (m ? max_expand(L, s+1, p, ep, cap) : NULL);
|
||||||
|
case '-': /* 0 or more repetitions (minimum) */
|
||||||
|
return min_expand(L, s, p, ep, cap);
|
||||||
|
default:
|
||||||
|
if (!m) return NULL;
|
||||||
|
s++; p=ep; goto init; /* else return match(L, s+1, ep, cap); */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static const char *lmemfind (const char *s1, size_t l1,
|
||||||
|
const char *s2, size_t l2) {
|
||||||
|
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||||
|
else if (l2 > l1) return NULL; /* avoids a negative `l1' */
|
||||||
|
else {
|
||||||
|
const char *init; /* to search for a `*s2' inside `s1' */
|
||||||
|
l2--; /* 1st char will be checked by `memchr' */
|
||||||
|
l1 = l1-l2; /* `s2' cannot be found after that */
|
||||||
|
while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
|
||||||
|
init++; /* 1st char is already checked */
|
||||||
|
if (memcmp(init, s2+1, l2) == 0)
|
||||||
|
return init-1;
|
||||||
|
else { /* correct `l1' and `s1' to try again */
|
||||||
|
l1 -= init-s1;
|
||||||
|
s1 = init;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL; /* not found */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int push_captures (lua_State *L, struct Capture *cap) {
|
||||||
|
int i;
|
||||||
|
luaL_checkstack(L, cap->level, "too many captures");
|
||||||
|
for (i=0; i<cap->level; i++) {
|
||||||
|
int l = cap->capture[i].len;
|
||||||
|
if (l == -1) lua_error(L, "unfinished capture");
|
||||||
|
lua_pushlstring(L, cap->capture[i].init, l);
|
||||||
|
}
|
||||||
|
return cap->level; /* number of strings pushed */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_find (lua_State *L) {
|
||||||
|
size_t l1, l2;
|
||||||
|
const char *s = luaL_check_lstr(L, 1, &l1);
|
||||||
|
const char *p = luaL_check_lstr(L, 2, &l2);
|
||||||
|
long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
|
||||||
|
struct Capture cap;
|
||||||
|
luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range");
|
||||||
|
if (lua_gettop(L) > 3 || /* extra argument? */
|
||||||
|
strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
|
||||||
|
const char *s2 = lmemfind(s+init, l1-init, p, l2);
|
||||||
|
if (s2) {
|
||||||
|
lua_pushnumber(L, s2-s+1);
|
||||||
|
lua_pushnumber(L, s2-s+l2);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||||
|
const char *s1=s+init;
|
||||||
|
cap.src_end = s+l1;
|
||||||
|
do {
|
||||||
|
const char *res;
|
||||||
|
cap.level = 0;
|
||||||
|
if ((res=match(L, s1, p, &cap)) != NULL) {
|
||||||
|
lua_pushnumber(L, s1-s+1); /* start */
|
||||||
|
lua_pushnumber(L, res-s); /* end */
|
||||||
|
return push_captures(L, &cap) + 2;
|
||||||
|
}
|
||||||
|
} while (s1++<cap.src_end && !anchor);
|
||||||
|
}
|
||||||
|
lua_pushnil(L); /* not found */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void add_s (lua_State *L, luaL_Buffer *b, struct Capture *cap) {
|
||||||
|
if (lua_isstring(L, 3)) {
|
||||||
|
const char *news = lua_tostring(L, 3);
|
||||||
|
size_t l = lua_strlen(L, 3);
|
||||||
|
size_t i;
|
||||||
|
for (i=0; i<l; i++) {
|
||||||
|
if (news[i] != ESC)
|
||||||
|
luaL_putchar(b, news[i]);
|
||||||
|
else {
|
||||||
|
i++; /* skip ESC */
|
||||||
|
if (!isdigit((unsigned char)news[i]))
|
||||||
|
luaL_putchar(b, news[i]);
|
||||||
|
else {
|
||||||
|
int level = check_capture(L, news[i], cap);
|
||||||
|
luaL_addlstring(b, cap->capture[level].init, cap->capture[level].len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { /* is a function */
|
||||||
|
int n;
|
||||||
|
lua_pushvalue(L, 3);
|
||||||
|
n = push_captures(L, cap);
|
||||||
|
lua_rawcall(L, n, 1);
|
||||||
|
if (lua_isstring(L, -1))
|
||||||
|
luaL_addvalue(b); /* add return to accumulated result */
|
||||||
|
else
|
||||||
|
lua_pop(L, 1); /* function result is not a string: pop it */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int str_gsub (lua_State *L) {
|
||||||
|
size_t srcl;
|
||||||
|
const char *src = luaL_check_lstr(L, 1, &srcl);
|
||||||
|
const char *p = luaL_check_string(L, 2);
|
||||||
|
int max_s = luaL_opt_int(L, 4, srcl+1);
|
||||||
|
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||||
|
int n = 0;
|
||||||
|
struct Capture cap;
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_arg_check(L,
|
||||||
|
lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
|
||||||
|
3, "string or function expected");
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
cap.src_end = src+srcl;
|
||||||
|
while (n < max_s) {
|
||||||
|
const char *e;
|
||||||
|
cap.level = 0;
|
||||||
|
e = match(L, src, p, &cap);
|
||||||
|
if (e) {
|
||||||
|
n++;
|
||||||
|
add_s(L, &b, &cap);
|
||||||
|
}
|
||||||
|
if (e && e>src) /* non empty match? */
|
||||||
|
src = e; /* skip it */
|
||||||
|
else if (src < cap.src_end)
|
||||||
|
luaL_putchar(&b, *src++);
|
||||||
|
else break;
|
||||||
|
if (anchor) break;
|
||||||
|
}
|
||||||
|
luaL_addlstring(&b, src, cap.src_end-src);
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
lua_pushnumber(L, n); /* number of substitutions */
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, arg, &l);
|
||||||
|
luaL_putchar(b, '"');
|
||||||
|
while (l--) {
|
||||||
|
switch (*s) {
|
||||||
|
case '"': case '\\': case '\n':
|
||||||
|
luaL_putchar(b, '\\');
|
||||||
|
luaL_putchar(b, *s);
|
||||||
|
break;
|
||||||
|
case '\0': luaL_addlstring(b, "\\000", 4); break;
|
||||||
|
default: luaL_putchar(b, *s);
|
||||||
|
}
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
luaL_putchar(b, '"');
|
||||||
|
}
|
||||||
|
|
||||||
|
/* maximum size of each formated item (> len(format('%99.99f', -1e308))) */
|
||||||
|
#define MAX_ITEM 512
|
||||||
|
/* maximum size of each format specification (such as '%-099.99d') */
|
||||||
|
#define MAX_FORMAT 20
|
||||||
|
|
||||||
|
static int str_format (lua_State *L) {
|
||||||
|
int arg = 1;
|
||||||
|
const char *strfrmt = luaL_check_string(L, arg);
|
||||||
|
luaL_Buffer b;
|
||||||
|
luaL_buffinit(L, &b);
|
||||||
|
while (*strfrmt) {
|
||||||
|
if (*strfrmt != '%')
|
||||||
|
luaL_putchar(&b, *strfrmt++);
|
||||||
|
else if (*++strfrmt == '%')
|
||||||
|
luaL_putchar(&b, *strfrmt++); /* %% */
|
||||||
|
else { /* format item */
|
||||||
|
struct Capture cap;
|
||||||
|
char form[MAX_FORMAT]; /* to store the format ('%...') */
|
||||||
|
char buff[MAX_ITEM]; /* to store the formatted item */
|
||||||
|
const char *initf = strfrmt;
|
||||||
|
form[0] = '%';
|
||||||
|
if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
|
||||||
|
arg = *initf - '0';
|
||||||
|
initf += 2; /* skip the 'n$' */
|
||||||
|
}
|
||||||
|
arg++;
|
||||||
|
cap.src_end = strfrmt+strlen(strfrmt)+1;
|
||||||
|
cap.level = 0;
|
||||||
|
strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
|
||||||
|
if (cap.capture[0].len > 2 || cap.capture[1].len > 2 || /* < 100? */
|
||||||
|
strfrmt-initf > MAX_FORMAT-2)
|
||||||
|
lua_error(L, "invalid format (width or precision too long)");
|
||||||
|
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
|
||||||
|
form[strfrmt-initf+2] = 0;
|
||||||
|
switch (*strfrmt++) {
|
||||||
|
case 'c': case 'd': case 'i':
|
||||||
|
sprintf(buff, form, luaL_check_int(L, arg));
|
||||||
|
break;
|
||||||
|
case 'o': case 'u': case 'x': case 'X':
|
||||||
|
sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
|
||||||
|
break;
|
||||||
|
case 'e': case 'E': case 'f': case 'g': case 'G':
|
||||||
|
sprintf(buff, form, luaL_check_number(L, arg));
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
luaI_addquoted(L, &b, arg);
|
||||||
|
continue; /* skip the "addsize" at the end */
|
||||||
|
case 's': {
|
||||||
|
size_t l;
|
||||||
|
const char *s = luaL_check_lstr(L, arg, &l);
|
||||||
|
if (cap.capture[1].len == 0 && l >= 100) {
|
||||||
|
/* no precision and string is too long to be formatted;
|
||||||
|
keep original string */
|
||||||
|
lua_pushvalue(L, arg);
|
||||||
|
luaL_addvalue(&b);
|
||||||
|
continue; /* skip the "addsize" at the end */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sprintf(buff, form, s);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
default: /* also treat cases 'pnLlh' */
|
||||||
|
lua_error(L, "invalid option in `format'");
|
||||||
|
}
|
||||||
|
luaL_addlstring(&b, buff, strlen(buff));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
luaL_pushresult(&b);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg strlib[] = {
|
||||||
|
{"strlen", str_len},
|
||||||
|
{"strsub", str_sub},
|
||||||
|
{"strlower", str_lower},
|
||||||
|
{"strupper", str_upper},
|
||||||
|
{"strchar", str_char},
|
||||||
|
{"strrep", str_rep},
|
||||||
|
{"ascii", str_byte}, /* for compatibility with 3.0 and earlier */
|
||||||
|
{"strbyte", str_byte},
|
||||||
|
{"format", str_format},
|
||||||
|
{"strfind", str_find},
|
||||||
|
{"gsub", str_gsub}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Open string library
|
||||||
|
*/
|
||||||
|
void lua_strlibopen (lua_State *L) {
|
||||||
|
luaL_openl(L, strlib);
|
||||||
|
}
|
||||||
303
ltable.c
Normal file
303
ltable.c
Normal file
@@ -0,0 +1,303 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ltable.c,v 1.54 2000/08/31 14:08:27 roberto Exp roberto $
|
||||||
|
** Lua tables (hash)
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Implementation of tables (aka arrays, objects, or hash tables);
|
||||||
|
** uses a mix of chained scatter table with Brent's variation.
|
||||||
|
** A main invariant of these tables is that, if an element is not
|
||||||
|
** in its main position (i.e. the `original' position that its hash gives
|
||||||
|
** to it), then the colliding element is in its own main position.
|
||||||
|
** In other words, there are collisions only when two elements have the
|
||||||
|
** same main position (i.e. the same hash values for that table size).
|
||||||
|
** Because of that, the load factor of these tables can be 100% without
|
||||||
|
** performance penalties.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define gcsize(L, n) numblocks(L, n*2, sizeof(Hash))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define TagDefault TAG_TABLE
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** returns the `main' position of an element in a table (that is, the index
|
||||||
|
** of its hash value)
|
||||||
|
*/
|
||||||
|
Node *luaH_mainposition (const Hash *t, const TObject *key) {
|
||||||
|
unsigned long h;
|
||||||
|
switch (ttype(key)) {
|
||||||
|
case TAG_NUMBER:
|
||||||
|
h = (unsigned long)(long)nvalue(key);
|
||||||
|
break;
|
||||||
|
case TAG_STRING:
|
||||||
|
h = tsvalue(key)->u.s.hash;
|
||||||
|
break;
|
||||||
|
case TAG_USERDATA:
|
||||||
|
h = IntPoint(tsvalue(key));
|
||||||
|
break;
|
||||||
|
case TAG_TABLE:
|
||||||
|
h = IntPoint(hvalue(key));
|
||||||
|
break;
|
||||||
|
case TAG_LCLOSURE: case TAG_CCLOSURE:
|
||||||
|
h = IntPoint(clvalue(key));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL; /* invalid key */
|
||||||
|
}
|
||||||
|
LUA_ASSERT(h%(unsigned int)t->size == (h&((unsigned int)t->size-1)),
|
||||||
|
"a&(x-1) == a%x, for x power of 2");
|
||||||
|
return &t->node[h&(t->size-1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static const TObject *luaH_getany (lua_State *L, const Hash *t,
|
||||||
|
const TObject *key) {
|
||||||
|
Node *n = luaH_mainposition(t, key);
|
||||||
|
if (!n)
|
||||||
|
lua_error(L, "table index is nil");
|
||||||
|
else do {
|
||||||
|
if (luaO_equalObj(key, &n->key))
|
||||||
|
return &n->val;
|
||||||
|
n = n->next;
|
||||||
|
} while (n);
|
||||||
|
return &luaO_nilobject; /* key not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* specialized version for numbers */
|
||||||
|
const TObject *luaH_getnum (const Hash *t, Number key) {
|
||||||
|
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
|
||||||
|
do {
|
||||||
|
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key)
|
||||||
|
return &n->val;
|
||||||
|
n = n->next;
|
||||||
|
} while (n);
|
||||||
|
return &luaO_nilobject; /* key not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* specialized version for strings */
|
||||||
|
const TObject *luaH_getstr (const Hash *t, TString *key) {
|
||||||
|
Node *n = &t->node[key->u.s.hash&(t->size-1)];
|
||||||
|
do {
|
||||||
|
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key)
|
||||||
|
return &n->val;
|
||||||
|
n = n->next;
|
||||||
|
} while (n);
|
||||||
|
return &luaO_nilobject; /* key not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
|
||||||
|
switch (ttype(key)) {
|
||||||
|
case TAG_NUMBER: return luaH_getnum(t, nvalue(key));
|
||||||
|
case TAG_STRING: return luaH_getstr(t, tsvalue(key));
|
||||||
|
default: return luaH_getany(L, t, key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
|
||||||
|
int i;
|
||||||
|
if (ttype(key) == TAG_NIL)
|
||||||
|
i = 0; /* first iteration */
|
||||||
|
else {
|
||||||
|
const TObject *v = luaH_get(L, t, key);
|
||||||
|
if (v == &luaO_nilobject)
|
||||||
|
lua_error(L, "invalid key for `next'");
|
||||||
|
i = (int)(((const char *)v -
|
||||||
|
(const char *)(&t->node[0].val)) / sizeof(Node)) + 1;
|
||||||
|
}
|
||||||
|
for (; i<t->size; i++) {
|
||||||
|
Node *n = node(t, i);
|
||||||
|
if (ttype(val(n)) != TAG_NIL)
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return NULL; /* no more elements */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** try to remove a key without value from a table. To avoid problems with
|
||||||
|
** hash, change `key' for a number with the same hash.
|
||||||
|
*/
|
||||||
|
void luaH_remove (Hash *t, TObject *key) {
|
||||||
|
if (ttype(key) == TAG_NUMBER ||
|
||||||
|
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30))
|
||||||
|
return; /* do not remove numbers nor small strings */
|
||||||
|
else {
|
||||||
|
/* try to find a number `n' with the same hash as `key' */
|
||||||
|
Node *mp = luaH_mainposition(t, key);
|
||||||
|
int n = mp - &t->node[0];
|
||||||
|
/* make sure `n' is not in `t' */
|
||||||
|
while (luaH_getnum(t, n) != &luaO_nilobject) {
|
||||||
|
if (n >= MAX_INT - t->size)
|
||||||
|
return; /* give up; (to avoid overflow) */
|
||||||
|
n += t->size;
|
||||||
|
}
|
||||||
|
ttype(key) = TAG_NUMBER;
|
||||||
|
nvalue(key) = n;
|
||||||
|
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void setnodevector (lua_State *L, Hash *t, lint32 size) {
|
||||||
|
int i;
|
||||||
|
if (size > MAX_INT)
|
||||||
|
lua_error(L, "table overflow");
|
||||||
|
t->node = luaM_newvector(L, size, Node);
|
||||||
|
for (i=0; i<(int)size; i++) {
|
||||||
|
ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL;
|
||||||
|
t->node[i].next = NULL;
|
||||||
|
}
|
||||||
|
L->nblocks += gcsize(L, size) - gcsize(L, t->size);
|
||||||
|
t->size = size;
|
||||||
|
t->firstfree = &t->node[size-1]; /* first free position to be used */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Hash *luaH_new (lua_State *L, int size) {
|
||||||
|
Hash *t = luaM_new(L, Hash);
|
||||||
|
t->htag = TagDefault;
|
||||||
|
t->next = L->roottable;
|
||||||
|
L->roottable = t;
|
||||||
|
t->mark = t;
|
||||||
|
t->size = 0;
|
||||||
|
L->nblocks += gcsize(L, 0);
|
||||||
|
t->node = NULL;
|
||||||
|
setnodevector(L, t, luaO_power2(size));
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaH_free (lua_State *L, Hash *t) {
|
||||||
|
L->nblocks -= gcsize(L, t->size);
|
||||||
|
luaM_free(L, t->node);
|
||||||
|
luaM_free(L, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int numuse (const Hash *t) {
|
||||||
|
Node *v = t->node;
|
||||||
|
int size = t->size;
|
||||||
|
int realuse = 0;
|
||||||
|
int i;
|
||||||
|
for (i=0; i<size; i++) {
|
||||||
|
if (ttype(&v[i].val) != TAG_NIL)
|
||||||
|
realuse++;
|
||||||
|
}
|
||||||
|
return realuse;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void rehash (lua_State *L, Hash *t) {
|
||||||
|
int oldsize = t->size;
|
||||||
|
Node *nold = t->node;
|
||||||
|
int nelems = numuse(t);
|
||||||
|
int i;
|
||||||
|
LUA_ASSERT(nelems<=oldsize, "wrong count");
|
||||||
|
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
|
||||||
|
setnodevector(L, t, (lint32)oldsize*2);
|
||||||
|
else if (nelems <= oldsize/4 && /* less than 1/4? */
|
||||||
|
oldsize > MINPOWER2)
|
||||||
|
setnodevector(L, t, oldsize/2);
|
||||||
|
else
|
||||||
|
setnodevector(L, t, oldsize);
|
||||||
|
for (i=0; i<oldsize; i++) {
|
||||||
|
Node *old = nold+i;
|
||||||
|
if (ttype(&old->val) != TAG_NIL)
|
||||||
|
*luaH_set(L, t, &old->key) = old->val;
|
||||||
|
}
|
||||||
|
luaM_free(L, nold); /* free old array */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** inserts a key into a hash table; first, check whether key is
|
||||||
|
** already present; if not, check whether key's main position is free;
|
||||||
|
** if not, check whether colliding node is in its main position or not;
|
||||||
|
** if it is not, move colliding node to an empty place and put new key
|
||||||
|
** in its main position; otherwise (colliding node is in its main position),
|
||||||
|
** new key goes to an empty position.
|
||||||
|
*/
|
||||||
|
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) {
|
||||||
|
Node *mp = luaH_mainposition(t, key);
|
||||||
|
Node *n = mp;
|
||||||
|
if (!mp)
|
||||||
|
lua_error(L, "table index is nil");
|
||||||
|
do { /* check whether `key' is somewhere in the chain */
|
||||||
|
if (luaO_equalObj(key, &n->key))
|
||||||
|
return &n->val; /* that's all */
|
||||||
|
else n = n->next;
|
||||||
|
} while (n);
|
||||||
|
/* `key' not found; must insert it */
|
||||||
|
if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */
|
||||||
|
Node *othern; /* main position of colliding node */
|
||||||
|
n = t->firstfree; /* get a free place */
|
||||||
|
/* is colliding node out of its main position? (can only happens if
|
||||||
|
its position is after "firstfree") */
|
||||||
|
if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) {
|
||||||
|
/* yes; move colliding node into free position */
|
||||||
|
while (othern->next != mp) othern = othern->next; /* find previous */
|
||||||
|
othern->next = n; /* redo the chain with `n' in place of `mp' */
|
||||||
|
*n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
|
||||||
|
mp->next = NULL; /* now `mp' is free */
|
||||||
|
}
|
||||||
|
else { /* colliding node is in its own main position */
|
||||||
|
/* new node will go into free position */
|
||||||
|
n->next = mp->next; /* chain new position */
|
||||||
|
mp->next = n;
|
||||||
|
mp = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
mp->key = *key;
|
||||||
|
for (;;) { /* correct `firstfree' */
|
||||||
|
if (ttype(&t->firstfree->key) == TAG_NIL)
|
||||||
|
return &mp->val; /* OK; table still has a free place */
|
||||||
|
else if (t->firstfree == t->node) break; /* cannot decrement from here */
|
||||||
|
else (t->firstfree)--;
|
||||||
|
}
|
||||||
|
rehash(L, t); /* no more free places */
|
||||||
|
return luaH_set(L, t, key); /* `rehash' invalidates this insertion */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TObject *luaH_setint (lua_State *L, Hash *t, int key) {
|
||||||
|
TObject index;
|
||||||
|
ttype(&index) = TAG_NUMBER;
|
||||||
|
nvalue(&index) = key;
|
||||||
|
return luaH_set(L, t, &index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
|
||||||
|
TObject *value, index;
|
||||||
|
ttype(&index) = TAG_STRING;
|
||||||
|
tsvalue(&index) = key;
|
||||||
|
value = luaH_set(L, t, &index);
|
||||||
|
ttype(value) = TAG_NUMBER;
|
||||||
|
nvalue(value) = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const TObject *luaH_getglobal (lua_State *L, const char *name) {
|
||||||
|
return luaH_getstr(L->gt, luaS_new(L, name));
|
||||||
|
}
|
||||||
|
|
||||||
34
ltable.h
Normal file
34
ltable.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ltable.h,v 1.23 2000/06/06 16:31:41 roberto Exp roberto $
|
||||||
|
** Lua tables (hash)
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ltable_h
|
||||||
|
#define ltable_h
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define node(t,i) (&(t)->node[i])
|
||||||
|
#define key(n) (&(n)->key)
|
||||||
|
#define val(n) (&(n)->val)
|
||||||
|
|
||||||
|
Hash *luaH_new (lua_State *L, int nhash);
|
||||||
|
void luaH_free (lua_State *L, Hash *t);
|
||||||
|
const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
|
||||||
|
const TObject *luaH_getnum (const Hash *t, Number key);
|
||||||
|
const TObject *luaH_getstr (const Hash *t, TString *key);
|
||||||
|
void luaH_remove (Hash *t, TObject *key);
|
||||||
|
TObject *luaH_set (lua_State *L, Hash *t, const TObject *key);
|
||||||
|
Node * luaH_next (lua_State *L, const Hash *t, const TObject *r);
|
||||||
|
TObject *luaH_setint (lua_State *L, Hash *t, int key);
|
||||||
|
void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val);
|
||||||
|
unsigned long luaH_hash (lua_State *L, const TObject *key);
|
||||||
|
const TObject *luaH_getglobal (lua_State *L, const char *name);
|
||||||
|
|
||||||
|
/* exported only for debugging */
|
||||||
|
Node *luaH_mainposition (const Hash *t, const TObject *key);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
470
ltests.c
Normal file
470
ltests.c
Normal file
@@ -0,0 +1,470 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ltests.c,v 1.41 2000/09/11 19:42:57 roberto Exp roberto $
|
||||||
|
** Internal Module for Debugging of the Lua Implementation
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lapi.h"
|
||||||
|
#include "lauxlib.h"
|
||||||
|
#include "lcode.h"
|
||||||
|
#include "ldebug.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "luadebug.h"
|
||||||
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
|
void luaB_opentests (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** The whole module only makes sense with DEBUG on
|
||||||
|
*/
|
||||||
|
#ifdef DEBUG
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void setnameval (lua_State *L, const char *name, int val) {
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
lua_pushnumber(L, val);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** Disassembler
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
static const char *const instrname[NUM_OPCODES] = {
|
||||||
|
"END", "RETURN", "CALL", "TAILCALL", "PUSHNIL", "POP", "PUSHINT",
|
||||||
|
"PUSHSTRING", "PUSHNUM", "PUSHNEGNUM", "PUSHUPVALUE", "GETLOCAL",
|
||||||
|
"GETGLOBAL", "GETTABLE", "GETDOTTED", "GETINDEXED", "PUSHSELF",
|
||||||
|
"CREATETABLE", "SETLOCAL", "SETGLOBAL", "SETTABLE", "SETLIST", "SETMAP",
|
||||||
|
"ADD", "ADDI", "SUB", "MULT", "DIV", "POW", "CONCAT", "MINUS", "NOT",
|
||||||
|
"JMPNE", "JMPEQ", "JMPLT", "JMPLE", "JMPGT", "JMPGE", "JMPT", "JMPF",
|
||||||
|
"JMPONT", "JMPONF", "JMP", "PUSHNILJMP", "FORPREP", "FORLOOP", "LFORPREP",
|
||||||
|
"LFORLOOP", "CLOSURE"
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static int pushop (lua_State *L, Proto *p, int pc) {
|
||||||
|
char buff[100];
|
||||||
|
Instruction i = p->code[pc];
|
||||||
|
OpCode o = GET_OPCODE(i);
|
||||||
|
const char *name = instrname[o];
|
||||||
|
sprintf(buff, "%5d - ", luaG_getline(p->lineinfo, pc, 1, NULL));
|
||||||
|
switch ((enum Mode)luaK_opproperties[o].mode) {
|
||||||
|
case iO:
|
||||||
|
sprintf(buff+8, "%s", name);
|
||||||
|
break;
|
||||||
|
case iU:
|
||||||
|
sprintf(buff+8, "%-12s%4u", name, GETARG_U(i));
|
||||||
|
break;
|
||||||
|
case iS:
|
||||||
|
sprintf(buff+8, "%-12s%4d", name, GETARG_S(i));
|
||||||
|
break;
|
||||||
|
case iAB:
|
||||||
|
sprintf(buff+8, "%-12s%4d %4d", name, GETARG_A(i), GETARG_B(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lua_pushstring(L, buff);
|
||||||
|
return (o != OP_END);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int listcode (lua_State *L) {
|
||||||
|
int pc;
|
||||||
|
Proto *p;
|
||||||
|
int res;
|
||||||
|
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||||
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
|
lua_newtable(L);
|
||||||
|
setnameval(L, "maxstack", p->maxstacksize);
|
||||||
|
setnameval(L, "numparams", p->numparams);
|
||||||
|
pc = 0;
|
||||||
|
do {
|
||||||
|
lua_pushnumber(L, pc+1);
|
||||||
|
res = pushop(L, p, pc++);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
} while (res);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int liststrings (lua_State *L) {
|
||||||
|
Proto *p;
|
||||||
|
int i;
|
||||||
|
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||||
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
|
lua_newtable(L);
|
||||||
|
for (i=0; i<p->nkstr; i++) {
|
||||||
|
lua_pushnumber(L, i+1);
|
||||||
|
lua_pushstring(L, p->kstr[i]->str);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int listlocals (lua_State *L) {
|
||||||
|
Proto *p;
|
||||||
|
int pc = luaL_check_int(L, 2) - 1;
|
||||||
|
int i = 0;
|
||||||
|
const char *name;
|
||||||
|
luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected");
|
||||||
|
p = clvalue(luaA_index(L, 1))->f.l;
|
||||||
|
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
|
||||||
|
lua_pushstring(L, name);
|
||||||
|
return i-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static int get_limits (lua_State *L) {
|
||||||
|
lua_newtable(L);
|
||||||
|
setnameval(L, "BITS_INT", BITS_INT);
|
||||||
|
setnameval(L, "LFPF", LFIELDS_PER_FLUSH);
|
||||||
|
setnameval(L, "MAXARG_A", MAXARG_A);
|
||||||
|
setnameval(L, "MAXARG_B", MAXARG_B);
|
||||||
|
setnameval(L, "MAXARG_S", MAXARG_S);
|
||||||
|
setnameval(L, "MAXARG_U", MAXARG_U);
|
||||||
|
setnameval(L, "MAXLOCALS", MAXLOCALS);
|
||||||
|
setnameval(L, "MAXPARAMS", MAXPARAMS);
|
||||||
|
setnameval(L, "MAXSTACK", MAXSTACK);
|
||||||
|
setnameval(L, "MAXUPVALUES", MAXUPVALUES);
|
||||||
|
setnameval(L, "MAXVARSLH", MAXVARSLH);
|
||||||
|
setnameval(L, "RFPF", RFIELDS_PER_FLUSH);
|
||||||
|
setnameval(L, "SIZE_A", SIZE_A);
|
||||||
|
setnameval(L, "SIZE_B", SIZE_B);
|
||||||
|
setnameval(L, "SIZE_OP", SIZE_OP);
|
||||||
|
setnameval(L, "SIZE_U", SIZE_U);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int mem_query (lua_State *L) {
|
||||||
|
if (lua_isnull(L, 1)) {
|
||||||
|
lua_pushnumber(L, memdebug_total);
|
||||||
|
lua_pushnumber(L, memdebug_numblocks);
|
||||||
|
lua_pushnumber(L, memdebug_maxmem);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
memdebug_memlimit = luaL_check_int(L, 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int hash_query (lua_State *L) {
|
||||||
|
if (lua_isnull(L, 2)) {
|
||||||
|
luaL_arg_check(L, lua_tag(L, 1) == TAG_STRING, 1, "string expected");
|
||||||
|
lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Hash *t;
|
||||||
|
luaL_checktype(L, 2, "table");
|
||||||
|
t = hvalue(luaA_index(L, 2));
|
||||||
|
lua_pushnumber(L, luaH_mainposition(t, luaA_index(L, 1)) - t->node);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int table_query (lua_State *L) {
|
||||||
|
const Hash *t;
|
||||||
|
int i = luaL_opt_int(L, 2, -1);
|
||||||
|
luaL_checktype(L, 1, "table");
|
||||||
|
t = hvalue(luaA_index(L, 1));
|
||||||
|
if (i == -1) {
|
||||||
|
lua_pushnumber(L, t->size);
|
||||||
|
lua_pushnumber(L, t->firstfree - t->node);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else if (i < t->size) {
|
||||||
|
luaA_pushobject(L, &t->node[i].key);
|
||||||
|
luaA_pushobject(L, &t->node[i].val);
|
||||||
|
if (t->node[i].next) {
|
||||||
|
lua_pushnumber(L, t->node[i].next - t->node);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int string_query (lua_State *L) {
|
||||||
|
stringtable *tb = (*luaL_check_string(L, 1) == 's') ? &L->strt : &L->udt;
|
||||||
|
int s = luaL_opt_int(L, 2, 0) - 1;
|
||||||
|
if (s==-1) {
|
||||||
|
lua_pushnumber(L ,tb->nuse);
|
||||||
|
lua_pushnumber(L ,tb->size);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else if (s < tb->size) {
|
||||||
|
TString *ts;
|
||||||
|
int n = 0;
|
||||||
|
for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
|
||||||
|
ttype(L->top) = TAG_STRING;
|
||||||
|
tsvalue(L->top) = ts;
|
||||||
|
incr_top;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int tref (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "any");
|
||||||
|
lua_pushvalue(L, 1);
|
||||||
|
lua_pushnumber(L, lua_ref(L, luaL_opt_int(L, 2, 1)));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getref (lua_State *L) {
|
||||||
|
if (lua_getref(L, luaL_check_int(L, 1)))
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int unref (lua_State *L) {
|
||||||
|
lua_unref(L, luaL_check_int(L, 1));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int newuserdata (lua_State *L) {
|
||||||
|
lua_pushusertag(L, (void *)luaL_check_int(L, 1), luaL_check_int(L, 2));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int udataval (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "userdata");
|
||||||
|
lua_pushnumber(L, (int)lua_touserdata(L, 1));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int newstate (lua_State *L) {
|
||||||
|
lua_State *L1 = lua_open(luaL_check_int(L, 1));
|
||||||
|
if (L1)
|
||||||
|
lua_pushuserdata(L, L1);
|
||||||
|
else
|
||||||
|
lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int loadlib (lua_State *L) {
|
||||||
|
lua_State *L1 = (lua_State *)lua_touserdata(L, 1);
|
||||||
|
switch (*luaL_check_string(L, 2)) {
|
||||||
|
case 'm': lua_mathlibopen(L1); break;
|
||||||
|
case 's': lua_strlibopen(L1); break;
|
||||||
|
case 'i': lua_iolibopen(L1); break;
|
||||||
|
case 'd': lua_dblibopen(L1); break;
|
||||||
|
case 'b': lua_baselibopen(L1); break;
|
||||||
|
default: luaL_argerror(L, 2, "invalid option");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int closestate (lua_State *L) {
|
||||||
|
luaL_checktype(L, 1, "userdata");
|
||||||
|
lua_close((lua_State *)lua_touserdata(L, 1));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int doremote (lua_State *L) {
|
||||||
|
lua_State *L1;
|
||||||
|
const char *code = luaL_check_string(L, 2);
|
||||||
|
int status;
|
||||||
|
luaL_checktype(L, 1, "userdata");
|
||||||
|
L1 = (lua_State *)lua_touserdata(L, 1);
|
||||||
|
status = lua_dostring(L1, code);
|
||||||
|
if (status != 0) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushnumber(L, status);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int i = 0;
|
||||||
|
while (!lua_isnull(L1, ++i))
|
||||||
|
lua_pushstring(L, lua_tostring(L1, i));
|
||||||
|
return i-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int settagmethod (lua_State *L) {
|
||||||
|
luaL_checktype(L, 3, "any");
|
||||||
|
lua_settagmethod(L, luaL_check_int(L, 1), luaL_check_string(L, 2));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int pushbool (lua_State *L, int b) {
|
||||||
|
if (b) lua_pushnumber(L, 1);
|
||||||
|
else lua_pushnil(L);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int equal (lua_State *L) {
|
||||||
|
return pushbool(L, lua_equal(L, 1, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** {======================================================
|
||||||
|
** function to test the API with C. It interprets a kind of "assembler"
|
||||||
|
** language with calls to the API, so the test can be driven by Lua code
|
||||||
|
** =======================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
static const char *const delimits = " \t\n,;";
|
||||||
|
|
||||||
|
static void skip (const char **pc) {
|
||||||
|
while (**pc != '\0' && strchr(delimits, **pc)) (*pc)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int getnum (lua_State *L, const char **pc) {
|
||||||
|
int res = 0;
|
||||||
|
int sig = 1;
|
||||||
|
skip(pc);
|
||||||
|
if (**pc == '.') {
|
||||||
|
res = (int)lua_tonumber(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
(*pc)++;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else if (**pc == '-') {
|
||||||
|
sig = -1;
|
||||||
|
(*pc)++;
|
||||||
|
}
|
||||||
|
while (isdigit(**pc)) res = res*10 + (*(*pc)++) - '0';
|
||||||
|
return sig*res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *getname (char *buff, const char **pc) {
|
||||||
|
int i = 0;
|
||||||
|
skip(pc);
|
||||||
|
while (**pc != '\0' && !strchr(delimits, **pc))
|
||||||
|
buff[i++] = *(*pc)++;
|
||||||
|
buff[i] = '\0';
|
||||||
|
return buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define EQ(s1) (strcmp(s1, inst) == 0)
|
||||||
|
|
||||||
|
#define getnum ((getnum)(L, &pc))
|
||||||
|
#define getname ((getname)(buff, &pc))
|
||||||
|
|
||||||
|
|
||||||
|
static int testC (lua_State *L) {
|
||||||
|
char buff[30];
|
||||||
|
const char *pc = luaL_check_string(L, 1);
|
||||||
|
for (;;) {
|
||||||
|
const char *inst = getname;
|
||||||
|
if EQ("") return 0;
|
||||||
|
else if EQ("return") {
|
||||||
|
return getnum;
|
||||||
|
}
|
||||||
|
else if EQ("gettop") {
|
||||||
|
lua_pushnumber(L, lua_gettop(L));
|
||||||
|
}
|
||||||
|
else if EQ("settop") {
|
||||||
|
lua_settop(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("pop") {
|
||||||
|
lua_pop(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("pushnum") {
|
||||||
|
lua_pushnumber(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("pushvalue") {
|
||||||
|
lua_pushvalue(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("remove") {
|
||||||
|
lua_remove(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("insert") {
|
||||||
|
lua_insert(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("next") {
|
||||||
|
lua_next(L, -2);
|
||||||
|
}
|
||||||
|
else if EQ("concat") {
|
||||||
|
lua_concat(L, getnum);
|
||||||
|
}
|
||||||
|
else if EQ("call") {
|
||||||
|
int narg = getnum;
|
||||||
|
int nres = getnum;
|
||||||
|
lua_rawcall(L, narg, nres);
|
||||||
|
}
|
||||||
|
else if EQ("type") {
|
||||||
|
lua_pushstring(L, lua_type(L, getnum));
|
||||||
|
}
|
||||||
|
else luaL_verror(L, "unknown instruction %.30s", buff);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* }====================================================== */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static const struct luaL_reg tests_funcs[] = {
|
||||||
|
{"hash", hash_query},
|
||||||
|
{"limits", get_limits},
|
||||||
|
{"listcode", listcode},
|
||||||
|
{"liststrings", liststrings},
|
||||||
|
{"listlocals", listlocals},
|
||||||
|
{"loadlib", loadlib},
|
||||||
|
{"querystr", string_query},
|
||||||
|
{"querytab", table_query},
|
||||||
|
{"testC", testC},
|
||||||
|
{"ref", tref},
|
||||||
|
{"getref", getref},
|
||||||
|
{"unref", unref},
|
||||||
|
{"newuserdata", newuserdata},
|
||||||
|
{"udataval", udataval},
|
||||||
|
{"newstate", newstate},
|
||||||
|
{"closestate", closestate},
|
||||||
|
{"doremote", doremote},
|
||||||
|
{"settagmethod", settagmethod},
|
||||||
|
{"equal", equal},
|
||||||
|
{"totalmem", mem_query}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void luaB_opentests (lua_State *L) {
|
||||||
|
lua_newtable(L);
|
||||||
|
lua_getglobals(L);
|
||||||
|
lua_pushvalue(L, -2);
|
||||||
|
lua_setglobals(L);
|
||||||
|
luaL_openl(L, tests_funcs); /* open functions inside new table */
|
||||||
|
lua_setglobals(L); /* restore old table of globals */
|
||||||
|
lua_setglobal(L, "T"); /* set new table as global T */
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
159
ltm.c
Normal file
159
ltm.c
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ltm.c,v 1.48 2000/09/11 19:45:27 roberto Exp roberto $
|
||||||
|
** Tag methods
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
|
const char *const luaT_eventname[] = { /* ORDER IM */
|
||||||
|
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||||
|
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||||
|
"le", "gt", "ge", /* deprecated options!! */
|
||||||
|
NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static int findevent (const char *name) {
|
||||||
|
int i;
|
||||||
|
for (i=0; luaT_eventname[i]; i++)
|
||||||
|
if (strcmp(luaT_eventname[i], name) == 0)
|
||||||
|
return i;
|
||||||
|
return -1; /* name not found */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaI_checkevent (lua_State *L, const char *name, int t) {
|
||||||
|
int e = findevent(name);
|
||||||
|
if (e >= IM_N)
|
||||||
|
luaO_verror(L, "event `%.50s' is deprecated", name);
|
||||||
|
if (e == IM_GC && t == TAG_TABLE)
|
||||||
|
luaO_verror(L, "event `gc' for tables is deprecated");
|
||||||
|
if (e < 0)
|
||||||
|
luaO_verror(L, "`%.50s' is not a valid event name", name);
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* events in TAG_NIL are all allowed, since this is used as a
|
||||||
|
* 'placeholder' for "default" fallbacks
|
||||||
|
*/
|
||||||
|
/* ORDER LUA_T, ORDER IM */
|
||||||
|
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
||||||
|
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */
|
||||||
|
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */
|
||||||
|
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */
|
||||||
|
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */
|
||||||
|
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */
|
||||||
|
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */
|
||||||
|
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
|
||||||
|
};
|
||||||
|
|
||||||
|
int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||||
|
return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void init_entry (lua_State *L, int tag) {
|
||||||
|
int i;
|
||||||
|
for (i=0; i<IM_N; i++)
|
||||||
|
ttype(luaT_getim(L, tag, i)) = TAG_NIL;
|
||||||
|
L->IMtable[tag].collected = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaT_init (lua_State *L) {
|
||||||
|
int t;
|
||||||
|
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
|
||||||
|
L->last_tag = NUM_TAGS-1;
|
||||||
|
for (t=0; t<=L->last_tag; t++)
|
||||||
|
init_entry(L, t);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_newtag (lua_State *L) {
|
||||||
|
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM,
|
||||||
|
"tag table overflow", MAX_INT);
|
||||||
|
L->last_tag++;
|
||||||
|
init_entry(L, L->last_tag);
|
||||||
|
return L->last_tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void checktag (lua_State *L, int tag) {
|
||||||
|
if (!(0 <= tag && tag <= L->last_tag))
|
||||||
|
luaO_verror(L, "%d is not a valid tag", tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
void luaT_realtag (lua_State *L, int tag) {
|
||||||
|
if (!(NUM_TAGS <= tag && tag <= L->last_tag))
|
||||||
|
luaO_verror(L, "tag %d was not created by `newtag'", tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||||
|
int e;
|
||||||
|
checktag(L, tagto);
|
||||||
|
checktag(L, tagfrom);
|
||||||
|
for (e=0; e<IM_N; e++) {
|
||||||
|
if (luaT_validevent(tagto, e))
|
||||||
|
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e);
|
||||||
|
}
|
||||||
|
return tagto;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaT_effectivetag (lua_State *L, const TObject *o) {
|
||||||
|
lua_Type t = ttype(o);
|
||||||
|
switch (t) {
|
||||||
|
case TAG_USERDATA: {
|
||||||
|
int tag = tsvalue(o)->u.d.tag;
|
||||||
|
return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
|
||||||
|
}
|
||||||
|
case TAG_TABLE: return hvalue(o)->htag;
|
||||||
|
default: return t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_gettagmethod (lua_State *L, int t, const char *event) {
|
||||||
|
int e;
|
||||||
|
e = luaI_checkevent(L, event, t);
|
||||||
|
checktag(L, t);
|
||||||
|
if (luaT_validevent(t, e))
|
||||||
|
*L->top = *luaT_getim(L, t,e);
|
||||||
|
else
|
||||||
|
ttype(L->top) = TAG_NIL;
|
||||||
|
incr_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void lua_settagmethod (lua_State *L, int t, const char *event) {
|
||||||
|
TObject temp;
|
||||||
|
int e;
|
||||||
|
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
|
||||||
|
"function or nil expected");
|
||||||
|
e = luaI_checkevent(L, event, t);
|
||||||
|
checktag(L, t);
|
||||||
|
if (!luaT_validevent(t, e))
|
||||||
|
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||||
|
luaT_eventname[e], luaO_typenames[t],
|
||||||
|
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag"
|
||||||
|
: "");
|
||||||
|
temp = *(L->top - 1);
|
||||||
|
*(L->top - 1) = *luaT_getim(L, t,e);
|
||||||
|
*luaT_getim(L, t, e) = temp;
|
||||||
|
}
|
||||||
|
|
||||||
56
ltm.h
Normal file
56
ltm.h
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
** $Id: ltm.h,v 1.14 2000/08/07 20:21:34 roberto Exp roberto $
|
||||||
|
** Tag methods
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ltm_h
|
||||||
|
#define ltm_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* WARNING: if you change the order of this enumeration,
|
||||||
|
* grep "ORDER IM"
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
IM_GETTABLE = 0,
|
||||||
|
IM_SETTABLE,
|
||||||
|
IM_INDEX,
|
||||||
|
IM_GETGLOBAL,
|
||||||
|
IM_SETGLOBAL,
|
||||||
|
IM_ADD,
|
||||||
|
IM_SUB,
|
||||||
|
IM_MUL,
|
||||||
|
IM_DIV,
|
||||||
|
IM_POW,
|
||||||
|
IM_UNM,
|
||||||
|
IM_LT,
|
||||||
|
IM_CONCAT,
|
||||||
|
IM_GC,
|
||||||
|
IM_FUNCTION,
|
||||||
|
IM_N /* number of elements in the enum */
|
||||||
|
} IMS;
|
||||||
|
|
||||||
|
|
||||||
|
struct IM {
|
||||||
|
TObject int_method[IM_N];
|
||||||
|
TString *collected; /* list of G. collected udata with this tag */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
||||||
|
#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
|
||||||
|
|
||||||
|
extern const char *const luaT_eventname[];
|
||||||
|
|
||||||
|
|
||||||
|
void luaT_init (lua_State *L);
|
||||||
|
void luaT_realtag (lua_State *L, int tag);
|
||||||
|
int luaT_effectivetag (lua_State *L, const TObject *o);
|
||||||
|
int luaT_validevent (int t, int e); /* used by compatibility module */
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
361
lua.c
361
lua.c
@@ -1,74 +1,325 @@
|
|||||||
/*
|
/*
|
||||||
** lua.c
|
** $Id: lua.c,v 1.50 2000/09/05 19:33:32 roberto Exp roberto $
|
||||||
** Linguagem para Usuarios de Aplicacao
|
** Lua stand-alone interpreter
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_lua="$Id: lua.c,v 1.3 1994/12/14 19:58:20 celes Exp $";
|
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "lua.h"
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "luadebug.h"
|
||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
static int lua_argc;
|
|
||||||
static char **lua_argv;
|
lua_State *lua_state = NULL;
|
||||||
|
#define L lua_state
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef PROMPT
|
||||||
|
#define PROMPT "> "
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _POSIX_SOURCE
|
||||||
|
#include <unistd.h>
|
||||||
|
#else
|
||||||
|
static int isatty (int x) { return x==0; } /* assume stdin is a tty */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
%F Allow Lua code to access argv strings.
|
** global options
|
||||||
%i Receive from Lua the argument number (starting with 1).
|
|
||||||
%o Return to Lua the argument, or nil if it does not exist.
|
|
||||||
*/
|
*/
|
||||||
static void lua_getargv (void)
|
struct Options {
|
||||||
{
|
int toclose;
|
||||||
lua_Object lo = lua_getparam(1);
|
int stacksize;
|
||||||
if (!lua_isnumber(lo))
|
};
|
||||||
lua_pushnil();
|
|
||||||
else
|
|
||||||
{
|
typedef void (*handler)(int); /* type for signal actions */
|
||||||
int n = (int)lua_getnumber(lo);
|
|
||||||
if (n < 1 || n > lua_argc) lua_pushnil();
|
static void laction (int i);
|
||||||
else lua_pushstring(lua_argv[n]);
|
|
||||||
}
|
|
||||||
|
static lua_Hook old_linehook = NULL;
|
||||||
|
static lua_Hook old_callhook = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef USERINIT
|
||||||
|
extern void USERINIT (void);
|
||||||
|
#else
|
||||||
|
#define USERINIT userinit
|
||||||
|
static void userinit (void) {
|
||||||
|
lua_baselibopen(L);
|
||||||
|
lua_iolibopen(L);
|
||||||
|
lua_strlibopen(L);
|
||||||
|
lua_mathlibopen(L);
|
||||||
|
lua_dblibopen(L);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static handler lreset (void) {
|
||||||
|
return signal(SIGINT, laction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main (int argc, char *argv[])
|
static void lstop (void) {
|
||||||
{
|
lua_setlinehook(L, old_linehook);
|
||||||
int i;
|
lua_setcallhook(L, old_callhook);
|
||||||
int result = 0;
|
lreset();
|
||||||
iolib_open ();
|
lua_error(L, "interrupted!");
|
||||||
strlib_open ();
|
|
||||||
mathlib_open ();
|
|
||||||
|
|
||||||
lua_register("argv", lua_getargv);
|
|
||||||
|
|
||||||
if (argc < 2)
|
|
||||||
{
|
|
||||||
char buffer[250];
|
|
||||||
while (gets(buffer) != 0)
|
|
||||||
result = lua_dostring(buffer);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (i=1; i<argc; i++)
|
|
||||||
{
|
|
||||||
if (strcmp(argv[i], "--") == 0)
|
|
||||||
{
|
|
||||||
lua_argc = argc-i-1;
|
|
||||||
lua_argv = argv+i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (i=1; i<argc; i++)
|
|
||||||
{
|
|
||||||
if (strcmp(argv[i], "--") == 0)
|
|
||||||
break;
|
|
||||||
else
|
|
||||||
result = lua_dofile (argv[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void laction (int i) {
|
||||||
|
(void)i; /* to avoid warnings */
|
||||||
|
signal(SIGINT, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||||
|
terminate process (default action) */
|
||||||
|
old_linehook = lua_setlinehook(L, (lua_Hook)lstop);
|
||||||
|
old_callhook = lua_setcallhook(L, (lua_Hook)lstop);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int ldo (int (*f)(lua_State *l, const char *), const char *name) {
|
||||||
|
int res;
|
||||||
|
handler h = lreset();
|
||||||
|
int top = lua_gettop(L);
|
||||||
|
res = f(L, name); /* dostring | dofile */
|
||||||
|
lua_settop(L, top); /* remove eventual results */
|
||||||
|
signal(SIGINT, h); /* restore old action */
|
||||||
|
if (res == LUA_ERRMEM) {
|
||||||
|
/* Lua gives no message in such case, so lua.c provides one */
|
||||||
|
fprintf(stderr, "lua: memory allocation error\n");
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void print_message (void) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"usage: lua [options]. Available options are:\n"
|
||||||
|
" - execute stdin as a file\n"
|
||||||
|
" -c close Lua when exiting\n"
|
||||||
|
" -e stat execute string `stat'\n"
|
||||||
|
" -f name execute file `name' with remaining arguments in table `arg'\n"
|
||||||
|
" -i enter interactive mode with prompt\n"
|
||||||
|
" -q enter interactive mode without prompt\n"
|
||||||
|
" -sNUM set stack size to NUM (must be the first option)\n"
|
||||||
|
" -v print version information\n"
|
||||||
|
" a=b set global `a' to string `b'\n"
|
||||||
|
" name execute file `name'\n"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void print_version (void) {
|
||||||
|
printf("%s %s\n", LUA_VERSION, LUA_COPYRIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void assign (char *arg) {
|
||||||
|
char *eq = strchr(arg, '=');
|
||||||
|
*eq = '\0'; /* spilt `arg' in two strings (name & value) */
|
||||||
|
lua_pushstring(L, eq+1);
|
||||||
|
lua_setglobal(L, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void getargs (char *argv[]) {
|
||||||
|
int i;
|
||||||
|
lua_newtable(L);
|
||||||
|
for (i=0; argv[i]; i++) {
|
||||||
|
/* arg[i] = argv[i] */
|
||||||
|
lua_pushnumber(L, i);
|
||||||
|
lua_pushstring(L, argv[i]);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
/* arg.n = maximum index in table `arg' */
|
||||||
|
lua_pushstring(L, "n");
|
||||||
|
lua_pushnumber(L, i-1);
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int l_getargs (lua_State *l) {
|
||||||
|
char **argv = (char **)lua_touserdata(l, -1);
|
||||||
|
getargs(argv);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int file_input (const char *argv) {
|
||||||
|
int result = ldo(lua_dofile, argv);
|
||||||
|
if (result) {
|
||||||
|
if (result == LUA_ERRFILE) {
|
||||||
|
fprintf(stderr, "lua: cannot execute file ");
|
||||||
|
perror(argv);
|
||||||
|
}
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* maximum length of an input string */
|
||||||
|
#ifndef MAXINPUT
|
||||||
|
#define MAXINPUT BUFSIZ
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void manual_input (int version, int prompt) {
|
||||||
|
int cont = 1;
|
||||||
|
if (version) print_version();
|
||||||
|
while (cont) {
|
||||||
|
char buffer[MAXINPUT];
|
||||||
|
int i = 0;
|
||||||
|
if (prompt) {
|
||||||
|
const char *s;
|
||||||
|
lua_getglobal(L, "_PROMPT");
|
||||||
|
s = lua_tostring(L, -1);
|
||||||
|
if (!s) s = PROMPT;
|
||||||
|
fputs(s, stdout);
|
||||||
|
lua_pop(L, 1); /* remove global */
|
||||||
|
}
|
||||||
|
for(;;) {
|
||||||
|
int c = getchar();
|
||||||
|
if (c == EOF) {
|
||||||
|
cont = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (c == '\n') {
|
||||||
|
if (i>0 && buffer[i-1] == '\\')
|
||||||
|
buffer[i-1] = '\n';
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
else if (i >= MAXINPUT-1) {
|
||||||
|
fprintf(stderr, "lua: input line too long\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else buffer[i++] = (char)c;
|
||||||
|
}
|
||||||
|
buffer[i] = '\0';
|
||||||
|
ldo(lua_dostring, buffer);
|
||||||
|
lua_settop(L, 0); /* remove eventual results */
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int handle_argv (char *argv[], struct Options *opt) {
|
||||||
|
if (opt->stacksize > 0) argv++; /* skip option `-s' (if present) */
|
||||||
|
if (*argv == NULL) { /* no more arguments? */
|
||||||
|
if (isatty(0)) {
|
||||||
|
manual_input(1, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||||
|
}
|
||||||
|
else { /* other arguments; loop over them */
|
||||||
|
int i;
|
||||||
|
for (i = 0; argv[i] != NULL; i++) {
|
||||||
|
if (argv[i][0] != '-') { /* not an option? */
|
||||||
|
if (strchr(argv[i], '='))
|
||||||
|
assign(argv[i]);
|
||||||
|
else
|
||||||
|
if (file_input(argv[i]) != EXIT_SUCCESS)
|
||||||
|
return EXIT_FAILURE; /* stop if file fails */
|
||||||
|
}
|
||||||
|
else switch (argv[i][1]) { /* option */
|
||||||
|
case 0: {
|
||||||
|
ldo(lua_dofile, NULL); /* executes stdin as a file */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'i': {
|
||||||
|
manual_input(0, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'q': {
|
||||||
|
manual_input(0, 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'c': {
|
||||||
|
opt->toclose = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'v': {
|
||||||
|
print_version();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'e': {
|
||||||
|
i++;
|
||||||
|
if (argv[i] == NULL) {
|
||||||
|
print_message();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
if (ldo(lua_dostring, argv[i]) != 0) {
|
||||||
|
fprintf(stderr, "lua: error running argument `%s'\n", argv[i]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'f': {
|
||||||
|
i++;
|
||||||
|
if (argv[i] == NULL) {
|
||||||
|
print_message();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
getargs(argv+i); /* collect remaining arguments */
|
||||||
|
lua_setglobal(L, "arg");
|
||||||
|
return file_input(argv[i]); /* stop scanning arguments */
|
||||||
|
}
|
||||||
|
case 's': {
|
||||||
|
fprintf(stderr, "lua: stack size (`-s') must be the first option\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
print_message();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void getstacksize (int argc, char *argv[], struct Options *opt) {
|
||||||
|
if (argc >= 2 && argv[1][0] == '-' && argv[1][1] == 's') {
|
||||||
|
int stacksize = atoi(&argv[1][2]);
|
||||||
|
if (stacksize <= 0) {
|
||||||
|
fprintf(stderr, "lua: invalid stack size ('%s')\n", &argv[1][2]);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
opt->stacksize = stacksize;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
opt->stacksize = 0; /* no stack size */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void register_getargs (char *argv[]) {
|
||||||
|
lua_pushuserdata(L, argv);
|
||||||
|
lua_pushcclosure(L, l_getargs, 1);
|
||||||
|
lua_setglobal(L, "getargs");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main (int argc, char *argv[]) {
|
||||||
|
struct Options opt;
|
||||||
|
int status;
|
||||||
|
opt.toclose = 0;
|
||||||
|
getstacksize(argc, argv, &opt); /* handle option `-s' */
|
||||||
|
L = lua_open(opt.stacksize); /* create state */
|
||||||
|
USERINIT(); /* open libraries */
|
||||||
|
register_getargs(argv); /* create `getargs' function */
|
||||||
|
status = handle_argv(argv+1, &opt);
|
||||||
|
if (opt.toclose)
|
||||||
|
lua_close(L);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
282
lua.h
282
lua.h
@@ -1,100 +1,214 @@
|
|||||||
/*
|
/*
|
||||||
** LUA - Linguagem para Usuarios de Aplicacao
|
** $Id: lua.h,v 1.69 2000/09/14 14:09:31 roberto Exp roberto $
|
||||||
** Grupo de Tecnologia em Computacao Grafica
|
** Lua - An Extensible Extension Language
|
||||||
** TeCGraf - PUC-Rio
|
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
|
||||||
** $Id: lua.h,v 3.15 1995/01/18 20:15:05 celes Exp celes $
|
** e-mail: lua@tecgraf.puc-rio.br
|
||||||
|
** www: http://www.tecgraf.puc-rio.br/lua/
|
||||||
|
** See Copyright Notice at the end of this file
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lua_h
|
#ifndef lua_h
|
||||||
#define lua_h
|
#define lua_h
|
||||||
|
|
||||||
/* Private Part */
|
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
LUA_T_NIL = -1,
|
|
||||||
LUA_T_NUMBER = -2,
|
|
||||||
LUA_T_STRING = -3,
|
|
||||||
LUA_T_ARRAY = -4,
|
|
||||||
LUA_T_FUNCTION = -5,
|
|
||||||
LUA_T_CFUNCTION= -6,
|
|
||||||
LUA_T_USERDATA = 0
|
|
||||||
} lua_Type;
|
|
||||||
|
|
||||||
|
|
||||||
/* Public Part */
|
/* definition of `size_t' */
|
||||||
|
#include <stddef.h>
|
||||||
#define LUA_NOOBJECT 0
|
|
||||||
|
|
||||||
typedef void (*lua_CFunction) (void);
|
|
||||||
typedef unsigned int lua_Object;
|
|
||||||
|
|
||||||
lua_Object lua_setfallback (char *name, lua_CFunction fallback);
|
|
||||||
|
|
||||||
void lua_error (char *s);
|
|
||||||
int lua_dofile (char *filename);
|
|
||||||
int lua_dostring (char *string);
|
|
||||||
int lua_callfunction (lua_Object function);
|
|
||||||
int lua_call (char *funcname);
|
|
||||||
|
|
||||||
void lua_beginblock (void);
|
|
||||||
void lua_endblock (void);
|
|
||||||
|
|
||||||
lua_Object lua_getparam (int number);
|
|
||||||
#define lua_getresult(_) lua_getparam(_)
|
|
||||||
|
|
||||||
float lua_getnumber (lua_Object object);
|
|
||||||
char *lua_getstring (lua_Object object);
|
|
||||||
lua_CFunction lua_getcfunction (lua_Object object);
|
|
||||||
void *lua_getuserdata (lua_Object object);
|
|
||||||
|
|
||||||
void lua_pushnil (void);
|
|
||||||
void lua_pushnumber (float n);
|
|
||||||
void lua_pushstring (char *s);
|
|
||||||
void lua_pushliteral (char *s);
|
|
||||||
void lua_pushcfunction (lua_CFunction fn);
|
|
||||||
void lua_pushusertag (void *u, int tag);
|
|
||||||
void lua_pushobject (lua_Object object);
|
|
||||||
|
|
||||||
lua_Object lua_getglobal (char *name);
|
|
||||||
void lua_storeglobal (char *name);
|
|
||||||
|
|
||||||
void lua_storesubscript (void);
|
|
||||||
lua_Object lua_getsubscript (void);
|
|
||||||
|
|
||||||
int lua_type (lua_Object object);
|
|
||||||
|
|
||||||
int lua_lock (void);
|
|
||||||
lua_Object lua_getlocked (int ref);
|
|
||||||
void lua_pushlocked (int ref);
|
|
||||||
void lua_unlock (int ref);
|
|
||||||
|
|
||||||
lua_Object lua_createtable (void);
|
|
||||||
|
|
||||||
|
|
||||||
/* some useful macros */
|
#define LUA_VERSION "Lua 4.0 (beta)"
|
||||||
|
#define LUA_COPYRIGHT "Copyright (C) 1994-2000 TeCGraf, PUC-Rio"
|
||||||
#define lua_lockobject(o) (lua_pushobject(o), lua_lock())
|
#define LUA_AUTHORS "W. Celes, R. Ierusalimschy & L. H. de Figueiredo"
|
||||||
|
|
||||||
#define lua_register(n,f) (lua_pushcfunction(f), lua_storeglobal(n))
|
|
||||||
|
|
||||||
#define lua_pushuserdata(u) lua_pushusertag(u, LUA_T_USERDATA)
|
|
||||||
|
|
||||||
#define lua_isnil(_) (lua_type(_)==LUA_T_NIL)
|
|
||||||
#define lua_isnumber(_) (lua_type(_)==LUA_T_NUMBER)
|
|
||||||
#define lua_isstring(_) (lua_type(_)==LUA_T_STRING)
|
|
||||||
#define lua_istable(_) (lua_type(_)==LUA_T_ARRAY)
|
|
||||||
#define lua_isfunction(_) (lua_type(_)==LUA_T_FUNCTION)
|
|
||||||
#define lua_iscfunction(_) (lua_type(_)==LUA_T_CFUNCTION)
|
|
||||||
#define lua_isuserdata(_) (lua_type(_)>=LUA_T_USERDATA)
|
|
||||||
|
|
||||||
|
|
||||||
/* for lua 1.1 compatibility. Avoid using these macros */
|
#define LUA_ERRORMESSAGE "_ERRORMESSAGE"
|
||||||
|
|
||||||
#define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
|
|
||||||
#define lua_getfield(o,f) (lua_pushobject(o), lua_pushliteral(f), lua_getsubscript())
|
|
||||||
|
|
||||||
#define lua_copystring(o) (strdup(lua_getstring(o)))
|
#define LUA_NOREF (-2)
|
||||||
|
#define LUA_REFNIL (-1)
|
||||||
|
|
||||||
|
#define LUA_ANYTAG (-1)
|
||||||
|
#define LUA_NOTAG (-2)
|
||||||
|
|
||||||
|
#define LUA_MULTRET (-1)
|
||||||
|
|
||||||
|
|
||||||
|
#define LUA_MINSTACK 20
|
||||||
|
|
||||||
|
|
||||||
|
/* error codes for lua_do* */
|
||||||
|
#define LUA_ERRRUN 1
|
||||||
|
#define LUA_ERRFILE 2
|
||||||
|
#define LUA_ERRSYNTAX 3
|
||||||
|
#define LUA_ERRMEM 4
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct lua_State lua_State;
|
||||||
|
|
||||||
|
typedef int (*lua_CFunction) (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** state manipulation
|
||||||
|
*/
|
||||||
|
lua_State *lua_open (int stacksize);
|
||||||
|
void lua_close (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** basic stack manipulation
|
||||||
|
*/
|
||||||
|
int lua_gettop (lua_State *L);
|
||||||
|
void lua_settop (lua_State *L, int index);
|
||||||
|
void lua_pushvalue (lua_State *L, int index);
|
||||||
|
void lua_remove (lua_State *L, int index);
|
||||||
|
void lua_insert (lua_State *L, int index);
|
||||||
|
int lua_stackspace (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** access functions (stack -> C)
|
||||||
|
*/
|
||||||
|
|
||||||
|
const char *lua_type (lua_State *L, int index);
|
||||||
|
int lua_isnumber (lua_State *L, int index);
|
||||||
|
int lua_iscfunction (lua_State *L, int index);
|
||||||
|
int lua_tag (lua_State *L, int index);
|
||||||
|
|
||||||
|
int lua_equal (lua_State *L, int index1, int index2);
|
||||||
|
int lua_lessthan (lua_State *L, int index1, int index2);
|
||||||
|
|
||||||
|
double lua_tonumber (lua_State *L, int index);
|
||||||
|
const char *lua_tostring (lua_State *L, int index);
|
||||||
|
size_t lua_strlen (lua_State *L, int index);
|
||||||
|
lua_CFunction lua_tocfunction (lua_State *L, int index);
|
||||||
|
void *lua_touserdata (lua_State *L, int index);
|
||||||
|
const void *lua_topointer (lua_State *L, int index);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** push functions (C -> stack)
|
||||||
|
*/
|
||||||
|
void lua_pushnil (lua_State *L);
|
||||||
|
void lua_pushnumber (lua_State *L, double n);
|
||||||
|
void lua_pushlstring (lua_State *L, const char *s, size_t len);
|
||||||
|
void lua_pushstring (lua_State *L, const char *s);
|
||||||
|
void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
|
||||||
|
void lua_pushusertag (lua_State *L, void *u, int tag);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get functions (Lua -> stack)
|
||||||
|
*/
|
||||||
|
void lua_getglobal (lua_State *L, const char *name);
|
||||||
|
void lua_gettable (lua_State *L, int index);
|
||||||
|
void lua_rawget (lua_State *L, int index);
|
||||||
|
void lua_rawgeti (lua_State *L, int index, int n);
|
||||||
|
void lua_getglobals (lua_State *L);
|
||||||
|
void lua_gettagmethod (lua_State *L, int tag, const char *event);
|
||||||
|
|
||||||
|
int lua_getref (lua_State *L, int ref);
|
||||||
|
|
||||||
|
void lua_newtable (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** set functions (stack -> Lua)
|
||||||
|
*/
|
||||||
|
void lua_setglobal (lua_State *L, const char *name);
|
||||||
|
void lua_settable (lua_State *L, int index);
|
||||||
|
void lua_rawset (lua_State *L, int index);
|
||||||
|
void lua_rawseti (lua_State *L, int index, int n);
|
||||||
|
void lua_setglobals (lua_State *L);
|
||||||
|
void lua_settagmethod (lua_State *L, int tag, const char *event);
|
||||||
|
int lua_ref (lua_State *L, int lock);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** "do" functions (run Lua code)
|
||||||
|
*/
|
||||||
|
int lua_call (lua_State *L, int nargs, int nresults);
|
||||||
|
void lua_rawcall (lua_State *L, int nargs, int nresults);
|
||||||
|
int lua_dofile (lua_State *L, const char *filename);
|
||||||
|
int lua_dostring (lua_State *L, const char *str);
|
||||||
|
int lua_dobuffer (lua_State *L, const char *buff, size_t size,
|
||||||
|
const char *name);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** miscellaneous functions
|
||||||
|
*/
|
||||||
|
int lua_newtag (lua_State *L);
|
||||||
|
int lua_copytagmethods (lua_State *L, int tagto, int tagfrom);
|
||||||
|
void lua_settag (lua_State *L, int tag);
|
||||||
|
|
||||||
|
void lua_error (lua_State *L, const char *s);
|
||||||
|
|
||||||
|
void lua_unref (lua_State *L, int ref);
|
||||||
|
|
||||||
|
long lua_collectgarbage (lua_State *L, long limit);
|
||||||
|
|
||||||
|
int lua_next (lua_State *L, int index);
|
||||||
|
int lua_getn (lua_State *L, int index);
|
||||||
|
|
||||||
|
void lua_concat (lua_State *L, int n);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** ===============================================================
|
||||||
|
** some useful macros
|
||||||
|
** ===============================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define lua_pop(L,n) lua_settop(L, -(n)-1)
|
||||||
|
|
||||||
|
#define lua_register(L,n,f) (lua_pushcfunction(L, f), lua_setglobal(L, n))
|
||||||
|
#define lua_pushuserdata(L,u) lua_pushusertag(L, u, 0)
|
||||||
|
#define lua_pushcfunction(L,f) lua_pushcclosure(L, f, 0)
|
||||||
|
#define lua_clonetag(L,t) lua_copytagmethods(L, lua_newtag(L), (t))
|
||||||
|
|
||||||
|
#define lua_isfunction(L,n) (*lua_type(L,n) == 'f')
|
||||||
|
#define lua_isstring(L,n) (lua_tostring(L,n) != 0)
|
||||||
|
#define lua_istable(L,n) (*lua_type(L,n) == 't')
|
||||||
|
#define lua_isuserdata(L,n) (*lua_type(L,n) == 'u')
|
||||||
|
#define lua_isnil(L,n) (lua_type(L,n)[2] == 'l')
|
||||||
|
#define lua_isnull(L,n) (*lua_type(L,n) == 'N')
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/******************************************************************************
|
||||||
|
* Copyright (C) 1994-2000 TeCGraf, PUC-Rio. All rights reserved.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, without written agreement and without license
|
||||||
|
* or royalty fees, to use, copy, modify, and distribute this software and its
|
||||||
|
* documentation for any purpose, including commercial applications, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* - The above copyright notice and this permission notice shall appear in all
|
||||||
|
* copies or substantial portions of this software.
|
||||||
|
*
|
||||||
|
* - The origin of this software must not be misrepresented; you must not
|
||||||
|
* claim that you wrote the original software. If you use this software in a
|
||||||
|
* product, an acknowledgment in the product documentation would be greatly
|
||||||
|
* appreciated (but it is not required).
|
||||||
|
*
|
||||||
|
* - Altered source versions must be plainly marked as such, and must not be
|
||||||
|
* misrepresented as being the original software.
|
||||||
|
*
|
||||||
|
* The authors specifically disclaim any warranties, including, but not limited
|
||||||
|
* to, the implied warranties of merchantability and fitness for a particular
|
||||||
|
* purpose. The software provided hereunder is on an "as is" basis, and the
|
||||||
|
* authors have no obligation to provide maintenance, support, updates,
|
||||||
|
* enhancements, or modifications. In no event shall TeCGraf, PUC-Rio, or the
|
||||||
|
* authors be held liable to any party for direct, indirect, special,
|
||||||
|
* incidental, or consequential damages arising out of the use of this software
|
||||||
|
* and its documentation.
|
||||||
|
*
|
||||||
|
* The Lua language and this implementation have been entirely designed and
|
||||||
|
* written by Waldemar Celes Filho, Roberto Ierusalimschy and
|
||||||
|
* Luiz Henrique de Figueiredo at TeCGraf, PUC-Rio.
|
||||||
|
*
|
||||||
|
* This implementation contains no third-party code.
|
||||||
|
******************************************************************************/
|
||||||
|
|||||||
974
lua.stx
974
lua.stx
@@ -1,974 +0,0 @@
|
|||||||
%{
|
|
||||||
|
|
||||||
char *rcs_luastx = "$Id: lua.stx,v 3.16 1994/12/27 20:41:11 celes Exp roberto $";
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "tree.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
/* to avoid warnings generated by yacc */
|
|
||||||
int yyparse (void);
|
|
||||||
#define malloc luaI_malloc
|
|
||||||
#define realloc luaI_realloc
|
|
||||||
#define free luaI_free
|
|
||||||
|
|
||||||
#ifndef LISTING
|
|
||||||
#define LISTING 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef CODE_BLOCK
|
|
||||||
#define CODE_BLOCK 256
|
|
||||||
#endif
|
|
||||||
static int maxcode;
|
|
||||||
static int maxmain;
|
|
||||||
static Long maxcurr; /* to allow maxcurr *= 2 without overflow */
|
|
||||||
static Byte *funcCode = NULL;
|
|
||||||
static Byte **initcode;
|
|
||||||
static Byte *basepc;
|
|
||||||
static int maincode;
|
|
||||||
static int pc;
|
|
||||||
|
|
||||||
#define MAXVAR 32
|
|
||||||
static Long varbuffer[MAXVAR]; /* variables in an assignment list;
|
|
||||||
it's long to store negative Word values */
|
|
||||||
static int nvarbuffer=0; /* number of variables at a list */
|
|
||||||
|
|
||||||
static Word localvar[STACKGAP]; /* store local variable names */
|
|
||||||
static int nlocalvar=0; /* number of local variables */
|
|
||||||
|
|
||||||
#define MAXFIELDS FIELDS_PER_FLUSH*2
|
|
||||||
static Word fields[MAXFIELDS]; /* fieldnames to be flushed */
|
|
||||||
static int nfields=0;
|
|
||||||
|
|
||||||
|
|
||||||
/* Internal functions */
|
|
||||||
|
|
||||||
static void code_byte (Byte c)
|
|
||||||
{
|
|
||||||
if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
|
|
||||||
{
|
|
||||||
if (maxcurr >= MAX_INT)
|
|
||||||
lua_error("code size overflow");
|
|
||||||
maxcurr *= 2;
|
|
||||||
if (maxcurr >= MAX_INT)
|
|
||||||
maxcurr = MAX_INT;
|
|
||||||
basepc = growvector(basepc, maxcurr, Byte);
|
|
||||||
}
|
|
||||||
basepc[pc++] = c;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void code_word (Word n)
|
|
||||||
{
|
|
||||||
CodeWord code;
|
|
||||||
code.w = n;
|
|
||||||
code_byte(code.m.c1);
|
|
||||||
code_byte(code.m.c2);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void code_float (float n)
|
|
||||||
{
|
|
||||||
CodeFloat code;
|
|
||||||
code.f = n;
|
|
||||||
code_byte(code.m.c1);
|
|
||||||
code_byte(code.m.c2);
|
|
||||||
code_byte(code.m.c3);
|
|
||||||
code_byte(code.m.c4);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void code_code (Byte *b)
|
|
||||||
{
|
|
||||||
CodeCode code;
|
|
||||||
code.b = b;
|
|
||||||
code_byte(code.m.c1);
|
|
||||||
code_byte(code.m.c2);
|
|
||||||
code_byte(code.m.c3);
|
|
||||||
code_byte(code.m.c4);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void code_word_at (Byte *p, Word n)
|
|
||||||
{
|
|
||||||
CodeWord code;
|
|
||||||
code.w = n;
|
|
||||||
*p++ = code.m.c1;
|
|
||||||
*p++ = code.m.c2;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void push_field (Word name)
|
|
||||||
{
|
|
||||||
if (nfields < STACKGAP-1)
|
|
||||||
fields[nfields++] = name;
|
|
||||||
else
|
|
||||||
lua_error ("too many fields in a constructor");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flush_record (int n)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
if (n == 0) return;
|
|
||||||
code_byte(STORERECORD);
|
|
||||||
code_byte(n);
|
|
||||||
for (i=0; i<n; i++)
|
|
||||||
code_word(fields[--nfields]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void flush_list (int m, int n)
|
|
||||||
{
|
|
||||||
if (n == 0) return;
|
|
||||||
if (m == 0)
|
|
||||||
code_byte(STORELIST0);
|
|
||||||
else
|
|
||||||
if (m < 255)
|
|
||||||
{
|
|
||||||
code_byte(STORELIST);
|
|
||||||
code_byte(m);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_error ("list constructor too long");
|
|
||||||
code_byte(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void add_nlocalvar (int n)
|
|
||||||
{
|
|
||||||
if (MAX_TEMPS+nlocalvar+MAXVAR+n < STACKGAP)
|
|
||||||
nlocalvar += n;
|
|
||||||
else
|
|
||||||
lua_error ("too many local variables");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void incr_nvarbuffer (void)
|
|
||||||
{
|
|
||||||
if (nvarbuffer < MAXVAR-1)
|
|
||||||
nvarbuffer++;
|
|
||||||
else
|
|
||||||
lua_error ("variable buffer overflow");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void code_number (float f)
|
|
||||||
{
|
|
||||||
Word i = (Word)f;
|
|
||||||
if (f == (float)i) /* f has an (short) integer value */
|
|
||||||
{
|
|
||||||
if (i <= 2) code_byte(PUSH0 + i);
|
|
||||||
else if (i <= 255)
|
|
||||||
{
|
|
||||||
code_byte(PUSHBYTE);
|
|
||||||
code_byte(i);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(PUSHWORD);
|
|
||||||
code_word(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(PUSHFLOAT);
|
|
||||||
code_float(f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Search a local name and if find return its index. If do not find return -1
|
|
||||||
*/
|
|
||||||
static int lua_localname (Word n)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
for (i=nlocalvar-1; i >= 0; i--)
|
|
||||||
if (n == localvar[i]) return i; /* local var */
|
|
||||||
return -1; /* global var */
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Push a variable given a number. If number is positive, push global variable
|
|
||||||
** indexed by (number -1). If negative, push local indexed by ABS(number)-1.
|
|
||||||
** Otherwise, if zero, push indexed variable (record).
|
|
||||||
*/
|
|
||||||
static void lua_pushvar (Long number)
|
|
||||||
{
|
|
||||||
if (number > 0) /* global var */
|
|
||||||
{
|
|
||||||
code_byte(PUSHGLOBAL);
|
|
||||||
code_word(number-1);
|
|
||||||
}
|
|
||||||
else if (number < 0) /* local var */
|
|
||||||
{
|
|
||||||
number = (-number) - 1;
|
|
||||||
if (number < 10) code_byte(PUSHLOCAL0 + number);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(PUSHLOCAL);
|
|
||||||
code_byte(number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(PUSHINDEXED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void lua_codeadjust (int n)
|
|
||||||
{
|
|
||||||
if (n+nlocalvar == 0)
|
|
||||||
code_byte(ADJUST0);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(ADJUST);
|
|
||||||
code_byte(n+nlocalvar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void init_function (TreeNode *func)
|
|
||||||
{
|
|
||||||
if (funcCode == NULL) /* first function */
|
|
||||||
{
|
|
||||||
funcCode = newvector(CODE_BLOCK, Byte);
|
|
||||||
maxcode = CODE_BLOCK;
|
|
||||||
}
|
|
||||||
pc=0; basepc=funcCode; maxcurr=maxcode;
|
|
||||||
nlocalvar=0;
|
|
||||||
if (lua_debug)
|
|
||||||
{
|
|
||||||
code_byte(SETFUNCTION);
|
|
||||||
code_code((Byte *)luaI_strdup(lua_file[lua_nfile-1]));
|
|
||||||
code_word(luaI_findconstant(func));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void codereturn (void)
|
|
||||||
{
|
|
||||||
if (lua_debug) code_byte(RESET);
|
|
||||||
if (nlocalvar == 0)
|
|
||||||
code_byte(RETCODE0);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(RETCODE);
|
|
||||||
code_byte(nlocalvar);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void codedebugline (void)
|
|
||||||
{
|
|
||||||
if (lua_debug)
|
|
||||||
{
|
|
||||||
code_byte(SETLINE);
|
|
||||||
code_word(lua_linenumber);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void adjust_mult_assign (int vars, int exps, int temps)
|
|
||||||
{
|
|
||||||
if (exps < 0)
|
|
||||||
{
|
|
||||||
int r = vars - (-exps-1);
|
|
||||||
if (r >= 0)
|
|
||||||
code_byte(r);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(0);
|
|
||||||
lua_codeadjust(temps);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (vars != exps)
|
|
||||||
lua_codeadjust(temps);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void lua_codestore (int i)
|
|
||||||
{
|
|
||||||
if (varbuffer[i] > 0) /* global var */
|
|
||||||
{
|
|
||||||
code_byte(STOREGLOBAL);
|
|
||||||
code_word(varbuffer[i]-1);
|
|
||||||
}
|
|
||||||
else if (varbuffer[i] < 0) /* local var */
|
|
||||||
{
|
|
||||||
int number = (-varbuffer[i]) - 1;
|
|
||||||
if (number < 10) code_byte(STORELOCAL0 + number);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(STORELOCAL);
|
|
||||||
code_byte(number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else /* indexed var */
|
|
||||||
{
|
|
||||||
int j;
|
|
||||||
int upper=0; /* number of indexed variables upper */
|
|
||||||
int param; /* number of itens until indexed expression */
|
|
||||||
for (j=i+1; j <nvarbuffer; j++)
|
|
||||||
if (varbuffer[j] == 0) upper++;
|
|
||||||
param = upper*2 + i;
|
|
||||||
if (param == 0)
|
|
||||||
code_byte(STOREINDEXED0);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
code_byte(STOREINDEXED);
|
|
||||||
code_byte(param);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void codeIf (Long thenAdd, Long elseAdd)
|
|
||||||
{
|
|
||||||
Long elseinit = elseAdd+sizeof(Word)+1;
|
|
||||||
if (pc == elseinit) /* no else */
|
|
||||||
{
|
|
||||||
pc -= sizeof(Word)+1;
|
|
||||||
elseinit = pc;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
basepc[elseAdd] = JMP;
|
|
||||||
code_word_at(basepc+elseAdd+1, pc-elseinit);
|
|
||||||
}
|
|
||||||
basepc[thenAdd] = IFFJMP;
|
|
||||||
code_word_at(basepc+thenAdd+1,elseinit-(thenAdd+sizeof(Word)+1));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void yyerror (char *s)
|
|
||||||
{
|
|
||||||
static char msg[256];
|
|
||||||
sprintf (msg,"%s near \"%s\" at line %d in file \"%s\"",
|
|
||||||
s, lua_lasttext (), lua_linenumber, lua_filename());
|
|
||||||
lua_error (msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Parse LUA code.
|
|
||||||
*/
|
|
||||||
void lua_parse (Byte **code)
|
|
||||||
{
|
|
||||||
initcode = code;
|
|
||||||
*initcode = newvector(CODE_BLOCK, Byte);
|
|
||||||
maincode = 0;
|
|
||||||
maxmain = CODE_BLOCK;
|
|
||||||
if (yyparse ()) lua_error("parse error");
|
|
||||||
(*initcode)[maincode++] = RETCODE0;
|
|
||||||
#if LISTING
|
|
||||||
{ static void PrintCode (Byte *c, Byte *end);
|
|
||||||
PrintCode(*initcode,*initcode+maincode); }
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
%}
|
|
||||||
|
|
||||||
|
|
||||||
%union
|
|
||||||
{
|
|
||||||
int vInt;
|
|
||||||
float vFloat;
|
|
||||||
char *pChar;
|
|
||||||
Word vWord;
|
|
||||||
Long vLong;
|
|
||||||
Byte *pByte;
|
|
||||||
TreeNode *pNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
%start functionlist
|
|
||||||
|
|
||||||
%token WRONGTOKEN
|
|
||||||
%token NIL
|
|
||||||
%token IF THEN ELSE ELSEIF WHILE DO REPEAT UNTIL END
|
|
||||||
%token RETURN
|
|
||||||
%token LOCAL
|
|
||||||
%token FUNCTION
|
|
||||||
%token <vFloat> NUMBER
|
|
||||||
%token <vWord> STRING
|
|
||||||
%token <pNode> NAME
|
|
||||||
%token <vInt> DEBUG
|
|
||||||
|
|
||||||
%type <vLong> PrepJump
|
|
||||||
%type <vInt> expr, exprlist, exprlist1, varlist1, funcParams, funcvalue
|
|
||||||
%type <vInt> fieldlist, localdeclist, decinit
|
|
||||||
%type <vInt> ffieldlist1
|
|
||||||
%type <vInt> lfieldlist1
|
|
||||||
%type <vLong> var, singlevar
|
|
||||||
%type <pByte> body
|
|
||||||
|
|
||||||
%left AND OR
|
|
||||||
%left EQ NE '>' '<' LE GE
|
|
||||||
%left CONC
|
|
||||||
%left '+' '-'
|
|
||||||
%left '*' '/'
|
|
||||||
%left UNARY NOT
|
|
||||||
%right '^'
|
|
||||||
|
|
||||||
|
|
||||||
%% /* beginning of rules section */
|
|
||||||
|
|
||||||
|
|
||||||
functionlist : /* empty */
|
|
||||||
| functionlist
|
|
||||||
{
|
|
||||||
pc=maincode; basepc=*initcode; maxcurr=maxmain;
|
|
||||||
nlocalvar=0;
|
|
||||||
}
|
|
||||||
stat sc
|
|
||||||
{
|
|
||||||
maincode=pc; *initcode=basepc; maxmain=maxcurr;
|
|
||||||
}
|
|
||||||
| functionlist function
|
|
||||||
| functionlist method
|
|
||||||
| functionlist setdebug
|
|
||||||
;
|
|
||||||
|
|
||||||
function : FUNCTION NAME
|
|
||||||
{
|
|
||||||
init_function($2);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
Word func = luaI_findsymbol($2);
|
|
||||||
s_tag(func) = LUA_T_FUNCTION;
|
|
||||||
s_bvalue(func) = $4;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
method : FUNCTION NAME ':' NAME
|
|
||||||
{
|
|
||||||
init_function($4);
|
|
||||||
localvar[nlocalvar]=luaI_findsymbolbyname("self");
|
|
||||||
add_nlocalvar(1);
|
|
||||||
}
|
|
||||||
body
|
|
||||||
{
|
|
||||||
/* assign function to table field */
|
|
||||||
pc=maincode; basepc=*initcode; maxcurr=maxmain;
|
|
||||||
nlocalvar=0;
|
|
||||||
lua_pushvar(luaI_findsymbol($2)+1);
|
|
||||||
code_byte(PUSHSTRING);
|
|
||||||
code_word(luaI_findconstant($4));
|
|
||||||
code_byte(PUSHFUNCTION);
|
|
||||||
code_code($6);
|
|
||||||
code_byte(STOREINDEXED0);
|
|
||||||
maincode=pc; *initcode=basepc; maxmain=maxcurr;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
body : '(' parlist ')' block END
|
|
||||||
{
|
|
||||||
codereturn();
|
|
||||||
$$ = newvector(pc, Byte);
|
|
||||||
memcpy($$, basepc, pc*sizeof(Byte));
|
|
||||||
funcCode = basepc; maxcode=maxcurr;
|
|
||||||
#if LISTING
|
|
||||||
PrintCode(funcCode,funcCode+pc);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
statlist : /* empty */
|
|
||||||
| statlist stat sc
|
|
||||||
;
|
|
||||||
|
|
||||||
sc : /* empty */ | ';' ;
|
|
||||||
|
|
||||||
stat : { codedebugline(); } stat1 ;
|
|
||||||
|
|
||||||
cond : { codedebugline(); } expr1 ;
|
|
||||||
|
|
||||||
stat1 : IF expr1 THEN PrepJump block PrepJump elsepart END
|
|
||||||
{ codeIf($4, $6); }
|
|
||||||
|
|
||||||
| WHILE {$<vLong>$=pc;} expr1 DO PrepJump block PrepJump END
|
|
||||||
{
|
|
||||||
basepc[$5] = IFFJMP;
|
|
||||||
code_word_at(basepc+$5+1, pc - ($5 + sizeof(Word)+1));
|
|
||||||
basepc[$7] = UPJMP;
|
|
||||||
code_word_at(basepc+$7+1, pc - ($<vLong>2));
|
|
||||||
}
|
|
||||||
|
|
||||||
| REPEAT {$<vLong>$=pc;} block UNTIL cond PrepJump
|
|
||||||
{
|
|
||||||
basepc[$6] = IFFUPJMP;
|
|
||||||
code_word_at(basepc+$6+1, pc - ($<vLong>2));
|
|
||||||
}
|
|
||||||
|
|
||||||
| varlist1 '=' exprlist1
|
|
||||||
{
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
adjust_mult_assign(nvarbuffer, $3, $1 * 2 + nvarbuffer);
|
|
||||||
for (i=nvarbuffer-1; i>=0; i--)
|
|
||||||
lua_codestore (i);
|
|
||||||
if ($1 > 1 || ($1 == 1 && varbuffer[0] != 0))
|
|
||||||
lua_codeadjust (0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
| functioncall { code_byte(0); }
|
|
||||||
| LOCAL localdeclist decinit
|
|
||||||
{ add_nlocalvar($2);
|
|
||||||
adjust_mult_assign($2, $3, 0);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
elsepart : /* empty */
|
|
||||||
| ELSE block
|
|
||||||
| ELSEIF cond THEN PrepJump block PrepJump elsepart
|
|
||||||
{ codeIf($4, $6); }
|
|
||||||
;
|
|
||||||
|
|
||||||
block : {$<vInt>$ = nlocalvar;} statlist ret
|
|
||||||
{
|
|
||||||
if (nlocalvar != $<vInt>1)
|
|
||||||
{
|
|
||||||
nlocalvar = $<vInt>1;
|
|
||||||
lua_codeadjust (0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
ret : /* empty */
|
|
||||||
| RETURN { codedebugline(); } exprlist sc
|
|
||||||
{
|
|
||||||
if ($3 < 0) code_byte(MULT_RET);
|
|
||||||
codereturn();
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
PrepJump : /* empty */
|
|
||||||
{
|
|
||||||
$$ = pc;
|
|
||||||
code_byte(0); /* open space */
|
|
||||||
code_word (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
expr1 : expr { if ($1 == 0) code_byte(1); }
|
|
||||||
;
|
|
||||||
|
|
||||||
expr : '(' expr ')' { $$ = $2; }
|
|
||||||
| expr1 EQ expr1 { code_byte(EQOP); $$ = 1; }
|
|
||||||
| expr1 '<' expr1 { code_byte(LTOP); $$ = 1; }
|
|
||||||
| expr1 '>' expr1 { code_byte(GTOP); $$ = 1; }
|
|
||||||
| expr1 NE expr1 { code_byte(EQOP); code_byte(NOTOP); $$ = 1; }
|
|
||||||
| expr1 LE expr1 { code_byte(LEOP); $$ = 1; }
|
|
||||||
| expr1 GE expr1 { code_byte(GEOP); $$ = 1; }
|
|
||||||
| expr1 '+' expr1 { code_byte(ADDOP); $$ = 1; }
|
|
||||||
| expr1 '-' expr1 { code_byte(SUBOP); $$ = 1; }
|
|
||||||
| expr1 '*' expr1 { code_byte(MULTOP); $$ = 1; }
|
|
||||||
| expr1 '/' expr1 { code_byte(DIVOP); $$ = 1; }
|
|
||||||
| expr1 '^' expr1 { code_byte(POWOP); $$ = 1; }
|
|
||||||
| expr1 CONC expr1 { code_byte(CONCOP); $$ = 1; }
|
|
||||||
| '-' expr1 %prec UNARY { code_byte(MINUSOP); $$ = 1;}
|
|
||||||
| table { $$ = 1; }
|
|
||||||
| varexp { $$ = 1;}
|
|
||||||
| NUMBER { code_number($1); $$ = 1; }
|
|
||||||
| STRING
|
|
||||||
{
|
|
||||||
code_byte(PUSHSTRING);
|
|
||||||
code_word($1);
|
|
||||||
$$ = 1;
|
|
||||||
}
|
|
||||||
| NIL {code_byte(PUSHNIL); $$ = 1; }
|
|
||||||
| functioncall { $$ = 0; }
|
|
||||||
| NOT expr1 { code_byte(NOTOP); $$ = 1;}
|
|
||||||
| expr1 AND PrepJump {code_byte(POP); } expr1
|
|
||||||
{
|
|
||||||
basepc[$3] = ONFJMP;
|
|
||||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
|
||||||
$$ = 1;
|
|
||||||
}
|
|
||||||
| expr1 OR PrepJump {code_byte(POP); } expr1
|
|
||||||
{
|
|
||||||
basepc[$3] = ONTJMP;
|
|
||||||
code_word_at(basepc+$3+1, pc - ($3 + sizeof(Word)+1));
|
|
||||||
$$ = 1;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
table :
|
|
||||||
{
|
|
||||||
code_byte(CREATEARRAY);
|
|
||||||
$<vLong>$ = pc; code_word(0);
|
|
||||||
}
|
|
||||||
'{' fieldlist '}'
|
|
||||||
{
|
|
||||||
code_word_at(basepc+$<vLong>1, $3);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
functioncall : funcvalue funcParams
|
|
||||||
{ code_byte(CALLFUNC); code_byte($1+$2); }
|
|
||||||
;
|
|
||||||
|
|
||||||
funcvalue : varexp { $$ = 0; }
|
|
||||||
| varexp ':' NAME
|
|
||||||
{
|
|
||||||
code_byte(PUSHSELF);
|
|
||||||
code_word(luaI_findconstant($3));
|
|
||||||
$$ = 1;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
funcParams : '(' exprlist ')'
|
|
||||||
{ if ($2<0) { code_byte(1); $$ = -$2; } else $$ = $2; }
|
|
||||||
| table { $$ = 1; }
|
|
||||||
;
|
|
||||||
|
|
||||||
exprlist : /* empty */ { $$ = 0; }
|
|
||||||
| exprlist1 { $$ = $1; }
|
|
||||||
;
|
|
||||||
|
|
||||||
exprlist1 : expr { if ($1 == 0) $$ = -1; else $$ = 1; }
|
|
||||||
| exprlist1 ',' { if ($1 < 0) code_byte(1); } expr
|
|
||||||
{
|
|
||||||
int r = $1 < 0 ? -$1 : $1;
|
|
||||||
$$ = ($4 == 0) ? -(r+1) : r+1;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
parlist : /* empty */ { lua_codeadjust(0); }
|
|
||||||
| parlist1 { lua_codeadjust(0); }
|
|
||||||
;
|
|
||||||
|
|
||||||
parlist1 : NAME
|
|
||||||
{
|
|
||||||
localvar[nlocalvar]=luaI_findsymbol($1);
|
|
||||||
add_nlocalvar(1);
|
|
||||||
}
|
|
||||||
| parlist1 ',' NAME
|
|
||||||
{
|
|
||||||
localvar[nlocalvar]=luaI_findsymbol($3);
|
|
||||||
add_nlocalvar(1);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
fieldlist : /* empty */ { $$ = 0; }
|
|
||||||
| lfieldlist1 lastcomma
|
|
||||||
{ $$ = $1; flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
|
|
||||||
| ffieldlist1 lastcomma
|
|
||||||
{ $$ = $1; flush_record($1%FIELDS_PER_FLUSH); }
|
|
||||||
| lfieldlist1 ';'
|
|
||||||
{ flush_list($1/FIELDS_PER_FLUSH, $1%FIELDS_PER_FLUSH); }
|
|
||||||
ffieldlist1 lastcomma
|
|
||||||
{ $$ = $1+$4; flush_record($4%FIELDS_PER_FLUSH); }
|
|
||||||
;
|
|
||||||
|
|
||||||
lastcomma : /* empty */
|
|
||||||
| ','
|
|
||||||
;
|
|
||||||
|
|
||||||
ffieldlist1 : ffield {$$=1;}
|
|
||||||
| ffieldlist1 ',' ffield
|
|
||||||
{
|
|
||||||
$$=$1+1;
|
|
||||||
if ($$%FIELDS_PER_FLUSH == 0) flush_record(FIELDS_PER_FLUSH);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
ffield : NAME '=' expr1
|
|
||||||
{
|
|
||||||
push_field(luaI_findconstant($1));
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
lfieldlist1 : expr1 {$$=1;}
|
|
||||||
| lfieldlist1 ',' expr1
|
|
||||||
{
|
|
||||||
$$=$1+1;
|
|
||||||
if ($$%FIELDS_PER_FLUSH == 0)
|
|
||||||
flush_list($$/FIELDS_PER_FLUSH - 1, FIELDS_PER_FLUSH);
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
varlist1 : var
|
|
||||||
{
|
|
||||||
nvarbuffer = 0;
|
|
||||||
varbuffer[nvarbuffer] = $1; incr_nvarbuffer();
|
|
||||||
$$ = ($1 == 0) ? 1 : 0;
|
|
||||||
}
|
|
||||||
| varlist1 ',' var
|
|
||||||
{
|
|
||||||
varbuffer[nvarbuffer] = $3; incr_nvarbuffer();
|
|
||||||
$$ = ($3 == 0) ? $1 + 1 : $1;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
var : singlevar { $$ = $1; }
|
|
||||||
| varexp '[' expr1 ']'
|
|
||||||
{
|
|
||||||
$$ = 0; /* indexed variable */
|
|
||||||
}
|
|
||||||
| varexp '.' NAME
|
|
||||||
{
|
|
||||||
code_byte(PUSHSTRING);
|
|
||||||
code_word(luaI_findconstant($3));
|
|
||||||
$$ = 0; /* indexed variable */
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
singlevar : NAME
|
|
||||||
{
|
|
||||||
Word s = luaI_findsymbol($1);
|
|
||||||
int local = lua_localname (s);
|
|
||||||
if (local == -1) /* global var */
|
|
||||||
$$ = s + 1; /* return positive value */
|
|
||||||
else
|
|
||||||
$$ = -(local+1); /* return negative value */
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
varexp : var { lua_pushvar($1); }
|
|
||||||
;
|
|
||||||
|
|
||||||
localdeclist : NAME {localvar[nlocalvar]=luaI_findsymbol($1); $$ = 1;}
|
|
||||||
| localdeclist ',' NAME
|
|
||||||
{
|
|
||||||
localvar[nlocalvar+$1]=luaI_findsymbol($3);
|
|
||||||
$$ = $1+1;
|
|
||||||
}
|
|
||||||
;
|
|
||||||
|
|
||||||
decinit : /* empty */ { $$ = 0; }
|
|
||||||
| '=' exprlist1 { $$ = $2; }
|
|
||||||
;
|
|
||||||
|
|
||||||
setdebug : DEBUG {lua_debug = $1;}
|
|
||||||
|
|
||||||
%%
|
|
||||||
|
|
||||||
#if LISTING
|
|
||||||
|
|
||||||
static void PrintCode (Byte *code, Byte *end)
|
|
||||||
{
|
|
||||||
Byte *p = code;
|
|
||||||
printf ("\n\nCODE\n");
|
|
||||||
while (p != end)
|
|
||||||
{
|
|
||||||
switch ((OpCode)*p)
|
|
||||||
{
|
|
||||||
case PUSHNIL: printf ("%d PUSHNIL\n", (p++)-code); break;
|
|
||||||
case PUSH0: case PUSH1: case PUSH2:
|
|
||||||
printf ("%d PUSH%c\n", p-code, *p-PUSH0+'0');
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case PUSHBYTE:
|
|
||||||
printf ("%d PUSHBYTE %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case PUSHWORD:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d PUSHWORD %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHFLOAT:
|
|
||||||
{
|
|
||||||
CodeFloat c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_float(c,p);
|
|
||||||
printf ("%d PUSHFLOAT %f\n", n, c.f);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHSTRING:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d PUSHSTRING %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHFUNCTION:
|
|
||||||
{
|
|
||||||
CodeCode c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_code(c,p);
|
|
||||||
printf ("%d PUSHFUNCTION %p\n", n, c.b);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PUSHLOCAL0: case PUSHLOCAL1: case PUSHLOCAL2: case PUSHLOCAL3:
|
|
||||||
case PUSHLOCAL4: case PUSHLOCAL5: case PUSHLOCAL6: case PUSHLOCAL7:
|
|
||||||
case PUSHLOCAL8: case PUSHLOCAL9:
|
|
||||||
printf ("%d PUSHLOCAL%c\n", p-code, *p-PUSHLOCAL0+'0');
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case PUSHLOCAL: printf ("%d PUSHLOCAL %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case PUSHGLOBAL:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d PUSHGLOBAL %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHINDEXED: printf ("%d PUSHINDEXED\n", (p++)-code); break;
|
|
||||||
case STORELOCAL0: case STORELOCAL1: case STORELOCAL2: case STORELOCAL3:
|
|
||||||
case STORELOCAL4: case STORELOCAL5: case STORELOCAL6: case STORELOCAL7:
|
|
||||||
case STORELOCAL8: case STORELOCAL9:
|
|
||||||
printf ("%d STORELOCAL%c\n", p-code, *p-STORELOCAL0+'0');
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case STORELOCAL:
|
|
||||||
printf ("%d STORELOCAL %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case STOREGLOBAL:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d STOREGLOBAL %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PUSHSELF:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d PUSHSELF %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case STOREINDEXED0: printf ("%d STOREINDEXED0\n", (p++)-code); break;
|
|
||||||
case STOREINDEXED: printf ("%d STOREINDEXED %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case STORELIST0:
|
|
||||||
printf("%d STORELIST0 %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case STORELIST:
|
|
||||||
printf("%d STORELIST %d %d\n", p-code, *(p+1), *(p+2));
|
|
||||||
p+=3;
|
|
||||||
break;
|
|
||||||
case STORERECORD:
|
|
||||||
printf("%d STORERECORD %d\n", p-code, *(++p));
|
|
||||||
p += *p*sizeof(Word) + 1;
|
|
||||||
break;
|
|
||||||
case ADJUST0: printf ("%d ADJUST0\n", (p++)-code); break;
|
|
||||||
case ADJUST:
|
|
||||||
printf ("%d ADJUST %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case CREATEARRAY:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d CREATEARRAY %d\n", n, c.w);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case EQOP: printf ("%d EQOP\n", (p++)-code); break;
|
|
||||||
case LTOP: printf ("%d LTOP\n", (p++)-code); break;
|
|
||||||
case LEOP: printf ("%d LEOP\n", (p++)-code); break;
|
|
||||||
case ADDOP: printf ("%d ADDOP\n", (p++)-code); break;
|
|
||||||
case SUBOP: printf ("%d SUBOP\n", (p++)-code); break;
|
|
||||||
case MULTOP: printf ("%d MULTOP\n", (p++)-code); break;
|
|
||||||
case DIVOP: printf ("%d DIVOP\n", (p++)-code); break;
|
|
||||||
case POWOP: printf ("%d POWOP\n", (p++)-code); break;
|
|
||||||
case CONCOP: printf ("%d CONCOP\n", (p++)-code); break;
|
|
||||||
case MINUSOP: printf ("%d MINUSOP\n", (p++)-code); break;
|
|
||||||
case NOTOP: printf ("%d NOTOP\n", (p++)-code); break;
|
|
||||||
case ONTJMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d ONTJMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ONFJMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d ONFJMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case JMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d JMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case UPJMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d UPJMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case IFFJMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d IFFJMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case IFFUPJMP:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d IFFUPJMP %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case POP: printf ("%d POP\n", (p++)-code); break;
|
|
||||||
case CALLFUNC:
|
|
||||||
printf ("%d CALLFUNC %d %d\n", p-code, *(p+1), *(p+2));
|
|
||||||
p+=3;
|
|
||||||
break;
|
|
||||||
case RETCODE0: printf ("%d RETCODE0\n", (p++)-code); break;
|
|
||||||
case RETCODE:
|
|
||||||
printf ("%d RETCODE %d\n", p-code, *(++p));
|
|
||||||
p++;
|
|
||||||
break;
|
|
||||||
case SETFUNCTION:
|
|
||||||
{
|
|
||||||
CodeCode c1;
|
|
||||||
CodeWord c2;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_code(c1,p);
|
|
||||||
get_word(c2,p);
|
|
||||||
printf ("%d SETFUNCTION %s %d\n", n, (char *)c1.b, c2.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SETLINE:
|
|
||||||
{
|
|
||||||
CodeWord c;
|
|
||||||
int n = p-code;
|
|
||||||
p++;
|
|
||||||
get_word(c,p);
|
|
||||||
printf ("%d SETLINE %d\n", n, c.w);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RESET: printf ("%d RESET\n", (p++)-code); break;
|
|
||||||
default: printf ("%d Cannot happen: code %d\n", (p++)-code, *(p-1)); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
46
luadebug.h
Normal file
46
luadebug.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
** $Id: luadebug.h,v 1.14 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
** Debugging API
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef luadebug_h
|
||||||
|
#define luadebug_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
typedef struct lua_Debug lua_Debug; /* activation record */
|
||||||
|
typedef struct lua_Localvar lua_Localvar;
|
||||||
|
|
||||||
|
typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
|
||||||
|
|
||||||
|
|
||||||
|
int lua_getstack (lua_State *L, int level, lua_Debug *ar);
|
||||||
|
int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
|
||||||
|
const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int localnum);
|
||||||
|
const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int localnum);
|
||||||
|
|
||||||
|
lua_Hook lua_setcallhook (lua_State *L, lua_Hook func);
|
||||||
|
lua_Hook lua_setlinehook (lua_State *L, lua_Hook func);
|
||||||
|
|
||||||
|
|
||||||
|
#define LUA_IDSIZE 60
|
||||||
|
|
||||||
|
struct lua_Debug {
|
||||||
|
const char *event; /* `call', `return' */
|
||||||
|
int currentline; /* (l) */
|
||||||
|
const char *name; /* (n) */
|
||||||
|
const char *namewhat; /* (n) `global', `tag method', `local', `field' */
|
||||||
|
int nups; /* (u) number of upvalues */
|
||||||
|
int linedefined; /* (S) */
|
||||||
|
const char *what; /* (S) `Lua' function, `C' function, Lua `main' */
|
||||||
|
const char *source; /* (S) */
|
||||||
|
char short_src[LUA_IDSIZE]; /* (S) */
|
||||||
|
/* private part */
|
||||||
|
struct lua_TObject *_func; /* active function */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
29
lualib.h
29
lualib.h
@@ -1,16 +1,29 @@
|
|||||||
/*
|
/*
|
||||||
** Libraries to be used in LUA programs
|
** $Id: lualib.h,v 1.11 2000/09/05 19:33:32 roberto Exp roberto $
|
||||||
** Grupo de Tecnologia em Computacao Grafica
|
** Lua standard libraries
|
||||||
** TeCGraf - PUC-Rio
|
** See Copyright Notice in lua.h
|
||||||
** $Id: lualib.h,v 1.2 1994/08/24 15:29:02 celes Stab roberto $
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#ifndef lualib_h
|
#ifndef lualib_h
|
||||||
#define lualib_h
|
#define lualib_h
|
||||||
|
|
||||||
void iolib_open (void);
|
#include "lua.h"
|
||||||
void strlib_open (void);
|
|
||||||
void mathlib_open (void);
|
|
||||||
|
#define LUA_ALERT "_ALERT"
|
||||||
|
|
||||||
|
void lua_baselibopen (lua_State *L);
|
||||||
|
void lua_iolibopen (lua_State *L);
|
||||||
|
void lua_strlibopen (lua_State *L);
|
||||||
|
void lua_mathlibopen (lua_State *L);
|
||||||
|
void lua_dblibopen (lua_State *L);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Auxiliary functions (private) */
|
||||||
|
|
||||||
|
const char *luaI_classend (lua_State *L, const char *p);
|
||||||
|
int luaI_singlematch (int c, const char *p, const char *ep);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
44
luamem.c
44
luamem.c
@@ -1,44 +0,0 @@
|
|||||||
/*
|
|
||||||
** mem.c
|
|
||||||
** TecCGraf - PUC-Rio
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_mem = "$Id: mem.c,v 1.4 1995/01/13 22:11:12 roberto Exp roberto $";
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
void luaI_free (void *block)
|
|
||||||
{
|
|
||||||
*((int *)block) = -1; /* to catch errors */
|
|
||||||
free(block);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *luaI_malloc (unsigned long size)
|
|
||||||
{
|
|
||||||
void *block = malloc((size_t)size);
|
|
||||||
if (block == NULL)
|
|
||||||
lua_error("not enough memory");
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void *luaI_realloc (void *oldblock, unsigned long size)
|
|
||||||
{
|
|
||||||
void *block = realloc(oldblock, (size_t)size);
|
|
||||||
if (block == NULL)
|
|
||||||
lua_error("not enough memory");
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
char *luaI_strdup (char *str)
|
|
||||||
{
|
|
||||||
char *newstr = luaI_malloc(strlen(str)+1);
|
|
||||||
strcpy(newstr, str);
|
|
||||||
return newstr;
|
|
||||||
}
|
|
||||||
25
luamem.h
25
luamem.h
@@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
** mem.c
|
|
||||||
** memory manager for lua
|
|
||||||
** $Id: mem.h,v 1.1 1994/11/16 17:38:08 roberto Stab roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef mem_h
|
|
||||||
#define mem_h
|
|
||||||
|
|
||||||
#ifndef NULL
|
|
||||||
#define NULL 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void luaI_free (void *block);
|
|
||||||
void *luaI_malloc (unsigned long size);
|
|
||||||
void *luaI_realloc (void *oldblock, unsigned long size);
|
|
||||||
|
|
||||||
char *luaI_strdup (char *str);
|
|
||||||
|
|
||||||
#define new(s) ((s *)luaI_malloc(sizeof(s)))
|
|
||||||
#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
|
|
||||||
#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s)))
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
244
lundump.c
Normal file
244
lundump.c
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lundump.c,v 1.32 2000/09/21 03:15:36 lhf Exp $
|
||||||
|
** load bytecodes from files
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lmem.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "lundump.h"
|
||||||
|
|
||||||
|
#define LoadByte ezgetc
|
||||||
|
|
||||||
|
static const char* ZNAME (ZIO* Z)
|
||||||
|
{
|
||||||
|
const char* s=zname(Z);
|
||||||
|
return (*s=='@') ? s+1 : s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
luaO_verror(L,"unexpected end of file in `%.255s'",ZNAME(Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int ezgetc (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
int c=zgetc(Z);
|
||||||
|
if (c==EOZ) unexpectedEOZ(L,Z);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
||||||
|
{
|
||||||
|
int r=zread(Z,b,n);
|
||||||
|
if (r!=0) unexpectedEOZ(L,Z);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
if (swap)
|
||||||
|
{
|
||||||
|
char *p=(char *) b+size-1;
|
||||||
|
int n=size;
|
||||||
|
while (n--) *p--=(char)ezgetc(L,Z);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ezread(L,Z,b,size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
if (swap)
|
||||||
|
{
|
||||||
|
char *q=(char *) b;
|
||||||
|
while (m--)
|
||||||
|
{
|
||||||
|
char *p=q+size-1;
|
||||||
|
int n=size;
|
||||||
|
while (n--) *p--=(char)ezgetc(L,Z);
|
||||||
|
q+=size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ezread(L,Z,b,m*size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int LoadInt (lua_State* L, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int x;
|
||||||
|
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
size_t x;
|
||||||
|
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
Number x;
|
||||||
|
LoadBlock(L,&x,sizeof(x),Z,swap);
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
size_t size=LoadSize(L,Z,swap);
|
||||||
|
if (size==0)
|
||||||
|
return NULL;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char* s=luaO_openspace(L,size);
|
||||||
|
LoadBlock(L,s,size,Z,0);
|
||||||
|
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int size=LoadInt(L,Z,swap);
|
||||||
|
tf->code=luaM_newvector(L,size,Instruction);
|
||||||
|
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
|
||||||
|
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int i,n;
|
||||||
|
tf->nlocvars=n=LoadInt(L,Z,swap);
|
||||||
|
tf->locvars=luaM_newvector(L,n,LocVar);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
{
|
||||||
|
tf->locvars[i].varname=LoadString(L,Z,swap);
|
||||||
|
tf->locvars[i].startpc=LoadInt(L,Z,swap);
|
||||||
|
tf->locvars[i].endpc=LoadInt(L,Z,swap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int n=LoadInt(L,Z,swap);
|
||||||
|
if (n==0) return;
|
||||||
|
tf->lineinfo=luaM_newvector(L,n,int);
|
||||||
|
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
|
||||||
|
|
||||||
|
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
int i,n;
|
||||||
|
tf->nkstr=n=LoadInt(L,Z,swap);
|
||||||
|
tf->kstr=luaM_newvector(L,n,TString*);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
tf->kstr[i]=LoadString(L,Z,swap);
|
||||||
|
tf->nknum=n=LoadInt(L,Z,swap);
|
||||||
|
tf->knum=luaM_newvector(L,n,Number);
|
||||||
|
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
|
||||||
|
tf->nkproto=n=LoadInt(L,Z,swap);
|
||||||
|
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||||
|
for (i=0; i<n; i++)
|
||||||
|
tf->kproto[i]=LoadFunction(L,Z,swap);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
|
||||||
|
{
|
||||||
|
Proto* tf=luaF_newproto(L);
|
||||||
|
tf->source=LoadString(L,Z,swap);
|
||||||
|
tf->lineDefined=LoadInt(L,Z,swap);
|
||||||
|
tf->numparams=LoadInt(L,Z,swap);
|
||||||
|
tf->is_vararg=LoadByte(L,Z);
|
||||||
|
tf->maxstacksize=LoadInt(L,Z,swap);
|
||||||
|
LoadCode(L,tf,Z,swap);
|
||||||
|
LoadLocals(L,tf,Z,swap);
|
||||||
|
LoadLines(L,tf,Z,swap);
|
||||||
|
LoadConstants(L,tf,Z,swap);
|
||||||
|
return tf;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadSignature (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
const char* s=SIGNATURE;
|
||||||
|
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||||
|
++s;
|
||||||
|
if (*s!=0) luaO_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
|
||||||
|
{
|
||||||
|
int r=ezgetc(L,Z);
|
||||||
|
if (r!=s)
|
||||||
|
luaO_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||||
|
" %s is %d but read %d",ZNAME(Z),what,s,r);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TESTSIZE(s) TestSize(L,s,#s,Z)
|
||||||
|
#define V(v) v/16,v%16
|
||||||
|
|
||||||
|
static int LoadHeader (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
int version,swap;
|
||||||
|
Number f=0,tf=TEST_NUMBER;
|
||||||
|
LoadSignature(L,Z);
|
||||||
|
version=ezgetc(L,Z);
|
||||||
|
if (version>VERSION)
|
||||||
|
luaO_verror(L,"`%.255s' too new:\n"
|
||||||
|
" read version %d.%d; expected at most %d.%d",
|
||||||
|
ZNAME(Z),V(version),V(VERSION));
|
||||||
|
if (version<VERSION0) /* check last major change */
|
||||||
|
luaO_verror(L,"`%.255s' too old:\n"
|
||||||
|
" read version %d.%d; expected at least %d.%d",
|
||||||
|
ZNAME(Z),V(version),V(VERSION));
|
||||||
|
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
|
||||||
|
TESTSIZE(sizeof(int));
|
||||||
|
TESTSIZE(sizeof(size_t));
|
||||||
|
TESTSIZE(sizeof(Instruction));
|
||||||
|
TESTSIZE(SIZE_INSTRUCTION);
|
||||||
|
TESTSIZE(SIZE_OP);
|
||||||
|
TESTSIZE(SIZE_B);
|
||||||
|
TESTSIZE(sizeof(Number));
|
||||||
|
f=LoadNumber(L,Z,swap);
|
||||||
|
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||||
|
luaO_verror(L,"unknown number format in `%.255s':\n"
|
||||||
|
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||||
|
ZNAME(Z),f,tf);
|
||||||
|
return swap;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Proto* LoadChunk (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
return LoadFunction(L,Z,LoadHeader(L,Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** load one chunk from a file or buffer
|
||||||
|
** return main if ok and NULL at EOF
|
||||||
|
*/
|
||||||
|
Proto* luaU_undump (lua_State* L, ZIO* Z)
|
||||||
|
{
|
||||||
|
Proto* tf=NULL;
|
||||||
|
int c=zgetc(Z);
|
||||||
|
if (c==ID_CHUNK)
|
||||||
|
tf=LoadChunk(L,Z);
|
||||||
|
c=zgetc(Z);
|
||||||
|
if (c!=EOZ)
|
||||||
|
luaO_verror(L,"`%.255s' apparently contains more than one chunk",ZNAME(Z));
|
||||||
|
return tf;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** find byte order
|
||||||
|
*/
|
||||||
|
int luaU_endianess (void)
|
||||||
|
{
|
||||||
|
int x=1;
|
||||||
|
return *(char*)&x;
|
||||||
|
}
|
||||||
41
lundump.h
Normal file
41
lundump.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp lhf $
|
||||||
|
** load pre-compiled Lua chunks
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lundump_h
|
||||||
|
#define lundump_h
|
||||||
|
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
/* load one chunk */
|
||||||
|
Proto* luaU_undump (lua_State* L, ZIO* Z);
|
||||||
|
#define luaU_undump1 luaU_undump
|
||||||
|
|
||||||
|
/* find byte order */
|
||||||
|
int luaU_endianess (void);
|
||||||
|
|
||||||
|
/* definitions for headers of binary files */
|
||||||
|
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||||
|
#define VERSION0 0x40 /* last major change was in 4.0 */
|
||||||
|
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||||
|
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||||
|
|
||||||
|
/* formats for error messages */
|
||||||
|
#define xSOURCE "<%d:%.255s>"
|
||||||
|
#define SOURCE "<%.255s:%d>"
|
||||||
|
#define IN " in %p " SOURCE
|
||||||
|
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||||
|
|
||||||
|
/* format for numbers in listings and error messages */
|
||||||
|
#ifndef NUMBER_FMT
|
||||||
|
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* a multiple of PI for testing native format */
|
||||||
|
/* multiplying by 1E8 gives non-trivial integer values */
|
||||||
|
#define TEST_NUMBER 3.14159265358979323846E8
|
||||||
|
|
||||||
|
#endif
|
||||||
706
lvm.c
Normal file
706
lvm.c
Normal file
@@ -0,0 +1,706 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lvm.c,v 1.135 2000/09/11 17:38:42 roberto Exp roberto $
|
||||||
|
** Lua virtual machine
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lapi.h"
|
||||||
|
#include "ldebug.h"
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lfunc.h"
|
||||||
|
#include "lgc.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "lopcodes.h"
|
||||||
|
#include "lstate.h"
|
||||||
|
#include "lstring.h"
|
||||||
|
#include "ltable.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
#include "lvm.h"
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef OLD_ANSI
|
||||||
|
#define strcoll(a,b) strcmp(a,b)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Extra stack size to run a function:
|
||||||
|
** TAG_LINE(1), NAME(1), TM calls(3) (plus some extra...)
|
||||||
|
*/
|
||||||
|
#define EXTRA_STACK 8
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int luaV_tonumber (TObject *obj) { /* LUA_NUMBER */
|
||||||
|
if (ttype(obj) != TAG_STRING)
|
||||||
|
return 1;
|
||||||
|
else {
|
||||||
|
if (!luaO_str2d(svalue(obj), &nvalue(obj)))
|
||||||
|
return 2;
|
||||||
|
ttype(obj) = TAG_NUMBER;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
|
||||||
|
if (ttype(obj) != TAG_NUMBER)
|
||||||
|
return 1;
|
||||||
|
else {
|
||||||
|
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
|
||||||
|
sprintf(s, "%.16g", (double)nvalue(obj));
|
||||||
|
tsvalue(obj) = luaS_new(L, s);
|
||||||
|
ttype(obj) = TAG_STRING;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) {
|
||||||
|
CallInfo *ci = infovalue(base-1);
|
||||||
|
int *lineinfo = ci->func->f.l->lineinfo;
|
||||||
|
int pc = (*ci->pc - 1) - ci->func->f.l->code;
|
||||||
|
int newline;
|
||||||
|
if (ci->line == 0) { /* first time? */
|
||||||
|
ci->line = 1;
|
||||||
|
ci->refi = 0;
|
||||||
|
ci->lastpc = pc+1; /* make sure it will call linehook */
|
||||||
|
}
|
||||||
|
newline = luaG_getline(lineinfo, pc, ci->line, &ci->refi);
|
||||||
|
/* calls linehook when enters a new line or jumps back (loop) */
|
||||||
|
if (newline != ci->line || pc <= ci->lastpc) {
|
||||||
|
ci->line = newline;
|
||||||
|
L->top = top;
|
||||||
|
luaD_lineHook(L, base-2, newline, linehook);
|
||||||
|
}
|
||||||
|
ci->lastpc = pc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) {
|
||||||
|
Closure *c = luaF_newclosure(L, nelems);
|
||||||
|
L->top -= nelems;
|
||||||
|
while (nelems--)
|
||||||
|
c->upvalue[nelems] = *(L->top+nelems);
|
||||||
|
ttype(L->top) = t;
|
||||||
|
clvalue(L->top) = c;
|
||||||
|
incr_top;
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) {
|
||||||
|
Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems);
|
||||||
|
cl->f.c = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
|
||||||
|
Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems);
|
||||||
|
cl->f.l = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Function to index a table.
|
||||||
|
** Receives the table at `t' and the key at top.
|
||||||
|
*/
|
||||||
|
const TObject *luaV_gettable (lua_State *L, StkId t) {
|
||||||
|
const TObject *im;
|
||||||
|
int tg;
|
||||||
|
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
|
||||||
|
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
|
||||||
|
ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */
|
||||||
|
/* do a primitive get */
|
||||||
|
const TObject *h = luaH_get(L, hvalue(t), t+1);
|
||||||
|
/* result is no nil or there is no `index' tag method? */
|
||||||
|
if (ttype(h) != TAG_NIL ||
|
||||||
|
(ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL))
|
||||||
|
return h; /* return result */
|
||||||
|
/* else call `index' tag method */
|
||||||
|
}
|
||||||
|
else { /* try a 'gettable' TM */
|
||||||
|
im = luaT_getimbyObj(L, t, IM_GETTABLE);
|
||||||
|
}
|
||||||
|
if (ttype(im) != TAG_NIL) { /* is there a tag method? */
|
||||||
|
luaD_checkstack(L, 2);
|
||||||
|
*(L->top+1) = *(L->top-1); /* key */
|
||||||
|
*L->top = *t; /* table */
|
||||||
|
*(L->top-1) = *im; /* tag method */
|
||||||
|
L->top += 2;
|
||||||
|
luaD_call(L, L->top - 3, 1);
|
||||||
|
return L->top - 1; /* call result */
|
||||||
|
}
|
||||||
|
else { /* no tag method */
|
||||||
|
luaG_typeerror(L, t, "index");
|
||||||
|
return NULL; /* to avoid warnings */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Receives table at `t', key at `key' and value at top.
|
||||||
|
*/
|
||||||
|
void luaV_settable (lua_State *L, StkId t, StkId key) {
|
||||||
|
int tg;
|
||||||
|
if (ttype(t) == TAG_TABLE && /* `t' is a table? */
|
||||||
|
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */
|
||||||
|
ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */
|
||||||
|
*luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */
|
||||||
|
else { /* try a `settable' tag method */
|
||||||
|
const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
|
||||||
|
if (ttype(im) != TAG_NIL) {
|
||||||
|
luaD_checkstack(L, 3);
|
||||||
|
*(L->top+2) = *(L->top-1);
|
||||||
|
*(L->top+1) = *key;
|
||||||
|
*(L->top) = *t;
|
||||||
|
*(L->top-1) = *im;
|
||||||
|
L->top += 3;
|
||||||
|
luaD_call(L, L->top - 4, 0); /* call `settable' tag method */
|
||||||
|
}
|
||||||
|
else /* no tag method... */
|
||||||
|
luaG_typeerror(L, t, "index");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const TObject *luaV_getglobal (lua_State *L, TString *s) {
|
||||||
|
const TObject *value = luaH_getstr(L->gt, s);
|
||||||
|
TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
|
||||||
|
if (ttype(im) == TAG_NIL) /* is there a tag method? */
|
||||||
|
return value; /* default behavior */
|
||||||
|
else { /* tag method */
|
||||||
|
luaD_checkstack(L, 3);
|
||||||
|
*L->top = *im;
|
||||||
|
ttype(L->top+1) = TAG_STRING;
|
||||||
|
tsvalue(L->top+1) = s; /* global name */
|
||||||
|
*(L->top+2) = *value;
|
||||||
|
L->top += 3;
|
||||||
|
luaD_call(L, L->top - 3, 1);
|
||||||
|
return L->top - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaV_setglobal (lua_State *L, TString *s) {
|
||||||
|
const TObject *oldvalue = luaH_getstr(L->gt, s);
|
||||||
|
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
|
||||||
|
if (ttype(im) == TAG_NIL) { /* is there a tag method? */
|
||||||
|
if (oldvalue != &luaO_nilobject) {
|
||||||
|
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
|
||||||
|
*(TObject *)oldvalue = *(L->top - 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
TObject key;
|
||||||
|
ttype(&key) = TAG_STRING;
|
||||||
|
tsvalue(&key) = s;
|
||||||
|
*luaH_set(L, L->gt, &key) = *(L->top - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
luaD_checkstack(L, 3);
|
||||||
|
*(L->top+2) = *(L->top-1); /* new value */
|
||||||
|
*(L->top+1) = *oldvalue;
|
||||||
|
ttype(L->top) = TAG_STRING;
|
||||||
|
tsvalue(L->top) = s;
|
||||||
|
*(L->top-1) = *im;
|
||||||
|
L->top += 3;
|
||||||
|
luaD_call(L, L->top - 4, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int call_binTM (lua_State *L, StkId top, IMS event) {
|
||||||
|
/* try first operand */
|
||||||
|
const TObject *im = luaT_getimbyObj(L, top-2, event);
|
||||||
|
L->top = top;
|
||||||
|
if (ttype(im) == TAG_NIL) {
|
||||||
|
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
|
||||||
|
if (ttype(im) == TAG_NIL) {
|
||||||
|
im = luaT_getim(L, 0, event); /* try a `global' method */
|
||||||
|
if (ttype(im) == TAG_NIL)
|
||||||
|
return 0; /* error */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushstring(L, luaT_eventname[event]);
|
||||||
|
luaD_callTM(L, im, 3, 1);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void call_arith (lua_State *L, StkId top, IMS event) {
|
||||||
|
if (!call_binTM(L, top, event))
|
||||||
|
luaG_binerror(L, top-2, TAG_NUMBER, "perform arithmetic on");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int luaV_strcomp (const TString *ls, const TString *rs) {
|
||||||
|
const char *l = ls->str;
|
||||||
|
size_t ll = ls->u.s.len;
|
||||||
|
const char *r = rs->str;
|
||||||
|
size_t lr = rs->u.s.len;
|
||||||
|
for (;;) {
|
||||||
|
int temp = strcoll(l, r);
|
||||||
|
if (temp != 0) return temp;
|
||||||
|
else { /* strings are equal up to a '\0' */
|
||||||
|
size_t len = strlen(l); /* index of first '\0' in both strings */
|
||||||
|
if (len == ll) /* l is finished? */
|
||||||
|
return (len == lr) ? 0 : -1; /* l is equal or smaller than r */
|
||||||
|
else if (len == lr) /* r is finished? */
|
||||||
|
return 1; /* l is greater than r (because l is not finished) */
|
||||||
|
/* both strings longer than `len'; go on comparing (after the '\0') */
|
||||||
|
len++;
|
||||||
|
l += len; ll -= len; r += len; lr -= len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
|
||||||
|
if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER)
|
||||||
|
return (nvalue(l) < nvalue(r));
|
||||||
|
else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING)
|
||||||
|
return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
|
||||||
|
else { /* call TM */
|
||||||
|
luaD_checkstack(L, 2);
|
||||||
|
*top++ = *l;
|
||||||
|
*top++ = *r;
|
||||||
|
if (!call_binTM(L, top, IM_LT))
|
||||||
|
luaG_ordererror(L, top-2);
|
||||||
|
L->top--;
|
||||||
|
return (ttype(L->top) != TAG_NIL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void luaV_strconc (lua_State *L, int total, StkId top) {
|
||||||
|
do {
|
||||||
|
int n = 2; /* number of elements handled in this pass (at least 2) */
|
||||||
|
if (tostring(L, top-2) || tostring(L, top-1)) {
|
||||||
|
if (!call_binTM(L, top, IM_CONCAT))
|
||||||
|
luaG_binerror(L, top-2, TAG_STRING, "concat");
|
||||||
|
}
|
||||||
|
else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
|
||||||
|
/* at least two string values; get as many as possible */
|
||||||
|
lint32 tl = (lint32)tsvalue(top-1)->u.s.len +
|
||||||
|
(lint32)tsvalue(top-2)->u.s.len;
|
||||||
|
char *buffer;
|
||||||
|
int i;
|
||||||
|
while (n < total && !tostring(L, top-n-1)) { /* collect total length */
|
||||||
|
tl += tsvalue(top-n-1)->u.s.len;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
if (tl > MAX_SIZET) lua_error(L, "string size overflow");
|
||||||
|
buffer = luaO_openspace(L, tl);
|
||||||
|
tl = 0;
|
||||||
|
for (i=n; i>0; i--) { /* concat all strings */
|
||||||
|
size_t l = tsvalue(top-i)->u.s.len;
|
||||||
|
memcpy(buffer+tl, tsvalue(top-i)->str, l);
|
||||||
|
tl += l;
|
||||||
|
}
|
||||||
|
tsvalue(top-n) = luaS_newlstr(L, buffer, tl);
|
||||||
|
}
|
||||||
|
total -= n-1; /* got `n' strings to create 1 new */
|
||||||
|
top -= n-1;
|
||||||
|
} while (total > 1); /* repeat until only 1 result left */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void luaV_pack (lua_State *L, StkId firstelem) {
|
||||||
|
int i;
|
||||||
|
Hash *htab = luaH_new(L, 0);
|
||||||
|
for (i=0; firstelem+i<L->top; i++)
|
||||||
|
*luaH_setint(L, htab, i+1) = *(firstelem+i);
|
||||||
|
/* store counter in field `n' */
|
||||||
|
luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
|
||||||
|
L->top = firstelem; /* remove elements from the stack */
|
||||||
|
ttype(L->top) = TAG_TABLE;
|
||||||
|
hvalue(L->top) = htab;
|
||||||
|
incr_top;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void adjust_varargs (lua_State *L, StkId base, int nfixargs) {
|
||||||
|
int nvararg = (L->top-base) - nfixargs;
|
||||||
|
if (nvararg < 0)
|
||||||
|
luaD_adjusttop(L, base, nfixargs);
|
||||||
|
luaV_pack(L, base+nfixargs);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define dojump(pc, i) { int d = GETARG_S(i); pc += d; }
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Executes the given Lua function. Parameters are between [base,top).
|
||||||
|
** Returns n such that the the results are between [n,top).
|
||||||
|
*/
|
||||||
|
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||||
|
const Proto *tf = cl->f.l;
|
||||||
|
StkId top; /* keep top local, for performance */
|
||||||
|
const Instruction *pc = tf->code;
|
||||||
|
TString **kstr = tf->kstr;
|
||||||
|
lua_Hook linehook = L->linehook;
|
||||||
|
infovalue(base-1)->pc = &pc;
|
||||||
|
luaD_checkstack(L, tf->maxstacksize+EXTRA_STACK);
|
||||||
|
if (tf->is_vararg) { /* varargs? */
|
||||||
|
adjust_varargs(L, base, tf->numparams);
|
||||||
|
luaC_checkGC(L);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
luaD_adjusttop(L, base, tf->numparams);
|
||||||
|
top = L->top;
|
||||||
|
/* main loop of interpreter */
|
||||||
|
for (;;) {
|
||||||
|
const Instruction i = *pc++;
|
||||||
|
if (linehook)
|
||||||
|
traceexec(L, base, top, linehook);
|
||||||
|
switch (GET_OPCODE(i)) {
|
||||||
|
case OP_END: {
|
||||||
|
return L->top; /* no results */
|
||||||
|
}
|
||||||
|
case OP_RETURN: {
|
||||||
|
L->top = top;
|
||||||
|
return base+GETARG_U(i);
|
||||||
|
}
|
||||||
|
case OP_CALL: {
|
||||||
|
int nres = GETARG_B(i);
|
||||||
|
if (nres == MULT_RET) nres = LUA_MULTRET;
|
||||||
|
L->top = top;
|
||||||
|
luaD_call(L, base+GETARG_A(i), nres);
|
||||||
|
top = L->top;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_TAILCALL: {
|
||||||
|
L->top = top;
|
||||||
|
luaD_call(L, base+GETARG_A(i), LUA_MULTRET);
|
||||||
|
return base+GETARG_B(i);
|
||||||
|
}
|
||||||
|
case OP_PUSHNIL: {
|
||||||
|
int n = GETARG_U(i);
|
||||||
|
LUA_ASSERT(n>0, "invalid argument");
|
||||||
|
do {
|
||||||
|
ttype(top++) = TAG_NIL;
|
||||||
|
} while (--n > 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_POP: {
|
||||||
|
top -= GETARG_U(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHINT: {
|
||||||
|
ttype(top) = TAG_NUMBER;
|
||||||
|
nvalue(top) = (Number)GETARG_S(i);
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHSTRING: {
|
||||||
|
ttype(top) = TAG_STRING;
|
||||||
|
tsvalue(top) = kstr[GETARG_U(i)];
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNUM: {
|
||||||
|
ttype(top) = TAG_NUMBER;
|
||||||
|
nvalue(top) = tf->knum[GETARG_U(i)];
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNEGNUM: {
|
||||||
|
ttype(top) = TAG_NUMBER;
|
||||||
|
nvalue(top) = -tf->knum[GETARG_U(i)];
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHUPVALUE: {
|
||||||
|
*top++ = cl->upvalue[GETARG_U(i)];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETLOCAL: {
|
||||||
|
*top++ = *(base+GETARG_U(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETGLOBAL: {
|
||||||
|
L->top = top;
|
||||||
|
*top = *luaV_getglobal(L, kstr[GETARG_U(i)]);
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETTABLE: {
|
||||||
|
L->top = top;
|
||||||
|
top--;
|
||||||
|
*(top-1) = *luaV_gettable(L, top-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETDOTTED: {
|
||||||
|
ttype(top) = TAG_STRING;
|
||||||
|
tsvalue(top) = kstr[GETARG_U(i)];
|
||||||
|
L->top = top+1;
|
||||||
|
*(top-1) = *luaV_gettable(L, top-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_GETINDEXED: {
|
||||||
|
*top = *(base+GETARG_U(i));
|
||||||
|
L->top = top+1;
|
||||||
|
*(top-1) = *luaV_gettable(L, top-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHSELF: {
|
||||||
|
TObject receiver;
|
||||||
|
receiver = *(top-1);
|
||||||
|
ttype(top) = TAG_STRING;
|
||||||
|
tsvalue(top++) = kstr[GETARG_U(i)];
|
||||||
|
L->top = top;
|
||||||
|
*(top-2) = *luaV_gettable(L, top-2);
|
||||||
|
*(top-1) = receiver;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CREATETABLE: {
|
||||||
|
L->top = top;
|
||||||
|
luaC_checkGC(L);
|
||||||
|
hvalue(top) = luaH_new(L, GETARG_U(i));
|
||||||
|
ttype(top) = TAG_TABLE;
|
||||||
|
top++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETLOCAL: {
|
||||||
|
*(base+GETARG_U(i)) = *(--top);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETGLOBAL: {
|
||||||
|
L->top = top;
|
||||||
|
luaV_setglobal(L, kstr[GETARG_U(i)]);
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETTABLE: {
|
||||||
|
StkId t = top-GETARG_A(i);
|
||||||
|
L->top = top;
|
||||||
|
luaV_settable(L, t, t+1);
|
||||||
|
top -= GETARG_B(i); /* pop values */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETLIST: {
|
||||||
|
int aux = GETARG_A(i) * LFIELDS_PER_FLUSH;
|
||||||
|
int n = GETARG_B(i);
|
||||||
|
Hash *arr = hvalue(top-n-1);
|
||||||
|
L->top = top-n; /* final value of `top' (in case of errors) */
|
||||||
|
for (; n; n--)
|
||||||
|
*luaH_setint(L, arr, n+aux) = *(--top);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SETMAP: {
|
||||||
|
int n = GETARG_U(i);
|
||||||
|
StkId finaltop = top-2*n;
|
||||||
|
Hash *arr = hvalue(finaltop-1);
|
||||||
|
L->top = finaltop; /* final value of `top' (in case of errors) */
|
||||||
|
for (; n; n--) {
|
||||||
|
top-=2;
|
||||||
|
*luaH_set(L, arr, top) = *(top+1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_ADD: {
|
||||||
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
|
call_arith(L, top, IM_ADD);
|
||||||
|
else
|
||||||
|
nvalue(top-2) += nvalue(top-1);
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_ADDI: {
|
||||||
|
if (tonumber(top-1)) {
|
||||||
|
ttype(top) = TAG_NUMBER;
|
||||||
|
nvalue(top) = (Number)GETARG_S(i);
|
||||||
|
call_arith(L, top+1, IM_ADD);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
nvalue(top-1) += (Number)GETARG_S(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_SUB: {
|
||||||
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
|
call_arith(L, top, IM_SUB);
|
||||||
|
else
|
||||||
|
nvalue(top-2) -= nvalue(top-1);
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_MULT: {
|
||||||
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
|
call_arith(L, top, IM_MUL);
|
||||||
|
else
|
||||||
|
nvalue(top-2) *= nvalue(top-1);
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_DIV: {
|
||||||
|
if (tonumber(top-2) || tonumber(top-1))
|
||||||
|
call_arith(L, top, IM_DIV);
|
||||||
|
else
|
||||||
|
nvalue(top-2) /= nvalue(top-1);
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_POW: {
|
||||||
|
if (!call_binTM(L, top, IM_POW))
|
||||||
|
lua_error(L, "undefined operation");
|
||||||
|
top--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CONCAT: {
|
||||||
|
int n = GETARG_U(i);
|
||||||
|
luaV_strconc(L, n, top);
|
||||||
|
top -= n-1;
|
||||||
|
L->top = top;
|
||||||
|
luaC_checkGC(L);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_MINUS: {
|
||||||
|
if (tonumber(top-1)) {
|
||||||
|
ttype(top) = TAG_NIL;
|
||||||
|
call_arith(L, top+1, IM_UNM);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
nvalue(top-1) = -nvalue(top-1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_NOT: {
|
||||||
|
ttype(top-1) =
|
||||||
|
(ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL;
|
||||||
|
nvalue(top-1) = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPNE: {
|
||||||
|
top -= 2;
|
||||||
|
if (!luaO_equalObj(top, top+1)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPEQ: {
|
||||||
|
top -= 2;
|
||||||
|
if (luaO_equalObj(top, top+1)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPLT: {
|
||||||
|
top -= 2;
|
||||||
|
if (luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPLE: { /* a <= b === !(b<a) */
|
||||||
|
top -= 2;
|
||||||
|
if (!luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPGT: { /* a > b === (b<a) */
|
||||||
|
top -= 2;
|
||||||
|
if (luaV_lessthan(L, top+1, top, top+2)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPGE: { /* a >= b === !(a<b) */
|
||||||
|
top -= 2;
|
||||||
|
if (!luaV_lessthan(L, top, top+1, top+2)) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPT: {
|
||||||
|
if (ttype(--top) != TAG_NIL) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPF: {
|
||||||
|
if (ttype(--top) == TAG_NIL) dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPONT: {
|
||||||
|
if (ttype(top-1) == TAG_NIL) top--;
|
||||||
|
else dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMPONF: {
|
||||||
|
if (ttype(top-1) != TAG_NIL) top--;
|
||||||
|
else dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_JMP: {
|
||||||
|
dojump(pc, i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_PUSHNILJMP: {
|
||||||
|
ttype(top++) = TAG_NIL;
|
||||||
|
pc++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_FORPREP: {
|
||||||
|
if (tonumber(top-1))
|
||||||
|
lua_error(L, "`for' step must be a number");
|
||||||
|
if (tonumber(top-2))
|
||||||
|
lua_error(L, "`for' limit must be a number");
|
||||||
|
if (tonumber(top-3))
|
||||||
|
lua_error(L, "`for' initial value must be a number");
|
||||||
|
if (nvalue(top-1) > 0 ?
|
||||||
|
nvalue(top-3) > nvalue(top-2) :
|
||||||
|
nvalue(top-3) < nvalue(top-2)) { /* `empty' loop? */
|
||||||
|
top -= 3; /* remove control variables */
|
||||||
|
dojump(pc, i); /* jump to loop end */
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_FORLOOP: {
|
||||||
|
LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step");
|
||||||
|
LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit");
|
||||||
|
if (ttype(top-3) != TAG_NUMBER)
|
||||||
|
lua_error(L, "`for' index must be a number");
|
||||||
|
nvalue(top-3) += nvalue(top-1); /* increment index */
|
||||||
|
if (nvalue(top-1) > 0 ?
|
||||||
|
nvalue(top-3) > nvalue(top-2) :
|
||||||
|
nvalue(top-3) < nvalue(top-2))
|
||||||
|
top -= 3; /* end loop: remove control variables */
|
||||||
|
else
|
||||||
|
dojump(pc, i); /* repeat loop */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_LFORPREP: {
|
||||||
|
Node *node;
|
||||||
|
if (ttype(top-1) != TAG_TABLE)
|
||||||
|
lua_error(L, "`for' table must be a table");
|
||||||
|
node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
|
||||||
|
if (node == NULL) { /* `empty' loop? */
|
||||||
|
top--; /* remove table */
|
||||||
|
dojump(pc, i); /* jump to loop end */
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
top += 2; /* index,value */
|
||||||
|
*(top-2) = *key(node);
|
||||||
|
*(top-1) = *val(node);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_LFORLOOP: {
|
||||||
|
Node *node;
|
||||||
|
LUA_ASSERT(ttype(top-3) == TAG_TABLE, "invalid table");
|
||||||
|
node = luaH_next(L, hvalue(top-3), top-2);
|
||||||
|
if (node == NULL) /* end loop? */
|
||||||
|
top -= 3; /* remove table, key, and value */
|
||||||
|
else {
|
||||||
|
*(top-2) = *key(node);
|
||||||
|
*(top-1) = *val(node);
|
||||||
|
dojump(pc, i); /* repeat loop */
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OP_CLOSURE: {
|
||||||
|
L->top = top;
|
||||||
|
luaV_Lclosure(L, tf->kproto[GETARG_A(i)], GETARG_B(i));
|
||||||
|
top = L->top;
|
||||||
|
luaC_checkGC(L);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
lvm.h
Normal file
32
lvm.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lvm.h,v 1.25 2000/08/31 21:02:55 roberto Exp roberto $
|
||||||
|
** Lua virtual machine
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef lvm_h
|
||||||
|
#define lvm_h
|
||||||
|
|
||||||
|
|
||||||
|
#include "ldo.h"
|
||||||
|
#include "lobject.h"
|
||||||
|
#include "ltm.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0))
|
||||||
|
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0))
|
||||||
|
|
||||||
|
|
||||||
|
int luaV_tonumber (TObject *obj);
|
||||||
|
int luaV_tostring (lua_State *L, TObject *obj);
|
||||||
|
const TObject *luaV_gettable (lua_State *L, StkId t);
|
||||||
|
void luaV_settable (lua_State *L, StkId t, StkId key);
|
||||||
|
const TObject *luaV_getglobal (lua_State *L, TString *s);
|
||||||
|
void luaV_setglobal (lua_State *L, TString *s);
|
||||||
|
StkId luaV_execute (lua_State *L, const Closure *cl, StkId base);
|
||||||
|
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems);
|
||||||
|
void luaV_Lclosure (lua_State *L, Proto *l, int nelems);
|
||||||
|
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top);
|
||||||
|
void luaV_strconc (lua_State *L, int total, StkId top);
|
||||||
|
|
||||||
|
#endif
|
||||||
84
lzio.c
Normal file
84
lzio.c
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lzio.c,v 1.12 2000/05/24 13:54:49 roberto Exp roberto $
|
||||||
|
** a generic input stream interface
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "lua.h"
|
||||||
|
|
||||||
|
#include "lzio.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------------------------------------- memory buffers --- */
|
||||||
|
|
||||||
|
static int zmfilbuf (ZIO* z) {
|
||||||
|
(void)z; /* to avoid warnings */
|
||||||
|
return EOZ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name) {
|
||||||
|
if (b==NULL) return NULL;
|
||||||
|
z->n = size;
|
||||||
|
z->p = (const unsigned char *)b;
|
||||||
|
z->filbuf = zmfilbuf;
|
||||||
|
z->u = NULL;
|
||||||
|
z->name = name;
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ------------------------------------------------------------ strings --- */
|
||||||
|
|
||||||
|
ZIO* zsopen (ZIO* z, const char* s, const char *name) {
|
||||||
|
if (s==NULL) return NULL;
|
||||||
|
return zmopen(z, s, strlen(s), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------- FILEs --- */
|
||||||
|
|
||||||
|
static int zffilbuf (ZIO* z) {
|
||||||
|
size_t n;
|
||||||
|
if (feof((FILE *)z->u)) return EOZ;
|
||||||
|
n = fread(z->buffer, 1, ZBSIZE, (FILE *)z->u);
|
||||||
|
if (n==0) return EOZ;
|
||||||
|
z->n = n-1;
|
||||||
|
z->p = z->buffer;
|
||||||
|
return *(z->p++);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ZIO* zFopen (ZIO* z, FILE* f, const char *name) {
|
||||||
|
if (f==NULL) return NULL;
|
||||||
|
z->n = 0;
|
||||||
|
z->p = z->buffer;
|
||||||
|
z->filbuf = zffilbuf;
|
||||||
|
z->u = f;
|
||||||
|
z->name = name;
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------- read --- */
|
||||||
|
size_t zread (ZIO *z, void *b, size_t n) {
|
||||||
|
while (n) {
|
||||||
|
size_t m;
|
||||||
|
if (z->n == 0) {
|
||||||
|
if (z->filbuf(z) == EOZ)
|
||||||
|
return n; /* return number of missing bytes */
|
||||||
|
zungetc(z); /* put result from `filbuf' in the buffer */
|
||||||
|
}
|
||||||
|
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
||||||
|
memcpy(b, z->p, m);
|
||||||
|
z->n -= m;
|
||||||
|
z->p += m;
|
||||||
|
b = (char *)b + m;
|
||||||
|
n -= m;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
50
lzio.h
Normal file
50
lzio.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
** $Id: lzio.h,v 1.5 1999/08/16 20:52:00 roberto Exp roberto $
|
||||||
|
** Buffered streams
|
||||||
|
** See Copyright Notice in lua.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef lzio_h
|
||||||
|
#define lzio_h
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* For Lua only */
|
||||||
|
#define zFopen luaZ_Fopen
|
||||||
|
#define zsopen luaZ_sopen
|
||||||
|
#define zmopen luaZ_mopen
|
||||||
|
#define zread luaZ_read
|
||||||
|
|
||||||
|
#define EOZ (-1) /* end of stream */
|
||||||
|
|
||||||
|
typedef struct zio ZIO;
|
||||||
|
|
||||||
|
ZIO* zFopen (ZIO* z, FILE* f, const char *name); /* open FILEs */
|
||||||
|
ZIO* zsopen (ZIO* z, const char* s, const char *name); /* string */
|
||||||
|
ZIO* zmopen (ZIO* z, const char* b, size_t size, const char *name); /* memory */
|
||||||
|
|
||||||
|
size_t zread (ZIO* z, void* b, size_t n); /* read next n bytes */
|
||||||
|
|
||||||
|
#define zgetc(z) (((z)->n--)>0 ? ((int)*(z)->p++): (z)->filbuf(z))
|
||||||
|
#define zungetc(z) (++(z)->n,--(z)->p)
|
||||||
|
#define zname(z) ((z)->name)
|
||||||
|
|
||||||
|
|
||||||
|
/* --------- Private Part ------------------ */
|
||||||
|
|
||||||
|
#define ZBSIZE 256 /* buffer size */
|
||||||
|
|
||||||
|
struct zio {
|
||||||
|
size_t n; /* bytes still unread */
|
||||||
|
const unsigned char* p; /* current position in buffer */
|
||||||
|
int (*filbuf)(ZIO* z);
|
||||||
|
void* u; /* additional data */
|
||||||
|
const char *name;
|
||||||
|
unsigned char buffer[ZBSIZE]; /* buffer */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
188
makefile
188
makefile
@@ -1,78 +1,160 @@
|
|||||||
# $Id: makefile,v 1.12 1995/02/02 19:02:03 roberto Exp $
|
#
|
||||||
|
## $Id: makefile,v 1.27 2000/09/11 20:29:27 roberto Exp roberto $
|
||||||
|
## Makefile
|
||||||
|
## See Copyright Notice in lua.h
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
#CONFIGURATION
|
||||||
|
|
||||||
|
# define (undefine) POPEN if your system (does not) support piped I/O
|
||||||
|
#
|
||||||
|
# define (undefine) _POSIX_SOURCE if your system is (not) POSIX compliant
|
||||||
|
#
|
||||||
|
# define (undefine) OLD_ANSI if your system does NOT have some new ANSI
|
||||||
|
# facilities (e.g. strerror, locale.h, memmove). SunOS does not comply;
|
||||||
|
# so, add "-DOLD_ANSI" on SunOS
|
||||||
|
#
|
||||||
|
# define LUA_NUM_TYPE if you need numbers to be different from double
|
||||||
|
# (for instance, -DLUA_NUM_TYPE=float)
|
||||||
|
# you may need to adapat the code, too.
|
||||||
|
#
|
||||||
|
# define LUA_COMPAT_READPATTERN if you need read patterns
|
||||||
|
# (only for compatibility with previous versions)
|
||||||
|
# define LUA_COMPAT_ARGRET for compatibility in the way function results
|
||||||
|
# are passed as arguments
|
||||||
|
# define LUA_DEPRECATETFUNCS to include obsolete functions
|
||||||
|
|
||||||
|
CONFIG = -DPOPEN -D_POSIX_SOURCE
|
||||||
|
#CONFIG = -DOLD_ANSI -DDEBUG -DLUA_COMPAT_READPATTERN -DLUA_COMPAT_ARGRET
|
||||||
|
# -DLUA_DEPRECATETFUNCS
|
||||||
|
|
||||||
|
|
||||||
# Compilation parameters
|
# Compilation parameters
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -I/usr/5include -Wall -Wmissing-prototypes -Wshadow -ansi -O2
|
CWARNS = -Wall -W -pedantic \
|
||||||
|
-Waggregate-return \
|
||||||
|
-Wcast-align \
|
||||||
|
-Wmissing-prototypes \
|
||||||
|
-Wnested-externs \
|
||||||
|
-Wpointer-arith \
|
||||||
|
-Wshadow \
|
||||||
|
-Wwrite-strings
|
||||||
|
# -Wcast-qual
|
||||||
|
# -Wtraditional
|
||||||
|
|
||||||
|
CFLAGS = $(CONFIG) $(CWARNS) -ansi -O2
|
||||||
|
|
||||||
|
|
||||||
|
# To make early versions
|
||||||
|
CO_OPTIONS =
|
||||||
|
|
||||||
#CC = acc
|
|
||||||
#CFLAGS = -fast -I/usr/5include
|
|
||||||
|
|
||||||
AR = ar
|
AR = ar
|
||||||
ARFLAGS = rvl
|
ARFLAGS = rvl
|
||||||
|
|
||||||
|
|
||||||
# Aplication modules
|
# Aplication modules
|
||||||
LUAMOD = \
|
LUAOBJS = \
|
||||||
parser \
|
lstate.o \
|
||||||
lex \
|
lapi.o \
|
||||||
opcode \
|
lmem.o \
|
||||||
hash \
|
lstring.o \
|
||||||
table \
|
ltable.o \
|
||||||
inout \
|
ltm.o \
|
||||||
tree \
|
lvm.o \
|
||||||
fallback\
|
ldo.o \
|
||||||
mem
|
lobject.o \
|
||||||
|
lfunc.o \
|
||||||
|
lgc.o \
|
||||||
|
lcode.o \
|
||||||
|
lparser.o \
|
||||||
|
llex.o \
|
||||||
|
lundump.o \
|
||||||
|
lzio.o \
|
||||||
|
ldebug.o \
|
||||||
|
ltests.o
|
||||||
|
|
||||||
LIBMOD = \
|
LIBOBJS = \
|
||||||
iolib \
|
lauxlib.o \
|
||||||
strlib \
|
lbaselib.o \
|
||||||
mathlib
|
liolib.o \
|
||||||
|
lmathlib.o \
|
||||||
|
lstrlib.o \
|
||||||
|
ldblib.o
|
||||||
|
|
||||||
LUAOBJS = $(LUAMOD:%=%.o)
|
|
||||||
|
|
||||||
LIBOBJS = $(LIBMOD:%=%.o)
|
lua : lua.o liblua.a liblualib.a
|
||||||
|
$(CC) $(CFLAGS) -o $@ lua.o -L. -llua -llualib -lm
|
||||||
|
|
||||||
lua : lua.o lua.a lualib.a
|
liblua.a : $(LUAOBJS)
|
||||||
$(CC) $(CFLAGS) -o $@ lua.c lua.a lualib.a -lm
|
|
||||||
|
|
||||||
lua.a : parser.c $(LUAOBJS)
|
|
||||||
$(AR) $(ARFLAGS) $@ $?
|
$(AR) $(ARFLAGS) $@ $?
|
||||||
ranlib lua.a
|
ranlib $@
|
||||||
|
|
||||||
lualib.a : $(LIBOBJS)
|
liblualib.a : $(LIBOBJS)
|
||||||
$(AR) $(ARFLAGS) $@ $?
|
$(AR) $(ARFLAGS) $@ $?
|
||||||
ranlib $@
|
ranlib $@
|
||||||
|
|
||||||
liblua.so.1.0 : lua.o
|
liblua.so.1.0 : lua.o
|
||||||
ld -o liblua.so.1.0 $(LUAOBJS)
|
ld -o liblua.so.1.0 $(LUAOBJS)
|
||||||
|
|
||||||
%.o : %.c
|
|
||||||
$(CC) $(CFLAGS) -c -o $@ $<
|
|
||||||
|
|
||||||
|
|
||||||
parser.c : lua.stx
|
|
||||||
yacc++ -d lua.stx ; mv -f y.tab.c parser.c ; mv -f y.tab.h parser.h
|
|
||||||
|
|
||||||
clear :
|
clear :
|
||||||
rcsclean
|
rcsclean
|
||||||
rm -f *.o
|
rm -f *.o *.a
|
||||||
rm -f parser.c parser.h
|
|
||||||
co lua.h lualib.h
|
|
||||||
|
|
||||||
% : RCS/%,v
|
|
||||||
co $@
|
|
||||||
|
|
||||||
|
|
||||||
fallback.o : fallback.c mem.h fallback.h opcode.h lua.h types.h tree.h inout.h
|
%.h : RCS/%.h,v
|
||||||
hash.o : hash.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
co $(CO_OPTIONS) $@
|
||||||
inout.o : inout.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
|
||||||
iolib.o : iolib.c mem.h lua.h lualib.h
|
%.c : RCS/%.c,v
|
||||||
lex.o : lex.c tree.h types.h table.h opcode.h lua.h inout.h parser.h ugly.h
|
co $(CO_OPTIONS) $@
|
||||||
lua.o : lua.c lua.h lualib.h
|
|
||||||
mathlib.o : mathlib.c lualib.h lua.h
|
|
||||||
mem.o : mem.c mem.h lua.h
|
lapi.o: lapi.c lua.h lapi.h lobject.h llimits.h ldo.h lstate.h \
|
||||||
opcode.o : opcode.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h \
|
luadebug.h lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h lvm.h
|
||||||
fallback.h
|
lauxlib.o: lauxlib.c lua.h lauxlib.h luadebug.h
|
||||||
strlib.o : strlib.c mem.h lua.h lualib.h
|
lbaselib.o: lbaselib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||||
table.o : table.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h \
|
lcode.o: lcode.c /usr/include/stdlib.h lua.h lcode.h llex.h lobject.h \
|
||||||
fallback.h
|
llimits.h lzio.h lopcodes.h lparser.h ldo.h lstate.h luadebug.h \
|
||||||
tree.o : tree.c mem.h lua.h tree.h types.h table.h opcode.h
|
lmem.h
|
||||||
parser.o : parser.c mem.h opcode.h lua.h types.h tree.h hash.h inout.h table.h
|
ldblib.o: ldblib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||||
|
ldebug.o: ldebug.c lua.h lapi.h lobject.h llimits.h lcode.h llex.h \
|
||||||
|
lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||||
|
lfunc.h lstring.h ltable.h ltm.h
|
||||||
|
ldo.o: ldo.c lua.h ldebug.h lstate.h lobject.h llimits.h luadebug.h \
|
||||||
|
ldo.h lgc.h lmem.h lparser.h lzio.h lstring.h ltable.h ltm.h \
|
||||||
|
lundump.h lvm.h
|
||||||
|
lfunc.o: lfunc.c lua.h lfunc.h lobject.h llimits.h lmem.h lstate.h \
|
||||||
|
luadebug.h
|
||||||
|
lgc.o: lgc.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||||
|
lfunc.h lgc.h lmem.h lstring.h ltable.h ltm.h
|
||||||
|
liolib.o: liolib.c lua.h lauxlib.h luadebug.h lualib.h
|
||||||
|
llex.o: llex.c lua.h llex.h lobject.h llimits.h lzio.h lmem.h \
|
||||||
|
lparser.h lstate.h luadebug.h lstring.h ltable.h
|
||||||
|
lmathlib.o: lmathlib.c lua.h lauxlib.h lualib.h
|
||||||
|
lmem.o: lmem.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||||
|
lmem.h
|
||||||
|
lobject.o: lobject.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||||
|
luadebug.h
|
||||||
|
lparser.o: lparser.c lua.h lcode.h llex.h lobject.h llimits.h lzio.h \
|
||||||
|
lopcodes.h lparser.h lfunc.h lmem.h lstate.h luadebug.h lstring.h
|
||||||
|
lstate.o: lstate.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||||
|
lgc.h llex.h lzio.h lmem.h lstring.h ltable.h ltm.h
|
||||||
|
lstring.o: lstring.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||||
|
luadebug.h lstring.h
|
||||||
|
lstrlib.o: lstrlib.c lua.h lauxlib.h lualib.h
|
||||||
|
ltable.o: ltable.c lua.h lmem.h llimits.h lobject.h lstate.h \
|
||||||
|
luadebug.h lstring.h ltable.h
|
||||||
|
ltests.o: ltests.c lua.h lapi.h lobject.h llimits.h lauxlib.h lcode.h \
|
||||||
|
llex.h lzio.h lopcodes.h lparser.h ldebug.h lstate.h luadebug.h ldo.h \
|
||||||
|
lfunc.h lmem.h lstring.h ltable.h lualib.h
|
||||||
|
ltm.o: ltm.c lua.h ldo.h lobject.h llimits.h lstate.h luadebug.h \
|
||||||
|
lmem.h ltm.h
|
||||||
|
lua.o: lua.c lua.h luadebug.h lualib.h
|
||||||
|
lundump.o: lundump.c lfunc.h lobject.h llimits.h lua.h lmem.h \
|
||||||
|
lopcodes.h lstring.h lstate.h luadebug.h lundump.h lzio.h
|
||||||
|
lvm.o: lvm.c lua.h lapi.h lobject.h llimits.h ldebug.h lstate.h \
|
||||||
|
luadebug.h ldo.h lfunc.h lgc.h lopcodes.h lstring.h ltable.h ltm.h \
|
||||||
|
lvm.h
|
||||||
|
lzio.o: lzio.c lua.h lzio.h
|
||||||
|
|||||||
3939
manual.tex
Normal file
3939
manual.tex
Normal file
File diff suppressed because it is too large
Load Diff
315
mathlib.c
315
mathlib.c
@@ -1,315 +0,0 @@
|
|||||||
/*
|
|
||||||
** mathlib.c
|
|
||||||
** Mathematics library to LUA
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_mathlib="$Id: mathlib.c,v 1.8 1995/01/04 18:49:54 roberto Exp $";
|
|
||||||
|
|
||||||
#include <stdio.h> /* NULL */
|
|
||||||
#include <math.h>
|
|
||||||
|
|
||||||
#include "lualib.h"
|
|
||||||
#include "lua.h"
|
|
||||||
|
|
||||||
#define PI 3.14159265358979323846
|
|
||||||
#define TODEGREE(a) ((a)*180.0/PI)
|
|
||||||
#define TORAD(a) ((a)*PI/180.0)
|
|
||||||
|
|
||||||
static void math_abs (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `abs'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `abs'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
if (d < 0) d = -d;
|
|
||||||
lua_pushnumber (d);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_sin (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `sin'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `sin'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (sin(TORAD(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void math_cos (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `cos'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `cos'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (cos(TORAD(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void math_tan (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `tan'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `tan'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (tan(TORAD(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_asin (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `asin'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `asin'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(asin(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_acos (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `acos'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `acos'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(acos(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void math_atan (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `atan'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `atan'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (TODEGREE(atan(d)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_ceil (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `ceil'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `ceil'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (ceil(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_floor (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `floor'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `floor'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (floor(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void math_mod (void)
|
|
||||||
{
|
|
||||||
int d1, d2;
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
if (!lua_isnumber(o1) || !lua_isnumber(o2))
|
|
||||||
lua_error ("incorrect arguments to function `mod'");
|
|
||||||
d1 = (int) lua_getnumber(o1);
|
|
||||||
d2 = (int) lua_getnumber(o2);
|
|
||||||
lua_pushnumber (d1%d2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_sqrt (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `sqrt'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `sqrt'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (sqrt(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
static int old_pow;
|
|
||||||
|
|
||||||
static void math_pow (void)
|
|
||||||
{
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
lua_Object op = lua_getparam(3);
|
|
||||||
if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
|
|
||||||
{
|
|
||||||
lua_Object old = lua_getlocked(old_pow);
|
|
||||||
lua_pushobject(o1);
|
|
||||||
lua_pushobject(o2);
|
|
||||||
lua_pushobject(op);
|
|
||||||
if (lua_callfunction(old) != 0)
|
|
||||||
lua_error(NULL);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
double d1 = lua_getnumber(o1);
|
|
||||||
double d2 = lua_getnumber(o2);
|
|
||||||
lua_pushnumber (pow(d1,d2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void math_min (void)
|
|
||||||
{
|
|
||||||
int i=1;
|
|
||||||
double d, dmin;
|
|
||||||
lua_Object o;
|
|
||||||
if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `min'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `min'");
|
|
||||||
dmin = lua_getnumber (o);
|
|
||||||
while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
|
|
||||||
{
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `min'");
|
|
||||||
d = lua_getnumber (o);
|
|
||||||
if (d < dmin) dmin = d;
|
|
||||||
}
|
|
||||||
lua_pushnumber (dmin);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_max (void)
|
|
||||||
{
|
|
||||||
int i=1;
|
|
||||||
double d, dmax;
|
|
||||||
lua_Object o;
|
|
||||||
if ((o = lua_getparam(i++)) == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `max'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `max'");
|
|
||||||
dmax = lua_getnumber (o);
|
|
||||||
while ((o = lua_getparam(i++)) != LUA_NOOBJECT)
|
|
||||||
{
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `max'");
|
|
||||||
d = lua_getnumber (o);
|
|
||||||
if (d > dmax) dmax = d;
|
|
||||||
}
|
|
||||||
lua_pushnumber (dmax);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_log (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `log'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `log'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (log(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_log10 (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `log10'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `log10'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (log10(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void math_exp (void)
|
|
||||||
{
|
|
||||||
double d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `exp'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `exp'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (exp(d));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void math_deg (void)
|
|
||||||
{
|
|
||||||
float d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `deg'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `deg'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (d*180./PI);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void math_rad (void)
|
|
||||||
{
|
|
||||||
float d;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_error ("too few arguments to function `rad'");
|
|
||||||
if (!lua_isnumber(o))
|
|
||||||
lua_error ("incorrect arguments to function `rad'");
|
|
||||||
d = lua_getnumber(o);
|
|
||||||
lua_pushnumber (d/180.*PI);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open math library
|
|
||||||
*/
|
|
||||||
void mathlib_open (void)
|
|
||||||
{
|
|
||||||
lua_register ("abs", math_abs);
|
|
||||||
lua_register ("sin", math_sin);
|
|
||||||
lua_register ("cos", math_cos);
|
|
||||||
lua_register ("tan", math_tan);
|
|
||||||
lua_register ("asin", math_asin);
|
|
||||||
lua_register ("acos", math_acos);
|
|
||||||
lua_register ("atan", math_atan);
|
|
||||||
lua_register ("ceil", math_ceil);
|
|
||||||
lua_register ("floor", math_floor);
|
|
||||||
lua_register ("mod", math_mod);
|
|
||||||
lua_register ("sqrt", math_sqrt);
|
|
||||||
lua_register ("min", math_min);
|
|
||||||
lua_register ("max", math_max);
|
|
||||||
lua_register ("log", math_log);
|
|
||||||
lua_register ("log10", math_log10);
|
|
||||||
lua_register ("exp", math_exp);
|
|
||||||
lua_register ("deg", math_deg);
|
|
||||||
lua_register ("rad", math_rad);
|
|
||||||
old_pow = lua_lockobject(lua_setfallback("arith", math_pow));
|
|
||||||
}
|
|
||||||
13
mathlib.h
13
mathlib.h
@@ -1,13 +0,0 @@
|
|||||||
/*
|
|
||||||
** Math library to LUA
|
|
||||||
** TeCGraf - PUC-Rio
|
|
||||||
** $Id: $
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef strlib_h
|
|
||||||
|
|
||||||
void mathlib_open (void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
164
opcode.h
164
opcode.h
@@ -1,164 +0,0 @@
|
|||||||
/*
|
|
||||||
** TeCGraf - PUC-Rio
|
|
||||||
** $Id: opcode.h,v 3.9 1994/11/23 14:31:11 roberto Stab $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef opcode_h
|
|
||||||
#define opcode_h
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "types.h"
|
|
||||||
#include "tree.h"
|
|
||||||
|
|
||||||
#ifndef STACKGAP
|
|
||||||
#define STACKGAP 128
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef real
|
|
||||||
#define real float
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define FIELDS_PER_FLUSH 40
|
|
||||||
|
|
||||||
#define MAX_TEMPS 20
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum
|
|
||||||
{
|
|
||||||
PUSHNIL,
|
|
||||||
PUSH0, PUSH1, PUSH2,
|
|
||||||
PUSHBYTE,
|
|
||||||
PUSHWORD,
|
|
||||||
PUSHFLOAT,
|
|
||||||
PUSHSTRING,
|
|
||||||
PUSHFUNCTION,
|
|
||||||
PUSHLOCAL0, PUSHLOCAL1, PUSHLOCAL2, PUSHLOCAL3, PUSHLOCAL4,
|
|
||||||
PUSHLOCAL5, PUSHLOCAL6, PUSHLOCAL7, PUSHLOCAL8, PUSHLOCAL9,
|
|
||||||
PUSHLOCAL,
|
|
||||||
PUSHGLOBAL,
|
|
||||||
PUSHINDEXED,
|
|
||||||
PUSHSELF,
|
|
||||||
STORELOCAL0, STORELOCAL1, STORELOCAL2, STORELOCAL3, STORELOCAL4,
|
|
||||||
STORELOCAL5, STORELOCAL6, STORELOCAL7, STORELOCAL8, STORELOCAL9,
|
|
||||||
STORELOCAL,
|
|
||||||
STOREGLOBAL,
|
|
||||||
STOREINDEXED0,
|
|
||||||
STOREINDEXED,
|
|
||||||
STORELIST0,
|
|
||||||
STORELIST,
|
|
||||||
STORERECORD,
|
|
||||||
ADJUST0,
|
|
||||||
ADJUST,
|
|
||||||
CREATEARRAY,
|
|
||||||
EQOP,
|
|
||||||
LTOP,
|
|
||||||
LEOP,
|
|
||||||
GTOP,
|
|
||||||
GEOP,
|
|
||||||
ADDOP,
|
|
||||||
SUBOP,
|
|
||||||
MULTOP,
|
|
||||||
DIVOP,
|
|
||||||
POWOP,
|
|
||||||
CONCOP,
|
|
||||||
MINUSOP,
|
|
||||||
NOTOP,
|
|
||||||
ONTJMP,
|
|
||||||
ONFJMP,
|
|
||||||
JMP,
|
|
||||||
UPJMP,
|
|
||||||
IFFJMP,
|
|
||||||
IFFUPJMP,
|
|
||||||
POP,
|
|
||||||
CALLFUNC,
|
|
||||||
RETCODE0,
|
|
||||||
RETCODE,
|
|
||||||
SETFUNCTION,
|
|
||||||
SETLINE,
|
|
||||||
RESET
|
|
||||||
} OpCode;
|
|
||||||
|
|
||||||
#define MULT_RET 255
|
|
||||||
|
|
||||||
|
|
||||||
typedef void (*Cfunction) (void);
|
|
||||||
typedef int (*Input) (void);
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
Cfunction f;
|
|
||||||
real n;
|
|
||||||
TaggedString *ts;
|
|
||||||
Byte *b;
|
|
||||||
struct Hash *a;
|
|
||||||
void *u;
|
|
||||||
} Value;
|
|
||||||
|
|
||||||
typedef struct Object
|
|
||||||
{
|
|
||||||
lua_Type tag;
|
|
||||||
Value value;
|
|
||||||
} Object;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
Object object;
|
|
||||||
} Symbol;
|
|
||||||
|
|
||||||
/* Macros to access structure members */
|
|
||||||
#define tag(o) ((o)->tag)
|
|
||||||
#define nvalue(o) ((o)->value.n)
|
|
||||||
#define svalue(o) ((o)->value.ts->str)
|
|
||||||
#define tsvalue(o) ((o)->value.ts)
|
|
||||||
#define bvalue(o) ((o)->value.b)
|
|
||||||
#define avalue(o) ((o)->value.a)
|
|
||||||
#define fvalue(o) ((o)->value.f)
|
|
||||||
#define uvalue(o) ((o)->value.u)
|
|
||||||
|
|
||||||
/* Macros to access symbol table */
|
|
||||||
#define s_object(i) (lua_table[i].object)
|
|
||||||
#define s_tag(i) (tag(&s_object(i)))
|
|
||||||
#define s_nvalue(i) (nvalue(&s_object(i)))
|
|
||||||
#define s_svalue(i) (svalue(&s_object(i)))
|
|
||||||
#define s_bvalue(i) (bvalue(&s_object(i)))
|
|
||||||
#define s_avalue(i) (avalue(&s_object(i)))
|
|
||||||
#define s_fvalue(i) (fvalue(&s_object(i)))
|
|
||||||
#define s_uvalue(i) (uvalue(&s_object(i)))
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
struct {char c1; char c2;} m;
|
|
||||||
Word w;
|
|
||||||
} CodeWord;
|
|
||||||
#define get_word(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;}
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
struct {char c1; char c2; char c3; char c4;} m;
|
|
||||||
float f;
|
|
||||||
} CodeFloat;
|
|
||||||
#define get_float(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
|
||||||
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
|
||||||
|
|
||||||
typedef union
|
|
||||||
{
|
|
||||||
struct {char c1; char c2; char c3; char c4;} m;
|
|
||||||
Byte *b;
|
|
||||||
} CodeCode;
|
|
||||||
#define get_code(code,pc) {code.m.c1 = *pc++; code.m.c2 = *pc++;\
|
|
||||||
code.m.c3 = *pc++; code.m.c4 = *pc++;}
|
|
||||||
|
|
||||||
|
|
||||||
/* Exported functions */
|
|
||||||
char *lua_strdup (char *l);
|
|
||||||
|
|
||||||
void lua_setinput (Input fn); /* from "lex.c" module */
|
|
||||||
char *lua_lasttext (void); /* from "lex.c" module */
|
|
||||||
int yylex (void); /* from "lex.c" module */
|
|
||||||
void lua_parse (Byte **code); /* from "lua.stx" module */
|
|
||||||
void lua_travstack (void (*fn)(Object *));
|
|
||||||
Object *luaI_Address (lua_Object o);
|
|
||||||
void luaI_pushobject (Object *o);
|
|
||||||
void luaI_gcFB (Object *o);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
167
strlib.c
167
strlib.c
@@ -1,167 +0,0 @@
|
|||||||
/*
|
|
||||||
** strlib.c
|
|
||||||
** String library to LUA
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $";
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
#include "lua.h"
|
|
||||||
#include "lualib.h"
|
|
||||||
|
|
||||||
|
|
||||||
static char *newstring (lua_Object o)
|
|
||||||
{
|
|
||||||
char *s = lua_getstring(o);
|
|
||||||
char *ns = (char *)malloc(strlen(s)+1);
|
|
||||||
if (ns == 0)
|
|
||||||
lua_error("not enough memory for new string");
|
|
||||||
strcpy(ns, s);
|
|
||||||
return ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the position of the first caracter of a substring into a string
|
|
||||||
** LUA interface:
|
|
||||||
** n = strfind (string, substring, init, end)
|
|
||||||
*/
|
|
||||||
static void str_find (void)
|
|
||||||
{
|
|
||||||
char *s1, *s2, *f;
|
|
||||||
int init;
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
lua_Object o3 = lua_getparam (3);
|
|
||||||
lua_Object o4 = lua_getparam (4);
|
|
||||||
if (!lua_isstring(o1) || !lua_isstring(o2))
|
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
if (o3 == LUA_NOOBJECT)
|
|
||||||
init = 0;
|
|
||||||
else if (lua_isnumber(o3))
|
|
||||||
init = lua_getnumber(o3)-1;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
return; /* to avoid warnings */
|
|
||||||
}
|
|
||||||
s1 = lua_getstring(o1);
|
|
||||||
s2 = lua_getstring(o2);
|
|
||||||
f = strstr(s1+init,s2);
|
|
||||||
if (f != NULL)
|
|
||||||
{
|
|
||||||
int pos = f-s1+1;
|
|
||||||
if (o4 == LUA_NOOBJECT)
|
|
||||||
lua_pushnumber (pos);
|
|
||||||
else if (!lua_isnumber(o4))
|
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1)
|
|
||||||
lua_pushnumber (pos);
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the string length
|
|
||||||
** LUA interface:
|
|
||||||
** n = strlen (string)
|
|
||||||
*/
|
|
||||||
static void str_len (void)
|
|
||||||
{
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (!lua_isstring(o))
|
|
||||||
lua_error ("incorrect arguments to function `strlen'");
|
|
||||||
lua_pushnumber(strlen(lua_getstring(o)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the substring of a string, from start to end
|
|
||||||
** LUA interface:
|
|
||||||
** substring = strsub (string, start, end)
|
|
||||||
*/
|
|
||||||
static void str_sub (void)
|
|
||||||
{
|
|
||||||
int start, end;
|
|
||||||
char *s;
|
|
||||||
lua_Object o1 = lua_getparam (1);
|
|
||||||
lua_Object o2 = lua_getparam (2);
|
|
||||||
lua_Object o3 = lua_getparam (3);
|
|
||||||
if (!lua_isstring(o1) || !lua_isnumber(o2))
|
|
||||||
lua_error ("incorrect arguments to function `strsub'");
|
|
||||||
if (o3 != LUA_NOOBJECT && !lua_isnumber(o3))
|
|
||||||
lua_error ("incorrect third argument to function `strsub'");
|
|
||||||
s = newstring(o1);
|
|
||||||
start = lua_getnumber (o2);
|
|
||||||
end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
|
|
||||||
if (end < start || start < 1 || end > strlen(s))
|
|
||||||
lua_pushliteral("");
|
|
||||||
else
|
|
||||||
{
|
|
||||||
s[end] = 0;
|
|
||||||
lua_pushstring (&s[start-1]);
|
|
||||||
}
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert a string to lower case.
|
|
||||||
** LUA interface:
|
|
||||||
** lowercase = strlower (string)
|
|
||||||
*/
|
|
||||||
static void str_lower (void)
|
|
||||||
{
|
|
||||||
char *s, *c;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (!lua_isstring(o))
|
|
||||||
lua_error ("incorrect arguments to function `strlower'");
|
|
||||||
c = s = newstring(o);
|
|
||||||
while (*c != 0)
|
|
||||||
{
|
|
||||||
*c = tolower(*c);
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Convert a string to upper case.
|
|
||||||
** LUA interface:
|
|
||||||
** uppercase = strupper (string)
|
|
||||||
*/
|
|
||||||
static void str_upper (void)
|
|
||||||
{
|
|
||||||
char *s, *c;
|
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (!lua_isstring(o))
|
|
||||||
lua_error ("incorrect arguments to function `strlower'");
|
|
||||||
c = s = newstring(o);
|
|
||||||
while (*c != 0)
|
|
||||||
{
|
|
||||||
*c = toupper(*c);
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Open string library
|
|
||||||
*/
|
|
||||||
void strlib_open (void)
|
|
||||||
{
|
|
||||||
lua_register ("strfind", str_find);
|
|
||||||
lua_register ("strlen", str_len);
|
|
||||||
lua_register ("strsub", str_sub);
|
|
||||||
lua_register ("strlower", str_lower);
|
|
||||||
lua_register ("strupper", str_upper);
|
|
||||||
}
|
|
||||||
13
strlib.h
13
strlib.h
@@ -1,13 +0,0 @@
|
|||||||
/*
|
|
||||||
** String library to LUA
|
|
||||||
** TeCGraf - PUC-Rio
|
|
||||||
** $Id: $
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#ifndef strlib_h
|
|
||||||
|
|
||||||
void strlib_open (void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
277
table.c
277
table.c
@@ -1,277 +0,0 @@
|
|||||||
/*
|
|
||||||
** table.c
|
|
||||||
** Module to control static tables
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_table="$Id: table.c,v 2.27 1995/01/13 22:11:12 roberto Exp celes $";
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
#include "tree.h"
|
|
||||||
#include "hash.h"
|
|
||||||
#include "inout.h"
|
|
||||||
#include "table.h"
|
|
||||||
#include "lua.h"
|
|
||||||
#include "fallback.h"
|
|
||||||
|
|
||||||
|
|
||||||
#define BUFFER_BLOCK 256
|
|
||||||
|
|
||||||
Symbol *lua_table;
|
|
||||||
static Word lua_ntable = 0;
|
|
||||||
static Long lua_maxsymbol = 0;
|
|
||||||
|
|
||||||
TaggedString **lua_constant;
|
|
||||||
static Word lua_nconstant = 0;
|
|
||||||
static Long lua_maxconstant = 0;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define MAXFILE 20
|
|
||||||
char *lua_file[MAXFILE];
|
|
||||||
int lua_nfile;
|
|
||||||
|
|
||||||
#define GARBAGE_BLOCK 256
|
|
||||||
#define MIN_GARBAGE_BLOCK 10
|
|
||||||
|
|
||||||
static void lua_nextvar (void);
|
|
||||||
static void setglobal (void);
|
|
||||||
static void getglobal (void);
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Initialise symbol table with internal functions
|
|
||||||
*/
|
|
||||||
static void lua_initsymbol (void)
|
|
||||||
{
|
|
||||||
Word n;
|
|
||||||
lua_maxsymbol = BUFFER_BLOCK;
|
|
||||||
lua_table = newvector(lua_maxsymbol, Symbol);
|
|
||||||
n = luaI_findsymbolbyname("next");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
|
|
||||||
n = luaI_findsymbolbyname("dofile");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldofile;
|
|
||||||
n = luaI_findsymbolbyname("setglobal");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
|
|
||||||
n = luaI_findsymbolbyname("getglobal");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
|
|
||||||
n = luaI_findsymbolbyname("nextvar");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
|
|
||||||
n = luaI_findsymbolbyname("type");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
|
|
||||||
n = luaI_findsymbolbyname("tonumber");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
|
|
||||||
n = luaI_findsymbolbyname("print");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
|
|
||||||
n = luaI_findsymbolbyname("dostring");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
|
|
||||||
n = luaI_findsymbolbyname("setfallback");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
|
|
||||||
n = luaI_findsymbolbyname("error");
|
|
||||||
s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Initialise constant table with pre-defined constants
|
|
||||||
*/
|
|
||||||
void lua_initconstant (void)
|
|
||||||
{
|
|
||||||
lua_maxconstant = BUFFER_BLOCK;
|
|
||||||
lua_constant = newvector(lua_maxconstant, TaggedString *);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Given a name, search it at symbol table and return its index. If not
|
|
||||||
** found, allocate it.
|
|
||||||
*/
|
|
||||||
Word luaI_findsymbol (TreeNode *t)
|
|
||||||
{
|
|
||||||
if (lua_table == NULL)
|
|
||||||
lua_initsymbol();
|
|
||||||
if (t->varindex == NOT_USED)
|
|
||||||
{
|
|
||||||
if (lua_ntable == lua_maxsymbol)
|
|
||||||
{
|
|
||||||
if (lua_maxsymbol >= MAX_WORD)
|
|
||||||
lua_error("symbol table overflow");
|
|
||||||
lua_maxsymbol *= 2;
|
|
||||||
if (lua_maxsymbol >= MAX_WORD)
|
|
||||||
lua_maxsymbol = MAX_WORD;
|
|
||||||
lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
|
|
||||||
}
|
|
||||||
t->varindex = lua_ntable;
|
|
||||||
s_tag(lua_ntable) = LUA_T_NIL;
|
|
||||||
lua_ntable++;
|
|
||||||
}
|
|
||||||
return t->varindex;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Word luaI_findsymbolbyname (char *name)
|
|
||||||
{
|
|
||||||
return luaI_findsymbol(lua_constcreate(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Given a name, search it at constant table and return its index. If not
|
|
||||||
** found, allocate it.
|
|
||||||
** On error, return -1.
|
|
||||||
*/
|
|
||||||
Word luaI_findconstant (TreeNode *t)
|
|
||||||
{
|
|
||||||
if (lua_constant == NULL)
|
|
||||||
lua_initconstant();
|
|
||||||
if (t->constindex == NOT_USED)
|
|
||||||
{
|
|
||||||
if (lua_nconstant == lua_maxconstant)
|
|
||||||
{
|
|
||||||
if (lua_maxconstant >= MAX_WORD)
|
|
||||||
lua_error("constant table overflow");
|
|
||||||
lua_maxconstant *= 2;
|
|
||||||
if (lua_maxconstant >= MAX_WORD)
|
|
||||||
lua_maxconstant = MAX_WORD;
|
|
||||||
lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
|
|
||||||
}
|
|
||||||
t->constindex = lua_nconstant;
|
|
||||||
lua_constant[lua_nconstant] = &(t->ts);
|
|
||||||
lua_nconstant++;
|
|
||||||
}
|
|
||||||
return t->constindex;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Traverse symbol table objects
|
|
||||||
*/
|
|
||||||
void lua_travsymbol (void (*fn)(Object *))
|
|
||||||
{
|
|
||||||
Word i;
|
|
||||||
for (i=0; i<lua_ntable; i++)
|
|
||||||
fn(&s_object(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Mark an object if it is a string or a unmarked array.
|
|
||||||
*/
|
|
||||||
void lua_markobject (Object *o)
|
|
||||||
{
|
|
||||||
if (tag(o) == LUA_T_STRING && !tsvalue(o)->marked)
|
|
||||||
tsvalue(o)->marked = 1;
|
|
||||||
else if (tag(o) == LUA_T_ARRAY)
|
|
||||||
lua_hashmark (avalue(o));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Garbage collection.
|
|
||||||
** Delete all unused strings and arrays.
|
|
||||||
*/
|
|
||||||
void lua_pack (void)
|
|
||||||
{
|
|
||||||
static Long block = GARBAGE_BLOCK; /* when garbage collector will be called */
|
|
||||||
static Long nentity = 0; /* counter of new entities (strings and arrays) */
|
|
||||||
Long recovered = 0;
|
|
||||||
if (nentity++ < block) return;
|
|
||||||
lua_travstack(lua_markobject); /* mark stack objects */
|
|
||||||
lua_travsymbol(lua_markobject); /* mark symbol table objects */
|
|
||||||
luaI_travlock(lua_markobject); /* mark locked objects */
|
|
||||||
recovered += lua_strcollector();
|
|
||||||
recovered += lua_hashcollector();
|
|
||||||
nentity = 0; /* reset counter */
|
|
||||||
block=(16*block-7*recovered)/12; /* adapt block size */
|
|
||||||
if (block < MIN_GARBAGE_BLOCK) block = MIN_GARBAGE_BLOCK;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Add a file name at file table, checking overflow. This function also set
|
|
||||||
** the external variable "lua_filename" with the function filename set.
|
|
||||||
** Return 0 on success or error message on error.
|
|
||||||
*/
|
|
||||||
char *lua_addfile (char *fn)
|
|
||||||
{
|
|
||||||
if (lua_nfile >= MAXFILE)
|
|
||||||
return "too many files";
|
|
||||||
if ((lua_file[lua_nfile++] = luaI_strdup (fn)) == NULL)
|
|
||||||
return "not enough memory";
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Delete a file from file stack
|
|
||||||
*/
|
|
||||||
int lua_delfile (void)
|
|
||||||
{
|
|
||||||
luaI_free(lua_file[--lua_nfile]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return the last file name set.
|
|
||||||
*/
|
|
||||||
char *lua_filename (void)
|
|
||||||
{
|
|
||||||
return lua_file[lua_nfile-1];
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Internal function: return next global variable
|
|
||||||
*/
|
|
||||||
static void lua_nextvar (void)
|
|
||||||
{
|
|
||||||
char *varname;
|
|
||||||
TreeNode *next;
|
|
||||||
lua_Object o = lua_getparam(1);
|
|
||||||
if (o == LUA_NOOBJECT)
|
|
||||||
lua_reportbug("too few arguments to function `nextvar'");
|
|
||||||
if (lua_getparam(2) != LUA_NOOBJECT)
|
|
||||||
lua_reportbug("too many arguments to function `nextvar'");
|
|
||||||
if (lua_isnil(o))
|
|
||||||
varname = NULL;
|
|
||||||
else if (!lua_isstring(o))
|
|
||||||
{
|
|
||||||
lua_reportbug("incorrect argument to function `nextvar'");
|
|
||||||
return; /* to avoid warnings */
|
|
||||||
}
|
|
||||||
else
|
|
||||||
varname = lua_getstring(o);
|
|
||||||
next = lua_varnext(varname);
|
|
||||||
if (next == NULL)
|
|
||||||
{
|
|
||||||
lua_pushnil();
|
|
||||||
lua_pushnil();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Object name;
|
|
||||||
tag(&name) = LUA_T_STRING;
|
|
||||||
tsvalue(&name) = &(next->ts);
|
|
||||||
luaI_pushobject(&name);
|
|
||||||
luaI_pushobject(&s_object(next->varindex));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void setglobal (void)
|
|
||||||
{
|
|
||||||
lua_Object name = lua_getparam(1);
|
|
||||||
lua_Object value = lua_getparam(2);
|
|
||||||
if (!lua_isstring(name))
|
|
||||||
lua_reportbug("incorrect argument to function `setglobal'");
|
|
||||||
lua_pushobject(value);
|
|
||||||
lua_storeglobal(lua_getstring(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void getglobal (void)
|
|
||||||
{
|
|
||||||
lua_Object name = lua_getparam(1);
|
|
||||||
if (!lua_isstring(name))
|
|
||||||
lua_reportbug("incorrect argument to function `getglobal'");
|
|
||||||
lua_pushobject(lua_getglobal(lua_getstring(name)));
|
|
||||||
}
|
|
||||||
31
table.h
31
table.h
@@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
** Module to control static tables
|
|
||||||
** TeCGraf - PUC-Rio
|
|
||||||
** $Id: table.h,v 2.9 1994/11/23 14:31:11 roberto Stab roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef table_h
|
|
||||||
#define table_h
|
|
||||||
|
|
||||||
#include "tree.h"
|
|
||||||
#include "opcode.h"
|
|
||||||
|
|
||||||
extern Symbol *lua_table;
|
|
||||||
extern TaggedString **lua_constant;
|
|
||||||
|
|
||||||
extern char *lua_file[];
|
|
||||||
extern int lua_nfile;
|
|
||||||
|
|
||||||
|
|
||||||
void lua_initconstant (void);
|
|
||||||
Word luaI_findsymbolbyname (char *name);
|
|
||||||
Word luaI_findsymbol (TreeNode *t);
|
|
||||||
Word luaI_findconstant (TreeNode *t);
|
|
||||||
void lua_travsymbol (void (*fn)(Object *));
|
|
||||||
void lua_markobject (Object *o);
|
|
||||||
void lua_pack (void);
|
|
||||||
char *lua_addfile (char *fn);
|
|
||||||
int lua_delfile (void);
|
|
||||||
char *lua_filename (void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
141
tree.c
141
tree.c
@@ -1,141 +0,0 @@
|
|||||||
/*
|
|
||||||
** tree.c
|
|
||||||
** TecCGraf - PUC-Rio
|
|
||||||
*/
|
|
||||||
|
|
||||||
char *rcs_tree="$Id: tree.c,v 1.12 1994/12/20 21:20:36 roberto Exp roberto $";
|
|
||||||
|
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "mem.h"
|
|
||||||
#include "lua.h"
|
|
||||||
#include "tree.h"
|
|
||||||
#include "table.h"
|
|
||||||
|
|
||||||
|
|
||||||
#define lua_strcmp(a,b) (a[0]<b[0]?(-1):(a[0]>b[0]?(1):strcmp(a,b)))
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct StringNode {
|
|
||||||
struct StringNode *next;
|
|
||||||
TaggedString ts;
|
|
||||||
} StringNode;
|
|
||||||
|
|
||||||
static StringNode *string_root = NULL;
|
|
||||||
|
|
||||||
static TreeNode *constant_root = NULL;
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Insert a new constant/variable at the tree.
|
|
||||||
*/
|
|
||||||
static TreeNode *tree_create (TreeNode **node, char *str)
|
|
||||||
{
|
|
||||||
if (*node == NULL)
|
|
||||||
{
|
|
||||||
*node = (TreeNode *) luaI_malloc(sizeof(TreeNode)+strlen(str));
|
|
||||||
(*node)->left = (*node)->right = NULL;
|
|
||||||
strcpy((*node)->ts.str, str);
|
|
||||||
(*node)->ts.marked = 0;
|
|
||||||
(*node)->ts.hash = 0;
|
|
||||||
(*node)->varindex = (*node)->constindex = NOT_USED;
|
|
||||||
return *node;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int c = lua_strcmp(str, (*node)->ts.str);
|
|
||||||
if (c < 0)
|
|
||||||
return tree_create(&(*node)->left, str);
|
|
||||||
else if (c > 0)
|
|
||||||
return tree_create(&(*node)->right, str);
|
|
||||||
else
|
|
||||||
return *node;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TaggedString *lua_createstring (char *str)
|
|
||||||
{
|
|
||||||
StringNode *newString;
|
|
||||||
if (str == NULL) return NULL;
|
|
||||||
lua_pack();
|
|
||||||
newString = (StringNode *)luaI_malloc(sizeof(StringNode)+strlen(str));
|
|
||||||
newString->ts.marked = 0;
|
|
||||||
newString->ts.hash = 0;
|
|
||||||
strcpy(newString->ts.str, str);
|
|
||||||
newString->next = string_root;
|
|
||||||
string_root = newString;
|
|
||||||
return &(newString->ts);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TreeNode *lua_constcreate (char *str)
|
|
||||||
{
|
|
||||||
return tree_create(&constant_root, str);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Garbage collection function.
|
|
||||||
** This function traverse the string list freeing unindexed strings
|
|
||||||
*/
|
|
||||||
Long lua_strcollector (void)
|
|
||||||
{
|
|
||||||
StringNode *curr = string_root, *prev = NULL;
|
|
||||||
Long counter = 0;
|
|
||||||
while (curr)
|
|
||||||
{
|
|
||||||
StringNode *next = curr->next;
|
|
||||||
if (!curr->ts.marked)
|
|
||||||
{
|
|
||||||
if (prev == NULL) string_root = next;
|
|
||||||
else prev->next = next;
|
|
||||||
luaI_free(curr);
|
|
||||||
++counter;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
curr->ts.marked = 0;
|
|
||||||
prev = curr;
|
|
||||||
}
|
|
||||||
curr = next;
|
|
||||||
}
|
|
||||||
return counter;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Return next variable.
|
|
||||||
*/
|
|
||||||
static TreeNode *tree_next (TreeNode *node, char *str)
|
|
||||||
{
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
else if (str == NULL) return node;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int c = lua_strcmp(str, node->ts.str);
|
|
||||||
if (c == 0)
|
|
||||||
return node->left != NULL ? node->left : node->right;
|
|
||||||
else if (c < 0)
|
|
||||||
{
|
|
||||||
TreeNode *result = tree_next(node->left, str);
|
|
||||||
return result != NULL ? result : node->right;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return tree_next(node->right, str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TreeNode *lua_varnext (char *n)
|
|
||||||
{
|
|
||||||
TreeNode *result;
|
|
||||||
char *name = n;
|
|
||||||
while (1)
|
|
||||||
{ /* repeat until a valid (non nil) variable */
|
|
||||||
result = tree_next(constant_root, name);
|
|
||||||
if (result == NULL) return NULL;
|
|
||||||
if (result->varindex != NOT_USED &&
|
|
||||||
s_tag(result->varindex) != LUA_T_NIL)
|
|
||||||
return result;
|
|
||||||
name = result->ts.str;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
37
tree.h
37
tree.h
@@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
** tree.h
|
|
||||||
** TecCGraf - PUC-Rio
|
|
||||||
** $Id: tree.h,v 1.8 1994/12/20 21:20:36 roberto Exp roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef tree_h
|
|
||||||
#define tree_h
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
|
|
||||||
#define NOT_USED 0xFFFE
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct TaggedString
|
|
||||||
{
|
|
||||||
unsigned long hash; /* 0 if not initialized */
|
|
||||||
char marked; /* for garbage collection */
|
|
||||||
char str[1]; /* \0 byte already reserved */
|
|
||||||
} TaggedString;
|
|
||||||
|
|
||||||
typedef struct TreeNode
|
|
||||||
{
|
|
||||||
struct TreeNode *right;
|
|
||||||
struct TreeNode *left;
|
|
||||||
unsigned short varindex; /* != NOT_USED if this is a symbol */
|
|
||||||
unsigned short constindex; /* != NOT_USED if this is a constant */
|
|
||||||
TaggedString ts;
|
|
||||||
} TreeNode;
|
|
||||||
|
|
||||||
|
|
||||||
TaggedString *lua_createstring (char *str);
|
|
||||||
TreeNode *lua_constcreate (char *str);
|
|
||||||
Long lua_strcollector (void);
|
|
||||||
TreeNode *lua_varnext (char *n);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
31
types.h
31
types.h
@@ -1,31 +0,0 @@
|
|||||||
/*
|
|
||||||
** TeCGraf - PUC-Rio
|
|
||||||
** $Id: types.h,v 1.2 1994/12/27 20:41:47 celes Exp roberto $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef types_h
|
|
||||||
#define types_h
|
|
||||||
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
#ifndef real
|
|
||||||
#define real float
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef int Bool; /* boolean values */
|
|
||||||
|
|
||||||
#define Byte lua_Byte /* some systems have Byte as a predefined type */
|
|
||||||
typedef unsigned char Byte; /* unsigned 8 bits */
|
|
||||||
|
|
||||||
#define Word lua_Word /* some systems have Word as a predefined type */
|
|
||||||
typedef unsigned short Word; /* unsigned 16 bits */
|
|
||||||
|
|
||||||
#define MAX_WORD (USHRT_MAX-2) /* maximum value of a word (-2 for safety) */
|
|
||||||
#define MAX_INT (INT_MAX-2) /* maximum value of a int (-2 for safety) */
|
|
||||||
|
|
||||||
#define Long lua_Long /* some systems have Long as a predefined type */
|
|
||||||
typedef signed long Long; /* 32 bits */
|
|
||||||
|
|
||||||
typedef unsigned int IntPoint; /* unsigned with same size as a pointer (for hashing) */
|
|
||||||
|
|
||||||
#endif
|
|
||||||
36
ugly.h
36
ugly.h
@@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
** ugly.h
|
|
||||||
** TecCGraf - PUC-Rio
|
|
||||||
** $Id: $
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ugly_h
|
|
||||||
#define ugly_h
|
|
||||||
|
|
||||||
/* This enum must have the same order of the array 'reserved' in lex.c */
|
|
||||||
|
|
||||||
enum {
|
|
||||||
U_and=128,
|
|
||||||
U_do,
|
|
||||||
U_else,
|
|
||||||
U_elseif,
|
|
||||||
U_end,
|
|
||||||
U_function,
|
|
||||||
U_if,
|
|
||||||
U_local,
|
|
||||||
U_nil,
|
|
||||||
U_not,
|
|
||||||
U_or,
|
|
||||||
U_repeat,
|
|
||||||
U_return,
|
|
||||||
U_then,
|
|
||||||
U_until,
|
|
||||||
U_while,
|
|
||||||
U_eq = '='+128,
|
|
||||||
U_le = '<'+128,
|
|
||||||
U_ge = '>'+128,
|
|
||||||
U_ne = '~'+128,
|
|
||||||
U_sc = '.'+128
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user