File indexing completed on 2024-05-12 03:47:45

0001 /*
0002     File                 : parser.h
0003     Project              : LabPlot
0004     Description          : Parser for mathematical expressions
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2014-2020 Stefan Gerlach <stefan.gerlach@uni.kn>
0007     SPDX-FileCopyrightText: 2014 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef PARSER_H
0012 #define PARSER_H
0013 
0014 #include "constants.h"
0015 #include "functions.h"
0016 #include "parserFunctionTypes.h"
0017 #include <gsl/gsl_version.h>
0018 #include <memory>
0019 #include <variant>
0020 
0021 /* uncomment to enable parser specific debugging */
0022 /* #define PDEBUG 1 */
0023 
0024 /* variables to pass to parser */
0025 #define MAX_VARNAME_LENGTH 10
0026 typedef struct parser_var {
0027     char name[MAX_VARNAME_LENGTH];
0028     double value;
0029 } parser_var;
0030 
0031 struct Payload {
0032     Payload(bool constant = false)
0033         : constant(constant) {
0034     }
0035     virtual ~Payload() {
0036     }
0037     bool constant{false};
0038 };
0039 
0040 struct special_function_def {
0041     special_function_def()
0042         : funsptr(nullptr)
0043         , payload(std::weak_ptr<Payload>()) {
0044     }
0045     funs* funsptr;
0046     std::weak_ptr<Payload> payload;
0047 };
0048 
0049 /* structure for list of symbols */
0050 typedef struct symbol {
0051     char* name; /* name of symbol */
0052     int type; /* type of symbol: either VAR or FNCT */
0053     std::variant<double, funs*, special_function_def> value;
0054     struct symbol* next; /* next symbol */
0055 } symbol;
0056 
0057 int variablesCounter();
0058 
0059 void init_table(void); /* initialize symbol table */
0060 void delete_table(void); /* delete symbol table */
0061 int parse_errors(void);
0062 symbol* assign_symbol(const char* symbol_name, double value);
0063 int remove_symbol(const char* symbol_name);
0064 double parse(const char* string, const char* locale);
0065 double parse_with_vars(const char[], const parser_var[], int nvars, const char* locale);
0066 bool set_specialfunction0(const char* function_name, func_tPayload function, std::shared_ptr<Payload> payload);
0067 bool set_specialfunction1(const char* function_name, func_t1Payload function, std::shared_ptr<Payload> payload);
0068 bool set_specialfunction2(const char* function_name, func_t2Payload function, std::shared_ptr<Payload> payload);
0069 bool set_specialfunction3(const char* function_name, func_t3Payload function, std::shared_ptr<Payload> payload);
0070 bool set_specialfunction4(const char* function_name, func_t4Payload function, std::shared_ptr<Payload> payload);
0071 const char* lastErrorMessage();
0072 
0073 extern bool skipSpecialFunctionEvaluation;
0074 #endif /*PARSER_H*/