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

A compact math expression engine. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "errors.h"
#include "symbolic.h"
#include "expression.h"
Include dependency graph for expression.c:

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...
 
size_t number_len (size_t str_len, char *str)
 
void str_cpy (char *dest, const char *src, size_t count)
 Same as strncpy, but throws an error if a null character is reached in src. 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

Function Documentation

◆ expression_evaluate()

value_t expression_evaluate ( expression_t  exp)
Here is the call graph for this function:

◆ expression_free()

void expression_free ( expression_t  exp)

Free an expression.

Parameters
expThe expression to free
Here is the call graph for this function:

◆ 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.

Here is the call graph for this function:

◆ number_len()

size_t number_len ( size_t  str_len,
char *  str 
)

◆ str_cpy()

void str_cpy ( char *  dest,
const char *  src,
size_t  count 
)

Same as strncpy, but throws an error if a null character is reached in src.

Parameters
destDestination string buffer.
srcSource string buffer.
countThe number of bytes to copy.
Here is the call graph for this function:

◆ 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
Here is the call graph for this function: