forked from 61c-teach/sp19-proj1-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.h
139 lines (116 loc) · 3.02 KB
/
tokens.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#ifndef TOKEN_H
#define TOKEN_H
/* Definition for Dinky-1C token */
enum TokenType {
TOKEN_IDENTIFIER,
TOKEN_INTEGER,
TOKEN_STRING,
TOKEN_CHARACTER,
TOKEN_KW_INT, /* Keyword "int" */
TOKEN_KW_CHAR, /* Keyword "char" */
TOKEN_KW_CONST, /* Keyword "const" */
TOKEN_KW_STRUCT, /* Keyword "struct" */
TOKEN_KW_BOOL, /* Keyword "bool" */
TOKEN_KW_IF, /* Keyword "if" */
TOKEN_KW_ELSE, /* Keyword "else" */
TOKEN_KW_TRUE, /* Keyword "true" */
TOKEN_KW_FALSE, /* Keyword "false" */
TOKEN_KW_RETURN, /* Keyword "return" */
TOKEN_KW_FOR, /* Keyword "for" */
TOKEN_KW_CONTINUE, /* Keyword "continue" */
TOKEN_KW_BREAK, /* Keyword "break" */
TOKEN_KW_NULL, /* Keyword "null" */
/* Math Tokens */
TOKEN_SYM_PLUS, /* + */
TOKEN_SYM_MINUS, /* - */
TOKEN_SYM_TIMES, /* * */
TOKEN_SYM_SLASH, /* / */
TOKEN_SYM_PLUSPLUS, /* ++ */
TOKEN_SYM_MINUSMINUS, /* -- */
/* Assignment and equality */
TOKEN_EQUAL, /* = */
TOKEN_EQUALEQUAL, /* == */
TOKEN_BANGEQUAL, /* != */
/* Comparison Tokens */
TOKEN_GREATER, /* > */
TOKEN_GREATEREQUAL, /* >= */
TOKEN_LESS, /* < */
TOKEN_LESSEQUAL, /* <= */
/* Logical Tokens */
TOKEN_SYM_ANDAND, /* && */
TOKEN_SYM_OROR, /* || */
TOKEN_SYM_NOTNOT, /* ! */
/* Bitwise Tokens */
TOKEN_SYM_AND, /* & */
TOKEN_SYM_OR, /* | */
TOKEN_SYM_XOR, /* ^ */
TOKEN_SYM_NOT, /* ~ */
/* Struct Tokens */
TOKEN_SYM_DOT, /* . */
TOKEN_SYM_ARROW, /* -> */
/* Misc symbols */
TOKEN_SYM_OPENPREN, /* ( */
TOKEN_SYM_CLOSEPREN, /* ) */
TOKEN_SYM_OPENBRACE, /* { */
TOKEN_SYM_CLOSEBRACE, /* } */
TOKEN_SYM_COMMA, /* , */
TOKEN_SYM_SEMICOLON, /* ; */
/* An error */
TOKEN_ERR
};
/* For tokens which have semantics, we use the
union to start it */
typedef union tokendata {
char* string;
char* identifier;
char character;
int integer;
/* Used for error reporting. */
char* error;
} TokenData;
typedef struct token {
enum TokenType type;
TokenData data;
/* Used for error reporting */
int linenum;
char* filename;
} Token;
typedef struct tokenlist {
Token* t;
struct tokenlist* next;
} TokenList;
/*
Mallocs space to create a new token.
*/
Token* create_token();
/*
Mallocs space to create a new list whose
token is t.
*/
TokenList* create_list(Token* t);
/*
Frees the memory allocated by a particular token.
*/
void FreeToken(Token* t);
/*
Frees the memory allocated by a Token_List.
*/
void FreeTokenList(TokenList* node);
/*
Takes two token lists and concatenates them together by placing
next at the end of the first list.
*/
void AppendTokenList(TokenList* first, TokenList* next);
/*
Produces a printed output a particular token.
*/
void PrintToken(Token* t);
/*
Produces a printed output for all tokens in the list.
*/
void PrintTokens(TokenList* tl);
/*
Used for reporting any errors. Returns 0 if there are no errors.
*/
int DisplayErrors(TokenList* tl);
#endif