Example #1
0
void Parser::stmt(TNode* parent) {
	// stmt: call | while | if | assign
	if(nextToken.name == "call") {
		// call: "procedure" proc_name ";"
		line++;
		TNode* callNode = createASTNode("CALL_NODE", nextToken.name, parent, line, currentProc);
		controller->statementTable->insertStatement(callNode);
		match("call");



		string newProcedure = procedureName();
		callNode->setData(newProcedure);
		int procIndex = getProcedureIndex(newProcedure);

		if(callees.find(currentProc) != callees.end()) {
			callees[currentProc].insert(make_pair(line, newProcedure));

			stack<int> tempStack = stack<int>();
			while(containerStack.size() > 0) {
				int top = containerStack.top(); containerStack.pop();
				tempStack.push(top);
				callees[currentProc].insert(make_pair(top, newProcedure));
			}

			while(tempStack.size() > 0) {
				containerStack.push(tempStack.top());
				tempStack.pop();
			}
		} else {
			set<std::pair<int, string>> calls;
			calls.insert(std::make_pair(line, newProcedure));

			stack<int> tempStack = stack<int>();
			while(containerStack.size() > 0) {
				int top = containerStack.top(); containerStack.pop();
				tempStack.push(top);
				calls.insert(std::make_pair(top, newProcedure));
			}

			while(tempStack.size() > 0) {
				containerStack.push(tempStack.top());
				tempStack.pop();
			}

			callees.insert(std::pair<int, set<pair<int, string>>>(currentProc, calls));
		}

		controller->callsTable->insertCalls(currentProc, procIndex);
		callStatements.insert(pair<int, string>(line, newProcedure));
		match(";");

		populateProcAndContainers(procIndex, line);
		populateFollows(line, false, prev, callNode);
		populateParent(parent, line);

	} else if(nextToken.name == "while") {
		// while: "while" var_name "{" stmtLst "}"
		line++;
		TNode* whileNode = createASTNode("WHILE_NODE", nextToken.name, parent, line, currentProc);
		controller->statementTable->insertStatement(whileNode);
		match("while");

		populateFollows(line, true, prev, whileNode);
		populateParent(parent, line);
		containerStack.push(line);
		containerNodeStack.push(whileNode);
		populateProcAndContainers(currentProc, line);


		//TNode* whileVarNode = createASTNode("VAR_NODE", nextToken.name, whileNode, line, currentProc);
		whileNode->setData(nextToken.name);
		variableName();

		populateUses(line, currentProc);

		match("{");
		TNode* whileStmtLstNode = createASTNode("STMTLST_NODE", "", whileNode, line, currentProc);
		stmtLst(whileStmtLstNode);
		match("}");
		 
		previousStmt = containerStack.top(); containerStack.pop();
		prev = containerNodeStack.top(); containerNodeStack.pop();
		
	} else if(nextToken.name == "if") {
		// if: "if" var_name "then" "{" stmtLst "}" "else" "{" stmtLst "}"
		++line;
		TNode* ifNode = createASTNode("IF_NODE", nextToken.name, parent, line, currentProc);
		controller->statementTable->insertStatement(ifNode);
		match("if");

		populateFollows(line, true, prev, ifNode);
		populateParent(parent, line);
		containerStack.push(line);
		containerNodeStack.push(ifNode);
		populateProcAndContainers(currentProc, line);

		//TNode* ifVarNode = createASTNode("VAR_NODE", nextToken.name, ifNode, line, currentProc);
		ifNode->setData(nextToken.name);
		variableName();

		populateUses(line, currentProc);

		TNode* thenNode = createASTNode("THEN_NODE", nextToken.name, ifNode, line, currentProc);
		match("then");
		match("{");
		TNode* thenStmtLstNode = createASTNode("STMTLST_NODE", "", thenNode, line, currentProc);
		stmtLst(thenStmtLstNode);
		match("}");
		previousStmt = 0;

		TNode* elseNode = createASTNode("ELSE_NODE", nextToken.name, ifNode, line, currentProc);
		match("else");
		match("{");
		TNode* elseStmtLstNode = createASTNode("STMTLST_NODE", "", elseNode, line, currentProc);
		stmtLst(elseStmtLstNode);
		match("}");

		controller->ast->assignRightSibling(thenNode, elseNode);

		previousStmt = containerStack.top(); containerStack.pop();
		prev = containerNodeStack.top(); containerNodeStack.pop();

	} else {
		// assign: var_name "=" expr ";"
		line++;
		TNode* assignNode = createASTNode("ASSIGN_NODE", "", parent, line, currentProc);
		controller->statementTable->insertStatement(assignNode);

		postfix = "";
		operatorStack.push(Operator(0, "NULL")); // sentinel

		populateFollows(line, false, prev, assignNode);
		populateParent(parent, line);

		TNode* varNode = createASTNode("VAR_NODE", nextToken.name, assignNode, line, currentProc);
		variableName();

		populateModifies(line, currentProc);

		match("=");
		expr(assignNode);
		match(";");

		while(!operatorStack.top().isNull()) {
			popOperator(operatorStack.top());
		}

		//TNode top = operandStack.top();

		controller->ast->assignChild(assignNode, (operandStack.top()));
		controller->ast->assignParent(operandStack.top(), assignNode);

		if(postfix.length() > 0) {
			postfix = " " + postfix + " ";
		}

		cout << "postfix : " << postfix << endl;
		assignNode->setData(postfix);

		operatorStack.pop();		// remove the sentinel
	}
}