예제 #1
0
Expression *ScriptManager::BuildExpression(ifstream &in)
{
	ExprType type = (ExprType)0;
	in.read((char *)&type, 1);
	ExprOp op = (ExprOp)0;
	string varName = "";
	Expression *lhs = 0, *rhs = 0;
	switch(type)
	{
	case EXPR_LITERAL:
		int value;
		in.read((char *)&value, 4);
		return new ExprLiteral(value);

	case EXPR_VAR:
		getline(in, varName, '\0');
		return new ExprVar(varName);

	case EXPR_UNARY_OP:
		in.read((char *)&op, 1);
		return new ExprUnaryOp(op, BuildExpression(in));

	case EXPR_BINARY_OP:
		in.read((char *)&op, 1);
		lhs = BuildExpression(in);
		rhs = BuildExpression(in);
		return new ExprBinaryOp(op, lhs, rhs);

	default:
		string oops = "Invalid opcode: ";
		oops += (char)type;
		throw ExprParseException(oops);
	}
}
예제 #2
0
BOOL CCalculator::Expression(CCalcExpression** ppcExpression)
{
	BOOL				bOperator;
	BOOL				bOperand;
	BOOL				bFirst;
	CArrayIntAndPointer		cArray;
	CCalcOperator*		pcOperator;
	CCalcExpression*	pcOperand;

	cArray.Init(16);
	bFirst = TRUE;
	for (;;)
	{
		for (;;)
		{
			bOperator = Operator(&pcOperator);
			if (!bOperator)
			{
				break;
			}
			cArray.Add(pcOperator, 0);
		}

		bOperand = Operand(&pcOperand);
		if (!bOperand && !bOperator)
		{
			if (bFirst)
			{
				cArray.Kill();
				return FALSE;
			}
			else
			{
				BuildExpression(ppcExpression, &cArray);

				cArray.Kill();

				if (*ppcExpression == NULL)
				{
					return FALSE;
				}
				else
				{
					return TRUE;
				}
			}
		}
		cArray.Add(pcOperand, 0);
		bFirst = FALSE;
	}
}
//-----------------------------------------------------------------------------
//	Interface to solve a conditional expression. Returns false on failure.
//-----------------------------------------------------------------------------
bool EvaluateExpression( bool &result, const char *InfixExpression, GetSymbolProc_t pGetSymbolProc )
{
	if ( !InfixExpression )
		return false;

	g_pGetSymbolProc = pGetSymbolProc;

	bool success	= false;
	mExpression		= InfixExpression;
	mExprTree		= 0;
	mCurPosition	= 0;

	// Building the expression tree will fail on bad syntax
	if ( BuildExpression() )
	{
		success = true;
		result = SimplifyNode( mExprTree );
	}

	FreeTree( mExprTree );
	return success;
}