Angband
Typedefs | Enumerations | Functions
z-expression.h File Reference

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_texpression_new (void)
 Allocate and initialize a new expression object.
void expression_free (expression_t *expression)
 Deallocate an expression object.
expression_texpression_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.

Detailed Description

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 Documentation

typedef s32b(* expression_base_value_f)(void)
typedef struct expression_s expression_t

Enumeration Type Documentation

Enumerator:
EXPRESSION_ERR_GENERIC 
EXPRESSION_ERR_INVALID_OPERATOR 
EXPRESSION_ERR_EXPECTED_OPERATOR 
EXPRESSION_ERR_EXPECTED_OPERAND 
EXPRESSION_ERR_DIVIDE_BY_ZERO 

Function Documentation

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.

Parameters
expressionis an initialized expression object.
stringis the string to be parsed.
Returns
The number of operations added to the expression or an error (expression_err_e).

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)
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)
expression_t* expression_new ( void  )
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 
)