Angband
|
Creating, storing, and deserializing simple math expressions. More...
#include "h-basic.h"
Go to the source code of this file.
Typedefs | |
typedef struct expression_operation_s | expression_operation_t |
typedef struct expression_s | expression_t |
typedef s32b(* | expression_base_value_f )(void) |
Enumerations | |
enum | expression_err_e { EXPRESSION_ERR_GENERIC = -1, EXPRESSION_ERR_INVALID_OPERATOR = -2, EXPRESSION_ERR_EXPECTED_OPERATOR = -3, EXPRESSION_ERR_EXPECTED_OPERAND = -4, EXPRESSION_ERR_DIVIDE_BY_ZERO = -5 } |
Functions | |
expression_t * | expression_new (void) |
Allocate and initialize a new expression object. | |
void | expression_free (expression_t *expression) |
Deallocate an expression object. | |
expression_t * | expression_copy (const expression_t *source) |
Return a deep copy of the given expression. | |
void | expression_set_base_value (expression_t *expression, expression_base_value_f function) |
Set the base value function that the operations operate on. | |
s32b | expression_evaluate (expression_t const *const expression) |
Evaluate the given expression. | |
s16b | expression_add_operations_string (expression_t *expression, const char *string) |
Parse a string and add operations and operands to an expression. | |
bool | expression_test_copy (const expression_t *a, const expression_t *b) |
Test to make sure that the deep copy from expression_copy() is equal in value. |
Creating, storing, and deserializing simple math expressions.
Copyright (c) 2013 Ben Semmler
This work is free software; you can redistribute it and/or modify it under the terms of either:
a) the GNU General Public License as published by the Free Software Foundation, version 2, or
b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.
typedef struct expression_operation_s expression_operation_t |
typedef struct expression_s expression_t |
enum expression_err_e |
s16b expression_add_operations_string | ( | expression_t * | expression, |
const char * | string | ||
) |
Parse a string and add operations and operands to an expression.
The string must be in prefix notation and must start with an operator. Basic operators (add, subtract, multiply, and divide) can have multiple operands after the operator. Unary operators (negation) must be followed by another operator. Parsing is done using a state table which is contained in the function.
expression | is an initialized expression object. |
string | is the string to be parsed. |
References expression_add_operation(), EXPRESSION_DELIMITER, EXPRESSION_ERR_DIVIDE_BY_ZERO, EXPRESSION_ERR_EXPECTED_OPERAND, EXPRESSION_ERR_EXPECTED_OPERATOR, EXPRESSION_ERR_GENERIC, EXPRESSION_ERR_INVALID_OPERATOR, expression_input_for_operator(), EXPRESSION_INPUT_INVALID, EXPRESSION_INPUT_MAX, EXPRESSION_INPUT_VALUE, EXPRESSION_MAX_OPERATIONS, expression_operator_from_token(), EXPRESSION_STATE_MAX, EXPRESSION_STATE_OPERAND, EXPRESSION_STATE_OPERATOR, EXPRESSION_STATE_START, i, my_stricmp(), N_ELEMENTS, expression_operation_s::operand, expression_operation_s::operator, OPERATOR_DIV, OPERATOR_NONE, string_free(), string_make(), and value.
Referenced by parse_act_expr(), parse_class_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_trap_expr(), test_alloc(), test_evaluate(), test_parse_failure(), and test_parse_success().
expression_t* expression_copy | ( | const expression_t * | source | ) |
Return a deep copy of the given expression.
References expression_s::base_value, i, mem_free(), mem_zalloc(), expression_operation_s::operand, expression_s::operation_count, expression_s::operations, expression_s::operations_size, and expression_operation_s::operator.
Referenced by dice_bind_expression(), and test_alloc().
s32b expression_evaluate | ( | expression_t const *const | expression | ) |
Evaluate the given expression.
If the base value function is NULL, expression is evaluated from zero.
References expression_s::base_value, i, expression_operation_s::operand, expression_s::operation_count, expression_s::operations, expression_operation_s::operator, OPERATOR_ADD, OPERATOR_DIV, OPERATOR_MUL, OPERATOR_NEG, OPERATOR_SUB, and value.
Referenced by dice_random_value(), and test_evaluate().
void expression_free | ( | expression_t * | expression | ) |
Deallocate an expression object.
References mem_free(), and expression_s::operations.
Referenced by dice_reset(), parse_act_expr(), parse_class_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_trap_expr(), test_alloc(), test_evaluate(), test_parse_failure(), and test_parse_success().
expression_t* expression_new | ( | void | ) |
Allocate and initialize a new expression object.
Returns NULL if it was unable to be created.
References expression_s::base_value, EXPRESSION_ALLOC_SIZE, mem_free(), mem_zalloc(), expression_s::operation_count, expression_s::operations, and expression_s::operations_size.
Referenced by parse_act_expr(), parse_class_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_trap_expr(), test_alloc(), test_evaluate(), test_parse_failure(), and test_parse_success().
void expression_set_base_value | ( | expression_t * | expression, |
expression_base_value_f | function | ||
) |
Set the base value function that the operations operate on.
References expression_s::base_value.
Referenced by parse_act_expr(), parse_class_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_trap_expr(), and test_evaluate().
bool expression_test_copy | ( | const expression_t * | a, |
const expression_t * | b | ||
) |
Test to make sure that the deep copy from expression_copy() is equal in value.
References expression_s::base_value, FALSE, i, expression_operation_s::operand, expression_s::operation_count, expression_s::operations, expression_s::operations_size, expression_operation_s::operator, and TRUE.
Referenced by test_alloc().