This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
token.h
166 lines (144 loc) · 2.94 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
/**
* Module for manipulating with tokens
*/
#include "system.h"
#include "string.h"
#include "vector.h"
#ifndef _TOKEN_H
#define _TOKEN_H
/**
* Enum codes for all tokens, keywords are nested
*/
typedef enum {
/** Regular tokens, castable from scanner state */
/********************************/
/* DONT CHANGE THE ORDER !!! */
/********************************/
/* priority 0 */
TT_unaryMinus = 0,
TT_not,
/* priority 1 */
TT_multiply,
TT_division,
TT_and,
/* priority 2 */
TT_plus,
TT_minus,
TT_or,
TT_xor,
/* priority 3 */
TT_less,
TT_greater, // 10
TT_lessOrEqual,
TT_greaterOrEqual,
TT_equality,
TT_inequality,
TT_leftBrace,
TT_rightBrace,
TT_function, // not used in scanner
TT_comma,
TT_empty, // special token, default value
TT_identifier, // 20
TT_real,
TT_integer,
TT_string,
TT_bool, // value in "n" (1 = true, 0 = false)
// TT_bool must stay here
TT_assignment, // must stay here !!! (size of precedence_table array)
TT_colon,
TT_dot,
TT_semicolon,
TT_leftCurlyBrace,
TT_rightCurlyBrace, // 30
TT_keyword
} TokenType;
/**
* Enum of keyword tokens
*/
typedef enum {
Key_and,
Key_begin,
Key_boolean,
Key_do,
Key_else,
Key_end,
Key_false,
Key_forward,
Key_function,
Key_if,
Key_integer,
Key_not,
Key_or,
Key_readln,
Key_real,
Key_repeat,
Key_string,
Key_then,
Key_true,
Key_var,
Key_until,
Key_while,
Key_write,
Key_xor,
Key_none // Special keyword, used in identifierToToken(String)
// Must be last
} KeywordTokenType;
/**
* Type which represents one token parsed
* from Scanner with getToken(Scanner *).
*/
typedef struct {
TokenType type;
KeywordTokenType keyword_token;
union {
String str; // string
double r; // real
int32_t n; // integer
};
} Token;
/**
* Token constructor, constructs also String.
* First malloc is done with appending to string
* @return Token structure
*/
Token initToken();
/**
* Token destructor, destructs String inside.
* @param token Pointer to token
*/
void destroyToken(Token *token);
inline void appendCharToToken(Token *token, char c) {
assert(token);
// TODO : Check for current type
appendCharToString(&token->str, c);
}
/**
* Prints basic info about token
* @param token Pointer to token
*/
void tokenInfo(Token *token);
/**
* Converts token's enum type to literals for printing (live debugging).
* @param token Pointer to token
* @return Literal describing token (TT_less -> "<")
*/
char *stringifyToken(Token *token);
/**
* Vector generation for type Token
*/
GenVectorPrototypes(Token)
#endif