struct instruction *parse_instruction(void) { struct expr *expr; struct instruction *res; switch (lookahead[0]->type) { case WHILE: return parse_while(); case DO: return parse_do(); case IDENTIFIER: case LPAREN: expr = parse_expression(); switch(lookahead[0]->type) { case ASSIGN: return parse_assignment_instr(expr); case EOL: if (expr->exprtype != funcalltype) { if (expr->exprtype == binopexprtype && expr->val.binopexpr.op == EQ) { error(expr->pos, "unexpected =, did you mean <- ?"); exit(1); } else syntaxerror("expected instruction, not expression"); } eat(EOL); res = funcallinstr(expr->val.funcall.fun_ident, expr->val.funcall.args, expr->pos); free(expr); return res; default: next(); syntaxerror("unexpected %s", tok->val); return parse_instruction(); } case RETURN: eat(RETURN); if (lookahead[0]->type == EOL) { eat(EOL); return return_stmt(NULL); } else { expr = parse_expression(); eat(EOL); return return_stmt(expr); } case FOR: return parse_for(); case IF: return parse_if(); case SWITCH: return parse_switch(); case ENDOFFILE: return NULL; default: next(); syntaxerror("expected instruction, not %s", tok->val); return parse_instruction(); } }
// Parse a statement. // // stmt -> block-stmt // | declaration-stmt // | expression-stmt Stmt* Parser::stmt() { switch (lookahead()) { case semicolon_tok: return empty_stmt(); case lbrace_tok: return block_stmt(); case return_kw: return return_stmt(); case if_kw: return if_stmt(); case while_kw: return while_stmt(); case break_kw: return break_stmt(); case continue_kw: return continue_stmt(); case var_kw: case def_kw: case foreign_kw: return declaration_stmt(); default: return expression_stmt(); } }
static stmt_code(stream, node, brk, cont, ret) { /* Handle the null expression. */ if ( !node ) return; auto op = node[0]; /* For debugging purposes, put a blank line between each statement. */ fputs("\n", stream); if ( op == 'dcls' ) declaration( stream, node ); else if ( op == 'brea' ) branch( stream, brk ); else if ( op == 'cont' ) branch( stream, cont ); else if ( op == 'retu' ) return_stmt( stream, node, ret ); else if ( op == 'goto' ) goto_stmt( stream, node ); else if ( op == 'if' ) if_stmt( stream, node, brk, cont, ret ); else if ( op == 'whil' ) while_stmt( stream, node, brk, cont, ret ); else if ( op == 'for' ) for_stmt( stream, node, brk, cont, ret ); else if ( op == 'do' ) do_stmt( stream, node, brk, cont, ret ); else if ( op == 'swit' ) switch_stmt( stream, node, brk, cont, ret ); else if ( op == 'case' || op == 'defa' ) case_stmt( stream, node, brk, cont, ret ); else if ( op == ':' ) label_stmt( stream, node, brk, cont, ret ); else if ( op == '{}' ) do_block( stream, node, brk, cont, ret ); else expr_code( stream, node, 0 ); }
static void statement() { int second_token; if(token == TOK_IF){ selection_stmt(); }else if(token == TOK_LBRACKET){ compound_stmt(); }else if(token == TOK_ID||token == TOK_AND){ second_token = virtual_get_token(true); if(second_token == TOK_ASSIGN){ assign_stmt(); }else if(second_token == TOK_LSQUARE){ while(second_token!=TOK_RSQUARE){ second_token = virtual_get_token(false); if(second_token == TOK_ASSIGN ||second_token == TOK_SEMI ||second_token == TOK_EOF) { fprintf( stderr, "line %d: error array var", save_line ); break; } } second_token = virtual_get_token(false); if(second_token == TOK_ASSIGN){ assign_stmt(); }else{ expression_stmt(); } }else{ expression_stmt(); } }else if(token == TOK_WHILE||token == TOK_FOR){ iteration_stmt(); }else if(token == TOK_RETURN){ return_stmt(); }else if(token == TOK_SEMI){ match(TOK_SEMI); }else fprintf( stderr, "line %d: error statements", save_line ); }
/* EBNF: statment -> expression-stmt | compound-stmt | * selection-stmt | iteration-stmt | return-stmt */ static TreeNode * statement (void) { TreeNode * t = NULL; while (token == COMMENT) unexpectedTokenHandling(); switch (token) { /* First(expression-stmt)={ID,LPAREN,NUM} */ case ID: case LPAREN: case NUM: t = expression_stmt(); break; case CLPAREN: t = compound_stmt(); break; case IF: t = selection_stmt(); break; case WHILE: t = iteration_stmt(); break; case RETURN: t = return_stmt(); break; default: unexpectedTokenHandling(); break; } return t; }
void stmt(int level) { int this_line = line_cnt; if(strcmp(tokenPos->textOfLine,"if") == 0) { push_child(this_line,IF); ifstmt(level); } else if(strcmp(tokenPos->textOfLine,"while") == 0) { push_child(this_line,WHILE); itestmt(level); } else if(strcmp(tokenPos->textOfLine,"return") == 0) { push_child(this_line,RETURN); return_stmt(level); } else if(strcmp(tokenPos->textOfLine,"{") == 0) { push_child(this_line,COM_STMT); compound_stmt(level); } else if(tokenPos->tokenType == ID || tokenPos->tokenType == NUM || strcmp(tokenPos->textOfLine,"(") == 0 || strcmp(tokenPos->textOfLine,";") == 0) { push_child(this_line,EXP_STMT); exp_stmt(level); } else { print_err(tokenPos->lineNum,tokenPos->textOfLine,"STMT","IF,WHILE,RETURN,{,ID,NUM,(,;",tokenPos->tokenType); } }