diff options
-rw-r--r-- | lcode.c | 38 | ||||
-rw-r--r-- | llex.c | 47 | ||||
-rw-r--r-- | llex.h | 5 | ||||
-rw-r--r-- | lobject.c | 21 | ||||
-rw-r--r-- | lobject.h | 3 | ||||
-rw-r--r-- | lparser.c | 13 | ||||
-rw-r--r-- | lparser.h | 8 |
7 files changed, 98 insertions, 37 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.62 2012/08/16 17:34:28 roberto Exp $ | 2 | ** $Id: lcode.c,v 2.63 2013/04/15 15:43:34 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -30,7 +30,7 @@ | |||
30 | 30 | ||
31 | 31 | ||
32 | static int isnumeral(expdesc *e) { | 32 | static int isnumeral(expdesc *e) { |
33 | return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); | 33 | return (e->k == VKFLT && e->t == NO_JUMP && e->f == NO_JUMP); |
34 | } | 34 | } |
35 | 35 | ||
36 | 36 | ||
@@ -322,6 +322,13 @@ int luaK_stringK (FuncState *fs, TString *s) { | |||
322 | } | 322 | } |
323 | 323 | ||
324 | 324 | ||
325 | static int luaK_intK (FuncState *fs, lua_Integer n) { | ||
326 | TValue o; | ||
327 | setivalue(&o, n); | ||
328 | return addk(fs, &o, &o); | ||
329 | } | ||
330 | |||
331 | |||
325 | int luaK_numberK (FuncState *fs, lua_Number r) { | 332 | int luaK_numberK (FuncState *fs, lua_Number r) { |
326 | int n; | 333 | int n; |
327 | lua_State *L = fs->ls->L; | 334 | lua_State *L = fs->ls->L; |
@@ -333,8 +340,11 @@ int luaK_numberK (FuncState *fs, lua_Number r) { | |||
333 | n = addk(fs, L->top - 1, &o); | 340 | n = addk(fs, L->top - 1, &o); |
334 | L->top--; | 341 | L->top--; |
335 | } | 342 | } |
336 | else | 343 | else { |
337 | n = addk(fs, &o, &o); /* regular case */ | 344 | TValue k; |
345 | setnvalue(&k, r + 0.5); /* ???? (avoid some collisions with ints) */ | ||
346 | n = addk(fs, &k, &o); /* regular case */ | ||
347 | } | ||
338 | return n; | 348 | return n; |
339 | } | 349 | } |
340 | 350 | ||
@@ -432,10 +442,14 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { | |||
432 | luaK_codek(fs, reg, e->u.info); | 442 | luaK_codek(fs, reg, e->u.info); |
433 | break; | 443 | break; |
434 | } | 444 | } |
435 | case VKNUM: { | 445 | case VKFLT: { |
436 | luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); | 446 | luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); |
437 | break; | 447 | break; |
438 | } | 448 | } |
449 | case VKINT: { | ||
450 | luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); | ||
451 | break; | ||
452 | } | ||
439 | case VRELOCABLE: { | 453 | case VRELOCABLE: { |
440 | Instruction *pc = &getcode(fs, e); | 454 | Instruction *pc = &getcode(fs, e); |
441 | SETARG_A(*pc, reg); | 455 | SETARG_A(*pc, reg); |
@@ -537,12 +551,18 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { | |||
537 | } | 551 | } |
538 | else break; | 552 | else break; |
539 | } | 553 | } |
540 | case VKNUM: { | 554 | case VKINT: { |
555 | e->u.info = luaK_intK(fs, e->u.ival); | ||
556 | e->k = VK; | ||
557 | goto vk; | ||
558 | } | ||
559 | case VKFLT: { | ||
541 | e->u.info = luaK_numberK(fs, e->u.nval); | 560 | e->u.info = luaK_numberK(fs, e->u.nval); |
542 | e->k = VK; | 561 | e->k = VK; |
543 | /* go through */ | 562 | /* go through */ |
544 | } | 563 | } |
545 | case VK: { | 564 | case VK: { |
565 | vk: | ||
546 | if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ | 566 | if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ |
547 | return RKASK(e->u.info); | 567 | return RKASK(e->u.info); |
548 | else break; | 568 | else break; |
@@ -626,7 +646,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { | |||
626 | pc = e->u.info; | 646 | pc = e->u.info; |
627 | break; | 647 | break; |
628 | } | 648 | } |
629 | case VK: case VKNUM: case VTRUE: { | 649 | case VK: case VKFLT: case VKINT: case VTRUE: { |
630 | pc = NO_JUMP; /* always true; do nothing */ | 650 | pc = NO_JUMP; /* always true; do nothing */ |
631 | break; | 651 | break; |
632 | } | 652 | } |
@@ -671,7 +691,7 @@ static void codenot (FuncState *fs, expdesc *e) { | |||
671 | e->k = VTRUE; | 691 | e->k = VTRUE; |
672 | break; | 692 | break; |
673 | } | 693 | } |
674 | case VK: case VKNUM: case VTRUE: { | 694 | case VK: case VKFLT: case VKINT: case VTRUE: { |
675 | e->k = VFALSE; | 695 | e->k = VFALSE; |
676 | break; | 696 | break; |
677 | } | 697 | } |
@@ -760,7 +780,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, | |||
760 | 780 | ||
761 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { | 781 | void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { |
762 | expdesc e2; | 782 | expdesc e2; |
763 | e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; | 783 | e2.t = e2.f = NO_JUMP; e2.k = VKFLT; e2.u.nval = 0; |
764 | switch (op) { | 784 | switch (op) { |
765 | case OPR_MINUS: { | 785 | case OPR_MINUS: { |
766 | if (isnumeral(e)) /* minus constant? */ | 786 | if (isnumeral(e)) /* minus constant? */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.c,v 2.62 2012/12/05 19:57:00 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp roberto $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -39,7 +39,7 @@ static const char *const luaX_tokens [] = { | |||
39 | "in", "local", "nil", "not", "or", "repeat", | 39 | "in", "local", "nil", "not", "or", "repeat", |
40 | "return", "then", "true", "until", "while", | 40 | "return", "then", "true", "until", "while", |
41 | "..", "...", "==", ">=", "<=", "~=", "::", "<eof>", | 41 | "..", "...", "==", ">=", "<=", "~=", "::", "<eof>", |
42 | "<number>", "<name>", "<string>" | 42 | "<number>", "<number>", "<name>", "<string>" |
43 | }; | 43 | }; |
44 | 44 | ||
45 | 45 | ||
@@ -90,9 +90,8 @@ const char *luaX_token2str (LexState *ls, int token) { | |||
90 | 90 | ||
91 | static const char *txtToken (LexState *ls, int token) { | 91 | static const char *txtToken (LexState *ls, int token) { |
92 | switch (token) { | 92 | switch (token) { |
93 | case TK_NAME: | 93 | case TK_NAME: case TK_STRING: |
94 | case TK_STRING: | 94 | case TK_FLT: case TK_INT: |
95 | case TK_NUMBER: | ||
96 | save(ls, '\0'); | 95 | save(ls, '\0'); |
97 | return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); | 96 | return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); |
98 | default: | 97 | default: |
@@ -216,7 +215,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { | |||
216 | if (!buff2d(ls->buff, &seminfo->r)) { | 215 | if (!buff2d(ls->buff, &seminfo->r)) { |
217 | /* format error with correct decimal point: no more options */ | 216 | /* format error with correct decimal point: no more options */ |
218 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ | 217 | buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ |
219 | lexerror(ls, "malformed number", TK_NUMBER); | 218 | lexerror(ls, "malformed number", TK_FLT); |
220 | } | 219 | } |
221 | } | 220 | } |
222 | 221 | ||
@@ -224,9 +223,10 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { | |||
224 | /* LUA_NUMBER */ | 223 | /* LUA_NUMBER */ |
225 | /* | 224 | /* |
226 | ** this function is quite liberal in what it accepts, as 'luaO_str2d' | 225 | ** this function is quite liberal in what it accepts, as 'luaO_str2d' |
227 | ** will reject ill-formed numerals. | 226 | ** will reject ill-formed numerals. 'isf' means the numeral is not |
227 | ** an integer (it has a dot or an exponent). | ||
228 | */ | 228 | */ |
229 | static void read_numeral (LexState *ls, SemInfo *seminfo) { | 229 | static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) { |
230 | const char *expo = "Ee"; | 230 | const char *expo = "Ee"; |
231 | int first = ls->current; | 231 | int first = ls->current; |
232 | lua_assert(lisdigit(ls->current)); | 232 | lua_assert(lisdigit(ls->current)); |
@@ -234,16 +234,30 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) { | |||
234 | if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ | 234 | if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ |
235 | expo = "Pp"; | 235 | expo = "Pp"; |
236 | for (;;) { | 236 | for (;;) { |
237 | if (check_next(ls, expo)) /* exponent part? */ | 237 | if (check_next(ls, expo)) { /* exponent part? */ |
238 | check_next(ls, "+-"); /* optional exponent sign */ | 238 | check_next(ls, "+-"); /* optional exponent sign */ |
239 | if (lisxdigit(ls->current) || ls->current == '.') | 239 | isf = 1; |
240 | } | ||
241 | if (lisxdigit(ls->current)) | ||
242 | save_and_next(ls); | ||
243 | else if (ls->current == '.') { | ||
240 | save_and_next(ls); | 244 | save_and_next(ls); |
241 | else break; | 245 | isf = 1; |
246 | } | ||
247 | else break; | ||
242 | } | 248 | } |
243 | save(ls, '\0'); | 249 | save(ls, '\0'); |
244 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | 250 | if (!isf) { |
245 | if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ | 251 | if (!luaO_str2int(luaZ_buffer(ls->buff), &seminfo->i)) |
246 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ | 252 | lexerror(ls, "malformed number", TK_INT); |
253 | return TK_INT; | ||
254 | } | ||
255 | else { | ||
256 | buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ | ||
257 | if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ | ||
258 | trydecpoint(ls, seminfo); /* try to update decimal point separator */ | ||
259 | return TK_FLT; | ||
260 | } | ||
247 | } | 261 | } |
248 | 262 | ||
249 | 263 | ||
@@ -472,12 +486,11 @@ static int llex (LexState *ls, SemInfo *seminfo) { | |||
472 | else return TK_CONCAT; /* '..' */ | 486 | else return TK_CONCAT; /* '..' */ |
473 | } | 487 | } |
474 | else if (!lisdigit(ls->current)) return '.'; | 488 | else if (!lisdigit(ls->current)) return '.'; |
475 | /* else go through */ | 489 | else return read_numeral(ls, seminfo, 1); |
476 | } | 490 | } |
477 | case '0': case '1': case '2': case '3': case '4': | 491 | case '0': case '1': case '2': case '3': case '4': |
478 | case '5': case '6': case '7': case '8': case '9': { | 492 | case '5': case '6': case '7': case '8': case '9': { |
479 | read_numeral(ls, seminfo); | 493 | return read_numeral(ls, seminfo, 0); |
480 | return TK_NUMBER; | ||
481 | } | 494 | } |
482 | case EOZ: { | 495 | case EOZ: { |
483 | return TK_EOS; | 496 | return TK_EOS; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.h,v 1.71 2011/06/20 16:52:48 roberto Exp roberto $ | 2 | ** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp roberto $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -27,7 +27,7 @@ enum RESERVED { | |||
27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, | 27 | TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, |
28 | /* other terminal symbols */ | 28 | /* other terminal symbols */ |
29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, | 29 | TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, |
30 | TK_NUMBER, TK_NAME, TK_STRING | 30 | TK_FLT, TK_INT, TK_NAME, TK_STRING |
31 | }; | 31 | }; |
32 | 32 | ||
33 | /* number of reserved words */ | 33 | /* number of reserved words */ |
@@ -36,6 +36,7 @@ enum RESERVED { | |||
36 | 36 | ||
37 | typedef union { | 37 | typedef union { |
38 | lua_Number r; | 38 | lua_Number r; |
39 | lua_Integer i; | ||
39 | TString *ts; | 40 | TString *ts; |
40 | } SemInfo; /* semantics information */ | 41 | } SemInfo; /* semantics information */ |
41 | 42 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.57 2013/01/29 16:00:40 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.58 2013/02/20 14:08:56 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -169,6 +169,25 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) { | |||
169 | } | 169 | } |
170 | 170 | ||
171 | 171 | ||
172 | int luaO_str2int (const char *s, lua_Integer *result) { | ||
173 | lua_Unsigned a = 0; | ||
174 | if (s[0] == '0' && | ||
175 | (s[1] == 'x' || s[1] == 'X')) { /* hexa? */ | ||
176 | s += 2; /* skip '0x' */ | ||
177 | for (; lisxdigit(cast_uchar(*s)); s++) | ||
178 | a = a * 16 + luaO_hexavalue(cast_uchar(*s)); | ||
179 | } | ||
180 | else { /* decimal */ | ||
181 | for (; lisdigit(cast_uchar(*s)); s++) | ||
182 | a = a * 10 + luaO_hexavalue(cast_uchar(*s)); | ||
183 | } | ||
184 | if (*s != '\0') return 0; /* something wrong in the numeral */ | ||
185 | else { | ||
186 | *result = cast(lua_Integer, a); | ||
187 | return 1; | ||
188 | } | ||
189 | } | ||
190 | |||
172 | 191 | ||
173 | static void pushstr (lua_State *L, const char *str, size_t l) { | 192 | static void pushstr (lua_State *L, const char *str, size_t l) { |
174 | setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); | 193 | setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 2.70 2012/05/11 14:10:50 roberto Exp $ | 2 | ** $Id: lobject.h,v 2.73 2013/04/15 15:44:46 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -498,6 +498,7 @@ LUAI_FUNC int luaO_fb2int (int x); | |||
498 | LUAI_FUNC int luaO_ceillog2 (unsigned int x); | 498 | LUAI_FUNC int luaO_ceillog2 (unsigned int x); |
499 | LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); | 499 | LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); |
500 | LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); | 500 | LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); |
501 | LUAI_FUNC int luaO_str2int (const char *s, lua_Integer *result); | ||
501 | LUAI_FUNC int luaO_hexavalue (int c); | 502 | LUAI_FUNC int luaO_hexavalue (int c); |
502 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, | 503 | LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, |
503 | va_list argp); | 504 | va_list argp); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 2.129 2012/08/06 13:36:34 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 2.130 2013/02/06 13:37:39 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -935,14 +935,19 @@ static void suffixedexp (LexState *ls, expdesc *v) { | |||
935 | 935 | ||
936 | 936 | ||
937 | static void simpleexp (LexState *ls, expdesc *v) { | 937 | static void simpleexp (LexState *ls, expdesc *v) { |
938 | /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | | 938 | /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | |
939 | constructor | FUNCTION body | suffixedexp */ | 939 | constructor | FUNCTION body | suffixedexp */ |
940 | switch (ls->t.token) { | 940 | switch (ls->t.token) { |
941 | case TK_NUMBER: { | 941 | case TK_FLT: { |
942 | init_exp(v, VKNUM, 0); | 942 | init_exp(v, VKFLT, 0); |
943 | v->u.nval = ls->t.seminfo.r; | 943 | v->u.nval = ls->t.seminfo.r; |
944 | break; | 944 | break; |
945 | } | 945 | } |
946 | case TK_INT: { | ||
947 | init_exp(v, VKINT, 0); | ||
948 | v->u.ival = ls->t.seminfo.i; | ||
949 | break; | ||
950 | } | ||
946 | case TK_STRING: { | 951 | case TK_STRING: { |
947 | codestring(ls, v, ls->t.seminfo.ts); | 952 | codestring(ls, v, ls->t.seminfo.ts); |
948 | break; | 953 | break; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.h,v 1.69 2011/07/27 18:09:01 roberto Exp roberto $ | 2 | ** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -22,7 +22,8 @@ typedef enum { | |||
22 | VTRUE, | 22 | VTRUE, |
23 | VFALSE, | 23 | VFALSE, |
24 | VK, /* info = index of constant in `k' */ | 24 | VK, /* info = index of constant in `k' */ |
25 | VKNUM, /* nval = numerical value */ | 25 | VKFLT, /* nval = numerical float value */ |
26 | VKINT, /* nval = numerical integer value */ | ||
26 | VNONRELOC, /* info = result register */ | 27 | VNONRELOC, /* info = result register */ |
27 | VLOCAL, /* info = local register */ | 28 | VLOCAL, /* info = local register */ |
28 | VUPVAL, /* info = index of upvalue in 'upvalues' */ | 29 | VUPVAL, /* info = index of upvalue in 'upvalues' */ |
@@ -46,7 +47,8 @@ typedef struct expdesc { | |||
46 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ | 47 | lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ |
47 | } ind; | 48 | } ind; |
48 | int info; /* for generic use */ | 49 | int info; /* for generic use */ |
49 | lua_Number nval; /* for VKNUM */ | 50 | lua_Number nval; /* for VKFLT */ |
51 | lua_Integer ival; /* for VKINT */ | ||
50 | } u; | 52 | } u; |
51 | int t; /* patch list of `exit when true' */ | 53 | int t; /* patch list of `exit when true' */ |
52 | int f; /* patch list of `exit when false' */ | 54 | int f; /* patch list of `exit when false' */ |