/// <summary> /// <whilestmt>::= while “(“ <Bexpr> ”)” “{“ <Statments> “}” /// </summary> /// <param name="pb"></param> /// <returns></returns> public Stmt ParseWhileStatement(ProcedureBuilder pb) { GetNext(); Exp exp = BExpr(pb); if (pb.TypeCheck(exp) != TYPE_INFO.TYPE_BOOL) { throw new Exception("Expects a boolean expression"); } if (Current_Token != TOKEN.TOK_OCBR) { throw new Exception("{ Expected in if stmt"); } GetNext();//Skip { /////******/// ArrayList body = StatementList(pb); if ((Current_Token != TOKEN.TOK_CCBR)) { throw new Exception("} Expected "); } return(new WhileStatement(exp, body)); }
/// <summary> /// <ifstmt>::=if <Bexpr> “{“<Statments> } [ else “{“<Statments>“}”] /// </summary> /// <param name="pb"></param> /// <returns></returns> public Stmt ParseIfStatement(ProcedureBuilder pb) { GetNext(); ArrayList true_part = null; ArrayList false_part = null; Exp exp = BExpr(pb); // Evaluate Expression if (pb.TypeCheck(exp) != TYPE_INFO.TYPE_BOOL) { throw new Exception("in function " + pb.Name + "\tExpects a boolean expression"); } if (Current_Token != TOKEN.TOK_OCBR) { throw new Exception("in function " + pb.Name + "\t { Expected in if stmt"); } // skip { GetNext(); true_part = StatementList(pb); //{.......} GetNext(); // skip } // no else stmt if (Current_Token == TOKEN.TOK_CCBR) {// return fals stmt as null return(new IfStatement(exp, true_part, false_part)); } if (Current_Token != TOKEN.TOK_ELSE) { throw new Exception("in function " + pb.Name + "\tELSE expected"); } GetNext(); // remove else GetNext(); // remove { false_part = StatementList(pb); if (Current_Token != TOKEN.TOK_CCBR) { throw new Exception("in function" + pb.Name + "\t} Expected"); } return(new IfStatement(exp, true_part, false_part)); }
/// <summary> /// <rexpr>=<Bexpr> |fun “(“ <arglist>“)” <retType> <Statments> “;” /// </summary> /// <param name="pb"></param> /// <returns></returns> public Stmt ParseReturnStatement(ProcedureBuilder pb) { GetNext(); //return fun (x numeric)numeric{..... if (Current_Token == TOKEN.TOK_FUNCTION) { GetNext(); pb.InitClsr(); pb.clsoure.Name = pb.Name + "1"; ///////////////////now parse for parameters//////////////////////////// if (Current_Token != TOKEN.TOK_OPAREN) { return(null); } GetNext(); // remove ( //(a int)||() ArrayList lst_types = new ArrayList(); while (Current_Token == TOKEN.TOK_UNQUOTED_STRING) { SYMBOL_INFO inf = new SYMBOL_INFO(); inf.SymbolName = last_str; GetNext(); if (Current_Token == TOKEN.TOK_VAR_BOOL || Current_Token == TOKEN.TOK_VAR_NUMBER || Current_Token == TOKEN.TOK_VAR_STRING) { inf.Type = (Current_Token == TOKEN.TOK_VAR_BOOL) ? TYPE_INFO.TYPE_BOOL : (Current_Token == TOKEN.TOK_VAR_NUMBER) ? TYPE_INFO.TYPE_NUMERIC : TYPE_INFO.TYPE_STRING; } else { return(null); } lst_types.Add(inf.Type); pb.clsoure.AddFormals(inf); pb.clsoure.AddLocal(inf); GetNext(); if (Current_Token != TOKEN.TOK_COMMA) { break; } GetNext(); } if (Current_Token != TOKEN.TOK_CPAREN) { return(null); } GetNext(); //parse for return type (= number/bool/function) if (!(Current_Token == TOKEN.TOK_VAR_BOOL || Current_Token == TOKEN.TOK_VAR_NUMBER || Current_Token == TOKEN.TOK_VAR_STRING || Current_Token == TOKEN.TOK_FUNCTION)) { return(null); } ///-------- Assign the return type pb.clsoure.TYPE = (Current_Token == TOKEN.TOK_VAR_BOOL) ? TYPE_INFO.TYPE_BOOL : (Current_Token == TOKEN.TOK_VAR_NUMBER) ? TYPE_INFO.TYPE_NUMERIC : (Current_Token == TOKEN.TOK_FUNCTION) ?TYPE_INFO.TYPE_FUNCTION:TYPE_INFO.TYPE_STRING; //////////////////////////////////// GetNext(); if (Current_Token != TOKEN.TOK_OCBR) { return(null); } GetNext(); ////***********/******************************************** ///add statments ArrayList lst = StatementList(pb.clsoure); foreach (Stmt st in lst) { pb.clsoure.AddStatement(st); } //ClsrStmt: if (Current_Token != TOKEN.TOK_CCBR) { throw new Exception("} expected in the function"); } return(null); } else { Exp exp = BExpr(pb); if (Current_Token != TOKEN.TOK_SEMI) { throw new Exception(pb.Name + "; expected "); } pb.TypeCheck(exp); return(new ReturnStatement(exp)); } }