当前位置: 首页>>代码示例>>C++>>正文


C++ TypeChecker::assertSameType方法代码示例

本文整理汇总了C++中TypeChecker::assertSameType方法的典型用法代码示例。如果您正苦于以下问题:C++ TypeChecker::assertSameType方法的具体用法?C++ TypeChecker::assertSameType怎么用?C++ TypeChecker::assertSameType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TypeChecker的用法示例。


在下文中一共展示了TypeChecker::assertSameType方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: typeCheck

void BinaryOpExpressionAST::typeCheck(TypeChecker& checker) {
	mRightHandSide->typeCheck(checker);
	mLeftHandSide->typeCheck(checker);

	auto lhsType = mLeftHandSide->expressionType(checker);
	auto rhsType = mRightHandSide->expressionType(checker);

	if (lhsType->name() != "Auto") {
		checker.assertSameType(
			*lhsType, 
			*rhsType,
			asString());
	} else {
		//Infer the type
		auto lhsVarDec = std::dynamic_pointer_cast<VariableDeclarationExpressionAST>(mLeftHandSide);

		if (lhsVarDec != nullptr) {
			if (lhsType->name() == "Auto" && *rhsType == NullReferenceType()) {
				checker.typeError("Implicitly type of a null variable is not allowed.");
			}

			mLeftHandSide = std::make_shared<VariableDeclarationExpressionAST>(
				rhsType->name(),
				lhsVarDec->name(),
				lhsVarDec->isFunctionParameter());

			//Update the symbol
			mSymbolTable->remove(lhsVarDec->name());
			mLeftHandSide->generateSymbols(checker.binder(), mSymbolTable);
		} else {
			//Should never happen
			checker.typeError("Auto type is only allowed in variable declaration.");
		}
	}
}
开发者ID:svenslaggare,项目名称:StackLang,代码行数:35,代码来源:operatorast.cpp

示例2: typeCheck

void ArraySetElementAST::typeCheck(TypeChecker& checker) {
	mArrayRefExpression->typeCheck(checker);
	mAccessExpression->typeCheck(checker);
	mRightHandSide->typeCheck(checker);

	//Check if array
	auto arrayRefType = std::dynamic_pointer_cast<ArrayType>(mArrayRefExpression->expressionType(checker));

	if (arrayRefType == nullptr) {
		checker.typeError("The expression '" + mArrayRefExpression->asString() + "' is not of array type.");
	}

	//Access access
	checker.assertSameType(
		*checker.makeType("Int"),
		*mAccessExpression->expressionType(checker),
		"Expected the array indexing to be of type 'Int'.");

	//Check rhs
	checker.assertSameType(
		*arrayRefType->elementType(), 
		*mRightHandSide->expressionType(checker),
		asString());
}
开发者ID:svenslaggare,项目名称:StackLang,代码行数:24,代码来源:arrayast.cpp

示例3: typeCheck

void SetFieldValueAST::typeCheck(TypeChecker& checker) {
	mObjectRefExpression->typeCheck(checker);

	std::shared_ptr<Type> objRefType;

	if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mObjectRefExpression)) {
		auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name()));
		objRefType = checker.findType(varSymbol->variableType());
	} else if (auto arrayRef = std::dynamic_pointer_cast<ArrayAccessAST>(mObjectRefExpression)) {
		objRefType = arrayRef->expressionType(checker);
	} else {
		checker.typeError("Not implemented");
	}

	auto memberName = getMemberName();
	std::string objName = objRefType->name();

	if (!checker.objectExists(objName)) {
		checker.typeError(objRefType->name() + " is not an object type.");
	}

	auto& object = checker.getObject(objName);

	if (!object.fieldExists(memberName)) {
		checker.typeError("There exists no field '" + memberName + "' in the type '" + objRefType->name() + "'.");
	}

	mRightHandSide->typeCheck(checker);

	//Check rhs
	std::shared_ptr<Type> fieldType;

	if (std::dynamic_pointer_cast<ArrayAccessAST>(mMemberExpression)) {
		fieldType = std::dynamic_pointer_cast<ArrayType>(object.getField(memberName).type())->elementType();
	} else {
		fieldType = object.getField(memberName).type();
	}

	checker.assertSameType(
		*fieldType,
		*mRightHandSide->expressionType(checker),
		asString());
}
开发者ID:svenslaggare,项目名称:StackLang,代码行数:43,代码来源:objectast.cpp


注:本文中的TypeChecker::assertSameType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。