Beispiel #1
0
void Expression::SubstituteSlots(const ExprVector &slots)
{
	string functionName = FunctionName();
	if(functionName == "Slot")
	{
		IntegerType index;
		if(leaves.empty())
			index = 1;
		else
		{
			//MachineInteger *indexInt = leaves.at(0)->MachineIntegerHead();
			Integer *indexInt(dynamic_cast<Integer*>(leaves.at(0)->NumberHead()));
			if(indexInt)
				index = indexInt->IntValue();
			else
				throw EvaluateException("Slot expected to have an Integer argument.");
		}
		Expression *slot;
		if(index > 0 && index-1 < slots.size())
			slot = slots.at(static_cast<ExprVector::size_type>(index-1));
		else
			throw EvaluateException("Slot not given.");
		AssignCloned(slot);
	}
	else if(functionName == "SlotSequence")
	{
		delete head;
		head = new Expression("Sequence");
		DeleteLeaves();
		leaves.reserve(slots.size());
		for(ExprVector::const_iterator leaf = slots.begin(); leaf != slots.end(); ++leaf)
			AppendLeaf((*leaf)->Clone());
	}
	else if(functionName != "Function")
	{
		if(head)
			head->SubstituteSlots(slots);
		for(ExprVector::const_iterator leaf = leaves.begin(); leaf != leaves.end(); ++leaf)
			(*leaf)->SubstituteSlots(slots);
	}
}