Skip to content

Commit 89913ac

Browse files
committed
Implement new PICO-8 0.2.0d operators <<> <<>= >>< and >><=.
1 parent df57f39 commit 89913ac

15 files changed

+57
-18
lines changed

lcode.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
815815
case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
816816
case OPR_MOD: case OPR_POW: case OPR_IDIV: case OPR_BAND:
817817
case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR:
818-
case OPR_LSHR: {
818+
case OPR_LSHR: case OPR_ROTL: case OPR_ROTR: {
819819
if (!isnumeral(v)) luaK_exp2RK(fs, v);
820820
break;
821821
}
@@ -861,7 +861,7 @@ void luaK_posfix (FuncState *fs, BinOpr op,
861861
case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
862862
case OPR_MOD: case OPR_POW: case OPR_IDIV: case OPR_BAND:
863863
case OPR_BOR: case OPR_BXOR: case OPR_SHL: case OPR_SHR:
864-
case OPR_LSHR: {
864+
case OPR_LSHR: case OPR_ROTL: case OPR_ROTR: {
865865
codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
866866
break;
867867
}

lcode.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
** grep "ORDER OPR" if you change these enums (ORDER OP)
2525
*/
2626
typedef enum BinOpr {
27-
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, OPR_IDIV,
28-
OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR, OPR_LSHR,
27+
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,
28+
OPR_IDIV, OPR_BAND, OPR_BOR, OPR_BXOR, OPR_SHL, OPR_SHR,
29+
OPR_LSHR, OPR_ROTL, OPR_ROTR,
2930
OPR_CONCAT,
3031
OPR_EQ, OPR_LT, OPR_LE,
3132
OPR_NE, OPR_GT, OPR_GE,

ldebug.c

+2
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,8 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) {
492492
case OP_SHL: tm = TM_SHL; break;
493493
case OP_SHR: tm = TM_SHR; break;
494494
case OP_LSHR: tm = TM_LSHR; break;
495+
case OP_ROTL: tm = TM_ROTL; break;
496+
case OP_ROTR: tm = TM_ROTR; break;
495497
case OP_UNM: tm = TM_UNM; break;
496498
case OP_BNOT: tm = TM_BNOT; break;
497499
case OP_LEN: tm = TM_LEN; break;

llex.c

+14-6
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ static const char *const luaX_tokens [] = {
4040
"return", "then", "true", "until", "while",
4141
"..", "...", "==", ">=", "<=", "~=", "!=", "::",
4242
"<eof>", "<number>", "<name>", "<string>", "?",
43-
"<eol>", "@@", "^^", "<<", ">>", ">>>",
44-
"+=", "-=", "*=", "/=", "%=", "^=", "\\=",
45-
"&=", "|=", "^^=", "<<=", ">>=", ">>>=", "..=",
43+
"<eol>", "@@", "^^", "<<", ">>", ">>>", "<<>", ">><",
44+
"+=", "-=", "*=", "/=", "%=", "^=", "\\=", "&=", "|=",
45+
"^^=", "<<=", ">>=", ">>>=", "<<>=", ">><=", "..=",
4646
};
4747

4848

@@ -495,13 +495,16 @@ static int llex (LexState *ls, SemInfo *seminfo) {
495495
if (ls->current != '=') return '=';
496496
else { next(ls); return TK_EQ; }
497497
}
498-
case '<': { /* '<=' or '<' or '<<=' or '<<' */
498+
case '<': { /* '<=' or '<' or '<<=' or '<<' or '<<>=' or '<<>' */
499499
next(ls);
500500
if (ls->current == '=') { next(ls); return TK_LE; }
501501
else if (ls->current != '<') return '<';
502502
next(ls);
503503
if (ls->current == '=') { next(ls); return TK_SHLE; }
504-
else return TK_SHL;
504+
else if (ls->current != '>') return TK_SHL;
505+
next(ls);
506+
if (ls->current == '=') { next(ls); return TK_ROTLE; }
507+
else return TK_ROTL;
505508
}
506509
case '^': { /* '^=' or '^' or '^^' or '^^=' */
507510
next(ls);
@@ -511,12 +514,17 @@ static int llex (LexState *ls, SemInfo *seminfo) {
511514
if (ls->current == '=') { next(ls); return TK_BXORE; }
512515
else return TK_BXOR;
513516
}
514-
case '>': { /* '>=' or '>' or '>>=' or '>>' or '>>>=' or '>>>' */
517+
case '>': { /* '>=', '>', '>>=', '>><=', >><', '>>', '>>>=', or '>>>' */
515518
next(ls);
516519
if (ls->current == '=') { next(ls); return TK_GE; }
517520
else if (ls->current != '>') return '>';
518521
next(ls);
519522
if (ls->current == '=') { next(ls); return TK_SHRE; }
523+
else if (ls->current == '<') {
524+
next(ls);
525+
if (ls->current == '=') { next(ls); return TK_ROTRE; }
526+
else return TK_ROTR;
527+
}
520528
else if (ls->current != '>') return TK_SHR;
521529
next(ls);
522530
if (ls->current == '=') { next(ls); return TK_LSHRE; }

llex.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ enum RESERVED {
2828
/* other terminal symbols */
2929
TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NE2, TK_DBCOLON,
3030
TK_EOS, TK_NUMBER, TK_NAME, TK_STRING, TK_PRINT, TK_EOL, TK_DBAT,
31-
TK_BXOR, TK_SHL, TK_SHR, TK_LSHR,
31+
TK_BXOR, TK_SHL, TK_SHR, TK_LSHR, TK_ROTL, TK_ROTR,
3232
/* these must be last and match "ORDER OPR" */
3333
TK_ADDE, TK_SUBE, TK_MULE, TK_DIVE, TK_MODE, TK_POWE, TK_IDIVE,
34-
TK_BANDE, TK_BORE, TK_BXORE, TK_SHLE, TK_SHRE, TK_LSHRE, TK_CONCATE,
34+
TK_BANDE, TK_BORE, TK_BXORE, TK_SHLE, TK_SHRE, TK_LSHRE, TK_ROTLE,
35+
TK_ROTRE, TK_CONCATE,
3536
};
3637

3738
/* number of reserved words */

lobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
8585
case LUA_OPSHL: return luai_numshl(NULL, v1, v2);
8686
case LUA_OPSHR: return luai_numshr(NULL, v1, v2);
8787
case LUA_OPLSHR: return luai_numlshr(NULL, v1, v2);
88+
case LUA_OPROTL: return luai_numrotl(NULL, v1, v2);
89+
case LUA_OPROTR: return luai_numrotr(NULL, v1, v2);
8890
case LUA_OPUNM: return luai_numunm(NULL, v1);
8991
case LUA_OPBNOT: return luai_numbnot(NULL, v1);
9092
default: lua_assert(0); return 0;

lopcodes.c

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
4141
"SHL",
4242
"SHR",
4343
"LSHR",
44+
"ROTL",
45+
"ROTR",
4446
"UNM",
4547
"BNOT",
4648
"NOT",
@@ -100,6 +102,8 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = {
100102
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHL */
101103
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_SHR */
102104
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_LSHR */
105+
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ROTL */
106+
,opmode(0, 1, OpArgK, OpArgK, iABC) /* OP_ROTR */
103107
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_UNM */
104108
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_BNOT */
105109
,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_NOT */

lopcodes.h

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ OP_BXOR,/* A B C R(A) := RK(B) ^^ RK(C) */
197197
OP_SHL,/* A B C R(A) := RK(B) << RK(C) */
198198
OP_SHR,/* A B C R(A) := RK(B) >> RK(C) */
199199
OP_LSHR,/* A B C R(A) := RK(B) >>> RK(C) */
200+
OP_ROTL,/* A B C R(A) := RK(B) <<> RK(C) */
201+
OP_ROTR,/* A B C R(A) := RK(B) >>< RK(C) */
200202

201203
OP_UNM,/* A B R(A) := -R(B) */
202204
OP_BNOT,/* A B R(A) := ~R(B) */

lparser.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,8 @@ static BinOpr getbinopr (int op) {
10141014
case TK_SHL: return OPR_SHL;
10151015
case TK_SHR: return OPR_SHR;
10161016
case TK_LSHR: return OPR_LSHR;
1017+
case TK_ROTL: return OPR_ROTL;
1018+
case TK_ROTR: return OPR_ROTR;
10171019
case TK_CONCAT: return OPR_CONCAT;
10181020
case TK_NE: case TK_NE2: return OPR_NE;
10191021
case TK_EQ: return OPR_EQ;
@@ -1033,7 +1035,8 @@ static const struct {
10331035
lu_byte right; /* right priority */
10341036
} priority[] = { /* ORDER OPR */
10351037
{7, 7}, {7, 7}, {8, 8}, {8, 8}, {8, 8}, {8, 8}, /* `+' `-' `*' `/' `%' `\' */
1036-
{6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, {6, 6}, /* `&' `|' `^^' `<<' `>>' `>>>' */
1038+
{6, 6}, {6, 6}, {6, 6}, {6, 6}, /* `&' `|' `^^' `<<' */
1039+
{6, 6}, {6, 6}, {6, 6}, {6, 6}, /*`>>' `>>>' `<<>' `>><' */
10371040
{10, 9}, {5, 4}, /* ^, .. (right associative) */
10381041
{3, 3}, {3, 3}, {3, 3}, /* ==, <, <= */
10391042
{3, 3}, {3, 3}, {3, 3}, /* ~=, >, >= */

ltm.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ void luaT_init (lua_State *L) {
3535
"__gc", "__mode", "__len", "__eq",
3636
"__add", "__sub", "__mul", "__div", "__mod",
3737
"__pow", "_idiv", "__and", "__or", "__xor",
38-
"__shl", "__shr", "__lshr", "__unm", "__not",
39-
"__peek", "__peek2", "__peek4",
38+
"__shl", "__shr", "__lshr", "__rotl", "rotr",
39+
"__unm", "__not", "__peek", "__peek2", "__peek4",
4040
"__lt", "__le", "__concat", "__call"
4141
};
4242
int i;

ltm.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef enum {
3535
TM_SHL,
3636
TM_SHR,
3737
TM_LSHR,
38+
TM_ROTL,
39+
TM_ROTR,
3840
TM_UNM,
3941
TM_BNOT,
4042
TM_PEEK,

lua.c

+3
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ int main (int argc, char **argv) {
484484
l_message(argv[0], "cannot create state: not enough memory");
485485
return EXIT_FAILURE;
486486
}
487+
unsigned char ram[65536];
488+
for (int i = 0; i < 65536; ++i) ram[i] = i %256;
489+
lua_setpico8memory(L, ram);
487490
/* call 'pmain' in protected mode */
488491
lua_pushcfunction(L, &pmain);
489492
lua_pushinteger(L, argc); /* 1st argument */

lua.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx);
194194
#define LUA_OPSHL 10
195195
#define LUA_OPSHR 11
196196
#define LUA_OPLSHR 12
197-
#define LUA_OPUNM 13
198-
#define LUA_OPBNOT 14
197+
#define LUA_OPROTL 13
198+
#define LUA_OPROTR 14
199+
#define LUA_OPUNM 15
200+
#define LUA_OPBNOT 16
199201

200202
LUA_API void (lua_arith) (lua_State *L, int op);
201203

luaconf.h

+2
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@
582582
#define luai_numshl(L,a,b) ((a)<<int((b)))
583583
#define luai_numshr(L,a,b) ((a)>>int((b)))
584584
#define luai_numlshr(L,a,b) (l_mathop(lshr)((a),(b)))
585+
#define luai_numrotl(L,a,b) (l_mathop(rotl)((a),(b)))
586+
#define luai_numrotr(L,a,b) (l_mathop(rotr)((a),(b)))
585587
#define luai_numbnot(L,a) (~(a))
586588
#define luai_numpeek(L,a) (lua_peek(L,a,1))
587589
#define luai_numpeek2(L,a) (lua_peek(L,a,2))

lvm.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ void luaV_finishOp (lua_State *L) {
451451
case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
452452
case OP_MOD: case OP_POW: case OP_IDIV: case OP_BAND:
453453
case OP_BOR: case OP_BXOR: case OP_SHL: case OP_SHR:
454-
case OP_LSHR: case OP_UNM: case OP_BNOT: case OP_LEN:
454+
case OP_LSHR: case OP_ROTL: case OP_ROTR:
455+
case OP_UNM: case OP_BNOT: case OP_LEN:
455456
case OP_GETTABUP: case OP_GETTABLE: case OP_SELF: {
456457
setobjs2s(L, base + GETARG_A(inst), --L->top);
457458
break;
@@ -684,6 +685,12 @@ void luaV_execute (lua_State *L) {
684685
vmcase(OP_LSHR,
685686
arith_op(luai_numlshr, TM_LSHR);
686687
)
688+
vmcase(OP_ROTL,
689+
arith_op(luai_numrotl, TM_ROTL);
690+
)
691+
vmcase(OP_ROTR,
692+
arith_op(luai_numrotr, TM_ROTR);
693+
)
687694
vmcase(OP_UNM,
688695
unary_op(luai_numunm, TM_UNM);
689696
)

0 commit comments

Comments
 (0)