142 lines
3.2 KiB
C
142 lines
3.2 KiB
C
#ifndef TREE_H
|
|
#define TREE_H
|
|
|
|
#include "lex.h"
|
|
#include "math_functions.h"
|
|
|
|
/**
|
|
* @brief Typ uzlu
|
|
*/
|
|
enum expr_type {
|
|
EXPR_CONST,
|
|
EXPR_NEG,
|
|
EXPR_ADD,
|
|
EXPR_SUB,
|
|
EXPR_MULT,
|
|
EXPR_DIV,
|
|
EXPR_POW,
|
|
EXPR_X,
|
|
EXPR_FN
|
|
};
|
|
|
|
/**
|
|
* @brief Uzel výrazu
|
|
*/
|
|
struct expr_node {
|
|
enum expr_type type;
|
|
union expr_vals {
|
|
struct expr_binop_vals {
|
|
struct expr_node *left;
|
|
struct expr_node *right;
|
|
} binop;
|
|
struct expr_fn_vals {
|
|
size_t fn_idx;
|
|
struct expr_node *args[MAX_MATH_FUNCTION_ARGS];
|
|
} fn;
|
|
struct expr_node *unop;
|
|
double num;
|
|
} vals;
|
|
};
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující konstantu
|
|
*
|
|
* @param val Hodnota konstanty
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_const(double val);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující negaci výrazu
|
|
*
|
|
* @param unop Negovaný výraz (unární operand)
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_neg(struct expr_node *unop);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující sčítání
|
|
*
|
|
* @param left Levý operand
|
|
* @param right Pravý operand
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_add(struct expr_node *left, struct expr_node *right);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující odčítání
|
|
*
|
|
* @param left Levý operand
|
|
* @param right Pravý operand
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_sub(struct expr_node *left, struct expr_node *right);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující násobení
|
|
*
|
|
* @param left Levý operand
|
|
* @param right Pravý operand
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_mult(struct expr_node *left, struct expr_node *right);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující dělení
|
|
*
|
|
* @param left Levý operand
|
|
* @param right Pravý operand
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_div(struct expr_node *left, struct expr_node *right);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující mocnění
|
|
*
|
|
* @param left Levý operand
|
|
* @param right Pravý operand
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_pow(struct expr_node *base, struct expr_node *power);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující proměnnou
|
|
*
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_x(void);
|
|
|
|
/**
|
|
* @brief Vytvoří uzel reprezentující funkci
|
|
*
|
|
* @param fn_idx Index funkce v poli vráceném fns_get()
|
|
* @param args Ukazatel na pole uzlů, které funkce obdrží jako argumenty
|
|
* @return Adresa uzlu
|
|
*/
|
|
struct expr_node *node_create_fn(size_t fn_idx, struct expr_node **args);
|
|
|
|
/**
|
|
* @brief Vyhodnotí uzel
|
|
*
|
|
* @param node Uzel
|
|
* @param x Proměnná
|
|
* @param y Výsledek vyhodnocení
|
|
* @return Stav vyhodnocení
|
|
*/
|
|
enum eval_result node_eval(const struct expr_node *node, double x, double *y);
|
|
|
|
#ifdef ENABLE_GRAPHVIZ_EXPORT
|
|
void node_debug_print_gv(const struct expr_node *node, FILE *output);
|
|
#endif /* ENABLE_GRAPHVIZ_EXPORT */
|
|
|
|
/**
|
|
* @brief Uvolní uzel
|
|
*
|
|
* Tato funkce uvolní i potomky uzlu (operandy, argumenty, ...)
|
|
*
|
|
* @param node Uzel pro uvolnění
|
|
*/
|
|
void node_free(struct expr_node *node);
|
|
|
|
|
|
#endif /* TREE_H */ |