Revised command line options.
[Jack-Compiler.git] / token.c
blobdfaaf4aebc808c15ff7325e3cb59e827a0b24257
1 #include <ctype.h>
2 #include <stdbool.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
7 #include "token.h"
9 char *keywords[] =
11 "boolean","char", "class", "constructor", "do", "else",
12 "false", "field", "function", "if", "int", "let", "method",
13 "null", "return", "static", "this", "true", "var", "void", "while"
16 int has_more_tokens(char *pC)
18 if(pC == NULL) { return false; }
20 if(*(pC+1) == '\0')
22 return false;
23 } else {
24 return true;
28 char *advance(char *pC, char pT[])
30 char ch;
31 int cont; /* continue */
32 int in_quote = 0; /* inside quotes? */
33 do {
34 cont = 0;
35 /* skip past C++ style comments */
36 if(*pC == '/' && *(pC+1) == '/')
38 pC = strchr(pC, '\n');
41 /* skip past C style comments */
42 if(*pC == '/' && *(pC+1) == '*')
44 pC += 2;
45 do {
46 pC++;
47 } while((*pC != '/') || (*(pC-1) != '*'));
48 pC++;
51 /* advance past spaces and newline chars */
52 ch = *pC;
53 while((strchr(SPACES, ch)) != NULL)
55 pC++;
56 ch = *pC;
59 /* determine if more advancement is needed */
60 if(*pC == '/' && *(pC+1) == '/') { cont++; }
61 if(*pC == '/' && *(pC+1) == '*') { cont++; }
63 } while (cont);
65 /* test for symbol - copy to buffer */
66 ch = *pC;
67 if(strchr(SYMBOLS, ch) != NULL)
69 pT[0] = ch;
70 pT[1] = '\0';
71 pC++;
72 return pC;
75 /* should be at beginning of token - copy to buffer */
76 cont = 0; in_quote = 0;
77 if(*pC == '"')
79 in_quote++; /* inside quote */
80 pC++; /* skip quote itself */
82 while(TRUE)
84 if(in_quote){
85 if(*pC == '"') { pC++; break; } /* found corresponding quote, stop */
86 } else {
87 if(((strchr(SPACES, ch)) != NULL) || ((strchr(SYMBOLS, ch)) != NULL))
89 break;
92 pT[cont] = *pC;
93 pC++;
94 ch = *pC;
95 cont++;
97 pT[cont] = '\0';
98 return pC;
102 token token_type(char pT[])
104 token t = IDENTIFIER;
105 int i = 0, j = 0;;
107 /* check token for symbols */
108 if(strchr(SYMBOLS, pT[0]) != NULL) { t = SYMBOL; }
110 /* check token for integer constants */
111 while(pT[i] != '\0')
113 if(isdigit(pT[i]) == 0)
115 j++;
116 break;
118 i++;
120 if(j == 0) { t = INT_CONST; }
122 /* check token for keywords */
123 i = 0; j = 0;
124 do {
125 j = strcmp(keywords[i], pT);
126 i++;
127 } while ((j != 0) && (i < KEYWORD_COUNT));
129 if(j == 0) { t = KEYWORD; }
130 return t;
134 ttype keyword(char pT[])
136 int i = 0;
137 do {
138 i++;
139 } while (strcmp(pT, keywords[i]) < 0 && i < KEYWORD_COUNT);
140 return i;
144 char symbol()
146 return 'z';
150 char *identifier(char *str)
152 str = NULL;
153 return NULL;
157 int int_val(char pT[])
159 return atoi(pT);
163 char *string_val()
165 return NULL;