|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080 |
- /***************************************************************
-
- bwb_cnd.c Conditional Expressions and Commands
- for Bywater BASIC Interpreter
-
- Copyright (c) 1993, Ted A. Campbell
- Bywater Software
-
- email: tcamp@delphi.com
-
- Copyright and Permissions Information:
-
- All U.S. and international rights are claimed by the author,
- Ted A. Campbell.
-
- This software is released under the terms of the GNU General
- Public License (GPL), which is distributed with this software
- in the file "COPYING". The GPL specifies the terms under
- which users may copy and use the software in this distribution.
-
- A separate license is available for commercial distribution,
- for information on which you should contact the author.
-
- ***************************************************************/
-
- /*---------------------------------------------------------------*/
- /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
- /* 11/1995 (eidetics@cerf.net). */
- /* */
- /* Those additionally marked with "DD" were at the suggestion of */
- /* Dale DePriest (daled@cadence.com). */
- /* */
- /* Version 3.00 by Howard Wulf, AF5NE */
- /* */
- /*---------------------------------------------------------------*/
-
-
-
- #include "bwbasic.h"
-
-
-
- /* declarations of functions visible to this file only */
-
- static int
- cnd_thenels(char *buffer, int position, int *then, int *els);
- static int
- cnd_tostep(char *buffer, int position, int *to, int *step);
- static int
- case_eval(struct exp_ese * expression, struct exp_ese * minval, struct exp_ese * maxval);
- static int
- FindTopLineOnStack(struct bwb_line * l);
- static int
- FindBottomLineOnStack(struct bwb_line * l);
- static struct bwb_line *
- FindExitLineOnStack(struct bwb_line * l);
- static struct bwb_line *
- find_NextTestInCode(struct bwb_line * l);
-
-
- /* FIXME: DELETE should remove DEF FN, FUNCTIONS, and SUBROUTINES in the deleted line range. */
- /* FIXME: add Variable Range checking, based upon type characters $, @, %, &, !, # */
- /* FIXME: check EOF, LOF, LOC, SEEK, such as QB45 */
- /* FIXME: centralize Variable/Function/Command name character checking, so we can configure it with OPTION VERSION */
-
-
- /*
- --------------------------------------------------------------------------------------------
- FUNCTION - END FUNCTION
- --------------------------------------------------------------------------------------------
- */
-
-
- struct bwb_line *
- bwb_FUNCTION(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* check current exec level */
- if (CURTASK exsc == 0)
- {
- /* skip over the entire function definition */
- return l->OtherLine->next; /* line after END SUB */
- }
- /* we are being executed via fnc_deffn() */
-
- /* if this is the first time at this SUB statement, note it */
-
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the END SUB statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);;
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("FUNCTION without END FUNCTION");
- return bwb_zline(l);
- }
- }
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
-
-
- struct bwb_line *
- bwb_EXIT_FUNCTION(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT FUNCTION without FUNCTION");
- return bwb_zline(l);
- }
- {
- /* EXIT FUNCTION */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_FUNCTION);
- return next_line;
- #endif
- }
-
-
- struct bwb_line *
- bwb_END_FUNCTION(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* check integrity of SUB commmand */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("END FUNCTION without FUNCTION");
- return bwb_zline(l);
- }
- /* decrement the stack */
- bwb_decexec();
-
- /* and return next from old line */
- CURTASK excs[CURTASK exsc].line->next->position = 0;
- return CURTASK excs[CURTASK exsc].line->next;
- }
-
-
-
- /*
- --------------------------------------------------------------------------------------------
- SUB - END SUB
- --------------------------------------------------------------------------------------------
- */
-
- /***************************************************************
-
- FUNCTION: bwb_sub()
-
- DESCRIPTION: This function implements the BASIC
- SUB command, introducing a named
- subroutine.
-
- SYNTAX: SUB subroutine-name
- ...
- [ EXIT SUB ]
- ...
- END SUB
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_SUB(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* check current exec level */
- if (CURTASK exsc == 0)
- {
- /* skip over the entire function definition */
- return l->OtherLine->next; /* line after END SUB */
- }
- /* we are being executed via fnc_deffn() */
-
- /* if this is the first time at this SUB statement, note it */
-
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the END SUB statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);;
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("SUB without END SUB");
- return bwb_zline(l);
- }
- }
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_endsub()
-
- DESCRIPTION: This C function implements the BASIC
- END SUB command, ending a subroutine
- definition. Because the command END
- can have multiple meanings, this function
- should be called from the bwb_xend()
- function, which should be able to identify
- an END SUB command.
-
- SYNTAX: END SUB
-
- ***************************************************************/
- struct bwb_line *
- bwb_EXIT_SUB(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT SUB without SUB");
- return bwb_zline(l);
- }
- {
- /* EXIT SUB */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_FUNCTION);
- return next_line;
- #endif
- }
-
- struct bwb_line *
- bwb_END_SUB(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* check integrity of SUB commmand */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("END SUB without SUB");
- return bwb_zline(l);
- }
- /* decrement the stack */
- bwb_decexec();
-
- /* and return next from old line */
- CURTASK excs[CURTASK exsc].line->next->position = 0;
- return CURTASK excs[CURTASK exsc].line->next;
- }
-
-
-
- /*
- --------------------------------------------------------------------------------------------
- IF - END IF
- --------------------------------------------------------------------------------------------
- */
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_IF()
-
- DESCRIPTION: This function handles the BASIC IF
- statement, standard flavor.
-
- SYNTAX: IF expression THEN line [ELSE line]
-
- ***************************************************************/
- struct bwb_line *
- bwb_IF(struct bwb_line * l)
- {
- /* classic IF */
- int then, els;
- struct exp_ese *e;
- int tpos;
- char tbuf[BasicStringLengthMax + 1];
- int Value;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* Call bwb_exp() to evaluate the condition. This should return with
- * position set to the "THEN" statement */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- Value = exp_getival(e);
-
-
- /* test for "THEN" and "ELSE" statements */
-
- cnd_thenels(l->buffer, l->position, &then, &els);
-
-
- /* evaluate and execute */
-
- if (Value != 0)
- {
- /* expression is TRUE */
- if (then == FALSE)
- {
- /* syntac error */
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- else
- {
- /* check for THEN followed by literal line number */
- struct bwb_line *x;
-
- tpos = then + strlen("THEN") + 1;
- adv_element(l->buffer, &tpos, tbuf);
-
- /* check for target label */
- x = find_label(tbuf);
- if (x != NULL)
- {
- adv_eos(l->buffer, &(l->position));
- x->position = 0;
- return x;
- }
- /* syntac error */
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
-
- }
- else
- {
- /* expression is FALSE */
-
- if (els == FALSE)
- {
- /* optional */
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
- else
- {
- /* check for ELSE followed by literal line number */
- struct bwb_line *x;
-
- tpos = els + strlen("ELSE") + 1;
- adv_element(l->buffer, &tpos, tbuf);
-
- /* check for target label */
- x = find_label(tbuf);
- if (x != NULL)
- {
- adv_eos(l->buffer, &(l->position));
- x->position = 0;
- return x;
- }
- /* syntac error */
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- }
-
- return bwb_zline(l);
-
- }
-
- /***************************************************************
-
- FUNCTION: cnd_thenelse()
-
- DESCRIPTION: This function searches through the
- <buffer> beginning at point <position>
- and attempts to find positions of THEN
- and ELSE statements.
-
- ***************************************************************/
-
- static int
- cnd_thenels(char *buffer, int position, int *then, int *els)
- {
- int loop, t_pos, b_pos, p_word;
- char tbuf[BasicStringLengthMax + 1];
-
- bwx_DEBUG(__FUNCTION__);
-
- /* set then and els to 0 initially */
-
- *then = *els = 0;
-
- /* loop to find words */
-
- p_word = b_pos = position;
- t_pos = 0;
- tbuf[0] = '\0';
- loop = TRUE;
- while (loop == TRUE)
- {
- char c;
- c = buffer[b_pos];
-
- if (c == '\0' || c == OptionCommentChar || c == ' ')
- {
-
- if (strncasecmp(tbuf, "THEN", (size_t) strlen("THEN")) == 0)
- {
- *then = p_word;
- }
- else
- if (strncasecmp(tbuf, "GOTO", (size_t) strlen("GOTO")) == 0)
- {
-
- *then = p_word;
- }
- else
- if (strncasecmp(tbuf, "ELSE", (size_t) strlen("ELSE")) == 0)
- {
-
- *els = p_word;
- }
- /* check for end of the line */
-
- if (c == '\0' || c == OptionCommentChar)
- {
- return TRUE;
- }
- ++b_pos;
- p_word = b_pos;
- t_pos = 0;
- tbuf[0] = '\0';
- }
- else
- {
- tbuf[t_pos] = c;
- ++b_pos;
- ++t_pos;
- tbuf[t_pos] = '\0';
- }
-
- }
-
- return FALSE;
-
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_IF_THEN()
-
- DESCRIPTION: This function handles the BASIC IF
- statement, structured flavor.
-
- SYNTAX: IF expression THEN
- ...
- END IF
-
- ***************************************************************/
- struct bwb_line *
- bwb_IF_THEN(struct bwb_line * l)
- {
- /* structured IF */
- struct exp_ese *e;
- struct bwb_line *else_line;
- struct bwb_line *endif_line;
- int Value;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* if this is the first time at this IF statement, note it */
-
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the LOOP statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);;
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("IF without END IF");
- return bwb_zline(l);
- }
- }
- /* Call bwb_exp() to evaluate the condition. This should return with
- * position set to the "THEN" statement */
-
- /* FIXME: extract the expression before calling bwb_exp() - no COMMANDS should be there */
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- Value = exp_getival(e);
-
-
- /* evaluate the expression */
-
- if (Value != 0)
- {
- CURTASK excs[CURTASK exsc].code = EXEC_IFTRUE; /* IF has been processed */
- /* fall thru to execute this code block */
-
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
- CURTASK excs[CURTASK exsc].code = EXEC_IFFALSE; /* IF has NOT been
- * processed */
-
- else_line = find_NextTestInCode(l);
- if (else_line != NULL)
- {
- else_line->position = 0;
- return else_line;
- }
- endif_line = CURTASK excs[CURTASK exsc].LoopBottomLine;
- endif_line->position = 0;
- return endif_line;
-
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_else()
-
- DESCRIPTION: This function handles the BASIC ELSE
- statement.
-
- SYNTAX: ELSE
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_ELSE(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* there are two legitamate ways to get here: 1. a fall thru from an
- * upper block, code == EXEC_IFTRUE we should jump to the END IF 2. a
- * jump from from an upper block, code == EXEC_IFFALSE we should
- * execute our code block */
-
- next_line = FindExitLineOnStack(l); /* END IF */
- if (next_line == NULL)
- {
- bwb_error("ELSE without IF");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_IFTRUE: /* IF has been processed */
- /* jump to the END IF */
- next_line->position = 0;
- return next_line;
- break;
- case EXEC_IFFALSE:
- CURTASK excs[CURTASK exsc].code = EXEC_IFTRUE; /* IF has been processed */
- /* fall thru to execute this code block */
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("ELSE without IF");
- break;
- }
- return bwb_zline(l);
- }
-
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_elseif()
-
- DESCRIPTION: This function handles the BASIC ELSEIF
- statement.
-
- SYNTAX: ELSEIF
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_ELSEIF(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
- /* there are two legitamate ways to get here: 1. a fall thru from an
- * upper block, code == EXEC_IFTRUE we should jump to the END IF 2. a
- * jump from from an upper block, code == EXEC_IFFALSE we should
- * execute our code block */
-
- next_line = FindExitLineOnStack(l); /* END IF */
- if (next_line == NULL)
- {
- bwb_error("ELSEIF without IF");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_IFTRUE: /* IF has been processed */
- /* jump to the END IF */
- next_line->position = 0;
- return next_line;
- break;
- case EXEC_IFFALSE:
- /* execute our code block (maybe) */
- {
- struct bwb_line *else_line;
- struct exp_ese *e;
- /* Call bwb_exp() to evaluate the condition. This
- * should return with position set to the "THEN"
- * statement */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) != FALSE)
- {
- /* condition is TRUE: proceed to the next
- * line */
- CURTASK excs[CURTASK exsc].code = EXEC_IFTRUE; /* IF has been processed */
- /* fall thru to execute this code block */
-
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
-
- }
- /* condition is FALSE */
-
- /* proceed to next ELSE line if there is one */
- else_line = find_NextTestInCode(l); /* ELSEIF or ELSE */
- if (else_line != NULL)
- {
- else_line->position = 0;
- return else_line;
- }
- /* no more ELSExx lines */
- /* jump to the END IF */
- next_line->position = 0;
- return next_line;
- }
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("ELSEIF without IF");
- break;
- }
- return bwb_zline(l);
- }
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_endif()
-
- DESCRIPTION: This function handles the BASIC END IF
- statement.
-
- SYNTAX: END IF
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_END_IF(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* check integrity of IF commmand */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("END IF without IF");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_IFTRUE: /* IF has been processed */
- break;
- case EXEC_IFFALSE:
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("ENDIF without IF");
- }
-
- /* remove IF from the stack (NEW) */
- bwb_decexec();
- bwb_setexec(l->next, 0, CURTASK excs[CURTASK exsc].code);
-
- return bwb_zline(l);
- }
-
- /*
- --------------------------------------------------------------------------------------------
- SELECT CASE - END SELECT
- --------------------------------------------------------------------------------------------
- */
-
-
- /***************************************************************
-
- FUNCTION: bwb_select()
-
- DESCRIPTION: This C function handles the BASIC SELECT
- statement.
-
- SYNTAX: SELECT CASE expression
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_SELECT(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
- bwb_error("SELECT without CASE");
- return bwb_zline(l);
- }
-
-
- struct bwb_line *
- bwb_SELECT_CASE(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* if this is the first time at this SELECT CASE statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the LOOP statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("SELECT CASE without END SELECT");
- return bwb_zline(l);
- }
- }
- /* increment the level and set to EXEC_SELFALSE */
-
- CURTASK excs[CURTASK exsc].code = EXEC_SELFALSE;
-
-
- /* evaluate the expression at this level */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (e->type == STRING)
- {
- CURTASK excs[CURTASK exsc].expression.type = STRING;
- str_btob(&(CURTASK excs[CURTASK exsc].expression.sval),
- &(e->sval));
- }
- else
- {
- CURTASK excs[CURTASK exsc].expression.type = NUMBER;
- CURTASK excs[CURTASK exsc].expression.nval
- = exp_getnval(e);
- }
-
- /* return */
-
- adv_eos(l->buffer, &(l->position));
-
- return bwb_zline(l);
- }
-
- /***************************************************************
-
- FUNCTION: bwb_case()
-
- DESCRIPTION: This C function handles the BASIC CASE
- statement.
-
- SYNTAX: CASE constant | IF partial-expression | ELSE
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_CASE_ELSE(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
- /* there are two legitamate ways to get here: 1. a fall thru from an
- * upper block, code == EXEC_SELTRUE we should jump to the END SELECT
- * 2. a jump from from an upper block, code == EXEC_SELFALSE we
- * should execute our code block */
-
- next_line = FindExitLineOnStack(l); /* END SELECT */
- if (next_line == NULL)
- {
- bwb_error("CASE ELSE without SELECT CASE");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_SELTRUE: /* SELECT has been processed */
- /* jump to the END SELECT */
- next_line->position = 0;
- return next_line;
- break;
- case EXEC_SELFALSE:
- CURTASK excs[CURTASK exsc].code = EXEC_SELTRUE; /* SELECT has been
- * processed */
- /* fall thru to execute this code block */
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("CASE ELSE without SELECT CASE");
- break;
- }
- return bwb_zline(l);
- }
-
-
-
- struct bwb_line *
- bwb_CASE(struct bwb_line * l) /* TODO - FIXME */
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* there are two legitamate ways to get here: 1. a fall thru from an
- * upper block, code == EXEC_SELTRUE we should jump to the END SELECT
- * 2. a jump from from an upper block, code == EXEC_SELFALSE we
- * should execute our code block */
-
- next_line = FindExitLineOnStack(l); /* END SELECT */
- if (next_line == NULL)
- {
- bwb_error("CASE without SELECT CASE");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_SELTRUE: /* SELECT has been processed */
- /* jump to the END SELECT */
- next_line->position = 0;
- return next_line;
- break;
- case EXEC_SELFALSE:
- /* fall thru to execute this code block (maybe) */
- {
- struct exp_ese minvalue;
- struct exp_ese *minval;
- struct bwb_line *case_line;
- int IsExpression;
-
- IsExpression = FALSE;
-
- minval = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- memcpy(&minvalue, minval, sizeof(struct exp_ese));
- minval = &minvalue;
-
- /* check for string value */
-
- if (minvalue.type == STRING)
- { /* STRING */
- bstring *a;
- bstring *b;
- a = &(CURTASK excs[CURTASK exsc].expression.sval);
- b = &(minvalue.sval);
- if (str_cmp(a, b) == 0)
- {
- IsExpression = TRUE;
- }
- }
- /* STRING */
- else
- { /* NUMBER */
- struct exp_ese *maxval;
- maxval = minval;
-
- /* not a string; advance */
-
- adv_ws(l->buffer, &(l->position));
-
- /* check for TO */
-
- if (is_eol(l->buffer, &(l->position)) != TRUE)
- {
- char tbuf[BasicStringLengthMax + 1];
-
- /* find the TO statement */
-
- adv_element(l->buffer, &(l->position), tbuf);
- if (strcasecmp(tbuf, "TO") != 0)
- {
- sprintf(bwb_ebuf, "CASE has inexplicable code following expression");
- bwb_error(bwb_ebuf);
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
- /* now evaluate the MAX expression */
-
- maxval = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- }
- /* evaluate the expression */
-
- if (case_eval(&(CURTASK excs[CURTASK exsc].expression), minval, maxval) == TRUE)
- {
- IsExpression = TRUE;
- }
- } /* NUMBER */
-
- if (IsExpression == TRUE)
- {
- /* condition is TRUE: proceed to the next
- * line */
- CURTASK excs[CURTASK exsc].code = EXEC_SELTRUE; /* SELECT has been
- * processed */
- /* fall thru to execute this code block */
-
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
- /* evaluation returns a FALSE value; find next CASExx
- * statement */
- case_line = find_NextTestInCode(l); /* CASE or CASE IF or
- * CASE ELSE */
- if (case_line != NULL)
- {
- case_line->position = 0;
- return case_line;
- }
- /* no more CASExx lines */
- /* jump to the END SELECT */
- next_line->position = 0;
- return next_line;
- }
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("CASE without SELECT CASE");
- break;
- }
- return bwb_zline(l);
- }
-
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_caseif()
-
- DESCRIPTION: This C function handles the BASIC CASE IF
- statement.
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_CASE_IS(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
- return bwb_CASE_IF(l);
- }
-
- struct bwb_line *
- bwb_CASE_IF(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- /* there are two legitamate ways to get here: 1. a fall thru from an
- * upper block, code == EXEC_SELTRUE we should jump to the END SELECT
- * 2. a jump from from an upper block, code == EXEC_SELFALSE we
- * should execute our code block */
-
- next_line = FindExitLineOnStack(l); /* END SELECT */
- if (next_line == NULL)
- {
- bwb_error("CASE IF without SELECT CASE");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_SELTRUE: /* IF has been processed */
- /* jump to the END IF */
- next_line->position = 0;
- return next_line;
- break;
- case EXEC_SELFALSE:
- /* execute our code block (maybe) */
- {
- char tbuf[BasicStringLengthMax + 1];
- int position;
- struct exp_ese *r;
- struct bwb_line *case_line;
-
- if (CURTASK excs[CURTASK exsc].expression.type == NUMBER)
- {
- sprintf(tbuf, BasicNumberPrintFormat " %s",
- CURTASK excs[CURTASK exsc].expression.nval,
- &(l->buffer[l->position]));
- }
- else
- {
- bwb_error(err_mismatch);
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
-
- position = 0;
- r = bwb_exp(tbuf, FALSE, &position);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (r->nval == TRUE)
- {
- /* condition is TRUE: proceed to the next
- * line */
- CURTASK excs[CURTASK exsc].code = EXEC_SELTRUE; /* SELECT has been
- * processed */
- /* fall thru to execute this code block */
-
- adv_eos(l->buffer, &(l->position));
- return bwb_zline(l);
- }
- /* condition is FALSE */
-
- /* proceed to next CASE line if there is one */
- case_line = find_NextTestInCode(l); /* CASE IF or CASE ELSE */
- if (case_line != NULL)
- {
- case_line->position = 0;
- return case_line;
- }
- /* no more CASExx lines */
- /* jump to the END SELECT */
- next_line->position = 0;
- return next_line;
- }
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("CASE IF without SELECT CASE");
- break;
- }
- return bwb_zline(l);
- }
-
-
-
- /***************************************************************
-
- FUNCTION: case_eval()
-
- DESCRIPTION: This function evaluates a case statement
- by comparing minimum and maximum values
- with a set expression. It returns either
- TRUE or FALSE
-
- ***************************************************************/
-
- static int
- case_eval(struct exp_ese * expression, struct exp_ese * minval,
- struct exp_ese * maxval)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* string value */
-
- if (expression->type == STRING)
- {
- bwb_error(err_mismatch);
- return FALSE;
- }
- /* numerical value */
-
-
- if ((expression->nval >= minval->nval)
- && (expression->nval <= maxval->nval))
- {
- return TRUE;
- }
- return FALSE;
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_endselect()
-
- DESCRIPTION: This function handles the BASIC END
- SELECT statement.
-
- SYNTAX: END SELECT
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_END_SELECT(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
-
- /* check integrity of SELECT CASE commmand */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("END SELECT without SELECT CASE");
- return bwb_zline(l);
- }
- switch (CURTASK excs[CURTASK exsc].code)
- {
- case EXEC_SELTRUE: /* SELECT has been processed */
- break;
- case EXEC_SELFALSE:
- break;
- default:
- /* the BASIC program has jumped into the middle of a
- * structured command */
- bwb_error("END SELECT without SELECT CASE");
- }
-
-
- /* remove SELECT from the stack */
- bwb_decexec();
- bwb_setexec(l->next, 0, CURTASK excs[CURTASK exsc].code);
-
-
- return bwb_zline(l);
- }
-
- /*
- --------------------------------------------------------------------------------------------
- DO - LOOP
- --------------------------------------------------------------------------------------------
- */
-
- /***************************************************************
-
- FUNCTION: bwb_DO()
-
- DESCRIPTION: This C function implements the ANSI BASIC
- DO statement, when DO is not followed by
- an argument. It is called by bwb_do() in
- bwb_cmd.c.
-
- SYNTAX: DO ' forever
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_DO(struct bwb_line * l)
- {
- bwx_DEBUG(__FUNCTION__);
-
- /* if this is the first time at this DO statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the LOOP statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);;
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("DO without LOOP");
- return bwb_zline(l);
- }
- }
- bwb_setexec(l, l->position, EXEC_DO);
- return bwb_zline(l);
- }
-
- /***************************************************************
-
- FUNCTION: bwb_DO_UNTIL()
-
- DESCRIPTION: This C function implements the ANSI BASIC
- DO UNTIL statement, when DO is not followed by
- an argument. It is called by bwb_do() in
- bwb_cmd.c.
-
- SYNTAX: DO UNTIL expression ' exit when X <> 0
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_DO_UNTIL(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
- /* if this is the first time at this WHILE statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the UEND statement (or LOOP statement) */
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("DO without LOOP");
- return bwb_zline(l);
- }
- }
- /*----------------------------------------------------*/
- /* Expression evaluation was at the top of bwb_while, */
- /* and the init portion was performed only if TRUE. */
- /* The init routine should be performed regardless of */
- /* expression value, else a segmentation fault can */
- /* occur! (JBV) */
- /*----------------------------------------------------*/
-
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) != FALSE)
- {
- /* EXIT DO */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* DO UNTIL ... */
- bwb_setexec(l, l->position, EXEC_DO);
- return bwb_zline(l);
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_DO_WHILE()
-
- DESCRIPTION: This C function implements the ANSI BASIC
- DO statement, when DO is not followed by
- an argument. It is called by bwb_do() in
- bwb_cmd.c.
-
- SYNTAX: DO WHILE expression ' exit when X = 0
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_DO_WHILE(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
- /* if this is the first time at this WHILE statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the LOOP statement */
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("DO without LOOP");
- return bwb_zline(l);
- }
- }
- /*----------------------------------------------------*/
- /* Expression evaluation was at the top of bwb_while, */
- /* and the init portion was performed only if TRUE. */
- /* The init routine should be performed regardless of */
- /* expression value, else a segmentation fault can */
- /* occur! (JBV) */
- /*----------------------------------------------------*/
-
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) == FALSE)
- {
- /* EXIT DO */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* DO WHILE ... */
- bwb_setexec(l, l->position, EXEC_DO);
- return bwb_zline(l);
-
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_EXIT_DO()
-
- DESCRIPTION: This function handles the BASIC EXIT
- DO statement. This is a structured
- programming command compatible with ANSI
- BASIC. It is called from the bwb_exit()
- subroutine.
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_EXIT_DO(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
-
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT DO without DO");
- return bwb_zline(l);
- }
- {
- /* EXIT DO */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- next_line = next_line->next;
-
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_DO);
- return next_line;
- #endif
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_LOOP()
-
- DESCRIPTION: This C function implements the ANSI BASIC
- LOOP statement.
-
- SYNTAX: LOOP ' forever
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_LOOP(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
-
- /* check integrity of DO loop */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("LOOP without DO");
- return bwb_zline(l);
- }
- /* reset to the top of the current DO loop */
-
-
- CURTASK excs[CURTASK exsc].LoopTopLine->position = 0;
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine, 0, EXEC_DO);
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
- }
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_LOOP_UNTIL()
-
- DESCRIPTION: This C function implements the ANSI BASIC
- LOOP UNTIL statement and is called by
- bwb_loop().
-
- SYNTAX: LOOP UNTIL expression ' exit when X <> 0
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_LOOP_UNTIL(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("LOOP without DO");
- return bwb_zline(l);
- }
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) != FALSE)
- {
- /* EXIT DO */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* loop around to DO again */
-
-
- CURTASK excs[CURTASK exsc].LoopTopLine->position = 0;
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine, 0, EXEC_DO);
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_LOOP_WHILE()
-
- DESCRIPTION: This C function implements the BASIC
- LOOP WHILE statement and is called by
- bwb_loop().
-
- SYNTAX: LOOP WHILE expression ' exit when X = 0
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_LOOP_WHILE(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("LOOP without DO");
- return bwb_zline(l);
- }
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) == FALSE)
- {
- /* EXIT DO */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* execute DO-LOOP again */
-
-
- CURTASK excs[CURTASK exsc].LoopTopLine->position = 0;
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine, 0, EXEC_DO);
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
- }
-
-
-
- /*
- --------------------------------------------------------------------------------------------
- WHILE - WEND
- --------------------------------------------------------------------------------------------
- */
-
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_WHILE()
-
- DESCRIPTION: This function handles the BASIC
- WHILE statement.
-
- SYNTAX: WHILE expression
- ...
- WEND
- SYNTAX: WHILE expression ' exit when X = 0
-
-
- ***************************************************************/
- struct bwb_line *
- bwb_WHILE(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
- /* if this is the first time at this WHILE statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the LOOP statement */
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("WHILE without WEND");
- return bwb_zline(l);
- }
- }
- /*----------------------------------------------------*/
- /* Expression evaluation was at the top of bwb_while, */
- /* and the init portion was performed only if TRUE. */
- /* The init routine should be performed regardless of */
- /* expression value, else a segmentation fault can */
- /* occur! (JBV) */
- /*----------------------------------------------------*/
-
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) == FALSE)
- {
- /* EXIT WHILE */
- struct bwb_line *r;
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* WHILE ... */
- bwb_setexec(l, l->position, EXEC_WHILE);
- return bwb_zline(l);
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_exitwhile()
-
- DESCRIPTION: This function handles the BASIC EXIT
- WHILE statement. This is a structured
- programming command compatible with ANSI
- BASIC. It is called from the bwb_exit()
- subroutine.
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_EXIT_WHILE(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
- /* Check the integrity of the WHILE statement */
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT WHILE without WHILE");
- return bwb_zline(l);
- }
- {
- /* EXIT WHILE */
- struct bwb_line *r;
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- next_line = next_line->next;
-
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_WHILE);
- return next_line;
- #endif
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_wend()
-
- DESCRIPTION: This function handles the BASIC WEND
- statement and the LOOP statement ending
- a DO WHILE loop.
-
- SYNTAX: WEND
- LOOP
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_WEND(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
- /* check integrity of WHILE loop */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("WEND without WHILE");
- return bwb_zline(l);
- }
- /* reset to the top of the current WHILE loop */
-
-
- CURTASK excs[CURTASK exsc].LoopTopLine->position = 0;
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine, 0, EXEC_WHILE);
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
- }
-
-
- /*
- --------------------------------------------------------------------------------------------
- UNTIL - UEND
- --------------------------------------------------------------------------------------------
- */
-
-
-
-
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_UNTIL()
-
- DESCRIPTION: This function handles the BASIC
- UNTIL statement.
-
- SYNTAX: UNTIL expression ' exit when X <> 0
- ...
- UEND
-
-
- ***************************************************************/
- struct bwb_line *
- bwb_UNTIL(struct bwb_line * l)
- {
- struct exp_ese *e;
-
- bwx_DEBUG(__FUNCTION__);
- /* if this is the first time at this WHILE statement, note it */
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the UEND statement (or LOOP statement) */
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("UNTIL without UEND");
- return bwb_zline(l);
- }
- }
- /*----------------------------------------------------*/
- /* Expression evaluation was at the top of bwb_while, */
- /* and the init portion was performed only if TRUE. */
- /* The init routine should be performed regardless of */
- /* expression value, else a segmentation fault can */
- /* occur! (JBV) */
- /*----------------------------------------------------*/
-
- /* call bwb_exp() to interpret the expression */
-
- e = bwb_exp(l->buffer, FALSE, &(l->position));
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- if (exp_getival(e) != FALSE)
- {
- /* EXIT UNTIL */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- /* UNTIL ... */
- bwb_setexec(l, l->position, EXEC_UNTIL);
- return bwb_zline(l);
-
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_exituntil()
-
- DESCRIPTION: This function handles the BASIC EXIT
- UNTIL statement. This is a structured
- programming command compatible with ANSI
- BASIC. It is called from the bwb_exit()
- subroutine.
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_EXIT_UNTIL(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
-
- /* Check the integrity of the UNTIL statement */
-
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT UNTIL without UNTIL");
- return bwb_zline(l);
- }
- {
- /* EXIT UNTIL */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- next_line = next_line->next;
-
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_UNTIL);
- return next_line;
- #endif
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_uend()
-
- DESCRIPTION: This function handles the BASIC UEND
- statement and the LOOP statement ending
- a DO UNTIL loop.
-
- SYNTAX: UEND
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_UEND(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
- /* check integrity of UNTIL loop */
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("UEND without UNTIL");
- return bwb_zline(l);
- }
- /* reset to the top of the current UNTIL loop */
-
- CURTASK excs[CURTASK exsc].LoopTopLine->position = 0;
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine, 0, EXEC_UNTIL);
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
- }
-
-
- /*
- --------------------------------------------------------------------------------------------
- FOR - NEXT
- --------------------------------------------------------------------------------------------
- */
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_for()
-
- DESCRIPTION: This function handles the BASIC FOR
- statement.
-
- SYNTAX: FOR counter = start TO finish [STEP increment]
-
- NOTE: This is controlled by the OptionVersion bitmask.
-
- The order of expression evaluation and variable creation varies.
- For example:
- FUNCTION FNA( Y )
- PRINT "Y="; Y
- FNA = Y
- END FUNCTION
- FOR X = FNA(3) TO FNA(1) STEP FNA(2)
- NEXT X
- ANSI/ECMA;
- Y= 1
- Y= 2
- Y= 3
- X is created (if it does not exist)
- X is assigned the value of 3
- MICROSOFT;
- X is created (if it does not exist)
- Y= 3
- X is assigned the value of 3
- Y= 1
- Y= 2
-
-
- ECMA-55: Section 13.4
- ...
- The action of the for-statement and the next-statement is de-
- fined in terms of other statements, as follows:
-
- FOR v = initial-value TO limit STEP increment
- (block)
- NEXT v
-
- is equivalent to:
-
- LET own1 = limit
- LET own2 = increment
- LET v = initial-value
- line1 IF (v-own1) * SGN (own2) > 0 THEN line2
- (block)
- LET v = v + own2
- GOTO line1
- line2 REM continued in sequence
- ...
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_FOR(struct bwb_line * l)
- {
-
- bwx_DEBUG(__FUNCTION__);
-
- #if 0
- if (CURTASK excs[CURTASK exsc].LoopTopLine != l)
- #endif
- if (FindTopLineOnStack(l) == FALSE)
- {
-
- bwb_incexec();
- CURTASK excs[CURTASK exsc].LoopTopLine = l;
-
- /* find the NEXT statement */
-
- CURTASK excs[CURTASK exsc].LoopBottomLine = find_BottomLineInCode(l);
-
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == NULL)
- {
- /* NOT FOUND */
- bwb_error("FOR without NEXT");
- return bwb_zline(l);
- }
- }
- /* -------------------------------------------------------------------
- * ------------------------------- */
-
- if (TRUE)
- {
- register int n;
- int e, loop;
- int to, step, p;
- BasicNumberType for_step; /* STEP value for FOR */
- BasicNumberType for_target; /* target value for FOR */
- struct exp_ese *exp;
- struct bwb_variable *v;
- char tbuf[BasicStringLengthMax + 1];
- int VarStart; /* position in l->buffer */
- int VarLength;
- int InitStart;
- int InitLength;
- int TermStart;
- int TermLength;
- int StepStart;
- int StepLength;
- BasicNumberType Value;
-
-
-
- /* VarStart = 0; */
- /* VarLength = 0; */
- InitStart = 0;
- InitLength = 0;
- TermStart = 0;
- TermLength = 0;
- StepStart = 0;
- StepLength = 0;
-
- /* get the variable name */
- VarStart = l->position;
- exp_getvfname(&(l->buffer[l->position]), tbuf);
- VarLength = strlen(tbuf);
- l->position += VarLength;
-
-
- /* at this point one should find an equals sign ('=') */
-
- adv_ws(l->buffer, &(l->position));
-
- if (l->buffer[l->position] != '=')
- {
- sprintf(bwb_ebuf, "in bwb_for(): failed to find equals sign, buf <%s>",
- &(l->buffer[l->position]));
- bwb_error(bwb_ebuf);
- return bwb_zline(l);
- }
- else
- {
- ++(l->position);
- }
-
- /* Find the TO and STEP statements */
-
- cnd_tostep(l->buffer, l->position, &to, &step);
-
- /* if there is no TO statement, then an error has ocurred */
-
- if (to < 1)
- {
- bwb_error("FOR without TO");
- return bwb_zline(l);
- }
- /* copy initial value to buffer and evaluate it */
-
- InitStart = l->position;
- tbuf[0] = '\0';
- p = 0;
- for (n = l->position; n < to; ++n)
- {
- tbuf[p] = l->buffer[n];
- ++p;
- ++l->position;
- tbuf[p] = '\0';
- }
- InitLength = p;
-
- /* copy target value to small buffer and evaluate it */
-
- tbuf[0] = '\0';
- p = 0;
- l->position = to + 2;
- if (step < 1)
- {
- e = strlen(l->buffer);
- }
- else
- {
- e = step - 1;
- }
-
- loop = TRUE;
- n = l->position;
-
-
- TermStart = l->position;
-
-
- while (loop == TRUE)
- {
- tbuf[p] = l->buffer[n];
- ++p;
- ++l->position;
- tbuf[p] = '\0';
-
- if (n >= e)
- {
- loop = FALSE;
- }
- ++n;
-
- if (l->buffer[n] == '\0' || l->buffer[n] == OptionCommentChar)
- {
- loop = FALSE;
- }
- }
- TermLength = p;
-
-
- /* If there is a STEP statement, copy it to a buffer and
- * evaluate it */
-
- if (step > 1)
- {
- tbuf[0] = '\0';
- p = 0;
- l->position = step + 4;
-
- StepStart = l->position;
-
-
- for (n = l->position; n < (int) strlen(l->buffer); ++n)
- {
- tbuf[p] = l->buffer[n];
- ++p;
- ++l->position;
- tbuf[p] = '\0';
- }
- StepLength = p;
- }
- if (OptionFlags & OPTION_BUGS_ON)
- {
- /* Create Variable */
- if (VarLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[VarStart], VarLength);
- tbuf[VarLength] = '\0';
- v = var_find(tbuf);
-
- /* Init */
- if (InitLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[InitStart], InitLength);
- tbuf[InitLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- Value = exp_getnval(exp);
-
- /* Assign Variable */
- var_setnval(v, Value);
-
- /* Limit */
- if (TermLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[TermStart], TermLength);
- tbuf[TermLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- for_target = exp_getnval(exp);
-
- /* Step */
- if (StepLength > 0)
- {
- strncpy(tbuf, &l->buffer[StepStart], StepLength);
- tbuf[StepLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- for_step = exp_getnval(exp);
- }
- else
- {
- for_step = 1.0;
- }
- }
- else
- {
- /* Limit */
- if (TermLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[TermStart], TermLength);
- tbuf[TermLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- for_target = exp_getnval(exp);
-
- /* Step */
- if (StepLength > 0)
- {
- strncpy(tbuf, &l->buffer[StepStart], StepLength);
- tbuf[StepLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- for_step = exp_getnval(exp);
- }
- else
- {
- for_step = 1.0;
- }
-
- /* Init */
- if (InitLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[InitStart], InitLength);
- tbuf[InitLength] = '\0';
- p = 0;
- exp = bwb_exp(tbuf, FALSE, &p);
- if (ERROR_PENDING)
- {
- return bwb_zline(l);
- }
- Value = exp_getnval(exp);
-
-
- /* Create Variable */
- if (VarLength < 1)
- {
- bwb_error(err_syntax);
- return bwb_zline(l);
- }
- strncpy(tbuf, &l->buffer[VarStart], VarLength);
- tbuf[VarLength] = '\0';
- v = var_find(tbuf);
-
- /* Assign Variable */
- var_setnval(v, Value);
-
- }
-
- CURTASK excs[CURTASK exsc].local_variable = v;
- CURTASK excs[CURTASK exsc].for_step = for_step;
- CURTASK excs[CURTASK exsc].for_target = for_target;
- }
- /* -------------------------------------------------------------------
- * ------------------------------ */
-
-
- bwb_setexec(l, l->position, EXEC_FOR);
-
-
- /* 800 FOR I1=9 TO I1 STEP I1 ' I1=-2 */
- if (TRUE)
- {
- int IsExit;
- BasicNumberType Value;
- BasicNumberType Target;
- BasicNumberType Step;
-
- IsExit = FALSE;
-
- Value = var_getnval(CURTASK excs[CURTASK exsc].local_variable);
- Target = CURTASK excs[CURTASK exsc].for_target;
- Step = CURTASK excs[CURTASK exsc].for_step;
-
- if (Step > 0)
- {
- /* POSITIVE */
- if (Value > Target)
- {
- IsExit = TRUE;
- }
- }
- else
- {
- /* NEGATIVE */
- if (Value < Target)
- {
- IsExit = TRUE;
- }
- }
- if (IsExit == TRUE)
- {
- /* EXIT FOR */
-
-
- struct bwb_line *next_line;
-
- next_line = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
-
- bwb_decexec();
-
- /* set the next line in the exec stack */
-
- next_line->position = 0;
- /* bwb_setexec( next_line, 0, EXEC_NORM ); *//* WRONG
- * (JBV) */
- bwb_setexec(next_line, 0, CURTASK excs[CURTASK exsc].code); /* JBV */
- return next_line;
-
- }
- }
- /* proceed with processing */
- return bwb_zline(l);
- }
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_exitfor()
-
- DESCRIPTION: This function handles the BASIC EXIT
- FOR statement. This is a structured
- programming command compatible with ANSI
- BASIC. It is called from the bwb_exit()
- subroutine.
-
- SYNTAX: EXIT FOR
-
- ***************************************************************/
-
- struct bwb_line *
- bwb_EXIT_FOR(struct bwb_line * l)
- {
- struct bwb_line *next_line;
-
- bwx_DEBUG(__FUNCTION__);
- /* Check the integrity of the FOR statement */
-
- next_line = FindExitLineOnStack(l);
- if (next_line == NULL)
- {
- bwb_error("EXIT FOR without FOR");
- return bwb_zline(l);
- }
- {
- /* EXIT FOR */
- struct bwb_line *r;
-
- CURTASK excs[CURTASK exsc].LoopTopLine = NULL;
- r = CURTASK excs[CURTASK exsc].LoopBottomLine->next;
- bwb_setexec(r, 0, CURTASK excs[CURTASK exsc - 1].code);
- r->position = 0;
- bwb_decexec();
- return r;
- }
- #if 0
- next_line = next_line->next;
-
- /* set the next line in the exec stack */
- next_line->position = 0;
- bwb_setexec(next_line, 0, EXEC_FOR);
- return next_line;
- #endif
- }
-
-
-
-
- /***************************************************************
-
- FUNCTION: bwb_next()
-
- DESCRIPTION: This function handles the BASIC NEXT
- statement.
-
- SYNTAX: NEXT counter
-
- ***************************************************************/
-
-
- struct bwb_line *
- bwb_NEXT(struct bwb_line * l)
- {
- char tbuf[BasicStringLengthMax + 1];
-
- bwx_DEBUG(__FUNCTION__);
-
- if (FindBottomLineOnStack(l) == FALSE)
- {
- /* NOT FOUND */
- bwb_error("NEXT without FOR");
- return bwb_zline(l);
- }
- /* read the argument, if there is one */
-
- exp_getvfname(&(l->buffer[l->position]), tbuf);
-
- if (strlen(tbuf) != 0)
- {
- /* NEXT variablename */
- struct bwb_variable *v;
-
-
- v = var_find(tbuf);
- l->position += strlen(tbuf);
-
- /* decrement or increment the value */
-
- if (CURTASK excs[CURTASK exsc].local_variable != v)
- {
- /* not found */
- bwb_error("NEXT without FOR");
- return bwb_zline(l);
- }
- }
- else
- {
- /* NEXT */
-
- if (CURTASK excs[CURTASK exsc].local_variable == NULL)
- {
- /* not found */
- bwb_error("NEXT without FOR");
- return bwb_zline(l);
- }
- }
-
-
-
-
- var_setnval(CURTASK excs[CURTASK exsc].local_variable,
- var_getnval(CURTASK excs[CURTASK exsc].local_variable)
- + CURTASK excs[CURTASK exsc].for_step);
-
- /* check for completion of the loop */
-
- if (CURTASK excs[CURTASK exsc].for_step > 0) /* if step is positive */
- {
-
- if (var_getnval(CURTASK excs[CURTASK exsc].local_variable)
- > CURTASK excs[CURTASK exsc].for_target)
- {
- bwb_decexec();
- bwb_setexec(l->next, 0, CURTASK excs[CURTASK exsc].code);
- return bwb_zline(l);
- }
- }
- else /* if step is negative */
- {
- if (var_getnval(CURTASK excs[CURTASK exsc].local_variable)
- < CURTASK excs[CURTASK exsc].for_target)
- {
- bwb_decexec();
- bwb_setexec(l->next, 0, CURTASK excs[CURTASK exsc].code);
- return bwb_zline(l);
- }
- }
-
- /* Target not reached: return to the top of the FOR loop */
-
- bwb_setexec(CURTASK excs[CURTASK exsc].LoopTopLine,
- CURTASK excs[CURTASK exsc].LoopTopLine->position, EXEC_FOR);
-
-
- return CURTASK excs[CURTASK exsc].LoopTopLine;
-
- }
-
-
-
- /***************************************************************
-
- FUNCTION: cnd_tostep()
-
- DESCRIPTION: This function searches through the
- <buffer> beginning at point <position>
- and attempts to find positions of TO
- and STEP statements.
-
- ***************************************************************/
-
- static int
- cnd_tostep(char *buffer, int position, int *to, int *step)
- {
- int loop, t_pos, b_pos, p_word;
- char tbuf[BasicStringLengthMax + 1];
-
- bwx_DEBUG(__FUNCTION__);
- /* set then and els to FALSE initially */
-
- *to = *step = FALSE;
-
- /* loop to find words */
-
- p_word = b_pos = position;
- t_pos = 0;
- tbuf[0] = '\0';
- loop = TRUE;
- while (loop == TRUE)
- {
-
- if (buffer[b_pos] == '\0' || buffer[b_pos] == OptionCommentChar)
- {
- /* end of string */
- return TRUE;
- }
- switch (buffer[b_pos])
- {
- case ' ': /* whitespace = end of word */
- if (strncasecmp(tbuf, "TO", (size_t) strlen("TO")) == 0)
- {
- *to = p_word;
- }
- else
- if (strncasecmp(tbuf, "STEP", (size_t) strlen("STEP")) == 0)
- {
- *step = p_word;
- }
- ++b_pos;
- p_word = b_pos;
- t_pos = 0;
- tbuf[0] = '\0';
- break;
-
- default:
- tbuf[t_pos] = buffer[b_pos];
- ++b_pos;
- ++t_pos;
- tbuf[t_pos] = '\0';
- break;
- }
-
- }
-
- return TRUE;
-
- }
-
- /***************************************************************
-
- FUNCTION: var_setnval()
-
- DESCRIPTION: This function sets the value of numerical
- variable v to the value of i.
-
- ***************************************************************/
-
- int
- var_setnval(struct bwb_variable * v, BasicNumberType i)
- {
- bwx_DEBUG(__FUNCTION__);
-
- switch (v->type)
- {
- case NUMBER:
- *var_findnval(v, v->array_pos) = i;
- break;
- default:
- sprintf(bwb_ebuf, "in var_setnval(): variable <%s> is not a number",
- v->name);
- bwb_error(bwb_ebuf);
- }
-
- /* successful assignment */
-
- return TRUE;
-
- }
-
- static
- int
- FindTopLineOnStack(struct bwb_line * l)
- {
- /* since we are the top of the loop, we MIGHT be on the stack */
- int i;
-
- bwx_DEBUG(__FUNCTION__);
-
- for (i = CURTASK exsc; i >= 0; i--)
- {
- struct bwb_line *current;
-
- current = CURTASK excs[i].LoopTopLine;
- if (current != NULL)
- {
- if (current == l)
- {
- /* FOUND */
- while (CURTASK exsc > i)
- {
- bwb_decexec();
- }
- /* we are now the top item on the stack */
- return TRUE;
- }
- /* do NOT cross a function/sub boundary */
- switch (current->cmdnum)
- {
- case C_FUNCTION:
- case C_SUB:
- /* NOT FOUND */
- return FALSE;
- break;
- }
- }
- }
-
- /* NOT FOUND */
- return FALSE;
- }
-
- static
- int
- FindBottomLineOnStack(struct bwb_line * l)
- {
- /* since we are the bottom of the loop, we MUST be on the stack */
-
- bwx_DEBUG(__FUNCTION__);
-
- while (CURTASK exsc >= 0)
- {
- if (CURTASK excs[CURTASK exsc].LoopBottomLine == l)
- {
- /* FOUND */
- return TRUE;
- }
- bwb_decexec();
- }
-
- /* NOT FOUND */
- return FALSE;
- }
-
- static struct bwb_line *
- FindExitLineOnStack(struct bwb_line * l)
- {
- /* we are an EXIT ... command, find the bottom line on the stack */
-
- bwx_DEBUG(__FUNCTION__);
- while (CURTASK exsc >= 0)
- {
- struct bwb_line *current;
-
- current = CURTASK excs[CURTASK exsc].LoopBottomLine;
- if (current != NULL)
- {
- switch (l->cmdnum)
- {
- case C_EXIT_DO:
- /* DO - LOOP */
- switch (current->cmdnum)
- {
- case C_LOOP:
- case C_LOOP_UNTIL:
- case C_LOOP_WHILE:
- return current;
- break;
- }
- break;
- case C_EXIT_WHILE:
- /* WHILE - WEND */
- switch (current->cmdnum)
- {
- case C_WEND:
- return current;
- break;
- }
- break;
- case C_EXIT_UNTIL:
- /* UNTIL - UEND */
- switch (current->cmdnum)
- {
- case C_UEND:
- return current;
- break;
- }
- break;
- case C_EXIT_FOR:
- /* FOR - NEXT */
- switch (current->cmdnum)
- {
- case C_NEXT:
- return current;
- break;
- }
- break;
- case C_EXIT_SUB:
- /* SUB - END SUB */
- switch (current->cmdnum)
- {
- case C_END_SUB:
- return current;
- break;
- }
- break;
- case C_EXIT_FUNCTION:
- /* FUNCTION - END FUNCTION */
- switch (current->cmdnum)
- {
- case C_END_FUNCTION:
- return current;
- break;
- }
- break;
- case C_ELSEIF:
- case C_ELSE:
- /* IF - END IF */
- switch (current->cmdnum)
- {
- case C_END_IF:
- return current;
- break;
- }
- break;
- case C_CASE_IF:
- case C_CASE_IS:
- case C_CASE:
- case C_CASE_ELSE:
- /* SELECT CASE - END SELECT */
- switch (current->cmdnum)
- {
- case C_END_SELECT:
- return current;
- break;
- }
- break;
- }
- }
- bwb_decexec();
- }
- /* NOT FOUND */
- return FALSE;
- }
-
-
- static struct bwb_line *
- scan_BottomLineInCode(struct bwb_line * l)
- {
- /* we are the TopLine. Let's find our matching BottomLine */
-
- struct bwb_line *current;
- register int w_level;
-
- bwx_DEBUG(__FUNCTION__);
-
- switch (l->cmdnum)
- {
- case C_DEF:
- case C_USER_LBL:
- /* just the single line */
- return l;
- break;
- }
-
- /* multiple lines */
- w_level = 1;
- for (current = l->next; current != &CURTASK bwb_end; current = current->next)
- {
- switch (l->cmdnum)
- {
- case C_DO:
- case C_DO_WHILE:
- case C_DO_UNTIL:
- /* DO - LOOP */
- switch (current->cmdnum)
- {
- case C_DO:
- case C_DO_WHILE:
- case C_DO_UNTIL:
- ++w_level;
- break;
- case C_LOOP:
- case C_LOOP_UNTIL:
- case C_LOOP_WHILE:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_WHILE:
- /* WHILE - WEND */
- switch (current->cmdnum)
- {
- case C_WHILE:
- ++w_level;
- break;
- case C_WEND:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_UNTIL:
- /* UNTIL - UEND */
- switch (current->cmdnum)
- {
- case C_UNTIL:
- ++w_level;
- break;
- case C_UEND:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_FOR:
- /* FOR - NEXT */
- switch (current->cmdnum)
- {
- case C_FOR:
- ++w_level;
- break;
- case C_NEXT:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_IF_THEN:
- /* IF - END IF */
- switch (current->cmdnum)
- {
- case C_IF_THEN:
- ++w_level;
- break;
- case C_END_IF:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_SELECT_CASE:
- /* SELECT CASE - END SELECT */
- switch (current->cmdnum)
- {
- case C_SELECT_CASE:
- ++w_level;
- break;
- case C_END_SELECT:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_SUB:
- /* SUB - END SUB */
- switch (current->cmdnum)
- {
- case C_SUB:
- ++w_level;
- break;
- case C_END_SUB:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- case C_FUNCTION:
- /* SUB - END SUB */
- switch (current->cmdnum)
- {
- case C_FUNCTION:
- ++w_level;
- break;
- case C_END_FUNCTION:
- --w_level;
- if (w_level == 0)
- {
- return current;
- }
- break;
- }
- break;
- default:
- return NULL;
- break;
- }
- }
- /* NOT FOUND */
- return NULL;
- }
-
-
- struct bwb_line *
- find_BottomLineInCode(struct bwb_line * l)
- {
- /* we only want to scan once */
- /* bwb_scan() sets l->OtherLine to NULL to ensure MERGE and DELETE
- * work correctly */
- bwx_DEBUG(__FUNCTION__);
-
- if (l->OtherLine == NULL)
- {
- /* we do not yet know */
- l->OtherLine = scan_BottomLineInCode(l);
- if (l->OtherLine != NULL)
- {
- /* FOUND */
- /* mark the bottom line to point to us, so we can use
- * this info in DELETE */
- l->OtherLine->OtherLine = l;
- }
- }
- return l->OtherLine;
- }
-
- static struct bwb_line *
- find_NextTestInCode(struct bwb_line * l)
- {
- struct bwb_line *current;
- register int w_level;
-
- bwx_DEBUG(__FUNCTION__);
-
- w_level = 1;
- for (current = l->next; current != &CURTASK bwb_end; current = current->next)
- {
- switch (l->cmdnum)
- {
- case C_IF_THEN:
- case C_ELSEIF:
- switch (current->cmdnum)
- {
- case C_IF_THEN:
- ++w_level;
- break;
- case C_ELSEIF:
- case C_ELSE:
- /* we must report only the first ELSE we
- * encounter at level 1 */
- if (w_level == 1)
- {
- return current;
- }
- break;
- case C_END_IF:
- --w_level;
- if (w_level == 0)
- {
- return NULL;
- }
- break;
- }
- break;
- case C_CASE:
- case C_CASE_IF:
- case C_CASE_IS:
- switch (current->cmdnum)
- {
- case C_SELECT_CASE:
- ++w_level;
- break;
- case C_CASE:
- case C_CASE_IF:
- case C_CASE_IS:
- case C_CASE_ELSE:
- /* we must report only the first CASE we
- * encounter at level 1 */
- if (w_level == 1)
- {
- return current;
- }
- break;
- case C_END_SELECT:
- --w_level;
- if (w_level == 0)
- {
- return NULL;
- }
- }
- break;
- default:
- return NULL;
- break;
- }
-
- }
- return NULL;
- }
-
- /* EOF */
|