2018-03-15 20:43:46 +00:00
|
|
|
#include <assert.h>
|
2018-03-26 18:33:32 +00:00
|
|
|
#include <ctype.h>
|
2018-03-15 20:43:46 +00:00
|
|
|
#include <stdbool.h>
|
2018-03-23 22:25:46 +00:00
|
|
|
#include <stddef.h>
|
2018-03-27 22:59:34 +00:00
|
|
|
#include <stdint.h>
|
2018-03-15 20:43:46 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-03-28 05:47:45 +00:00
|
|
|
#ifndef RELEASE
|
|
|
|
#define tprint(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
|
2018-03-27 22:59:34 +00:00
|
|
|
#else
|
2018-03-28 05:47:45 +00:00
|
|
|
#define tprint(...) do {} while (0)
|
2018-03-27 22:59:34 +00:00
|
|
|
#endif
|
2018-03-23 22:25:46 +00:00
|
|
|
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
2018-03-15 20:43:46 +00:00
|
|
|
|
|
|
|
// stretchy buffers
|
2018-03-15 22:53:22 +00:00
|
|
|
typedef struct _BufHeader {
|
|
|
|
size_t len;
|
|
|
|
size_t cap;
|
|
|
|
char buf[0]; // C99
|
|
|
|
} BufHeader;
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
void *buf__grow(const void *, size_t, size_t);
|
|
|
|
#define buf__hdr(b) ((BufHeader *)((char *)(b) - offsetof(BufHeader, buf)))
|
|
|
|
#define buf__fits(b, n) ((buf_len(b) + n) <= buf_cap(b))
|
|
|
|
#define buf__fit(b, n) (buf__fits(b, n) ? 0 : ((b) = (buf__grow((b), buf_len(b) + (n), sizeof(*(b))))))
|
2018-03-15 22:53:22 +00:00
|
|
|
|
|
|
|
#define buf_len(b) ((b) ? buf__hdr(b)->len : 0)
|
|
|
|
#define buf_cap(b) ((b) ? buf__hdr(b)->cap : 0)
|
2018-03-23 22:25:46 +00:00
|
|
|
#define buf_push(b, x) (buf__fit((b), 1), (b)[buf_len((b))] = (x), buf__hdr((b))->len++)
|
|
|
|
#define buf_free(b) ((b) ? free(buf__hdr((b))) : 0)
|
|
|
|
|
2018-03-27 22:59:34 +00:00
|
|
|
#define prlu(m, v) (tprint("%s: %lu\n", m, (unsigned long)(v)))
|
2018-03-15 22:53:22 +00:00
|
|
|
|
|
|
|
void *
|
|
|
|
buf__grow(const void *buf, size_t nlen, size_t esize)
|
2018-03-15 20:43:46 +00:00
|
|
|
{
|
2018-03-23 22:25:46 +00:00
|
|
|
size_t ncap = MAX(1 + 2*buf_cap(buf), nlen);
|
2018-03-15 22:53:22 +00:00
|
|
|
assert(nlen <= ncap);
|
2018-03-23 22:25:46 +00:00
|
|
|
size_t nsize = offsetof(BufHeader, buf) + ncap * esize;
|
|
|
|
BufHeader *nhdr;
|
2018-03-15 22:53:22 +00:00
|
|
|
|
|
|
|
if (buf) {
|
2018-03-23 22:25:46 +00:00
|
|
|
nhdr = realloc(buf__hdr(buf), nsize);
|
2018-03-15 20:43:46 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-03-23 22:25:46 +00:00
|
|
|
nhdr = malloc(nsize);
|
2018-03-15 22:53:22 +00:00
|
|
|
nhdr->len = 0;
|
2018-03-15 20:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
nhdr->cap = ncap;
|
|
|
|
return nhdr->buf;
|
2018-03-15 20:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
static void
|
|
|
|
buf_test(void)
|
2018-03-15 20:43:46 +00:00
|
|
|
{
|
2018-03-23 22:25:46 +00:00
|
|
|
int *stretchy = NULL;
|
2018-03-27 22:59:34 +00:00
|
|
|
|
|
|
|
tprint("buf_test\n");
|
2018-03-23 22:25:46 +00:00
|
|
|
for (int i = 0; i < 1024; i++) {
|
|
|
|
buf_push(stretchy, i);
|
|
|
|
}
|
2018-03-15 20:43:46 +00:00
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
for (int i = 0; i < buf_len(stretchy); i++) {
|
|
|
|
assert(stretchy[i] == i);
|
2018-03-15 20:43:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
buf_free(stretchy);
|
2018-03-27 22:59:34 +00:00
|
|
|
tprint("OK\n");
|
2018-03-23 22:25:46 +00:00
|
|
|
}
|
|
|
|
typedef enum TokenKind {
|
2018-03-26 18:33:32 +00:00
|
|
|
TOKEN_INT = 128,
|
2018-03-23 22:25:46 +00:00
|
|
|
TOKEN_NAME,
|
|
|
|
// ...
|
|
|
|
} TokenKind;
|
|
|
|
|
|
|
|
typedef struct Token {
|
|
|
|
TokenKind kind;
|
2018-03-27 22:59:34 +00:00
|
|
|
union {
|
|
|
|
uint64_t val;
|
|
|
|
struct {
|
|
|
|
const char *start;
|
|
|
|
const char *end;
|
|
|
|
};
|
|
|
|
};
|
2018-03-23 22:25:46 +00:00
|
|
|
// ...
|
|
|
|
} Token;
|
|
|
|
|
2018-03-26 18:33:32 +00:00
|
|
|
const char *stream;
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
Token token;
|
|
|
|
void next_token(void) {
|
|
|
|
switch (*stream) {
|
2018-03-27 22:59:34 +00:00
|
|
|
/* Numeric literal */
|
2018-03-23 22:25:46 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
2018-03-27 22:59:34 +00:00
|
|
|
case '9': {
|
|
|
|
uint64_t val = 0; /* reset the token's value */
|
2018-03-23 22:25:46 +00:00
|
|
|
while (isdigit(*stream)) {
|
2018-03-27 22:59:34 +00:00
|
|
|
val *= 10;
|
|
|
|
val += (*stream - '0');
|
2018-03-23 22:25:46 +00:00
|
|
|
stream++;
|
|
|
|
}
|
|
|
|
token.kind = TOKEN_INT;
|
2018-03-27 22:59:34 +00:00
|
|
|
token.val = val;
|
2018-03-23 22:25:46 +00:00
|
|
|
break;
|
2018-03-27 22:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Identifier */
|
|
|
|
case 'a':
|
|
|
|
case 'b':
|
|
|
|
case 'c':
|
|
|
|
case 'd':
|
|
|
|
case 'e':
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
|
|
|
case 'h':
|
|
|
|
case 'i':
|
|
|
|
case 'j':
|
|
|
|
case 'k':
|
|
|
|
case 'l':
|
|
|
|
case 'm':
|
|
|
|
case 'n':
|
|
|
|
case 'o':
|
|
|
|
case 'p':
|
|
|
|
case 'q':
|
|
|
|
case 'r':
|
|
|
|
case 's':
|
|
|
|
case 't':
|
|
|
|
case 'u':
|
|
|
|
case 'v':
|
|
|
|
case 'w':
|
|
|
|
case 'x':
|
|
|
|
case 'y':
|
|
|
|
case 'z':
|
|
|
|
case 'A':
|
|
|
|
case 'B':
|
|
|
|
case 'C':
|
|
|
|
case 'D':
|
|
|
|
case 'E':
|
|
|
|
case 'F':
|
|
|
|
case 'G':
|
|
|
|
case 'H':
|
|
|
|
case 'I':
|
|
|
|
case 'J':
|
|
|
|
case 'K':
|
|
|
|
case 'L':
|
|
|
|
case 'M':
|
|
|
|
case 'N':
|
|
|
|
case 'O':
|
|
|
|
case 'P':
|
|
|
|
case 'Q':
|
|
|
|
case 'R':
|
|
|
|
case 'S':
|
|
|
|
case 'T':
|
|
|
|
case 'U':
|
|
|
|
case 'V':
|
|
|
|
case 'W':
|
|
|
|
case 'X':
|
|
|
|
case 'Y':
|
|
|
|
case 'Z':
|
|
|
|
case '_': {
|
|
|
|
const char *start = stream++;
|
|
|
|
while (isalnum(*stream) || *stream == '_') {
|
|
|
|
stream++;
|
|
|
|
}
|
|
|
|
|
|
|
|
token.kind = TOKEN_NAME;
|
|
|
|
token.start = start;
|
|
|
|
token.end = stream;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-23 22:25:46 +00:00
|
|
|
default:
|
2018-03-26 18:33:32 +00:00
|
|
|
token.kind = *stream++;
|
2018-03-23 22:25:46 +00:00
|
|
|
break;
|
2018-03-15 20:43:46 +00:00
|
|
|
}
|
2018-03-23 22:25:46 +00:00
|
|
|
}
|
2018-03-15 20:43:46 +00:00
|
|
|
|
2018-03-27 22:59:34 +00:00
|
|
|
void
|
|
|
|
print_token(void)
|
|
|
|
{
|
2018-03-28 05:47:45 +00:00
|
|
|
tprint("TOKEN: ");
|
|
|
|
switch (token.kind) {
|
|
|
|
case TOKEN_INT:
|
|
|
|
tprint("INT VALUE: %lu", token.val);
|
|
|
|
break;
|
|
|
|
case TOKEN_NAME:
|
|
|
|
tprint("NAME VALUE: %.*s", (int)(token.end-token.start), token.start);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tprint("'%c'", token.kind);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tprint("\n");
|
2018-03-27 22:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lex_test(void)
|
2018-03-26 18:33:32 +00:00
|
|
|
{
|
2018-03-28 05:47:45 +00:00
|
|
|
char source[] = "+()123456+IDDQD,994_id3kfa";
|
2018-03-27 22:59:34 +00:00
|
|
|
tprint("lex_test\n");
|
2018-03-26 18:33:32 +00:00
|
|
|
stream = source;
|
|
|
|
next_token();
|
|
|
|
while (token.kind) {
|
2018-03-28 05:47:45 +00:00
|
|
|
print_token();
|
2018-03-26 18:33:32 +00:00
|
|
|
next_token();
|
|
|
|
}
|
2018-03-27 22:59:34 +00:00
|
|
|
tprint("OK\n");
|
2018-03-26 18:33:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 22:25:46 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
buf_test();
|
2018-03-26 18:33:32 +00:00
|
|
|
lex_test();
|
2018-03-15 20:43:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|