⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 funcheck.cpp

📁 C-MINUS编译器
💻 CPP
字号:
#include "FunCheck.h"

void FunArgsCheck::initial()
{
	if (first) delete first;
	first = last = NULL;
}

void FunArgsCheck::insert(TreeNode *pNode)
{	// pNode->nodekind must be funK,
	// pNode->name	must be not in FunDecListRec;
	FunDecListRec *temp = new FunDecListRec(pNode->name, pNode->type);

	// count the params
	TreeNode *p = pNode->child[0];	// params link in pNode->chilld[0]->sibling
    temp->lineno = pNode->lineno;	// record functions' line
	
	if (p)
	{
		temp->params = new ParamListRec(p->type, p->bArr);
		temp->count++;

		ParamListRec *l = temp->params;
		while (p->sibling)
		{
			p = p->sibling;
			l->next = new ParamListRec(p->type, p->bArr);
			l = l->next;
			temp->count++;
		}
	}

	if (!first)  // has not function declarations in list
	{
		first = last = temp;
	}
	else
	{
		last->next = temp;
		last = last->next;
	}
}

// ...
/**:	check(TreeNode* pNode, string &args, int &line)
&
*	check if a function call's argument match its declaration parameters;
*	return -1, not found;
*	return -2, type not match;
*	return -3, match;
*	else return delcaration parameter count, not match,  and
*	"string &args"  is the function's parament list, like "int, int[], int"
*   line to record the functions' lineno that its defined here.
*
*	author: lonelyforest
*	data:	2006.04.08
*/
int FunArgsCheck::check(TreeNode *pNode, string &args, int &line)
{
	FunDecListRec *l = first;

	while (l && l->name != pNode->name)	l = l->next;
	if (l==NULL)	return -1;	// function use before its declaration or not declara

	ParamListRec *p = l->params;
	TreeNode *t = pNode->child[0];

    // record paraments list, this is not a good idea
    while (p) {
    	if (p->type == k_VOID) args  = "void, " + args;
        else if (p->type == k_INT){
        	 args =  (p->isArr ? "int[], " : "int, ") + args;
        }

        p = p->next;
    }

    args.erase(args.length()-2);	// erase last ", "

    line = l->lineno;
    // set it point to l->params after record paraments list
    p = l->params;
	while (p &&t)
	{
		if ((p->type == t->type && p->isArr == t->bArr)
			|| (t->nodekind == expK && t->kind.exp == ConstK && t->type == k_NUM))
		{
			p = p->next;
			t = t->sibling ;
		}
		else
		{
			return -2;	// type not match;
		}
	}

	if (p || t) {
         	return l->count;	// params count not match
    }
	else	return -3;				// all match;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -