summaryrefslogtreecommitdiff
path: root/jast.h
diff options
context:
space:
mode:
Diffstat (limited to 'jast.h')
-rw-r--r--jast.h239
1 files changed, 239 insertions, 0 deletions
diff --git a/jast.h b/jast.h
new file mode 100644
index 0000000..35dec2b
--- /dev/null
+++ b/jast.h
@@ -0,0 +1,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 */