Skip to content

Commit

Permalink
add tokenize pass escape '\n'
Browse files Browse the repository at this point in the history
  • Loading branch information
sukesan1984 committed May 6, 2020
1 parent ed89412 commit 5b608a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
10 changes: 8 additions & 2 deletions workspace/9cc/codegen.c
Expand Up @@ -5,12 +5,18 @@ char* argreg32[] = {"edi", "esi", "edx", "ecx", "r8d", "r9d"};
char* argreg8[] = {"dil", "sil", "dl", "cl", "r8b", "r9b"};

static char *escape(char *s, int len) {
static char escaped[256] = {
['\b'] = 'b', ['\f'] = 'f', ['\n'] = 'n', ['\r'] = 'r',
['\t'] = 't', ['\\'] = '\\', ['\''] = '\'', ['"'] = '"',
};
char *buf = malloc(len * 4);
char *p = buf;
for (int i = 0; i < len; i++) {
if(s[i] == '\\') {
*p++ = '\\';
uint8_t c = s[i];
char esc = escaped[c];
if (esc) {
*p++ = '\\';
*p++ = esc;
} else if (isgraph(s[i]) || s[i] == ' ') {
*p++ = s[i];
} else {
Expand Down
44 changes: 39 additions & 5 deletions workspace/9cc/tokenize.c
Expand Up @@ -30,6 +30,31 @@ int tokenize_comparable(Vector* tokens, int ty, char *p, char* token) {
return 0;
}

static char *c_char(int *res, char *p) {
// Nonescaped
if (*p != '\\') {
*res = *p;
return p + 1;
}
p++;

static char escaped[256] = {
['a'] = '\a', ['b'] = '\b', ['f'] = '\f',
['n'] = '\n', ['r'] = '\r', ['t'] = '\t',
['v'] = '\v', ['e'] = '\033', ['E'] = '\033',
};

// Simple (e.g. `\n` or `\a`)
int esc = escaped[(uint8_t) *p];

if (esc) {
*res = esc;
return p + 1;
}
*res = *p;
return p + 1;
}

// pが指している文字列をトークンに分割してtokensに保存する
void tokenize(char *p) {
while (*p) {
Expand Down Expand Up @@ -144,16 +169,25 @@ void tokenize(char *p) {
char* init_p = p;
int len = 0;
Token *t = add_token(tokens, TK_STR, p);
while(*p != '"') {
// mallocするのにlenをとっておく
while(*p++ != '"')
len++;
p++;
}
p++;
char *str = (char*) malloc(len + 1);
strncpy(str, init_p, len);
p = init_p;
int i = 0;
len = 0;
// ここで、\, nみたいに分かれているのを統一する => \n
while(*p != '"') {
int c;
p = c_char(&c, p);
str[i++] = c;
len++;
}
str[len] = '\0';
p++;
t->str = str;
continue;
continue;
}

if (is_alnum(*p)) {
Expand Down

0 comments on commit 5b608a8

Please sign in to comment.