Skip to content

Commit b671a13

Browse files
committed
COMMON: Added LSHIFT and RSHIFT bit shift operators
1 parent f729aa9 commit b671a13

File tree

7 files changed

+107
-16
lines changed

7 files changed

+107
-16
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Removed BALLOC, MALLOC and VADR keywords. Removed duplicate ENVIRON
44
Removed system constants BPP and VIDADR, OSNAME, LINECHART, BARCHART
55
Added TIMESTAMP to return the modified-datetime of a file
6+
Added LSHIFT and RSHIFT bit shift operators
67
SBVER now includes build information
78
TICKS now returns millisecond intervals
89
Refactor eval() for performance
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'
2+
' Base64 encoding and decoding
3+
' see: https://tools.ietf.org/html/rfc4648
4+
'
5+
6+
Unit Base64
7+
Export encode
8+
9+
const alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
10+
const padding="="
11+
12+
func encode(message)
13+
local result=""
14+
local in_len = len(message)
15+
local offset = 1
16+
local group, group_len
17+
local b_a, b_b, b_c
18+
19+
while offset <= in_len
20+
group_len = min(3, (in_len - offset + 1))
21+
select case group_len
22+
case 1
23+
' final unit of encoded output will be two characters followed by two "=" padding characters.
24+
b_a = mid(message, offset, 1)
25+
result += mid(alphabet, (asc(b_a) rshift 2) + 1, 1)
26+
result += mid(alphabet, ((asc(b_a) band 0x3) lshift 4) + 1, 1)
27+
result += padding
28+
result += padding
29+
case 2
30+
' final unit of encoded output will be three characters followed by one "=" padding character.
31+
b_a = mid(message, offset, 1)
32+
b_b = mid(message, offset + 1, 1)
33+
result += mid(alphabet, (asc(b_a) rshift 2) + 1, 1)
34+
result += mid(alphabet, (((asc(b_a) band 0x3) lshift 4) + (asc(b_b) rshift 4)) + 1, 1)
35+
result += mid(alphabet, ((asc(b_b) band 0xf) lshift 2) + 1, 1)
36+
result += padding
37+
case else
38+
b_a = mid(message, offset, 1)
39+
b_b = mid(message, offset + 1, 1)
40+
b_c = mid(message, offset + 2, 1)
41+
result += mid(alphabet, (asc(b_a) rshift 2) + 1, 1)
42+
result += mid(alphabet, (((asc(b_a) band 0x3) lshift 4) + (asc(b_b) rshift 4)) + 1, 1)
43+
result += mid(alphabet, (((asc(b_b) band 0xf) lshift 2) + (asc(b_c) rshift 6)) + 1, 1)
44+
result += mid(alphabet, (asc(b_c) band 0x3f) + 1, 1)
45+
end select
46+
offset += group_len
47+
wend
48+
encode = result
49+
end
50+
51+
func decode(message)
52+
local result = ""
53+
decode = result
54+
end
55+
56+
sub test
57+
if (len(alphabet) != 64) then
58+
throw "Invalid alphaet length"
59+
fi
60+
if (encode("foobar") != "Zm9vYmFy") then
61+
throw "base64 encoding error 1"
62+
endif
63+
if (encode("This is a longer string") != "VGhpcyBpcyBhIGxvbmdlciBzdHJpbmc=") then
64+
throw "base64 encoding error 2"
65+
endif
66+
if (encode("This result will end with two equals.") != "VGhpcyByZXN1bHQgd2lsbCBlbmQgd2l0aCB0d28gZXF1YWxzLg==") then
67+
throw "base64 encoding error 3"
68+
endif
69+
' if (base64_dec("Zm9vYmEy") != "foobar") then
70+
' throw "base64 encoding error"
71+
' endif
72+
end
73+
74+
test
75+

samples/distro-examples/tests/eval-test.bas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ assertEq 0xcccccccc Eqv 0xaaaaaaaa, 0x99999999, PROGLINE
203203
assertEq 0xcccc Eqv 0xaaaa, 0x9999, PROGLINE
204204
assertEq 0xcc Eqv 0xaa, 0x99, PROGLINE
205205
assertEq 0xc Eqv 0xa, 0x9, PROGLINE
206+
207+
' bit shift operators
208+
assertEq 0xFF LSHIFT 1, 0x1FE, PROGLINE
209+
assertEq 0x1FE RSHIFT 1, 0xFF, PROGLINE

