diff options
author | James R. McKaskill <james@foobar.co.nz> | 2012-04-25 09:00:30 +0400 |
---|---|---|
committer | James R. McKaskill <james@foobar.co.nz> | 2012-04-25 09:00:30 +0400 |
commit | fdf4985c47c03d470762eaaf90355ed687e00e60 (patch) | |
tree | 98f31f0a6684791e99b9c178173cf2e5cd374bb4 /parser.c | |
parent | 141144fd46e42a59dfd3fe42762a5b9b7a014c08 (diff) |
Handle casts to (int) in constants
Diffstat (limited to 'parser.c')
-rw-r--r-- | parser.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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); |