Mathematic Expressions Library  0.1
The core math data structures and manipulators
expression.h File Reference

A compact math expression engine. More...

#include <stddef.h>
#include "types.h"
#include "symbolic.h"
#include "expression_lite.h"
Include dependency graph for expression.h:

Go to the source code of this file.

Data Structures

struct  expression_data_tree
 Expression data container for the expanded expression. More...
 
union  expression_data
 Expression data container. More...
 
struct  expression
 The stored representation of mathematical expressions. More...
 

Macros

#define EXP_BUF_SIZE   256
 

Typedefs

typedef char exp_buf[EXP_BUF_SIZE]
 

Enumerations

enum  expression_type { EXP_VALUE, EXP_TREE, EXP_SYMBOLIC }
 Expression types. More...
 

Functions

expression_t expression_new (void)
 New blank expression. More...
 
expression_t expression_new_value (value_t val)
 
expression_t expression_new_tree (char op, expression_t left, expression_t right)
 
expression_t expression_new_sym (sym_t sym)
 
void expression_free (expression_t exp)
 Free an expression. More...
 
value_t expression_evaluate (expression_t exp)
 
void expression_to_string (char *dst_str, expression_t src_exp)
 Expression to String. More...
 
expression_t string_to_expression (size_t str_len, char const *str)
 Convert String to an Expression. More...
 

Detailed Description

A compact math expression engine.

Date
Apr 6, 2014
Author
Craig Hesling

Macro Definition Documentation

◆ EXP_BUF_SIZE

#define EXP_BUF_SIZE   256

Typedef Documentation

◆ exp_buf

typedef char exp_buf[EXP_BUF_SIZE]

Enumeration Type Documentation

◆ expression_type

Expression types.

This is an enumeration of expression_t's types. Specific types correspond to specific data containers in expression_data.

Enumerator
EXP_VALUE 

Contains a simple numeric values and undefines.

EXP_TREE 

An expression over an operation and sub-expression.

EXP_SYMBOLIC 

Symbolic named references.

Function Documentation

◆ expression_evaluate()

value_t expression_evaluate ( expression_t  exp)

◆ expression_free()

void expression_free ( expression_t  exp)

Free an expression.

Parameters
expThe expression to free

◆ expression_new()

expression_t expression_new ( void  )

New blank expression.

This function creates the most basic empty expression.

Returns
A new empty expression

◆ expression_new_sym()

expression_t expression_new_sym ( sym_t  sym)
Here is the call graph for this function:

◆ expression_new_tree()

expression_t expression_new_tree ( char  op,
expression_t  left,
expression_t  right 
)
Here is the call graph for this function:

◆ expression_new_value()

expression_t expression_new_value ( value_t  val)
Here is the call graph for this function:

◆ expression_to_string()

void expression_to_string ( char *  dst_str,
expression_t  src_exp 
)

Expression to String.

◆ string_to_expression()

expression_t string_to_expression ( size_t  str_len,
char const *  str 
)

Convert String to an Expression.

Parses a string into an expression.

Note
level was type int - changed to unsigned to temporarily quiet compiler
Bug:
Cannot parse negative numbers
Warning
Symbols must start with ALPHA chars to be properly identified.

Algorithm

Parsing Overview

  • Step 1: Recording details about the string while scanning from left to right.
  • Step 2: There are two real cases. The string is a single number (with no operations) or it is two sub-expressions joined by an operation.
    Cases:
    • If it is a simple number, we know how to parse it and create a value expression.
    • If it is not just a number, we want split the string about the operation at the lowest level. (in practice, we end up finding the one that is farthest left of the lowest level) We then construct a tree expression with the chosen operation and the sub-expression results from running the function recursively on each of the sub-strings.
Parameters
str_lenLength of given string
strString to parse
Returns
The expression_t representation of the inputed string
Test:
Test for negative values
Todo:
Allow symbols to be detected when they start with numeric digits also. string_to_sym is already compatible.

< Store original index

< Used to explore the presence of a symbol parameter

Todo:
Do series of additional syntax checks here.
Todo:
Syntax Check: Check that level==0
Todo:
Syntax Check: Check that oparen_count==cparen_count
Note
left_buf and right_buf were changed to use malloc to avoid errors with C90 compilers