Skip to content

Commit 468bf37

Browse files
committed
Add support for hex literals in arithmetic expressions
1 parent 9f98840 commit 468bf37

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

parser/arithm.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,53 @@ static bool parse_str(struct mrsh_parser *parser, const char *str) {
5353
return true;
5454
}
5555

56-
static size_t peek_literal(struct mrsh_parser *parser) {
57-
size_t i = 0;
56+
static int ishexdigit(char c) {
57+
return isdigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
58+
}
59+
60+
static size_t peek_hex_literal(struct mrsh_parser *parser) {
61+
size_t i = 2; // We already know '0x' prefix is there
62+
5863
while (true) {
5964
parser_peek(parser, NULL, i + 1);
6065

6166
char c = parser->buf.data[i];
62-
// TODO: 0x, 0b prefixes
67+
if (!ishexdigit(c)) {
68+
break;
69+
}
70+
71+
++i;
72+
}
73+
74+
return i;
75+
}
76+
77+
static size_t peek_literal(struct mrsh_parser *parser) {
78+
size_t i = 0;
79+
80+
parser_peek(parser, NULL, 1);
81+
82+
char c = parser->buf.data[0];
83+
if (c == '0') {
84+
++i;
85+
parser_peek(parser, NULL, 2);
86+
87+
c = parser->buf.data[1];
88+
// TODO: 0b prefix
89+
if (c == 'x' || c == 'X') {
90+
return peek_hex_literal(parser);
91+
}
92+
}
93+
94+
while (true) {
6395
if (!isdigit(c)) {
6496
break;
6597
}
6698

6799
++i;
100+
parser_peek(parser, NULL, i + 1);
101+
102+
c = parser->buf.data[i];
68103
}
69104

70105
return i;

test/arithm.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/bin/sh -eu
22

33
echo "1 =" $((1))
4+
echo "0x0 = " $((0x0))
5+
echo "0x100 = " $((0x100))
6+
echo "0xabcd = " $((0xabcd))
47
echo "2*5 =" $((2*5))
58
echo "2/5 =" $((2/5))
69
echo "2%5 =" $((2%5))

0 commit comments

Comments
 (0)