aboutsummaryrefslogtreecommitdiff
path: root/5.20/token.c
diff options
context:
space:
mode:
Diffstat (limited to '5.20/token.c')
-rw-r--r--5.20/token.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/5.20/token.c b/5.20/token.c
new file mode 100644
index 0000000..0a69b09
--- /dev/null
+++ b/5.20/token.c
@@ -0,0 +1,117 @@
+#include <string.h>
+#include <ctype.h>
+#include "getch.h"
+#include "token.h"
+#include "err.h"
+
+#define ARR_SIZE(X) sizeof(X)/sizeof(X[0])
+
+char tokentype;
+char token[MAXTOKEN];
+char prevtoken;
+
+char *typqual[] = {
+ "const",
+ "volatile"
+};
+
+char *storspec[] = {
+ "auto",
+ "extern",
+ "register",
+ "static"
+};
+
+char *datatyp[] = {
+ "char",
+ "int",
+ "long",
+ "short",
+ "signed",
+ "unsigned",
+ "void"
+};
+
+
+int gettoken(void)
+{
+ char c, *p = token;
+ char getch(void);
+ void ungetch(char);
+
+ if (prevtoken == YES) {
+ prevtoken = NO;
+ return tokentype;
+ }
+
+ while(isblank(c = getch()))
+ ;
+
+ if (c == '/') { /* ignore comments */
+ if ((c = getch()) == '/') {
+ while ((c = getch()) != '\n')
+ ;
+ } else if (c == '*') {
+ while (getch() != '*' || (c = getch()) != '/')
+ if (c == '*')
+ ungetch('*');
+ return gettoken();
+ } else {
+ ungetch(c);
+ c = '/';
+ }
+ }
+
+ if (c == '(') {
+ if ((c = getch()) == ')') {
+ strcpy(token, "()");
+ return tokentype = PARENS;
+ } else {
+ ungetch(c);
+ return tokentype = '(';
+ }
+ } else if (c == '[') {
+ for (*p++ = '['; (*p++ = getch()) != ']';)
+ ;
+ *p = '\0';
+ return tokentype = BRACKETS;
+ } else if (isalpha(c)) {
+ for (*p++ = c; isalnum(c = getch());)
+ *p++ = c;
+ *p = '\0';
+ ungetch(c);
+ return tokentype = NAME;
+ } else {
+ return tokentype = c;
+ }
+}
+
+int peaktoken()
+{
+ char type;
+
+ if (prevtoken == YES)
+ err("prevtoken overflow");
+ type = gettoken();
+ prevtoken = YES;
+ return type;
+}
+
+int isdatatyp(void)
+{
+ int i, len;
+
+ for (i = 0, len = ARR_SIZE(typqual); i < len; ++i)
+ if(!strcmp(token, typqual[i]))
+ return 1;
+
+ for (i = 0, len = ARR_SIZE(storspec); i < len; ++i)
+ if(!strcmp(token, storspec[i]))
+ return 1;
+
+ for (i = 0, len = ARR_SIZE(datatyp); i < len; ++i)
+ if(!strcmp(token, datatyp[i]))
+ return 1;
+
+ return 0;
+}