├── .editorconfig ├── .gitignore └── README.rst /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*.rst] 4 | insert_final_newline = true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ==================== 2 | Read Lua Source Code 3 | ==================== 4 | 5 | Reading Lua_ code is fairly easy. `The language is simple`_, 6 | `well documented`_, has a `official book`_, a `package manager`_ 7 | and plenty of resources online. 8 | 9 | The thing is, there is not a lot of resources explaining 10 | how to read the lua source code, which is a work of 11 | art on its own and a great way to study low level 12 | software design. This project attempts to put 13 | together suggestions and material that might 14 | help you read the lua source code in a timely 15 | fashion. 16 | 17 | The material available here is mostly the work and 18 | suggestions of other people compiled in a single 19 | place. Few free to point out if something is 20 | mentioned without a source or send merge requests 21 | with more material. 22 | 23 | .. _official book: https://www.lua.org/docs.html#books 24 | .. _The language is simple: https://devhints.io/lua 25 | .. _well documented: https://www.lua.org/docs.html 26 | 27 | ------------------------ 28 | What Do You Need To Know 29 | ------------------------ 30 | 31 | The Lua source code is written in `ANSI C`_, a low level, statically 32 | typed programming language very common as a base for 33 | software that should live very close to the hardware. 34 | 35 | So, the first thing you should learn is the C language. Below 36 | there are some resources you could try these: 37 | 38 | Online material 39 | =============== 40 | 41 | - `learn-c`_ 42 | - `coursera c-programming`_ 43 | - `guru99 c-programming-tutorial`_ 44 | - `programiz c-programming`_ 45 | - `tutorialspoint c programming`_ 46 | - `ucam teaching C`_ 47 | - `ravi lua5.3 bytecode reference`_ 48 | - `ravi lua parsing and code generation`_ 49 | 50 | .. _ravi lua5.3 bytecode reference: https://the-ravi-programming-language.readthedocs.io/en/latest/lua_bytecode_reference.html 51 | .. _ravi lua parsing and code generation: https://the-ravi-programming-language.readthedocs.io/en/latest/lua-parser.html 52 | 53 | Books 54 | ===== 55 | 56 | - "C Programming Absolute Beginner's Guide" by Greg Perry and Dean Mille 57 | - "The C Programming Language" by Brain W. 58 | - "The C Programming Language" by Kernighan 59 | - "C Programming: A Modern Approach" by Taschenbuch 60 | 61 | List compiled from guru99_. 62 | 63 | .. _guru99: https://www.guru99.com 64 | 65 | Articles 66 | ======== 67 | 68 | - `A No-Frills Introduction to Lua 5.1 VM Instructions`_ 69 | - `The Implementation of Lua 5.0`_ 70 | 71 | .. _The Implementation of Lua 5.0: https://www.researchgate.net/profile/Roberto_Ierusalimschy/publication/220349489_The_Implementation_of_Lua_50/links/02e7e52403a963d32c000000.pdf 72 | .. _A No-Frills Introduction to Lua 5.1 VM Instructions: http://luaforge.net/docman/83/98/ANoFrillsIntroToLua51VMInstructions.pdf 73 | 74 | -------------- 75 | Where To Start 76 | -------------- 77 | 78 | Know where to start is very important, because you need to build the 79 | knowledge in your mind to put all the pieces together. Mimemike 80 | published a suggestion in `this reddit post`_ that I'll copy below: 81 | 82 | - lmathlib.c, lstrlib.c: get familiar with the external C API. Don't bother with the pattern matcher though. Just the easy functions. 83 | - lapi.c: Check how the API is implemented internally. Only skim this to get a feeling for the code. Cross-reference to lua.h and luaconf.h as needed. 84 | - lobject.h: tagged values and object representation. skim through this first. you'll want to keep a window with this file open all the time. 85 | - lstate.h: state objects. ditto. 86 | - lopcodes.h: bytecode instruction format and opcode definitions. easy. 87 | - lvm.c: scroll down to luaV_execute, the main interpreter loop. see how all of the instructions are implemented. skip the details for now. reread later. 88 | - ldo.c: calls, stacks, exceptions, coroutines. tough read. 89 | - lstring.c: string interning. cute, huh? 90 | - ltable.c: hash tables and arrays. tricky code. 91 | - ltm.c: metamethod handling, reread all of lvm.c now. 92 | - You may want to reread lapi.c now. 93 | - ldebug.c: surprise waiting for you. abstract interpretation is used to find object names for tracebacks. does bytecode verification, too. 94 | - lparser.c, lcode.c: recursive descent parser, targetting a register-based VM. start from chunk() and work your way through. read the expression parser and the code generator parts last. 95 | - lgc.c: incremental garbage collector. take your time. 96 | - Read all the other files as you see references to them. Don't let your stack get too deep though. 97 | 98 | Happy reading! Please, share your results and suggestions! 99 | 100 | .. _ANSI C: https://en.wikipedia.org/wiki/C_(programming_language) 101 | .. _coursera c-programming: https://coursera.org/specializations/c-programming 102 | .. _guru99 c-programming-tutorial: https://www.guru99.com/c-programming-tutorial.html 103 | .. _learn-c: https://www.learn-c.org/ 104 | .. _Lua: https://www.lua.org/ 105 | .. _package manager: https://luarocks.org/ 106 | .. _programiz c-programming: https://www.programiz.com/c-programming 107 | .. _this reddit post: https://www.reddit.com/r/programming/comments/63hth/ask_reddit_which_oss_codebases_out_there_are_so/c02pxbp/ 108 | .. _tutorialspoint c programming: https://www.tutorialspoint.com/cprogramming/index.htm 109 | .. _ucam teaching C: http://www-h.eng.cam.ac.uk/help/tpl/languages/C/teaching_C/teaching_C.html 110 | --------------------------------------------------------------------------------