aboutsummaryrefslogtreecommitdiff
path: root/5.19/token.c
diff options
context:
space:
mode:
Diffstat (limited to '5.19/token.c')
-rw-r--r--5.19/token.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/5.19/token.c b/5.19/token.c
new file mode 100644
index 0000000..7f4d555
--- /dev/null
+++ b/5.19/token.c
@@ -0,0 +1,72 @@
+#include <string.h>
+#include <ctype.h>
+#include "getch.h"
+#include "token.h"
+
+enum { NO, YES };
+
+char tokentype;
+char token[MAXTOKEN];
+char prevtoken;
+
+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;
+
+ type = gettoken();
+ prevtoken = YES;
+ return type;
+}