evalCond.c 2.02 KB
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <expValue.h>
#include <tepal_pars.h>
#include <evalCond.h>


/*
 * static function (internal only)
 */
static
	int
evalIntComp (int op, s_expVal * _op1, s_expVal * _op2)
{
	long op1 = expValueInt (_op1);
	long op2 = expValueInt (_op2);

	switch (op)
	{
		case EQ:		return (op1 == op2);
		case NE:		return (op1 != op2);
		case LT:		return (op1 < op2);
		case GT:		return (op1 > op2);
		case LE:		return (op1 <= op2);
		case GE:		return (op1 >= op2);
	}
}

static
	int
evalFloatComp (int op, s_expVal * _op1, s_expVal * _op2)
{
	double op1 = expValueFloat (_op1);
	double op2 = expValueFloat (_op2);

	switch (op)
	{
		case EQ:		return (op1 == op2);
		case NE:		return (op1 != op2);
		case LT:		return (op1 < op2);
		case GT:		return (op1 > op2);
		case LE:		return (op1 <= op2);
		case GE:		return (op1 >= op2);
	}
}

static
	int
evalStringComp (int op, s_expVal * _op1, s_expVal * _op2)
{
	char * op1 = expValueString (_op1);
	char * op2 = expValueString (_op2);
	long   ret;

	switch (op)
	{
		case EQ:		ret = (! strcmp (op1, op2));
		case NE:		ret = strcmp (op1, op2);
		case LT:		ret = (strcmp (op1, op2) < 0) ? 1 : 0;
		case GT:		ret = (strcmp (op1, op2) > 0) ? 1 : 0;
		case LE:		ret = (strcmp (op1, op2) <= 0) ? 1 : 0;
		case GE:		ret = (strcmp (op1, op2) >= 0) ? 1 : 0;
	}

	free (op1);
	free (op2);

	return ret;
}



/*
 * public functions (interface)
 */
	int
evalCondExpr (s_expVal * _op)
{
	switch (expValueGetType (_op))
	{
		case EXP_TYP_INT:		return expValueInt (_op);
		case EXP_TYP_FLOAT:	return expValueInt (_op);
		case EXP_TYP_STRING:
									{
										char * op  = expValueString (_op);
										long   ret = strlen (op);

										free (op);

										return ret;
									}
	}

	return 0;
}

	int
evalComp (int op, s_expVal * op1, s_expVal * op2)
{
	switch (expValueGetType (op1))
	{
		case EXP_TYP_INT:
			return evalIntComp (op, op1, op2);

		case EXP_TYP_FLOAT:
			return evalFloatComp (op, op1, op2);

		case EXP_TYP_STRING:
			return evalStringComp (op, op1, op2);
	}
}