-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
79 lines (63 loc) · 1.74 KB
/
token.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef TOKEN_H_
#define TOKEN_H_
#define KEYWORD_COUNT 21
#define SYMBOLS "{}()[].,;+-*/&|<>=~"
#define SPACES "\t\n\r "
#define BINARY_OP "*+-&/|<>="
#define UNARY_OP "-~"
extern int space_count;
extern char pT[];
typedef enum {
BOOLEAN, CHAR, CLASS, CONSTRUCTOR, DO, ELSE,
FALSE, FIELD, FUNCTION, IF, INT, LET, METHOD,
NUL, RETURN, STATIC, THIS, TRUE, VAR, VOID, WHILE
} ttype;
typedef enum { KEYWORD, SYMBOL, IDENTIFIER, INT_CONST, STRING_CONST } token;
typedef enum { OPEN, CLOSE, BOTH } ptoken;
/*
* Look at source stream and determine if more tokens are available.
* Returns > 0 if more are found and 0 if none found.
*/
int has_more_tokens(char *pC);
/*
* Move source pointer to next token in source stream.
* pC is pointer to code and pT is pointer to an
* array of char that contains the token.
* Should be called only if has_more_tokens is true.
*/
char *advance(char *pC, char pT[]);
/*
* Returns type of current token.
*/
token token_type(char pT[]);
/*
* Returns keyword of current token.
* Called only when token_type is KEYWORD.
*/
ttype keyword(char pT[]);
/*
* Returns character which is the current token.
* Called only when token_type is a SYMBOL.
*/
char symbol();
/*
* Returns identifier which is the current token.
* Called only when token_type is IDENTIFIER.
*/
char *identifier(char *str);
/*
* Returns integer value of the current token.
* Called only when token_type is INT_CONST.
*/
int int_val(char pT[]);
/*
* Returns a pointer to an array of char, without double quotes.
* Called only when token_type is STRING_CONST.
*/
char *string_val();
/*
* Prints opening token to stdout.
* Adds number of spaces stored in space_count to beginning of token.
*/
void token_print(char *s, ptoken print_spec);
#endif