Browse Source

advance() instead of manually doing ++text --size

master
Алексей Олохтонов 2 years ago
parent
commit
f8fc74f4d8
  1. 7
      input.c
  2. 281
      main.c

7
input.c

@ -1,7 +1,12 @@ @@ -1,7 +1,12 @@
ident_
i1dent
i\uffffent
i\UFFfFaaAadent
i\UFFfFaaAadent // comment
/*
multi
line
comment */
123
0707

281
main.c

@ -37,6 +37,22 @@ struct token { @@ -37,6 +37,22 @@ struct token {
char *end; // one past end
};
static unsigned long long
usec_now(void)
{
struct timespec tp = { 0 };
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
unsigned long long now = tp.tv_sec * 1000000ULL + tp.tv_nsec / 1000ULL;
return(now);
}
static inline void
advance(struct str *s, int by)
{
s->text += by;
s->size -= by;
}
static int
nondigit(struct str s)
{
@ -159,23 +175,19 @@ integer_suffix(struct str s) @@ -159,23 +175,19 @@ integer_suffix(struct str s)
int sym = 0;
if ((sym = unsigned_suffix(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
int ll = long_long_suffix(s);
if (ll) {
return(sym + ll);
}
return(sym + long_suffix(s));
} else if ((sym = long_long_suffix(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
return(sym + unsigned_suffix(s));
} else if ((sym = long_suffix(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
return(sym + unsigned_suffix(s));
}
@ -206,20 +218,16 @@ octal_escape_sequence(struct str s) @@ -206,20 +218,16 @@ octal_escape_sequence(struct str s)
int start = s.size;
if (s.size && s.text[0] == '\\') {
s.text++;
s.size--;
advance(&s, 1);
if (octal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
if (octal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
if (octal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
}
}
@ -237,17 +245,14 @@ hexadecimal_escape_sequence(struct str s) @@ -237,17 +245,14 @@ hexadecimal_escape_sequence(struct str s)
if (s.size >= 2) {
if (s.text[0] == '\\' && s.text[1] == 'x') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -266,14 +271,11 @@ hex_quad(struct str s) @@ -266,14 +271,11 @@ hex_quad(struct str s)
{
if (s.size >= 4) {
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
if (hexadecimal_digit(s)) {
return(4);
}
@ -293,19 +295,16 @@ universal_character_name(struct str s) @@ -293,19 +295,16 @@ universal_character_name(struct str s)
if (s.text[0] == '\\') {
if (s.text[1] == 'u') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
int hq = hex_quad(s);
if (hq) {
return(2 + hq);
}
} else if (s.text[1] == 'U') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
int hq1 = hex_quad(s);
if (hq1) {
s.text += hq1;
s.size -= hq1;
advance(&s, hq1);
int hq2 = hex_quad(s);
if (hq2) {
return(2 + hq1 + hq2);
@ -336,15 +335,13 @@ identifier(struct str s) @@ -336,15 +335,13 @@ identifier(struct str s)
int in = identifier_nondigit(s);
if (in) {
s.text += in;
s.size -= in;
advance(&s, in);
int sym = 0;
for (;;) {
if ((sym = identifier_nondigit(s)) || (sym = digit(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
} else {
break;
}
@ -362,13 +359,11 @@ decimal_constant(struct str s) @@ -362,13 +359,11 @@ decimal_constant(struct str s)
int start = s.size;
if (nonzero_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -387,13 +382,11 @@ octal_constant(struct str s) @@ -387,13 +382,11 @@ octal_constant(struct str s)
if (s.size) {
if (s.text[0] == '0') {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (octal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -413,17 +406,14 @@ hexadecimal_constant(struct str s) @@ -413,17 +406,14 @@ hexadecimal_constant(struct str s)
int hp = hexadecimal_prefix(s);
if (hp) {
s.text += hp;
s.size -= hp;
advance(&s, hp);
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -442,8 +432,7 @@ integer_constant(struct str s) @@ -442,8 +432,7 @@ integer_constant(struct str s)
int sym = 0;
if ((sym = hexadecimal_constant(s)) || (sym = octal_constant(s)) || (sym = decimal_constant(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
return(sym + integer_suffix(s));
}
@ -456,13 +445,11 @@ digit_sequence(struct str s) @@ -456,13 +445,11 @@ digit_sequence(struct str s)
int start = s.size;
if (digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -484,13 +471,10 @@ fractional_constant(struct str s) @@ -484,13 +471,10 @@ fractional_constant(struct str s)
if (s.size) {
if (s.text[0] == '.') {
s.text++;
s.size--;
advance(&s, 1);
int ds2 = digit_sequence(s);
s.text += ds2;
s.size -= ds2;
advance(&s, ds2);
if (ds1 > 0 || ds2 > 0) {
return(ds1 + ds2 + 1);
@ -508,17 +492,14 @@ exponent_part(struct str s) @@ -508,17 +492,14 @@ exponent_part(struct str s)
if (s.size) {
if (s.text[0] == 'e' || s.text[0] == 'E') {
s.text++;
s.size--;
advance(&s, 1);
int sgn = sign(s);
s.text += sgn;
s.size -= sgn;
advance(&s, sgn);
int ds = digit_sequence(s);
if (ds) {
s.text += ds;
s.size -= ds;
advance(&s, ds);
return(start - s.size);
}
}
@ -532,23 +513,19 @@ decimal_floating_constant(struct str s) @@ -532,23 +513,19 @@ decimal_floating_constant(struct str s)
{
int sym = 0;
if ((sym = fractional_constant(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
int ep = exponent_part(s);
s.text += ep;
s.size -= ep;
advance(&s, ep);
return(sym + ep + floating_suffix(s));
} else if ((sym = digit_sequence(s))) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
int ep = 0;
if ((ep = exponent_part(s))) {
s.text += ep;
s.size -= ep;
advance(&s, ep);
return(sym + ep + floating_suffix(s));
}
}
@ -562,13 +539,11 @@ hexadecimal_digit_sequence(struct str s) @@ -562,13 +539,11 @@ hexadecimal_digit_sequence(struct str s)
int start = s.size;
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
for (;;) {
if (hexadecimal_digit(s)) {
s.text++;
s.size--;
advance(&s, 1);
} else {
break;
}
@ -585,12 +560,10 @@ hexadecimal_fractional_constant(struct str s) @@ -585,12 +560,10 @@ hexadecimal_fractional_constant(struct str s)
{
int hds1 = hexadecimal_digit_sequence(s);
s.text += hds1;
s.size -= hds1;
advance(&s, hds1);
if (s.size && s.text[0] == '.') {
s.text++;
s.size--;
advance(&s, 1);
int hds2 = hexadecimal_digit_sequence(s);
@ -608,16 +581,14 @@ binary_exponent_part(struct str s) @@ -608,16 +581,14 @@ binary_exponent_part(struct str s)
int start = s.size;
if (s.size && (s.text[0] == 'p' || s.text[0] == 'P')) {
s.text++;
s.size--;
advance(&s, 1);
int sgn = sign(s);
s.text += sgn;
s.size -= sgn;
advance(&s, sgn);
int ds = digit_sequence(s);
if (ds) {
s.size -= ds;
advance(&s, ds);
return(start - s.size);
}
}
@ -632,28 +603,23 @@ hexadecimal_floating_constant(struct str s) @@ -632,28 +603,23 @@ hexadecimal_floating_constant(struct str s)
int start = s.size;
if ((hp = hexadecimal_prefix(s))) {
s.text += hp;
s.size -= hp;
advance(&s, hp);
int hfc = 0;
int hds = 0;
if ((hfc = hexadecimal_fractional_constant(s))) {
s.text += hfc;
s.size -= hfc;
advance(&s, hfc);
int bep = binary_exponent_part(s);
if (bep) {
s.text += bep;
s.size -= bep;
advance(&s, bep);
return(start - s.size + floating_suffix(s));
}
} else if ((hds = hexadecimal_digit_sequence(s))) {
s.text += hds;
s.size -= hds;
advance(&s, hds);
int bep = binary_exponent_part(s);
if (bep) {
s.text += bep;
s.size -= bep;
advance(&s, bep);
return(start - s.size + floating_suffix(s));
}
}
@ -742,13 +708,11 @@ c_char_sequence(struct str s) @@ -742,13 +708,11 @@ c_char_sequence(struct str s)
int sc = 0;
if ((sc = c_char(s))) {
s.text += sc;
s.size -= sc;
advance(&s, sc);
for (;;) {
if ((sc = c_char(s))) {
s.text += sc;
s.size -= sc;
advance(&s, sc);
} else {
break;
}
@ -767,30 +731,25 @@ character_constant(struct str s) @@ -767,30 +731,25 @@ character_constant(struct str s)
int ok = 0;
if (s.size && s.text[0] == '\'') {
s.text++;
s.size--;
advance(&s, 1);
ok = 1;
} else if (s.size >= 2 && s.text[0] == 'L' && s.text[1] == '\'') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
ok = 1;
} else if (s.size >= 2 && s.text[0] == 'u' && s.text[1] == '\'') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
ok = 1;
} else if (s.size >= 2 && s.text[0] == 'U' && s.text[1] == '\'') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
ok = 1;
}
if (ok) {
int ccs = c_char_sequence(s);
if (ccs) {
s.text += ccs;
s.size -= ccs;
advance(&s, ccs);
if (s.size && s.text[0] == '\'') {
s.size--;
advance(&s, 1);
return(start - s.size);
}
}
@ -818,8 +777,7 @@ whitespace(struct str s) @@ -818,8 +777,7 @@ whitespace(struct str s)
int start = s.size;
while (s.size && (s.text[0] == ' ' || s.text[0] == '\t' || s.text[0] == '\n' || s.text[0] == '\r')) {
s.text++;
s.size--;
advance(&s, 1);
}
return(start - s.size);
@ -849,13 +807,11 @@ s_char_sequence(struct str s) @@ -849,13 +807,11 @@ s_char_sequence(struct str s)
int sc = 0;
if ((sc = s_char(s))) {
s.text += sc;
s.size -= sc;
advance(&s, sc);
for (;;) {
if ((sc = s_char(s))) {
s.text += sc;
s.size -= sc;
advance(&s, sc);
} else {
break;
}
@ -890,19 +846,16 @@ string_literal(struct str s) @@ -890,19 +846,16 @@ string_literal(struct str s)
int start = s.size;
int ep = encoding_prefix(s);
s.text += ep;
s.size -= ep;
advance(&s, ep);
if (s.size && s.text[0] == '\"') {
s.text++;
s.size--;
advance(&s, 1);
int scs = s_char_sequence(s);
s.text += scs;
s.size -= scs;
advance(&s, scs);
if (s.size && s.text[0] == '\"') {
s.size--;
advance(&s, 1);
return(start - s.size);
}
}
@ -974,16 +927,14 @@ static int @@ -974,16 +927,14 @@ static int
h_char_sequence(struct str s)
{
int start = s.size;
int sc = 0;
int hc = 0;
if ((sc = h_char(s))) {
s.text += sc;
s.size -= sc;
if ((hc = h_char(s))) {
advance(&s, hc);
for (;;) {
if ((sc = h_char(s))) {
s.text += sc;
s.size -= sc;
if ((hc = h_char(s))) {
advance(&s, hc);
} else {
break;
}
@ -999,16 +950,14 @@ static int @@ -999,16 +950,14 @@ static int
q_char_sequence(struct str s)
{
int start = s.size;
int sc = 0;
int qc = 0;
if ((sc = q_char(s))) {
s.text += sc;
s.size -= sc;
if ((qc = q_char(s))) {
advance(&s, qc);
for (;;) {
if ((sc = q_char(s))) {
s.text += sc;
s.size -= sc;
if ((qc = q_char(s))) {
advance(&s, qc);
} else {
break;
}
@ -1024,13 +973,11 @@ static int @@ -1024,13 +973,11 @@ static int
header_name(struct str s)
{
if (s.size && s.text[0] == '<') {
s.text++;
s.size--;
advance(&s, 1);
int hcs = h_char_sequence(s);
if (hcs) {
s.text += hcs;
s.size -= hcs;
advance(&s, hcs);
if (s.size && s.text[0] == '>') {
return(hcs + 2);
@ -1039,13 +986,11 @@ header_name(struct str s) @@ -1039,13 +986,11 @@ header_name(struct str s)
}
if (s.size && s.text[0] == '\"') {
s.text++;
s.size--;
advance(&s, 1);
int qcs = q_char_sequence(s);
if (qcs) {
s.text += qcs;
s.size -= qcs;
advance(&s, qcs);
if (s.size && s.text[0] == '\"') {
return(qcs + 2);
@ -1064,18 +1009,15 @@ comment(struct str s) @@ -1064,18 +1009,15 @@ comment(struct str s)
if (s.size >= 2) {
if (s.text[0] == '/' && s.text[1] == '/') {
/* single-line comment */
s.text += 2;
s.size -= 2;
advance(&s, 2);
while (s.size) {
if (s.text[0] == '\n') {
s.text++;
s.size--;
advance(&s, 1);
break;
}
s.text++;
s.size--;
advance(&s, 1);
}
return(start - s.size);
@ -1086,13 +1028,11 @@ comment(struct str s) @@ -1086,13 +1028,11 @@ comment(struct str s)
while (s.size) {
if (s.size >= 2 && s.text[0] == '*' && s.text[1] == '/') {
s.text += 2;
s.size -= 2;
advance(&s, 2);
break;
}
s.text++;
s.size--;
advance(&s, 1);
}
return(start - s.size);
@ -1109,8 +1049,7 @@ lex(char *text, int size) @@ -1109,8 +1049,7 @@ lex(char *text, int size)
while (s.size) {
int sym = whitespace(s);
s.text += sym;
s.size -= sym;
advance(&s, sym);
if ((sym = comment(s))) {
//printf("Comment: ");
@ -1136,8 +1075,7 @@ lex(char *text, int size) @@ -1136,8 +1075,7 @@ lex(char *text, int size)
//printf("%.*s\n", sym, s.text);
if (sym) {
s.text += sym;
s.size -= sym;
advance(&s, sym);
} else if (s.size) {
fprintf(stderr, "Error!\n");
break;
@ -1168,22 +1106,17 @@ preprocess(char *data, int size) @@ -1168,22 +1106,17 @@ preprocess(char *data, int size)
static void
run(char *data, int size)
{
struct timespec tp = { 0 };
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
unsigned long long before = tp.tv_sec * 1000000ULL + tp.tv_nsec / 1000ULL;
unsigned long long before = usec_now();
{
preprocess(data, size);
struct token *tokens = lex(data, size);
(void) tokens;
}
unsigned long long after = usec_now();
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
unsigned long long after = tp.tv_sec * 1000000ULL + tp.tv_nsec / 1000ULL;
unsigned long long dt = after - before;
float dt = after - before;
fprintf(stderr, "%.2fms, %.2fMB/s\n", (float) dt / 1000, (float) size / dt);
fprintf(stderr, "%.2fms, %.2fMB/s\n", dt / 1000, size / dt);
}
int

Loading…
Cancel
Save