Пример #1
0
static void checkTuple(sst::TypecheckState* fs, DecompMapping* bind, fir::Type* rhs, bool immut)
{
	iceAssert(!bind->array);
	if(!rhs->isTupleType())
		error(bind->loc, "expected tuple type in destructuring declaration; found type '%s' instead", rhs);

	auto tty = rhs->toTupleType();
	if(bind->inner.size() != tty->getElementCount())
	{
		error(bind->loc, "too %s bindings in destructuring declaration; expected %d, found %d instead",
			(bind->inner.size() < tty->getElementCount() ? "few" : "many"), tty->getElementCount(), bind->inner.size());
	}

	for(size_t i = 0; i < tty->getElementCount(); i++)
		checkAndAddBinding(fs, &bind->inner[i], tty->getElementN(i), immut, true);
}
void *getRandomElement(List_PNTR l){
  unsigned short rand, max;
  if(l==NULL){
    DAL_error(NULL_POINTER_ERROR);
    return(NULL);
  }
  if(isEmpty(l)){
#if DEBUG
    DAL_error(EMPTY_CONTAINER_ERROR);
#endif
    return(NULL);
  }
  max = getListLength(l) - 1; // elements numbered from 0, so max=length-1
  rand = DAL_random(max);
  return(getElementN(l, rand));
}
Пример #3
0
CGResult sst::TupleAssignOp::_codegen(cgn::CodegenState* cs, fir::Type* infer)
{
	cs->pushLoc(this);
	defer(cs->popLoc());

	auto tuple = this->right->codegen(cs).value;
	if(!tuple->getType()->isTupleType())
		error(this->right, "Expected tuple type in assignment to tuple on left-hand-side; found type '%s' instead", tuple->getType());

	auto tty = tuple->getType()->toTupleType();

	std::vector<CGResult> results;

	size_t idx = 0;
	for(auto v : this->lefts)
	{
		auto res = v->codegen(cs, tty->getElementN(idx));
		if(res->islvalue())
			error(v, "Cannot assign to non-lvalue expression in tuple assignment");

		results.push_back(res);
		idx++;
	}

	for(size_t i = 0; i < idx; i++)
	{
		auto lr = results[i];
		auto val = cs->irb.ExtractValue(tuple, { i });

		auto rr = cs->oneWayAutocast(val, lr.value->getType());
		if(!rr || rr->getType() != lr.value->getType())
		{
			error(this->right, "Mismatched types in assignment to tuple element %d; assigning type '%s' to '%s'",
				val->getType(), lr.value->getType());
		}

		cs->autoAssignRefCountedValue(lr.value, rr, /* isInitial: */ false, /* performStore: */ true);
	}

	return CGResult(0);
}