More functional Makefile.
[Jack-Compiler.git] / token.c
blob20ee9d7c1a5ce3584073c437cd4fb5b4e120d5ff
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)
86 if(*pC == '"') { pC++; break; } /* found corresponding quote, stop */
87 } else {
88 if(((strchr(SPACES, ch)) != NULL) || ((strchr(SYMBOLS, ch)) != NULL))
90 break;
93 pT[cont] = *pC;
94 pC++;
95 ch = *pC;
96 cont++;
98 pT[cont] = '\0';
99 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;
133 ttype keyword(char pT[])
135 int i = 0;
136 do {
137 i++;
138 } while (strcmp(pT, keywords[i]) < 0 && i < KEYWORD_COUNT);
139 return i;
142 char symbol()
144 return 'z';
147 char *identifier(char *str)
149 str = NULL;
150 return NULL;
153 int int_val(char pT[])
155 return atoi(pT);
158 char *string_val()
160 return NULL;
163 void token_print(char *s, ptoken print_spec)
165 int i = 0;
167 while(i < space_count)
169 printf(" ");
170 i++;
173 switch(print_spec)
175 case OPEN:
176 printf("<%s>\r\n", s);
177 break;
178 case CLOSE:
179 printf("</%s>\r\n", s);
180 break;
181 case BOTH:
182 /* convert '<' and '>' to HTML */
183 if(*pT == '<')
185 printf("<%s> &lt; </%s>\r\n", s, s);
186 } else if (*pT == '>')
188 printf("<%s> &gt; </%s>\r\n", s, s);
189 } else if (*pT == '&')
191 printf("<%s> &amp; </%s>\r\n", s, s);
192 } else {
193 printf("<%s> %s </%s>\r\n", s, pT, s);
195 break;