Exemplo n.º 1
0
void Expression::PrintTree(std::ostream& os) const
{
	if (Empty())
	{
		throw ExpressionError("Error: Expression is empty!");
	}

	m_topNode->Print(os);
}
Exemplo n.º 2
0
double Expression::Evaluate() const
{
	if (Empty())
	{
		throw ExpressionError("Error: Expression is empty!");
	}

	return m_topNode->Evaluate();
}
Exemplo n.º 3
0
int IsVariableDefinition(const std::string &a_expression)
{
	bool found = false;
	int index;

	for (int i = 0; i < a_expression.length(); i++)
	{
		if (a_expression[i] == ':')
		{
			if (!found)
			{
				found = true;
				index = i;
			}
			else throw ExpressionError("Invalid assignment", new Token);
		}
	}

	if (found)
	{
		// parse
		int name_end = index - 1;
		while (a_expression[name_end] == ' ' && name_end >= 0) name_end--;
		if (name_end < 0) throw ExpressionError("Invalid variable definition", new Token);
		
		last_variable_name = a_expression.substr(0, name_end + 1);
		if (!isvalidname(last_variable_name) || isallupper(last_variable_name, true)) return 0;

		int value_start = index + 1;
		while (a_expression[value_start] == ' ' && value_start < a_expression.length()) value_start++;
		if (value_start == a_expression.length())
		{
			G::variables.Remove(HashFunc(last_variable_name));
			return 2;
		}
		last_variable_value = a_expression.substr(value_start);
		return 1;
	}

	return 0;
}
Exemplo n.º 4
0
void TokenListToRPN(TokenList &a_list, RPNStack &a_rpn)
{
	// Shunting yard algorithm
	std::vector<Token*> opStack;
	Token *c_token = a_list.head;

	while (c_token)
	{
		switch (c_token->type)
		{
		case Token::Type::Name:
			if (isalllower(c_token->name, true) && isvalidname(c_token->name))
			{
				if (G::functions.Exists(c_token->name))
				{
					opStack.push_back(c_token);
					c_token->type = Token::Type::E_Function;
				}
				else if (G::variables.Exists(c_token->name))
				{
					a_rpn.push_back({ ExprNode::Type::Number, G::variables.Get(c_token->name) });
				}
				else throw ExpressionError("Undefined reference", c_token);
			}
			else if (isallupper(c_token->name, true) && isvalidname(c_token->name))
			{
				if (G::constants.Exists(c_token->name))
					a_rpn.push_back({ ExprNode::Type::Number, G::constants.Get(c_token->name) });
				else throw ExpressionError("Undefined constant", c_token);
			}
			else throw ExpressionError("Invalid value name", c_token);
			break;
		case Token::Type::Number:
			a_rpn.push_back({ ExprNode::Type::Number, c_token->value });
			break;
		case Token::Type::Operator:
			if (opStack.size() > 0)
			{
				if (c_token->optype == Token::OpType::RPar)
				{
					Token *tos = opStack.back();
					if (opStack.size() == 0)
						throw ExpressionError("#1 Mismatched parenthesis", c_token);
					while (tos->optype != Token::OpType::LPar)
					{
						a_rpn.push_back({ ExprNode::Type::Operator, 0, tos->optype });
						opStack.pop_back();
						tos = opStack.back();
						if (opStack.size() == 0)
							throw ExpressionError("#2 Mismatched parenthesis", c_token);
					}
					opStack.pop_back(); // pop opening parantheses
					if (opStack.size() > 0)
					{
						tos = opStack.back();
						if (tos->type == Token::Type::E_Function)
						{
							a_rpn.push_back({ ExprNode::Type::Function, 0, Token::OpType::None,
								HashFunc(tos->name) });
							opStack.pop_back();
						}
					}
				}
				else if (c_token->optype == Token::OpType::Comma)
				{
					Token *tos = opStack.back();
					if (opStack.size() == 0)
						throw ExpressionError("Invalid separator", c_token);
					while (tos->optype != Token::OpType::LPar)
					{
						a_rpn.push_back({ ExprNode::Type::Operator, 0, tos->optype });
						opStack.pop_back();
						tos = opStack.back();
						if (opStack.size() == 0)
							throw ExpressionError("Invalid separator", c_token);
					}
				}
				else
				{
					if (c_token->optype != Token::OpType::LPar)
					{
						while (opStack.size() > 0 && opStack.back()->optype != Token::OpType::LPar)
						{
							Token *tos = opStack.back();
							if ((is_la(c_token->optype) && precedence(c_token->optype) <= precedence(tos->optype)) ||
								(is_ra(c_token->optype) && precedence(c_token->optype) < precedence(tos->optype)))
							{
								opStack.pop_back();
								a_rpn.push_back({ ExprNode::Type::Operator, 0, tos->optype });
							}
							else break;
						}
					}
					opStack.push_back(c_token);
				}
			}
			else if (c_token->optype != Token::OpType::RPar ||
					c_token->optype != Token::OpType::Comma)
				opStack.push_back(c_token);
			break;
		case Token::Type::Special:
			a_rpn.push_back({ ExprNode::Type::Special, 0, ExprNode::OpType::None, 0, c_token->sptype });
			break;
		}

		c_token = c_token->next;
	}
	
	for (unsigned i = opStack.size(); i-- > 0; )
	{
		if (opStack[i]->optype == Token::OpType::LPar ||
			opStack[i]->optype == Token::OpType::RPar)
			throw ExpressionError("#3 Mismatched parenthesis", opStack[i]);
		a_rpn.push_back({ ExprNode::Type::Operator, 0, opStack[i]->optype });
	}
}
Exemplo n.º 5
0
void ExprToValue(const ExprPtrT& e, short unsigned int& val)
{
   if (!(e->toTypeQ(typeid(int)))) 
      throw ExpressionError(ExprToValueErrorDescription<int>(e));
   val = int(*e);
};
Exemplo n.º 6
0
void ExprToValue(const ExprPtrT& e, float& val)
{
   if (!(e->toTypeQ(typeid(double)))) 
      throw ExpressionError(ExprToValueErrorDescription<double>(e));
   val = double(*e);
};