Angband
Data Structures | Macros | Typedefs | Enumerations | Functions
z-expression.c File Reference

Creating, storing, and deserializing simple math expressions. More...

#include "z-expression.h"
#include "z-virt.h"
#include "z-util.h"

Data Structures

struct  expression_operation_s
 
struct  expression_s
 

Macros

#define EXPRESSION_ALLOC_SIZE   5
 Allocation block size for the operations array in expression_t. More...
 
#define EXPRESSION_DELIMITER   " "
 Token delimiter for the parser. More...
 
#define EXPRESSION_MAX_OPERATIONS   50
 Maximum number of operations in an expression. More...
 

Typedefs

typedef enum expression_operator_e expression_operator_t
 Operator types. More...
 
typedef enum expression_state_e expression_state_t
 States for parser state table. More...
 
typedef enum expression_input_e expression_input_t
 Input types for parser state table. More...
 

Enumerations

enum  expression_operator_e {
  OPERATOR_NONE, OPERATOR_ADD, OPERATOR_SUB, OPERATOR_MUL,
  OPERATOR_DIV, OPERATOR_NEG
}
 Operator types. More...
 
enum  expression_state_e { EXPRESSION_STATE_START, EXPRESSION_STATE_OPERATOR, EXPRESSION_STATE_OPERAND, EXPRESSION_STATE_MAX }
 States for parser state table. More...
 
enum  expression_input_e {
  EXPRESSION_INPUT_INVALID, EXPRESSION_INPUT_NEEDS_OPERANDS, EXPRESSION_INPUT_UNARY_OPERATOR, EXPRESSION_INPUT_VALUE,
  EXPRESSION_INPUT_MAX
}
 Input types for parser state table. More...
 

Functions

static expression_operator_t expression_operator_from_token (const char *token)
 Return an operator type based on the input token. More...
 
static expression_input_t expression_input_for_operator (expression_operator_t operator)
 Return the state table input type for a given operator type. More...
 
expression_texpression_new (void)
 Allocate and initialize a new expression object. More...
 
void expression_free (expression_t *expression)
 Deallocate an expression object. More...
 
expression_texpression_copy (const expression_t *source)
 Return a deep copy of the given expression. More...
 
void expression_set_base_value (expression_t *expression, expression_base_value_f function)
 Set the base value function that the operations operate on. More...
 
s32b expression_evaluate (expression_t const *const expression)
 Evaluate the given expression. More...
 
static void expression_add_operation (expression_t *expression, const expression_operation_t operation)
 Add an operation to an expression, allocating more memory as needed. More...
 
s16b expression_add_operations_string (expression_t *expression, const char *string)
 Parse a string and add operations and operands to an expression. More...
 
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. More...
 

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.

Macro Definition Documentation

#define EXPRESSION_ALLOC_SIZE   5

Allocation block size for the operations array in expression_t.

Referenced by expression_add_operation(), and expression_new().

#define EXPRESSION_DELIMITER   " "

Token delimiter for the parser.

Referenced by expression_add_operations_string().

#define EXPRESSION_MAX_OPERATIONS   50

Maximum number of operations in an expression.

This number is used in the parser to allocate an array of expression_operation_t.

Referenced by expression_add_operations_string().

Typedef Documentation

Input types for parser state table.

Operator types.

States for parser state table.

Enumeration Type Documentation

Input types for parser state table.

Enumerator
EXPRESSION_INPUT_INVALID 
EXPRESSION_INPUT_NEEDS_OPERANDS 
EXPRESSION_INPUT_UNARY_OPERATOR 
EXPRESSION_INPUT_VALUE 
EXPRESSION_INPUT_MAX 

Operator types.

Enumerator
OPERATOR_NONE 
OPERATOR_ADD 
OPERATOR_SUB 
OPERATOR_MUL 
OPERATOR_DIV 
OPERATOR_NEG 

States for parser state table.

Enumerator
EXPRESSION_STATE_START 
EXPRESSION_STATE_OPERATOR 
EXPRESSION_STATE_OPERAND 
EXPRESSION_STATE_MAX 

Function Documentation

static void expression_add_operation ( expression_t expression,
const expression_operation_t  operation 
)
static

Add an operation to an expression, allocating more memory as needed.

References EXPRESSION_ALLOC_SIZE, mem_realloc(), expression_s::operation_count, expression_s::operations, and expression_s::operations_size.

Referenced by expression_add_operations_string().

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, NULL, 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_curse_expr(), parse_mon_spell_expr(), parse_object_expr(), parse_object_power_expr(), parse_trap_expr(), parse_trap_expr_xtra(), 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, NULL, 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)
static expression_input_t expression_input_for_operator ( expression_operator_t  operator)
static
expression_t* expression_new ( void  )
static expression_operator_t expression_operator_from_token ( const char *  token)
static

Return an operator type based on the input token.

References OPERATOR_ADD, OPERATOR_DIV, OPERATOR_MUL, OPERATOR_NEG, OPERATOR_NONE, and OPERATOR_SUB.

Referenced by expression_add_operations_string().

void expression_set_base_value ( expression_t expression,
expression_base_value_f  function 
)
bool expression_test_copy ( const expression_t a,
const expression_t b 
)