issue #76: work on type assertion

pull/81/head
Dibyendu Majumdar 9 years ago
parent 60ba32546e
commit aa453e0aae

@ -196,7 +196,7 @@ if (LLVM_JIT)
endif ()
#Main library
add_library(ravi SHARED
add_library(libravi SHARED
${RAVI_HEADERS}
${LUA_LIB_SRCS}
${LUA_CORE_SRCS}
@ -205,22 +205,21 @@ add_library(ravi SHARED
${NO_JIT_SRCS})
if (WIN32)
# enable DLL export
set_target_properties(ravi PROPERTIES DEFINE_SYMBOL "LUA_BUILD_AS_DLL")
set_target_properties(libravi PROPERTIES DEFINE_SYMBOL "LUA_BUILD_AS_DLL")
endif ()
target_link_libraries(ravi ${EXTRA_LIBRARIES} ${LLVM_LIBS} ${GCCJIT_LIBRARIES})
add_executable(ravi-bin src/lua.c)
set_target_properties(ravi-bin PROPERTIES OUTPUT_NAME ravi)
target_link_libraries(ravi-bin ravi)
add_executable(ravi src/lua.c)
target_link_libraries(ravi libravi)
#VM test
add_executable(test_vm tests/test_vm.c)
target_link_libraries(test_vm ravi)
target_link_libraries(test_vm libravi)
if (LLVM_JIT)
#LLVM playground
add_executable(test_llvm tests/test_llvm.cpp)
target_link_libraries(test_llvm ravi)
target_link_libraries(test_llvm libravi)
endif ()
add_executable(test_misc tests/test_misc.c)
@ -233,7 +232,7 @@ add_test(TestMisc test_misc)
install(FILES ${LUA_HEADERS}
DESTINATION include/ravi)
install(TARGETS ravi ravi-bin
install(TARGETS libravi ravi
RUNTIME DESTINATION bin
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib)

@ -37,7 +37,8 @@ typedef enum BinOpr {
} BinOpr;
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_TO_INTEGER,
OPR_TO_NUMBER, OPR_TO_INTARRAY, OPR_TO_NUMARRAY, OPR_TO_TABLE, OPR_NOUNOPR } UnOpr;
#define getcode(fs,e) ((fs)->f->code[(e)->u.info])

@ -33,7 +33,9 @@ enum RESERVED {
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
TK_SHL, TK_SHR,
TK_DBCOLON, TK_EOS,
TK_FLT, TK_INT, TK_NAME, TK_STRING
TK_FLT, TK_INT, TK_NAME, TK_STRING,
TK_TO_INTEGER, TK_TO_NUMBER, TK_TO_INTARRAY, TK_TO_NUMARRAY,
TK_TO_TABLE
};
/* number of reserved words */

@ -1177,6 +1177,60 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
e1->ravi_type = RAVI_TANY;
}
static void codecast(FuncState *fs, UnOpr op, expdesc *e, int line) {
luaK_dischargevars(fs, e);
switch (e->k) {
case VKFLT: {
if (op == OPR_TO_NUMBER) {
e->ravi_type = RAVI_TNUMFLT; /* RAVI TODO*/
return;
}
break;
}
case VKINT: {
if (op == OPR_TO_INTEGER) {
e->ravi_type = RAVI_TNUMINT; /* RAVI TODO*/
return;
}
break;
}
case VRELOCABLE:
case VNONRELOC: {
discharge2anyreg(fs, e);
freeexp(fs, e);
OpCode opcode;
ravitype_t tt;
if (op == OPR_TO_NUMBER && e->ravi_type != RAVI_TNUMFLT) {
opcode = OP_RAVI_TOFLT;
tt = RAVI_TNUMFLT;
}
else if (op == OPR_TO_INTEGER && e->ravi_type != RAVI_TNUMINT) {
opcode = OP_RAVI_TOINT;
tt = RAVI_TNUMINT;
}
else if (op == OPR_TO_INTARRAY && e->ravi_type != RAVI_TARRAYINT) {
opcode = OP_RAVI_TOARRAYI;
tt = RAVI_TARRAYINT;
}
else if (op == OPR_TO_NUMARRAY && e->ravi_type != RAVI_TARRAYFLT) {
opcode = OP_RAVI_TOARRAYF;
tt = RAVI_TARRAYFLT;
}
else if (op == OPR_TO_TABLE && e->ravi_type != RAVI_TTABLE) {
opcode = OP_RAVI_TOTAB;
tt = RAVI_TTABLE;
}
else {
/* noting to do*/
return;
}
luaK_codeABC(fs, opcode, e->u.info, 0, 0);
e->ravi_type = tt; /* RAVI TODO */
return;
}
}
luaX_syntaxerror(fs->ls, "invalid type assertion"); /* cannot happen */
}
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
expdesc e2;
@ -1187,6 +1241,9 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
codeexpval(fs, cast(OpCode, (op - OPR_MINUS) + OP_UNM), e, &e2, line);
break;
}
case OPR_TO_INTEGER: case OPR_TO_NUMBER: case OPR_TO_INTARRAY:
case OPR_TO_NUMARRAY: case OPR_TO_TABLE:
codecast(fs, op, e, line); break;
case OPR_NOT: codenot(fs, e); break;
default: lua_assert(0);
}

