summaryrefslogtreecommitdiff
path: root/jast.h
blob: 35dec2b47bc9af2197480933d26904cb2c739133 (plain) (blame)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Jast Another Symbol Table
 * Copyright (c) 2012 Guillermo Ramos GutiƩrrez <0xwille@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef __JAST_H
#define __JAST_H

#define JAST_MAX_NAMELEN	128		//!< Maximum length allowed to use in names


#define JAST_ERR	-1

enum jast_entry_t {
	JAST_UNDEF_T,
	JAST_VAR,
	JAST_CONST,
	JAST_PARAM,
	JAST_FUNCTION
};

enum jast_data_t {
	JAST_UNDEF_D,
	JAST_CHAR,
	JAST_NUM,
	JAST_FLOAT,
	JAST_STR,
	JAST_ARRAY,
	JAST_SEQ,
	JAST_POINTER
};

#define __jast_strentry(e, v)		\
	switch (e) {		\
	case JAST_VAR:		\
		v = "VAR";		\
		break;		\
	case JAST_CONST:		\
		v = "CONST";		\
		break;		\
	case JAST_PARAM:		\
		v = "PARAM";		\
		break;		\
	case JAST_FUNCTION:		\
		v = "FUNCTION";		\
		break;		\
	case JAST_UNDEF_T:		\
		v = "-UNDEF-";		\
		break;		\
	default:		\
		v = "-";		\
	}

#define __jast_strdata(e, v)		\
	switch (e) {		\
	case JAST_CHAR:		\
		v = "CHAR";		\
		break;		\
	case JAST_NUM:		\
		v = "NUM";		\
		break;		\
	case JAST_FLOAT:		\
		v = "FLOAT";		\
		break;		\
	case JAST_STR:		\
		v = "STR";		\
		break;		\
	case JAST_ARRAY:		\
		v = "ARRAY";		\
		break;		\
	case JAST_SEQ:		\
		v = "SEQ";		\
		break;		\
	case JAST_POINTER:		\
		v = "POINTER";		\
		break;		\
	case JAST_UNDEF_D:		\
		v = "-UNDEF-";		\
		break;		\
	default:		\
		v = "-";		\
	}


/*!
 * Symbol table
 *
 * Stores a list of entries (identifiers) declared in a scope of the program.
 *
 */
struct jast {
	char *name;				//!< Name of the symbol table
	struct doll *entries;	//!< List of entries inside the table
	struct jast *parent;	//!< Symtab up in the scope (NULL if global)
	struct doll *children;	//!> List of symtabs inside its scope
};

/*!
 * Entry of a symbol table
 *
 * It stores all the needed info about an identifier. All fields are optional
 * (initialized to a default value) except name, which will be used to reference
 * each entry.
 *
 * Example of entry (int i = 0):
 * 		entry->name == "i"
 * 		entry->type == VAR
 * 		entry->datatype == INT
 * 		*entry->value == 0
 * 		entry->attribs == NULL
 *
 */
struct jast_entry {
	char *name;					//!< Entry name, used to reference it
	char *uniq;					//!< Unique name
	int tmp;					//!< Temporal?
	enum jast_entry_t type;		//!< Entry type (VAR, PARAM, FUNCTION, ...)
	enum jast_data_t datatype;	//!< Data type (CHAR, INT, FLOAT, ...)
	unsigned datasize;			//!< Data size in bytes
	unsigned offset;			//!< Runtime location, if local var
	void *value;				//!< Value of the entry
	struct doll *attribs;		//!< All other data
};



/*!
 * Create a new symbol table
 *
 * \param name Symbol table name. If not NULL, a *copy* of it will be assigned
 * to the 'name' member of the table. Otherwise, a default name will be created
 * \return pointer to the new table
 * \see jast_destroy
 * \see jast_destroy_all
 * \see JAST_MAX_NAMELEN
 */
struct jast *jast_creat(char *name);

/*!
 * Free all memory used by a symbol table
 *
 * \param tab the symbol table to destroy
 * \see jast_creat
 * \see jast_destroy_all
 */
void jast_destroy(struct jast *tab);

/*!
 * Destroy all symbol tables
 *
 * \see jast_creat
 * \see jast_destroy
 */
void jast_destroy_all(void);

/*!
 * Create and insert a new entry into tab
 *
 * \param tab symbol table
 * \param name name of the new entry
 * \return pointer to the entry or NULL if error
 * \see jast_entry_type
 */
struct jast_entry *jast_new_entry(struct jast *tab, char *name);

// TODO documentar
struct jast_entry *jast_get_entry(struct jast *tab, char *name);

/*!
 * Get the type assigned to an entry
 *
 * \param tab symbol table
 * \param name name of the lexeme
 * \return pointer to the type
 * \see jast_lex_settype
 */
enum jast_entry_t jast_entry_gettype(struct jast *tab, char *name);

/*!
 * Set the type assigned to an entry
 *
 * \param tab symbol table
 * \param name name of the lexeme
 * \param type type of the lexeme
 * \see jast_lex_gettype
 */
void jast_entry_settype(struct jast *tab, char *name, enum jast_entry_t type);

// TODO documentar
enum jast_data_t jast_entry_getdatatype(struct jast *tab, char *name);
void jast_entry_setdatatype(struct jast *tab, char *name, enum jast_data_t type);

// TODO recorrer tablas hijas

/*!
 * Write all contents of symbol table into a file
 *
 * If a file exists with the same name, it'll be overwritten
 *
 * \param tab symbol table
 * \param filename name of the file
 * \return pointer to the symbol table
 */
int jast_dump(struct jast *tab, char *filename);

/*!
 * Show a list of all the active symbol tables
 */
void jast_show_all(void);

#endif	/* __JAST_H */

/*! \file jast.h */