Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

few syntax improvements #63

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/lua/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@


#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
#ifndef LUA_NUMBER_INTEGRAL
#include <math.h>
#endif

#define lapi_c
#define LUA_CORE
Expand Down Expand Up @@ -976,11 +978,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
break;
}
case LUA_GCSTEP: {
lu_mem a;
if(is_block_gc(L)) {
res = 1; /* gc is block so we need to pretend that the collection cycle finished. */
break;
}
lu_mem a = (cast(lu_mem, data) << 10);
a = (cast(lu_mem, data) << 10);
if (a <= g->totalbytes)
g->GCthreshold = g->totalbytes - a;
else
Expand Down
1 change: 0 additions & 1 deletion src/lua/lauxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


#include <stddef.h>
#include <stdio.h>

#include "lua.h"

Expand Down
10 changes: 6 additions & 4 deletions src/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static int luaB_tonumber (lua_State *L) {
else {
const char *s1 = luaL_checkstring(L, 1);
char *s2;
unsigned long n;
lua_Number n;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
n = strtoul(s1, &s2, base);
n = (lua_Number)strtoul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
if (*s2 == '\0') { /* no invalid trailing characters? */
Expand Down Expand Up @@ -485,17 +485,19 @@ const LUA_REG_TYPE base_funcs_list[] = {


static int luaB_index(lua_State *L) {
const char *keyname;
void *res;
#if LUA_OPTIMIZE_MEMORY == 2
int fres;
if ((fres = luaR_findfunction(L, base_funcs_list)) != 0)
return fres;
#endif
const char *keyname = luaL_checkstring(L, 2);
keyname = luaL_checkstring(L, 2);
if (!strcmp(keyname, "_VERSION")) {
lua_pushliteral(L, LUA_VERSION);
return 1;
}
void *res = luaR_findglobal(keyname, strlen(keyname));
res = luaR_findglobal(keyname, strlen(keyname));
if (!res)
return 0;
else {
Expand Down
35 changes: 29 additions & 6 deletions src/lua/lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,23 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
case OP_ADD: r = luai_numadd(v1, v2); break;
case OP_SUB: r = luai_numsub(v1, v2); break;
case OP_MUL: r = luai_nummul(v1, v2); break;
case OP_DIV:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_numdiv(v1, v2); break;
case OP_MOD:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_nummod(v1, v2); break;
case OP_POW: r = luai_numpow(v1, v2); break;
case OP_DIV:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_numdiv(v1, v2); break;
case OP_IDIV:
if (v2 == 0) return 0; /* do not attempt to divide by 0 */
r = luai_numdiv(v1, v2); break;
case OP_BAND: r = luai_numband(v1, v2); break;
case OP_BOR: r = luai_numbor(v1, v2); break;
case OP_BXOR: r = luai_numbxor(v1, v2); break;
case OP_SHL: r = luai_numshl(v1, v2); break;
case OP_SHR: r = luai_numshr(v1, v2); break;
case OP_UNM: r = luai_numunm(v1); break;
case OP_BNOT: r = luai_numbnot(v1); break;
case OP_LEN: return 0; /* no constant folding for 'len' */
default: lua_assert(0); r = 0; break;
}
Expand Down Expand Up @@ -696,6 +705,12 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
codearith(fs, OP_UNM, e, &e2);
break;
}
case OPR_BNOT: {
if (!isnumeral(e))
luaK_exp2anyreg(fs, e); /* cannot operate on non-numeric constants */
codearith(fs, OP_BNOT, e, &e2);
break;
}
case OPR_NOT: codenot(fs, e); break;
case OPR_LEN: {
luaK_exp2anyreg(fs, e); /* cannot operate on constants */
Expand All @@ -721,8 +736,10 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
luaK_exp2nextreg(fs, v); /* operand must be on the `stack' */
break;
}
case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
case OPR_MOD: case OPR_POW: {
case OPR_ADD: case OPR_SUB: case OPR_MUL:
case OPR_MOD: case OPR_POW: case OPR_DIV: case OPR_IDIV:
case OPR_BAND: case OPR_BOR: case OPR_BXOR:
case OPR_SHL: case OPR_SHR: {
if (!isnumeral(v)) luaK_exp2RK(fs, v);
break;
}
Expand Down Expand Up @@ -767,9 +784,15 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
case OPR_ADD: codearith(fs, OP_ADD, e1, e2); break;
case OPR_SUB: codearith(fs, OP_SUB, e1, e2); break;
case OPR_MUL: codearith(fs, OP_MUL, e1, e2); break;
case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
case OPR_MOD: codearith(fs, OP_MOD, e1, e2); break;
case OPR_POW: codearith(fs, OP_POW, e1, e2); break;
case OPR_DIV: codearith(fs, OP_DIV, e1, e2); break;
case OPR_IDIV: codearith(fs, OP_IDIV, e1, e2); break;
case OPR_BAND: codearith(fs, OP_BAND, e1, e2); break;
case OPR_BOR: codearith(fs, OP_BOR, e1, e2); break;
case OPR_BXOR: codearith(fs, OP_BXOR, e1, e2); break;
case OPR_SHL: codearith(fs, OP_SHL, e1, e2); break;
case OPR_SHR: codearith(fs, OP_SHR, e1, e2); break;
case OPR_EQ: codecomp(fs, OP_EQ, 1, e1, e2); break;
case OPR_NE: codecomp(fs, OP_EQ, 0, e1, e2); break;
case OPR_LT: codecomp(fs, OP_LT, 1, e1, e2); break;
Expand Down
8 changes: 6 additions & 2 deletions src/lua/lcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
** grep "ORDER OPR" if you change these enums
*/
typedef enum BinOpr {
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
OPR_ADD, OPR_SUB, OPR_MUL, OPR_MOD, OPR_POW,
OPR_DIV,
OPR_IDIV,
OPR_BAND, OPR_BOR, OPR_BXOR,
OPR_SHL, OPR_SHR,
OPR_CONCAT,
OPR_NE, OPR_EQ,
OPR_LT, OPR_LE, OPR_GT, OPR_GE,
Expand All @@ -33,7 +37,7 @@ typedef enum BinOpr {
} BinOpr;


typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;
typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;


#define getcode(fs,e) ((fs)->f->code[(e)->u.s.info])
Expand Down
17 changes: 11 additions & 6 deletions src/lua/ldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void MaybeByteSwap(char *number, size_t numbersize, DumpState *D)
int platform_little_endian = *(char*)&x;
if (platform_little_endian != D->target.little_endian)
{
unsigned long i;
int i;
for (i=0; i<numbersize/2; i++)
{
char temp = number[i];
Expand All @@ -78,15 +78,17 @@ static void DumpIntWithSize(int x, int sizeof_int, DumpState* D)
DumpChar(x,D);
} break;
case 2: {
int16_t y;
if (x>0x7FFF || x<(-0x8000)) D->status=LUA_ERR_CC_INTOVERFLOW;
int16_t y=(int16_t)x;
y=(int16_t)x;
MaybeByteSwap((char*)&y,2,D);
DumpVar(y,D);
} break;
case 4: {
int32_t y;
/* Need to reduce bounds by 1 to avoid messing 32-bit compilers up */
if (x>0x7FFFFFFE || x<(-0x7FFFFFFF)) D->status=LUA_ERR_CC_INTOVERFLOW;
int32_t y=(int32_t)x;
y=(int32_t)x;
MaybeByteSwap((char*)&y,4,D);
DumpVar(y,D);
} break;
Expand All @@ -108,15 +110,17 @@ static void DumpSize(uint32_t x, DumpState* D)
DumpChar(x,D);
} break;
case 2: {
uint16_t y;
if (x>0xFFFF) D->status=LUA_ERR_CC_INTOVERFLOW;
uint16_t y=(uint16_t)x;
y=(uint16_t)x;
MaybeByteSwap((char*)&y,2,D);
DumpVar(y,D);
} break;
case 4: {
uint32_t y;
/* Reduce bounds to avoid messing 32-bit compilers up */
if (x>0xFFFFFFFE) D->status=LUA_ERR_CC_INTOVERFLOW;
uint32_t y=x;
y=x;
MaybeByteSwap((char*)&y,4,D);
DumpVar(y,D);
} break;
Expand Down Expand Up @@ -167,9 +171,10 @@ static void DumpNumber(lua_Number x, DumpState* D)

static void DumpCode(const Proto *f, DumpState* D)
{
DumpInt(f->sizecode,D);
char buf[10];
int i;

DumpInt(f->sizecode,D);
Align4(D);
for (i=0; i<f->sizecode; i++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/lua/lgc.c
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,10 @@ static l_mem singlestep (lua_State *L) {

void luaC_step (lua_State *L) {
global_State *g = G(L);
l_mem lim;
if(is_block_gc(L)) return;
set_block_gc(L);
l_mem lim = (GCSTEPSIZE/100) * g->gcstepmul;
lim = (GCSTEPSIZE/100) * g->gcstepmul;
if (lim == 0)
lim = (MAX_LUMEM-1)/2; /* no limit */
g->gcdept += g->totalbytes - g->GCthreshold;
Expand Down
40 changes: 30 additions & 10 deletions src/lua/llex.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@


#include <ctype.h>
#ifndef LUA_NUMBER_INTEGRAL
#include <locale.h>
#endif
#include <string.h>

#define llex_c
Expand Down Expand Up @@ -39,9 +41,9 @@ const char *const luaX_tokens [] = {
"end", "false", "for", "function", "if",
"in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while",
"..", "...", "==", ">=", "<=", "~=",
"//", "..", "...", "==", ">=", "<=", "~=",
"<<", ">>",
"<number>", "<name>", "<string>", "<eof>",
NULL
};


Expand Down Expand Up @@ -160,7 +162,7 @@ static int check_next (LexState *ls, const char *set) {
return 1;
}


#ifndef LUA_NUMBER_INTEGRAL
static void buffreplace (LexState *ls, char from, char to) {
size_t n = luaZ_bufflen(ls->buff);
char *p = luaZ_buffer(ls->buff);
Expand All @@ -181,7 +183,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) {
luaX_lexerror(ls, "malformed number", TK_NUMBER);
}
}

#endif

/* LUA_NUMBER */
static void read_numeral (LexState *ls, SemInfo *seminfo) {
Expand All @@ -194,9 +196,16 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) {
while (isalnum(ls->current) || ls->current == '_')
save_and_next(ls);
save(ls, '\0');
#ifndef LUA_NUMBER_INTEGRAL
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
trydecpoint(ls, seminfo); /* try to update decimal point separator */
#else
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
/* format error: no more options */
luaX_lexerror(ls, "malformed number", TK_NUMBER);
}
#endif
}


Expand Down Expand Up @@ -368,18 +377,25 @@ static int llex (LexState *ls, SemInfo *seminfo) {
}
case '<': {
next(ls);
if (ls->current != '=') return '<';
else { next(ls); return TK_LE; }
if (ls->current == '=') { next(ls); return TK_LE; }
else if (ls->current == '<') { next(ls); return TK_SHL; }
else return '<';
}
case '>': {
next(ls);
if (ls->current != '=') return '>';
else { next(ls); return TK_GE; }
if (ls->current == '=') { next(ls); return TK_GE; }
else if (ls->current == '>') { next(ls); return TK_SHR; }
else return '>';
}
case '/': {
next(ls);
if (ls->current == '/') { next(ls); return TK_IDIV; }
else return '/';
}
case '~': {
next(ls);
if (ls->current != '=') return '~';
else { next(ls); return TK_NE; }
if (ls->current == '=') { next(ls); return TK_NE; }
else return '~';
}
case '"':
case '\'': {
Expand All @@ -393,11 +409,15 @@ static int llex (LexState *ls, SemInfo *seminfo) {
return TK_DOTS; /* ... */
else return TK_CONCAT; /* .. */
}
#ifdef LUA_NUMBER_INTEGRAL
return '.';
#else
else if (!isdigit(ls->current)) return '.';
else {
read_numeral(ls, seminfo);
return TK_NUMBER;
}
#endif
}
case EOZ: {
return TK_EOS;
Expand Down
5 changes: 3 additions & 2 deletions src/lua/llex.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ enum RESERVED {
TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
/* other terminal symbols */
TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
TK_NAME, TK_STRING, TK_EOS
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE,
TK_SHL, TK_SHR,
TK_NUMBER, TK_NAME, TK_STRING, TK_EOS
};

/* number of reserved words */
Expand Down
1 change: 1 addition & 0 deletions src/lua/loadlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <stdlib.h>
#include <string.h>
#include <stdio.h>


#define loadlib_c
Expand Down
2 changes: 1 addition & 1 deletion src/lua/lobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
}
case 'p': {
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
sprintf(buff, "%p", va_arg(argp, void *));
snprintf(buff, sizeof buff, "%p", va_arg(argp, void *));
pushstr(L, buff);
break;
}
Expand Down