You are on page 1of 2

token.

c 6/16/17, 9:48 PM

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "token.h"

Token token;

int istoken(int sym) { return token.sym == sym; }

void clear_lexeme() { token.index = 0; }


void delete_prev() { --token.index; }
void outch(int ch) { token.text[token.index++] = ch; }
int prevch() { return (token.index>0) ? (token.text[token.index-1]) : '\n';
}

static char tmp[4];

static char *tokenname[] = {


"id", "lit", "op", "cmt",
"if", "else", "switch", "case", "default",
"while", "break", "continue", "return",
"do", "for",
0
};

char *nameof(int sym) {


if (sym < ID)
{ tmp[0] = sym; tmp[1] = 0; return tmp; }
else if (sym < tEND)
return tokenname[sym-ID];
else
return "";
}

#define SQ ('\'')
#define DQ ('\"')

static int scancode[] = {


/* 0x20 - 0x2f */
' ', '!', DQ, -1, -1, '%', '&', SQ, '(', ')', '*', '+', -1, '-', '.', '/
',
/* 0x30 - 0x3f */
'0', '9', '9', '9', '9', '9', '9', '9', '9', '9', -1, -1, '<', '=', '>
', -1,
/* 0x40 - 0x4f */
'@', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z
', 'Z',
/* 0x50 - 0x5f */
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', '{', -1, '}', -1,
'Z',
/* 0x60 - 0x6f */
-1 , 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z
', 'Z',
/* 0x70 - 0x7f */
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', '[', '|', ']', -1
, -1
};
#undef SQ
#undef DQ

Page 1 of 2
token.c 6/16/17, 9:48 PM

int codeof(int ch) {


if (ch < 0x20 || ch > 0x7f) return ch;
return scancode[ch - 0x20];
}

static kwentry kwtable[] = {


"if", tIF,
"else", tELSE,
"switch", tSWITCH,
"case", tCASE,
"default", tDEFAULT,
"while", tWHILE,
"break", tBREAK,
"continue", tCONTINUE,
"return", tRETURN,
"do", tDO,
"for", tFOR,
0, 0
};

kwentry *kwlookup(const char *t) {


kwentry *e;
for (e=kwtable;e->text;e++)
if (strcmp(e->text,t)==0) return e;
return 0;
}

Page 2 of 2

You might also like