twnlua: partial impl for return propagation, input now works
This commit is contained in:
		| @@ -31,27 +31,37 @@ def default(parameter): | ||||
|     raise BaseException("Unhandled default value of type '%s'" % parameter["type"]) | ||||
|  | ||||
|  | ||||
| print('#include "twn_game_api.h"') | ||||
| print('#include "twn_game_api.h"\n') | ||||
| print('#include <lua.h>') | ||||
| print('#include <lualib.h>') | ||||
| print('#include <lauxlib.h>\n') | ||||
| print('#include <string.h>\n') | ||||
|  | ||||
|  | ||||
| for typename, typedesc in api["types"].items(): | ||||
|     converter = "static %s table_to_%s(lua_State *L, int idx) {\n" % (typename, typename.lower()) | ||||
|     converter = "static %s to_%s(lua_State *L, int idx) {\n" % (typename, typename.lower()) | ||||
|     converter += "    %s %s;\n" % (typename, typename.lower()); | ||||
|  | ||||
|     if not "fields" in typedesc: | ||||
|         continue | ||||
|     if "fields" in typedesc: | ||||
|         for field in typedesc["fields"]: | ||||
|             converter += "    lua_getfield(L, idx, \"%s\");\n" % (field["name"]); | ||||
|             if field["type"] == "float": | ||||
|                 converter += "    %s.%s = (float)lua_tonumber(L, -1);\n" % (typename.lower(), field["name"]); | ||||
|             elif field["type"] == "uint8_t": | ||||
|                 converter += "    %s.%s = (uint8_t)lua_tointeger(L, -1);\n" % (typename.lower(), field["name"]); | ||||
|             else: | ||||
|                 raise BaseException("Unhandled converter field type '%s'" % (field["type"])) | ||||
|             converter += "    lua_pop(L, 1);\n"; | ||||
|  | ||||
|     for field in typedesc["fields"]: | ||||
|         converter += "    lua_getfield(L, idx, \"%s\");\n" % (field["name"]); | ||||
|         if field["type"] == "float": | ||||
|             converter += "    %s.%s = (float)lua_tonumber(L, -1);\n" % (typename.lower(), field["name"]); | ||||
|         elif field["type"] == "uint8_t": | ||||
|             converter += "    %s.%s = (uint8_t)lua_tointeger(L, -1);\n" % (typename.lower(), field["name"]); | ||||
|         else: | ||||
|             raise BaseException("Unhandled converter field type '%s'" % (field["type"])) | ||||
|     # todo: use a hashtable instead | ||||
|     elif "enums" in typedesc: | ||||
|         converter += "    char *value = lua_tostring(L, -1);\n"; | ||||
|         for index, enum in enumerate(typedesc["enums"]): | ||||
|             if index == 0: | ||||
|                 converter += "    if (strncmp(\"%s\", value, sizeof(\"%s\")) == 0)\n" % (enum, enum) | ||||
|             else: | ||||
|                 converter += "    else if (strncmp(\"%s\", value, sizeof(\"%s\")) == 0)\n" % (enum, enum) | ||||
|             converter += "        %s = %s;\n" % (typename.lower(), typedesc["enums"][enum]) | ||||
|         converter += "    lua_pop(L, 1);\n"; | ||||
|  | ||||
|     converter += "    return %s;\n}\n" % (typename.lower()) | ||||
| @@ -83,17 +93,25 @@ for procedure, procedure_desc in api["procedures"].items(): | ||||
|             elif parameter["type"] == "char *": | ||||
|                 binding += "    %s = lua_tostring(L, -1);\n" % (parameter["name"]); | ||||
|             elif basetype in api["types"]: | ||||
|                 if "enums" in api["types"][basetype]: | ||||
|                     binding += "    %s = lua_tointeger(L, -1);\n" % (parameter["name"]); | ||||
|                 elif parameter["type"].endswith("*"): | ||||
|                     binding += "    { %s_value = table_to_%s(L, -1); %s = &%s_value; }\n" % (parameter["name"], basetype.lower(), parameter["name"], parameter["name"]); | ||||
|                 if parameter["type"].endswith("*"): | ||||
|                     binding += "    { %s_value = to_%s(L, -1); %s = &%s_value; }\n" % (parameter["name"], basetype.lower(), parameter["name"], parameter["name"]); | ||||
|                 else: | ||||
|                     binding += "    %s = table_to_%s(L, -1);\n" % (parameter["name"], basetype.lower()); | ||||
|                     binding += "    %s = to_%s(L, -1);\n" % (parameter["name"], basetype.lower()); | ||||
|             else: | ||||
|                 raise BaseException("Unhandled parameter type '%s'" % (parameter["type"])) | ||||
|  | ||||
|     binding += "    %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) | ||||
|     binding += "}\n" | ||||
|     if "return" in procedure_desc: | ||||
|         if procedure_desc["return"] == "bool": | ||||
|             binding += "    lua_pushboolean(L, (int)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) | ||||
|         elif procedure_desc["return"] == "float": | ||||
|             binding += "    lua_pushnumber(L, (double)%s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) | ||||
|         else: | ||||
|             binding += "    lua_warning(L, \"Unhandled return type!\", 0);\n" | ||||
|         binding += "    return 1;\n}\n" | ||||
|     else: | ||||
|         binding += "    %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) | ||||
|         binding += "    return 0;\n}\n" | ||||
|  | ||||
|     print(binding) | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -4,6 +4,11 @@ offset = { x = 0, y = 0 } | ||||
| angle = 0 | ||||
|  | ||||
| function game_tick() | ||||
|     input.action { | ||||
|         name = "press", | ||||
|         control = "A" | ||||
|     } | ||||
|  | ||||
|     draw.rectangle { | ||||
|         rect = { x = 0, y = 0, w = 640, h = 360 }, | ||||
|         color = { r = 127, g = 0, b = 127, a = 255 }, | ||||
| @@ -19,11 +24,13 @@ function game_tick() | ||||
|         }, | ||||
|     } | ||||
|  | ||||
|     draw.text { | ||||
|         string = "it never happened", | ||||
|         position = offset, | ||||
|         font = "/fonts/kenney-pixel.ttf", | ||||
|     } | ||||
|     if input.action_pressed { name = "press" } then | ||||
|         draw.text { | ||||
|             string = "it never happened", | ||||
|             position = offset, | ||||
|             font = "/fonts/kenney-pixel.ttf", | ||||
|         } | ||||
|     end | ||||
|  | ||||
|     offset.x = ORIGIN.x + (math.cos(angle) * RADIUS) | ||||
|     offset.y = ORIGIN.y + (math.sin(angle) * RADIUS) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user