tinyexpr

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

tinyexpr

Post by srvaldez »

here's a tiny expression evaluator https://github.com/codeplea/tinyexpr
before you compile tinyexpr I suggest that you un-comment #define TE_POW_FROM_RIGHT
when compiled as a static library with -O2 it's about 1.75 times faster than muParser
tinyexpr.bi

Code: Select all

#pragma once

#inclib "tinyexpr"

extern "C"

#define __TINYEXPR_H__

type te_expr
	as long type

	union
		value as double
		bound as const double ptr
		function as const any ptr
	end union

	parameters(0 to 0) as any ptr
end type

enum
	TE_VARIABLE = 0
	TE_FUNCTION0 = 8
	TE_FUNCTION1
	TE_FUNCTION2
	TE_FUNCTION3
	TE_FUNCTION4
	TE_FUNCTION5
	TE_FUNCTION6
	TE_FUNCTION7
	TE_CLOSURE0 = 16
	TE_CLOSURE1
	TE_CLOSURE2
	TE_CLOSURE3
	TE_CLOSURE4
	TE_CLOSURE5
	TE_CLOSURE6
	TE_CLOSURE7
	TE_FLAG_PURE = 32
end enum

type te_variable
	name as zstring ptr
	address as any ptr
	as long type
	context as any ptr
end type

declare function te_interp(byval expression as const zstring ptr, byval err0r as long ptr) as double
declare function te_compile(byval expression as const zstring ptr, byval variables as const te_variable ptr, byval var_count as long, byval err0r as long ptr) as te_expr ptr
declare function te_eval(byval n as const te_expr ptr) as double
declare sub te_print(byval n as const te_expr ptr)
declare sub te_free(byval n as te_expr ptr)

end extern
example2.bas

Code: Select all

#pragma once

#include once "tinyexpr.bi"
#include once "crt/stdio.bi"

dim expression as string = "(cos( 20 ) *  sin( 5  )  ^ 3  * 21 * ln(  123 ) / 2.5) * variable_a * variable_b "
printf(!"Evaluating:\n\t%s\n", expression)
dim x as double
dim y as double
dim vars(0 to ...) as te_variable = {(@"variable_a", @x), (@"variable_b", @y)}
dim as long err0r, iter
dim n as te_expr ptr = te_compile(expression, @vars(0), 2, @err0r)
dim as double time1, fbsum, fbtime, EvalResult
if n then
	time1=timer
	iter=0
	fbsum = 0
	for inc as integer = 0 to 10000
		x=10
		y=10.345
		EvalResult = te_eval(n)
		fbsum += EvalResult
		iter+=1
	next
	fbtime=timer-time1
	? fbtime
	printf(!"Result:\n\t%.16g\n", fbsum)
	te_free(n)
else
	printf(!"\t%*s^\nError near here", err0r - 1, "")
end if
example3.bas

Code: Select all

#pragma once

#include once "crt/stdio.bi"
#include once "tinyexpr.bi"

extern "C"
function my_sum (byval a as double, byval b as double) as double
	printf(!"Called FB function with %f and %f.\n", a, b)
	return a + b
end function
end extern

dim vars(0 to ...) as te_variable = {(@"mysum", @my_sum, TE_FUNCTION2)}
dim expression as string = "mysum(5, 6)"
printf(!"Evaluating:\n\t%s\n", expression)
dim err0r as long
dim n as te_expr ptr = te_compile(expression, @vars(0), 1, @err0r)
if n then
	dim r as const double = te_eval(n)
	printf(!"Result:\n\t%f\n", r)
	te_free(n)
else
	printf(!"\t%*s^\nError near here", err0r - 1, "")
end if
Post Reply