Ejemplo n.º 1
0
	shared_ptr<Statement> Parser::parseElse()
	{
		shared_ptr<ElseStmt> elseStmt = make_shared<ElseStmt>();
		elseStmt->value = *it;
		it++;
		if (match(IF))//match if
		{
			shared_ptr<IfStmt> elseifnode = static_pointer_cast<IfStmt>(parseIf());
			elseStmt->addNode(elseifnode);
		}
		else if (match(BEGIN))//match begin Compound Statement 复合语句
		{
			shared_ptr<Statement> body = parseStmtList(END);
			elseStmt->body = body;
			match(END);
			it++;
			match(SEMI);
		}
		else//单行语句
		{
			shared_ptr<Statement> body1 = parseStatement();
			elseStmt->addNode(body1);
		}
		return elseStmt;
	}
Ejemplo n.º 2
0
/*** Parse a statement ***/
Node* Parser::parseStatement()
{
	if(hasTokens(2) && peekToken().getType() == T_IDENTIFIER && peekToken(2).getType() == T_ASSIGN)
		return parseAssign();
	if(hasTokens(2) && peekToken().getType() == T_IDENTIFIER && peekToken(2).getType() == T_LPAREN)
		return parseCall();
	if(hasTokens() && peekToken().getType() == T_CALL)
		return parseCall();
	if(hasTokens() && peekToken().getType() == T_CASE)
		return parseCase();
	if(hasTokens() && peekToken().getType() == T_DO)
		return parseDo();
	if(hasTokens() && peekToken().getType() == T_END)
		return parseEnd();
	if(hasTokens() && peekToken().getType() == T_EXIT)
		return parseExit();
	if(hasTokens() && peekToken().getType() == T_FOR)
		return parseFor();
	if(hasTokens() && peekToken().getType() == T_IF)
		return parseIf();
	if(hasTokens() && peekToken().getType() == T_INPUT)
		return parseInput();
	if(hasTokens() && peekToken().getType() == T_OUTPUT)
		return parseOutput();
	if(hasTokens() && peekToken().getType() == T_REPEAT)
		return parseRepeat();
	if(hasTokens() && peekToken().getType() == T_RETURN)
		return parseReturn();
	if(hasTokens(2) && peekToken(1).getType() == T_IDENTIFIER && peekToken(2).getType() == T_LBRACKET)
		return parseSliceSelectAssign();
	if(hasTokens() && peekToken().getType() == T_WHILE)
		return parseWhile();
	throw ParserSyntaxException(getToken(), "Invalid statement!");
	return 0;
}
Ejemplo n.º 3
0
AstNode* Parser::parseStatement() {
    if (isKeyword(currentTokenValue())) {
      if (currentTokenValue() == "function") {
          return parseFunction();
      } else if (currentTokenValue() == "for") {
          return parseFor();
      } else if (currentTokenValue() == "if") {
          return parseIf();
      } else if (currentTokenValue() == "while") {
          return parseWhile();
      } else if (currentTokenValue() == "print") {
          return parsePrint();
      } else if (currentTokenValue() == "int") {
          return parseDeclaration(VT_INT);
      } else if (currentTokenValue() == "double") {
          return parseDeclaration(VT_DOUBLE);
      } else if (currentTokenValue() == "string") {
          return parseDeclaration(VT_STRING);
      } else if (currentTokenValue() == "return") {
          return parseReturn();
      } else {
          cout << "unhandled keyword " << currentTokenValue() << endl;
          assert(false);
          return 0;
      }
    }
    if (currentToken() == tLBRACE) {
       return parseBlock(true);
    }
    if ((currentToken() == tIDENT) && isAssignment(lookaheadToken(1))) {
        return parseAssignment();
    }
    return parseExpression();
}
Ejemplo n.º 4
0
void DirectiveParser::parseDirective(Token *token)
{
    assert(token->type == Token::PP_HASH);

    mTokenizer->lex(token);
    if (isEOD(token))
    {
        // Empty Directive.
        return;
    }

    DirectiveType directive = getDirective(token);

    // While in an excluded conditional block/group,
    // we only parse conditional directives.
    if (skipping() && !isConditionalDirective(directive))
    {
        skipUntilEOD(mTokenizer, token);
        return;
    }

    switch(directive)
    {
      case DIRECTIVE_NONE:
        mDiagnostics->report(Diagnostics::PP_DIRECTIVE_INVALID_NAME,
                             token->location, token->text);
        skipUntilEOD(mTokenizer, token);
        break;
      case DIRECTIVE_DEFINE:
        parseDefine(token);
        break;
      case DIRECTIVE_UNDEF:
        parseUndef(token);
        break;
      case DIRECTIVE_IF:
        parseIf(token);
        break;
      case DIRECTIVE_IFDEF:
        parseIfdef(token);
        break;
      case DIRECTIVE_IFNDEF:
        parseIfndef(token);
        break;
      case DIRECTIVE_ELSE:
        parseElse(token);
        break;
      case DIRECTIVE_ELIF:
        parseElif(token);
        break;
      case DIRECTIVE_ENDIF:
        parseEndif(token);
        break;
      case DIRECTIVE_ERROR:
        parseError(token);
        break;
      case DIRECTIVE_PRAGMA:
        parsePragma(token);
        break;
      case DIRECTIVE_EXTENSION:
        parseExtension(token);
        break;
      case DIRECTIVE_VERSION:
        parseVersion(token);
        break;
      case DIRECTIVE_LINE:
        parseLine(token);
        break;
      default:
        assert(false);
        break;
    }

    skipUntilEOD(mTokenizer, token);
    if (token->type == Token::LAST)
    {
        mDiagnostics->report(Diagnostics::PP_EOF_IN_DIRECTIVE,
                             token->location, token->text);
    }
}
Ejemplo n.º 5
0
	//Core Method
	shared_ptr<Statement> Parser::parseStatement()
	{
		Tag tags[] = { EQ, UNEQ, LT, GT, LE, GE };

		auto isRealOp = [&](Tag t){
			for (auto i : tags)
			{
				if (t == i)
				{
					return true;
				}
			}
			return false;
		};
		switch (it->tag)
		{
			//VAR,TYPE,Function call,If,Else,While,For,有的可改变currNode,有的不可
			/*case BEGIN:
				break;*/
			case IDENT://此处默认为赋值表达式,实际上大错特错,如果是 函数调用 何如?
				if (advance(BIND))
				{
					shared_ptr<AssignStmt> assign = parseAssign();
					//currNode->addNode(assign);
					return assign;
				}
				else if (advance(OpenBracket))
				{
					shared_ptr<FuncCall> funcCall = parseFunctionCall();
					//currNode->addNode(funcCall);
					return funcCall;
				}
			//暂不支持嵌套函数
			/*case FUNCTION:
				break;
			case PROCEDURE:
				break;*/
			case IF:
			{
				shared_ptr<Node> tmp = currNode;
				shared_ptr<IfStmt> ifstmt = static_pointer_cast<IfStmt>(parseIf());
				currNode = tmp;
				//currNode->addNode(ifstmt);
				return ifstmt; 
			}
			case WHILE:
			{
				shared_ptr<Node> tmp = currNode;
				shared_ptr<WhileStmt> whStmt = static_pointer_cast<WhileStmt>(parseWhile());
				currNode = tmp;
				//currNode->addNode(whStmt);
				return whStmt; 
			}
			case REPEAT:
			{
				shared_ptr<Node> tmp = currNode;
				shared_ptr<RepeatStmt> repeatStmt = static_pointer_cast<RepeatStmt>(parseRepeat());
				currNode = tmp;
				//currNode->addNode(repeatStmt);
				return repeatStmt; 
			}
			case FOR:
			{
				shared_ptr<Node> tmp = currNode;
				shared_ptr<ForStmt> forStmt = static_pointer_cast<ForStmt>(parseFor());
				currNode = tmp;
				//currNode->addNode(forStmt);
				return forStmt; 
			}
			case CASE:
			{
				shared_ptr<Node> tmp = currNode;
				shared_ptr<CaseStmt> caseStmt = static_pointer_cast<CaseStmt>(parseCase());
				currNode = tmp;
				//currNode->addNode(caseStmt);
				return caseStmt; 
			}
			default:
				return NULL;
		}
		return NULL;
	}