Added declarations and comments for parse module.
[Jack-Compiler.git] / token.c
blob5f929cdd41e3e03d6c7a3fb7d2588d9b6824da26
1 #include <ctype.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
6 #include "token.h"
8 char *keywords[] =
10 "boolean","char", "class", "constructor", "do", "else",
11 "false", "field", "function", "if", "int", "let", "method",
12 "null", "return", "static", "this", "true", "var", "void", "while"
15 int has_more_tokens(char *pC)
17 if(*(pC+1) == '\0')
19 return FALSE;
20 } else {
21 return TRUE;
25 char *advance(char *pC, char pT[])
27 char ch;
28 int cont; /* continue */
29 int in_quote = 0; /* inside quotes? */
30 do {
31 cont = 0;
32 /* skip past C++ style comments */
33 if(*pC == '/' && *(pC+1) == '/')
35 pC = strchr(pC, '\n');
38 /* skip past C style comments */
39 if(*pC == '/' && *(pC+1) == '*')
41 pC++;
42 pC = strchr(pC, '/');
43 pC++;
46 /* advance past spaces and newline chars */
47 ch = *pC;
48 while((strchr(SPACES, ch)) != NULL)
50 pC++;
51 ch = *pC;
54 /* determine if more advancement is needed */
55 if(*pC == '/' && *(pC+1) == '/') { cont++; }
56 if(*pC == '/' && *(pC+1) == '*') { cont++; }
58 } while (cont);
60 /* test for symbol - copy to buffer */
61 ch = *pC;
62 if(strchr(SYMBOLS, ch) != NULL)
64 pT[0] = ch;
65 pT[1] = '\0';
66 pC++;
67 return pC;
70 /* should be at beginning of token - copy to buffer */
71 cont = 0; in_quote = 0;
72 if(*pC == '"')
74 in_quote++; /* inside quote */
75 pC++; /* skip quote itself */
77 while(TRUE)
79 if(in_quote){
80 if(*pC == '"') { pC++; break; } /* found corresponding quote, stop */
81 } else {
82 if(((strchr(SPACES, ch)) != NULL) || ((strchr(SYMBOLS, ch)) != NULL))
84 break;
87 pT[cont] = *pC;
88 pC++;
89 ch = *pC;
90 cont++;
92 pT[cont] = '\0';
93 return pC;
97 token token_type(char pT[])
99 token t = IDENTIFIER;
100 int i = 0, j = 0;;
102 /* check token for symbols */
103 if(strchr(SYMBOLS, pT[0]) != NULL) { t = SYMBOL; }
105 /* check token for integer constants */
106 while(pT[i] != '\0')
108 if(isdigit(pT[i]) == 0)
110 j++;
111 break;
113 i++;
115 if(j == 0) { t = INT_CONST; }
117 /* check token for keywords */
118 i = 0; j = 0;
119 do {
120 j = strcmp(keywords[i], pT);
121 i++;
122 } while ((j != 0) && (i < KEYWORD_COUNT));
124 if(j == 0) { t = KEYWORD; }
125 return t;
129 ttype keyword(char pT[])
131 int i = 0;
132 do {
133 i++;
134 } while (strcmp(pT, keywords[i]) < 0 && i < KEYWORD_COUNT);
135 return i;
139 char symbol()
141 return 'z';
145 char *identifier(char *str)
147 str = NULL;
148 return NULL;
152 int int_val(char pT[])
154 return atoi(pT);
158 char *string_val()
160 return NULL;