From 0b55e12f7f9fc98d22b2bead44d064af8c3f92c4 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 4 Sep 2021 12:24:57 -0500 Subject: remove va_nargs.h and begin adding examples --- CMakeLists.txt | 25 +++++++- examples/argument_parsing/#argument_parsing.c# | 28 +++++++++ examples/argument_parsing/argument_parsing.c | 23 ++++++++ examples/table_creation/a.out | Bin 0 -> 26712 bytes examples/table_creation/table_creation.c | 28 +++++++++ src/honeysuckle.h | 78 ++++++++++++++++++++++++- src/va_nargs.h | 75 ------------------------ 7 files changed, 180 insertions(+), 77 deletions(-) create mode 100644 examples/argument_parsing/#argument_parsing.c# create mode 100644 examples/argument_parsing/argument_parsing.c create mode 100755 examples/table_creation/a.out create mode 100644 examples/table_creation/table_creation.c delete mode 100644 src/va_nargs.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6431b09..d9776cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.2) project(honeysuckle - VERSION 1.0.0 + VERSION 0.1.0 DESCRIPTION "A C library to make writing lua bindings simple") find_package(Lua51 REQUIRED) @@ -25,6 +25,7 @@ set_target_properties(honeysuckle PROPERTIES VERSION ${PROJECT_VERSION} PUBLIC_HEADER src/honeysuckle.h) + # optionally build the tests set(TEST_ROOT ${CMAKE_SOURCE_DIR}/src/tests) set(TEST_SOURCES @@ -47,6 +48,28 @@ set_target_properties(test PROPERTIES CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") target_link_libraries(test ${LUA_LIBRARIES} honeysuckle) + +# optionally build the examples +set(EX_ROOT ${CMAKE_SOURCE_DIR}/examples) + +add_executable(example_table_creation EXCLUDE_FROM_ALL + ${EX_ROOT}/table_creation/table_creation.c) +set_target_properties(example_table_creation PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_table_creation ${LUA_LIBRARIES} honeysuckle) + +add_executable(example_argument_parsing EXCLUDE_FROM_ALL + ${EX_ROOT}/argument_parsing/argument_parsing.c) +set_target_properties(example_argument_parsing PROPERTIES + C_STANDARD 99 + CMAKE_C_FLAGS "-Wall -Wextra -Werror -Wfatal-errors -Wpedantic") +target_link_libraries(example_argument_parsing ${LUA_LIBRARIES} honeysuckle) + +add_custom_target(examples) +add_dependencies(examples example_table_creation example_argument_parsing) + + include(GNUInstallDirs) install(TARGETS honeysuckle diff --git a/examples/argument_parsing/#argument_parsing.c# b/examples/argument_parsing/#argument_parsing.c# new file mode 100644 index 0000000..38309db --- /dev/null +++ b/examples/argument_parsing/#argument_parsing.c# @@ -0,0 +1,28 @@ +#include + +int demo(lua_State *L) +{ + lua_Integer i; + lua_Number n; + char *string; + hs_parse_args(L, hs_int(i), hs_num(n), hs_str(string)); + printf("received %d, %f, and %s\n", i, n, string); + return 0; +} + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, demo); + lua_setglobal(L, "demo"); + + luaL_dostring("demo(12, 34.4, 'hi there!')"); + return 0; +} + +grindr +hinge +bumble +lex diff --git a/examples/argument_parsing/argument_parsing.c b/examples/argument_parsing/argument_parsing.c new file mode 100644 index 0000000..11a48f4 --- /dev/null +++ b/examples/argument_parsing/argument_parsing.c @@ -0,0 +1,23 @@ +#include + +int demo(lua_State *L) +{ + lua_Integer i; + lua_Number n; + char *string; + hs_parse_args(L, hs_int(i), hs_num(n), hs_str(string)); + printf("received %ld, %f, and %s\n", i, n, string); + return 0; +} + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + lua_pushcfunction(L, demo); + lua_setglobal(L, "demo"); + + luaL_dostring(L, "demo(12, 34.4, 'hi there!')"); + return 0; +} diff --git a/examples/table_creation/a.out b/examples/table_creation/a.out new file mode 100755 index 0000000..0e506e3 Binary files /dev/null and b/examples/table_creation/a.out differ diff --git a/examples/table_creation/table_creation.c b/examples/table_creation/table_creation.c new file mode 100644 index 0000000..42088c6 --- /dev/null +++ b/examples/table_creation/table_creation.c @@ -0,0 +1,28 @@ +#include + +int main() +{ + lua_State *L = luaL_newstate(); + luaL_openlibs(L); + + /* create the table + * { "one"=1, "two"=2, "half"=0.5, "runme"=[some function] } + */ + luaL_dostring(L, + "return function(self) \n" \ + "print(self.one)\n" \ + "print(self.two)\n" \ + "print(self.half)\n" \ + "end"); + int func_index = lua_gettop(L); + + hs_create_table(L, + hs_str_int("one", 1), + hs_str_int("two", 2), + hs_str_num("half", 0.5f), + hs_str_func("runme", func_index)); + lua_setglobal(L, "runnable"); + + luaL_dostring(L, "runnable:runme()"); + return 0; +} diff --git a/src/honeysuckle.h b/src/honeysuckle.h index 233e3cd..96243ba 100644 --- a/src/honeysuckle.h +++ b/src/honeysuckle.h @@ -7,7 +7,83 @@ #include #include -#include "va_nargs.h" +/* variadic macro argument counting */ +#ifndef VA_NARGS + +#define VA_NARGS(...) VA_NARGS_SHIFT_COUNT(__VA_ARGS__, VA_NARGS_COUNTS) + +#define VA_NARGS_COUNTS \ + 255, 254, 253, 252, 251, 250, 249, 248, \ + 247, 246, 245, 244, 243, 242, 241, 240, \ + 239, 238, 237, 236, 235, 234, 233, 232, \ + 231, 230, 229, 228, 227, 226, 225, 224, \ + 223, 222, 221, 220, 219, 218, 217, 216, \ + 215, 214, 213, 212, 211, 210, 209, 208, \ + 207, 206, 205, 204, 203, 202, 201, 200, \ + 199, 198, 197, 196, 195, 194, 193, 192, \ + 191, 190, 189, 188, 187, 186, 185, 184, \ + 183, 182, 181, 180, 179, 178, 177, 176, \ + 175, 174, 173, 172, 171, 170, 169, 168, \ + 167, 166, 165, 164, 163, 162, 161, 160, \ + 159, 158, 157, 156, 155, 154, 153, 152, \ + 151, 150, 149, 148, 147, 146, 145, 144, \ + 143, 142, 141, 140, 139, 138, 137, 136, \ + 135, 134, 133, 132, 131, 130, 129, 128, \ + 127, 126, 125, 124, 123, 122, 121, 120, \ + 119, 118, 117, 116, 115, 114, 113, 112, \ + 111, 110, 109, 108, 107, 106, 105, 104, \ + 103, 102, 101, 100, 99, 98, 97, 96, \ + 95, 94, 93, 92, 91, 90, 89, 88, \ + 87, 86, 85, 84, 83, 82, 81, 80, \ + 79, 78, 77, 76, 75, 74, 73, 72, \ + 71, 70, 69, 68, 67, 66, 65, 64, \ + 63, 62, 61, 60, 59, 58, 57, 56, \ + 55, 54, 53, 52, 51, 50, 49, 48, \ + 47, 46, 45, 44, 43, 42, 41, 40, \ + 39, 38, 37, 36, 35, 34, 33, 32, \ + 31, 30, 29, 28, 27, 26, 25, 24, \ + 23, 22, 21, 20, 19, 18, 17, 16, \ + 15, 14, 13, 12, 11, 10, 9, 8, \ + 7, 6, 5, 4, 3, 2, 1, 0 + +#define VA_NARGS_GET_COUNT( \ + _255, _254, _253, _252, _251, _250, _249, _248, \ + _247, _246, _245, _244, _243, _242, _241, _240, \ + _239, _238, _237, _236, _235, _234, _233, _232, \ + _231, _230, _229, _228, _227, _226, _225, _224, \ + _223, _222, _221, _220, _219, _218, _217, _216, \ + _215, _214, _213, _212, _211, _210, _209, _208, \ + _207, _206, _205, _204, _203, _202, _201, _200, \ + _199, _198, _197, _196, _195, _194, _193, _192, \ + _191, _190, _189, _188, _187, _186, _185, _184, \ + _183, _182, _181, _180, _179, _178, _177, _176, \ + _175, _174, _173, _172, _171, _170, _169, _168, \ + _167, _166, _165, _164, _163, _162, _161, _160, \ + _159, _158, _157, _156, _155, _154, _153, _152, \ + _151, _150, _149, _148, _147, _146, _145, _144, \ + _143, _142, _141, _140, _139, _138, _137, _136, \ + _135, _134, _133, _132, _131, _130, _129, _128, \ + _127, _126, _125, _124, _123, _122, _121, _120, \ + _119, _118, _117, _116, _115, _114, _113, _112, \ + _111, _110, _109, _108, _107, _106, _105, _104, \ + _103, _102, _101, _100, _99, _98, _97, _96, \ + _95, _94, _93, _92, _91, _90, _89, _88, \ + _87, _86, _85, _84, _83, _82, _81, _80, \ + _79, _78, _77, _76, _75, _74, _73, _72, \ + _71, _70, _69, _68, _67, _66, _65, _64, \ + _63, _62, _61, _60, _59, _58, _57, _56, \ + _55, _54, _53, _52, _51, _50, _49, _48, \ + _47, _46, _45, _44, _43, _42, _41, _40, \ + _39, _38, _37, _36, _35, _34, _33, _32, \ + _31, _30, _29, _28, _27, _26, _25, _24, \ + _23, _22, _21, _20, _19, _18, _17, _16, \ + _15, _14, _13, _12, _11, _10, _9, _8, \ + _7, _6, _5, _4, _3, _2, _1, N, ...) N + +#define VA_NARGS_SHIFT_COUNT(...) VA_NARGS_GET_COUNT(__VA_ARGS__) + +#endif + /* type constants */ typedef enum diff --git a/src/va_nargs.h b/src/va_nargs.h deleted file mode 100644 index 0037445..0000000 --- a/src/va_nargs.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef VA_NARGS - -#define VA_NARGS(...) VA_NARGS_SHIFT_COUNT(__VA_ARGS__, VA_NARGS_COUNTS) - -#define VA_NARGS_COUNTS \ - 255, 254, 253, 252, 251, 250, 249, 248, \ - 247, 246, 245, 244, 243, 242, 241, 240, \ - 239, 238, 237, 236, 235, 234, 233, 232, \ - 231, 230, 229, 228, 227, 226, 225, 224, \ - 223, 222, 221, 220, 219, 218, 217, 216, \ - 215, 214, 213, 212, 211, 210, 209, 208, \ - 207, 206, 205, 204, 203, 202, 201, 200, \ - 199, 198, 197, 196, 195, 194, 193, 192, \ - 191, 190, 189, 188, 187, 186, 185, 184, \ - 183, 182, 181, 180, 179, 178, 177, 176, \ - 175, 174, 173, 172, 171, 170, 169, 168, \ - 167, 166, 165, 164, 163, 162, 161, 160, \ - 159, 158, 157, 156, 155, 154, 153, 152, \ - 151, 150, 149, 148, 147, 146, 145, 144, \ - 143, 142, 141, 140, 139, 138, 137, 136, \ - 135, 134, 133, 132, 131, 130, 129, 128, \ - 127, 126, 125, 124, 123, 122, 121, 120, \ - 119, 118, 117, 116, 115, 114, 113, 112, \ - 111, 110, 109, 108, 107, 106, 105, 104, \ - 103, 102, 101, 100, 99, 98, 97, 96, \ - 95, 94, 93, 92, 91, 90, 89, 88, \ - 87, 86, 85, 84, 83, 82, 81, 80, \ - 79, 78, 77, 76, 75, 74, 73, 72, \ - 71, 70, 69, 68, 67, 66, 65, 64, \ - 63, 62, 61, 60, 59, 58, 57, 56, \ - 55, 54, 53, 52, 51, 50, 49, 48, \ - 47, 46, 45, 44, 43, 42, 41, 40, \ - 39, 38, 37, 36, 35, 34, 33, 32, \ - 31, 30, 29, 28, 27, 26, 25, 24, \ - 23, 22, 21, 20, 19, 18, 17, 16, \ - 15, 14, 13, 12, 11, 10, 9, 8, \ - 7, 6, 5, 4, 3, 2, 1, 0 - -#define VA_NARGS_GET_COUNT( \ - _255, _254, _253, _252, _251, _250, _249, _248, \ - _247, _246, _245, _244, _243, _242, _241, _240, \ - _239, _238, _237, _236, _235, _234, _233, _232, \ - _231, _230, _229, _228, _227, _226, _225, _224, \ - _223, _222, _221, _220, _219, _218, _217, _216, \ - _215, _214, _213, _212, _211, _210, _209, _208, \ - _207, _206, _205, _204, _203, _202, _201, _200, \ - _199, _198, _197, _196, _195, _194, _193, _192, \ - _191, _190, _189, _188, _187, _186, _185, _184, \ - _183, _182, _181, _180, _179, _178, _177, _176, \ - _175, _174, _173, _172, _171, _170, _169, _168, \ - _167, _166, _165, _164, _163, _162, _161, _160, \ - _159, _158, _157, _156, _155, _154, _153, _152, \ - _151, _150, _149, _148, _147, _146, _145, _144, \ - _143, _142, _141, _140, _139, _138, _137, _136, \ - _135, _134, _133, _132, _131, _130, _129, _128, \ - _127, _126, _125, _124, _123, _122, _121, _120, \ - _119, _118, _117, _116, _115, _114, _113, _112, \ - _111, _110, _109, _108, _107, _106, _105, _104, \ - _103, _102, _101, _100, _99, _98, _97, _96, \ - _95, _94, _93, _92, _91, _90, _89, _88, \ - _87, _86, _85, _84, _83, _82, _81, _80, \ - _79, _78, _77, _76, _75, _74, _73, _72, \ - _71, _70, _69, _68, _67, _66, _65, _64, \ - _63, _62, _61, _60, _59, _58, _57, _56, \ - _55, _54, _53, _52, _51, _50, _49, _48, \ - _47, _46, _45, _44, _43, _42, _41, _40, \ - _39, _38, _37, _36, _35, _34, _33, _32, \ - _31, _30, _29, _28, _27, _26, _25, _24, \ - _23, _22, _21, _20, _19, _18, _17, _16, \ - _15, _14, _13, _12, _11, _10, _9, _8, \ - _7, _6, _5, _4, _3, _2, _1, N, ...) N - -#define VA_NARGS_SHIFT_COUNT(...) VA_NARGS_GET_COUNT(__VA_ARGS__) - -#endif -- cgit v1.2.1