@ -44,7 +44,9 @@ static const char *const luaX_tokens [] = {
"return", "then", "true", "until", "while",
"//", "..", "...", "==", ">=", "<=", "~=",
"<<", ">>", "::", "<eof>",
"<number>", "<integer>", "<name>", "<string>"
"<number>", "<integer>", "<name>", "<string>",
"@integer", "@number", "@intarray", "@numarray",
"@table"
};
@ -206,6 +208,18 @@ static int check_next2 (LexState *ls, const char *set) {
else return 0;
}
/*
** Check whether current char is in set 'set' (with three chars) and
** saves it
*/
static int check_next3(LexState *ls, const char *set) {
lua_assert(set[3] == '\0');
if (ls->current == set[0] || ls->current == set[1] || ls->current == set[2]) {
save_and_next(ls);
return 1;
}
else return 0;
}
/*
** change all characters 'from' in buffer to 'to'
@ -551,6 +565,36 @@ static int llex (LexState *ls, SemInfo *seminfo) {
case EOZ: {
return TK_EOS;
}
case '@': {
next(ls);
if (check_next3(ls, "int")) {
char temp[10];
int n, n1;
while (lislalnum(ls->current)) {
save_and_next(ls);
}
n = luaZ_bufflen(ls->buff);
n1 = n > ((sizeof temp)-1) ? ((sizeof temp)-1) : n;
strncpy(temp, luaZ_buffer(ls->buff), n1+1);
luaZ_buffremove(ls->buff, n);
if (strncmp(temp, "integer", n1) == 0)
return TK_TO_INTEGER;
else if (strncmp(temp, "number", n1) == 0)
return TK_TO_NUMBER;
else if (strncmp(temp, "numarray", n1) == 0)
return TK_TO_NUMARRAY;
else if (strncmp(temp, "intarray", n1) == 0)
return TK_TO_INTARRAY;
else if (strncmp(temp, "table", n1) == 0)
return TK_TO_TABLE;
else {
return '@';
}
}
else {
return '@';
}
}
default: {
if (lislalpha(ls->current)) { /* identifier or reserved word? */
TString *ts;

@ -173,6 +173,7 @@ void raviY_printf(FuncState *fs, const char *format, ...) {
char buf[80] = {0};
va_list ap;
const char *cp;
printf("%s(%d) ", getstr(fs->ls->source), fs->ls->lastline);
va_start(ap, format);
for (cp = format; *cp; cp++) {
if (cp[0] == '%' && cp[1] == 'e') {
@ -1511,6 +1512,11 @@ static UnOpr getunopr (int op) {
case '-': return OPR_MINUS;
case '~': return OPR_BNOT;
case '#': return OPR_LEN;
case TK_TO_INTEGER: return OPR_TO_INTEGER;
case TK_TO_NUMBER: return OPR_TO_NUMBER;
case TK_TO_INTARRAY: return OPR_TO_INTARRAY;
case TK_TO_NUMARRAY: return OPR_TO_NUMARRAY;
case TK_TO_TABLE: return OPR_TO_TABLE;
default: return OPR_NOUNOPR;
}
}

@ -19,7 +19,6 @@
#include "lauxlib.h"
#include "lualib.h"
#if !defined(LUA_PROMPT)
#define LUA_PROMPT "> "
#define LUA_PROMPT2 ">> "

Loading…
Cancel
Save