Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/facebook/luaffifb.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames R. McKaskill <james@foobar.co.nz>2012-04-25 09:00:30 +0400
committerJames R. McKaskill <james@foobar.co.nz>2012-04-25 09:00:30 +0400
commitfdf4985c47c03d470762eaaf90355ed687e00e60 (patch)
tree98f31f0a6684791e99b9c178173cf2e5cd374bb4 /parser.c
parent141144fd46e42a59dfd3fe42762a5b9b7a014c08 (diff)
Handle casts to (int) in constants
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/parser.c b/parser.c
index ece1fca..26b0d40 100644
--- a/parser.c
+++ b/parser.c
@@ -2089,6 +2089,30 @@ int ffi_cdef(lua_State* L)
* precedence and above. calculate_constant1 is the highest precedence
*/
+static int try_cast(lua_State* L)
+{
+ struct parser* P = (struct parser*) lua_touserdata(L, 1);
+ struct ctype ct;
+ struct token name, tok;
+ memset(&name, 0, sizeof(name));
+
+ parse_type(L, P, &ct);
+ parse_argument(L, P, -1, &ct, &name, NULL);
+
+ require_token(L, P, &tok);
+ if (tok.type != TOK_CLOSE_PAREN || name.size) {
+ return luaL_error(L, "invalid cast");
+ }
+
+ if (ct.pointers || ct.type != INT32_TYPE) {
+ return luaL_error(L, "unsupported cast on line %d", P->line);
+ }
+
+ return 0;
+}
+
+static int64_t calculate_constant2(lua_State* L, struct parser* P, struct token* tok);
+
/* () */
static int64_t calculate_constant1(lua_State* L, struct parser* P, struct token* tok)
{
@@ -2117,6 +2141,19 @@ static int64_t calculate_constant1(lua_State* L, struct parser* P, struct token*
return ret;
} else if (tok->type == TOK_OPEN_PAREN) {
+ struct parser before_cast = *P;
+ int top = lua_gettop(L);
+
+ /* see if this is a numeric cast, which we ignore */
+ lua_pushcfunction(L, &try_cast);
+ lua_pushlightuserdata(L, P);
+ if (!lua_pcall(L, 1, 0, 0)) {
+ next_token(L, P, tok);
+ return calculate_constant2(L, P, tok);
+ }
+ lua_settop(L, top);
+
+ *P = before_cast;
ret = calculate_constant(L, P);
require_token(L, P, tok);