Parse_stat MCLocaltoken::parse(MCScriptPoint &sp) { initpoint(sp); while (True) { Symbol_type type; Parse_stat stat = sp.next(type); if (stat == PS_EOL) return PS_NORMAL; const LT *te; MCExpression *newfact = NULL; if (stat != PS_NORMAL || type != ST_ID || sp.lookup(SP_FACTOR, te) != PS_NO_MATCH || sp.lookupconstant(&newfact) == PS_NORMAL) { delete newfact; MCperror->add(PE_LOCAL_BADNAME, sp); return PS_ERROR; } MCAutoNameRef t_token_name; /* UNCHECKED */ t_token_name . Clone(sp . gettoken_nameref()); MCExpression *e = NULL; MCVarref *v = NULL; if (sp.gethandler() == NULL) if (constant) sp.gethlist()->findconstant(t_token_name, &e); else sp.gethlist()->findvar(t_token_name, false, &v); else if (constant) sp.gethandler()->findconstant(t_token_name, &e); else sp.gethandler()->findvar(t_token_name, &v); if (e != NULL || v != NULL) { MCperror->add(PE_LOCAL_SHADOW, sp); delete v; delete e; return PS_ERROR; } MCVariable *tmp; for (tmp = MCglobals ; tmp != NULL ; tmp = tmp->getnext()) if (tmp -> hasname(t_token_name)) if (MCexplicitvariables) { MCperror->add(PE_LOCAL_SHADOW, sp); return PS_ERROR; } MCVarref *tvar = NULL; MCString init; bool initialised = false; if (sp.skip_token(SP_FACTOR, TT_BINOP, O_EQ) == PS_NORMAL) { if (sp.next(type) != PS_NORMAL || MCexplicitvariables && type != ST_LIT && type != ST_NUM) { if (constant) MCperror->add(PE_CONSTANT_BADINIT, sp); else MCperror->add(PE_LOCAL_BADINIT, sp); return PS_ERROR; } if (type == ST_MIN) { // negative initializer const char *sptr = sp.gettoken().getstring(); if (sp.next(type) != PS_NORMAL || type != ST_NUM) { if (constant) MCperror->add(PE_CONSTANT_BADINIT, sp); else MCperror->add(PE_LOCAL_BADINIT, sp); return PS_ERROR; } uint4 l = sp.gettoken().getstring() + sp.gettoken().getlength() - sptr; init.set(sptr, l); } else init = sp.gettoken(); initialised = true; } else if (constant) { MCperror->add(PE_CONSTANT_NOINIT, sp); return PS_ERROR; } else init = NULL; MCAutoNameRef t_init_name; if (initialised) /* UNCHECKED */ t_init_name . CreateWithOldString(init); else t_init_name . Clone(kMCEmptyName); if (sp.gethandler() == NULL) { if (constant) sp.gethlist()->newconstant(t_token_name, t_init_name); else if (sp.gethlist()->newvar(t_token_name, t_init_name, &tvar, initialised) != PS_NORMAL) { MCperror->add(PE_LOCAL_BADNAME, sp); return PS_ERROR; } } else if (constant) sp.gethandler()->newconstant(t_token_name, t_init_name); else if (sp.gethandler()->newvar(t_token_name, t_init_name, &tvar) != PS_NORMAL) { MCperror->add(PE_LOCAL_BADNAME, sp); return PS_ERROR; } delete tvar; switch (sp.next(type)) { case PS_NORMAL: if (type != ST_SEP) { MCperror->add(PE_STATEMENT_NOTSEP, sp); return PS_ERROR; } break; case PS_EOL: case PS_EOF: return PS_NORMAL; default: MCperror->add(PE_STATEMENT_NOTSEP, sp); return PS_ERROR; } } return PS_NORMAL; }
Parse_stat MCRepeat::parse(MCScriptPoint &sp) { Parse_stat stat; Symbol_type type; const LT *te; initpoint(sp); if ((stat = sp.next(type)) != PS_NORMAL) { if (stat == PS_EOL) { if (sp.skip_eol() != PS_NORMAL) { MCperror->add (PE_REPEAT_BADCONDEOL, sp); return PS_ERROR; } } else { MCperror->add (PE_REPEAT_BADCONDTYPE, sp); return PS_ERROR; } } else switch (stat) { case PS_NORMAL: if (sp.lookup(SP_REPEAT, te) != PS_NORMAL) { sp.backup(); form = RF_FOR; if (sp.parseexp(False, True, &endcond) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADCOND, sp); return PS_ERROR; } } else { MCExpression *newfact = NULL; switch (form = (Repeat_form)te->which) { case RF_FOREVER: break; case RF_FOR: if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_EACH) == PS_NORMAL) { if (sp.next(type) != PS_NORMAL || sp.lookup(SP_UNIT, te) != PS_NORMAL || sp.next(type) != PS_NORMAL) { MCperror->add(PE_REPEAT_BADCOND, sp); return PS_ERROR; } each = (File_unit)te->which; if (sp.lookupconstant(&newfact) == PS_NORMAL || sp . findnewvar(sp.gettoken_nameref(), kMCEmptyName, &loopvar) != PS_NORMAL) { delete newfact; MCperror->add(PE_REPEAT_BADWITHVAR, sp); return PS_ERROR; } if (sp.skip_token(SP_FACTOR, TT_IN) != PS_NORMAL) { MCperror->add(PE_REPEAT_NOOF, sp); return PS_ERROR; } } case RF_UNTIL: case RF_WHILE: if (sp.parseexp(False, True, &endcond) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADCOND, sp); return PS_ERROR; } break; case RF_WITH: if ((stat = sp.next(type)) != PS_NORMAL) { MCperror->add (PE_REPEAT_NOWITHVAR, sp); return PS_ERROR; } if (sp.lookupconstant(&newfact) == PS_NORMAL || sp.findnewvar(sp.gettoken_nameref(), kMCEmptyName, &loopvar) != PS_NORMAL) { delete newfact; MCperror->add (PE_REPEAT_BADWITHVAR, sp); return PS_ERROR; } if ((stat = sp.next(type)) != PS_NORMAL) { MCperror->add (PE_REPEAT_NOEQUALS, sp); return PS_ERROR; } if (sp.lookup(SP_FACTOR, te) != PS_NORMAL || te->type != TT_BINOP || te->which != O_EQ) { MCperror->add (PE_REPEAT_NOTEQUALS, sp); return PS_ERROR; } if (sp.parseexp(False, True, &startcond) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADWITHSTARTEXP, sp); return PS_ERROR; } if ((stat = sp.next(type)) != PS_NORMAL) { MCperror->add (PE_REPEAT_NOWITHTO, sp); return PS_ERROR; } if (sp.lookup(SP_FACTOR, te) != PS_NORMAL) { if (sp.gettoken() != "down") { MCperror->add (PE_REPEAT_NOWITHTO, sp); return PS_ERROR; } stepval = -1.0; if (sp.skip_token(SP_FACTOR, TT_TO) != PS_NORMAL) { MCperror->add (PE_REPEAT_NODOWNTO, sp); return PS_ERROR; } } else if (te->type != TT_TO) { MCperror->add (PE_REPEAT_NOTWITHTO, sp); return PS_ERROR; } else stepval = 1.0; if (sp.parseexp(False, True, &endcond) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADWITHENDEXP, sp); return PS_ERROR; } if (sp.skip_token(SP_REPEAT, TT_UNDEFINED, RF_STEP) == PS_NORMAL) if (sp.parseexp(False, True, &step) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADWITHSTARTEXP, sp); return PS_ERROR; } break; default: /* repeat form */ fprintf(stderr, "Repeat: ERROR bad control form\n"); return PS_ERROR; } } if (sp.skip_eol() != PS_NORMAL) { MCperror->add (PE_REPEAT_BADFORMEOL, sp); return PS_ERROR; } break; case PS_EOL: break; default: /* token type */ MCperror->add (PE_REPEAT_BADFORMTYPE, sp); return PS_ERROR; } MCStatement *curstatement = NULL; MCStatement *newstatement = NULL; while (True) { switch (sp.next(type)) { case PS_NORMAL: if (type == ST_DATA) newstatement = new MCEcho; else if (sp.lookup(SP_COMMAND, te) != PS_NORMAL) { if (type == ST_ID) newstatement = new MCComref(sp.gettoken_nameref()); else { MCperror->add (PE_REPEAT_NOTCOMMAND, sp); return PS_ERROR; } } else { switch (te->type) { case TT_STATEMENT: newstatement = MCN_new_statement(te->which); break; case TT_END: if (sp.skip_token(SP_COMMAND, TT_STATEMENT, S_REPEAT) != PS_NORMAL) { MCperror->add (PE_REPEAT_WANTEDENDREPEAT, sp); return PS_ERROR; } return PS_NORMAL; default: MCperror->add (PE_REPEAT_BADSTATEMENT, sp); return PS_ERROR; } } break; case PS_EOL: if (sp.skip_eol() != PS_NORMAL) { MCperror->add (PE_REPEAT_WANTEDENDREPEAT, sp); return PS_ERROR; } continue; default: MCperror->add (PE_REPEAT_BADTOKEN, sp); return PS_ERROR; } if (newstatement->parse(sp) != PS_NORMAL) { MCperror->add (PE_REPEAT_BADCOMMAND, sp); delete newstatement; return PS_ERROR; } if (statements == NULL) statements = curstatement = newstatement; else { curstatement->setnext(newstatement); curstatement = newstatement; } } return PS_NORMAL; }
Exec_stat MCRepeat::exec(MCExecPoint &ep) { real8 endn = 0.0; int4 count = 0; MCExecPoint ep2(ep); MCScriptPoint *sp = NULL; Parse_stat ps; const char *sptr; uint4 l; MCVariableValue *tvar = NULL; MCHashentry *hptr = NULL; uint4 kindex = 0; Boolean donumeric = False; Exec_stat stat; switch (form) { case RF_FOR: if (loopvar != NULL) { while ((stat = endcond->eval(ep2)) != ES_NORMAL && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep2, getline(), getpos(), EE_REPEAT_BADFORCOND); if (stat != ES_NORMAL) { MCeerror->add(EE_REPEAT_BADFORCOND, line, pos); return ES_ERROR; } if (each == FU_ELEMENT) { tvar = ep2.getarray(); if (tvar != NULL && tvar -> is_array()) { l = 1; kindex = 0; donumeric = tvar -> get_array() -> isnumeric(); hptr = tvar -> get_array() -> getnextelement(kindex, hptr, donumeric, ep); if (hptr == NULL) { kindex = 0; donumeric = False; hptr = tvar -> get_array() -> getnextelement(kindex, hptr, donumeric, ep); } } else l = 0; } else if (each == FU_KEY) { tvar = ep2.getarray(); if (tvar != NULL && tvar -> is_array()) { l = 1; kindex = 0; donumeric = False; hptr = tvar -> get_array() -> getnextkey(kindex, hptr); // [[ Bug 3871 ]] : If hptr is NULL then we are done already (this can happen // with an empty custom property set). if (hptr == NULL) l = 0; } else l = 0; } else { sptr = ep2.getsvalue().getstring(); l = ep2.getsvalue().getlength(); if (each == FU_WORD) MCU_skip_spaces(sptr, l); else if (each == FU_TOKEN) { sp = new MCScriptPoint(ep2); ps = sp->nexttoken(); if (ps == PS_ERROR || ps == PS_EOF) l = 0; } } } else { while (((stat = endcond->eval(ep)) != ES_NORMAL || (stat = ep.ton()) != ES_NORMAL) && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADFORCOND); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADFORCOND, line, pos); return ES_ERROR; } count = MCU_max(ep.getint4(), 0); } break; case RF_WITH: if (step != NULL) { while (((stat = step->eval(ep)) != ES_NORMAL || (stat = ep.ton()) != ES_NORMAL) && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHSTEP); stepval = ep.getnvalue(); if (stat != ES_NORMAL || stepval == 0.0) { MCeerror->add (EE_REPEAT_BADWITHSTEP, line, pos); return ES_ERROR; } } while (((stat = startcond->eval(ep)) != ES_NORMAL || (stat = ep.ton()) != ES_NORMAL) && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHSTART); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWITHSTART, line, pos); return ES_ERROR; } ep.setnvalue(ep.getnvalue() - stepval); while ((stat = loopvar->set (ep)) != ES_NORMAL && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHVAR); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWITHVAR, line, pos); return ES_ERROR; } while (((stat = endcond->eval(ep)) != ES_NORMAL || (stat = ep.ton()) != ES_NORMAL) && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHEND); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWITHEND, line, pos); return ES_ERROR; } endn = ep.getnvalue(); break; default: break; } MCString s; Boolean done = False; bool t_first; t_first = false; while (True) { switch (form) { case RF_FOREVER: break; case RF_FOR: if (loopvar != NULL) { if (l == 0) { done = True; // OK-2007-12-05 : Bug 5605. If there has been at least one iteration, set the loop variable to // whatever the value was in the last iteration. if (!t_first) { // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } } } else { const char *startptr; // = sptr; switch (each) { case FU_KEY: // MW-2010-12-15: [[ Bug 9218 ]] Make a copy of the key so that it can't be mutated // accidentally. ep . setstaticcstring(hptr -> string); loopvar -> set(ep); hptr = tvar -> get_array() -> getnextkey(kindex, hptr); if (hptr == NULL) l = 0; break; case FU_ELEMENT: hptr -> value . fetch(ep); loopvar -> set(ep); hptr = tvar -> get_array() -> getnextelement(kindex, hptr, donumeric, ep); if (hptr == NULL) l = 0; break; case FU_LINE: startptr = sptr; if (!MCU_strchr(sptr, l, ep.getlinedel())) { sptr += l; l = 0; } s.set(startptr, sptr - startptr); MCU_skip_char(sptr, l); break; case FU_ITEM: startptr = sptr; if (!MCU_strchr(sptr, l, ep.getitemdel())) { sptr += l; l = 0; } s.set(startptr, sptr - startptr); MCU_skip_char(sptr, l); break; case FU_WORD: startptr = sptr; if (*sptr == '\"') { MCU_skip_char(sptr, l); while (l && *sptr != '\"' && *sptr != '\n') MCU_skip_char(sptr, l); MCU_skip_char(sptr, l); } else while (l && !isspace((uint1)*sptr)) MCU_skip_char(sptr, l); s.set(startptr, sptr - startptr); MCU_skip_spaces(sptr, l); break; case FU_TOKEN: s = sp->gettoken(); ps = sp->nexttoken(); if (ps == PS_ERROR || ps == PS_EOF) l = 0; break; case FU_CHARACTER: default: startptr = sptr; s.set(startptr, 1); MCU_skip_char(sptr, l); } // MW-2010-12-15: [[ Bug 9218 ]] Added KEY to the type of repeat that already // copies the value. if (each != FU_ELEMENT && each != FU_KEY) { if (MCtrace) { ep.setsvalue(s); loopvar->set(ep); } else loopvar->sets(s); } } } else done = count-- == 0; break; case RF_UNTIL: while ((stat = endcond->eval(ep)) != ES_NORMAL && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADUNTILCOND); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADUNTILCOND, line, pos); return ES_ERROR; } done = ep.getsvalue() == MCtruemcstring; break; case RF_WHILE: while ((stat = endcond->eval(ep)) != ES_NORMAL && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWHILECOND); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWHILECOND, line, pos); return ES_ERROR; } done = ep.getsvalue() != MCtruemcstring; break; case RF_WITH: while (((stat = loopvar->eval(ep)) != ES_NORMAL || (stat = ep.ton()) != ES_NORMAL) && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHVAR); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWITHVAR, line, pos); return ES_ERROR; } if (stepval < 0) { if (ep.getnvalue() <= endn) done = True; } else if (ep.getnvalue() >= endn) done = True; if (!done) { ep.setnvalue(ep.getnvalue() + stepval); while ((stat = loopvar->set (ep)) != ES_NORMAL && (MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) MCB_error(ep, getline(), getpos(), EE_REPEAT_BADWITHVAR); if (stat != ES_NORMAL) { MCeerror->add (EE_REPEAT_BADWITHVAR, line, pos); return ES_ERROR; } } break; default: break; } if (done) break; Exec_stat stat; MCStatement *tspr = statements; while (tspr != NULL) { if (MCtrace || MCnbreakpoints) { MCB_trace(ep, tspr->getline(), tspr->getpos()); if (MCexitall) break; } ep.setline(tspr->getline()); stat = tspr->exec(ep); // MW-2011-08-17: [[ Redraw ]] Flush any screen updates. MCRedrawUpdateScreen(); switch(stat) { case ES_NORMAL: if (MCexitall) { // OK-2007-12-05 : Bug 5605 : If exiting loop, set the loop variable to the value it had // in the last iteration. // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (form == RF_FOR && loopvar != NULL && each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } delete sp; return ES_NORMAL; } tspr = tspr->getnext(); break; case ES_NEXT_REPEAT: tspr = NULL; break; case ES_EXIT_REPEAT: // OK-2007-12-05 : Bug 5605 : If exiting loop, set the loop variable to the value it had // in the last iteration. // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (form == RF_FOR && loopvar != NULL && each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } delete sp; return ES_NORMAL; case ES_ERROR: if ((MCtrace || MCnbreakpoints) && !MCtrylock && !MClockerrors) do { MCB_error(ep, tspr->getline(), tspr->getpos(), EE_REPEAT_BADSTATEMENT); } while (MCtrace && (stat = tspr->exec(ep)) != ES_NORMAL); if (stat == ES_ERROR) { // OK-2007-12-05 : Bug 5605 : If exiting loop, set the loop variable to the value it had // in the last iteration. // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (form == RF_FOR && loopvar != NULL && each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } delete sp; if (MCexitall) return ES_NORMAL; else { MCeerror->add(EE_REPEAT_BADSTATEMENT, line, pos); return ES_ERROR; } } else tspr = tspr->getnext(); break; default: // OK-2007-12-05 : Bug 5605 : If exiting loop, set the loop variable to the value it had // in the last iteration. // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (form == RF_FOR && loopvar != NULL && each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } delete sp; return stat; } } if (MCscreen->abortkey()) { // OK-2007-12-05 : Bug 5605 : If exiting loop, set the loop variable to the value it had // in the last iteration. // MW-2011-02-08: [[ Bug ]] Make sure we don't use 's' if the repeat type is 'key' or // 'element'. if (form == RF_FOR && loopvar != NULL && each != FU_ELEMENT && each != FU_KEY) { ep.setsvalue(s); loopvar->set(ep); } delete sp; MCeerror->add(EE_REPEAT_ABORT, line, pos); return ES_ERROR; } if (MCtrace || MCnbreakpoints) { MCB_trace(ep, getline(), getpos()); if (MCexitall) break; } t_first = false; } delete sp; return ES_NORMAL; }
void MCError::add(uint2 id, MCScriptPoint &sp) { add(id, sp.getline(), sp.getpos(), sp.gettoken()); }