1 /* 2 * Copyright (C) 1999, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2011, 3 * 2012, 2013 Free Software Foundation, Inc. 4 * 5 * This file is part of GNU libmatheval 6 * 7 * GNU libmatheval is free software: you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation, either version 3 of the 10 * License, or (at your option) any later version. 11 * 12 * GNU libmatheval is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with GNU libmatheval. If not, see 19 * <http://www.gnu.org/licenses/>. 20 */ 21 22 #ifndef SYMBOL_TABLE_H 23 #define SYMBOL_TABLE_H 1 24 25 #if HAVE_CONFIG_H 26 #include "config.h" 27 #endif 28 29 /* Data structure representing symbol table record. */ 30 typedef struct _Record { 31 struct _Record *next; /* Pointer to next record. */ 32 char *name; /* Symbol name. */ 33 char type; /* Symbol type ('c' for constant, 'v' for 34 * variable, 'f' for function). */ 35 union { 36 double value; /* Constant or variable value. */ 37 double (*function) (double); /* Pointer to 38 * function to 39 * calculate its 40 * value. */ 41 } data; 42 int flag; /* Record flag used for symbol table 43 * selective traversal. */ 44 } Record; 45 46 /* Data structure representing symbol table (hash table is used for this 47 * purpose). */ 48 typedef struct { 49 int length; /* Hash table length. */ 50 Record *records; /* Hash table buckets. */ 51 int reference_count; /* Reference count for 52 * symbol table (evaluator 53 * for derivative uses 54 * same symbol table as 55 * evaluator for 56 * corresponding 57 * function). */ 58 } SymbolTable; 59 60 /* Create symbol table using specified length of hash table. */ 61 SymbolTable *symbol_table_create(int length); 62 63 /* Destroy symbol table. */ 64 void symbol_table_destroy(SymbolTable * symbol_table); 65 66 /* Insert symbol into given symbol table. Further arguments are symbol 67 * name and its type, as well as additional arguments according to symbol 68 * type. Return value is pointer to symbol table record created to 69 * represent symbol. If symbol already in symbol table, pointer to its 70 * record is returned immediately. */ 71 Record *symbol_table_insert(SymbolTable * symbol_table, char *name, 72 char type, ...); 73 74 /* Lookup symbol by name from given symbol table. Pointer to symbol 75 * record is returned if symbol found, null pointer otherwise. */ 76 Record *symbol_table_lookup(SymbolTable * symbol_table, 77 char *name); 78 79 /* Clear flag for each symbol table record. */ 80 void symbol_table_clear_flags(SymbolTable * symbol_table); 81 82 /* Count number of flagged records in symbol table. */ 83 int symbol_table_get_flagged_count(SymbolTable * symbol_table); 84 85 /* Fill given array with pointers to records from given symbol table that 86 * have flag set. Further arguments are array to store pointers and array 87 * capacity. Number of records that are actually put into array is 88 * returned. */ 89 int symbol_table_get_flagged(SymbolTable * symbol_table, 90 Record ** records, int length); 91 92 /* Return symbol table pointer to be assigned to variable. This function 93 * should be used instead of simple pointer assignement for proper 94 * reference counting. Users willing to manage reference counts by 95 * themselves are free to ignore this function. */ 96 SymbolTable *symbol_table_assign(SymbolTable * symbol_table); 97 98 #endif 99