src/common/eval.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ static inline void oper_log(var_t *r, var_t *left, byte op) {
534534
case OPLOG_XOR:
535535
ri = li ^ ri;
536536
break;
537+
case OPLOG_LSHIFT:
538+
ri = li << ri;
539+
break;
540+
case OPLOG_RSHIFT:
541+
ri = li >> ri;
542+
break;
537543
}
538544

539545
// cleanup

src/common/kw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ extern "C" {
4242
#define OPLOG_MOD 'M' // MOD (remain)
4343
#define OPLOG_MDL 'L' // MDL (modulus)
4444
#define OPLOG_LIKE 'W' // LIKE wc
45+
#define OPLOG_LSHIFT 'X' // LSHIFT
46+
#define OPLOG_RSHIFT 'Y' // RSHIFT
47+
4548
/**
4649
* @ingroup sys
4750
* @enum keyword

src/languages/keywords.en.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,23 @@ struct keyword_s keyword_table[] = {
133133
* OPERATORS (not the symbols)
134134
*/
135135
struct opr_keyword_s opr_table[] = {
136-
{ "AND", kwTYPE_LOGOPR, '&' },
137-
{ "OR", kwTYPE_LOGOPR, '|' },
138-
{ "BAND", kwTYPE_LOGOPR, OPLOG_BAND },
139-
{ "BOR", kwTYPE_LOGOPR, OPLOG_BOR },
140-
{ "XOR", kwTYPE_LOGOPR, '~' },
141-
{ "NOT", kwTYPE_UNROPR, '!' },
142-
{ "MOD", kwTYPE_MULOPR, OPLOG_MOD },
143-
{ "MDL", kwTYPE_MULOPR, OPLOG_MDL },
144-
{ "EQV", kwTYPE_LOGOPR, OPLOG_EQV },
145-
{ "IMP", kwTYPE_LOGOPR, OPLOG_IMP },
146-
{ "NAND", kwTYPE_LOGOPR, OPLOG_NAND },
147-
{ "NOR", kwTYPE_LOGOPR, OPLOG_NOR },
148-
{ "XNOR", kwTYPE_LOGOPR, OPLOG_XNOR },
149-
{ "IN", kwTYPE_CMPOPR, OPLOG_IN },
150-
{ "LIKE", kwTYPE_CMPOPR, OPLOG_LIKE },
136+
{ "AND", kwTYPE_LOGOPR, '&' },
137+
{ "OR", kwTYPE_LOGOPR, '|' },
138+
{ "BAND", kwTYPE_LOGOPR, OPLOG_BAND },
139+
{ "BOR", kwTYPE_LOGOPR, OPLOG_BOR },
140+
{ "XOR", kwTYPE_LOGOPR, '~' },
141+
{ "NOT", kwTYPE_UNROPR, '!' },
142+
{ "MOD", kwTYPE_MULOPR, OPLOG_MOD },
143+
{ "MDL", kwTYPE_MULOPR, OPLOG_MDL },
144+
{ "EQV", kwTYPE_LOGOPR, OPLOG_EQV },
145+
{ "IMP", kwTYPE_LOGOPR, OPLOG_IMP },
146+
{ "NAND", kwTYPE_LOGOPR, OPLOG_NAND },
147+
{ "NOR", kwTYPE_LOGOPR, OPLOG_NOR },
148+
{ "XNOR", kwTYPE_LOGOPR, OPLOG_XNOR },
149+
{ "IN", kwTYPE_CMPOPR, OPLOG_IN },
150+
{ "LIKE", kwTYPE_CMPOPR, OPLOG_LIKE },
151+
{ "LSHIFT", kwTYPE_LOGOPR, OPLOG_LSHIFT },
152+
{ "RSHIFT", kwTYPE_LOGOPR, OPLOG_RSHIFT },
151153
{ "", 0, 0 }
152154
};
153155

src/platform/sdl/editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void System::editSource(String loadPath) {
190190
break;
191191
case SB_KEY_F(1):
192192
case SB_KEY_ALT('h'):
193-
_output->setStatus("Keyword Help. Esc=Close");
193+
_output->setStatus("Keyword Help. F2=online, Esc=Close");
194194
widget = helpWidget;
195195
helpWidget->createKeywordIndex();
196196
helpWidget->show();

0 commit comments

Comments
 (0)