示例#1
0
void getExtDef(TreeNode *head){
	TreeNode *child = head->firstChild, *temp = child;
	TypePoint specifier = getSpecifier(child);
	if(specifier == NULL)
		return;
	child = child->nextSibling;
	if(child->name == ExtDecList){
		//ExtDef->Specifier ExtDecList SEMI;
		getExtDecList(specifier,child);
	}
	else if (child->name == FunDec){
		//Extdef -> Specifier FunDec CompSt | Specifier FunDec SEMI;
		TypePoint type = getFunDec(specifier,child);
		TableNode *result = NULL;
		TableNode *findResult = findTableNode(type->data.structure->name);
		if (findResult == NULL){
			result = insertTableNode(type->data.structure->name, type);	//insert into the table
			FieldListPoint fp = type->data.structure->tail;
			for(;fp!=NULL;fp=fp->tail){
				if(insertTableNode(fp->name,fp->type)==NULL){
					printf("Error type 3 at Line %d: Redefine variable \"%s\".\n",fp->lineno,fp->name);
				}
			}
			if(child->nextSibling->name == SEMI){
				// 将声明的函数加入“声明表”以待后续检验
				result->def_state = -1;		//声明
				insertFuncDefTable(child->lineno, result);
			}
			else{
				result->def_state = 1;
				getCompSt(specifier,child->nextSibling);
			}
		}
		else { //findResult != NULL
			// 判断在表中的类型,如果不符合,返回
			//printf(">>>>%d\n", testType(findResult->type, type));
			if (testType(findResult->type, type) == -1) {
				printf("Errot type 19 at Line %d: Conflict between function declarations.\n", child->lineno);
				return;
			}	
			if(child->nextSibling->name == CompSt){
				// set def_state to 1 and judge redefinition
				if (findResult->def_state == -1) {
					findResult->def_state = 1;
				}
				else {
					printf("Error type 4 at Line %d: Redefined function name \"%s\".\n",root->lineno,type->data.structure->name);
					return;
				}
				getCompSt(specifier,child->nextSibling);
			}

		}
		//getCompSt(specifier,child->nextSibling);
	}
	else{
		//ExtDef -> Specifier SEMI;
	}
	return;
}
示例#2
0
 formatType readFormat(string const &format) {
     formatType answer;
     formatIndex++;
     getFlag(&answer, format);
     answer.width = getNumber(format);
     answer.precision = getPrecision(format);
     answer.length = getLength(format);
     answer.spec = getSpecifier(format);
     return answer;
 }
示例#3
0
 string toString(bool starMode, formatType prototype, string const &format) {
     string answer;
     while (true) { //prints symbols before next "%"
         if (format[formatIndex] == '%' && format[formatIndex + 1] == '%') {
             answer += "%";
             formatIndex += 2;
         }
         formatIndex++;
         if (format[formatIndex - 1] == '%' && getSpecifier(format) != none) {
             throw std::out_of_range("Not enough arguments.");
         }
         if (format[formatIndex - 1] == '%' && getSpecifier(format) == none) {
             throw std::invalid_argument("Wrong format.");
         }
         formatIndex--;
         if (format[formatIndex] == '\0') {
             return answer;
         }
         answer += format[formatIndex];
         formatIndex++;
     }
 }
