├── LICENSE ├── Makefile ├── README.md ├── bin └── windows │ ├── killa.exe │ ├── killac.exe │ ├── killaext.exe │ └── test.bat ├── build ├── compare.pl └── msvc2019 │ ├── killa.sln │ ├── killa.vcproj │ ├── killa.vcxproj │ ├── killac.vcproj │ ├── killac.vcxproj │ ├── killaext.vcproj │ ├── killaext.vcxproj │ └── killaext.vcxproj.filters ├── doc └── Killa, un lenguaje de programacion basado en Lua y JavaScript.pdf ├── ext └── bc │ ├── config.h │ ├── lbc.c │ ├── lbc.h │ ├── number.c │ ├── number.h │ └── test.kia ├── ide ├── editplus │ └── killa.stx └── sublime │ └── Killa.sublime-syntax ├── lua ├── lua-5.2.0 │ └── src │ │ ├── Makefile │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lbaselib.c │ │ ├── lbitlib.c │ │ ├── lcode.c │ │ ├── lcode.h │ │ ├── lcorolib.c │ │ ├── lctype.c │ │ ├── lctype.h │ │ ├── ldblib.c │ │ ├── ldebug.c │ │ ├── ldebug.h │ │ ├── ldo.c │ │ ├── ldo.h │ │ ├── ldump.c │ │ ├── lfunc.c │ │ ├── lfunc.h │ │ ├── lgc.c │ │ ├── lgc.h │ │ ├── linit.c │ │ ├── liolib.c │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── loadlib.c │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lopcodes.c │ │ ├── lopcodes.h │ │ ├── loslib.c │ │ ├── lparser.c │ │ ├── lparser.h │ │ ├── lstate.c │ │ ├── lstate.h │ │ ├── lstring.c │ │ ├── lstring.h │ │ ├── lstrlib.c │ │ ├── ltable.c │ │ ├── ltable.h │ │ ├── ltablib.c │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── lua.c │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── luac.c │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lzio.c │ │ └── lzio.h └── lua-5.2.4 │ └── src │ ├── Makefile │ ├── lapi.c │ ├── lapi.h │ ├── lauxlib.c │ ├── lauxlib.h │ ├── lbaselib.c │ ├── lbitlib.c │ ├── lcode.c │ ├── lcode.h │ ├── lcorolib.c │ ├── lctype.c │ ├── lctype.h │ ├── ldblib.c │ ├── ldebug.c │ ├── ldebug.h │ ├── ldo.c │ ├── ldo.h │ ├── ldump.c │ ├── lfunc.c │ ├── lfunc.h │ ├── lgc.c │ ├── lgc.h │ ├── linit.c │ ├── liolib.c │ ├── llex.c │ ├── llex.h │ ├── llimits.h │ ├── lmathlib.c │ ├── lmem.c │ ├── lmem.h │ ├── loadlib.c │ ├── lobject.c │ ├── lobject.h │ ├── lopcodes.c │ ├── lopcodes.h │ ├── loslib.c │ ├── lparser.c │ ├── lparser.h │ ├── lstate.c │ ├── lstate.h │ ├── lstring.c │ ├── lstring.h │ ├── lstrlib.c │ ├── ltable.c │ ├── ltable.h │ ├── ltablib.c │ ├── ltm.c │ ├── ltm.h │ ├── lua.c │ ├── lua.h │ ├── lua.hpp │ ├── luac.c │ ├── luaconf.h │ ├── lualib.h │ ├── lundump.c │ ├── lundump.h │ ├── lvm.c │ ├── lvm.h │ ├── lzio.c │ └── lzio.h ├── src ├── Makefile ├── kapi.c ├── kapi.h ├── kauxlib.c ├── kauxlib.h ├── kbaselib.c ├── kbitlib.c ├── kcode.c ├── kcode.h ├── kcorolib.c ├── kctype.c ├── kctype.h ├── kdblib.c ├── kdebug.c ├── kdebug.h ├── kdo.c ├── kdo.h ├── kdump.c ├── kfunc.c ├── kfunc.h ├── kgc.c ├── kgc.h ├── killa.c ├── killa.h ├── killa.hpp ├── killac.c ├── killaconf.h ├── killalib.h ├── kinit.c ├── kiolib.c ├── klex.c ├── klex.h ├── klimits.h ├── klua.h ├── kmathlib.c ├── kmem.c ├── kmem.h ├── koadlib.c ├── kobject.c ├── kobject.h ├── kopcodes.c ├── kopcodes.h ├── koslib.c ├── kparser.c ├── kparser.h ├── kstate.c ├── kstate.h ├── kstring.c ├── kstring.h ├── kstrlib.c ├── ktable.c ├── ktable.h ├── ktablib.c ├── ktm.c ├── ktm.h ├── kubit.c ├── kubit.h ├── kundump.c ├── kundump.h ├── kvm.c ├── kvm.h ├── kzio.c └── kzio.h └── test ├── 99beers.kia ├── automaton.kia ├── decrypt.kia ├── euler_393.kia ├── euler_393bit.kia ├── strings.kia ├── test.kia ├── test.out └── to_string.kia /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Laurens Rodriguez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for installing Killa 2 | # See doc/readme.html for installation and customization instructions. 3 | 4 | # == CHANGE THE SETTINGS BELOW TO SUIT YOUR ENVIRONMENT ======================= 5 | 6 | # Your platform. See PLATS for possible values. 7 | PLAT= none 8 | 9 | # Where to install. The installation starts in the src and doc directories, 10 | # so take care if INSTALL_TOP is not an absolute path. See the local target. 11 | # You may want to make INSTALL_LMOD and INSTALL_CMOD consistent with 12 | # KILLA_ROOT, KILLA_LDIR, and KILLA_CDIR in killaconf.h. 13 | INSTALL_TOP= /usr/local 14 | INSTALL_BIN= $(INSTALL_TOP)/bin 15 | INSTALL_INC= $(INSTALL_TOP)/include 16 | INSTALL_LIB= $(INSTALL_TOP)/lib 17 | INSTALL_MAN= $(INSTALL_TOP)/man/man1 18 | INSTALL_LMOD= $(INSTALL_TOP)/share/killa/$V 19 | INSTALL_CMOD= $(INSTALL_TOP)/lib/killa/$V 20 | 21 | # How to install. If your install program does not support "-p", then 22 | # you may have to run ranlib on the installed libkilla.a. 23 | INSTALL= install -p 24 | INSTALL_EXEC= $(INSTALL) -m 0755 25 | INSTALL_DATA= $(INSTALL) -m 0644 26 | # 27 | # If you don't have "install" you can use "cp" instead. 28 | # INSTALL= cp -p 29 | # INSTALL_EXEC= $(INSTALL) 30 | # INSTALL_DATA= $(INSTALL) 31 | 32 | # Other utilities. 33 | MKDIR= mkdir -p 34 | RM= rm -f 35 | 36 | # == END OF USER SETTINGS -- NO NEED TO CHANGE ANYTHING BELOW THIS LINE ======= 37 | 38 | # Convenience platforms targets. 39 | PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris 40 | 41 | # What to install. 42 | TO_BIN= killa killac 43 | TO_INC= killa.h killaconf.h killalib.h lauxlib.h killa.hpp 44 | TO_LIB= libkilla.a 45 | TO_MAN= killa.1 killac.1 46 | 47 | # Killa version and release. 48 | V= 0.1 49 | R= $V.0 50 | 51 | # Targets start here. 52 | all: $(PLAT) 53 | 54 | $(PLATS) clean: 55 | cd src && $(MAKE) $@ 56 | 57 | test: dummy 58 | src/killa test/test.kia 59 | 60 | install: dummy 61 | cd src && $(MKDIR) $(INSTALL_BIN) $(INSTALL_INC) $(INSTALL_LIB) $(INSTALL_MAN) $(INSTALL_LMOD) $(INSTALL_CMOD) 62 | cd src && $(INSTALL_EXEC) $(TO_BIN) $(INSTALL_BIN) 63 | cd src && $(INSTALL_DATA) $(TO_INC) $(INSTALL_INC) 64 | cd src && $(INSTALL_DATA) $(TO_LIB) $(INSTALL_LIB) 65 | cd doc && $(INSTALL_DATA) $(TO_MAN) $(INSTALL_MAN) 66 | 67 | uninstall: 68 | cd src && cd $(INSTALL_BIN) && $(RM) $(TO_BIN) 69 | cd src && cd $(INSTALL_INC) && $(RM) $(TO_INC) 70 | cd src && cd $(INSTALL_LIB) && $(RM) $(TO_LIB) 71 | cd doc && cd $(INSTALL_MAN) && $(RM) $(TO_MAN) 72 | 73 | local: 74 | $(MAKE) install INSTALL_TOP=../install 75 | 76 | none: 77 | @echo "Please do 'make PLATFORM' where PLATFORM is one of these:" 78 | @echo " $(PLATS)" 79 | @echo "See doc/readme.html for complete instructions." 80 | 81 | # make may get confused with test/ and install/ 82 | dummy: 83 | 84 | # echo config parameters 85 | echo: 86 | @cd src && $(MAKE) -s echo 87 | @echo "PLAT= $(PLAT)" 88 | @echo "V= $V" 89 | @echo "R= $R" 90 | @echo "TO_BIN= $(TO_BIN)" 91 | @echo "TO_INC= $(TO_INC)" 92 | @echo "TO_LIB= $(TO_LIB)" 93 | @echo "TO_MAN= $(TO_MAN)" 94 | @echo "INSTALL_TOP= $(INSTALL_TOP)" 95 | @echo "INSTALL_BIN= $(INSTALL_BIN)" 96 | @echo "INSTALL_INC= $(INSTALL_INC)" 97 | @echo "INSTALL_LIB= $(INSTALL_LIB)" 98 | @echo "INSTALL_MAN= $(INSTALL_MAN)" 99 | @echo "INSTALL_LMOD= $(INSTALL_LMOD)" 100 | @echo "INSTALL_CMOD= $(INSTALL_CMOD)" 101 | @echo "INSTALL_EXEC= $(INSTALL_EXEC)" 102 | @echo "INSTALL_DATA= $(INSTALL_DATA)" 103 | 104 | # echo pkg-config data 105 | pc: 106 | @echo "version=$R" 107 | @echo "prefix=$(INSTALL_TOP)" 108 | @echo "libdir=$(INSTALL_LIB)" 109 | @echo "includedir=$(INSTALL_INC)" 110 | 111 | # list targets that do not create files (but not all makes understand .PHONY) 112 | .PHONY: all $(PLATS) clean test install local none dummy echo pecho lecho 113 | 114 | # (end of Makefile) 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Killa 2 | 3 | Killa is a programming language based in Lua and Javascript. It uses the Lua virtual machine and the Lua external C API with almost no changes but it has a different syntax similar to Javascript without the prototype mechanism and the ugly parts. 4 | 5 | Killa is a work in progress but it is already functional and has been tested inside popular game engines like the cocos2d-x engine: https://github.com/ex/cocos2d-x 6 | 7 | ### Documentation 8 | 9 | Proper documentation is missing but most of the functions are straightforward Lua functions, with the same functionality, except index zero changes. Some blog posts exist and you can also check the script examples in the [test](https://github.com/ex/Killa/tree/master/test) folder. The Spanish Thesis from where this project was born is [here](https://github.com/ex/Killa/raw/master/doc/Killa%2C%20un%20lenguaje%20de%20programacion%20basado%20en%20Lua%20y%20JavaScript.pdf) 10 | 11 | * http://elrinconde-ex.blogspot.com/2012/09/killa-my-scripting-language-based-in.html 12 | 13 | * http://elrinconde-ex.blogspot.com/2012/10/ruby-and-joy-of-programming.html 14 | 15 | * http://elrinconde-ex.blogspot.com/2012/11/bitwise-operators-in-c-javascript-lua.html 16 | 17 | * http://elrinconde-ex.blogspot.com/2013/07/killa-case-for-mandatory-braces.html 18 | 19 | 20 | ### Build on Windows 21 | 22 | For Windows use the provided Visual Studio 2019 Community solution. 23 | 24 | ### Build on Mac OSX 25 | 26 | Open the terminal and go to the directory where the Makefile is and type: 27 |
28 | make macosx
29 | make test
30 | 
31 | tested in Mac OSX 10.6.8 32 | 33 | ### Build on Linux 34 | 35 | From the terminal type: 36 |
37 | make linux
38 | make test
39 | 
40 | 41 | tested in Ubuntu 11 42 | In case of error due to a readline.h header, install the library: 43 |
44 | sudo apt-get install libreadline-dev
45 | 
46 | 47 | ### Try it 48 | 49 | For Windows users there are provided binaries and some example scripts to run. 50 | Go to the directory [bin/windows] and type in the console window: 51 |
52 | killa test.kia
53 | killac -l test.kia
54 | 
55 | 56 | 57 | -------------------------------------------------------------------------------- /bin/windows/killa.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/bin/windows/killa.exe -------------------------------------------------------------------------------- /bin/windows/killac.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/bin/windows/killac.exe -------------------------------------------------------------------------------- /bin/windows/killaext.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/bin/windows/killaext.exe -------------------------------------------------------------------------------- /bin/windows/test.bat: -------------------------------------------------------------------------------- 1 | killa.exe ../../test/test.kia > ../../test/test.txt 2 | start "" "../../test/test.txt" -------------------------------------------------------------------------------- /build/compare.pl: -------------------------------------------------------------------------------- 1 | ##============================================================================== 2 | ## Transform source code to compare with upstream 3 | ## Author: Laurens Rodriguez. 4 | ##------------------------------------------------------------------------------ 5 | use strict; 6 | use warnings; 7 | use diagnostics; 8 | 9 | use File::Basename; 10 | use File::Copy::Recursive qw( fcopy ); 11 | use File::Path qw( make_path rmtree ); 12 | 13 | my $LUA_PATH = '../lua/lua-5.2.0'; 14 | my $KILLA_PATH = '../src'; 15 | my $COMPARE_PATH = '../temp'; 16 | 17 | rmtree( $COMPARE_PATH ); 18 | make_path( "$COMPARE_PATH/killa" ); 19 | recurse( $KILLA_PATH, \&processKillaFile ); 20 | 21 | make_path( "$COMPARE_PATH/lua" ); 22 | recurse( $LUA_PATH, \&processLuaFile ); 23 | 24 | 25 | ##------------------------------------------------------------------------------ 26 | sub processKillaFile 27 | { 28 | my $origFile = $_[0]; 29 | my $destFile = basename( $_[0] ); 30 | $destFile =~ s/killa/lua/; 31 | $destFile =~ s/^k(\S+)\./l$1\./; 32 | $destFile = "$COMPARE_PATH/killa/$destFile"; 33 | print "$destFile\n"; 34 | 35 | open( my $file, '<', $origFile ) or die( "Can't open $origFile: $!" ); 36 | open( my $temp, '>', $destFile ) or die( "Can't create $destFile: $!" ); 37 | while ( my $line = <$file> ) 38 | { 39 | $line =~ s/killa/lua/g; 40 | $line =~ s/KILLA/LUA/g; 41 | print( $temp $line ); 42 | } 43 | close( $file ); 44 | close( $temp ); 45 | } 46 | 47 | ##------------------------------------------------------------------------------ 48 | sub processLuaFile 49 | { 50 | my $origFile = $_[0]; 51 | my $destFile = basename( $_[0] ); 52 | $destFile = "$COMPARE_PATH/lua/$destFile"; 53 | 54 | open( my $file, '<', $origFile ) or die( "Can't open $origFile: $!" ); 55 | open( my $temp, '>', $destFile ) or die( "Can't create $destFile: $!" ); 56 | while ( my $line = <$file> ) 57 | { 58 | print( $temp $line ); 59 | } 60 | close( $file ); 61 | close( $temp ); 62 | } 63 | 64 | ##------------------------------------------------------------------------------ 65 | sub recurse 66 | { 67 | my $path = shift; 68 | my $onFileCallback = shift; 69 | my $scaped = $path =~ /".+"/; 70 | 71 | die( "Path too short: $path" ) if ( length( $path ) < 4 ); 72 | if ( ( -e $path ) && ( ! -d $path ) ) 73 | { 74 | $onFileCallback->( $path ) if ( defined $onFileCallback ); 75 | return; 76 | } 77 | 78 | ## Append a trailing / if it's not there. 79 | $path .= '/' if ( $path !~ /\/$/ ); 80 | 81 | ## Loop through the files contained in the directory. 82 | for my $eachFile ( glob( $path . '*' ) ) 83 | { 84 | if ( -d $eachFile ) 85 | { 86 | $eachFile = "\"$eachFile\"" if ( $scaped ); 87 | 88 | ## If the file is a directory, continue recursive scan. 89 | recurse( $eachFile, $onFileCallback ); 90 | } 91 | else 92 | { 93 | $onFileCallback->( $eachFile ) if ( defined $onFileCallback ); 94 | } 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /build/msvc2019/killa.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.1082 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "killa", "killa.vcxproj", "{9F100C8D-465E-4860-B7DD-1E2E51FA98A9}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "killac", "killac.vcxproj", "{9F100C8D-465E-4860-B7DD-1E2E51FA9819}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "killaext", "killaext.vcxproj", "{9F100C8D-465E-4860-B7DD-1E2E51FA98A0}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Win32 = Debug|Win32 15 | Release|Win32 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A9}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A9}.Debug|Win32.Build.0 = Debug|Win32 20 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A9}.Release|Win32.ActiveCfg = Release|Win32 21 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A9}.Release|Win32.Build.0 = Release|Win32 22 | {9F100C8D-465E-4860-B7DD-1E2E51FA9819}.Debug|Win32.ActiveCfg = Debug|Win32 23 | {9F100C8D-465E-4860-B7DD-1E2E51FA9819}.Debug|Win32.Build.0 = Debug|Win32 24 | {9F100C8D-465E-4860-B7DD-1E2E51FA9819}.Release|Win32.ActiveCfg = Release|Win32 25 | {9F100C8D-465E-4860-B7DD-1E2E51FA9819}.Release|Win32.Build.0 = Release|Win32 26 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A0}.Debug|Win32.ActiveCfg = Debug|Win32 27 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A0}.Debug|Win32.Build.0 = Debug|Win32 28 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A0}.Release|Win32.ActiveCfg = Release|Win32 29 | {9F100C8D-465E-4860-B7DD-1E2E51FA98A0}.Release|Win32.Build.0 = Release|Win32 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {23FDDB2D-4689-40C5-96AA-0357FCA3919A} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /doc/Killa, un lenguaje de programacion basado en Lua y JavaScript.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/doc/Killa, un lenguaje de programacion basado en Lua y JavaScript.pdf -------------------------------------------------------------------------------- /ext/bc/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * config.h 3 | * number.c from GNU bc-1.06 exports some symbols without the bc_ prefix. 4 | * This header file fixes this without touching either number.c or number.h 5 | * (luckily, number.c already wants to include a config.h). 6 | * Clients of number.c should include config.h before number.h. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #define _zero_ bc_zero 13 | #define _one_ bc_one 14 | #define _two_ bc_two 15 | #define num2str bc_num2str 16 | #define mul_base_digits bc_mul_base_digits 17 | 18 | #define bc_rt_warn bc_error 19 | #define bc_rt_error bc_error 20 | #define bc_out_of_memory() bc_error(NULL) 21 | 22 | void bc_error(const char *mesg); 23 | -------------------------------------------------------------------------------- /ext/bc/lbc.h: -------------------------------------------------------------------------------- 1 | 2 | #include "klua.h" 3 | 4 | LUALIB_API int luaopen_bc(lua_State *L); 5 | -------------------------------------------------------------------------------- /ext/bc/number.h: -------------------------------------------------------------------------------- 1 | /* number.h: Arbitrary precision numbers header file. */ 2 | /* 3 | Copyright (C) 1991, 1992, 1993, 1994, 1997, 2000 Free Software Foundation, Inc. 4 | 5 | This program is free software; you can redistribute it and/or modify 6 | it under the terms of the GNU General Public License as published by 7 | the Free Software Foundation; either version 2 of the License , or 8 | (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | GNU General Public License for more details. 14 | 15 | You should have received a copy of the GNU General Public License 16 | along with this program; see the file COPYING. If not, write to: 17 | 18 | The Free Software Foundation, Inc. 19 | 59 Temple Place, Suite 330 20 | Boston, MA 02111-1307 USA. 21 | 22 | 23 | You may contact the author by: 24 | e-mail: philnelson@acm.org 25 | us-mail: Philip A. Nelson 26 | Computer Science Department, 9062 27 | Western Washington University 28 | Bellingham, WA 98226-9062 29 | 30 | *************************************************************************/ 31 | 32 | #ifndef _NUMBER_H_ 33 | #define _NUMBER_H_ 34 | 35 | typedef enum {PLUS, MINUS} sign; 36 | 37 | typedef struct bc_struct *bc_num; 38 | 39 | typedef struct bc_struct 40 | { 41 | sign n_sign; 42 | int n_len; /* The number of digits before the decimal point. */ 43 | int n_scale; /* The number of digits after the decimal point. */ 44 | int n_refs; /* The number of pointers to this number. */ 45 | bc_num n_next; /* Linked list for available list. */ 46 | char *n_ptr; /* The pointer to the actual storage. 47 | If NULL, n_value points to the inside of 48 | another number (bc_multiply...) and should 49 | not be "freed." */ 50 | char *n_value; /* The number. Not zero char terminated. 51 | May not point to the same place as n_ptr as 52 | in the case of leading zeros generated. */ 53 | } bc_struct; 54 | 55 | 56 | /* The base used in storing the numbers in n_value above. 57 | Currently this MUST be 10. */ 58 | 59 | #define BASE 10 60 | 61 | /* Some useful macros and constants. */ 62 | 63 | #define CH_VAL(c) (c - '0') 64 | #define BCD_CHAR(d) (d + '0') 65 | 66 | #ifdef MIN 67 | #undef MIN 68 | #undef MAX 69 | #endif 70 | #define MAX(a,b) ((a)>(b)?(a):(b)) 71 | #define MIN(a,b) ((a)>(b)?(b):(a)) 72 | #define ODD(a) ((a)&1) 73 | 74 | #ifndef TRUE 75 | #define TRUE 1 76 | #define FALSE 0 77 | #endif 78 | 79 | #ifndef LONG_MAX 80 | #define LONG_MAX 0x7ffffff 81 | #endif 82 | 83 | 84 | /* Global numbers. */ 85 | extern bc_num _zero_; 86 | extern bc_num _one_; 87 | extern bc_num _two_; 88 | 89 | 90 | /* Function Prototypes */ 91 | 92 | /* Define the _PROTOTYPE macro if it is needed. */ 93 | 94 | #ifndef _PROTOTYPE 95 | #ifdef __STDC__ 96 | #define _PROTOTYPE(func, args) func args 97 | #else 98 | #define _PROTOTYPE(func, args) func() 99 | #endif 100 | #endif 101 | 102 | _PROTOTYPE(void bc_init_numbers, (void)); 103 | 104 | _PROTOTYPE(bc_num bc_new_num, (int length, int scale)); 105 | 106 | _PROTOTYPE(void bc_free_num, (bc_num *num)); 107 | 108 | _PROTOTYPE(bc_num bc_copy_num, (bc_num num)); 109 | 110 | _PROTOTYPE(void bc_init_num, (bc_num *num)); 111 | 112 | _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale)); 113 | 114 | _PROTOTYPE(char *bc_num2str, (bc_num num)); 115 | 116 | _PROTOTYPE(void bc_int2num, (bc_num *num, int val)); 117 | 118 | _PROTOTYPE(long bc_num2long, (bc_num num)); 119 | 120 | _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2)); 121 | 122 | _PROTOTYPE(char bc_is_zero, (bc_num num)); 123 | 124 | _PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale)); 125 | 126 | _PROTOTYPE(char bc_is_neg, (bc_num num)); 127 | 128 | _PROTOTYPE(void bc_add, (bc_num n1, bc_num n2, bc_num *result, int scale_min)); 129 | 130 | _PROTOTYPE(void bc_sub, (bc_num n1, bc_num n2, bc_num *result, int scale_min)); 131 | 132 | _PROTOTYPE(void bc_multiply, (bc_num n1, bc_num n2, bc_num *prod, int scale)); 133 | 134 | _PROTOTYPE(int bc_divide, (bc_num n1, bc_num n2, bc_num *quot, int scale)); 135 | 136 | _PROTOTYPE(int bc_modulo, (bc_num num1, bc_num num2, bc_num *result, 137 | int scale)); 138 | 139 | _PROTOTYPE(int bc_divmod, (bc_num num1, bc_num num2, bc_num *quot, 140 | bc_num *rem, int scale)); 141 | 142 | _PROTOTYPE(int bc_raisemod, (bc_num base, bc_num expo, bc_num mod, 143 | bc_num *result, int scale)); 144 | 145 | _PROTOTYPE(void bc_raise, (bc_num num1, bc_num num2, bc_num *result, 146 | int scale)); 147 | 148 | _PROTOTYPE(int bc_sqrt, (bc_num *num, int scale)); 149 | 150 | _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int), 151 | int leading_zero)); 152 | 153 | #endif 154 | -------------------------------------------------------------------------------- /ide/editplus/killa.stx: -------------------------------------------------------------------------------- 1 | #TITLE=KILLA 2 | #DELIMITER=,(){}[]-+*%/="'~!&|<>?.:;#$ 3 | #QUOTATION1=' 4 | #QUOTATION2=" 5 | #LINECOMMENT=// 6 | #LINECOMMENT2= 7 | #COMMENTON=/* 8 | #COMMENTOFF=*/ 9 | #ESCAPE=\ 10 | #CASE=y 11 | #NUMBER_PATTERN=cpp 12 | 13 | #KEYWORD=Operators 14 | + 15 | - 16 | / 17 | * 18 | = 19 | % 20 | & 21 | | 22 | ! 23 | ( 24 | ) 25 | [ 26 | ] 27 | . 28 | , 29 | : 30 | ^; 31 | ^^ 32 | $ 33 | ^# 34 | { 35 | } 36 | ~ 37 | < 38 | > 39 | ; 40 | ? 41 | 42 | #KEYWORD=Reserved words 43 | as 44 | break 45 | case 46 | catch 47 | class 48 | const 49 | continue 50 | default 51 | delete 52 | do 53 | dynamic 54 | each 55 | else 56 | extends 57 | false 58 | final 59 | finally 60 | for 61 | function 62 | get 63 | goto 64 | if 65 | implements 66 | import 67 | in 68 | include 69 | instanceof 70 | interface 71 | internal 72 | is 73 | namespace 74 | native 75 | new 76 | null 77 | override 78 | package 79 | private 80 | protected 81 | public 82 | return 83 | static 84 | super 85 | switch 86 | this 87 | throw 88 | to 89 | true 90 | try 91 | undefined 92 | var 93 | void 94 | while 95 | with 96 | 97 | #KEYWORD=Library 98 | global 99 | arg 100 | assert 101 | dofile 102 | error 103 | ipairs 104 | loadfile 105 | pairs 106 | pcall 107 | print 108 | require 109 | setmetatable 110 | setmetatableerror 111 | type 112 | tonumber 113 | tostring 114 | xpcall 115 | 116 | open 117 | 118 | abs 119 | acos 120 | asin 121 | atan 122 | atan2 123 | ceil 124 | cos 125 | cosh 126 | deg 127 | exp 128 | floor 129 | fmod 130 | frexp 131 | huge 132 | ldexp 133 | log 134 | max 135 | min 136 | modf 137 | pi 138 | pow 139 | rad 140 | random 141 | randomseed 142 | sin 143 | sinh 144 | sqrt 145 | tan 146 | tanh 147 | 148 | time 149 | 150 | byte 151 | char 152 | find 153 | format 154 | gmatch 155 | gsub 156 | len 157 | lower 158 | match 159 | rep 160 | reverse 161 | sub 162 | upper 163 | 164 | concat 165 | insert 166 | sort 167 | remove 168 | pack 169 | unpack 170 | 171 | test 172 | extract 173 | replace 174 | lrotate 175 | rrotate 176 | 177 | #KEYWORD=Global modules 178 | Coroutine 179 | Table 180 | Io 181 | Os 182 | String 183 | Bit32 184 | Math 185 | Debug 186 | Package 187 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7 2009/11/27 15:37:59 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58 2011/08/30 16:26:41 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.3 2011/08/23 17:24:34 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL = lua_newthread(L); 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | lua_pushvalue(L, 1); /* move function to top */ 86 | lua_xmove(L, NL, 1); /* move function from L to NL */ 87 | return 1; 88 | } 89 | 90 | 91 | static int luaB_cowrap (lua_State *L) { 92 | luaB_cocreate(L); 93 | lua_pushcclosure(L, luaB_auxwrap, 1); 94 | return 1; 95 | } 96 | 97 | 98 | static int luaB_yield (lua_State *L) { 99 | return lua_yield(L, lua_gettop(L)); 100 | } 101 | 102 | 103 | static int luaB_costatus (lua_State *L) { 104 | lua_State *co = lua_tothread(L, 1); 105 | luaL_argcheck(L, co, 1, "coroutine expected"); 106 | if (L == co) lua_pushliteral(L, "running"); 107 | else { 108 | switch (lua_status(co)) { 109 | case LUA_YIELD: 110 | lua_pushliteral(L, "suspended"); 111 | break; 112 | case LUA_OK: { 113 | lua_Debug ar; 114 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 115 | lua_pushliteral(L, "normal"); /* it is running */ 116 | else if (lua_gettop(co) == 0) 117 | lua_pushliteral(L, "dead"); 118 | else 119 | lua_pushliteral(L, "suspended"); /* initial state */ 120 | break; 121 | } 122 | default: /* some error occurred */ 123 | lua_pushliteral(L, "dead"); 124 | break; 125 | } 126 | } 127 | return 1; 128 | } 129 | 130 | 131 | static int luaB_corunning (lua_State *L) { 132 | int ismain = lua_pushthread(L); 133 | lua_pushboolean(L, ismain); 134 | return 2; 135 | } 136 | 137 | 138 | static const luaL_Reg co_funcs[] = { 139 | {"create", luaB_cocreate}, 140 | {"resume", luaB_coresume}, 141 | {"running", luaB_corunning}, 142 | {"status", luaB_costatus}, 143 | {"wrap", luaB_cowrap}, 144 | {"yield", luaB_yield}, 145 | {NULL, NULL} 146 | }; 147 | 148 | 149 | 150 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 151 | luaL_newlib(L, co_funcs); 152 | return 1; 153 | } 154 | 155 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11 2011/10/03 16:19:23 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12 2011/07/15 12:50:29 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7 2011/10/07 20:45:19 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20 2011/11/29 15:55:08 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 1.19 2011/11/23 17:48:18 lhf Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttype(o),D); 88 | switch (ttype(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | } 102 | } 103 | n=f->sizep; 104 | DumpInt(n,D); 105 | for (i=0; ip[i],D); 106 | } 107 | 108 | static void DumpUpvalues(const Proto* f, DumpState* D) 109 | { 110 | int i,n=f->sizeupvalues; 111 | DumpInt(n,D); 112 | for (i=0; iupvalues[i].instack,D); 115 | DumpChar(f->upvalues[i].idx,D); 116 | } 117 | } 118 | 119 | static void DumpDebug(const Proto* f, DumpState* D) 120 | { 121 | int i,n; 122 | DumpString((D->strip) ? NULL : f->source,D); 123 | n= (D->strip) ? 0 : f->sizelineinfo; 124 | DumpVector(f->lineinfo,n,sizeof(int),D); 125 | n= (D->strip) ? 0 : f->sizelocvars; 126 | DumpInt(n,D); 127 | for (i=0; ilocvars[i].varname,D); 130 | DumpInt(f->locvars[i].startpc,D); 131 | DumpInt(f->locvars[i].endpc,D); 132 | } 133 | n= (D->strip) ? 0 : f->sizeupvalues; 134 | DumpInt(n,D); 135 | for (i=0; iupvalues[i].name,D); 136 | } 137 | 138 | static void DumpFunction(const Proto* f, DumpState* D) 139 | { 140 | DumpInt(f->linedefined,D); 141 | DumpInt(f->lastlinedefined,D); 142 | DumpChar(f->numparams,D); 143 | DumpChar(f->is_vararg,D); 144 | DumpChar(f->maxstacksize,D); 145 | DumpCode(f,D); 146 | DumpConstants(f,D); 147 | DumpUpvalues(f,D); 148 | DumpDebug(f,D); 149 | } 150 | 151 | static void DumpHeader(DumpState* D) 152 | { 153 | lu_byte h[LUAC_HEADERSIZE]; 154 | luaU_header(h); 155 | DumpBlock(h,LUAC_HEADERSIZE,D); 156 | } 157 | 158 | /* 159 | ** dump Lua function as precompiled chunk 160 | */ 161 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 162 | { 163 | DumpState D; 164 | D.L=L; 165 | D.writer=w; 166 | D.data=data; 167 | D.strip=strip; 168 | D.status=0; 169 | DumpHeader(&D); 170 | DumpFunction(f,&D); 171 | return D.status; 172 | } 173 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.6 2010/06/04 13:06:15 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, Proto *p); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeclosure (lua_State *L, Closure *c); 29 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 30 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 31 | int pc); 32 | 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.83 2011/11/30 12:42:49 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | #if defined(TRACEMEM) 98 | { /* auxiliary patch to monitor garbage collection. 99 | ** To plot, gnuplot with following command: 100 | ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines 101 | */ 102 | static unsigned long total = 0; /* our "time" */ 103 | static FILE *f = NULL; /* output file */ 104 | total++; /* "time" always grows */ 105 | if ((total % 200) == 0) { 106 | if (f == NULL) f = fopen(TRACEMEM, "w"); 107 | fprintf(f, "%lu %u %d %d\n", total, 108 | gettotalbytes(g), g->GCdebt, g->gcstate * 10000); 109 | } 110 | } 111 | #endif 112 | 113 | return newblock; 114 | } 115 | 116 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.38 2011/12/02 13:26:54 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | #define luaM_reallocv(L,b,on,n,e) \ 18 | ((cast(size_t, (n)+1) > MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 19 | (luaM_toobig(L), (void *)0) : \ 20 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 21 | 22 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 23 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 24 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 25 | 26 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 27 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 28 | #define luaM_newvector(L,n,t) \ 29 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 30 | 31 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 41 | 42 | /* not to be called directly */ 43 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 44 | size_t size); 45 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 46 | size_t size_elem, int limit, 47 | const char *what); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.48 2011/04/19 16:22:13 roberto Exp $ 3 | ** See Copyright Notice in lua.h 4 | */ 5 | 6 | 7 | #define lopcodes_c 8 | #define LUA_CORE 9 | 10 | 11 | #include "lopcodes.h" 12 | 13 | 14 | /* ORDER OP */ 15 | 16 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 17 | "MOVE", 18 | "LOADK", 19 | "LOADKX", 20 | "LOADBOOL", 21 | "LOADNIL", 22 | "GETUPVAL", 23 | "GETTABUP", 24 | "GETTABLE", 25 | "SETTABUP", 26 | "SETUPVAL", 27 | "SETTABLE", 28 | "NEWTABLE", 29 | "SELF", 30 | "ADD", 31 | "SUB", 32 | "MUL", 33 | "DIV", 34 | "MOD", 35 | "POW", 36 | "UNM", 37 | "NOT", 38 | "LEN", 39 | "CONCAT", 40 | "JMP", 41 | "EQ", 42 | "LT", 43 | "LE", 44 | "TEST", 45 | "TESTSET", 46 | "CALL", 47 | "TAILCALL", 48 | "RETURN", 49 | "FORLOOP", 50 | "FORPREP", 51 | "TFORCALL", 52 | "TFORLOOP", 53 | "SETLIST", 54 | "CLOSURE", 55 | "VARARG", 56 | "EXTRAARG", 57 | NULL 58 | }; 59 | 60 | 61 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 62 | 63 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 64 | /* T A B C mode opcode */ 65 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 66 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 67 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 68 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 69 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 71 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 72 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 73 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 74 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 75 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 76 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 77 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 78 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 84 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 87 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 88 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 89 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 92 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 93 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 94 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 96 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 97 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 99 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 100 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 101 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 102 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 104 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 105 | }; 106 | 107 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.69 2011/07/27 18:09:01 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 roberto Exp $ 3 | ** String table (keeps all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lstring_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lmem.h" 16 | #include "lobject.h" 17 | #include "lstate.h" 18 | #include "lstring.h" 19 | 20 | 21 | 22 | void luaS_resize (lua_State *L, int newsize) { 23 | int i; 24 | stringtable *tb = &G(L)->strt; 25 | /* cannot resize while GC is traversing strings */ 26 | luaC_runtilstate(L, ~bitmask(GCSsweepstring)); 27 | if (newsize > tb->size) { 28 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 29 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 30 | } 31 | /* rehash */ 32 | for (i=0; isize; i++) { 33 | GCObject *p = tb->hash[i]; 34 | tb->hash[i] = NULL; 35 | while (p) { /* for each node in the list */ 36 | GCObject *next = gch(p)->next; /* save next */ 37 | unsigned int h = lmod(gco2ts(p)->hash, newsize); /* new position */ 38 | gch(p)->next = tb->hash[h]; /* chain it */ 39 | tb->hash[h] = p; 40 | resetoldbit(p); /* see MOVE OLD rule */ 41 | p = next; 42 | } 43 | } 44 | if (newsize < tb->size) { 45 | /* shrinking slice must be empty */ 46 | lua_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 47 | luaM_reallocvector(L, tb->hash, tb->size, newsize, GCObject *); 48 | } 49 | tb->size = newsize; 50 | } 51 | 52 | 53 | static TString *newlstr (lua_State *L, const char *str, size_t l, 54 | unsigned int h) { 55 | size_t totalsize; /* total size of TString object */ 56 | GCObject **list; /* (pointer to) list where it will be inserted */ 57 | TString *ts; 58 | stringtable *tb = &G(L)->strt; 59 | if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 60 | luaM_toobig(L); 61 | if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 62 | luaS_resize(L, tb->size*2); /* too crowded */ 63 | totalsize = sizeof(TString) + ((l + 1) * sizeof(char)); 64 | list = &tb->hash[lmod(h, tb->size)]; 65 | ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts; 66 | ts->tsv.len = l; 67 | ts->tsv.hash = h; 68 | ts->tsv.reserved = 0; 69 | memcpy(ts+1, str, l*sizeof(char)); 70 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 71 | tb->nuse++; 72 | return ts; 73 | } 74 | 75 | 76 | TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 77 | GCObject *o; 78 | unsigned int h = cast(unsigned int, l); /* seed */ 79 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 80 | size_t l1; 81 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 82 | h = h ^ ((h<<5)+(h>>2)+cast(unsigned char, str[l1-1])); 83 | for (o = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 84 | o != NULL; 85 | o = gch(o)->next) { 86 | TString *ts = rawgco2ts(o); 87 | if (h == ts->tsv.hash && 88 | ts->tsv.len == l && 89 | (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) { 90 | if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */ 91 | changewhite(o); /* resurrect it */ 92 | return ts; 93 | } 94 | } 95 | return newlstr(L, str, l, h); /* not found; create a new string */ 96 | } 97 | 98 | 99 | TString *luaS_new (lua_State *L, const char *str) { 100 | return luaS_newlstr(L, str, strlen(str)); 101 | } 102 | 103 | 104 | Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { 105 | Udata *u; 106 | if (s > MAX_SIZET - sizeof(Udata)) 107 | luaM_toobig(L); 108 | u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; 109 | u->uv.len = s; 110 | u->uv.metatable = NULL; 111 | u->uv.env = e; 112 | return u; 113 | } 114 | 115 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.46 2010/04/05 16:26:37 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** as all string are internalized, string equality becomes 27 | ** pointer equality 28 | */ 29 | #define eqstr(a,b) ((a) == (b)) 30 | 31 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 32 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 33 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 34 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 35 | 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16 2011/08/17 20:26:47 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 22 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 25 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L); 28 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 29 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 30 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 31 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 32 | LUAI_FUNC int luaH_getn (Table *t); 33 | 34 | 35 | #if defined(LUA_DEBUG) 36 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 37 | LUAI_FUNC int luaH_isdummy (Node *n); 38 | #endif 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43 2011/12/08 12:11:37 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(t1,t2) \ 23 | (ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2)) 24 | 25 | 26 | /* not to called directly */ 27 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 28 | 29 | 30 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 32 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 33 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 34 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 35 | StkId val); 36 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 37 | StkId val); 38 | LUAI_FUNC void luaV_finishOp (lua_State *L); 39 | LUAI_FUNC void luaV_execute (lua_State *L); 40 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 41 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 42 | const TValue *rc, TMS op); 43 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.34 2011/07/15 12:35:32 roberto Exp $ 3 | ** a generic input stream interface 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /lua/lua-5.2.0/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26 2011/07/15 12:48:03 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lapi.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lapi.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Lua API 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lapi_h 8 | #define lapi_h 9 | 10 | 11 | #include "llimits.h" 12 | #include "lstate.h" 13 | 14 | #define api_incr_top(L) {L->top++; api_check(L, L->top <= L->ci->top, \ 15 | "stack overflow");} 16 | 17 | #define adjustresults(L,nres) \ 18 | { if ((nres) == LUA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 19 | 20 | #define api_checknelems(L,n) api_check(L, (n) < (L->top - L->ci->func), \ 21 | "not enough elements in the stack") 22 | 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcode.h,v 1.58.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Code generator for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lcode_h 8 | #define lcode_h 9 | 10 | #include "llex.h" 11 | #include "lobject.h" 12 | #include "lopcodes.h" 13 | #include "lparser.h" 14 | 15 | 16 | /* 17 | ** Marks the end of a patch list. It is an invalid value both as an absolute 18 | ** address, and as a list link (would link an element to itself). 19 | */ 20 | #define NO_JUMP (-1) 21 | 22 | 23 | /* 24 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 25 | */ 26 | typedef enum BinOpr { 27 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 28 | OPR_CONCAT, 29 | OPR_EQ, OPR_LT, OPR_LE, 30 | OPR_NE, OPR_GT, OPR_GE, 31 | OPR_AND, OPR_OR, 32 | OPR_NOBINOPR 33 | } BinOpr; 34 | 35 | 36 | typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; 37 | 38 | 39 | #define getcode(fs,e) ((fs)->f->code[(e)->u.info]) 40 | 41 | #define luaK_codeAsBx(fs,o,A,sBx) luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx) 42 | 43 | #define luaK_setmultret(fs,e) luaK_setreturns(fs, e, LUA_MULTRET) 44 | 45 | #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t) 46 | 47 | LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx); 48 | LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C); 49 | LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k); 50 | LUAI_FUNC void luaK_fixline (FuncState *fs, int line); 51 | LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n); 52 | LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n); 53 | LUAI_FUNC void luaK_checkstack (FuncState *fs, int n); 54 | LUAI_FUNC int luaK_stringK (FuncState *fs, TString *s); 55 | LUAI_FUNC int luaK_numberK (FuncState *fs, lua_Number r); 56 | LUAI_FUNC void luaK_dischargevars (FuncState *fs, expdesc *e); 57 | LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e); 58 | LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e); 59 | LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e); 60 | LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e); 61 | LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e); 62 | LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key); 63 | LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k); 64 | LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e); 65 | LUAI_FUNC void luaK_goiffalse (FuncState *fs, expdesc *e); 66 | LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e); 67 | LUAI_FUNC void luaK_setreturns (FuncState *fs, expdesc *e, int nresults); 68 | LUAI_FUNC void luaK_setoneret (FuncState *fs, expdesc *e); 69 | LUAI_FUNC int luaK_jump (FuncState *fs); 70 | LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret); 71 | LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target); 72 | LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); 73 | LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level); 74 | LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); 75 | LUAI_FUNC int luaK_getlabel (FuncState *fs); 76 | LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line); 77 | LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); 78 | LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, 79 | expdesc *v2, int line); 80 | LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lcorolib.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lcorolib.c,v 1.5.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Coroutine Library 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | 11 | #define lcorolib_c 12 | #define LUA_LIB 13 | 14 | #include "lua.h" 15 | 16 | #include "lauxlib.h" 17 | #include "lualib.h" 18 | 19 | 20 | static int auxresume (lua_State *L, lua_State *co, int narg) { 21 | int status; 22 | if (!lua_checkstack(co, narg)) { 23 | lua_pushliteral(L, "too many arguments to resume"); 24 | return -1; /* error flag */ 25 | } 26 | if (lua_status(co) == LUA_OK && lua_gettop(co) == 0) { 27 | lua_pushliteral(L, "cannot resume dead coroutine"); 28 | return -1; /* error flag */ 29 | } 30 | lua_xmove(L, co, narg); 31 | status = lua_resume(co, L, narg); 32 | if (status == LUA_OK || status == LUA_YIELD) { 33 | int nres = lua_gettop(co); 34 | if (!lua_checkstack(L, nres + 1)) { 35 | lua_pop(co, nres); /* remove results anyway */ 36 | lua_pushliteral(L, "too many results to resume"); 37 | return -1; /* error flag */ 38 | } 39 | lua_xmove(co, L, nres); /* move yielded values */ 40 | return nres; 41 | } 42 | else { 43 | lua_xmove(co, L, 1); /* move error message */ 44 | return -1; /* error flag */ 45 | } 46 | } 47 | 48 | 49 | static int luaB_coresume (lua_State *L) { 50 | lua_State *co = lua_tothread(L, 1); 51 | int r; 52 | luaL_argcheck(L, co, 1, "coroutine expected"); 53 | r = auxresume(L, co, lua_gettop(L) - 1); 54 | if (r < 0) { 55 | lua_pushboolean(L, 0); 56 | lua_insert(L, -2); 57 | return 2; /* return false + error message */ 58 | } 59 | else { 60 | lua_pushboolean(L, 1); 61 | lua_insert(L, -(r + 1)); 62 | return r + 1; /* return true + `resume' returns */ 63 | } 64 | } 65 | 66 | 67 | static int luaB_auxwrap (lua_State *L) { 68 | lua_State *co = lua_tothread(L, lua_upvalueindex(1)); 69 | int r = auxresume(L, co, lua_gettop(L)); 70 | if (r < 0) { 71 | if (lua_isstring(L, -1)) { /* error object is a string? */ 72 | luaL_where(L, 1); /* add extra info */ 73 | lua_insert(L, -2); 74 | lua_concat(L, 2); 75 | } 76 | return lua_error(L); /* propagate error */ 77 | } 78 | return r; 79 | } 80 | 81 | 82 | static int luaB_cocreate (lua_State *L) { 83 | lua_State *NL; 84 | luaL_checktype(L, 1, LUA_TFUNCTION); 85 | NL = lua_newthread(L); 86 | lua_pushvalue(L, 1); /* move function to top */ 87 | lua_xmove(L, NL, 1); /* move function from L to NL */ 88 | return 1; 89 | } 90 | 91 | 92 | static int luaB_cowrap (lua_State *L) { 93 | luaB_cocreate(L); 94 | lua_pushcclosure(L, luaB_auxwrap, 1); 95 | return 1; 96 | } 97 | 98 | 99 | static int luaB_yield (lua_State *L) { 100 | return lua_yield(L, lua_gettop(L)); 101 | } 102 | 103 | 104 | static int luaB_costatus (lua_State *L) { 105 | lua_State *co = lua_tothread(L, 1); 106 | luaL_argcheck(L, co, 1, "coroutine expected"); 107 | if (L == co) lua_pushliteral(L, "running"); 108 | else { 109 | switch (lua_status(co)) { 110 | case LUA_YIELD: 111 | lua_pushliteral(L, "suspended"); 112 | break; 113 | case LUA_OK: { 114 | lua_Debug ar; 115 | if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */ 116 | lua_pushliteral(L, "normal"); /* it is running */ 117 | else if (lua_gettop(co) == 0) 118 | lua_pushliteral(L, "dead"); 119 | else 120 | lua_pushliteral(L, "suspended"); /* initial state */ 121 | break; 122 | } 123 | default: /* some error occurred */ 124 | lua_pushliteral(L, "dead"); 125 | break; 126 | } 127 | } 128 | return 1; 129 | } 130 | 131 | 132 | static int luaB_corunning (lua_State *L) { 133 | int ismain = lua_pushthread(L); 134 | lua_pushboolean(L, ismain); 135 | return 2; 136 | } 137 | 138 | 139 | static const luaL_Reg co_funcs[] = { 140 | {"create", luaB_cocreate}, 141 | {"resume", luaB_coresume}, 142 | {"running", luaB_corunning}, 143 | {"status", luaB_costatus}, 144 | {"wrap", luaB_cowrap}, 145 | {"yield", luaB_yield}, 146 | {NULL, NULL} 147 | }; 148 | 149 | 150 | 151 | LUAMOD_API int luaopen_coroutine (lua_State *L) { 152 | luaL_newlib(L, co_funcs); 153 | return 1; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lctype.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.c,v 1.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #define lctype_c 8 | #define LUA_CORE 9 | 10 | #include "lctype.h" 11 | 12 | #if !LUA_USE_CTYPE /* { */ 13 | 14 | #include 15 | 16 | LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = { 17 | 0x00, /* EOZ */ 18 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 19 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 20 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 22 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 23 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 24 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 25 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 26 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 27 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 28 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 29 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 30 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 32 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 33 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 50 | }; 51 | 52 | #endif /* } */ 53 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lctype.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lctype.h,v 1.12.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** 'ctype' functions for Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lctype_h 8 | #define lctype_h 9 | 10 | #include "lua.h" 11 | 12 | 13 | /* 14 | ** WARNING: the functions defined here do not necessarily correspond 15 | ** to the similar functions in the standard C ctype.h. They are 16 | ** optimized for the specific needs of Lua 17 | */ 18 | 19 | #if !defined(LUA_USE_CTYPE) 20 | 21 | #if 'A' == 65 && '0' == 48 22 | /* ASCII case: can use its own tables; faster and fixed */ 23 | #define LUA_USE_CTYPE 0 24 | #else 25 | /* must use standard C ctype */ 26 | #define LUA_USE_CTYPE 1 27 | #endif 28 | 29 | #endif 30 | 31 | 32 | #if !LUA_USE_CTYPE /* { */ 33 | 34 | #include 35 | 36 | #include "llimits.h" 37 | 38 | 39 | #define ALPHABIT 0 40 | #define DIGITBIT 1 41 | #define PRINTBIT 2 42 | #define SPACEBIT 3 43 | #define XDIGITBIT 4 44 | 45 | 46 | #define MASK(B) (1 << (B)) 47 | 48 | 49 | /* 50 | ** add 1 to char to allow index -1 (EOZ) 51 | */ 52 | #define testprop(c,p) (luai_ctype_[(c)+1] & (p)) 53 | 54 | /* 55 | ** 'lalpha' (Lua alphabetic) and 'lalnum' (Lua alphanumeric) both include '_' 56 | */ 57 | #define lislalpha(c) testprop(c, MASK(ALPHABIT)) 58 | #define lislalnum(c) testprop(c, (MASK(ALPHABIT) | MASK(DIGITBIT))) 59 | #define lisdigit(c) testprop(c, MASK(DIGITBIT)) 60 | #define lisspace(c) testprop(c, MASK(SPACEBIT)) 61 | #define lisprint(c) testprop(c, MASK(PRINTBIT)) 62 | #define lisxdigit(c) testprop(c, MASK(XDIGITBIT)) 63 | 64 | /* 65 | ** this 'ltolower' only works for alphabetic characters 66 | */ 67 | #define ltolower(c) ((c) | ('A' ^ 'a')) 68 | 69 | 70 | /* two more entries for 0 and -1 (EOZ) */ 71 | LUAI_DDEC const lu_byte luai_ctype_[UCHAR_MAX + 2]; 72 | 73 | 74 | #else /* }{ */ 75 | 76 | /* 77 | ** use standard C ctypes 78 | */ 79 | 80 | #include 81 | 82 | 83 | #define lislalpha(c) (isalpha(c) || (c) == '_') 84 | #define lislalnum(c) (isalnum(c) || (c) == '_') 85 | #define lisdigit(c) (isdigit(c)) 86 | #define lisspace(c) (isspace(c)) 87 | #define lisprint(c) (isprint(c)) 88 | #define lisxdigit(c) (isxdigit(c)) 89 | 90 | #define ltolower(c) (tolower(c)) 91 | 92 | #endif /* } */ 93 | 94 | #endif 95 | 96 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.7.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | /* Active Lua function (given call info) */ 21 | #define ci_func(ci) (clLvalue((ci)->func)) 22 | 23 | 24 | LUAI_FUNC l_noret luaG_typeerror (lua_State *L, const TValue *o, 25 | const char *opname); 26 | LUAI_FUNC l_noret luaG_concaterror (lua_State *L, StkId p1, StkId p2); 27 | LUAI_FUNC l_noret luaG_aritherror (lua_State *L, const TValue *p1, 28 | const TValue *p2); 29 | LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, 30 | const TValue *p2); 31 | LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); 32 | LUAI_FUNC l_noret luaG_errormsg (lua_State *L); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.20.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 17 | luaD_growstack(L, n); else condmovestack(L); 18 | 19 | 20 | #define incr_top(L) {L->top++; luaD_checkstack(L,0);} 21 | 22 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 23 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 24 | 25 | 26 | /* type of protected functions, to be ran by `runprotected' */ 27 | typedef void (*Pfunc) (lua_State *L, void *ud); 28 | 29 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, 30 | const char *mode); 31 | LUAI_FUNC void luaD_hook (lua_State *L, int event, int line); 32 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 33 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults, 34 | int allowyield); 35 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 36 | ptrdiff_t oldtop, ptrdiff_t ef); 37 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 38 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 39 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 40 | LUAI_FUNC void luaD_shrinkstack (lua_State *L); 41 | 42 | LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode); 43 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ldump.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** save precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #include 8 | 9 | #define ldump_c 10 | #define LUA_CORE 11 | 12 | #include "lua.h" 13 | 14 | #include "lobject.h" 15 | #include "lstate.h" 16 | #include "lundump.h" 17 | 18 | typedef struct { 19 | lua_State* L; 20 | lua_Writer writer; 21 | void* data; 22 | int strip; 23 | int status; 24 | } DumpState; 25 | 26 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 27 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 28 | 29 | static void DumpBlock(const void* b, size_t size, DumpState* D) 30 | { 31 | if (D->status==0) 32 | { 33 | lua_unlock(D->L); 34 | D->status=(*D->writer)(D->L,b,size,D->data); 35 | lua_lock(D->L); 36 | } 37 | } 38 | 39 | static void DumpChar(int y, DumpState* D) 40 | { 41 | char x=(char)y; 42 | DumpVar(x,D); 43 | } 44 | 45 | static void DumpInt(int x, DumpState* D) 46 | { 47 | DumpVar(x,D); 48 | } 49 | 50 | static void DumpNumber(lua_Number x, DumpState* D) 51 | { 52 | DumpVar(x,D); 53 | } 54 | 55 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 56 | { 57 | DumpInt(n,D); 58 | DumpMem(b,n,size,D); 59 | } 60 | 61 | static void DumpString(const TString* s, DumpState* D) 62 | { 63 | if (s==NULL) 64 | { 65 | size_t size=0; 66 | DumpVar(size,D); 67 | } 68 | else 69 | { 70 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 71 | DumpVar(size,D); 72 | DumpBlock(getstr(s),size*sizeof(char),D); 73 | } 74 | } 75 | 76 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D) 77 | 78 | static void DumpFunction(const Proto* f, DumpState* D); 79 | 80 | static void DumpConstants(const Proto* f, DumpState* D) 81 | { 82 | int i,n=f->sizek; 83 | DumpInt(n,D); 84 | for (i=0; ik[i]; 87 | DumpChar(ttypenv(o),D); 88 | switch (ttypenv(o)) 89 | { 90 | case LUA_TNIL: 91 | break; 92 | case LUA_TBOOLEAN: 93 | DumpChar(bvalue(o),D); 94 | break; 95 | case LUA_TNUMBER: 96 | DumpNumber(nvalue(o),D); 97 | break; 98 | case LUA_TSTRING: 99 | DumpString(rawtsvalue(o),D); 100 | break; 101 | default: lua_assert(0); 102 | } 103 | } 104 | n=f->sizep; 105 | DumpInt(n,D); 106 | for (i=0; ip[i],D); 107 | } 108 | 109 | static void DumpUpvalues(const Proto* f, DumpState* D) 110 | { 111 | int i,n=f->sizeupvalues; 112 | DumpInt(n,D); 113 | for (i=0; iupvalues[i].instack,D); 116 | DumpChar(f->upvalues[i].idx,D); 117 | } 118 | } 119 | 120 | static void DumpDebug(const Proto* f, DumpState* D) 121 | { 122 | int i,n; 123 | DumpString((D->strip) ? NULL : f->source,D); 124 | n= (D->strip) ? 0 : f->sizelineinfo; 125 | DumpVector(f->lineinfo,n,sizeof(int),D); 126 | n= (D->strip) ? 0 : f->sizelocvars; 127 | DumpInt(n,D); 128 | for (i=0; ilocvars[i].varname,D); 131 | DumpInt(f->locvars[i].startpc,D); 132 | DumpInt(f->locvars[i].endpc,D); 133 | } 134 | n= (D->strip) ? 0 : f->sizeupvalues; 135 | DumpInt(n,D); 136 | for (i=0; iupvalues[i].name,D); 137 | } 138 | 139 | static void DumpFunction(const Proto* f, DumpState* D) 140 | { 141 | DumpInt(f->linedefined,D); 142 | DumpInt(f->lastlinedefined,D); 143 | DumpChar(f->numparams,D); 144 | DumpChar(f->is_vararg,D); 145 | DumpChar(f->maxstacksize,D); 146 | DumpCode(f,D); 147 | DumpConstants(f,D); 148 | DumpUpvalues(f,D); 149 | DumpDebug(f,D); 150 | } 151 | 152 | static void DumpHeader(DumpState* D) 153 | { 154 | lu_byte h[LUAC_HEADERSIZE]; 155 | luaU_header(h); 156 | DumpBlock(h,LUAC_HEADERSIZE,D); 157 | } 158 | 159 | /* 160 | ** dump Lua function as precompiled chunk 161 | */ 162 | int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) 163 | { 164 | DumpState D; 165 | D.L=L; 166 | D.writer=w; 167 | D.data=data; 168 | D.strip=strip; 169 | D.status=0; 170 | DumpHeader(&D); 171 | DumpFunction(f,&D); 172 | return D.status; 173 | } 174 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lfunc.h,v 2.8.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Auxiliary functions to manipulate prototypes and closures 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lfunc_h 8 | #define lfunc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | #define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ 15 | cast(int, sizeof(TValue)*((n)-1))) 16 | 17 | #define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ 18 | cast(int, sizeof(TValue *)*((n)-1))) 19 | 20 | 21 | LUAI_FUNC Proto *luaF_newproto (lua_State *L); 22 | LUAI_FUNC Closure *luaF_newCclosure (lua_State *L, int nelems); 23 | LUAI_FUNC Closure *luaF_newLclosure (lua_State *L, int nelems); 24 | LUAI_FUNC UpVal *luaF_newupval (lua_State *L); 25 | LUAI_FUNC UpVal *luaF_findupval (lua_State *L, StkId level); 26 | LUAI_FUNC void luaF_close (lua_State *L, StkId level); 27 | LUAI_FUNC void luaF_freeproto (lua_State *L, Proto *f); 28 | LUAI_FUNC void luaF_freeupval (lua_State *L, UpVal *uv); 29 | LUAI_FUNC const char *luaF_getlocalname (const Proto *func, int local_number, 30 | int pc); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/linit.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: linit.c,v 1.32.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Initialization of libraries for lua.c and other clients 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | /* 9 | ** If you embed Lua in your program and need to open the standard 10 | ** libraries, call luaL_openlibs in your program. If you need a 11 | ** different set of libraries, copy this file to your project and edit 12 | ** it to suit your needs. 13 | */ 14 | 15 | 16 | #define linit_c 17 | #define LUA_LIB 18 | 19 | #include "lua.h" 20 | 21 | #include "lualib.h" 22 | #include "lauxlib.h" 23 | 24 | 25 | /* 26 | ** these libs are loaded by lua.c and are readily available to any Lua 27 | ** program 28 | */ 29 | static const luaL_Reg loadedlibs[] = { 30 | {"_G", luaopen_base}, 31 | {LUA_LOADLIBNAME, luaopen_package}, 32 | {LUA_COLIBNAME, luaopen_coroutine}, 33 | {LUA_TABLIBNAME, luaopen_table}, 34 | {LUA_IOLIBNAME, luaopen_io}, 35 | {LUA_OSLIBNAME, luaopen_os}, 36 | {LUA_STRLIBNAME, luaopen_string}, 37 | {LUA_BITLIBNAME, luaopen_bit32}, 38 | {LUA_MATHLIBNAME, luaopen_math}, 39 | {LUA_DBLIBNAME, luaopen_debug}, 40 | {NULL, NULL} 41 | }; 42 | 43 | 44 | /* 45 | ** these libs are preloaded and must be required before used 46 | */ 47 | static const luaL_Reg preloadedlibs[] = { 48 | {NULL, NULL} 49 | }; 50 | 51 | 52 | LUALIB_API void luaL_openlibs (lua_State *L) { 53 | const luaL_Reg *lib; 54 | /* call open functions from 'loadedlibs' and set results to global table */ 55 | for (lib = loadedlibs; lib->func; lib++) { 56 | luaL_requiref(L, lib->name, lib->func, 1); 57 | lua_pop(L, 1); /* remove lib */ 58 | } 59 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 60 | luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD"); 61 | for (lib = preloadedlibs; lib->func; lib++) { 62 | lua_pushcfunction(L, lib->func); 63 | lua_setfield(L, -2, lib->name); 64 | } 65 | lua_pop(L, 1); /* remove _PRELOAD table */ 66 | } 67 | 68 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/llex.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llex.h,v 1.72.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lexical Analyzer 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llex_h 8 | #define llex_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | 14 | #define FIRST_RESERVED 257 15 | 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER RESERVED" 21 | */ 22 | enum RESERVED { 23 | /* terminal symbols denoted by reserved words */ 24 | TK_AND = FIRST_RESERVED, TK_BREAK, 25 | TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION, 26 | TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT, 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, 28 | /* other terminal symbols */ 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, 30 | TK_NUMBER, TK_NAME, TK_STRING 31 | }; 32 | 33 | /* number of reserved words */ 34 | #define NUM_RESERVED (cast(int, TK_WHILE-FIRST_RESERVED+1)) 35 | 36 | 37 | typedef union { 38 | lua_Number r; 39 | TString *ts; 40 | } SemInfo; /* semantics information */ 41 | 42 | 43 | typedef struct Token { 44 | int token; 45 | SemInfo seminfo; 46 | } Token; 47 | 48 | 49 | /* state of the lexer plus state of the parser when shared by all 50 | functions */ 51 | typedef struct LexState { 52 | int current; /* current character (charint) */ 53 | int linenumber; /* input line counter */ 54 | int lastline; /* line of last token `consumed' */ 55 | Token t; /* current token */ 56 | Token lookahead; /* look ahead token */ 57 | struct FuncState *fs; /* current function (parser) */ 58 | struct lua_State *L; 59 | ZIO *z; /* input stream */ 60 | Mbuffer *buff; /* buffer for tokens */ 61 | struct Dyndata *dyd; /* dynamic structures used by the parser */ 62 | TString *source; /* current source name */ 63 | TString *envn; /* environment variable name */ 64 | char decpoint; /* locale decimal point */ 65 | } LexState; 66 | 67 | 68 | LUAI_FUNC void luaX_init (lua_State *L); 69 | LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, 70 | TString *source, int firstchar); 71 | LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); 72 | LUAI_FUNC void luaX_next (LexState *ls); 73 | LUAI_FUNC int luaX_lookahead (LexState *ls); 74 | LUAI_FUNC l_noret luaX_syntaxerror (LexState *ls, const char *s); 75 | LUAI_FUNC const char *luaX_token2str (LexState *ls, int token); 76 | 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lmem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.c,v 1.84.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lmem_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "ldebug.h" 16 | #include "ldo.h" 17 | #include "lgc.h" 18 | #include "lmem.h" 19 | #include "lobject.h" 20 | #include "lstate.h" 21 | 22 | 23 | 24 | /* 25 | ** About the realloc function: 26 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 27 | ** (`osize' is the old size, `nsize' is the new size) 28 | ** 29 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 30 | ** matter 'x'). 31 | ** 32 | ** * frealloc(ud, p, x, 0) frees the block `p' 33 | ** (in this specific case, frealloc must return NULL); 34 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 35 | ** (which is equivalent to free(NULL) in ANSI C) 36 | ** 37 | ** frealloc returns NULL if it cannot create or reallocate the area 38 | ** (any reallocation to an equal or smaller size cannot fail!) 39 | */ 40 | 41 | 42 | 43 | #define MINSIZEARRAY 4 44 | 45 | 46 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elems, 47 | int limit, const char *what) { 48 | void *newblock; 49 | int newsize; 50 | if (*size >= limit/2) { /* cannot double it? */ 51 | if (*size >= limit) /* cannot grow even a little? */ 52 | luaG_runerror(L, "too many %s (limit is %d)", what, limit); 53 | newsize = limit; /* still have at least one free place */ 54 | } 55 | else { 56 | newsize = (*size)*2; 57 | if (newsize < MINSIZEARRAY) 58 | newsize = MINSIZEARRAY; /* minimum size */ 59 | } 60 | newblock = luaM_reallocv(L, block, *size, newsize, size_elems); 61 | *size = newsize; /* update only when everything else is OK */ 62 | return newblock; 63 | } 64 | 65 | 66 | l_noret luaM_toobig (lua_State *L) { 67 | luaG_runerror(L, "memory allocation error: block too big"); 68 | } 69 | 70 | 71 | 72 | /* 73 | ** generic allocation routine. 74 | */ 75 | void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76 | void *newblock; 77 | global_State *g = G(L); 78 | size_t realosize = (block) ? osize : 0; 79 | lua_assert((realosize == 0) == (block == NULL)); 80 | #if defined(HARDMEMTESTS) 81 | if (nsize > realosize && g->gcrunning) 82 | luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 | #endif 84 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 | if (newblock == NULL && nsize > 0) { 86 | api_check(L, nsize > realosize, 87 | "realloc cannot fail when shrinking a block"); 88 | if (g->gcrunning) { 89 | luaC_fullgc(L, 1); /* try to free some memory... */ 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 91 | } 92 | if (newblock == NULL) 93 | luaD_throw(L, LUA_ERRMEM); 94 | } 95 | lua_assert((nsize == 0) == (newblock == NULL)); 96 | g->GCdebt = (g->GCdebt + nsize) - realosize; 97 | return newblock; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | 17 | /* 18 | ** This macro avoids the runtime division MAX_SIZET/(e), as 'e' is 19 | ** always constant. 20 | ** The macro is somewhat complex to avoid warnings: 21 | ** +1 avoids warnings of "comparison has constant result"; 22 | ** cast to 'void' avoids warnings of "value unused". 23 | */ 24 | #define luaM_reallocv(L,b,on,n,e) \ 25 | (cast(void, \ 26 | (cast(size_t, (n)+1) > MAX_SIZET/(e)) ? (luaM_toobig(L), 0) : 0), \ 27 | luaM_realloc_(L, (b), (on)*(e), (n)*(e))) 28 | 29 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 30 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 31 | #define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 32 | 33 | #define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s)) 34 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 35 | #define luaM_newvector(L,n,t) \ 36 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 37 | 38 | #define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s)) 39 | 40 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 41 | if ((nelems)+1 > (size)) \ 42 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 43 | 44 | #define luaM_reallocvector(L, v,oldn,n,t) \ 45 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 46 | 47 | LUAI_FUNC l_noret luaM_toobig (lua_State *L); 48 | 49 | /* not to be called directly */ 50 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 51 | size_t size); 52 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 53 | size_t size_elem, int limit, 54 | const char *what); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lopcodes.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lopcodes.c,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Opcodes for Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #define lopcodes_c 9 | #define LUA_CORE 10 | 11 | 12 | #include "lopcodes.h" 13 | 14 | 15 | /* ORDER OP */ 16 | 17 | LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { 18 | "MOVE", 19 | "LOADK", 20 | "LOADKX", 21 | "LOADBOOL", 22 | "LOADNIL", 23 | "GETUPVAL", 24 | "GETTABUP", 25 | "GETTABLE", 26 | "SETTABUP", 27 | "SETUPVAL", 28 | "SETTABLE", 29 | "NEWTABLE", 30 | "SELF", 31 | "ADD", 32 | "SUB", 33 | "MUL", 34 | "DIV", 35 | "MOD", 36 | "POW", 37 | "UNM", 38 | "NOT", 39 | "LEN", 40 | "CONCAT", 41 | "JMP", 42 | "EQ", 43 | "LT", 44 | "LE", 45 | "TEST", 46 | "TESTSET", 47 | "CALL", 48 | "TAILCALL", 49 | "RETURN", 50 | "FORLOOP", 51 | "FORPREP", 52 | "TFORCALL", 53 | "TFORLOOP", 54 | "SETLIST", 55 | "CLOSURE", 56 | "VARARG", 57 | "EXTRAARG", 58 | NULL 59 | }; 60 | 61 | 62 | #define opmode(t,a,b,c,m) (((t)<<7) | ((a)<<6) | ((b)<<4) | ((c)<<2) | (m)) 63 | 64 | LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { 65 | /* T A B C mode opcode */ 66 | opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */ 67 | ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */ 68 | ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */ 69 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */ 70 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */ 71 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */ 72 | ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */ 73 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */ 74 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABUP */ 75 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_SETUPVAL */ 76 | ,opmode(0, 0, OpArgK, OpArgK, iABC) /* OP_SETTABLE */ 77 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_NEWTABLE */ 78 | ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_SELF */ 79 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ADD */ 80 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SUB */ 81 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MUL */ 82 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_DIV */ 83 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_MOD */ 84 | ,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_POW */ 85 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */ 86 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */ 87 | ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LEN */ 88 | ,opmode(0, 1, OpArgR, OpArgR, iABC) /* OP_CONCAT */ 89 | ,opmode(0, 0, OpArgR, OpArgN, iAsBx) /* OP_JMP */ 90 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */ 91 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */ 92 | ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */ 93 | ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */ 94 | ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */ 95 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */ 96 | ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */ 97 | ,opmode(0, 0, OpArgU, OpArgN, iABC) /* OP_RETURN */ 98 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORLOOP */ 99 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_FORPREP */ 100 | ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */ 101 | ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */ 102 | ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */ 103 | ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */ 104 | ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */ 105 | ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */ 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lparser.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lparser.h,v 1.70.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua Parser 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lparser_h 8 | #define lparser_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* 16 | ** Expression descriptor 17 | */ 18 | 19 | typedef enum { 20 | VVOID, /* no value */ 21 | VNIL, 22 | VTRUE, 23 | VFALSE, 24 | VK, /* info = index of constant in `k' */ 25 | VKNUM, /* nval = numerical value */ 26 | VNONRELOC, /* info = result register */ 27 | VLOCAL, /* info = local register */ 28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 29 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 30 | VJMP, /* info = instruction pc */ 31 | VRELOCABLE, /* info = instruction pc */ 32 | VCALL, /* info = instruction pc */ 33 | VVARARG /* info = instruction pc */ 34 | } expkind; 35 | 36 | 37 | #define vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 38 | #define vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 39 | 40 | typedef struct expdesc { 41 | expkind k; 42 | union { 43 | struct { /* for indexed variables (VINDEXED) */ 44 | short idx; /* index (R/K) */ 45 | lu_byte t; /* table (register or upvalue) */ 46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 47 | } ind; 48 | int info; /* for generic use */ 49 | lua_Number nval; /* for VKNUM */ 50 | } u; 51 | int t; /* patch list of `exit when true' */ 52 | int f; /* patch list of `exit when false' */ 53 | } expdesc; 54 | 55 | 56 | /* description of active local variable */ 57 | typedef struct Vardesc { 58 | short idx; /* variable index in stack */ 59 | } Vardesc; 60 | 61 | 62 | /* description of pending goto statements and label statements */ 63 | typedef struct Labeldesc { 64 | TString *name; /* label identifier */ 65 | int pc; /* position in code */ 66 | int line; /* line where it appeared */ 67 | lu_byte nactvar; /* local level where it appears in current block */ 68 | } Labeldesc; 69 | 70 | 71 | /* list of labels or gotos */ 72 | typedef struct Labellist { 73 | Labeldesc *arr; /* array */ 74 | int n; /* number of entries in use */ 75 | int size; /* array size */ 76 | } Labellist; 77 | 78 | 79 | /* dynamic structures used by the parser */ 80 | typedef struct Dyndata { 81 | struct { /* list of active local variables */ 82 | Vardesc *arr; 83 | int n; 84 | int size; 85 | } actvar; 86 | Labellist gt; /* list of pending gotos */ 87 | Labellist label; /* list of active labels */ 88 | } Dyndata; 89 | 90 | 91 | /* control of blocks */ 92 | struct BlockCnt; /* defined in lparser.c */ 93 | 94 | 95 | /* state needed to generate code for a given function */ 96 | typedef struct FuncState { 97 | Proto *f; /* current function header */ 98 | Table *h; /* table to find (and reuse) elements in `k' */ 99 | struct FuncState *prev; /* enclosing function */ 100 | struct LexState *ls; /* lexical state */ 101 | struct BlockCnt *bl; /* chain of current blocks */ 102 | int pc; /* next position to code (equivalent to `ncode') */ 103 | int lasttarget; /* 'label' of last 'jump label' */ 104 | int jpc; /* list of pending jumps to `pc' */ 105 | int nk; /* number of elements in `k' */ 106 | int np; /* number of elements in `p' */ 107 | int firstlocal; /* index of first local var (in Dyndata array) */ 108 | short nlocvars; /* number of elements in 'f->locvars' */ 109 | lu_byte nactvar; /* number of active local variables */ 110 | lu_byte nups; /* number of upvalues */ 111 | lu_byte freereg; /* first free register */ 112 | } FuncState; 113 | 114 | 115 | LUAI_FUNC Closure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, 116 | Dyndata *dyd, const char *name, int firstchar); 117 | 118 | 119 | #endif 120 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstring.h,v 1.49.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** String table (keep all strings handled by Lua) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstring_h 8 | #define lstring_h 9 | 10 | #include "lgc.h" 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | 14 | 15 | #define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char)) 16 | 17 | #define sizeudata(u) (sizeof(union Udata)+(u)->len) 18 | 19 | #define luaS_newliteral(L, s) (luaS_newlstr(L, "" s, \ 20 | (sizeof(s)/sizeof(char))-1)) 21 | 22 | #define luaS_fix(s) l_setbit((s)->tsv.marked, FIXEDBIT) 23 | 24 | 25 | /* 26 | ** test whether a string is a reserved word 27 | */ 28 | #define isreserved(s) ((s)->tsv.tt == LUA_TSHRSTR && (s)->tsv.extra > 0) 29 | 30 | 31 | /* 32 | ** equality for short strings, which are always internalized 33 | */ 34 | #define eqshrstr(a,b) check_exp((a)->tsv.tt == LUA_TSHRSTR, (a) == (b)) 35 | 36 | 37 | LUAI_FUNC unsigned int luaS_hash (const char *str, size_t l, unsigned int seed); 38 | LUAI_FUNC int luaS_eqlngstr (TString *a, TString *b); 39 | LUAI_FUNC int luaS_eqstr (TString *a, TString *b); 40 | LUAI_FUNC void luaS_resize (lua_State *L, int newsize); 41 | LUAI_FUNC Udata *luaS_newudata (lua_State *L, size_t s, Table *e); 42 | LUAI_FUNC TString *luaS_newlstr (lua_State *L, const char *str, size_t l); 43 | LUAI_FUNC TString *luaS_new (lua_State *L, const char *str); 44 | 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.16.1.2 2013/08/30 15:49:41 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.tvk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define invalidateTMcache(t) ((t)->flags = 0) 19 | 20 | /* returns the key, given the value of a table entry */ 21 | #define keyfromval(v) \ 22 | (gkey(cast(Node *, cast(char *, (v)) - offsetof(Node, i_val)))) 23 | 24 | 25 | LUAI_FUNC const TValue *luaH_getint (Table *t, int key); 26 | LUAI_FUNC void luaH_setint (lua_State *L, Table *t, int key, TValue *value); 27 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 28 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 29 | LUAI_FUNC TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key); 30 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 31 | LUAI_FUNC Table *luaH_new (lua_State *L); 32 | LUAI_FUNC void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize); 33 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 34 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 35 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 36 | LUAI_FUNC int luaH_getn (Table *t); 37 | 38 | 39 | #if defined(LUA_DEBUG) 40 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 41 | LUAI_FUNC int luaH_isdummy (Node *n); 42 | #endif 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ltm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.c,v 2.14.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define ltm_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "lobject.h" 16 | #include "lstate.h" 17 | #include "lstring.h" 18 | #include "ltable.h" 19 | #include "ltm.h" 20 | 21 | 22 | static const char udatatypename[] = "userdata"; 23 | 24 | LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = { 25 | "no value", 26 | "nil", "boolean", udatatypename, "number", 27 | "string", "table", "function", udatatypename, "thread", 28 | "proto", "upval" /* these last two cases are used for tests only */ 29 | }; 30 | 31 | 32 | void luaT_init (lua_State *L) { 33 | static const char *const luaT_eventname[] = { /* ORDER TM */ 34 | "__index", "__newindex", 35 | "__gc", "__mode", "__len", "__eq", 36 | "__add", "__sub", "__mul", "__div", "__mod", 37 | "__pow", "__unm", "__lt", "__le", 38 | "__concat", "__call" 39 | }; 40 | int i; 41 | for (i=0; itmname[i] = luaS_new(L, luaT_eventname[i]); 43 | luaS_fix(G(L)->tmname[i]); /* never collect these names */ 44 | } 45 | } 46 | 47 | 48 | /* 49 | ** function to be used with macro "fasttm": optimized for absence of 50 | ** tag methods 51 | */ 52 | const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { 53 | const TValue *tm = luaH_getstr(events, ename); 54 | lua_assert(event <= TM_EQ); 55 | if (ttisnil(tm)) { /* no tag method? */ 56 | events->flags |= cast_byte(1u<metatable; 68 | break; 69 | case LUA_TUSERDATA: 70 | mt = uvalue(o)->metatable; 71 | break; 72 | default: 73 | mt = G(L)->mt[ttypenv(o)]; 74 | } 75 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : luaO_nilobject); 76 | } 77 | 78 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.11.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_LEN, 24 | TM_EQ, /* last tag method with `fast' access */ 25 | TM_ADD, 26 | TM_SUB, 27 | TM_MUL, 28 | TM_DIV, 29 | TM_MOD, 30 | TM_POW, 31 | TM_UNM, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | #define ttypename(x) luaT_typenames_[(x) + 1] 47 | #define objtypename(x) ttypename(ttypenv(x)) 48 | 49 | LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS]; 50 | 51 | 52 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 53 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 54 | TMS event); 55 | LUAI_FUNC void luaT_init (lua_State *L); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lualib.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lualib.h,v 1.43.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua standard libraries 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lualib_h 9 | #define lualib_h 10 | 11 | #include "lua.h" 12 | 13 | 14 | 15 | LUAMOD_API int (luaopen_base) (lua_State *L); 16 | 17 | #define LUA_COLIBNAME "coroutine" 18 | LUAMOD_API int (luaopen_coroutine) (lua_State *L); 19 | 20 | #define LUA_TABLIBNAME "table" 21 | LUAMOD_API int (luaopen_table) (lua_State *L); 22 | 23 | #define LUA_IOLIBNAME "io" 24 | LUAMOD_API int (luaopen_io) (lua_State *L); 25 | 26 | #define LUA_OSLIBNAME "os" 27 | LUAMOD_API int (luaopen_os) (lua_State *L); 28 | 29 | #define LUA_STRLIBNAME "string" 30 | LUAMOD_API int (luaopen_string) (lua_State *L); 31 | 32 | #define LUA_BITLIBNAME "bit32" 33 | LUAMOD_API int (luaopen_bit32) (lua_State *L); 34 | 35 | #define LUA_MATHLIBNAME "math" 36 | LUAMOD_API int (luaopen_math) (lua_State *L); 37 | 38 | #define LUA_DBLIBNAME "debug" 39 | LUAMOD_API int (luaopen_debug) (lua_State *L); 40 | 41 | #define LUA_LOADLIBNAME "package" 42 | LUAMOD_API int (luaopen_package) (lua_State *L); 43 | 44 | 45 | /* open all previous libraries */ 46 | LUALIB_API void (luaL_openlibs) (lua_State *L); 47 | 48 | 49 | 50 | #if !defined(lua_assert) 51 | #define lua_assert(x) ((void)0) 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h,v 1.39.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "lobject.h" 11 | #include "lzio.h" 12 | 13 | /* load one chunk; from lundump.c */ 14 | LUAI_FUNC Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name); 15 | 16 | /* make header; from lundump.c */ 17 | LUAI_FUNC void luaU_header (lu_byte* h); 18 | 19 | /* dump one chunk; from ldump.c */ 20 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip); 21 | 22 | /* data to catch conversion errors */ 23 | #define LUAC_TAIL "\x19\x93\r\n\x1a\n" 24 | 25 | /* size in bytes of header of binary files */ 26 | #define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char)) 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lvm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lvm.h,v 2.18.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Lua virtual machine 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lvm_h 8 | #define lvm_h 9 | 10 | 11 | #include "ldo.h" 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | 15 | 16 | #define tostring(L,o) (ttisstring(o) || (luaV_tostring(L, o))) 17 | 18 | #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL)) 19 | 20 | #define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2)) 21 | 22 | #define luaV_rawequalobj(o1,o2) equalobj(NULL,o1,o2) 23 | 24 | 25 | /* not to called directly */ 26 | LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2); 27 | 28 | 29 | LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); 30 | LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); 31 | LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n); 32 | LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj); 33 | LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, 34 | StkId val); 35 | LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, 36 | StkId val); 37 | LUAI_FUNC void luaV_finishOp (lua_State *L); 38 | LUAI_FUNC void luaV_execute (lua_State *L); 39 | LUAI_FUNC void luaV_concat (lua_State *L, int total); 40 | LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb, 41 | const TValue *rc, TMS op); 42 | LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb); 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lzio.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.c,v 1.35.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #include 9 | 10 | #define lzio_c 11 | #define LUA_CORE 12 | 13 | #include "lua.h" 14 | 15 | #include "llimits.h" 16 | #include "lmem.h" 17 | #include "lstate.h" 18 | #include "lzio.h" 19 | 20 | 21 | int luaZ_fill (ZIO *z) { 22 | size_t size; 23 | lua_State *L = z->L; 24 | const char *buff; 25 | lua_unlock(L); 26 | buff = z->reader(L, z->data, &size); 27 | lua_lock(L); 28 | if (buff == NULL || size == 0) 29 | return EOZ; 30 | z->n = size - 1; /* discount char being returned */ 31 | z->p = buff; 32 | return cast_uchar(*(z->p++)); 33 | } 34 | 35 | 36 | void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { 37 | z->L = L; 38 | z->reader = reader; 39 | z->data = data; 40 | z->n = 0; 41 | z->p = NULL; 42 | } 43 | 44 | 45 | /* --------------------------------------------------------------- read --- */ 46 | size_t luaZ_read (ZIO *z, void *b, size_t n) { 47 | while (n) { 48 | size_t m; 49 | if (z->n == 0) { /* no bytes in buffer? */ 50 | if (luaZ_fill(z) == EOZ) /* try to read more */ 51 | return n; /* no more input; return number of missing bytes */ 52 | else { 53 | z->n++; /* luaZ_fill consumed first byte; put it back */ 54 | z->p--; 55 | } 56 | } 57 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 58 | memcpy(b, z->p, m); 59 | z->n -= m; 60 | z->p += m; 61 | b = (char *)b + m; 62 | n -= m; 63 | } 64 | return 0; 65 | } 66 | 67 | /* ------------------------------------------------------------------------ */ 68 | char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) { 69 | if (n > buff->buffsize) { 70 | if (n < LUA_MINBUFFER) n = LUA_MINBUFFER; 71 | luaZ_resizebuffer(L, buff, n); 72 | } 73 | return buff->buffer; 74 | } 75 | 76 | 77 | -------------------------------------------------------------------------------- /lua/lua-5.2.4/src/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.26.1.1 2013/04/12 18:48:47 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define zgetc(z) (((z)->n--)>0 ? cast_uchar(*(z)->p++) : luaZ_fill(z)) 21 | 22 | 23 | typedef struct Mbuffer { 24 | char *buffer; 25 | size_t n; 26 | size_t buffsize; 27 | } Mbuffer; 28 | 29 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 30 | 31 | #define luaZ_buffer(buff) ((buff)->buffer) 32 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 33 | #define luaZ_bufflen(buff) ((buff)->n) 34 | 35 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 36 | 37 | 38 | #define luaZ_resizebuffer(L, buff, size) \ 39 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 40 | (buff)->buffsize = size) 41 | 42 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 43 | 44 | 45 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 46 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 47 | void *data); 48 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 49 | 50 | 51 | 52 | /* --------- Private Part ------------------ */ 53 | 54 | struct Zio { 55 | size_t n; /* bytes still unread */ 56 | const char *p; /* current position in buffer */ 57 | lua_Reader reader; /* reader function */ 58 | void* data; /* additional data */ 59 | lua_State *L; /* Lua state (for reader) */ 60 | }; 61 | 62 | 63 | LUAI_FUNC int luaZ_fill (ZIO *z); 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /src/kapi.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Auxiliary functions from Killa API */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KAPI_H_ 12 | #define KAPI_H_ 13 | 14 | 15 | #include "klimits.h" 16 | #include "kstate.h" 17 | 18 | #define killaA_incr_top(L) {L->top++; killa_api_check(L, L->top <= L->ci->top, \ 19 | "stack overflow");} 20 | 21 | #define killaA_adjustresults(L,nres) \ 22 | { if ((nres) == KILLA_MULTRET && L->ci->top < L->top) L->ci->top = L->top; } 23 | 24 | #define killaA_checknelems(L,n) killa_api_check(L, (n) < (L->top - L->ci->func), \ 25 | "not enough elements in the stack") 26 | 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /src/kcode.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Code generator for Killa */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KCODE_H_ 12 | #define KCODE_H_ 13 | 14 | #include "klex.h" 15 | #include "kobject.h" 16 | #include "kopcodes.h" 17 | #include "kparser.h" 18 | 19 | 20 | /* 21 | ** Marks the end of a patch list. It is an invalid value both as an absolute 22 | ** address, and as a list link (would link an element to itself). 23 | */ 24 | #define KILLA_NO_JUMP (-1) 25 | 26 | 27 | /* 28 | ** grep "ORDER OPR" if you change these enums (ORDER OP) 29 | */ 30 | typedef enum killaK_BinOpr { 31 | OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, 32 | OPR_BAND, OPR_BOR, OPR_BXOR, OPR_BLSH, OPR_BRSH, 33 | OPR_CONCAT, 34 | OPR_EQ, OPR_LT, OPR_LE, 35 | OPR_NE, OPR_GT, OPR_GE, 36 | OPR_AND, OPR_OR, 37 | OPR_CADD, OPR_CSUB, OPR_CMUL, OPR_CDIV, OPR_CMOD, OPR_CCONCAT, 38 | OPR_NOBINOPR 39 | } killaK_BinOpr; 40 | 41 | 42 | typedef enum killaK_UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_BNOT, OPR_NOUNOPR } killaK_UnOpr; 43 | 44 | 45 | #define killaK_getcode(fs,e) ((fs)->f->code[(e)->u.info]) 46 | 47 | #define killaK_codeAsBx(fs,o,A,sBx) killaK_codeABx(fs,o,A,(sBx)+KILLA_MAXARG_sBx) 48 | 49 | #define killaK_setmultret(fs,e) killaK_setreturns(fs, e, KILLA_MULTRET) 50 | 51 | #define killaK_jumpto(fs,t) killaK_patchlist(fs, killaK_jump(fs), t) 52 | 53 | KILLAI_FUNC int killaK_codeABx (killa_FuncState *fs, killa_OpCode o, int A, unsigned int Bx); 54 | KILLAI_FUNC int killaK_codeABC (killa_FuncState *fs, killa_OpCode o, int A, int B, int C); 55 | KILLAI_FUNC int killaK_codek (killa_FuncState *fs, int reg, int k); 56 | KILLAI_FUNC void killaK_fixline (killa_FuncState *fs, int line); 57 | KILLAI_FUNC void killaK_nil (killa_FuncState *fs, int from, int n); 58 | KILLAI_FUNC void killaK_reserveregs (killa_FuncState *fs, int n); 59 | KILLAI_FUNC void killaK_checkstack (killa_FuncState *fs, int n); 60 | KILLAI_FUNC int killaK_stringK (killa_FuncState *fs, killa_TString *s); 61 | KILLAI_FUNC int killaK_numberK (killa_FuncState *fs, killa_Number r); 62 | KILLAI_FUNC void killaK_dischargevars (killa_FuncState *fs, killa_expdesc *e); 63 | KILLAI_FUNC int killaK_exp2anyreg (killa_FuncState *fs, killa_expdesc *e); 64 | KILLAI_FUNC void killaK_exp2reg (killa_FuncState *fs, killa_expdesc *e, int reg); 65 | KILLAI_FUNC void killaK_exp2anyregup (killa_FuncState *fs, killa_expdesc *e); 66 | KILLAI_FUNC void killaK_exp2nextreg (killa_FuncState *fs, killa_expdesc *e); 67 | KILLAI_FUNC void killaK_exp2val (killa_FuncState *fs, killa_expdesc *e); 68 | KILLAI_FUNC int killaK_exp2RK (killa_FuncState *fs, killa_expdesc *e); 69 | KILLAI_FUNC void killaK_self (killa_FuncState *fs, killa_expdesc *e, killa_expdesc *key); 70 | KILLAI_FUNC void killaK_indexed (killa_FuncState *fs, killa_expdesc *t, killa_expdesc *k); 71 | KILLAI_FUNC void killaK_goiftrue (killa_FuncState *fs, killa_expdesc *e); 72 | KILLAI_FUNC void killaK_goiffalse (killa_FuncState *fs, killa_expdesc *e); 73 | KILLAI_FUNC void killaK_storevar (killa_FuncState *fs, killa_expdesc *var, killa_expdesc *e); 74 | KILLAI_FUNC void killaK_setreturns (killa_FuncState *fs, killa_expdesc *e, int nresults); 75 | KILLAI_FUNC void killaK_setoneret (killa_FuncState *fs, killa_expdesc *e); 76 | KILLAI_FUNC int killaK_jump (killa_FuncState *fs); 77 | KILLAI_FUNC void killaK_ret (killa_FuncState *fs, int first, int nret); 78 | KILLAI_FUNC void killaK_patchlist (killa_FuncState *fs, int list, int target); 79 | KILLAI_FUNC void killaK_patchtohere (killa_FuncState *fs, int list); 80 | KILLAI_FUNC void killaK_patchclose (killa_FuncState *fs, int list, int level); 81 | KILLAI_FUNC void killaK_concat (killa_FuncState *fs, int *l1, int l2); 82 | KILLAI_FUNC int killaK_getlabel (killa_FuncState *fs); 83 | KILLAI_FUNC void killaK_prefix (killa_FuncState *fs, killaK_UnOpr op, killa_expdesc *v, int line); 84 | KILLAI_FUNC void killaK_infix (killa_FuncState *fs, killaK_BinOpr op, killa_expdesc *v); 85 | KILLAI_FUNC void killaK_posfix (killa_FuncState *fs, killaK_BinOpr op, killa_expdesc *v1, 86 | killa_expdesc *v2, int line); 87 | KILLAI_FUNC void killaK_setlist (killa_FuncState *fs, int base, int nelems, int tostore); 88 | 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/kctype.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* 'ctype' functions for Killa */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #define KILLA_CORE 12 | 13 | #include "kctype.h" 14 | 15 | #if !KILLA_USE_CTYPE /* { */ 16 | 17 | #include 18 | 19 | KILLAI_DDEF const killa_ubyte killai_ctype_[UCHAR_MAX + 2] = { 20 | 0x00, /* KILLA_EOZ */ 21 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0. */ 22 | 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00, 23 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1. */ 24 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 25 | 0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, /* 2. */ 26 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 27 | 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, /* 3. */ 28 | 0x16, 0x16, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 29 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 4. */ 30 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 31 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 5. */ 32 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x05, 33 | 0x04, 0x15, 0x15, 0x15, 0x15, 0x15, 0x15, 0x05, /* 6. */ 34 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 35 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, /* 7. */ 36 | 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x00, 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 8. */ 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 9. */ 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* a. */ 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* b. */ 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 45 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* c. */ 46 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* d. */ 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* e. */ 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* f. */ 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 53 | }; 54 | 55 | #endif /* } */ 56 | -------------------------------------------------------------------------------- /src/kctype.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* 'ctype' functions for Killa */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KCTYPE_H_ 12 | #define KCTYPE_H_ 13 | 14 | #include "killa.h" 15 | 16 | 17 | /* 18 | ** WARNING: the functions defined here do not necessarily correspond 19 | ** to the similar functions in the standard C ctype.h. They are 20 | ** optimized for the specific needs of Killa 21 | */ 22 | 23 | #if !defined(KILLA_USE_CTYPE) 24 | 25 | #if 'A' == 65 && '0' == 48 26 | /* ASCII case: can use its own tables; faster and fixed */ 27 | #define KILLA_USE_CTYPE 0 28 | #else 29 | /* must use standard C ctype */ 30 | #define KILLA_USE_CTYPE 1 31 | #endif 32 | 33 | #endif 34 | 35 | 36 | #if !KILLA_USE_CTYPE /* { */ 37 | 38 | #include 39 | 40 | #include "klimits.h" 41 | 42 | 43 | #define KILLA_ALPHABIT 0 44 | #define KILLA_DIGITBIT 1 45 | #define KILLA_PRINTBIT 2 46 | #define KILLA_SPACEBIT 3 47 | #define KILLA_XDIGITBIT 4 48 | 49 | 50 | #define KILLA_MASK(B) (1 << (B)) 51 | 52 | 53 | /* 54 | ** add 1 to char to allow index -1 (KILLA_EOZ) 55 | */ 56 | #define killa_testprop(c,p) (killai_ctype_[(c)+1] & (p)) 57 | 58 | /* 59 | ** 'lalpha' (Killa alphabetic) and 'lalnum' (Killa alphanumeric) both include '_' 60 | */ 61 | #define killa_islalpha(c) killa_testprop(c, KILLA_MASK(KILLA_ALPHABIT)) 62 | #define killa_islalnum(c) killa_testprop(c, (KILLA_MASK(KILLA_ALPHABIT) | KILLA_MASK(KILLA_DIGITBIT))) 63 | #define killa_isdigit(c) killa_testprop(c, KILLA_MASK(KILLA_DIGITBIT)) 64 | #define killa_isspace(c) killa_testprop(c, KILLA_MASK(KILLA_SPACEBIT)) 65 | #define killa_isprint(c) killa_testprop(c, KILLA_MASK(KILLA_PRINTBIT)) 66 | #define killa_isxdigit(c) killa_testprop(c, KILLA_MASK(KILLA_XDIGITBIT)) 67 | 68 | /* 69 | ** this 'killa_tolower' only works for alphabetic characters 70 | */ 71 | #define killa_tolower(c) ((c) | ('A' ^ 'a')) 72 | 73 | 74 | /* two more entries for 0 and -1 (KILLA_EOZ) */ 75 | KILLAI_DDEC const killa_ubyte killai_ctype_[UCHAR_MAX + 2]; 76 | 77 | 78 | #else /* }{ */ 79 | 80 | /* 81 | ** use standard C ctypes 82 | */ 83 | 84 | #include 85 | 86 | 87 | #define killa_islalpha(c) (isalpha(c) || (c) == '_') 88 | #define killa_islalnum(c) (isalnum(c) || (c) == '_') 89 | #define killa_isdigit(c) (isdigit(c)) 90 | #define killa_isspace(c) (isspace(c)) 91 | #define killa_isprint(c) (isprint(c)) 92 | #define killa_isxdigit(c) (isxdigit(c)) 93 | 94 | #define killa_tolower(c) (tolower(c)) 95 | 96 | #endif /* } */ 97 | 98 | #endif 99 | 100 | -------------------------------------------------------------------------------- /src/kdebug.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Auxiliary functions from Debug Interface module */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KDEBUG_H_ 12 | #define KDEBUG_H_ 13 | 14 | 15 | #include "kstate.h" 16 | 17 | 18 | #define killa_pcRel(pc, p) (killa_cast(int, (pc) - (p)->code) - 1) 19 | 20 | #define killa_getfuncline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 21 | 22 | #define killa_resethookcount(L) (L->hookcount = L->basehookcount) 23 | 24 | /* Active Killa function (given call info) */ 25 | #define killa_ci_func(ci) (killa_clLvalue((ci)->func)) 26 | 27 | 28 | KILLAI_FUNC killa_noret killaG_typeerror (killa_State *L, const killa_TValue *o, 29 | const char *opname); 30 | KILLAI_FUNC killa_noret killaG_concaterror (killa_State *L, killa_StkId p1, killa_StkId p2); 31 | KILLAI_FUNC killa_noret killaG_aritherror (killa_State *L, const killa_TValue *p1, 32 | const killa_TValue *p2); 33 | KILLAI_FUNC killa_noret killaG_ordererror (killa_State *L, const killa_TValue *p1, 34 | const killa_TValue *p2); 35 | KILLAI_FUNC killa_noret killaG_runerror (killa_State *L, const char *fmt, ...); 36 | KILLAI_FUNC killa_noret killaG_errormsg (killa_State *L); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /src/kdo.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Stack and Call structure of Killa */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KDO_H_ 12 | #define KDO_H_ 13 | 14 | 15 | #include "kobject.h" 16 | #include "kstate.h" 17 | #include "kzio.h" 18 | 19 | 20 | #define killaD_checkstack(L,n) if (L->stack_last - L->top <= (n)) \ 21 | killaD_growstack(L, n); else killa_condmovestack(L); 22 | 23 | 24 | #define killa_incr_top(L) {L->top++; killaD_checkstack(L,0);} 25 | 26 | #define killa_savestack(L,p) ((char *)(p) - (char *)L->stack) 27 | #define killa_restorestack(L,n) ((killa_TValue *)((char *)L->stack + (n))) 28 | 29 | 30 | /* type of protected functions, to be ran by `runprotected' */ 31 | typedef void (*Pfunc) (killa_State *L, void *ud); 32 | 33 | KILLAI_FUNC int killaD_protectedparser (killa_State *L, killa_ZIO *z, const char *name, 34 | const char *mode); 35 | KILLAI_FUNC void killaD_hook (killa_State *L, int event, int line); 36 | KILLAI_FUNC int killaD_precall (killa_State *L, killa_StkId func, int nresults); 37 | KILLAI_FUNC void killaD_call (killa_State *L, killa_StkId func, int nResults, 38 | int allowyield); 39 | KILLAI_FUNC int killaD_pcall (killa_State *L, Pfunc func, void *u, 40 | ptrdiff_t oldtop, ptrdiff_t ef); 41 | KILLAI_FUNC int killaD_poscall (killa_State *L, killa_StkId firstResult); 42 | KILLAI_FUNC void killaD_reallocstack (killa_State *L, int newsize); 43 | KILLAI_FUNC void killaD_growstack (killa_State *L, int n); 44 | KILLAI_FUNC void killaD_shrinkstack (killa_State *L); 45 | 46 | KILLAI_FUNC killa_noret killaD_throw (killa_State *L, int errcode); 47 | KILLAI_FUNC int killaD_rawrunprotected (killa_State *L, Pfunc f, void *ud); 48 | 49 | #endif 50 | 51 | -------------------------------------------------------------------------------- /src/kdump.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Save precompiled Killa scripts */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | 13 | #define KILLA_CORE 14 | 15 | #include "killa.h" 16 | 17 | #include "kobject.h" 18 | #include "kstate.h" 19 | #include "kundump.h" 20 | 21 | typedef struct { 22 | killa_State* L; 23 | killa_Writer writer; 24 | void* data; 25 | int strip; 26 | int status; 27 | } DumpState; 28 | 29 | #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D) 30 | #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D) 31 | 32 | static void DumpBlock(const void* b, size_t size, DumpState* D) 33 | { 34 | if (D->status==0) 35 | { 36 | killa_unlock(D->L); 37 | D->status=(*D->writer)(D->L,b,size,D->data); 38 | killa_lock(D->L); 39 | } 40 | } 41 | 42 | static void DumpChar(int y, DumpState* D) 43 | { 44 | char x=(char)y; 45 | DumpVar(x,D); 46 | } 47 | 48 | static void DumpInt(int x, DumpState* D) 49 | { 50 | DumpVar(x,D); 51 | } 52 | 53 | static void DumpNumber(killa_Number x, DumpState* D) 54 | { 55 | DumpVar(x,D); 56 | } 57 | 58 | static void DumpVector(const void* b, int n, size_t size, DumpState* D) 59 | { 60 | DumpInt(n,D); 61 | DumpMem(b,n,size,D); 62 | } 63 | 64 | static void DumpString(const killa_TString* s, DumpState* D) 65 | { 66 | if (s==NULL) 67 | { 68 | size_t size=0; 69 | DumpVar(size,D); 70 | } 71 | else 72 | { 73 | size_t size=s->tsv.len+1; /* include trailing '\0' */ 74 | DumpVar(size,D); 75 | DumpBlock(killa_getstr(s),size*sizeof(char),D); 76 | } 77 | } 78 | 79 | #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(killa_Instruction),D) 80 | 81 | static void DumpFunction(const killa_Proto* f, DumpState* D); 82 | 83 | static void DumpConstants(const killa_Proto* f, DumpState* D) 84 | { 85 | int i,n=f->sizek; 86 | DumpInt(n,D); 87 | for (i=0; ik[i]; 90 | DumpChar(killa_ttype(o),D); 91 | switch (killa_ttype(o)) 92 | { 93 | case KILLA_TNULL: 94 | break; 95 | case KILLA_TBOOLEAN: 96 | DumpChar(killa_bvalue(o),D); 97 | break; 98 | case KILLA_TNUMBER: 99 | DumpNumber(killa_nvalue(o),D); 100 | break; 101 | case KILLA_TSTRING: 102 | DumpString(killa_rawtsvalue(o),D); 103 | break; 104 | } 105 | } 106 | n=f->sizep; 107 | DumpInt(n,D); 108 | for (i=0; ip[i],D); 109 | } 110 | 111 | static void DumpUpvalues(const killa_Proto* f, DumpState* D) 112 | { 113 | int i,n=f->sizeupvalues; 114 | DumpInt(n,D); 115 | for (i=0; iupvalues[i].instack,D); 118 | DumpChar(f->upvalues[i].idx,D); 119 | } 120 | } 121 | 122 | static void DumpDebug(const killa_Proto* f, DumpState* D) 123 | { 124 | int i,n; 125 | DumpString((D->strip) ? NULL : f->source,D); 126 | n= (D->strip) ? 0 : f->sizelineinfo; 127 | DumpVector(f->lineinfo,n,sizeof(int),D); 128 | n= (D->strip) ? 0 : f->sizelocvars; 129 | DumpInt(n,D); 130 | for (i=0; ilocvars[i].varname,D); 133 | DumpInt(f->locvars[i].startpc,D); 134 | DumpInt(f->locvars[i].endpc,D); 135 | } 136 | n= (D->strip) ? 0 : f->sizeupvalues; 137 | DumpInt(n,D); 138 | for (i=0; iupvalues[i].name,D); 139 | } 140 | 141 | static void DumpFunction(const killa_Proto* f, DumpState* D) 142 | { 143 | DumpInt(f->linedefined,D); 144 | DumpInt(f->lastlinedefined,D); 145 | DumpChar(f->numparams,D); 146 | DumpChar(f->is_vararg,D); 147 | DumpChar(f->maxstacksize,D); 148 | DumpCode(f,D); 149 | DumpConstants(f,D); 150 | DumpUpvalues(f,D); 151 | DumpDebug(f,D); 152 | } 153 | 154 | static void DumpHeader(DumpState* D) 155 | { 156 | killa_ubyte h[KILLAC_HEADERSIZE]; 157 | killaU_header(h); 158 | DumpBlock(h,KILLAC_HEADERSIZE,D); 159 | } 160 | 161 | /* 162 | ** dump Killa function as precompiled chunk 163 | */ 164 | int killaU_dump (killa_State* L, const killa_Proto* f, killa_Writer w, void* data, int strip) 165 | { 166 | DumpState D; 167 | D.L=L; 168 | D.writer=w; 169 | D.data=data; 170 | D.strip=strip; 171 | D.status=0; 172 | DumpHeader(&D); 173 | DumpFunction(f,&D); 174 | return D.status; 175 | } 176 | -------------------------------------------------------------------------------- /src/kfunc.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Auxiliary functions to manipulate prototypes and closures */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KFUNC_H_ 12 | #define KFUNC_H_ 13 | 14 | 15 | #include "kobject.h" 16 | 17 | 18 | #define killa_sizeCclosure(n) (killa_cast(int, sizeof(killa_CClosure)) + \ 19 | killa_cast(int, sizeof(killa_TValue)*((n)-1))) 20 | 21 | #define killa_sizeLclosure(n) (killa_cast(int, sizeof(killa_LClosure)) + \ 22 | killa_cast(int, sizeof(killa_TValue *)*((n)-1))) 23 | 24 | 25 | KILLAI_FUNC killa_Proto *killaF_newproto (killa_State *L); 26 | KILLAI_FUNC killa_Closure *killaF_newCclosure (killa_State *L, int nelems); 27 | KILLAI_FUNC killa_Closure *killaF_newLclosure (killa_State *L, killa_Proto *p); 28 | KILLAI_FUNC killa_UpVal *killaF_newupval (killa_State *L); 29 | KILLAI_FUNC killa_UpVal *killaF_findupval (killa_State *L, killa_StkId level); 30 | KILLAI_FUNC void killaF_close (killa_State *L, killa_StkId level); 31 | KILLAI_FUNC void killaF_freeproto (killa_State *L, killa_Proto *f); 32 | KILLAI_FUNC void killaF_freeclosure (killa_State *L, killa_Closure *c); 33 | KILLAI_FUNC void killaF_freeupval (killa_State *L, killa_UpVal *uv); 34 | KILLAI_FUNC const char *killaF_getlocalname (const killa_Proto *func, 35 | int local_number, int pc); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /src/killa.hpp: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Lua header files for C++ */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | // <> not supplied automatically because Lua also compiles as C++ 12 | 13 | extern "C" { 14 | #include "lua.h" 15 | #include "lualib.h" 16 | #include "lauxlib.h" 17 | } 18 | -------------------------------------------------------------------------------- /src/killalib.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Killa standard libraries */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KILLALIB_H_ 12 | #define KILLALIB_H_ 13 | 14 | #include "killa.h" 15 | 16 | 17 | 18 | KILLAMOD_API int (killaopen_base) (killa_State *L); 19 | 20 | #define KILLA_COLIBNAME "coroutine" 21 | KILLAMOD_API int (killaopen_coroutine) (killa_State *L); 22 | 23 | #define KILLA_TABLIBNAME "table" 24 | KILLAMOD_API int (killaopen_table) (killa_State *L); 25 | 26 | #define KILLA_IOLIBNAME "io" 27 | KILLAMOD_API int (killaopen_io) (killa_State *L); 28 | 29 | #define KILLA_OSLIBNAME "os" 30 | KILLAMOD_API int (killaopen_os) (killa_State *L); 31 | 32 | #define KILLA_STRLIBNAME "string" 33 | KILLAMOD_API int (killaopen_string) (killa_State *L); 34 | 35 | #define KILLA_BITLIBNAME "bit32" 36 | KILLAMOD_API int (killaopen_bit32) (killa_State *L); 37 | 38 | #define KILLA_MATHLIBNAME "math" 39 | KILLAMOD_API int (killaopen_math) (killa_State *L); 40 | 41 | #define KILLA_DBLIBNAME "debug" 42 | KILLAMOD_API int (killaopen_debug) (killa_State *L); 43 | 44 | #define KILLA_LOADLIBNAME "package" 45 | KILLAMOD_API int (killaopen_package) (killa_State *L); 46 | 47 | 48 | /* open all previous libraries */ 49 | KILLALIB_API void (killaL_openlibs) (killa_State *L); 50 | 51 | 52 | 53 | #if !defined(killa_assert) 54 | #define killa_assert(x) ((void)0) 55 | #endif 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /src/kinit.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Initialization of libraries for killa.c */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | /* 12 | ** If you embed Killa in your program and need to open the standard 13 | ** libraries, call killaL_openlibs in your program. If you need a 14 | ** different set of libraries, copy this file to your project and edit 15 | ** it to suit your needs. 16 | */ 17 | 18 | 19 | #define KILLA_LIB 20 | 21 | #include "killa.h" 22 | 23 | #include "killalib.h" 24 | #include "kauxlib.h" 25 | 26 | #ifdef KILLA_EXTENDED_BC 27 | #include "lbc.h" 28 | #endif 29 | 30 | /* 31 | ** these libs are loaded by killa.c and are readily available to any Killa 32 | ** program 33 | */ 34 | static const killaL_Reg loadedlibs[] = { 35 | {"_G", killaopen_base}, 36 | {KILLA_LOADLIBNAME, killaopen_package}, 37 | {KILLA_COLIBNAME, killaopen_coroutine}, 38 | {KILLA_TABLIBNAME, killaopen_table}, 39 | {KILLA_IOLIBNAME, killaopen_io}, 40 | {KILLA_OSLIBNAME, killaopen_os}, 41 | {KILLA_STRLIBNAME, killaopen_string}, 42 | {KILLA_BITLIBNAME, killaopen_bit32}, 43 | {KILLA_MATHLIBNAME, killaopen_math}, 44 | {KILLA_DBLIBNAME, killaopen_debug}, 45 | {NULL, NULL} 46 | }; 47 | 48 | 49 | /* 50 | ** these libs are preloaded and must be required before used 51 | */ 52 | static const killaL_Reg preloadedlibs[] = { 53 | #ifdef KILLA_EXTENDED 54 | #ifdef KILLA_EXTENDED_BC 55 | {"bc", luaopen_bc}, 56 | #endif 57 | #endif 58 | {NULL, NULL} 59 | }; 60 | 61 | 62 | KILLALIB_API void killaL_openlibs (killa_State *L) { 63 | const killaL_Reg *lib; 64 | /* call open functions from 'loadedlibs' and set results to global table */ 65 | for (lib = loadedlibs; lib->func; lib++) { 66 | killaL_requiref(L, lib->name, lib->func, 1); 67 | killa_pop(L, 1); /* remove lib */ 68 | } 69 | /* add open functions from 'preloadedlibs' into 'package.preload' table */ 70 | killaL_getsubtable(L, KILLA_REGISTRYINDEX, "_PRELOAD"); 71 | for (lib = preloadedlibs; lib->func; lib++) { 72 | killa_pushcfunction(L, lib->func); 73 | killa_setfield(L, -2, lib->name); 74 | } 75 | killa_pop(L, 1); /* remove _PRELOAD table */ 76 | } 77 | 78 | -------------------------------------------------------------------------------- /src/klex.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Lexical Analyzer */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KLEX_H_ 12 | #define KLEX_H_ 13 | 14 | #include "kobject.h" 15 | #include "kzio.h" 16 | 17 | 18 | #define KILLA_FIRST_RESERVED 257 19 | 20 | 21 | 22 | /* 23 | * WARNING: if you change the order of this enumeration, 24 | * grep "ORDER RESERVED" 25 | */ 26 | enum KILLA_RESERVED { 27 | /* terminal symbols denoted by reserved words */ 28 | TK_BREAK = KILLA_FIRST_RESERVED, TK_DO, TK_EACH, 29 | TK_ELSE, TK_FALSE, TK_FOR, TK_FUNCTION, TK_GOTO, 30 | TK_IF, TK_IN, TK_NULL, TK_PRIVATE, TK_PUBLIC, 31 | TK_RETURN, TK_TO, TK_TRUE, TK_VAR, TK_WHILE, 32 | /* FUTURE RESERVED */ 33 | TK_CASE, TK_CATCH, TK_CLASS, TK_CONST, TK_CONTINUE, 34 | TK_DEFAULT, TK_EXTENDS, TK_FINALLY, TK_IMPLEMENTS, TK_IMPORT, 35 | TK_INTERFACE, TK_NEW, TK_OVERRIDE, TK_PROTECTED, TK_STATIC, 36 | TK_SUPER, TK_SWITCH, TK_THROW, TK_TRY, TK_UNDEFINED, 37 | /* other terminal symbols */ 38 | TK_AND, TK_OR, TK_POW, TK_DBCOLON, 39 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, 40 | TK_CADD, TK_CSUB, TK_CMUL, TK_CDIV, TK_CMOD, TK_CCONCAT, 41 | TK_BLSH, TK_BRSH, 42 | TK_EOS, TK_NUMBER, TK_NAME, TK_STRING 43 | }; 44 | 45 | /* number of reserved words */ 46 | #define KILLA_NUM_RESERVED (killa_cast(int, TK_UNDEFINED - KILLA_FIRST_RESERVED + 1)) 47 | 48 | 49 | typedef union { 50 | killa_Number r; 51 | killa_TString *ts; 52 | } killa_SemInfo; /* semantics information */ 53 | 54 | 55 | typedef struct killa_Token { 56 | int token; 57 | killa_SemInfo seminfo; 58 | } killa_Token; 59 | 60 | 61 | /* state of the lexer plus state of the parser when shared by all 62 | functions */ 63 | typedef struct killa_LexState { 64 | int current; /* current character (charint) */ 65 | int linenumber; /* input line counter */ 66 | int lastline; /* line of last token `consumed' */ 67 | killa_Token t; /* current token */ 68 | killa_Token lookahead; /* look ahead token */ 69 | struct killa_FuncState *fs; /* current function (parser) */ 70 | struct killa_State *L; 71 | killa_ZIO *z; /* input stream */ 72 | killa_Mbuffer *buff; /* buffer for tokens */ 73 | struct killa_Dyndata *dyd; /* dynamic structures used by the parser */ 74 | killa_TString *source; /* current source name */ 75 | killa_TString *envn; /* environment variable name */ 76 | char decpoint; /* locale decimal point */ 77 | } killa_LexState; 78 | 79 | 80 | KILLAI_FUNC void killaX_init (killa_State *L); 81 | KILLAI_FUNC void killaX_setinput (killa_State *L, killa_LexState *ls, killa_ZIO *z, 82 | killa_TString *source, int firstchar); 83 | KILLAI_FUNC killa_TString *killaX_newstring (killa_LexState *ls, const char *str, size_t l); 84 | KILLAI_FUNC void killaX_next (killa_LexState *ls); 85 | KILLAI_FUNC int killaX_lookahead (killa_LexState *ls); 86 | KILLAI_FUNC killa_noret killaX_syntaxerror (killa_LexState *ls, const char *s); 87 | KILLAI_FUNC const char *killaX_token2str (killa_LexState *ls, int token); 88 | 89 | 90 | #endif 91 | -------------------------------------------------------------------------------- /src/kmem.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Interface to Memory Manager */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | 13 | #define KILLA_CORE 14 | 15 | #include "killa.h" 16 | 17 | #include "kdebug.h" 18 | #include "kdo.h" 19 | #include "kgc.h" 20 | #include "kmem.h" 21 | #include "kobject.h" 22 | #include "kstate.h" 23 | 24 | 25 | 26 | /* 27 | ** About the realloc function: 28 | ** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 29 | ** (`osize' is the old size, `nsize' is the new size) 30 | ** 31 | ** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no 32 | ** matter 'x'). 33 | ** 34 | ** * frealloc(ud, p, x, 0) frees the block `p' 35 | ** (in this specific case, frealloc must return NULL); 36 | ** particularly, frealloc(ud, NULL, 0, 0) does nothing 37 | ** (which is equivalent to free(NULL) in ANSI C) 38 | ** 39 | ** frealloc returns NULL if it cannot create or reallocate the area 40 | ** (any reallocation to an equal or smaller size cannot fail!) 41 | */ 42 | 43 | 44 | 45 | #define MINSIZEARRAY 4 46 | 47 | 48 | void *killaM_growaux_ (killa_State *L, void *block, int *size, size_t size_elems, 49 | int limit, const char *what) { 50 | void *newblock; 51 | int newsize; 52 | if (*size >= limit/2) { /* cannot double it? */ 53 | if (*size >= limit) /* cannot grow even a little? */ 54 | killaG_runerror(L, "too many %s (limit is %d)", what, limit); 55 | newsize = limit; /* still have at least one free place */ 56 | } 57 | else { 58 | newsize = (*size)*2; 59 | if (newsize < MINSIZEARRAY) 60 | newsize = MINSIZEARRAY; /* minimum size */ 61 | } 62 | newblock = killaM_reallocv(L, block, *size, newsize, size_elems); 63 | *size = newsize; /* update only when everything else is OK */ 64 | return newblock; 65 | } 66 | 67 | 68 | killa_noret killaM_toobig (killa_State *L) { 69 | killaG_runerror(L, "memory allocation error: block too big"); 70 | } 71 | 72 | 73 | 74 | /* 75 | ** generic allocation routine. 76 | */ 77 | void *killaM_realloc_ (killa_State *L, void *block, size_t osize, size_t nsize) { 78 | void *newblock; 79 | killa_GlobalState *g = KILLA_G(L); 80 | size_t realosize = (block) ? osize : 0; 81 | killa_assert((realosize == 0) == (block == NULL)); 82 | #if defined(KILLA_HARDMEMTESTS) 83 | if (nsize > realosize && g->gcrunning) 84 | killaC_fullgc(L, 1); /* force a GC whenever possible */ 85 | #endif 86 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); 87 | if (newblock == NULL && nsize > 0) { 88 | killa_api_check(L, nsize > realosize, 89 | "realloc cannot fail when shrinking a block"); 90 | if (g->gcrunning) { 91 | killaC_fullgc(L, 1); /* try to free some memory... */ 92 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 93 | } 94 | if (newblock == NULL) 95 | killaD_throw(L, KILLA_ERRMEM); 96 | } 97 | killa_assert((nsize == 0) == (newblock == NULL)); 98 | g->GCdebt = (g->GCdebt + nsize) - realosize; 99 | #if defined(TRACEMEM) 100 | { /* auxiliary patch to monitor garbage collection. 101 | ** To plot, gnuplot with following command: 102 | ** plot TRACEMEM using 1:2 with lines, TRACEMEM using 1:3 with lines 103 | */ 104 | static unsigned long total = 0; /* our "time" */ 105 | static FILE *f = NULL; /* output file */ 106 | total++; /* "time" always grows */ 107 | if ((total % 200) == 0) { 108 | if (f == NULL) f = fopen(TRACEMEM, "w"); 109 | fprintf(f, "%lu %u %d %d\n", total, 110 | killa_gettotalbytes(g), g->GCdebt, g->gcstate * 10000); 111 | } 112 | } 113 | #endif 114 | 115 | return newblock; 116 | } 117 | 118 | -------------------------------------------------------------------------------- /src/kmem.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Interface to Memory Manager */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KMEM_H_ 12 | #define KMEM_H_ 13 | 14 | 15 | #include 16 | 17 | #include "klimits.h" 18 | #include "killa.h" 19 | 20 | 21 | #define killaM_reallocv(L,b,on,n,e) \ 22 | ((killa_cast(size_t, (n)+1) > KILLA_MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 23 | (killaM_toobig(L), (void *)0) : \ 24 | killaM_realloc_(L, (b), (on)*(e), (n)*(e))) 25 | 26 | #define killaM_freemem(L, b, s) killaM_realloc_(L, (b), (s), 0) 27 | #define killaM_free(L, b) killaM_realloc_(L, (b), sizeof(*(b)), 0) 28 | #define killaM_freearray(L, b, n) killaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 29 | 30 | #define killaM_malloc(L,s) killaM_realloc_(L, NULL, 0, (s)) 31 | #define killaM_new(L,t) killa_cast(t *, killaM_malloc(L, sizeof(t))) 32 | #define killaM_newvector(L,n,t) \ 33 | killa_cast(t *, killaM_reallocv(L, NULL, 0, n, sizeof(t))) 34 | 35 | #define killaM_newobject(L,tag,s) killaM_realloc_(L, NULL, tag, (s)) 36 | 37 | #define killaM_growvector(L,v,nelems,size,t,limit,e) \ 38 | if ((nelems)+1 > (size)) \ 39 | ((v)=killa_cast(t *, killaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 40 | 41 | #define killaM_reallocvector(L, v,oldn,n,t) \ 42 | ((v)=killa_cast(t *, killaM_reallocv(L, v, oldn, n, sizeof(t)))) 43 | 44 | KILLAI_FUNC killa_noret killaM_toobig (killa_State *L); 45 | 46 | /* not to be called directly */ 47 | KILLAI_FUNC void *killaM_realloc_ (killa_State *L, void *block, size_t oldsize, 48 | size_t size); 49 | KILLAI_FUNC void *killaM_growaux_ (killa_State *L, void *block, int *size, 50 | size_t size_elem, int limit, 51 | const char *what); 52 | 53 | #endif 54 | 55 | -------------------------------------------------------------------------------- /src/kparser.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Killa Parser */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KPARSER_H_ 12 | #define KPARSER_H_ 13 | 14 | #include "klimits.h" 15 | #include "kobject.h" 16 | #include "kzio.h" 17 | 18 | 19 | /* 20 | * Expression descriptor 21 | */ 22 | 23 | typedef enum { 24 | VVOID, /* no value */ 25 | VUNDEFINED, 26 | VNULL, 27 | VTRUE, 28 | VFALSE, 29 | VK, /* info = index of constant in `k' */ 30 | VKNUM, /* nval = numerical value */ 31 | VNONRELOC, /* info = result register */ 32 | VLOCAL, /* info = local register */ 33 | VUPVAL, /* info = index of upvalue in 'upvalues' */ 34 | VINDEXED, /* t = table register/upvalue; idx = index R/K */ 35 | VJMP, /* info = instruction pc */ 36 | VRELOCABLE, /* info = instruction pc */ 37 | VCALL, /* info = instruction pc */ 38 | VVARARG /* info = instruction pc */ 39 | } killa_expkind; 40 | 41 | 42 | #define killa_vkisvar(k) (VLOCAL <= (k) && (k) <= VINDEXED) 43 | #define killa_vkisinreg(k) ((k) == VNONRELOC || (k) == VLOCAL) 44 | 45 | typedef struct killa_expdesc { 46 | killa_expkind k; 47 | union { 48 | struct { /* for indexed variables (VINDEXED) */ 49 | short idx; /* index (R/K) */ 50 | killa_ubyte t; /* table (register or upvalue) */ 51 | killa_ubyte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ 52 | } ind; 53 | int info; /* for generic use */ 54 | killa_Number nval; /* for VKNUM */ 55 | } u; 56 | int t; /* patch list of `exit when true' */ 57 | int f; /* patch list of `exit when false' */ 58 | } killa_expdesc; 59 | 60 | 61 | /* description of active local variable */ 62 | typedef struct killa_Vardesc { 63 | short idx; /* variable index in stack */ 64 | } killa_Vardesc; 65 | 66 | 67 | /* description of pending goto statements and label statements */ 68 | typedef struct killa_Labeldesc { 69 | killa_TString *name; /* label identifier */ 70 | int pc; /* position in code */ 71 | int line; /* line where it appeared */ 72 | killa_ubyte nactvar; /* local level where it appears in current block */ 73 | } killa_Labeldesc; 74 | 75 | 76 | /* list of labels or gotos */ 77 | typedef struct killa_Labellist { 78 | killa_Labeldesc *arr; /* array */ 79 | int n; /* number of entries in use */ 80 | int size; /* array size */ 81 | } killa_Labellist; 82 | 83 | 84 | /* dynamic structures used by the parser */ 85 | typedef struct killa_Dyndata { 86 | struct { /* list of active local variables */ 87 | killa_Vardesc *arr; 88 | int n; 89 | int size; 90 | } actvar; 91 | killa_Labellist gt; /* list of pending gotos */ 92 | killa_Labellist label; /* list of active labels */ 93 | } killa_Dyndata; 94 | 95 | 96 | /* control of blocks */ 97 | struct killa_BlockCnt; /* defined in lparser.c */ 98 | 99 | 100 | /* state needed to generate code for a given function */ 101 | typedef struct killa_FuncState { 102 | killa_Proto *f; /* current function header */ 103 | killa_Table *h; /* table to find (and reuse) elements in `k' */ 104 | struct killa_FuncState *prev; /* enclosing function */ 105 | struct killa_LexState *ls; /* lexical state */ 106 | struct killa_BlockCnt *bl; /* chain of current blocks */ 107 | int pc; /* next position to code (equivalent to `ncode') */ 108 | int lasttarget; /* 'label' of last 'jump label' */ 109 | int jpc; /* list of pending jumps to `pc' */ 110 | int nk; /* number of elements in `k' */ 111 | int np; /* number of elements in `p' */ 112 | int firstlocal; /* index of first local var (in killa_Dyndata array) */ 113 | short nlocvars; /* number of elements in 'f->locvars' */ 114 | killa_ubyte nactvar; /* number of active local variables */ 115 | killa_ubyte nups; /* number of upvalues */ 116 | killa_ubyte freereg; /* first free register */ 117 | } killa_FuncState; 118 | 119 | 120 | KILLAI_FUNC killa_Proto *killaY_parser (killa_State *L, killa_ZIO *z, killa_Mbuffer *buff, 121 | killa_Dyndata *dyd, const char *name, int firstchar); 122 | 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /src/kstring.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* String table (keeps all strings handled by Killa) */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | 13 | #define KILLA_CORE 14 | 15 | #include "killa.h" 16 | 17 | #include "kmem.h" 18 | #include "kobject.h" 19 | #include "kstate.h" 20 | #include "kstring.h" 21 | 22 | 23 | 24 | void killaS_resize (killa_State *L, int newsize) { 25 | int i; 26 | killa_StringTable *tb = &KILLA_G(L)->strt; 27 | /* cannot resize while GC is traversing strings */ 28 | killaC_runtilstate(L, ~killa_bitmask(killa_GCSsweepstring)); 29 | if (newsize > tb->size) { 30 | killaM_reallocvector(L, tb->hash, tb->size, newsize, killa_GCObject *); 31 | for (i = tb->size; i < newsize; i++) tb->hash[i] = NULL; 32 | } 33 | /* rehash */ 34 | for (i=0; isize; i++) { 35 | killa_GCObject *p = tb->hash[i]; 36 | tb->hash[i] = NULL; 37 | while (p) { /* for each node in the list */ 38 | killa_GCObject *next = gch(p)->next; /* save next */ 39 | unsigned int h = killa_lmod(killa_gco2ts(p)->hash, newsize); /* new position */ 40 | gch(p)->next = tb->hash[h]; /* chain it */ 41 | tb->hash[h] = p; 42 | killa_resetoldbit(p); /* see MOVE OLD rule */ 43 | p = next; 44 | } 45 | } 46 | if (newsize < tb->size) { 47 | /* shrinking slice must be empty */ 48 | killa_assert(tb->hash[newsize] == NULL && tb->hash[tb->size - 1] == NULL); 49 | killaM_reallocvector(L, tb->hash, tb->size, newsize, killa_GCObject *); 50 | } 51 | tb->size = newsize; 52 | } 53 | 54 | 55 | static killa_TString *newlstr (killa_State *L, const char *str, size_t l, 56 | unsigned int h) { 57 | size_t totalsize; /* total size of killa_TString object */ 58 | killa_GCObject **list; /* (pointer to) list where it will be inserted */ 59 | killa_TString *ts; 60 | killa_StringTable *tb = &KILLA_G(L)->strt; 61 | if (l+1 > (KILLA_MAX_SIZET - sizeof(killa_TString))/sizeof(char)) 62 | killaM_toobig(L); 63 | if (tb->nuse >= killa_cast(killa_uint32, tb->size) && tb->size <= KILLA_MAX_INT/2) 64 | killaS_resize(L, tb->size*2); /* too crowded */ 65 | totalsize = sizeof(killa_TString) + ((l + 1) * sizeof(char)); 66 | list = &tb->hash[killa_lmod(h, tb->size)]; 67 | ts = &killaC_newobj(L, KILLA_TSTRING, totalsize, list, 0)->ts; 68 | ts->tsv.len = l; 69 | ts->tsv.hash = h; 70 | ts->tsv.reserved = 0; 71 | memcpy(ts+1, str, l*sizeof(char)); 72 | ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 73 | tb->nuse++; 74 | return ts; 75 | } 76 | 77 | 78 | killa_TString *killaS_newlstr (killa_State *L, const char *str, size_t l) { 79 | killa_GCObject *o; 80 | unsigned int h = killa_cast(unsigned int, l); /* seed */ 81 | size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 82 | size_t l1; 83 | for (l1=l; l1>=step; l1-=step) /* compute hash */ 84 | h = h ^ ((h<<5)+(h>>2)+killa_cast(unsigned char, str[l1-1])); 85 | for (o = KILLA_G(L)->strt.hash[killa_lmod(h, KILLA_G(L)->strt.size)]; 86 | o != NULL; 87 | o = gch(o)->next) { 88 | killa_TString *ts = killa_rawgco2ts(o); 89 | if (h == ts->tsv.hash && 90 | ts->tsv.len == l && 91 | (memcmp(str, killa_getstr(ts), l * sizeof(char)) == 0)) { 92 | if (killa_isdead(KILLA_G(L), o)) /* string is dead (but was not collected yet)? */ 93 | killa_changewhite(o); /* resurrect it */ 94 | return ts; 95 | } 96 | } 97 | return newlstr(L, str, l, h); /* not found; create a new string */ 98 | } 99 | 100 | 101 | killa_TString *killaS_new (killa_State *L, const char *str) { 102 | return killaS_newlstr(L, str, strlen(str)); 103 | } 104 | 105 | 106 | killa_Udata *killaS_newudata (killa_State *L, size_t s, killa_Table *e) { 107 | killa_Udata *u; 108 | if (s > KILLA_MAX_SIZET - sizeof(killa_Udata)) 109 | killaM_toobig(L); 110 | u = &killaC_newobj(L, KILLA_TUSERDATA, sizeof(killa_Udata) + s, NULL, 0)->u; 111 | u->uv.len = s; 112 | u->uv.metatable = NULL; 113 | u->uv.env = e; 114 | return u; 115 | } 116 | 117 | -------------------------------------------------------------------------------- /src/kstring.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* String table (keeps all strings handled by Killa) */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KSTRING_H_ 12 | #define KSTRING_H_ 13 | 14 | #include "kgc.h" 15 | #include "kobject.h" 16 | #include "kstate.h" 17 | 18 | 19 | #define killa_sizestring(s) (sizeof(union killa_TString)+((s)->len+1)*sizeof(char)) 20 | 21 | #define killa_sizeudata(u) (sizeof(union killa_Udata)+(u)->len) 22 | 23 | #define killaS_newliteral(L, s) (killaS_newlstr(L, "" s, \ 24 | (sizeof(s)/sizeof(char))-1)) 25 | 26 | #define killaS_fix(s) killa_l_setbit((s)->tsv.marked, KILLA_FIXEDBIT) 27 | 28 | 29 | /* 30 | ** as all string are internalized, string equality becomes 31 | ** pointer equality 32 | */ 33 | #define killa_eqstr(a,b) ((a) == (b)) 34 | 35 | KILLAI_FUNC void killaS_resize (killa_State *L, int newsize); 36 | KILLAI_FUNC killa_Udata *killaS_newudata (killa_State *L, size_t s, killa_Table *e); 37 | KILLAI_FUNC killa_TString *killaS_newlstr (killa_State *L, const char *str, size_t l); 38 | KILLAI_FUNC killa_TString *killaS_new (killa_State *L, const char *str); 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /src/ktable.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Killa tables (hash) */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KTABLE_H_ 12 | #define KTABLE_H_ 13 | 14 | #include "kobject.h" 15 | 16 | 17 | #define killa_gnode(t,i) (&(t)->node[i]) 18 | #define killa_gkey(n) (&(n)->i_key.tvk) 19 | #define killa_gval(n) (&(n)->i_val) 20 | #define killa_gnext(n) ((n)->i_key.nk.next) 21 | 22 | #define killa_invalidateTMcache(t) ((t)->flags = 0) 23 | 24 | 25 | KILLAI_FUNC const killa_TValue *killaH_getint (killa_Table *t, int key); 26 | KILLAI_FUNC void killaH_setint (killa_State *L, killa_Table *t, int key, killa_TValue *value); 27 | KILLAI_FUNC const killa_TValue *killaH_getstr (killa_Table *t, killa_TString *key); 28 | KILLAI_FUNC const killa_TValue *killaH_get (killa_Table *t, const killa_TValue *key); 29 | KILLAI_FUNC killa_TValue *killaH_newkey (killa_State *L, killa_Table *t, const killa_TValue *key); 30 | KILLAI_FUNC killa_TValue *killaH_set (killa_State *L, killa_Table *t, const killa_TValue *key); 31 | KILLAI_FUNC killa_Table *killaH_new (killa_State *L); 32 | KILLAI_FUNC void killaH_resize (killa_State *L, killa_Table *t, int nasize, int nhsize); 33 | KILLAI_FUNC void killaH_resizearray (killa_State *L, killa_Table *t, int nasize); 34 | KILLAI_FUNC void killaH_free (killa_State *L, killa_Table *t); 35 | KILLAI_FUNC int killaH_next (killa_State *L, killa_Table *t, killa_StkId key); 36 | KILLAI_FUNC int killaH_getn (killa_Table *t); 37 | 38 | 39 | #if defined(KILLA_DEBUG) 40 | KILLAI_FUNC killa_Node *killaH_mainposition (const killa_Table *t, const killa_TValue *key); 41 | KILLAI_FUNC int killaH_isdummy (killa_Node *n); 42 | #endif 43 | 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/ktm.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Tag methods` */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | 13 | #define KILLA_CORE 14 | 15 | #include "killa.h" 16 | 17 | #include "kobject.h" 18 | #include "kstate.h" 19 | #include "kstring.h" 20 | #include "ktable.h" 21 | #include "ktm.h" 22 | 23 | 24 | static const char udatatypename[] = "userdata"; 25 | 26 | KILLAI_DDEF const char *const killaT_typenames_[KILLA_TOTALTAGS] = { 27 | "no value", 28 | "null", "boolean", udatatypename, "number", 29 | "string", "table", "function", udatatypename, "thread", 30 | "proto", "upval" /* these last two cases are used for tests only */ 31 | }; 32 | 33 | 34 | void killaT_init (killa_State *L) { 35 | static const char *const killaT_eventname[] = { /* ORDER TM */ 36 | "__index", "__newindex", 37 | "__gc", "__mode", "__len", "__eq", 38 | "__add", "__sub", "__mul", "__div", "__mod", 39 | "__pow", 40 | "__bit_and", "__bit_or", "__bit_xor", 41 | "__bit_lsh", "__bit_rsh", "__bit_not", 42 | "__unm", "__lt", "__le", 43 | "__concat", "__call", 44 | "__cadd", "__csub", "__cmul", "__cdiv", "__cmod", "__cconcat" 45 | }; 46 | int i; 47 | for (i=0; itmname[i] = killaS_new(L, killaT_eventname[i]); 49 | killaS_fix(KILLA_G(L)->tmname[i]); /* never collect these names */ 50 | } 51 | } 52 | 53 | 54 | /* 55 | ** function to be used with macro "killa_fasttm": optimized for absence of 56 | ** tag methods 57 | */ 58 | const killa_TValue *killaT_gettm (killa_Table *events, KILLA_TMS event, killa_TString *ename) { 59 | const killa_TValue *tm = killaH_getstr(events, ename); 60 | killa_assert(event <= TM_EQ); 61 | if (killa_ttisnil(tm)) { /* no tag method? */ 62 | events->flags |= killa_cast_byte(1u<metatable; 74 | break; 75 | case KILLA_TUSERDATA: 76 | mt = killa_uvalue(o)->metatable; 77 | break; 78 | default: 79 | mt = KILLA_G(L)->mt[killa_ttypenv(o)]; 80 | } 81 | return (mt ? killaH_getstr(mt, KILLA_G(L)->tmname[event]) : killaO_nilobject); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /src/ktm.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Tag methods */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KTM_H_ 12 | #define KTM_H_ 13 | 14 | 15 | #include "kobject.h" 16 | 17 | 18 | /* 19 | * WARNING: if you change the order of this enumeration, 20 | * grep "ORDER TM" 21 | */ 22 | typedef enum { 23 | TM_INDEX, 24 | TM_NEWINDEX, 25 | TM_GC, 26 | TM_MODE, 27 | TM_LEN, 28 | TM_EQ, /* last tag method with `fast' access */ 29 | TM_ADD, 30 | TM_SUB, 31 | TM_MUL, 32 | TM_DIV, 33 | TM_MOD, 34 | TM_POW, 35 | TM_BAND, 36 | TM_BOR, 37 | TM_BXOR, 38 | TM_BLSH, 39 | TM_BRSH, 40 | TM_BNOT, 41 | TM_UNM, 42 | TM_LT, 43 | TM_LE, 44 | TM_CONCAT, 45 | TM_CALL, 46 | TM_CADD, 47 | TM_CSUB, 48 | TM_CMUL, 49 | TM_CDIV, 50 | TM_CMOD, 51 | TM_CCONCAT, 52 | TM_N /* number of elements in the enum */ 53 | } KILLA_TMS; 54 | 55 | 56 | 57 | #define killa_gfasttm(g,et,e) ((et) == NULL ? NULL : \ 58 | ((et)->flags & (1u<<(e))) ? NULL : killaT_gettm(et, e, (g)->tmname[e])) 59 | 60 | #define killa_fasttm(l,et,e) killa_gfasttm(KILLA_G(l), et, e) 61 | 62 | #define killa_ttypename(x) killaT_typenames_[(x) + 1] 63 | #define killa_objtypename(x) killa_ttypename(killa_ttypenv(x)) 64 | 65 | KILLAI_DDEC const char *const killaT_typenames_[KILLA_TOTALTAGS]; 66 | 67 | 68 | KILLAI_FUNC const killa_TValue *killaT_gettm (killa_Table *events, KILLA_TMS event, killa_TString *ename); 69 | KILLAI_FUNC const killa_TValue *killaT_gettmbyobj (killa_State *L, const killa_TValue *o, 70 | KILLA_TMS event); 71 | KILLAI_FUNC void killaT_init (killa_State *L); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/kubit.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Unsigned int bitwise operations */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* */ 6 | /* This code is licensed under the MIT license: */ 7 | /* http://www.opensource.org/licenses/mit-license.php */ 8 | /* -------------------------------------------------------------------------- */ 9 | 10 | #define KUBIT_C_ 11 | #define KILLA_CORE 12 | 13 | #include "kubit.h" 14 | 15 | killa_Number bit_and(killa_State *L, killa_Number v1, killa_Number v2) { 16 | killa_Unsigned u1,u2; 17 | killa_number2unsigned(u1,v1); 18 | killa_number2unsigned(u2,v2); 19 | return killa_unsigned2number(u1 & u2); 20 | } 21 | 22 | killa_Number bit_or(killa_State *L, killa_Number v1, killa_Number v2) { 23 | killa_Unsigned u1,u2; 24 | killa_number2unsigned(u1,v1); 25 | killa_number2unsigned(u2,v2); 26 | return killa_unsigned2number(u1 | u2); 27 | } 28 | 29 | killa_Number bit_xor(killa_State *L, killa_Number v1, killa_Number v2) { 30 | killa_Unsigned u1,u2; 31 | killa_number2unsigned(u1,v1); 32 | killa_number2unsigned(u2,v2); 33 | return killa_unsigned2number(u1 ^ u2); 34 | } 35 | 36 | killa_Number bit_lshift(killa_State *L, killa_Number v1, killa_Number v2) { 37 | killa_Unsigned u1,u2; 38 | killa_number2unsigned(u1,v1); 39 | killa_number2unsigned(u2,v2); 40 | return killa_unsigned2number(u1 << u2); 41 | } 42 | 43 | killa_Number bit_rshift(killa_State *L, killa_Number v1, killa_Number v2) { 44 | killa_Unsigned u1,u2; 45 | killa_number2unsigned(u1,v1); 46 | killa_number2unsigned(u2,v2); 47 | return killa_unsigned2number(u1 >> u2); 48 | } 49 | 50 | killa_Number bit_not(killa_State *L, killa_Number v1) { 51 | killa_Unsigned u1; 52 | killa_number2unsigned(u1, v1); 53 | return killa_unsigned2number(~ u1); 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/kubit.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Unsigned int bitwise operations */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* */ 6 | /* This code is licensed under the MIT license: */ 7 | /* http://www.opensource.org/licenses/mit-license.php */ 8 | /* -------------------------------------------------------------------------- */ 9 | 10 | #ifndef KUBIT_H_ 11 | #define KUBIT_H_ 12 | 13 | #include "klimits.h" 14 | #include "killa.h" 15 | 16 | KILLAI_FUNC killa_Number bit_and (killa_State *L, killa_Number v1, killa_Number v2); 17 | KILLAI_FUNC killa_Number bit_or (killa_State *L, killa_Number v1, killa_Number v2); 18 | KILLAI_FUNC killa_Number bit_xor (killa_State *L, killa_Number v1, killa_Number v2); 19 | KILLAI_FUNC killa_Number bit_lshift (killa_State *L, killa_Number v1, killa_Number v2); 20 | KILLAI_FUNC killa_Number bit_rshift (killa_State *L, killa_Number v1, killa_Number v2); 21 | KILLAI_FUNC killa_Number bit_not (killa_State *L, killa_Number v1); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/kundump.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Load precompiled Killa scripts */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KUNDUMP_H_ 12 | #define KUNDUMP_H_ 13 | 14 | #include "kobject.h" 15 | #include "kzio.h" 16 | 17 | /* load one chunk; from lundump.c */ 18 | KILLAI_FUNC killa_Proto* killaU_undump (killa_State* L, killa_ZIO* Z, killa_Mbuffer* buff, const char* name); 19 | 20 | /* make header; from lundump.c */ 21 | KILLAI_FUNC void killaU_header (killa_ubyte* h); 22 | 23 | /* dump one chunk; from ldump.c */ 24 | KILLAI_FUNC int killaU_dump (killa_State* L, const killa_Proto* f, killa_Writer w, void* data, int strip); 25 | 26 | /* data to catch conversion errors */ 27 | #define KILLAC_TAIL "\x19\x93\r\n\x1a\n" 28 | 29 | /* size in bytes of header of binary files */ 30 | #define KILLAC_HEADERSIZE (sizeof(KILLA_SIGNATURE)-sizeof(char)+2+6+sizeof(KILLAC_TAIL)-sizeof(char)) 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/kvm.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Killa virtual machine */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KVM_H_ 12 | #define KVM_H_ 13 | 14 | 15 | #include "kdo.h" 16 | #include "kobject.h" 17 | #include "ktm.h" 18 | 19 | 20 | #define killa_vmtostring(L,o) (killa_ttisstring(o) || (killaV_tostring(L, o))) 21 | 22 | #define killa_vmtonumber(o,n) (killa_ttisnumber(o) || (((o) = killaV_tonumber(o,n)) != NULL)) 23 | 24 | #define killa_equalobj(L,o1,o2) (killa_ttisequal(o1, o2) && killaV_equalobj_(L, o1, o2)) 25 | 26 | #define killaV_rawequalobj(t1,t2) \ 27 | (killa_ttisequal(t1,t2) && killaV_equalobj_(NULL,t1,t2)) 28 | 29 | 30 | /* not to called directly */ 31 | KILLAI_FUNC int killaV_equalobj_ (killa_State *L, const killa_TValue *t1, const killa_TValue *t2); 32 | 33 | 34 | KILLAI_FUNC int killaV_lessthan (killa_State *L, const killa_TValue *l, const killa_TValue *r); 35 | KILLAI_FUNC int killaV_lessequal (killa_State *L, const killa_TValue *l, const killa_TValue *r); 36 | KILLAI_FUNC const killa_TValue *killaV_tonumber (const killa_TValue *obj, killa_TValue *n); 37 | KILLAI_FUNC int killaV_tostring (killa_State *L, killa_StkId obj); 38 | KILLAI_FUNC void killaV_gettable (killa_State *L, const killa_TValue *t, killa_TValue *key, 39 | killa_StkId val); 40 | KILLAI_FUNC void killaV_settable (killa_State *L, const killa_TValue *t, killa_TValue *key, 41 | killa_StkId val); 42 | KILLAI_FUNC void killaV_finishOp (killa_State *L); 43 | KILLAI_FUNC void killaV_execute (killa_State *L); 44 | KILLAI_FUNC void killaV_concat (killa_State *L, int total); 45 | KILLAI_FUNC void killaV_arith (killa_State *L, killa_StkId ra, const killa_TValue *rb, 46 | const killa_TValue *rc, KILLA_TMS op); 47 | KILLAI_FUNC void killaV_objlen (killa_State *L, killa_StkId ra, const killa_TValue *rb); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/kzio.c: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* A generic input stream interface */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #include 12 | 13 | #define KILLA_CORE 14 | 15 | #include "killa.h" 16 | 17 | #include "klimits.h" 18 | #include "kmem.h" 19 | #include "kstate.h" 20 | #include "kzio.h" 21 | 22 | 23 | int killaZ_fill (killa_ZIO *z) { 24 | size_t size; 25 | killa_State *L = z->L; 26 | const char *buff; 27 | killa_unlock(L); 28 | buff = z->reader(L, z->data, &size); 29 | killa_lock(L); 30 | if (buff == NULL || size == 0) 31 | return KILLA_EOZ; 32 | z->n = size - 1; /* discount char being returned */ 33 | z->p = buff; 34 | return killa_cast_uchar(*(z->p++)); 35 | } 36 | 37 | 38 | void killaZ_init (killa_State *L, killa_ZIO *z, killa_Reader reader, void *data) { 39 | z->L = L; 40 | z->reader = reader; 41 | z->data = data; 42 | z->n = 0; 43 | z->p = NULL; 44 | } 45 | 46 | 47 | /* --------------------------------------------------------------- read --- */ 48 | size_t killaZ_read (killa_ZIO *z, void *b, size_t n) { 49 | while (n) { 50 | size_t m; 51 | if (z->n == 0) { /* no bytes in buffer? */ 52 | if (killaZ_fill(z) == KILLA_EOZ) /* try to read more */ 53 | return n; /* no more input; return number of missing bytes */ 54 | else { 55 | z->n++; /* killaZ_fill consumed first byte; put it back */ 56 | z->p--; 57 | } 58 | } 59 | m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ 60 | memcpy(b, z->p, m); 61 | z->n -= m; 62 | z->p += m; 63 | b = (char *)b + m; 64 | n -= m; 65 | } 66 | return 0; 67 | } 68 | 69 | /* ------------------------------------------------------------------------ */ 70 | char *killaZ_openspace (killa_State *L, killa_Mbuffer *buff, size_t n) { 71 | if (n > buff->buffsize) { 72 | if (n < KILLA_MINBUFFER) n = KILLA_MINBUFFER; 73 | killaZ_resizebuffer(L, buff, n); 74 | } 75 | return buff->buffer; 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /src/kzio.h: -------------------------------------------------------------------------------- 1 | /* ========================================================================== */ 2 | /* Buffered streams */ 3 | /* -------------------------------------------------------------------------- */ 4 | /* Copyright (c) 2012 Laurens Rodriguez Oscanoa. */ 5 | /* Copyright (C) 1994-2012 Lua.org, PUC-Rio. */ 6 | /* */ 7 | /* This code is licensed under the MIT license: */ 8 | /* http://www.opensource.org/licenses/mit-license.php */ 9 | /* -------------------------------------------------------------------------- */ 10 | 11 | #ifndef KZIO_H_ 12 | #define KZIO_H_ 13 | 14 | #include "killa.h" 15 | 16 | #include "kmem.h" 17 | 18 | 19 | #define KILLA_EOZ (-1) /* end of stream */ 20 | 21 | typedef struct killa_Zio killa_ZIO; 22 | 23 | #define killa_zgetc(z) (((z)->n--)>0 ? killa_cast_uchar(*(z)->p++) : killaZ_fill(z)) 24 | 25 | 26 | typedef struct killa_Mbuffer { 27 | char *buffer; 28 | size_t n; 29 | size_t buffsize; 30 | } killa_Mbuffer; 31 | 32 | #define killaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 33 | 34 | #define killaZ_buffer(buff) ((buff)->buffer) 35 | #define killaZ_sizebuffer(buff) ((buff)->buffsize) 36 | #define killaZ_bufflen(buff) ((buff)->n) 37 | 38 | #define killaZ_resetbuffer(buff) ((buff)->n = 0) 39 | 40 | 41 | #define killaZ_resizebuffer(L, buff, size) \ 42 | (killaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 43 | (buff)->buffsize = size) 44 | 45 | #define killaZ_freebuffer(L, buff) killaZ_resizebuffer(L, buff, 0) 46 | 47 | 48 | KILLAI_FUNC char *killaZ_openspace (killa_State *L, killa_Mbuffer *buff, size_t n); 49 | KILLAI_FUNC void killaZ_init (killa_State *L, killa_ZIO *z, killa_Reader reader, 50 | void *data); 51 | KILLAI_FUNC size_t killaZ_read (killa_ZIO* z, void* b, size_t n); /* read next n bytes */ 52 | 53 | 54 | 55 | /* --------- Private Part ------------------ */ 56 | 57 | struct killa_Zio { 58 | size_t n; /* bytes still unread */ 59 | const char *p; /* current position in buffer */ 60 | killa_Reader reader; /* reader function */ 61 | void* data; /* additional data */ 62 | killa_State *L; /* Killa state (for reader) */ 63 | }; 64 | 65 | 66 | KILLAI_FUNC int killaZ_fill (killa_ZIO *z); 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /test/99beers.kia: -------------------------------------------------------------------------------- 1 | 2 | //---------------------------------------------------------------------------------------- 3 | // 99 Bottles of beer: http://99-bottles-of-beer.net/lyrics.html 4 | function songOfBeers(beers) { 5 | 6 | function bottlesOfBeer(i) { return i .. ' bottles of beer' } 7 | function bottlesOnTheWall(i) { return bottlesOfBeer(i) .. ' on the wall' } 8 | function takeOneDown() { return '\nTake one down and pass it around, ' } 9 | 10 | function createVerse(i) { 11 | return function() { 12 | return bottlesOnTheWall(i) .. ', ' .. bottlesOfBeer(i) .. '.' 13 | .. takeOneDown() .. bottlesOnTheWall(i - 1) .. '.\n' 14 | } 15 | } 16 | 17 | // Create the closures that will sing the song. 18 | var verses = [] 19 | for (var i = beers; i >= 0; i -= 1) { 20 | if (i >= 2) { 21 | table.insert(verses, createVerse(i)); 22 | } 23 | else if (i == 1) { 24 | table.insert(verses, function() { 25 | return '1 bottle of beer on the wall, 1 bottle of beer.' 26 | .. takeOneDown() .. bottlesOnTheWall('no more') .. '.\n' 27 | }) 28 | } 29 | else { 30 | table.insert(verses, function() { 31 | return bottlesOnTheWall('No more') .. ', ' .. bottlesOfBeer('no more') 32 | .. '.\nGo to the store and buy some more, ' 33 | .. bottlesOnTheWall(99) .. '.\n' 34 | }) 35 | } 36 | } 37 | return verses 38 | } 39 | 40 | for each (var k, verse in ipairs(songOfBeers(99))) { 41 | print(verse()) 42 | } 43 | -------------------------------------------------------------------------------- /test/automaton.kia: -------------------------------------------------------------------------------- 1 | 2 | // A small ascii-art program in Killa (translated from Python). 3 | // Original author: Danny Yoo 4 | // See: http://www.wolframscience.com/preview/nks_pages/?NKS0032.gif 5 | // and Wolfram's "A New Kind of Science" for more information. 6 | 7 | // Wolfram's automaton uses eight rules for generating the next row of 8 | // output. Let's take a look at them. 9 | var RULE_100 = { ["111"]: 0, 10 | ["110"]: 1, 11 | ["101"]: 1, 12 | ["100"]: 0, 13 | ["011"]: 1, 14 | ["010"]: 1, 15 | ["001"]: 1, 16 | ["000"]: 0 } 17 | 18 | function drawRow(row) { 19 | for each (var _,character in ipairs(row)) { 20 | if (character == 1) { 21 | io.write('O') 22 | } 23 | else { 24 | io.write('.') 25 | } 26 | } 27 | io.write('\n') 28 | } 29 | 30 | var STARTING_ROW = {} 31 | for each (var i = 1 to 75) { 32 | if (i != 73) { 33 | table.insert(STARTING_ROW,0) 34 | } 35 | else { 36 | table.insert(STARTING_ROW,1) 37 | } 38 | } 39 | 40 | function applyRuleOnRow(rule, row) { 41 | var new_row = [0] 42 | for each (var i = 1 to $row - 2) { 43 | var triple = string.format("%s%s%s", row[i-1], row[i], row[i+1]) 44 | table.insert(new_row, rule[triple]) 45 | } 46 | table.insert(new_row, 0) 47 | return new_row 48 | } 49 | 50 | var row = STARTING_ROW 51 | for each (var i = 1 to 70) { 52 | drawRow(row) 53 | row = applyRuleOnRow(RULE_100, row) 54 | } 55 | -------------------------------------------------------------------------------- /test/decrypt.kia: -------------------------------------------------------------------------------- 1 | 2 | // The text to decipher. It was created using a substitution cipher 3 | var text = """ 4 | Fxr nb, ekb dmrdxjb xf upfb pj, ie ubije direuo, ex kizb txo. 5 | Drxlrinnbrj xfeby fbbu txo akby ekbo qiy qxyqbyerieb xy ekb qrbiepzb 6 | jpwb xf drxlrinnpyl, Jx Rmho pj wbjplybw ex nigb drxlrinnbrj kiddo. 7 | Rmho pykbrpebw ekb Dbru dkpuxjxdko xf kizpyl nxrb ekiy xyb aio 8 | ex wx ekb jinb ekpyl. P pykbrpebw ekie dkpuxjxdko frxn Uirro Aiuu, 9 | akx pj no kbrx iqemiuuo. P aiye ex nigb Rmho mjbrj frbb. P aiye ex 10 | lpzb ekbn ekb frbbwxn ex qkxxjb. Dbxdub irb wpffbrbye. Dbxdub qkxxjb 11 | wpffbrbye qrpebrpi. Hme pf ekbrb pj i hbeebr aio inxyl niyo 12 | iuebryiepzbj, P aiye ex byqxmrilb ekie aio ho nigpyl pe qxnfxreihub. 13 | Jx ekie'j akie P'zb erpbw ex wx. 14 | P aiye ex jxuzb drxhubnj P nbbe py ekb wipuo upfb ho mjpyl qxndmebrj, 15 | jx P ybbw ex arpeb drxlrinj. Ho mjpyl Rmho, P aiye ex qxyqbyerieb ekb 16 | ekpylj P wx, yxe ekb nilpqiu rmubj xf ekb uiylmilb, upgb jeirepyl apek 17 | dmhupq zxpw jxnbekpyl jxnbekpyl jxnbekpyl ex jio, "drpye kbuux axruw." 18 | P tmje aiye ex jio, "drpye ekpj!" P wxy'e aiye iuu ekb jmrrxmywpyl 19 | nilpq gboaxrwj. P tmje aiye ex qxyqbyerieb xy ekb eijg. Ekie'j ekb hijpq 20 | pwbi. Jx P kizb erpbw ex nigb Rmho qxwb qxyqpjb iyw jmqqpyqe. 21 | Omgpkprx Niejmnxex. (lxllub keed://aaa.irepni.qxn/pyez/rmhoD.kenu) 22 | """ 23 | 24 | // This is the frequency table of the language of the original text 25 | // (from more frequent to less frequent). 26 | var freqLang = "TEOIARNSHMLYGCPUDWFBVKJXQZ" 27 | 28 | // Create frequency table for the encrypted text. 29 | var len = string.len(text) 30 | var frequency = [] 31 | 32 | for (var k = 0; k < len; k += 1) { 33 | var c = string.upper(string.sub(text, k, k)); 34 | // Check if character is an uppercase letter. 35 | if (string.find(c, "%u") != null) { 36 | if (frequency[c] == null) { 37 | frequency[c] = 1 38 | } 39 | else { 40 | frequency[c] += 1 41 | } 42 | } 43 | } 44 | 45 | // Create table for sorting the text frequency. 46 | var sortFreq = {} 47 | for each (var k,v in pairs(frequency)) { 48 | table.insert(sortFreq, {key: k, value: v}) 49 | } 50 | 51 | // Sort in descending order. 52 | table.sort(sortFreq, function(a, b) { return (a.value > b.value) }) 53 | 54 | // Create dictionary for deciphering. 55 | var dic = {} 56 | var freqText = '' 57 | var index = 0 58 | 59 | for each (var _,v in pairs(sortFreq)) { 60 | freqText ..= v.key 61 | dic[v.key] = string.sub(freqLang, index, index) 62 | index += 1 63 | } 64 | 65 | // Deciphering text by replacing characters. 66 | var decrypted = '' 67 | for (var k = 0; k < len; k += 1) { 68 | var uppercase = false; 69 | var c = string.sub(text, k, k) 70 | if (string.find(c, "%u")) { 71 | uppercase = true 72 | } 73 | else { 74 | c = string.upper(c); 75 | } 76 | if (dic[c] != null) { 77 | if (uppercase) { 78 | decrypted ..= dic[c] 79 | } 80 | else { 81 | decrypted ..= string.lower(dic[c]) 82 | } 83 | } 84 | else { 85 | decrypted ..= c 86 | } 87 | } 88 | print(decrypted) 89 | -------------------------------------------------------------------------------- /test/euler_393.kia: -------------------------------------------------------------------------------- 1 | 2 | var USE_BC = true // Set this to false if you don't have bc library. 3 | var one = (USE_BC)? require("bc").number(1) : 1 4 | 5 | var IN = 1 6 | var OUT = 2 7 | 8 | // Insert board to state. 9 | // If the board is already present just increase the board counter. 10 | // Board data is stored as: [boardCount, boardArray] 11 | function insertBoard(state, boardArray, boardCount) { 12 | var boardKey = table.concat(boardArray, ',') 13 | if (state[boardKey] == null) { 14 | state[boardKey] = [boardCount, boardArray] 15 | } 16 | else { 17 | state[boardKey][0] += boardCount 18 | } 19 | } 20 | 21 | function clone(t) { 22 | var out = [] 23 | for (var k = 0; k < $t; k += 1) { 24 | out[k] = t[k] 25 | } 26 | return out 27 | } 28 | 29 | function euler_393(n) { 30 | // Initial board has no ants moving IN or OUT (all zero) 31 | var initBoard = [] 32 | for (var k = 0; k < n; k += 1) { 33 | initBoard[k] = 0 34 | } 35 | 36 | // Initial state contains only the initial board 37 | var state = {} 38 | var init = table.concat(initBoard, ',') 39 | 40 | // We are going to use a data type: [counter, board] for storing boards 41 | state[init] = [one, initBoard]; 42 | 43 | // Columns 44 | for (var col = 0; col < n; col += 1) { 45 | // Rows 46 | for (var row = 0; row < n; row += 1) { 47 | var newState = {} 48 | var newBoard 49 | for each (var _,data in pairs(state)) { 50 | var boardCount = data[0] 51 | var board = data[1] 52 | if (board[row] == 0) { 53 | if ((row < n - 1) && (col < n - 1)) { 54 | if (board[row + 1] != IN) { 55 | newBoard = clone(board) 56 | newBoard[row] = OUT 57 | newBoard[row + 1] += IN 58 | insertBoard(newState, newBoard, boardCount) 59 | } 60 | if (board[row + 1] != OUT) { 61 | newBoard = clone(board) 62 | newBoard[row] = IN 63 | newBoard[row + 1] += OUT 64 | insertBoard(newState, newBoard, boardCount) 65 | } 66 | } 67 | } 68 | else if (board[row] == IN) { 69 | if ((row < n - 1) && (board[row + 1] != IN)) { 70 | newBoard = clone(board) 71 | newBoard[row] = 0 72 | newBoard[row + 1] += IN 73 | insertBoard(newState, newBoard, boardCount) 74 | } 75 | if (col < n - 1) { 76 | insertBoard(newState, board, boardCount) 77 | } 78 | } 79 | else if (board[row] == OUT) { 80 | if ((row < n - 1) && (board[row + 1] != OUT)) { 81 | newBoard = clone(board) 82 | newBoard[row] = 0 83 | newBoard[row + 1] += OUT 84 | insertBoard(newState, newBoard, boardCount) 85 | } 86 | if (col < n - 1) { 87 | insertBoard(newState, board, boardCount) 88 | } 89 | } 90 | else { 91 | // board[row] == IN + OUT 92 | newBoard = clone(board) 93 | newBoard[row] = 0 94 | insertBoard(newState, newBoard, boardCount) 95 | } 96 | } 97 | state = newState 98 | } 99 | } 100 | return (state[init] != null)? state[init][0] : 0 101 | } 102 | 103 | var tm = os.clock() 104 | print(euler_393(10)) 105 | print("Time: ", os.clock() - tm) 106 | 107 | -------------------------------------------------------------------------------- /test/euler_393bit.kia: -------------------------------------------------------------------------------- 1 | 2 | var USE_BC = true // Set this to false if you don't have bc library. 3 | var one = (USE_BC)? require("bc").number(1) : 1 4 | 5 | var IN = 1 6 | var OUT = 2 7 | 8 | // Insert board to state. 9 | // If the board is already present just increase the board counter. 10 | function insertBoard(state, board, boardCount) { 11 | if (state[board] == null) { 12 | state[board] = boardCount 13 | } 14 | else { 15 | state[board] += boardCount 16 | } 17 | } 18 | 19 | function setRow(board, row, value) { 20 | return (board & ~(3 << 2 * row)) | (value << 2 * row); 21 | } 22 | 23 | function addRow(board, row, value) { 24 | return board | (value << 2 * row); 25 | } 26 | 27 | function getRow(board, row) { 28 | return (board & (3 << 2 * row)) >> 2 * row; 29 | } 30 | 31 | //function setRow(board, row, value) { 32 | // board = bit32.band(board, bit32.bnot(bit32.lshift(3, 2 * row))); 33 | // return bit32.bor(board, bit32.lshift(value, 2 * row)); 34 | //} 35 | // 36 | //function addRow(board, row, value) { 37 | // return bit32.bor(board, bit32.lshift(value, 2 * row)); 38 | //} 39 | // 40 | //function getRow(board, row) { 41 | // return bit32.rshift(bit32.band(board, bit32.lshift(3, 2 * row)), 2 * row); 42 | //} 43 | 44 | //function setRow(board, row, value) { 45 | // return bit32.replace(board, value, 2 * row, 2) 46 | //} 47 | // 48 | //function addRow(board, row, value) { 49 | // return bit32.bor(board, bit32.lshift(value, 2 * row)); 50 | //} 51 | // 52 | //function getRow(board, row) { 53 | // return bit32.extract(board, 2 * row, 2); 54 | //} 55 | 56 | function printBoard(board, n) { 57 | var s = "" 58 | for (var k = 0; k < n; k += 1) { 59 | s ..= (board & 3) 60 | board = board >> 2 61 | } 62 | print(s) 63 | } 64 | 65 | function euler_393(n) { 66 | // Initial board has no ants moving IN or OUT (all bits zero) 67 | // Initial state contains only the initial board. 68 | var state = {} 69 | state[0] = one; 70 | 71 | // Columns 72 | for (var col = 0; col < n; col += 1) { 73 | // Rows 74 | for (var row = 0; row < n; row += 1) { 75 | var newState = {} 76 | var newBoard 77 | for each (var board, boardCount in pairs(state)) { 78 | // Every cell uses 2 bits. 79 | var cell = getRow(board, row); 80 | if (cell == 0) { 81 | if ((row < n - 1) && (col < n - 1)) { 82 | var cellBelow = getRow(board, row + 1); 83 | if (cellBelow != IN) { 84 | newBoard = setRow(board, row, OUT) 85 | newBoard = addRow(newBoard, row + 1, IN) 86 | insertBoard(newState, newBoard, boardCount) 87 | } 88 | if (cellBelow != OUT) { 89 | newBoard = setRow(board, row, IN) 90 | newBoard = addRow(newBoard, row + 1, OUT) 91 | insertBoard(newState, newBoard, boardCount) 92 | } 93 | } 94 | } 95 | else if (cell == IN) { 96 | if ((row < n - 1) && (getRow(board, row + 1) != IN)) { 97 | newBoard = setRow(board, row, 0) 98 | newBoard = addRow(newBoard, row + 1, IN) 99 | insertBoard(newState, newBoard, boardCount) 100 | } 101 | if (col < n - 1) { 102 | insertBoard(newState, board, boardCount) 103 | } 104 | } 105 | else if (cell == OUT) { 106 | if ((row < n - 1) && (getRow(board, row + 1) != OUT)) { 107 | newBoard = setRow(board, row, 0) 108 | newBoard = addRow(newBoard, row + 1, OUT) 109 | insertBoard(newState, newBoard, boardCount) 110 | } 111 | if (col < n - 1) { 112 | insertBoard(newState, board, boardCount) 113 | } 114 | } 115 | else { 116 | // cell == IN + OUT 117 | newBoard = setRow(board, row, 0) 118 | insertBoard(newState, newBoard, boardCount) 119 | } 120 | } 121 | state = newState 122 | } 123 | } 124 | return (state[0] != null)? state[0] : 0 125 | } 126 | 127 | var tm = os.clock() 128 | print(euler_393(10)) 129 | print("Time: ", os.clock() - tm) 130 | -------------------------------------------------------------------------------- /test/strings.kia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/test/strings.kia -------------------------------------------------------------------------------- /test/test.kia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ex/Killa/d0fb8f930b3271d0cf94539fbf4c1efddb25b1e2/test/test.kia -------------------------------------------------------------------------------- /test/test.out: -------------------------------------------------------------------------------- 1 | 1 2 3 2 | 100 200 300 3 3 | gg null 4 | private x private y 5 | global f() 6 | local f() 7 | 0) 100 8 | 1) 200 9 | 2) 300 10 | 0) 100 11 | 1) 200 12 | 2) 300 13 | 3 14 | 0) 100 15 | 1) 200 16 | 2 17 | 3 18 | 0) 100 19 | 1) 200 20 | 2) 700 21 | 200,100 22 | 0] 100 23 | 1] 200 24 | 2] 700 25 | 89 26 | 120 27 | found a Pythagorean triple: 4 3 5 28 | now trying next z... 29 | found a Pythagorean triple: 8 6 10 30 | now trying next z... 31 | 2) 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 32 | 3) 1 3 9 27 81 243 729 2187 6561 19683 59049 177147 531441 33 | 4) 1 4 16 64 256 1024 4096 16384 65536 262144 34 | 5) 1 5 25 125 625 3125 15625 78125 390625 35 | 6) 1 6 36 216 1296 7776 46656 279936 36 | 7) 1 7 49 343 2401 16807 117649 823543 37 | 8) 1 8 64 512 4096 32768 262144 38 | 9) 1 9 81 729 6561 59049 531441 39 | 10 40 | 20 41 | 30 42 | 40 43 | 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97 44 | 2^0 = 1 45 | 2^1 = 2 46 | 2^2 = 4 47 | 2^3 = 8 48 | 2^4 = 16 49 | 2^5 = 32 50 | 2^6 = 64 51 | 2^7 = 128 52 | 2^8 = 256 53 | 2^9 = 512 54 | - 5 Buzz 55 | - 6 Fizz 56 | - 7 57 | - 8 58 | - 9 Fizz 59 | - 10 Buzz 60 | - 11 61 | - 12 Fizz 62 | - 13 63 | - 14 64 | - 15 FizzBuzz 65 | 1: 0 66 | 2: 1 67 | 3: 1 68 | 4: 2 69 | 5: 3 70 | 6: 5 71 | 7: 8 72 | 8: 13 73 | 9: 21 74 | 10: 34 75 | 11: 55 76 | 12: 89 77 | 13: 144 78 | 14: 233 79 | 15: 377 80 | 16: 610 81 | 17: 987 82 | 18: 1597 83 | 19: 2584 84 | 20: 4181 85 | 10 86 | 12 87 | 11 88 | 10 89 | closures[0](): 21 90 | closures[0](): 22 91 | closures[0](): 23 92 | closures[1](): 21 93 | closures[0](): 204 94 | closures[1](): 202 95 | trueExpr 96 | even 97 | 30 98 | null 99 | 4 bottles of beer on the wall, 4 bottles of beer. 100 | Take one down and pass it around, 3 bottles of beer on the wall. 101 | 102 | 3 bottles of beer on the wall, 3 bottles of beer. 103 | Take one down and pass it around, 2 bottles of beer on the wall. 104 | 105 | 2 bottles of beer on the wall, 2 bottles of beer. 106 | Take one down and pass it around, 1 bottles of beer on the wall. 107 | 108 | 1 bottle of beer on the wall, 1 bottle of beer. 109 | Take one down and pass it around, no more bottles of beer on the wall. 110 | 111 | No more bottles of beer on the wall, no more bottles of beer. 112 | Go to the store and buy some more, 99 bottles of beer on the wall. 113 | 114 | 3.1415976535648 115 | 3.1415926535898 116 | -------------------------------------------------------------------------------- /test/to_string.kia: -------------------------------------------------------------------------------- 1 | 2 | function table_print(tt, done) { 3 | done = done || {} 4 | if (type(tt) == "table") { 5 | var sb = {} 6 | for each (var key, value in pairs(tt)) { 7 | if ("number" == type(key)) { 8 | table.insert(sb, string.format("%s, ", to_string(value, done))) 9 | } 10 | else if (type(value) == "table" && !done[value]) { 11 | done[value] = true 12 | table.insert(sb, to_string(key)); 13 | table.insert(sb, ": "); 14 | table.insert(sb, table_print(value, done)) 15 | table.insert(sb, "; "); 16 | } 17 | else { 18 | table.insert(sb, string.format("%s: %s; ", 19 | to_string(key), 20 | to_string(value))) 21 | } 22 | } 23 | return '{'..table.concat(sb)..'}' 24 | } 25 | else { 26 | return tt 27 | } 28 | } 29 | 30 | public function to_string(tbl) { 31 | if ("null" == type(tbl)) { 32 | return "null" 33 | } 34 | else if ("table" == type(tbl)) { 35 | return table_print(tbl) 36 | } 37 | else if ("string" == type(tbl)) { 38 | return '"'..tbl..'"' 39 | } 40 | else { 41 | return tostring(tbl) 42 | } 43 | } 44 | --------------------------------------------------------------------------------