├── C02 ├── dummylua-2-1 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ └── luastate.h │ ├── compiler │ │ └── TODO.txt │ ├── main.c │ ├── test │ │ ├── p1_test.c │ │ └── p1_test.h │ └── vm │ │ ├── luado.c │ │ └── luado.h ├── dummylua-2-2 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ └── luastate.h │ ├── compiler │ │ └── TODO.txt │ ├── main.c │ ├── test │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ └── p2_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luagc.c │ │ └── luagc.h ├── dummylua-2-3 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ └── luatable.h │ ├── compiler │ │ └── TODO.txt │ ├── main.c │ ├── test │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ └── p3_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luavm.c │ │ └── luavm.h └── dummylua-2-4 │ ├── CMakeLists.txt │ ├── clib │ ├── luaaux.c │ └── luaaux.h │ ├── common │ ├── lua.h │ ├── luamem.c │ ├── luamem.h │ ├── luaobject.c │ ├── luaobject.h │ ├── luastate.c │ ├── luastate.h │ ├── luastring.c │ ├── luastring.h │ ├── luatable.c │ └── luatable.h │ ├── compiler │ └── TODO.txt │ ├── main.c │ ├── test │ ├── p1_test.c │ ├── p1_test.h │ ├── p2_test.c │ ├── p2_test.h │ ├── p3_test.c │ ├── p3_test.h │ ├── p4_test.c │ └── p4_test.h │ └── vm │ ├── luado.c │ ├── luado.h │ ├── luagc.c │ ├── luagc.h │ ├── luavm.c │ └── luavm.h ├── C03 └── dummylua-3 │ ├── CMakeLists.txt │ ├── clib │ ├── luaaux.c │ └── luaaux.h │ ├── common │ ├── lua.h │ ├── luabase.c │ ├── luabase.h │ ├── luainit.c │ ├── luamem.c │ ├── luamem.h │ ├── luaobject.c │ ├── luaobject.h │ ├── luastate.c │ ├── luastate.h │ ├── luastring.c │ ├── luastring.h │ ├── luatable.c │ └── luatable.h │ ├── compiler │ ├── TODO.txt │ ├── luacode.c │ ├── luacode.h │ ├── lualexer.c │ ├── lualexer.h │ ├── luaparser.c │ ├── luaparser.h │ ├── luazio.c │ └── luazio.h │ ├── main.c │ ├── scripts │ ├── parser_test01.lua │ ├── part05_test.lua │ └── token_test.lua │ ├── test │ ├── p1_test.c │ ├── p1_test.h │ ├── p2_test.c │ ├── p2_test.h │ ├── p3_test.c │ ├── p3_test.h │ ├── p4_test.c │ ├── p4_test.h │ ├── p5_test.c │ └── p5_test.h │ └── vm │ ├── luado.c │ ├── luado.h │ ├── luafunc.c │ ├── luafunc.h │ ├── luagc.c │ ├── luagc.h │ ├── luaopcodes.c │ ├── luaopcodes.h │ ├── luavm.c │ └── luavm.h ├── C04 ├── dummylua-4-1 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ └── luatable.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ └── p7_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h ├── dummylua-4-2 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ └── luatable.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ └── p7_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h └── dummylua-4-3 │ ├── CMakeLists.txt │ ├── clib │ ├── luaaux.c │ └── luaaux.h │ ├── common │ ├── lua.h │ ├── luabase.c │ ├── luabase.h │ ├── luainit.c │ ├── luamem.c │ ├── luamem.h │ ├── luaobject.c │ ├── luaobject.h │ ├── luastate.c │ ├── luastate.h │ ├── luastring.c │ ├── luastring.h │ ├── luatable.c │ └── luatable.h │ ├── compiler │ ├── TODO.txt │ ├── luacode.c │ ├── luacode.h │ ├── lualexer.c │ ├── lualexer.h │ ├── luaparser.c │ ├── luaparser.h │ ├── luazio.c │ └── luazio.h │ ├── main.c │ ├── scripts │ ├── parser_test01.lua │ ├── part05_test.lua │ ├── part06_test.lua │ ├── part07_test.lua │ ├── part08_test.lua │ └── token_test.lua │ ├── test │ ├── p1_test.c │ ├── p1_test.h │ ├── p2_test.c │ ├── p2_test.h │ ├── p3_test.c │ ├── p3_test.h │ ├── p4_test.c │ ├── p4_test.h │ ├── p5_test.c │ ├── p5_test.h │ ├── p6_test.c │ ├── p6_test.h │ ├── p7_test.c │ ├── p7_test.h │ ├── p8_test.c │ └── p8_test.h │ └── vm │ ├── luado.c │ ├── luado.h │ ├── luafunc.c │ ├── luafunc.h │ ├── luagc.c │ ├── luagc.h │ ├── luaopcodes.c │ ├── luaopcodes.h │ ├── luavm.c │ └── luavm.h ├── C05 ├── dummylua-5-1 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luadebug.c │ │ ├── luadebug.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ ├── luatable.h │ │ ├── luatm.c │ │ └── luatm.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── debug_test.lua │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ ├── part08_test.lua │ │ ├── part09_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ ├── p7_test.h │ │ ├── p8_test.c │ │ ├── p8_test.h │ │ ├── p9_test.c │ │ └── p9_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h ├── dummylua-5-2 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luadebug.c │ │ ├── luadebug.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ ├── luatable.h │ │ ├── luatm.c │ │ └── luatm.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── debug_test.lua │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ ├── part08_test.lua │ │ ├── part09_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p10_test.c │ │ ├── p10_test.h │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ ├── p7_test.h │ │ ├── p8_test.c │ │ ├── p8_test.h │ │ ├── p9_test.c │ │ └── p9_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h ├── dummylua-5-3 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luadebug.c │ │ ├── luadebug.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ ├── luatable.h │ │ ├── luatm.c │ │ └── luatm.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── debug_test.lua │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ ├── part08_test.lua │ │ ├── part09_test.lua │ │ ├── part11_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p10_test.c │ │ ├── p10_test.h │ │ ├── p11_test.c │ │ ├── p11_test.h │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ ├── p7_test.h │ │ ├── p8_test.c │ │ ├── p8_test.h │ │ ├── p9_test.c │ │ └── p9_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h ├── dummylua-5-4 │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luadebug.c │ │ ├── luadebug.h │ │ ├── luainit.c │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ ├── luatable.h │ │ ├── luatm.c │ │ └── luatm.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── main.c │ ├── scripts │ │ ├── debug_test.lua │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ ├── part08_test.lua │ │ ├── part09_test.lua │ │ ├── part11_test.lua │ │ ├── part12_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p10_test.c │ │ ├── p10_test.h │ │ ├── p11_test.c │ │ ├── p11_test.h │ │ ├── p12_test.c │ │ ├── p12_test.h │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ ├── p7_test.h │ │ ├── p8_test.c │ │ ├── p8_test.h │ │ ├── p9_test.c │ │ └── p9_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h └── dummylua-5-5 │ ├── 3rd │ ├── loadlib.dll │ └── loadlib.so │ ├── CMakeLists.txt │ ├── clib │ ├── luaaux.c │ └── luaaux.h │ ├── common │ ├── lua.h │ ├── luabase.c │ ├── luabase.h │ ├── luadebug.c │ ├── luadebug.h │ ├── luainit.c │ ├── lualoadlib.c │ ├── lualoadlib.h │ ├── luamem.c │ ├── luamem.h │ ├── luaobject.c │ ├── luaobject.h │ ├── luastate.c │ ├── luastate.h │ ├── luastring.c │ ├── luastring.h │ ├── luatable.c │ ├── luatable.h │ ├── luatm.c │ └── luatm.h │ ├── compiler │ ├── TODO.txt │ ├── luacode.c │ ├── luacode.h │ ├── lualexer.c │ ├── lualexer.h │ ├── luaparser.c │ ├── luaparser.h │ ├── luazio.c │ └── luazio.h │ ├── loadlib.c │ ├── main.c │ ├── scripts │ ├── debug_test.lua │ ├── p13_require_test.lua │ ├── p13_test_return_table.lua │ ├── parser_test01.lua │ ├── part05_test.lua │ ├── part06_test.lua │ ├── part07_test.lua │ ├── part08_test.lua │ ├── part09_test.lua │ ├── part11_test.lua │ ├── part12_test.lua │ ├── part13_test.lua │ └── token_test.lua │ ├── test │ ├── p10_test.c │ ├── p10_test.h │ ├── p11_test.c │ ├── p11_test.h │ ├── p12_test.c │ ├── p12_test.h │ ├── p13_test.c │ ├── p13_test.h │ ├── p1_test.c │ ├── p1_test.h │ ├── p2_test.c │ ├── p2_test.h │ ├── p3_test.c │ ├── p3_test.h │ ├── p4_test.c │ ├── p4_test.h │ ├── p5_test.c │ ├── p5_test.h │ ├── p6_test.c │ ├── p6_test.h │ ├── p7_test.c │ ├── p7_test.h │ ├── p8_test.c │ ├── p8_test.h │ ├── p9_test.c │ └── p9_test.h │ └── vm │ ├── luado.c │ ├── luado.h │ ├── luafunc.c │ ├── luafunc.h │ ├── luagc.c │ ├── luagc.h │ ├── luaopcodes.c │ ├── luaopcodes.h │ ├── luavm.c │ └── luavm.h ├── C06 └── tetris │ ├── 3rd │ ├── clibs │ │ ├── d2d.dll │ │ └── d2d1.dll │ └── d2d │ │ ├── CMakeLists.txt │ │ ├── main.cpp │ │ ├── render.cpp │ │ └── render.h │ ├── dummylua │ ├── 3rd │ │ ├── loadlib.dll │ │ └── loadlib.so │ ├── CMakeLists.txt │ ├── clib │ │ ├── luaaux.c │ │ └── luaaux.h │ ├── common │ │ ├── lua.h │ │ ├── luabase.c │ │ ├── luabase.h │ │ ├── luadebug.c │ │ ├── luadebug.h │ │ ├── luainit.c │ │ ├── lualoadlib.c │ │ ├── lualoadlib.h │ │ ├── luamem.c │ │ ├── luamem.h │ │ ├── luaobject.c │ │ ├── luaobject.h │ │ ├── luastate.c │ │ ├── luastate.h │ │ ├── luastring.c │ │ ├── luastring.h │ │ ├── luatable.c │ │ ├── luatable.h │ │ ├── luatm.c │ │ └── luatm.h │ ├── compiler │ │ ├── TODO.txt │ │ ├── luacode.c │ │ ├── luacode.h │ │ ├── lualexer.c │ │ ├── lualexer.h │ │ ├── luaparser.c │ │ ├── luaparser.h │ │ ├── luazio.c │ │ └── luazio.h │ ├── dummylua.lib │ ├── main.c │ ├── scripts │ │ ├── debug_test.lua │ │ ├── p13_require_test.lua │ │ ├── p13_test_return_table.lua │ │ ├── parser_test01.lua │ │ ├── part05_test.lua │ │ ├── part06_test.lua │ │ ├── part07_test.lua │ │ ├── part08_test.lua │ │ ├── part09_test.lua │ │ ├── part11_test.lua │ │ ├── part12_test.lua │ │ ├── part13_test.lua │ │ └── token_test.lua │ ├── test │ │ ├── p10_test.c │ │ ├── p10_test.h │ │ ├── p11_test.c │ │ ├── p11_test.h │ │ ├── p12_test.c │ │ ├── p12_test.h │ │ ├── p13_test.c │ │ ├── p13_test.h │ │ ├── p1_test.c │ │ ├── p1_test.h │ │ ├── p2_test.c │ │ ├── p2_test.h │ │ ├── p3_test.c │ │ ├── p3_test.h │ │ ├── p4_test.c │ │ ├── p4_test.h │ │ ├── p5_test.c │ │ ├── p5_test.h │ │ ├── p6_test.c │ │ ├── p6_test.h │ │ ├── p7_test.c │ │ ├── p7_test.h │ │ ├── p8_test.c │ │ ├── p8_test.h │ │ ├── p9_test.c │ │ └── p9_test.h │ └── vm │ │ ├── luado.c │ │ ├── luado.h │ │ ├── luafunc.c │ │ ├── luafunc.h │ │ ├── luagc.c │ │ ├── luagc.h │ │ ├── luaopcodes.c │ │ ├── luaopcodes.h │ │ ├── luavm.c │ │ └── luavm.h │ └── game │ ├── CMakeLists.txt │ ├── logic.cpp │ ├── logic.h │ ├── main.cpp │ └── modules │ ├── const.lua │ ├── controller.lua │ ├── core │ ├── class.lua │ ├── object.lua │ └── render.lua │ ├── logic │ ├── block │ │ ├── base.lua │ │ ├── lshape.lua │ │ ├── square.lua │ │ ├── stick.lua │ │ ├── tshape.lua │ │ └── zshape.lua │ ├── blockmgr.lua │ └── board.lua │ ├── start.lua │ └── ui │ ├── mgr.lua │ ├── score.lua │ └── tips.lua ├── LICENSE └── README.md /C02/dummylua-2-1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luamem.c common/luaobject.c common/luastate.c) 7 | set(CLIB_SRC clib/luaaux.c) 8 | set(VM_SRC vm/luado.c) 9 | set(TEST_SRC test/p1_test.c) 10 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC}) 11 | 12 | # add the executable 13 | add_executable(dummylua main.c ${SRC}) 14 | 15 | IF (WIN32) 16 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 17 | ENDIF() 18 | 19 | target_include_directories(dummylua PUBLIC 20 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 21 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 22 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 23 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 24 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 25 | ) -------------------------------------------------------------------------------- /C02/dummylua-2-1/common/luamem.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luamem_h 22 | #define luamem_h 23 | 24 | #include "luastate.h" 25 | 26 | void* luaM_realloc(struct lua_State* L, void* ptr, size_t osize, size_t nsize); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /C02/dummylua-2-1/common/luaobject.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luaobject.h" 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-1/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-1/compiler/TODO.txt -------------------------------------------------------------------------------- /C02/dummylua-2-1/main.c: -------------------------------------------------------------------------------- 1 | #include "clib/luaaux.h" 2 | #include "test/p1_test.h" 3 | 4 | int main(int argc, char** argv) { 5 | printf("\n--------------------test case 1--------------------------\n"); 6 | p1_test_result01(); 7 | printf("\n--------------------test case 2--------------------------\n"); 8 | p1_test_result02(); 9 | printf("\n--------------------test case 3--------------------------\n"); 10 | p1_test_result03(); 11 | printf("\n--------------------test case 4--------------------------\n"); 12 | p1_test_result04(); 13 | printf("\n--------------------test case 5--------------------------\n"); 14 | p1_test_result05(); 15 | printf("\n--------------------test case 6--------------------------\n"); 16 | p1_test_result06(); 17 | printf("\n--------------------test case 7--------------------------\n"); 18 | p1_test_result07(); 19 | printf("\n--------------------test case 8--------------------------\n"); 20 | p1_test_result08(); 21 | printf("\n--------------------test case 9--------------------------\n"); 22 | p1_test_result09(); 23 | printf("\n--------------------test case 10-------------------------\n"); 24 | p1_test_result10(); 25 | printf("\n--------------------test case 11-------------------------\n"); 26 | p1_test_nestcall01(); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /C02/dummylua-2-1/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luamem.c common/luaobject.c common/luastate.c) 7 | set(CLIB_SRC clib/luaaux.c) 8 | set(VM_SRC vm/luado.c vm/luagc.c) 9 | set(TEST_SRC test/p1_test.c test/p2_test.c) 10 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC}) 11 | 12 | # add the executable 13 | add_executable(dummylua main.c ${SRC}) 14 | 15 | IF (WIN32) 16 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 17 | ENDIF() 18 | 19 | target_include_directories(dummylua PUBLIC 20 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 21 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 22 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 23 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 24 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 25 | ) -------------------------------------------------------------------------------- /C02/dummylua-2-2/common/luamem.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luamem_h 22 | #define luamem_h 23 | 24 | #include "luastate.h" 25 | 26 | #define luaM_free(L, ptr, osize) luaM_realloc(L, ptr, osize, 0) 27 | 28 | void* luaM_realloc(struct lua_State* L, void* ptr, size_t osize, size_t nsize); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/common/luaobject.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luaobject.h" 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-2/compiler/TODO.txt -------------------------------------------------------------------------------- /C02/dummylua-2-2/main.c: -------------------------------------------------------------------------------- 1 | #include "clib/luaaux.h" 2 | #include "test/p1_test.h" 3 | #include "test/p2_test.h" 4 | 5 | int main(int argc, char** argv) { 6 | // p1_test_main(); 7 | p2_test_main(); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = time(NULL); 17 | int end_time = time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C02/dummylua-2-2/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luamem.c common/luaobject.c common/luastate.c common/luastring.c common/luatable.c) 7 | set(CLIB_SRC clib/luaaux.c) 8 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c) 9 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c) 10 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC}) 11 | 12 | # add the executable 13 | add_executable(dummylua main.c ${SRC}) 14 | 15 | IF (WIN32) 16 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 17 | ENDIF() 18 | 19 | target_include_directories(dummylua PUBLIC 20 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 21 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 22 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 23 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 24 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 25 | ) -------------------------------------------------------------------------------- /C02/dummylua-2-3/common/luaobject.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luaobject.h" 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/common/luatable.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luatable.h" 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/common/luatable.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luatable_h 22 | #define luatable_h 23 | 24 | 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-3/compiler/TODO.txt -------------------------------------------------------------------------------- /C02/dummylua-2-3/main.c: -------------------------------------------------------------------------------- 1 | #include "clib/luaaux.h" 2 | #include "test/p1_test.h" 3 | #include "test/p2_test.h" 4 | #include "test/p3_test.h" 5 | 6 | int main(int argc, char** argv) { 7 | // p1_test_main(); 8 | // p2_test_main(); 9 | p3_test_main(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = time(NULL); 17 | int end_time = time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C02/dummylua-2-3/vm/luavm.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-3/vm/luavm.c -------------------------------------------------------------------------------- /C02/dummylua-2-3/vm/luavm.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-3/vm/luavm.h -------------------------------------------------------------------------------- /C02/dummylua-2-4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luamem.c common/luaobject.c common/luastate.c common/luastring.c common/luatable.c) 7 | set(CLIB_SRC clib/luaaux.c) 8 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c) 9 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c) 10 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC}) 11 | 12 | # add the executable 13 | add_executable(dummylua main.c ${SRC}) 14 | 15 | IF(NOT WIN32) 16 | target_link_libraries(dummylua m) 17 | ENDIF() 18 | 19 | IF (WIN32) 20 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 21 | ENDIF() 22 | 23 | target_include_directories(dummylua PUBLIC 24 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 25 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 26 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 27 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 29 | ) -------------------------------------------------------------------------------- /C02/dummylua-2-4/common/luaobject.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luaobject.h" 22 | 23 | const TValue luaO_nilobject_ = { {NULL}, LUA_TNIL }; 24 | 25 | int luaO_ceillog2(int value) { 26 | int i = 0; 27 | 28 | for (; i < 32; i++) { 29 | if ((int)pow(2, i) >= value) 30 | return i; 31 | } 32 | 33 | return i; 34 | } 35 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C02/dummylua-2-4/compiler/TODO.txt -------------------------------------------------------------------------------- /C02/dummylua-2-4/main.c: -------------------------------------------------------------------------------- 1 | #include "clib/luaaux.h" 2 | #include "test/p1_test.h" 3 | #include "test/p2_test.h" 4 | #include "test/p3_test.h" 5 | #include "test/p4_test.h" 6 | 7 | int main(int argc, char** argv) { 8 | // p1_test_main(); 9 | // p2_test_main(); 10 | // p3_test_main(); 11 | p4_test_main(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C02/dummylua-2-4/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C03/dummylua-3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luainit.c common/luamem.c common/luaobject.c 7 | common/luastate.c common/luastring.c common/luatable.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c) 12 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 13 | 14 | # add the executable 15 | add_executable(dummylua main.c ${SRC}) 16 | 17 | IF(NOT WIN32) 18 | target_link_libraries(dummylua m) 19 | ENDIF() 20 | 21 | IF (WIN32) 22 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 23 | ENDIF() 24 | 25 | target_include_directories(dummylua PUBLIC 26 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 27 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 31 | ) -------------------------------------------------------------------------------- /C03/dummylua-3/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C03/dummylua-3/common/luaobject.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #include "luaobject.h" 22 | 23 | const TValue luaO_nilobject_ = { {NULL}, LUA_TNIL }; 24 | 25 | int luaO_ceillog2(int value) { 26 | int i = 0; 27 | 28 | for (; i < 32; i++) { 29 | if ((int)pow(2, i) >= value) 30 | return i; 31 | } 32 | 33 | return i; 34 | } 35 | -------------------------------------------------------------------------------- /C03/dummylua-3/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C03/dummylua-3/compiler/TODO.txt -------------------------------------------------------------------------------- /C03/dummylua-3/compiler/luacode.h: -------------------------------------------------------------------------------- 1 | #ifndef luacode_h_ 2 | #define luacode_h_ 3 | 4 | #include "../common/lua.h" 5 | #include "lualexer.h" 6 | #include "luaparser.h" 7 | 8 | #define get_instruction(fs, e) (fs->p->code[e->u.info]) 9 | 10 | int luaK_exp2RK(FuncState* fs, expdesc* e); // discharge expression to constant table or specific register 11 | int luaK_stringK(FuncState* fs, TString* key); // generate an expression to index constant in constants vector 12 | int luaK_nilK(FuncState* fs); 13 | int luaK_floatK(FuncState* fs, lua_Number r); 14 | int luaK_intK(FuncState* fs, lua_Integer i); 15 | int luaK_boolK(FuncState* fs, int v); 16 | int luaK_ret(FuncState* fs, int first, int nret); 17 | 18 | void luaK_indexed(FuncState* fs, expdesc* e, expdesc* key); // generate an expression to index a key which is in a local table or a upvalue table 19 | 20 | int luaK_codeABC(FuncState* fs, int opcode, int a, int b, int c); 21 | int luaK_codeABx(FuncState* fs, int opcode, int a, int bx); 22 | 23 | void luaK_dischargevars(FuncState* fs, expdesc* e); 24 | int luaK_exp2nextreg(FuncState* fs, expdesc* e); // discharge expression to next register 25 | int luaK_exp2anyreg(FuncState* fs, expdesc * e); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /C03/dummylua-3/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C03/dummylua-3/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p5_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p5_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C03/dummylua-3/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C03/dummylua-3/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C03/dummylua-3/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C03/dummylua-3/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C03/dummylua-3/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C03/dummylua-3/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C03/dummylua-3/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C03/dummylua-3/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C03/dummylua-3/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C03/dummylua-3/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C03/dummylua-3/vm/luaopcodes.c: -------------------------------------------------------------------------------- 1 | #include "luaopcodes.h" 2 | 3 | /*** 4 | t: if it is a test instruction or not 5 | a: can register will be update by current instruction 6 | b: OpArgMode 7 | c: OpArgMode 8 | m: mode(iABC,iABx,iAsBx) 9 | ***/ 10 | #define opmode(t, a, b, c, m) ((t << 7) | (a << 6) | (b << 4) | (c << 2) | m) 11 | 12 | const lu_byte luaP_opmodes[NUM_OPCODES] = { 13 | // t a b c m 14 | opmode(0, 1, OpArgR, OpArgN, iABC) // OP_MOVE, 15 | ,opmode(0, 1, OpArgK, OpArgN, iABC) // OP_LOADK, 16 | ,opmode(0, 1, OpArgU, OpArgN, iABC) // OP_GETUPVAL, 17 | ,opmode(0, 1, OpArgU, OpArgU, iABC) // OP_CALL, 18 | ,opmode(0, 0, OpArgU, OpArgU, iABC) // OP_RETURN, 19 | ,opmode(0, 1, OpArgU, OpArgU, iABC) // OP_GETTABUP, 20 | ,opmode(0, 1, OpArgU, OpArgU, iABC) // OP_GETTABLE, 21 | }; -------------------------------------------------------------------------------- /C04/dummylua-4-1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luainit.c common/luamem.c common/luaobject.c 7 | common/luastate.c common/luastring.c common/luatable.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C04/dummylua-4-1/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-1/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C04/dummylua-4-1/compiler/TODO.txt -------------------------------------------------------------------------------- /C04/dummylua-4-1/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C04/dummylua-4-1/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p6_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p6_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C04/dummylua-4-1/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C04/dummylua-4-1/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-1/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | a = { 6, s = "hello world", ["subtable"] = { 1, 2, 3, key = "value"} } 2 | b = a["subtable"].key 3 | c = a.subtable.key 4 | print(b, c) -------------------------------------------------------------------------------- /C04/dummylua-4-1/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-1/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luainit.c common/luamem.c common/luaobject.c 7 | common/luastate.c common/luastring.c common/luatable.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C04/dummylua-4-2/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-2/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C04/dummylua-4-2/compiler/TODO.txt -------------------------------------------------------------------------------- /C04/dummylua-4-2/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C04/dummylua-4-2/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p7_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p7_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C04/dummylua-4-2/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C04/dummylua-4-2/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-2/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | -- Test 2 | a = { 6, s = "hello world", ["subtable"] = { 1, 2, 3, key = "value"} } 3 | b = a["subtable"].key 4 | c = a.subtable.key 5 | print(b, c) -------------------------------------------------------------------------------- /C04/dummylua-4-2/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-2/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luainit.c common/luamem.c common/luaobject.c 7 | common/luastate.c common/luastring.c common/luatable.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c test/p8_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C04/dummylua-4-3/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-3/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C04/dummylua-4-3/compiler/TODO.txt -------------------------------------------------------------------------------- /C04/dummylua-4-3/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C04/dummylua-4-3/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p8_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p8_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C04/dummylua-4-3/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C04/dummylua-4-3/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-3/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C04/dummylua-4-3/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p8_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | int ok = luaL_loadfile(L, "../scripts/part08_test.lua"); 9 | if (ok == LUA_OK) { 10 | luaL_pcall(L, 0, 0); 11 | } 12 | 13 | luaL_close(L); 14 | } -------------------------------------------------------------------------------- /C04/dummylua-4-3/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luadebug.c common/luainit.c common/luamem.c 7 | common/luaobject.c common/luastate.c common/luastring.c common/luatable.c common/luatm.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c test/p8_test.c test/p9_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C05/dummylua-5-1/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_TOTAL 33 | }TMS; 34 | 35 | void luaT_init(struct lua_State* L); 36 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 37 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 38 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 39 | 40 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-1/compiler/TODO.txt -------------------------------------------------------------------------------- /C05/dummylua-5-1/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p9_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | 8 | p9_test_main(); 9 | 10 | #ifdef _WINDOWS_PLATFORM_ 11 | system("pause"); 12 | #endif 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C05/dummylua-5-1/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../scripts/part08_test.lua"); 18 | if (ok == LUA_OK) { 19 | luaL_pcall(L, 0, 0); 20 | } 21 | 22 | ok = luaL_pcall(L, 0, 0); 23 | check_error(L, ok); 24 | 25 | luaL_close(L); 26 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C05/dummylua-5-1/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luadebug.c common/luainit.c common/luamem.c 7 | common/luaobject.c common/luastate.c common/luastring.c common/luatable.c common/luatm.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c test/p8_test.c test/p9_test.c test/p10_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C05/dummylua-5-2/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_TOTAL 33 | }TMS; 34 | 35 | void luaT_init(struct lua_State* L); 36 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 37 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 38 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 39 | 40 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-2/compiler/TODO.txt -------------------------------------------------------------------------------- /C05/dummylua-5-2/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p10_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p10_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C05/dummylua-5-2/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p10_test.c: -------------------------------------------------------------------------------- 1 | #include "p10_test.h" 2 | #include "../common/luastring.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luatable.h" 5 | 6 | typedef struct Vector3 { 7 | float x; 8 | float y; 9 | float z; 10 | } Vector3; 11 | 12 | int gcfunc(struct lua_State* L) { 13 | Udata* u = lua_touserdata(L, -1); 14 | Vector3* v3 = (Vector3*)getudatamem(u); 15 | printf("total_size:%d x:%f, y:%f, z:%f", u->len, v3->x, v3->y, v3->z); 16 | return 0; 17 | } 18 | 19 | void test_create_object(struct lua_State* L) { 20 | Udata* u = luaS_newuserdata(L, sizeof(Vector3)); 21 | 22 | Vector3* v3 = (Vector3*)getudatamem(u); 23 | v3->x = 10.0f; 24 | v3->y = 10.0f; 25 | v3->z = 10.0f; 26 | 27 | L->top->tt_ = LUA_TUSERDATA; 28 | L->top->value_.gc = obj2gco(u); 29 | increase_top(L); 30 | 31 | struct Table* t = luaH_new(L); 32 | struct GCObject* gco = obj2gco(t); 33 | TValue tv; 34 | tv.tt_ = LUA_TTABLE; 35 | tv.value_.gc = gco; 36 | setobj(L->top, &tv); 37 | increase_top(L); 38 | 39 | lua_pushCclosure(L, gcfunc, 0); 40 | lua_setfield(L, -2, "__gc"); 41 | 42 | lua_setmetatable(L, -2); 43 | L->top--; 44 | 45 | return; 46 | } 47 | 48 | void p10_test_main() { 49 | struct lua_State* L = luaL_newstate(); 50 | luaL_openlibs(L); 51 | 52 | test_create_object(L); 53 | luaC_fullgc(L); 54 | 55 | luaL_close(L); 56 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p10_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p10_test_h_ 2 | #define _p10_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p10_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../scripts/part08_test.lua"); 18 | if (ok == LUA_OK) { 19 | luaL_pcall(L, 0, 0); 20 | } 21 | 22 | ok = luaL_pcall(L, 0, 0); 23 | check_error(L, ok); 24 | 25 | luaL_close(L); 26 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C05/dummylua-5-2/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luadebug.c common/luainit.c common/luamem.c 7 | common/luaobject.c common/luastate.c common/luastring.c common/luatable.c common/luatm.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c test/p8_test.c test/p9_test.c test/p10_test.c test/p11_test.c) 13 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 14 | 15 | # add the executable 16 | add_executable(dummylua main.c ${SRC}) 17 | 18 | IF(NOT WIN32) 19 | target_link_libraries(dummylua m) 20 | ENDIF() 21 | 22 | IF (WIN32) 23 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 24 | ENDIF() 25 | 26 | target_include_directories(dummylua PUBLIC 27 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 28 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 32 | ) -------------------------------------------------------------------------------- /C05/dummylua-5-3/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_TOTAL 33 | }TMS; 34 | 35 | void luaT_init(struct lua_State* L); 36 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 37 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 38 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 39 | 40 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-3/compiler/TODO.txt -------------------------------------------------------------------------------- /C05/dummylua-5-3/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p11_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | 8 | p11_test_main(); 9 | 10 | #ifdef _WINDOWS_PLATFORM_ 11 | system("pause"); 12 | #endif 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/part11_test.lua: -------------------------------------------------------------------------------- 1 | -- test case 1 2 | do 3 | local var1 = 1 4 | function aaa() 5 | local var2 = 1 6 | function bbb() 7 | var1 = var1 + 1 8 | var2 = var2 + 1 9 | print("bbb:", var1, var2) 10 | end 11 | 12 | function ccc() 13 | var1 = var1 + 1 14 | var2 = var2 + 1 15 | print("ccc:", var1, var2) 16 | end 17 | 18 | bbb() 19 | print("hahaha") 20 | end 21 | 22 | aaa() 23 | bbb() 24 | bbb() 25 | ccc() 26 | ccc() 27 | end 28 | 29 | -- test case 2 30 | do 31 | local var1 = 1 32 | local function aaa() 33 | local var2 = 1 34 | return function() 35 | var1 = var1 + 1 36 | var2 = var2 + 1 37 | 38 | print(var1, var2) 39 | end 40 | end 41 | 42 | local f1 = aaa() 43 | local f2 = aaa() 44 | 45 | f1() 46 | f2() 47 | end 48 | 49 | -- test case 3 50 | do 51 | local var1 = 1 52 | local function aaa() 53 | local var2 = {} 54 | var2.value = 1 55 | 56 | return function() 57 | var1 = var1 + 1 58 | var2.value = var2.value + 1 59 | print(var1, var2.value) 60 | end 61 | end 62 | 63 | local f1 = aaa() 64 | local f2 = aaa() 65 | f1() 66 | f2() 67 | end -------------------------------------------------------------------------------- /C05/dummylua-5-3/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p10_test.c: -------------------------------------------------------------------------------- 1 | #include "p10_test.h" 2 | #include "../common/luastring.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luatable.h" 5 | 6 | typedef struct Vector3 { 7 | float x; 8 | float y; 9 | float z; 10 | } Vector3; 11 | 12 | int gcfunc(struct lua_State* L) { 13 | Udata* u = lua_touserdata(L, -1); 14 | Vector3* v3 = (Vector3*)getudatamem(u); 15 | printf("total_size:%d x:%f, y:%f, z:%f", u->len, v3->x, v3->y, v3->z); 16 | return 0; 17 | } 18 | 19 | void test_create_object(struct lua_State* L) { 20 | Udata* u = luaS_newuserdata(L, sizeof(Vector3)); 21 | 22 | Vector3* v3 = (Vector3*)getudatamem(u); 23 | v3->x = 10.0f; 24 | v3->y = 10.0f; 25 | v3->z = 10.0f; 26 | 27 | L->top->tt_ = LUA_TUSERDATA; 28 | L->top->value_.gc = obj2gco(u); 29 | increase_top(L); 30 | 31 | struct Table* t = luaH_new(L); 32 | struct GCObject* gco = obj2gco(t); 33 | TValue tv; 34 | tv.tt_ = LUA_TTABLE; 35 | tv.value_.gc = gco; 36 | setobj(L->top, &tv); 37 | increase_top(L); 38 | 39 | lua_pushCclosure(L, gcfunc, 0); 40 | lua_setfield(L, -2, "__gc"); 41 | 42 | lua_setmetatable(L, -2); 43 | L->top--; 44 | 45 | return; 46 | } 47 | 48 | void p10_test_main() { 49 | struct lua_State* L = luaL_newstate(); 50 | luaL_openlibs(L); 51 | 52 | test_create_object(L); 53 | luaC_fullgc(L); 54 | 55 | luaL_close(L); 56 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p10_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p10_test_h_ 2 | #define _p10_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p10_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p11_test.c: -------------------------------------------------------------------------------- 1 | #include "p11_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p11_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../scripts/part11_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } 24 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p11_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p11_test_h_ 2 | #define _p11_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p11_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../scripts/part08_test.lua"); 18 | if (ok == LUA_OK) { 19 | luaL_pcall(L, 0, 0); 20 | } 21 | 22 | ok = luaL_pcall(L, 0, 0); 23 | check_error(L, ok); 24 | 25 | luaL_close(L); 26 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C05/dummylua-5-3/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(dummylua) 5 | 6 | set(COMMON_SRC common/luabase.c common/luadebug.c common/luainit.c common/luamem.c 7 | common/luaobject.c common/luastate.c common/luastring.c common/luatable.c common/luatm.c) 8 | set(CLIB_SRC clib/luaaux.c) 9 | set(VM_SRC vm/luado.c vm/luagc.c vm/luavm.c vm/luafunc.c vm/luaopcodes.c) 10 | set(COMPILER_SRC compiler/luazio.c compiler/lualexer.c compiler/luaparser.c compiler/luacode.c) 11 | set(TEST_SRC test/p1_test.c test/p2_test.c test/p3_test.c test/p4_test.c test/p5_test.c 12 | test/p6_test.c test/p7_test.c test/p8_test.c test/p9_test.c test/p10_test.c test/p11_test.c 13 | test/p12_test.c) 14 | set(SRC ${COMMON_SRC} ${CLIB_SRC} ${VM_SRC} ${TEST_SRC} ${COMPILER_SRC}) 15 | 16 | # add the executable 17 | add_executable(dummylua main.c ${SRC}) 18 | 19 | IF(NOT WIN32) 20 | target_link_libraries(dummylua m) 21 | ENDIF() 22 | 23 | IF (WIN32) 24 | target_compile_definitions(dummylua PRIVATE _WINDOWS_PLATFORM_=1) 25 | ENDIF() 26 | 27 | target_include_directories(dummylua PUBLIC 28 | "${CMAKE_CURRENT_SOURCE_DIR}/common" 29 | "${CMAKE_CURRENT_SOURCE_DIR}/clib" 30 | "${CMAKE_CURRENT_SOURCE_DIR}/compiler" 31 | "${CMAKE_CURRENT_SOURCE_DIR}/vm" 32 | "${CMAKE_CURRENT_SOURCE_DIR}/test" 33 | ) -------------------------------------------------------------------------------- /C05/dummylua-5-4/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_MODE, 33 | 34 | TM_TOTAL 35 | }TMS; 36 | 37 | void luaT_init(struct lua_State* L); 38 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 39 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 40 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 41 | 42 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-4/compiler/TODO.txt -------------------------------------------------------------------------------- /C05/dummylua-5-4/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p12_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | 8 | p12_test_main(); 9 | 10 | #ifdef _WINDOWS_PLATFORM_ 11 | system("pause"); 12 | #endif 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/part11_test.lua: -------------------------------------------------------------------------------- 1 | -- test case 1 2 | do 3 | local var1 = 1 4 | function aaa() 5 | local var2 = 1 6 | function bbb() 7 | var1 = var1 + 1 8 | var2 = var2 + 1 9 | print("bbb:", var1, var2) 10 | end 11 | 12 | function ccc() 13 | var1 = var1 + 1 14 | var2 = var2 + 1 15 | print("ccc:", var1, var2) 16 | end 17 | 18 | bbb() 19 | print("hahaha") 20 | end 21 | 22 | aaa() 23 | bbb() 24 | bbb() 25 | ccc() 26 | ccc() 27 | end 28 | 29 | -- test case 2 30 | do 31 | local var1 = 1 32 | local function aaa() 33 | local var2 = 1 34 | return function() 35 | var1 = var1 + 1 36 | var2 = var2 + 1 37 | 38 | print(var1, var2) 39 | end 40 | end 41 | 42 | local f1 = aaa() 43 | local f2 = aaa() 44 | 45 | f1() 46 | f2() 47 | end 48 | 49 | -- test case 3 50 | do 51 | local var1 = 1 52 | local function aaa() 53 | local var2 = {} 54 | var2.value = 1 55 | 56 | return function() 57 | var1 = var1 + 1 58 | var2.value = var2.value + 1 59 | print(var1, var2.value) 60 | end 61 | end 62 | 63 | local f1 = aaa() 64 | local f2 = aaa() 65 | f1() 66 | f2() 67 | end -------------------------------------------------------------------------------- /C05/dummylua-5-4/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p10_test.c: -------------------------------------------------------------------------------- 1 | #include "p10_test.h" 2 | #include "../common/luastring.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luatable.h" 5 | 6 | typedef struct Vector3 { 7 | float x; 8 | float y; 9 | float z; 10 | } Vector3; 11 | 12 | int gcfunc(struct lua_State* L) { 13 | Udata* u = lua_touserdata(L, -1); 14 | Vector3* v3 = (Vector3*)getudatamem(u); 15 | printf("total_size:%d x:%f, y:%f, z:%f", u->len, v3->x, v3->y, v3->z); 16 | return 0; 17 | } 18 | 19 | void test_create_object(struct lua_State* L) { 20 | Udata* u = luaS_newuserdata(L, sizeof(Vector3)); 21 | 22 | Vector3* v3 = (Vector3*)getudatamem(u); 23 | v3->x = 10.0f; 24 | v3->y = 10.0f; 25 | v3->z = 10.0f; 26 | 27 | L->top->tt_ = LUA_TUSERDATA; 28 | L->top->value_.gc = obj2gco(u); 29 | increase_top(L); 30 | 31 | struct Table* t = luaH_new(L); 32 | struct GCObject* gco = obj2gco(t); 33 | TValue tv; 34 | tv.tt_ = LUA_TTABLE; 35 | tv.value_.gc = gco; 36 | setobj(L->top, &tv); 37 | increase_top(L); 38 | 39 | lua_pushCclosure(L, gcfunc, 0); 40 | lua_setfield(L, -2, "__gc"); 41 | 42 | lua_setmetatable(L, -2); 43 | L->top--; 44 | 45 | return; 46 | } 47 | 48 | void p10_test_main() { 49 | struct lua_State* L = luaL_newstate(); 50 | luaL_openlibs(L); 51 | 52 | test_create_object(L); 53 | luaC_fullgc(L); 54 | 55 | luaL_close(L); 56 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p10_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p10_test_h_ 2 | #define _p10_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p10_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p11_test.c: -------------------------------------------------------------------------------- 1 | #include "p11_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p11_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../scripts/part11_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p11_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p11_test_h_ 2 | #define _p11_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p11_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p12_test.c: -------------------------------------------------------------------------------- 1 | #include "p12_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p12_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../scripts/part12_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p12_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p12_test_h_ 2 | #define _p12_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p12_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../scripts/part08_test.lua"); 18 | if (ok == LUA_OK) { 19 | luaL_pcall(L, 0, 0); 20 | } 21 | 22 | ok = luaL_pcall(L, 0, 0); 23 | check_error(L, ok); 24 | 25 | luaL_close(L); 26 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C05/dummylua-5-4/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/3rd/loadlib.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-5/3rd/loadlib.dll -------------------------------------------------------------------------------- /C05/dummylua-5-5/3rd/loadlib.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-5/3rd/loadlib.so -------------------------------------------------------------------------------- /C05/dummylua-5-5/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/common/lualoadlib.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luapackage_h 22 | #define luapackage_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openpackage(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_MODE, 33 | 34 | TM_TOTAL 35 | }TMS; 36 | 37 | void luaT_init(struct lua_State* L); 38 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 39 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 40 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 41 | 42 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C05/dummylua-5-5/compiler/TODO.txt -------------------------------------------------------------------------------- /C05/dummylua-5-5/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/loadlib.c: -------------------------------------------------------------------------------- 1 | #include "clib/luaaux.h" 2 | #include "common/luastate.h" 3 | 4 | static int hello_world(struct lua_State* L) { 5 | lua_getglobal(L, "print"); 6 | lua_pushstring(L, "loadlib:hello world"); 7 | luaL_pcall(L, 1, 0); 8 | 9 | return 0; 10 | } 11 | 12 | static int hello_world_core(struct lua_State* L) { 13 | lua_getglobal(L, "print"); 14 | lua_pushstring(L, "loadlib_core:hello world"); 15 | luaL_pcall(L, 1, 0); 16 | 17 | return 0; 18 | } 19 | 20 | LUA_API int luaopen_loadlib(struct lua_State* L) { 21 | lua_createtable(L); 22 | lua_pushcfunction(L, hello_world); 23 | lua_setfield(L, -2, "hello_world"); 24 | 25 | return 1; 26 | } 27 | 28 | LUA_API int luaopen_loadlib_core(struct lua_State* L) { 29 | lua_createtable(L); 30 | lua_pushcfunction(L, hello_world_core); 31 | lua_setfield(L, -2, "hello_world_core"); 32 | 33 | return 1; 34 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p13_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p13_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/p13_require_test.lua: -------------------------------------------------------------------------------- 1 | print("hello world 2022") -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/p13_test_return_table.lua: -------------------------------------------------------------------------------- 1 | print("test module") 2 | local module = {} 3 | 4 | function module.hello() 5 | print("module.hello") 6 | end 7 | 8 | function module.world() 9 | print("module.world") 10 | end 11 | 12 | return module -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/part11_test.lua: -------------------------------------------------------------------------------- 1 | -- test case 1 2 | do 3 | local var1 = 1 4 | function aaa() 5 | local var2 = 1 6 | function bbb() 7 | var1 = var1 + 1 8 | var2 = var2 + 1 9 | print("bbb:", var1, var2) 10 | end 11 | 12 | function ccc() 13 | var1 = var1 + 1 14 | var2 = var2 + 1 15 | print("ccc:", var1, var2) 16 | end 17 | 18 | bbb() 19 | print("hahaha") 20 | end 21 | 22 | aaa() 23 | bbb() 24 | bbb() 25 | ccc() 26 | ccc() 27 | end 28 | 29 | -- test case 2 30 | do 31 | local var1 = 1 32 | local function aaa() 33 | local var2 = 1 34 | return function() 35 | var1 = var1 + 1 36 | var2 = var2 + 1 37 | 38 | print(var1, var2) 39 | end 40 | end 41 | 42 | local f1 = aaa() 43 | local f2 = aaa() 44 | 45 | f1() 46 | f2() 47 | end 48 | 49 | -- test case 3 50 | do 51 | local var1 = 1 52 | local function aaa() 53 | local var2 = {} 54 | var2.value = 1 55 | 56 | return function() 57 | var1 = var1 + 1 58 | var2.value = var2.value + 1 59 | print(var1, var2.value) 60 | end 61 | end 62 | 63 | local f1 = aaa() 64 | local f2 = aaa() 65 | f1() 66 | f2() 67 | end -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/part13_test.lua: -------------------------------------------------------------------------------- 1 | -- test package path 2 | print(package.path) 3 | print(package.cpath) 4 | 5 | -- test preload 6 | package.preload["test.aa.bb"] = function(file) print(file) end 7 | require("test.aa.bb") 8 | local ret = require("test.aa.bb") 9 | print(ret) 10 | 11 | -- test package.loaded and package.searchers 12 | print("---------package.loaded---------") 13 | print(package.loaded) 14 | for k, v in pairs(package.loaded) do 15 | print(k, v) 16 | end 17 | 18 | print("---------package.searchers---------") 19 | print(package.searchers) 20 | for k, v in pairs(package.searchers) do 21 | print(k, v) 22 | end 23 | 24 | -- require a file that does not exist 25 | print("----require lua file") 26 | package.path = package.path .. "../?.lua;" 27 | local file_without_return = require("scripts.p13_require_test") 28 | print(file_without_return) 29 | 30 | -- require again 31 | file_without_return = require("scripts.p13_require_test") 32 | print(file_without_return) 33 | 34 | -- require a module 35 | local mod = require("scripts/p13_test_return_table") 36 | mod.hello() 37 | mod.world() 38 | 39 | local mod2 = require("scripts/p13_test_return_table") 40 | mod2.hello() 41 | mod2.world() 42 | 43 | print(package.cpath) 44 | local clib = require("loadlib") 45 | clib.hello_world() 46 | 47 | local clibcore = require("loadlib.core") 48 | clibcore.hello_world_core() -------------------------------------------------------------------------------- /C05/dummylua-5-5/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p10_test.c: -------------------------------------------------------------------------------- 1 | #include "p10_test.h" 2 | #include "../common/luastring.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luatable.h" 5 | 6 | typedef struct Vector3 { 7 | float x; 8 | float y; 9 | float z; 10 | } Vector3; 11 | 12 | int gcfunc(struct lua_State* L) { 13 | Udata* u = lua_touserdata(L, -1); 14 | Vector3* v3 = (Vector3*)getudatamem(u); 15 | printf("total_size:%d x:%f, y:%f, z:%f", u->len, v3->x, v3->y, v3->z); 16 | return 0; 17 | } 18 | 19 | void test_create_object(struct lua_State* L) { 20 | Udata* u = luaS_newuserdata(L, sizeof(Vector3)); 21 | 22 | Vector3* v3 = (Vector3*)getudatamem(u); 23 | v3->x = 10.0f; 24 | v3->y = 10.0f; 25 | v3->z = 10.0f; 26 | 27 | L->top->tt_ = LUA_TUSERDATA; 28 | L->top->value_.gc = obj2gco(u); 29 | increase_top(L); 30 | 31 | struct Table* t = luaH_new(L); 32 | struct GCObject* gco = obj2gco(t); 33 | TValue tv; 34 | tv.tt_ = LUA_TTABLE; 35 | tv.value_.gc = gco; 36 | setobj(L->top, &tv); 37 | increase_top(L); 38 | 39 | lua_pushCclosure(L, gcfunc, 0); 40 | lua_setfield(L, -2, "__gc"); 41 | 42 | lua_setmetatable(L, -2); 43 | L->top--; 44 | 45 | return; 46 | } 47 | 48 | void p10_test_main() { 49 | struct lua_State* L = luaL_newstate(); 50 | luaL_openlibs(L); 51 | 52 | test_create_object(L); 53 | luaC_fullgc(L); 54 | 55 | luaL_close(L); 56 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p10_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p10_test_h_ 2 | #define _p10_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p10_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p11_test.c: -------------------------------------------------------------------------------- 1 | #include "p11_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p11_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../dummylua/scripts/part11_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p11_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p11_test_h_ 2 | #define _p11_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p11_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p12_test.c: -------------------------------------------------------------------------------- 1 | #include "p12_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p12_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../dummylua/scripts/part12_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p12_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p12_test_h_ 2 | #define _p12_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p12_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p13_test.c: -------------------------------------------------------------------------------- 1 | #include "p13_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK && (novariant(L->top - 1) == LUA_TSTRING)) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p13_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | const char* filename = "../scripts/part13_test.lua"; 17 | int ok = luaL_loadfile(L, filename); 18 | if (ok == LUA_OK) { 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | } 22 | else { 23 | printf("failure to load file %s\n", filename); 24 | } 25 | 26 | lua_close(L); 27 | } 28 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p13_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p13_test_h_ 2 | #define _p13_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p13_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../dummylua/scripts/debug_test.lua"); 18 | check_error(L, ok); 19 | 20 | ok = luaL_pcall(L, 0, 0); 21 | check_error(L, ok); 22 | 23 | luaL_close(L); 24 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../dummylua/scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C05/dummylua-5-5/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/3rd/clibs/d2d.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/3rd/clibs/d2d.dll -------------------------------------------------------------------------------- /C06/tetris/3rd/clibs/d2d1.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/3rd/clibs/d2d1.dll -------------------------------------------------------------------------------- /C06/tetris/3rd/d2d/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(d2d) 5 | 6 | add_library(${PROJECT_NAME} MODULE main.cpp render.h render.cpp) 7 | 8 | SET(DUMMYLUA_LIB ${CMAKE_CURRENT_SOURCE_DIR}/../../dummylua/dummylua.lib) 9 | target_link_libraries(${PROJECT_NAME} ${DUMMYLUA_LIB}) 10 | target_link_libraries(${PROJECT_NAME} d2d1.lib Dwrite.lib) 11 | 12 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 13 | COMMAND ${CMAKE_COMMAND} -E copy 14 | $/${PROJECT_NAME}.dll 15 | ${CMAKE_CURRENT_SOURCE_DIR}/../clibs) 16 | 17 | target_compile_definitions(${PROJECT_NAME} PRIVATE _WINDOWS_PLATFORM_=1) 18 | target_compile_definitions(${PROJECT_NAME} PRIVATE _CRT_SECURE_NO_WARNINGS=1) 19 | 20 | SET(DUMMYLUA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../dummylua) 21 | target_include_directories(${PROJECT_NAME} PUBLIC 22 | "${DUMMYLUA_DIR}/common" 23 | "${DUMMYLUA_DIR}/clib" 24 | "${DUMMYLUA_DIR}/compiler" 25 | "${DUMMYLUA_DIR}/vm" 26 | "${DUMMYLUA_DIR}/test" 27 | ) 28 | -------------------------------------------------------------------------------- /C06/tetris/3rd/d2d/render.h: -------------------------------------------------------------------------------- 1 | #ifndef _render_h_ 2 | #define _render_h_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define BOX_RED 0 10 | #define BOX_YELLOW 1 11 | #define BOX_BLUE 2 12 | #define BOX_GREEN 3 13 | #define MAX_BOX_BLUSH 4 14 | 15 | int render_init(HWND hwnd); 16 | void render_begin(); 17 | void render_draw_box(int x, int y, int width, int height, int fill_color); 18 | void render_draw_text(int x, int y, const char* text); 19 | void render_end(); 20 | int render_destroy(); 21 | 22 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/3rd/loadlib.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/dummylua/3rd/loadlib.dll -------------------------------------------------------------------------------- /C06/tetris/dummylua/3rd/loadlib.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/dummylua/3rd/loadlib.so -------------------------------------------------------------------------------- /C06/tetris/dummylua/common/luabase.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luabase_h 22 | #define luabase_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openbase(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/common/luadebug.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef lua_debug_h 22 | #define lua_debug_h 23 | 24 | #include "../common/luastate.h" 25 | 26 | void luaG_runerror(struct lua_State* L, const char* fmt, ...); 27 | 28 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/common/lualoadlib.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2018 Manistein,https://manistein.github.io/blog/ 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE.*/ 20 | 21 | #ifndef luapackage_h 22 | #define luapackage_h 23 | 24 | #include "luastate.h" 25 | 26 | int luaB_openpackage(struct lua_State* L); 27 | 28 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/common/luatm.h: -------------------------------------------------------------------------------- 1 | #ifndef _lua_tm_h_ 2 | #define _lua_tm_h_ 3 | 4 | #include "luaobject.h" 5 | 6 | typedef enum { 7 | TM_LT, 8 | TM_GT, 9 | TM_LE, 10 | TM_GE, 11 | TM_EQ, 12 | TM_CONCAT, 13 | 14 | TM_ADD, 15 | TM_SUB, 16 | TM_MUL, 17 | TM_DIV, 18 | TM_IDIV, 19 | TM_POW, 20 | TM_BAND, 21 | TM_BOR, 22 | TM_XOR, 23 | TM_SHL, 24 | TM_SHR, 25 | TM_MOD, 26 | 27 | TM_INDEX, 28 | TM_NEWINDEX, 29 | 30 | TM_GC, 31 | 32 | TM_MODE, 33 | 34 | TM_TOTAL 35 | }TMS; 36 | 37 | void luaT_init(struct lua_State* L); 38 | void luaT_trycallbinTM(struct lua_State* L, TValue* lhs, TValue* rhs, TMS tm); 39 | TValue* luaT_gettmbyobj(struct lua_State* L, TValue* o, TMS tm); 40 | void luaT_callTM(struct lua_State* L, TValue* f, TValue* p1, TValue* p2, TValue* p3, int hasres); 41 | 42 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/compiler/TODO.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/dummylua/compiler/TODO.txt -------------------------------------------------------------------------------- /C06/tetris/dummylua/compiler/luazio.c: -------------------------------------------------------------------------------- 1 | #include "luazio.h" 2 | 3 | void luaZ_init(struct lua_State* L, Zio* zio, lua_Reader reader, void* data) { 4 | lua_assert(L); 5 | lua_assert(zio); 6 | lua_assert(data); 7 | 8 | zio->L = L; 9 | zio->data = data; 10 | zio->n = 0; 11 | zio->p = NULL; 12 | zio->reader = reader; 13 | } 14 | 15 | int luaZ_fill(Zio* z) { 16 | int c = 0; 17 | 18 | size_t read_size = 0; 19 | z->p = (void*)z->reader(z->L, z->data, &read_size); 20 | 21 | if (read_size > 0) { 22 | z->n = (int)read_size; 23 | c = (int)(*z->p); 24 | 25 | z->p++; 26 | z->n--; 27 | } 28 | else { 29 | c = EOF; 30 | } 31 | 32 | return c; 33 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/dummylua.lib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Manistein/let-us-build-a-lua-interpreter/b878d6711f7e7f2cf085c64af90b43407197969b/C06/tetris/dummylua/dummylua.lib -------------------------------------------------------------------------------- /C06/tetris/dummylua/main.c: -------------------------------------------------------------------------------- 1 | #include "test/p13_test.h" 2 | #ifdef _WINDOWS_PLATFORM_ 3 | #include 4 | #endif 5 | 6 | int main(int argc, char** argv) { 7 | p13_test_main(); 8 | 9 | #ifdef _WINDOWS_PLATFORM_ 10 | system("pause"); 11 | #endif 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/debug_test.lua: -------------------------------------------------------------------------------- 1 | function test() 2 | c = a .. b 3 | end 4 | 5 | function test2() 6 | test() 7 | end 8 | 9 | test2() -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/p13_require_test.lua: -------------------------------------------------------------------------------- 1 | print("hello world 2022") -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/p13_test_return_table.lua: -------------------------------------------------------------------------------- 1 | print("test module") 2 | local module = {} 3 | 4 | function module.hello() 5 | print("module.hello") 6 | end 7 | 8 | function module.world() 9 | print("module.world") 10 | end 11 | 12 | return module -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/parser_test01.lua: -------------------------------------------------------------------------------- 1 | local str = "hello world" 2 | local year = 2020 3 | print(str) 4 | print(year) -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/part05_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- test 3 | print("hello world") 4 | print("hahaha") 5 | print(nil, true, false, "hello", 2020) -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/part06_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/part07_test.lua: -------------------------------------------------------------------------------- 1 | print("h"), a, b = 1, 2, 3 -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/part11_test.lua: -------------------------------------------------------------------------------- 1 | -- test case 1 2 | do 3 | local var1 = 1 4 | function aaa() 5 | local var2 = 1 6 | function bbb() 7 | var1 = var1 + 1 8 | var2 = var2 + 1 9 | print("bbb:", var1, var2) 10 | end 11 | 12 | function ccc() 13 | var1 = var1 + 1 14 | var2 = var2 + 1 15 | print("ccc:", var1, var2) 16 | end 17 | 18 | bbb() 19 | print("hahaha") 20 | end 21 | 22 | aaa() 23 | bbb() 24 | bbb() 25 | ccc() 26 | ccc() 27 | end 28 | 29 | -- test case 2 30 | do 31 | local var1 = 1 32 | local function aaa() 33 | local var2 = 1 34 | return function() 35 | var1 = var1 + 1 36 | var2 = var2 + 1 37 | 38 | print(var1, var2) 39 | end 40 | end 41 | 42 | local f1 = aaa() 43 | local f2 = aaa() 44 | 45 | f1() 46 | f2() 47 | end 48 | 49 | -- test case 3 50 | do 51 | local var1 = 1 52 | local function aaa() 53 | local var2 = {} 54 | var2.value = 1 55 | 56 | return function() 57 | var1 = var1 + 1 58 | var2.value = var2.value + 1 59 | print(var1, var2.value) 60 | end 61 | end 62 | 63 | local f1 = aaa() 64 | local f2 = aaa() 65 | f1() 66 | f2() 67 | end -------------------------------------------------------------------------------- /C06/tetris/dummylua/scripts/token_test.lua: -------------------------------------------------------------------------------- 1 | local function print_test() 2 | local str = "hello world" 3 | print("hello world") 4 | end 5 | 6 | print_test() 7 | 8 | local number = 0.123 9 | local number2 = .456 10 | local tbl = {} 11 | tbl["key"] = "value" .. "value2" 12 | 13 | function print_r(...) 14 | return ... 15 | end 16 | 17 | tbl.key 18 | 19 | -- This is comment 20 | tbl.sum = 100 + 200.0 - 10 * 12 / 13 % (1+2) 21 | if tbl.sum ~= 100 then 22 | tbl.sum = tbl.sum << 2 23 | elseif tbl.sum == 200 then 24 | tbl.sum = tbl.sum >> 2 25 | elseif tbl.sum > 1 then 26 | elseif tbl.sum < 2 then 27 | elseif tbl.sum >= 3 then 28 | elseif tbl.sum <= 4 then 29 | tbl.sum = nil 30 | end 31 | 32 | tbl.true = true 33 | tbl.false = false 34 | 35 | local a, b = 11, 22 -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p10_test.c: -------------------------------------------------------------------------------- 1 | #include "p10_test.h" 2 | #include "../common/luastring.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luatable.h" 5 | 6 | typedef struct Vector3 { 7 | float x; 8 | float y; 9 | float z; 10 | } Vector3; 11 | 12 | int gcfunc(struct lua_State* L) { 13 | Udata* u = lua_touserdata(L, -1); 14 | Vector3* v3 = (Vector3*)getudatamem(u); 15 | printf("total_size:%d x:%f, y:%f, z:%f", u->len, v3->x, v3->y, v3->z); 16 | return 0; 17 | } 18 | 19 | void test_create_object(struct lua_State* L) { 20 | Udata* u = luaS_newuserdata(L, sizeof(Vector3)); 21 | 22 | Vector3* v3 = (Vector3*)getudatamem(u); 23 | v3->x = 10.0f; 24 | v3->y = 10.0f; 25 | v3->z = 10.0f; 26 | 27 | L->top->tt_ = LUA_TUSERDATA; 28 | L->top->value_.gc = obj2gco(u); 29 | increase_top(L); 30 | 31 | struct Table* t = luaH_new(L); 32 | struct GCObject* gco = obj2gco(t); 33 | TValue tv; 34 | tv.tt_ = LUA_TTABLE; 35 | tv.value_.gc = gco; 36 | setobj(L->top, &tv); 37 | increase_top(L); 38 | 39 | lua_pushCclosure(L, gcfunc, 0); 40 | lua_setfield(L, -2, "__gc"); 41 | 42 | lua_setmetatable(L, -2); 43 | L->top--; 44 | 45 | return; 46 | } 47 | 48 | void p10_test_main() { 49 | struct lua_State* L = luaL_newstate(); 50 | luaL_openlibs(L); 51 | 52 | test_create_object(L); 53 | luaC_fullgc(L); 54 | 55 | luaL_close(L); 56 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p10_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p10_test_h_ 2 | #define _p10_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p10_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p11_test.c: -------------------------------------------------------------------------------- 1 | #include "p11_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p11_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../dummylua/scripts/part11_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p11_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p11_test_h_ 2 | #define _p11_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p11_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p12_test.c: -------------------------------------------------------------------------------- 1 | #include "p12_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p12_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | int ok = luaL_loadfile(L, "../dummylua/scripts/part12_test.lua"); 17 | check_error(L, ok); 18 | 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | 22 | luaL_close(L); 23 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p12_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p12_test_h_ 2 | #define _p12_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p12_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p13_test.c: -------------------------------------------------------------------------------- 1 | #include "p13_test.h" 2 | #include "../vm/luagc.h" 3 | #include "../common/luastring.h" 4 | 5 | static void check_error(struct lua_State* L, int code) { 6 | if (code != LUA_OK && (novariant(L->top - 1) == LUA_TSTRING)) { 7 | TString* ts = gco2ts(gcvalue(L->top - 1)); 8 | printf(getstr(ts)); 9 | } 10 | } 11 | 12 | void p13_test_main() { 13 | struct lua_State* L = luaL_newstate(); 14 | luaL_openlibs(L); 15 | 16 | const char* filename = "../scripts/part13_test.lua"; 17 | int ok = luaL_loadfile(L, filename); 18 | if (ok == LUA_OK) { 19 | ok = luaL_pcall(L, 0, 0); 20 | check_error(L, ok); 21 | } 22 | else { 23 | check_error(L, ok); 24 | printf("failure to load file %s\n", filename); 25 | } 26 | 27 | lua_close(L); 28 | } 29 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p13_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p13_test_h_ 2 | #define _p13_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p13_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p1_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p1_test_h 2 | #define p1_test_h 3 | 4 | // test result 5 | void p1_test_result01(); // nwant = 0; and nresult = 0; 6 | void p1_test_result02(); // nwant = 0; and nresult = 1; 7 | void p1_test_result03(); // nwant = 0; and nresult > 1; 8 | void p1_test_result04(); // nwant = 1; and nresult = 0; 9 | void p1_test_result05(); // nwant = 1; and nresult = 1; 10 | void p1_test_result06(); // nwant = 1; and nresult > 1; 11 | void p1_test_result07(); // nwant > 1; and nresult = 0; 12 | void p1_test_result08(); // nwant > 1; and nresult > 0; 13 | void p1_test_result09(); // nwant = -1; and nresult = 0; 14 | void p1_test_result10(); // nwant = -1; and nresult > 0; 15 | 16 | void p1_test_nestcall01(); // call count < LUA_MAXCALLS 17 | void p1_test_nestcall02(); // call count >= LUA_MAXCALLS 18 | 19 | void p1_test_main(); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p2_test.c: -------------------------------------------------------------------------------- 1 | #include "../clib/luaaux.h" 2 | #include "../vm/luagc.h" 3 | #include "p2_test.h" 4 | #include 5 | 6 | #define ELEMENTNUM 5 7 | 8 | void p2_test_main() { 9 | struct lua_State* L = luaL_newstate(); 10 | 11 | int i = 0; 12 | for (; i < ELEMENTNUM; i ++) { 13 | luaL_pushnil(L); 14 | } 15 | 16 | int start_time = (int)time(NULL); 17 | int end_time = (int)time(NULL); 18 | size_t max_bytes = 0; 19 | struct global_State* g = G(L); 20 | int j = 0; 21 | for (; j < 500000000; j ++) { 22 | TValue* o = luaL_index2addr(L, (j % ELEMENTNUM) + 1); 23 | struct GCObject* gco = luaC_newobj(L, LUA_TSTRING, sizeof(TString)); 24 | o->value_.gc = gco; 25 | o->tt_ = LUA_TSTRING; 26 | luaC_checkgc(L); 27 | 28 | if ((g->totalbytes + g->GCdebt) > max_bytes) { 29 | max_bytes = g->totalbytes + g->GCdebt; 30 | } 31 | 32 | if (j % 1000 == 0) { 33 | printf("timestamp:%d totalbytes:%f kb \n", (int)time(NULL), (float)(g->totalbytes + g->GCdebt) / 1024.0f); 34 | } 35 | } 36 | end_time = (int)time(NULL); 37 | printf("finish test start_time:%d end_time:%d max_bytes:%f kb \n", start_time, end_time, (float)max_bytes / 1024.0f); 38 | 39 | luaL_close(L); 40 | } 41 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p2_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p2_test_h 2 | #define p2_test_h 3 | 4 | void p2_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p3_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p3_test_h 2 | #define p3_test_h 3 | 4 | void p3_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p4_test.h: -------------------------------------------------------------------------------- 1 | #ifndef p4_test_h 2 | #define p4_test_h 3 | 4 | void p4_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p5_test.c: -------------------------------------------------------------------------------- 1 | #include "p5_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p5_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part05_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p5_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p5_test_h_ 2 | #define _p5_test_h_ 3 | 4 | void p5_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p6_test.c: -------------------------------------------------------------------------------- 1 | #include "p6_test.h" 2 | #include "../clib/luaaux.h" 3 | 4 | void p6_test_main() { 5 | struct lua_State* L = luaL_newstate(); 6 | luaL_openlibs(L); 7 | 8 | luaL_loadfile(L, "../dummylua/scripts/part06_test.lua"); 9 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p6_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p6_test_h_ 2 | #define _p6_test_h_ 3 | 4 | void p6_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p7_test.c: -------------------------------------------------------------------------------- 1 | #include "p7_test.h" 2 | #include "../common/lua.h" 3 | #include "../clib/luaaux.h" 4 | 5 | void p7_test_main() { 6 | struct lua_State* L = luaL_newstate(); 7 | luaL_openlibs(L); 8 | 9 | int ok = luaL_loadfile(L, "../dummylua/scripts/part07_test.lua"); 10 | if (ok == LUA_OK) { 11 | luaL_pcall(L, 0, 0); 12 | } 13 | 14 | luaL_close(L); 15 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p7_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p7_test_h_ 2 | #define _p7_test_h_ 3 | 4 | void p7_test_main(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p8_test.c: -------------------------------------------------------------------------------- 1 | #include "p8_test.h" 2 | #include "../clib/luaaux.h" 3 | #include "../vm/luagc.h" 4 | #include "../common/luastring.h" 5 | 6 | static void check_error(struct lua_State* L, int code) { 7 | if (code != LUA_OK) { 8 | TString* ts = gco2ts(gcvalue(L->top - 1)); 9 | printf(getstr(ts)); 10 | } 11 | } 12 | 13 | void p8_test_main() { 14 | struct lua_State* L = luaL_newstate(); 15 | luaL_openlibs(L); 16 | 17 | int ok = luaL_loadfile(L, "../dummylua/scripts/debug_test.lua"); 18 | check_error(L, ok); 19 | 20 | ok = luaL_pcall(L, 0, 0); 21 | check_error(L, ok); 22 | 23 | luaL_close(L); 24 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p8_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p8_test_h_ 2 | #define _p8_test_h_ 3 | 4 | void p8_test_main(); 5 | 6 | #endif -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p9_test.c: -------------------------------------------------------------------------------- 1 | #include "p9_test.h" 2 | 3 | #include "../clib/luaaux.h" 4 | #include "../vm/luagc.h" 5 | #include "../common/luastring.h" 6 | 7 | static void check_error(struct lua_State* L, int code) { 8 | if (code != LUA_OK) { 9 | TString* ts = gco2ts(gcvalue(L->top - 1)); 10 | printf(getstr(ts)); 11 | } 12 | } 13 | 14 | void p9_test_main() { 15 | struct lua_State* L = luaL_newstate(); 16 | luaL_openlibs(L); 17 | 18 | int ok = luaL_loadfile(L, "../dummylua/scripts/part09_test.lua"); 19 | check_error(L, ok); 20 | 21 | ok = luaL_pcall(L, 0, 0); 22 | check_error(L, ok); 23 | 24 | luaL_close(L); 25 | } -------------------------------------------------------------------------------- /C06/tetris/dummylua/test/p9_test.h: -------------------------------------------------------------------------------- 1 | #ifndef _p9_test_h_ 2 | #define _p9_test_h_ 3 | 4 | #include "../clib/luaaux.h" 5 | 6 | void p9_test_main(); 7 | 8 | #endif -------------------------------------------------------------------------------- /C06/tetris/game/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5.1) 2 | 3 | # set the project name 4 | project(game) 5 | 6 | add_executable(${PROJECT_NAME} WIN32 main.cpp logic.h logic.cpp) 7 | 8 | IF (WIN32) 9 | SET(DUMMYLUA_LIB ${CMAKE_CURRENT_SOURCE_DIR}/../dummylua/dummylua.lib) 10 | target_link_libraries(${PROJECT_NAME} ${DUMMYLUA_LIB}) 11 | 12 | target_compile_definitions(${PROJECT_NAME} PRIVATE _WINDOWS_PLATFORM_=1) 13 | target_compile_definitions(${PROJECT_NAME} PRIVATE _CRT_SECURE_NO_WARNINGS=1) 14 | 15 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 16 | COMMAND ${CMAKE_COMMAND} -E copy 17 | ${CMAKE_CURRENT_SOURCE_DIR}/../3rd/clibs/d2d1.dll 18 | $) 19 | ELSE() 20 | target_link_libraries(${PROJECT_NAME} m) 21 | target_link_libraries(${PROJECT_NAME} dl) 22 | ENDIF() 23 | 24 | SET(DUMMYLUA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../dummylua) 25 | target_include_directories(${PROJECT_NAME} PUBLIC 26 | "${DUMMYLUA_DIR}/common" 27 | "${DUMMYLUA_DIR}/clib" 28 | "${DUMMYLUA_DIR}/compiler" 29 | "${DUMMYLUA_DIR}/vm" 30 | "${DUMMYLUA_DIR}/test" 31 | ) 32 | -------------------------------------------------------------------------------- /C06/tetris/game/logic.h: -------------------------------------------------------------------------------- 1 | #ifndef _logic_h_ 2 | #define _logic_h_ 3 | 4 | int logic_init(void* hwnd, int width, int height); 5 | void logic_destroy(); 6 | 7 | int logic_frame(int delta); 8 | int logic_key_w_press(); 9 | int logic_key_s_press(); 10 | int logic_key_a_press(); 11 | int logic_key_d_press(); 12 | int logic_key_space_press(); 13 | int logic_key_esc_press(); 14 | int logic_key_enter_press(); 15 | 16 | #endif -------------------------------------------------------------------------------- /C06/tetris/game/modules/const.lua: -------------------------------------------------------------------------------- 1 | local const = {} 2 | 3 | const.BLOCK_COLOR = { 4 | RED = 0, 5 | YELLOW = 1, 6 | BLUE = 2, 7 | GREEN = 3, 8 | MAX_COLOR = 4, 9 | } 10 | 11 | const.KEY_EVENT = { 12 | MOVE_UP = 0, 13 | MOVE_DOWN = 1, 14 | MOVE_LEFT = 2, 15 | MOVE_RIGHT = 3, 16 | RESTART = 4, 17 | PAUSE = 5, 18 | RESUME = 6, 19 | } 20 | 21 | const.BOX_SIZE = { 22 | WIDTH = 30, 23 | HEIGHT = 30, 24 | } 25 | 26 | const.BOARD_SIZE = { 27 | X = 10, 28 | Y = 20, 29 | } 30 | 31 | const.FLIP_TYPE = { 32 | NORMAL = 0, -- 不翻转 33 | FLIPED = 1, -- 翻转 34 | } 35 | 36 | const.BLOCK_TYPE = { 37 | LSHAPE = 1, 38 | SQUARE = 2, 39 | STICK = 3, 40 | TSHAPE = 4, 41 | ZSHAPE = 5, 42 | TOTAL = 6, 43 | } 44 | 45 | const.GAME_STATUS = { 46 | RUNNING = 1, 47 | PAUSE = 2, 48 | GAME_OVER = 3, 49 | } 50 | 51 | const.MAX_ROTATE = 4 52 | 53 | return const -------------------------------------------------------------------------------- /C06/tetris/game/modules/core/class.lua: -------------------------------------------------------------------------------- 1 | -- simple class template 2 | local class = {} 3 | 4 | function class:inherit(o) 5 | o = o or {} 6 | o._is_class = true 7 | setmetatable(o, { __index = self }) 8 | return o 9 | end 10 | 11 | return class -------------------------------------------------------------------------------- /C06/tetris/game/modules/core/object.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local class = require("modules.core.class") 3 | local object = class:inherit() 4 | 5 | function object:new() 6 | local o = {} 7 | setmetatable(o, { __index = self }) 8 | 9 | if o.init then 10 | o:init() 11 | end 12 | 13 | return o 14 | end 15 | 16 | function object:draw() 17 | end 18 | 19 | function object:release() 20 | for k, v in pairs(self) do 21 | self[k] = nil 22 | end 23 | end 24 | 25 | return object -------------------------------------------------------------------------------- /C06/tetris/game/modules/core/render.lua: -------------------------------------------------------------------------------- 1 | local d2d = require("d2d") 2 | 3 | local render = {} 4 | 5 | function render.init(w) 6 | d2d.init(w) 7 | end 8 | 9 | function render.destroy() 10 | d2d.destroy() 11 | end 12 | 13 | function render.begin_draw() 14 | d2d.begin_draw() 15 | end 16 | 17 | function render.end_draw() 18 | d2d.end_draw() 19 | end 20 | 21 | function render.draw_box(x, y, width, height, color) 22 | d2d.draw_box(x, y, width, height, color) 23 | end 24 | 25 | function render.draw_text(x, y, text) 26 | d2d.draw_text(x, y, text) 27 | end 28 | 29 | function render.log(text) 30 | local str = tostring(text) 31 | d2d.error(str) 32 | end 33 | 34 | return render -------------------------------------------------------------------------------- /C06/tetris/game/modules/logic/block/lshape.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local base = require("modules.logic.block.base") 3 | local const = require("modules.const") 4 | 5 | render.log("lshape class " .. tostring(base)) 6 | local lshape = base:inherit() 7 | 8 | function lshape:init() 9 | base.init(self) 10 | self.center_idx = 3 11 | self.type = const.BLOCK_TYPE.LSHAPE 12 | 13 | if self:try_flip() then 14 | self.vertexes = { 15 | {x = 0, y = 2}, 16 | {x = 0, y = 1}, 17 | {x = 0, y = 0}, 18 | {x = 1, y = 0}, 19 | } 20 | else 21 | self.vertexes = { 22 | {x = 0, y = 2}, 23 | {x = 0, y = 1}, 24 | {x = 0, y = 0}, 25 | {x = -1, y = 0}, 26 | } 27 | end 28 | end 29 | 30 | return lshape -------------------------------------------------------------------------------- /C06/tetris/game/modules/logic/block/square.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local base = require("modules.logic.block.base") 3 | local const = require("modules.const") 4 | 5 | local square = base:inherit() 6 | 7 | function square:init() 8 | base.init(self) 9 | self.center_idx = 1 10 | self.type = const.BLOCK_TYPE.SQUARE 11 | 12 | self.vertexes = { 13 | {x = 0, y = 0}, 14 | {x = 0, y = 1}, 15 | {x = 1, y = 1}, 16 | {x = 1, y = 0}, 17 | } 18 | end 19 | 20 | function square:rotate90() 21 | end 22 | 23 | return square -------------------------------------------------------------------------------- /C06/tetris/game/modules/logic/block/stick.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local base = require("modules.logic.block.base") 3 | local const = require("modules.const") 4 | 5 | local stick = base:inherit() 6 | 7 | function stick:init() 8 | base.init(self) 9 | self.center_idx = 3 10 | self.type = const.BLOCK_TYPE.STICK 11 | 12 | self.vertexes = { 13 | {x = 0, y = -2}, 14 | {x = 0, y = -1}, 15 | {x = 0, y = 0}, 16 | {x = 0, y = 1}, 17 | } 18 | end 19 | 20 | return stick -------------------------------------------------------------------------------- /C06/tetris/game/modules/logic/block/tshape.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local base = require("modules.logic.block.base") 3 | local const = require("modules.const") 4 | 5 | local tshape = base:inherit() 6 | 7 | function tshape:init() 8 | base.init(self) 9 | self.center_idx = 2 10 | self.type = const.BLOCK_TYPE.TSHAPE 11 | 12 | self.vertexes = { 13 | {x = -1, y = 0}, 14 | {x = 0, y = 0}, 15 | {x = 1, y = 0}, 16 | {x = 0, y = 1}, 17 | } 18 | end 19 | 20 | return tshape -------------------------------------------------------------------------------- /C06/tetris/game/modules/logic/block/zshape.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local base = require("modules.logic.block.base") 3 | local const = require("modules.const") 4 | 5 | local zshape = base:inherit() 6 | 7 | function zshape:init() 8 | base.init(self) 9 | self.center_idx = 3 10 | self.type = const.BLOCK_TYPE.ZSHAPE 11 | 12 | if self:try_flip() then 13 | self.vertexes = { 14 | {x = -1, y = 0}, 15 | {x = 0, y = 1}, 16 | {x = 0, y = 0}, 17 | {x = 1, y = 1}, 18 | } 19 | else 20 | self.vertexes = { 21 | {x = -1, y = 1}, 22 | {x = 0, y = 1}, 23 | {x = 0, y = 0}, 24 | {x = 1, y = 0}, 25 | } 26 | end 27 | end 28 | 29 | return zshape -------------------------------------------------------------------------------- /C06/tetris/game/modules/ui/mgr.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local object = require("modules.core.object") 3 | local score_class = require("modules.ui.score") 4 | local tips_class = require("modules.ui.tips") 5 | local const = require("modules.const") 6 | 7 | local uimgr = object:inherit() 8 | 9 | function uimgr:init() 10 | self.score = score_class:new() 11 | self.tips = tips_class:new() 12 | self.game_status = const.GAME_STATUS.RUNNING 13 | render.log("uimgr.init success") 14 | end 15 | 16 | function uimgr:reset() 17 | self.score:reset() 18 | self.game_status = const.GAME_STATUS.RUNNING 19 | render.log("uimgr.reset") 20 | end 21 | 22 | function uimgr:update_score(v) 23 | self.score:set_score(v) 24 | end 25 | 26 | function uimgr:get_downward_gap() 27 | return self.score:get_downward_gap() 28 | end 29 | 30 | function uimgr:draw() 31 | self.score:draw() 32 | self.tips:draw() 33 | 34 | if self.game_status == const.GAME_STATUS.GAME_OVER then 35 | render.draw_text(250, 300, "Game Over, press enter to restart") 36 | elseif self.game_status == const.GAME_STATUS.PAUSE then 37 | render.draw_text(350, 300, "Game Pause") 38 | end 39 | end 40 | 41 | function uimgr:set_game_status(status) 42 | self.game_status = status 43 | end 44 | 45 | return uimgr -------------------------------------------------------------------------------- /C06/tetris/game/modules/ui/score.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local object = require("modules.core.object") 3 | 4 | local score = object:inherit() 5 | 6 | function score:init() 7 | self:reset() 8 | render.log("score|init|success") 9 | end 10 | 11 | function score:reset() 12 | self.value = 0 13 | self.x = 0 14 | self.y = 0 15 | end 16 | 17 | function score:set_score(v) 18 | self.value = v 19 | end 20 | 21 | function score:draw() 22 | local str = "score:" .. tostring(self.value) 23 | render.draw_text(self.x, self.y, str) 24 | end 25 | 26 | function score:get_downward_gap() 27 | if self.value <= 200 then 28 | return 1000 29 | elseif self.value <= 500 then 30 | return 800 31 | elseif self.value <= 1000 then 32 | return 600 33 | else 34 | return 400 35 | end 36 | end 37 | 38 | return score -------------------------------------------------------------------------------- /C06/tetris/game/modules/ui/tips.lua: -------------------------------------------------------------------------------- 1 | local render = require("modules.core.render") 2 | local object = require("modules.core.object") 3 | 4 | local tips = object:inherit() 5 | 6 | function tips:init() 7 | self.x = 0 8 | self.y = 100 9 | self.text = "key press tips:\nw:rotate\ns:speedup\na:move to left\nd:move to right\nesc:exit game\nspace:pause/resume\nenter:restart" 10 | end 11 | 12 | function tips:draw() 13 | render.draw_text(self.x, self.y, self.text) 14 | end 15 | 16 | return tips -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Manistein 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 | --------------------------------------------------------------------------------