示例#4
0
FieldListPoint getDef(TreeNode *head){
	TreeNode* child = head->firstChild;
	TypePoint type = getSpecifier(child);
	if(type == NULL)
		return NULL;
	FieldListPoint r = NULL;
	child = child->nextSibling->firstChild;//child->name = Dec
	for(;;){
		TreeNode *dec = child->firstChild;//dec->name = VarDec;
		FieldListPoint result = getVarDec(type,dec);//Here may exist error type 5:Type mismatched for assignment; Aslo  set dec inital.
		if(opType == 0 || opType == 1){
			if(dec->nextSibling!=NULL){//Dec->VarDec ASSIGNOP Exp{
				if(opType == 0){
					printf("Error type 15 at Line %d: Illegal initial in structure for variable \"%s\".\n",result->lineno,result->name);
					child = child->nextSibling;
					if(child != NULL)
						child = child->nextSibling->firstChild;
					else
						break;
					continue;
					printf("Never arrive this in %s at Line%d.\n",__FILE__,__LINE__);
				}
				else{
					TypePoint t = getExp(dec->nextSibling->nextSibling);
					if(t != NULL && testType(t,type) == -1){
						printf("Error type 7 at Line %d: Type mismatched for \"=\".\n",dec->lineno);
					}
				}
			}
			FieldListPoint newField = (FieldListPoint)malloc(sizeof(FieldList));
			newField->name = result->name;
			newField->type = result->type;
			newField->lineno = dec->lineno;
			newField->tail = r;
			r = newField;
		}
		child = child->nextSibling;
		if(child!=NULL){
			child = child->nextSibling->firstChild;
		}
		else
			break;
	}
	return r;
}
示例#5
0
TypePoint getFunDec(TypePoint lastType,TreeNode *root){
	TypePoint type = (TypePoint)malloc(sizeof(Type));
	TreeNode *child = root->firstChild;
	type->kind = FUNCTION;
	type->data.structure = (FieldListPoint)malloc(sizeof(FieldList));
	type->data.structure->name = child->data;
	type->data.structure->type = lastType;
	FieldListPoint field = type->data.structure;
	child = child->nextSibling->nextSibling;
	if(child->name == RP){//FunDec->ID LP RP
		field->tail = NULL;
	}
	else{
		isInsert = 0;
		//FunDec->ID LP VarList Rp
		for(child = child->firstChild;;){
			
			TypePoint t = getSpecifier(child->firstChild);
			if(t!=NULL){
				FieldListPoint fieldTemp=getVarDec(t,child->firstChild->nextSibling);
				
				if(opType == 0){//insert Success!
					field->tail = fieldTemp;
					field = fieldTemp;
				}
			}
			child=child->nextSibling;
			if(child == NULL)
				break;
			else
				child = child->nextSibling->firstChild;
		}
		isInsert = 1;
	}
	return type;
}
示例#6
0
std::string Component::getString() {
	// This will hold our return value
	std::string sentence;

	int componentId = _type.getId();

	if (isInverted()) {
		sentence += "do NOT ";
	}

	SpecifierPtr sp1 = getSpecifier(Specifier::FIRST_SPECIFIER);
	SpecifierPtr sp2 = getSpecifier(Specifier::SECOND_SPECIFIER);

	if (componentId == ComponentType::COMP_KILL().getId()) {
		// First add the verb
		sentence += "kill";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_KO().getId()) {
		// First add the verb
		sentence += "knockout";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_AI_FIND_ITEM().getId()) {
		// First add the verb
		sentence += "let AI find item:";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_AI_FIND_BODY().getId()) {
		sentence += "let AI find body:";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";

		sentence += " (amount: " + getArgument(0) + ")";
	}
	else if (componentId == ComponentType::COMP_ALERT().getId()) {
		sentence += "alert";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";

		// Add the alert level info
		sentence += " " + getArgument(0) + " times to a minimum alert level of " + getArgument(1);
	}
	else if (componentId == ComponentType::COMP_DESTROY().getId()) {
		sentence += "destroy";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_ITEM().getId()) {
		sentence += "acquire";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_PICKPOCKET().getId()) {
		sentence += "pickpocket";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_LOCATION().getId()) {
		sentence += "let the target";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";

		sentence += " be at location";
		sentence += (sp2) ? (" " + sp2->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_INFO_LOCATION().getId()) {
		sentence += "let the target";

		// Add the Specifier details, if any
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";

		sentence += " be at info_location";
		sentence += (sp2) ? (" " + sp2->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_CUSTOM_ASYNC().getId()) {
		sentence += "controlled by external script";
	}
	else if (componentId == ComponentType::COMP_CUSTOM_CLOCKED().getId()) {
		sentence += "call the script function ";
		sentence += getArgument(0);
	}
	else if (componentId == ComponentType::COMP_DISTANCE().getId()) {
		sentence += "let the entities " + getArgument(0);
		sentence += " and " + getArgument(1) + " get closer than ";
		sentence += getArgument(2) + " units";
	}
	else if (componentId == ComponentType::COMP_READABLE_OPENED().getId())
	{
		sentence += "open the readable ";
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_READABLE_CLOSED().getId())
	{
		sentence += "close the readable ";
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}
	else if (componentId == ComponentType::COMP_READABLE_PAGE_REACHED().getId())
	{
		sentence += "view page " + getArgument(0) + " of readable ";
		sentence += (sp1) ? (" " + sp1->getSentence(*this)) : "";
	}

	if (getClockInterval() > 0) {
		sentence += " (check interval: " + string::to_string(getClockInterval()) + " seconds)";
	}

	// Convert the first character of the sentence to upper case
	if (!sentence.empty()) {
		std::string c(sentence.begin(), sentence.begin()+1);
		sentence[0] = string::to_upper_copy(c)[0];

		// Append a full stop at the end of the sentence
		if (sentence[sentence.length() - 1] != '.') {
			sentence.append(".");
		}
	}

	// Replace all double-space characters with one single space
	string::replace_all(sentence, "  ", " ");

    return sentence;
}