ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1938 lines
41 KiB

  1. /***************************************************************
  2. bwb_cnd.c Conditional Expressions and Commands
  3. for Bywater BASIC Interpreter
  4. Copyright (c) 1993, Ted A. Campbell
  5. Bywater Software
  6. email: tcamp@delphi.com
  7. Copyright and Permissions Information:
  8. All U.S. and international rights are claimed by the author,
  9. Ted A. Campbell.
  10. This software is released under the terms of the GNU General
  11. Public License (GPL), which is distributed with this software
  12. in the file "COPYING". The GPL specifies the terms under
  13. which users may copy and use the software in this distribution.
  14. A separate license is available for commercial distribution,
  15. for information on which you should contact the author.
  16. ***************************************************************/
  17. /*---------------------------------------------------------------*/
  18. /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
  19. /* 11/1995 (eidetics@cerf.net). */
  20. /* */
  21. /* Those additionally marked with "DD" were at the suggestion of */
  22. /* Dale DePriest (daled@cadence.com). */
  23. /* */
  24. /* Version 3.00 by Howard Wulf, AF5NE */
  25. /* */
  26. /* Version 3.10 by Howard Wulf, AF5NE */
  27. /* */
  28. /* Version 3.20 by Howard Wulf, AF5NE */
  29. /* */
  30. /*---------------------------------------------------------------*/
  31. #include "bwbasic.h"
  32. static LineType *bwb_then_else (LineType * l, int Value);
  33. static LineType *bwb_if_file (LineType * l, int ThenValue);
  34. static int FindTopLineOnStack (LineType * l);
  35. static int for_limit_check (DoubleType Value, DoubleType Target,
  36. DoubleType Step);
  37. static int IsTypeMismatch (char LeftTypeCode, char RightTypeCode);
  38. /*
  39. --------------------------------------------------------------------------------------------
  40. EXIT
  41. --------------------------------------------------------------------------------------------
  42. */
  43. LineType *
  44. bwb_EXIT (LineType * l)
  45. {
  46. assert (l != NULL);
  47. WARN_SYNTAX_ERROR;
  48. return (l);
  49. }
  50. /*
  51. --------------------------------------------------------------------------------------------
  52. SELECT
  53. --------------------------------------------------------------------------------------------
  54. */
  55. LineType *
  56. bwb_SELECT (LineType * l)
  57. {
  58. assert (l != NULL);
  59. WARN_SYNTAX_ERROR;
  60. return (l);
  61. }
  62. /*
  63. --------------------------------------------------------------------------------------------
  64. FUNCTION - END FUNCTION
  65. --------------------------------------------------------------------------------------------
  66. */
  67. /***************************************************************
  68. FUNCTION: bwb_FUNCTION()
  69. DESCRIPTION: This function implements the BASIC
  70. FUNCTION command, introducing a named
  71. function.
  72. SYNTAX: FUNCTION subroutine-name
  73. ...
  74. [ EXIT FUNCTION ]
  75. ...
  76. END FUNCTION
  77. ***************************************************************/
  78. LineType *
  79. bwb_FUNCTION (LineType * l)
  80. {
  81. assert (l != NULL);
  82. if (l->LineFlags & (LINE_USER))
  83. {
  84. WARN_ILLEGAL_DIRECT;
  85. return (l);
  86. }
  87. /* check current exec level */
  88. assert(My != NULL);
  89. assert(My->StackHead != NULL);
  90. if (My->StackHead->next == NULL)
  91. {
  92. /* skip over the entire function definition */
  93. l = l->OtherLine; /* line of END SUB */
  94. l = l->next; /* line after END SUB */
  95. l->position = 0;
  96. return l;
  97. }
  98. /* we are being executed via IntrinsicFunction_deffn() */
  99. /* if this is the first time at this SUB statement, note it */
  100. if (My->StackHead->LoopTopLine != l)
  101. {
  102. if (bwb_incexec ())
  103. {
  104. /* OK */
  105. My->StackHead->LoopTopLine = l;
  106. }
  107. else
  108. {
  109. /* ERROR */
  110. WARN_OUT_OF_MEMORY;
  111. return My->EndMarker;
  112. }
  113. }
  114. line_skip_eol (l);
  115. return (l);
  116. }
  117. LineType *
  118. bwb_EXIT_FUNCTION (LineType * l)
  119. {
  120. assert (l != NULL);
  121. if (l->LineFlags & (LINE_USER))
  122. {
  123. WARN_ILLEGAL_DIRECT;
  124. return (l);
  125. }
  126. /* check integrity of SUB commmand */
  127. if (FindTopLineOnStack (l->OtherLine))
  128. {
  129. /* FOUND */
  130. LineType *r;
  131. bwb_decexec ();
  132. r = l->OtherLine; /* line of FUNCTION */
  133. r = r->OtherLine; /* line of END FUNCTION */
  134. r = r->next; /* line after END FUNCTION */
  135. r->position = 0;
  136. return r;
  137. }
  138. /* NOT FOUND */
  139. WARN_EXIT_FUNCTION_WITHOUT_FUNCTION;
  140. return (l);
  141. }
  142. LineType *
  143. bwb_END_FUNCTION (LineType * l)
  144. {
  145. assert (l != NULL);
  146. if (l->LineFlags & (LINE_USER))
  147. {
  148. WARN_ILLEGAL_DIRECT;
  149. return (l);
  150. }
  151. /* check integrity of SUB commmand */
  152. if (FindTopLineOnStack (l->OtherLine) == FALSE)
  153. {
  154. /* NOT FOUND */
  155. WARN_END_FUNCTION_WITHOUT_FUNCTION;
  156. return (l);
  157. }
  158. /* decrement the stack */
  159. bwb_decexec ();
  160. /* and return next from old line */
  161. assert(My != NULL);
  162. assert(My->StackHead != NULL);
  163. My->StackHead->line->next->position = 0;
  164. return My->StackHead->line->next;
  165. }
  166. LineType *
  167. bwb_FNEND (LineType * l)
  168. {
  169. assert (l != NULL);
  170. return bwb_END_FUNCTION (l);
  171. }
  172. LineType *
  173. bwb_FEND (LineType * l)
  174. {
  175. assert (l != NULL);
  176. return bwb_END_FUNCTION (l);
  177. }
  178. /*
  179. --------------------------------------------------------------------------------------------
  180. SUB - END SUB
  181. --------------------------------------------------------------------------------------------
  182. */
  183. /***************************************************************
  184. FUNCTION: bwb_sub()
  185. DESCRIPTION: This function implements the BASIC
  186. SUB command, introducing a named
  187. subroutine.
  188. SYNTAX: SUB subroutine-name
  189. ...
  190. [ EXIT SUB ]
  191. ...
  192. END SUB
  193. ***************************************************************/
  194. LineType *
  195. bwb_SUB (LineType * l)
  196. {
  197. assert (l != NULL);
  198. if (l->LineFlags & (LINE_USER))
  199. {
  200. WARN_ILLEGAL_DIRECT;
  201. return (l);
  202. }
  203. /* check current exec level */
  204. assert(My != NULL);
  205. assert(My->StackHead != NULL);
  206. if (My->StackHead->next == NULL)
  207. {
  208. /* skip over the entire function definition */
  209. l = l->OtherLine; /* line of END SUB */
  210. l = l->next; /* line after END SUB */
  211. l->position = 0;
  212. return l;
  213. }
  214. /* we are being executed via IntrinsicFunction_deffn() */
  215. /* if this is the first time at this SUB statement, note it */
  216. if (My->StackHead->LoopTopLine != l)
  217. {
  218. if (bwb_incexec ())
  219. {
  220. /* OK */
  221. My->StackHead->LoopTopLine = l;
  222. }
  223. else
  224. {
  225. /* ERROR */
  226. WARN_OUT_OF_MEMORY;
  227. return My->EndMarker;
  228. }
  229. }
  230. line_skip_eol (l);
  231. return (l);
  232. }
  233. LineType *
  234. bwb_EXIT_SUB (LineType * l)
  235. {
  236. assert (l != NULL);
  237. if (l->LineFlags & (LINE_USER))
  238. {
  239. WARN_ILLEGAL_DIRECT;
  240. return (l);
  241. }
  242. /* check integrity of SUB commmand */
  243. if (FindTopLineOnStack (l->OtherLine))
  244. {
  245. /* FOUND */
  246. LineType *r;
  247. bwb_decexec ();
  248. r = l->OtherLine; /* line of FUNCTION */
  249. r = r->OtherLine; /* line of END FUNCTION */
  250. r = r->next; /* line after END FUNCTION */
  251. r->position = 0;
  252. return r;
  253. }
  254. /* NOT FOUND */
  255. WARN_EXIT_SUB_WITHOUT_SUB;
  256. return (l);
  257. }
  258. LineType *
  259. bwb_SUBEXIT (LineType * l)
  260. {
  261. assert (l != NULL);
  262. return bwb_EXIT_SUB (l);
  263. }
  264. LineType *
  265. bwb_SUB_EXIT (LineType * l)
  266. {
  267. assert (l != NULL);
  268. return bwb_EXIT_SUB (l);
  269. }
  270. LineType *
  271. bwb_END_SUB (LineType * l)
  272. {
  273. assert (l != NULL);
  274. if (l->LineFlags & (LINE_USER))
  275. {
  276. WARN_ILLEGAL_DIRECT;
  277. return (l);
  278. }
  279. /* check integrity of SUB commmand */
  280. if (FindTopLineOnStack (l->OtherLine) == FALSE)
  281. {
  282. /* NOT FOUND */
  283. WARN_END_SUB_WITHOUT_SUB;
  284. return (l);
  285. }
  286. /* decrement the stack */
  287. bwb_decexec ();
  288. /* and return next from old line */
  289. assert(My != NULL);
  290. assert(My->StackHead != NULL);
  291. My->StackHead->line->next->position = 0;
  292. return My->StackHead->line->next;
  293. }
  294. LineType *
  295. bwb_SUBEND (LineType * l)
  296. {
  297. assert (l != NULL);
  298. return bwb_END_SUB (l);
  299. }
  300. LineType *
  301. bwb_SUB_END (LineType * l)
  302. {
  303. assert (l != NULL);
  304. return bwb_END_SUB (l);
  305. }
  306. /*
  307. --------------------------------------------------------------------------------------------
  308. IF - END IF
  309. --------------------------------------------------------------------------------------------
  310. */
  311. /***************************************************************
  312. FUNCTION: bwb_IF()
  313. DESCRIPTION: This function handles the BASIC IF
  314. statement, standard flavor.
  315. standard
  316. SYNTAX: IF expression THEN line [ELSE line]
  317. IF END # file THEN line [ELSE line]
  318. IF MORE # file THEN line [ELSE line]
  319. ***************************************************************/
  320. LineType *
  321. bwb_IF (LineType * l)
  322. {
  323. /* classic IF */
  324. /* IF expression THEN 100 */
  325. /* IF expression THEN 100 ELSE 200 */
  326. int Value;
  327. assert (l != NULL);
  328. if (l->LineFlags & (LINE_USER))
  329. {
  330. WARN_ILLEGAL_DIRECT;
  331. return (l);
  332. }
  333. if (line_read_integer_expression (l, &Value) == FALSE)
  334. {
  335. WARN_SYNTAX_ERROR;
  336. return (l);
  337. }
  338. return bwb_then_else (l, Value);
  339. }
  340. LineType *
  341. bwb_IF_END (LineType * l)
  342. {
  343. /* IF END #1 THEN 100 */
  344. assert (l != NULL);
  345. if (l->LineFlags & (LINE_USER))
  346. {
  347. WARN_ILLEGAL_DIRECT;
  348. return (l);
  349. }
  350. assert(My != NULL);
  351. assert(My->CurrentVersion != NULL);
  352. if (My->CurrentVersion->OptionVersionValue & (C77))
  353. {
  354. /* sets a linenumber to branch to on EOF */
  355. int FileNumber = 0;
  356. int LineNumber = 0;
  357. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  358. {
  359. WARN_BAD_FILE_NUMBER;
  360. return (l);
  361. }
  362. if (FileNumber <= 0)
  363. {
  364. WARN_BAD_FILE_NUMBER;
  365. return (l);
  366. }
  367. if (line_skip_word (l, "THEN") == FALSE)
  368. {
  369. WARN_SYNTAX_ERROR;
  370. return (l);
  371. }
  372. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  373. {
  374. WARN_UNDEFINED_LINE;
  375. return (l);
  376. }
  377. if (LineNumber < 0)
  378. {
  379. WARN_UNDEFINED_LINE;
  380. return (l);
  381. }
  382. /* now, we are ready to create the file */
  383. My->CurrentFile = find_file_by_number (FileNumber);
  384. if (My->CurrentFile == NULL)
  385. {
  386. My->CurrentFile = file_new ();
  387. My->CurrentFile->FileNumber = FileNumber;
  388. }
  389. My->CurrentFile->EOF_LineNumber = LineNumber;
  390. return (l);
  391. }
  392. /* branch to the line if we are currently at EOF */
  393. return bwb_if_file (l, TRUE);
  394. }
  395. LineType *
  396. bwb_IF_MORE (LineType * l)
  397. {
  398. /* IF MORE #1 THEN 100 */
  399. assert (l != NULL);
  400. if (l->LineFlags & (LINE_USER))
  401. {
  402. WARN_ILLEGAL_DIRECT;
  403. return (l);
  404. }
  405. /* branch to the line if we are not currently at EOF */
  406. return bwb_if_file (l, FALSE);
  407. }
  408. /***************************************************************
  409. FUNCTION: bwb_IF8THEN()
  410. DESCRIPTION: This function handles the BASIC IF
  411. statement, structured flavor.
  412. SYNTAX: IF expression THEN
  413. ...
  414. ELSEIF expression
  415. ...
  416. ELSE
  417. ...
  418. END IF
  419. ***************************************************************/
  420. LineType *
  421. bwb_IF8THEN (LineType * l)
  422. {
  423. /* structured IF */
  424. LineType *else_line;
  425. int Value;
  426. assert (l != NULL);
  427. if (l->LineFlags & (LINE_USER))
  428. {
  429. WARN_ILLEGAL_DIRECT;
  430. return (l);
  431. }
  432. /* evaluate the expression */
  433. if (line_read_integer_expression (l, &Value) == FALSE)
  434. {
  435. WARN_SYNTAX_ERROR;
  436. return (l);
  437. }
  438. if (Value)
  439. {
  440. /* expression is TRUE */
  441. l->next->position = 0;
  442. return l->next;
  443. }
  444. /*
  445. RESUME knows we iterate thru the various ELSEIF commands, and restarts at the IF THEN command.
  446. RESUME NEXT knows we iterate thru the various ELSEIF commands, and restarts at the END IF command.
  447. */
  448. for (else_line = l->OtherLine; else_line->cmdnum == C_ELSEIF;
  449. else_line = else_line->OtherLine)
  450. {
  451. else_line->position = else_line->Startpos;
  452. /* evaluate the expression */
  453. if (line_read_integer_expression (else_line, &Value) == FALSE)
  454. {
  455. WARN_SYNTAX_ERROR;
  456. return (l);
  457. }
  458. if (Value)
  459. {
  460. /* expression is TRUE */
  461. else_line->next->position = 0;
  462. return else_line->next;
  463. }
  464. }
  465. /* ELSE or END IF */
  466. else_line->next->position = 0;
  467. return else_line->next;
  468. }
  469. LineType *
  470. bwb_ELSEIF (LineType * l)
  471. {
  472. assert (l != NULL);
  473. if (l->LineFlags & (LINE_USER))
  474. {
  475. WARN_ILLEGAL_DIRECT;
  476. return (l);
  477. }
  478. for (l = l->OtherLine; l->OtherLine != NULL; l = l->OtherLine);
  479. l = l->next; /* line after END IF */
  480. l->position = 0;
  481. return l;
  482. }
  483. LineType *
  484. bwb_ELSE (LineType * l)
  485. {
  486. assert (l != NULL);
  487. if (l->LineFlags & (LINE_USER))
  488. {
  489. WARN_ILLEGAL_DIRECT;
  490. return (l);
  491. }
  492. for (l = l->OtherLine; l->OtherLine != NULL; l = l->OtherLine);
  493. l = l->next; /* line after END IF */
  494. l->position = 0;
  495. return l;
  496. }
  497. LineType *
  498. bwb_END_IF (LineType * l)
  499. {
  500. assert (l != NULL);
  501. if (l->LineFlags & (LINE_USER))
  502. {
  503. WARN_ILLEGAL_DIRECT;
  504. return (l);
  505. }
  506. return (l);
  507. }
  508. /*
  509. --------------------------------------------------------------------------------------------
  510. SELECT CASE - END SELECT
  511. --------------------------------------------------------------------------------------------
  512. */
  513. /***************************************************************
  514. FUNCTION: bwb_select()
  515. DESCRIPTION: This C function handles the BASIC SELECT
  516. statement.
  517. SYNTAX: SELECT CASE expression ' examples:
  518. CASE value ' CASE 5
  519. CASE min TO max ' CASE 1 TO 10
  520. CASE IF relationaloperator value ' CASE IF > 5
  521. CASE IS relationaloperator value ' CASE IS > 5
  522. CASE ELSE
  523. END SELECT
  524. ***************************************************************/
  525. LineType *
  526. bwb_SELECT_CASE (LineType * l)
  527. {
  528. VariantType selectvalue;
  529. VariantType *e;
  530. LineType *else_line;
  531. assert (l != NULL);
  532. e = &selectvalue;
  533. CLEAR_VARIANT (e);
  534. if (l->LineFlags & (LINE_USER))
  535. {
  536. WARN_ILLEGAL_DIRECT;
  537. return (l);
  538. }
  539. /* evaluate the expression */
  540. if (line_read_expression (l, e) == FALSE) /* bwb_SELECT_CASE */
  541. {
  542. WARN_SYNTAX_ERROR;
  543. return (l);
  544. }
  545. /*
  546. **
  547. ** RESUME knows we iterate thru the various CASE commands, and restarts at the SELECT CASE command.
  548. ** RESUME NEXT knows we iterate thru the various CASE commands, and restarts at the END SELECT command.
  549. **
  550. */
  551. for (else_line = l->OtherLine; else_line->cmdnum == C_CASE;
  552. else_line = else_line->OtherLine)
  553. {
  554. else_line->position = else_line->Startpos;
  555. do
  556. {
  557. /* evaluate the expression */
  558. if (line_skip_word (else_line, "IF")
  559. || line_skip_word (else_line, "IS"))
  560. {
  561. /* CASE IS < 10 */
  562. /* CASE IF < "DEF" */
  563. /* CASE IS > 7 */
  564. /* CASE IS > "ABC" */
  565. char *tbuf;
  566. int tlen;
  567. size_t n; /* number of characters we want to put in tbuf */
  568. int position;
  569. VariantType casevalue;
  570. VariantType *r;
  571. assert(My != NULL);
  572. assert(My->ConsoleOutput != NULL);
  573. assert(MAX_LINE_LENGTH > 1);
  574. tbuf = My->ConsoleOutput;
  575. tlen = MAX_LINE_LENGTH;
  576. n = 0;
  577. r = &casevalue;
  578. CLEAR_VARIANT (r);
  579. /*
  580. **
  581. ** Available choices:
  582. ** 1. Parse every possible operator combination, depending upon the BASIC flavor.
  583. ** 2. Jump into the middle of the expression parser, by exposing the parser internals.
  584. ** 3. Limit the length of the expression. This is the choice I made.
  585. **
  586. */
  587. if (e->VariantTypeCode == StringTypeCode)
  588. {
  589. /* STRING */
  590. n += bwb_strlen (e->Buffer);
  591. if (n > tlen)
  592. {
  593. WARN_STRING_FORMULA_TOO_COMPLEX; /* bwb_SELECT_CASE */
  594. return (l);
  595. }
  596. /* OK , everything will fit */
  597. bwb_strcpy (tbuf, e->Buffer);
  598. }
  599. else
  600. {
  601. /* NUMBER */
  602. FormatBasicNumber (e->Number, tbuf);
  603. n += bwb_strlen (tbuf);
  604. if (n > tlen)
  605. {
  606. WARN_STRING_FORMULA_TOO_COMPLEX; /* bwb_SELECT_CASE */
  607. return (l);
  608. }
  609. /* OK , everything will fit */
  610. }
  611. {
  612. char *Space;
  613. Space = " ";
  614. n += bwb_strlen (Space);
  615. if (n > tlen)
  616. {
  617. WARN_STRING_FORMULA_TOO_COMPLEX; /* bwb_SELECT_CASE */
  618. return (l);
  619. }
  620. /* OK , everything will fit */
  621. bwb_strcat (tbuf, Space);
  622. }
  623. {
  624. n += bwb_strlen (&(else_line->buffer[else_line->position]));
  625. if (n > tlen)
  626. {
  627. WARN_STRING_FORMULA_TOO_COMPLEX; /* bwb_SELECT_CASE */
  628. return (l);
  629. }
  630. /* OK , everything will fit */
  631. bwb_strcat (tbuf, &(else_line->buffer[else_line->position]));
  632. }
  633. position = 0;
  634. if (buff_read_expression (tbuf, &position, r) == FALSE) /* bwb_SELECT_CASE */
  635. {
  636. WARN_SYNTAX_ERROR;
  637. return (l);
  638. }
  639. if (r->VariantTypeCode == StringTypeCode)
  640. {
  641. RELEASE_VARIANT (r);
  642. WARN_TYPE_MISMATCH;
  643. return (l);
  644. }
  645. if (r->Number)
  646. {
  647. /* expression is TRUE */
  648. else_line->next->position = 0;
  649. return else_line->next;
  650. }
  651. /* condition is FALSE */
  652. /* proceed to next CASE line if there is one */
  653. }
  654. else
  655. {
  656. /* CASE 7 */
  657. /* CASE 7 TO 10 */
  658. /* CASE "ABC" */
  659. /* CASE "ABC" TO "DEF" */
  660. VariantType minvalue;
  661. VariantType *minval;
  662. minval = &minvalue;
  663. CLEAR_VARIANT (minval);
  664. /* evaluate the MIN expression */
  665. if (line_read_expression (else_line, minval) == FALSE) /* bwb_SELECT_CASE */
  666. {
  667. WARN_SYNTAX_ERROR;
  668. return (l);
  669. }
  670. if (IsTypeMismatch (e->VariantTypeCode, minval->VariantTypeCode))
  671. {
  672. RELEASE_VARIANT (minval);
  673. WARN_TYPE_MISMATCH;
  674. return (l);
  675. }
  676. if (line_skip_word (else_line, "TO"))
  677. {
  678. /* CASE 7 TO 10 */
  679. /* CASE "ABC" TO "DEF" */
  680. VariantType maxvalue;
  681. VariantType *maxval;
  682. maxval = &maxvalue;
  683. CLEAR_VARIANT (maxval);
  684. /* evaluate the MAX expression */
  685. if (line_read_expression (else_line, maxval) == FALSE) /* bwb_SELECT_CASE */
  686. {
  687. WARN_SYNTAX_ERROR;
  688. return (l);
  689. }
  690. if (IsTypeMismatch (e->VariantTypeCode, maxval->VariantTypeCode))
  691. {
  692. RELEASE_VARIANT (maxval);
  693. WARN_TYPE_MISMATCH;
  694. return (l);
  695. }
  696. if (e->VariantTypeCode == StringTypeCode)
  697. {
  698. /* STRING */
  699. if (bwb_strcmp (e->Buffer, minval->Buffer) >= 0
  700. && bwb_strcmp (e->Buffer, maxval->Buffer) <= 0)
  701. {
  702. /* expression is TRUE */
  703. RELEASE_VARIANT (maxval);
  704. else_line->next->position = 0;
  705. return else_line->next;
  706. }
  707. RELEASE_VARIANT (maxval);
  708. }
  709. else
  710. {
  711. /* NUMBER */
  712. if (e->Number >= minval->Number && e->Number <= maxval->Number)
  713. {
  714. /* expression is TRUE */
  715. else_line->next->position = 0;
  716. return else_line->next;
  717. }
  718. }
  719. }
  720. else
  721. {
  722. /* CASE 7 */
  723. /* CASE "ABC" */
  724. if (e->VariantTypeCode == StringTypeCode)
  725. {
  726. /* STRING */
  727. if (bwb_strcmp (e->Buffer, minval->Buffer) == 0)
  728. {
  729. /* expression is TRUE */
  730. RELEASE_VARIANT (minval);
  731. else_line->next->position = 0;
  732. return else_line->next;
  733. }
  734. RELEASE_VARIANT (minval);
  735. }
  736. else
  737. {
  738. /* NUMBER */
  739. if (e->Number == minval->Number)
  740. {
  741. /* expression is TRUE */
  742. else_line->next->position = 0;
  743. return else_line->next;
  744. }
  745. }
  746. }
  747. /* condition is FALSE */
  748. /* proceed to next CASE line if there is one */
  749. }
  750. }
  751. while (line_skip_seperator (else_line));
  752. }
  753. /* CASE_ELSE or END_SELECT */
  754. RELEASE_VARIANT (e);
  755. else_line->next->position = 0;
  756. return else_line->next;
  757. }
  758. LineType *
  759. bwb_CASE (LineType * l)
  760. {
  761. assert (l != NULL);
  762. if (l->LineFlags & (LINE_USER))
  763. {
  764. WARN_ILLEGAL_DIRECT;
  765. return (l);
  766. }
  767. for (l = l->OtherLine; l->OtherLine != NULL; l = l->OtherLine);
  768. l = l->next; /* line after END SELECT */
  769. l->position = 0;
  770. return l;
  771. }
  772. LineType *
  773. bwb_CASE_ELSE (LineType * l)
  774. {
  775. assert (l != NULL);
  776. if (l->LineFlags & (LINE_USER))
  777. {
  778. WARN_ILLEGAL_DIRECT;
  779. return (l);
  780. }
  781. for (l = l->OtherLine; l->OtherLine != NULL; l = l->OtherLine);
  782. l = l->next; /* line after END SELECT */
  783. l->position = 0;
  784. return l;
  785. }
  786. LineType *
  787. bwb_END_SELECT (LineType * l)
  788. {
  789. assert (l != NULL);
  790. if (l->LineFlags & (LINE_USER))
  791. {
  792. WARN_ILLEGAL_DIRECT;
  793. return (l);
  794. }
  795. return (l);
  796. }
  797. /*
  798. --------------------------------------------------------------------------------------------
  799. DO - LOOP
  800. --------------------------------------------------------------------------------------------
  801. */
  802. /***************************************************************
  803. FUNCTION: bwb_DO()
  804. DESCRIPTION: This C function implements the ANSI BASIC
  805. DO statement.
  806. SYNTAX: DO [UNTIL|WHILE condition]
  807. ...
  808. [EXIT DO]
  809. ...
  810. LOOP [UNTIL|WHILE condition]
  811. ***************************************************************/
  812. LineType *
  813. bwb_DO (LineType * l)
  814. {
  815. LineType *r;
  816. int Value;
  817. assert (l != NULL);
  818. /* DO ' forever */
  819. /* DO UNTIL ' exits when != 0 */
  820. /* DO WHILE ' exits when == 0 */
  821. if (l->LineFlags & (LINE_USER))
  822. {
  823. WARN_ILLEGAL_DIRECT;
  824. return (l);
  825. }
  826. do
  827. {
  828. /* evaluate the expression */
  829. if (line_is_eol (l))
  830. {
  831. break; /* exit 'do' */
  832. }
  833. else if (line_skip_word (l, "UNTIL"))
  834. {
  835. /* DO UNTIL */
  836. if (line_read_integer_expression (l, &Value) == FALSE)
  837. {
  838. WARN_SYNTAX_ERROR;
  839. return (l);
  840. }
  841. if (Value != 0)
  842. {
  843. /* EXIT DO */
  844. r = l->OtherLine; /* line of LOOP */
  845. r = r->next; /* line after LOOP */
  846. r->position = 0;
  847. return r;
  848. }
  849. }
  850. else if (line_skip_word (l, "WHILE"))
  851. {
  852. /* DO WHILE */
  853. if (line_read_integer_expression (l, &Value) == FALSE)
  854. {
  855. WARN_SYNTAX_ERROR;
  856. return (l);
  857. }
  858. if (Value == 0)
  859. {
  860. /* EXIT DO */
  861. r = l->OtherLine; /* line of LOOP */
  862. r = r->next; /* line after LOOP */
  863. r->position = 0;
  864. return r;
  865. }
  866. }
  867. }
  868. while (line_skip_seperator (l));
  869. return (l);
  870. }
  871. LineType *
  872. bwb_EXIT_DO (LineType * l)
  873. {
  874. LineType *r;
  875. assert (l != NULL);
  876. /* EXIT DO */
  877. if (l->LineFlags & (LINE_USER))
  878. {
  879. WARN_ILLEGAL_DIRECT;
  880. return (l);
  881. }
  882. r = l->OtherLine; /* line of DO */
  883. r = r->OtherLine; /* line of LOOP */
  884. r = r->next; /* line after LOOP */
  885. r->position = 0;
  886. return r;
  887. }
  888. LineType *
  889. bwb_LOOP (LineType * l)
  890. {
  891. LineType *r;
  892. int Value;
  893. assert (l != NULL);
  894. /* LOOP ' forever */
  895. /* LOOP UNTIL ' exits when != 0 */
  896. /* LOOP WHILE ' exits when == 0 */
  897. if (l->LineFlags & (LINE_USER))
  898. {
  899. WARN_ILLEGAL_DIRECT;
  900. return (l);
  901. }
  902. do
  903. {
  904. /* evaluate the expression */
  905. if (line_is_eol (l))
  906. {
  907. break; /* exit 'do' */
  908. }
  909. else if (line_skip_word (l, "UNTIL"))
  910. {
  911. /* LOOP UNTIL */
  912. if (line_read_integer_expression (l, &Value) == FALSE)
  913. {
  914. WARN_SYNTAX_ERROR;
  915. return (l);
  916. }
  917. if (Value != 0)
  918. {
  919. /* EXIT DO */
  920. return (l);
  921. }
  922. }
  923. else if (line_skip_word (l, "WHILE"))
  924. {
  925. /* LOOP WHILE */
  926. if (line_read_integer_expression (l, &Value) == FALSE)
  927. {
  928. WARN_SYNTAX_ERROR;
  929. return (l);
  930. }
  931. if (Value == 0)
  932. {
  933. /* EXIT DO */
  934. return (l);
  935. }
  936. }
  937. }
  938. while (line_skip_seperator (l));
  939. /* loop around to DO again */
  940. r = l->OtherLine; /* line of DO */
  941. r->position = 0;
  942. return r;
  943. }
  944. /*
  945. --------------------------------------------------------------------------------------------
  946. WHILE - WEND
  947. --------------------------------------------------------------------------------------------
  948. */
  949. /***************************************************************
  950. FUNCTION: bwb_WHILE()
  951. DESCRIPTION: This function handles the BASIC
  952. WHILE statement.
  953. SYNTAX: WHILE expression ' exits when == 0
  954. ...
  955. [EXIT WHILE]
  956. ...
  957. WEND
  958. ***************************************************************/
  959. LineType *
  960. bwb_WHILE (LineType * l)
  961. {
  962. int Value;
  963. LineType *r;
  964. assert (l != NULL);
  965. if (l->LineFlags & (LINE_USER))
  966. {
  967. WARN_ILLEGAL_DIRECT;
  968. return (l);
  969. }
  970. if (line_read_integer_expression (l, &Value) == FALSE)
  971. {
  972. WARN_SYNTAX_ERROR;
  973. return (l);
  974. }
  975. if (Value == 0)
  976. {
  977. /* EXIT WHILE */
  978. r = l->OtherLine; /* line of WEND */
  979. r = r->next; /* line after WEND */
  980. r->position = 0;
  981. return r;
  982. }
  983. return (l);
  984. }
  985. LineType *
  986. bwb_EXIT_WHILE (LineType * l)
  987. {
  988. LineType *r;
  989. assert (l != NULL);
  990. /* EXIT WHILE */
  991. if (l->LineFlags & (LINE_USER))
  992. {
  993. WARN_ILLEGAL_DIRECT;
  994. return (l);
  995. }
  996. r = l->OtherLine; /* line of WHILE */
  997. r = r->OtherLine; /* line of WEND */
  998. r = r->next; /* line after WEND */
  999. r->position = 0;
  1000. return r;
  1001. }
  1002. LineType *
  1003. bwb_WEND (LineType * l)
  1004. {
  1005. LineType *r;
  1006. assert (l != NULL);
  1007. if (l->LineFlags & (LINE_USER))
  1008. {
  1009. WARN_ILLEGAL_DIRECT;
  1010. return (l);
  1011. }
  1012. r = l->OtherLine; /* line of WHILE */
  1013. r->position = 0;
  1014. return r;
  1015. }
  1016. /*
  1017. --------------------------------------------------------------------------------------------
  1018. REPEAT - UNTIL
  1019. --------------------------------------------------------------------------------------------
  1020. */
  1021. /***************************************************************
  1022. FUNCTION: bwb_UNTIL()
  1023. DESCRIPTION: This function handles the BASIC
  1024. UNTIL statement.
  1025. SYNTAX: UNTIL expression ' exits when != 0
  1026. ...
  1027. [EXIT UNTIL]
  1028. ...
  1029. UEND
  1030. ***************************************************************/
  1031. LineType *
  1032. bwb_REPEAT (LineType * l)
  1033. {
  1034. assert (l != NULL);
  1035. if (l->LineFlags & (LINE_USER))
  1036. {
  1037. WARN_ILLEGAL_DIRECT;
  1038. return (l);
  1039. }
  1040. return (l);
  1041. }
  1042. LineType *
  1043. bwb_EXIT_REPEAT (LineType * l)
  1044. {
  1045. LineType *r;
  1046. assert (l != NULL);
  1047. /* EXIT REPEAT */
  1048. if (l->LineFlags & (LINE_USER))
  1049. {
  1050. WARN_ILLEGAL_DIRECT;
  1051. return (l);
  1052. }
  1053. r = l->OtherLine; /* line of REPEAT */
  1054. r = r->OtherLine; /* line of UNTIL */
  1055. r = r->next; /* line after UNTIL */
  1056. r->position = 0;
  1057. return r;
  1058. }
  1059. LineType *
  1060. bwb_UNTIL (LineType * l)
  1061. {
  1062. int Value;
  1063. assert (l != NULL);
  1064. if (l->LineFlags & (LINE_USER))
  1065. {
  1066. WARN_ILLEGAL_DIRECT;
  1067. return (l);
  1068. }
  1069. if (line_read_integer_expression (l, &Value) == FALSE)
  1070. {
  1071. WARN_SYNTAX_ERROR;
  1072. return (l);
  1073. }
  1074. if (Value == 0)
  1075. {
  1076. /* GOTO REPEAT */
  1077. LineType *r;
  1078. r = l->OtherLine; /* line of REPEAT */
  1079. r->position = 0;
  1080. return r;
  1081. }
  1082. /* EXITS when Value != 0 */
  1083. return (l);
  1084. }
  1085. /*
  1086. --------------------------------------------------------------------------------------------
  1087. FOR - NEXT
  1088. --------------------------------------------------------------------------------------------
  1089. */
  1090. /***************************************************************
  1091. FUNCTION: bwb_for()
  1092. DESCRIPTION: This function handles the BASIC FOR
  1093. statement.
  1094. SYNTAX: FOR counter = start TO finish [STEP increment]
  1095. ...
  1096. [EXIT FOR]
  1097. ...
  1098. NEXT [counter]
  1099. NOTE: This is controlled by the OptionVersion bitmask.
  1100. The order of expression evaluation and variable creation varies.
  1101. For example:
  1102. FUNCTION FNA( Y )
  1103. PRINT "Y="; Y
  1104. FNA = Y
  1105. END FUNCTION
  1106. FOR X = FNA(3) TO FNA(1) STEP FNA(2)
  1107. NEXT X
  1108. ANSI/ECMA;
  1109. Y= 1
  1110. Y= 2
  1111. Y= 3
  1112. X is created (if it does not exist)
  1113. X is assigned the value of 3
  1114. MICROSOFT;
  1115. X is created (if it does not exist)
  1116. Y= 3
  1117. X is assigned the value of 3
  1118. Y= 1
  1119. Y= 2
  1120. ECMA-55: Section 13.4
  1121. ...
  1122. The action of the for-statement and the next-statement is de-
  1123. fined in terms of other statements, as follows:
  1124. FOR v = initial-value TO limit STEP increment
  1125. (block)
  1126. NEXT v
  1127. is equivalent to:
  1128. LET own1 = limit
  1129. LET own2 = increment
  1130. LET v = initial-value
  1131. line1 IF (v-own1) * SGN (own2) > 0 THEN line2
  1132. (block)
  1133. LET v = v + own2
  1134. GOTO line1
  1135. line2 REM continued in sequence
  1136. ...
  1137. ***************************************************************/
  1138. LineType *
  1139. bwb_FOR (LineType * l)
  1140. {
  1141. LineType *r;
  1142. VariableType *v;
  1143. DoubleType Value;
  1144. DoubleType Target;
  1145. DoubleType Step;
  1146. VariantType variant;
  1147. CLEAR_VARIANT (&variant);
  1148. assert (l != NULL);
  1149. assert(My != NULL);
  1150. assert(My->CurrentVersion != NULL);
  1151. if (l->LineFlags & (LINE_USER))
  1152. {
  1153. WARN_ILLEGAL_DIRECT;
  1154. return (l);
  1155. }
  1156. /* if this is the first time at this FOR statement, note it */
  1157. if (FindTopLineOnStack (l) == FALSE)
  1158. {
  1159. if (bwb_incexec ())
  1160. {
  1161. /* OK */
  1162. }
  1163. else
  1164. {
  1165. /* ERROR */
  1166. WARN_OUT_OF_MEMORY;
  1167. return My->EndMarker;
  1168. }
  1169. }
  1170. /* INITIALIZE */
  1171. if ((v = line_read_scalar (l)) == NULL)
  1172. {
  1173. WARN_VARIABLE_NOT_DECLARED;
  1174. return (l);
  1175. }
  1176. if (v->dimensions > 0)
  1177. {
  1178. WARN_TYPE_MISMATCH;
  1179. return (l);
  1180. }
  1181. if (v->VariableTypeCode == StringTypeCode)
  1182. {
  1183. WARN_TYPE_MISMATCH;
  1184. return (l);
  1185. }
  1186. if (line_skip_EqualChar (l) == FALSE)
  1187. {
  1188. WARN_SYNTAX_ERROR;
  1189. return (l);
  1190. }
  1191. if (line_read_numeric_expression (l, &Value) == FALSE)
  1192. {
  1193. WARN_ILLEGAL_FUNCTION_CALL;
  1194. return (l);
  1195. }
  1196. if (My->CurrentVersion->OptionFlags & OPTION_BUGS_ON /* FOR X = ... */ )
  1197. {
  1198. /* Assign Variable */
  1199. variant.VariantTypeCode = v->VariableTypeCode;
  1200. variant.Number = Value;
  1201. if (var_set (v, &variant) == FALSE)
  1202. {
  1203. WARN_VARIABLE_NOT_DECLARED;
  1204. return (l);
  1205. }
  1206. }
  1207. else
  1208. {
  1209. /* assigned below */
  1210. }
  1211. if (line_skip_word (l, "TO") == FALSE)
  1212. {
  1213. WARN_SYNTAX_ERROR;
  1214. return (l);
  1215. }
  1216. if (line_read_numeric_expression (l, &Target) == FALSE)
  1217. {
  1218. WARN_ILLEGAL_FUNCTION_CALL;
  1219. return (l);
  1220. }
  1221. if (line_skip_word (l, "STEP"))
  1222. {
  1223. if (line_read_numeric_expression (l, &Step) == FALSE)
  1224. {
  1225. WARN_ILLEGAL_FUNCTION_CALL;
  1226. return (l);
  1227. }
  1228. }
  1229. else
  1230. {
  1231. Step = 1;
  1232. }
  1233. if (My->CurrentVersion->OptionFlags & OPTION_BUGS_ON /* FOR X = ... */ )
  1234. {
  1235. /* assigned above */
  1236. }
  1237. else
  1238. {
  1239. /* Assign Variable */
  1240. variant.VariantTypeCode = v->VariableTypeCode;
  1241. variant.Number = Value;
  1242. if (var_set (v, &variant) == FALSE)
  1243. {
  1244. WARN_VARIABLE_NOT_DECLARED;
  1245. return (l);
  1246. }
  1247. }
  1248. /* CHECK */
  1249. if (for_limit_check (Value, Target, Step))
  1250. {
  1251. /* EXIT FOR */
  1252. bwb_decexec ();
  1253. r = l->OtherLine; /* line of NEXT */
  1254. r = r->next; /* line after NEXT */
  1255. r->position = 0;
  1256. return r;
  1257. }
  1258. /* we will loop at least once */
  1259. assert(My->StackHead != NULL);
  1260. My->StackHead->line = l;
  1261. My->StackHead->ExecCode = EXEC_FOR;
  1262. My->StackHead->local_variable = v;
  1263. My->StackHead->for_step = Step;
  1264. My->StackHead->for_target = Target;
  1265. My->StackHead->LoopTopLine = l;
  1266. My->StackHead->OnErrorGoto = 0;
  1267. /* proceed with processing */
  1268. return (l);
  1269. }
  1270. LineType *
  1271. bwb_EXIT_FOR (LineType * l)
  1272. {
  1273. LineType *r;
  1274. assert (l != NULL);
  1275. if (l->LineFlags & (LINE_USER))
  1276. {
  1277. WARN_ILLEGAL_DIRECT;
  1278. return (l);
  1279. }
  1280. if (FindTopLineOnStack (l->OtherLine) == FALSE)
  1281. {
  1282. WARN_EXIT_FOR_WITHOUT_FOR;
  1283. return (l);
  1284. }
  1285. assert(My != NULL);
  1286. assert(My->StackHead != NULL);
  1287. My->StackHead->ExecCode = EXEC_FOR;
  1288. bwb_decexec ();
  1289. r = l->OtherLine; /* line of FOR */
  1290. r = r->OtherLine; /* line of NEXT */
  1291. r = r->next; /* line after NEXT */
  1292. r->position = 0;
  1293. return r;
  1294. }
  1295. LineType *
  1296. bwb_NEXT (LineType * l)
  1297. {
  1298. LineType *r;
  1299. VariableType *v;
  1300. DoubleType Value;
  1301. DoubleType Target;
  1302. DoubleType Step;
  1303. assert (l != NULL);
  1304. if (l->LineFlags & (LINE_USER))
  1305. {
  1306. WARN_ILLEGAL_DIRECT;
  1307. return (l);
  1308. }
  1309. if (FindTopLineOnStack (l->OtherLine) == FALSE)
  1310. {
  1311. WARN_NEXT_WITHOUT_FOR;
  1312. return (l);
  1313. }
  1314. assert(My != NULL);
  1315. assert(My->StackHead != NULL);
  1316. My->StackHead->ExecCode = EXEC_FOR;
  1317. /* INCREMENT */
  1318. v = My->StackHead->local_variable;
  1319. Target = My->StackHead->for_target;
  1320. Step = My->StackHead->for_step;
  1321. /* if( TRUE ) */
  1322. {
  1323. VariantType variant;
  1324. CLEAR_VARIANT (&variant);
  1325. if (var_get (v, &variant) == FALSE)
  1326. {
  1327. WARN_NEXT_WITHOUT_FOR;
  1328. return (l);
  1329. }
  1330. if (variant.VariantTypeCode == StringTypeCode)
  1331. {
  1332. WARN_NEXT_WITHOUT_FOR;
  1333. return (l);
  1334. }
  1335. variant.Number += Step;
  1336. Value = variant.Number;
  1337. if (var_set (v, &variant) == FALSE)
  1338. {
  1339. WARN_NEXT_WITHOUT_FOR;
  1340. return (l);
  1341. }
  1342. }
  1343. /* CHECK */
  1344. if (for_limit_check (Value, Target, Step))
  1345. {
  1346. /* EXIT FOR */
  1347. bwb_decexec ();
  1348. return (l);
  1349. }
  1350. /* proceed with processing */
  1351. r = l->OtherLine; /* line of FOR */
  1352. #if FALSE /* keep this ... */
  1353. /*
  1354. This example causes a Syntax Error:
  1355. 100 FOR I = 1 TO 1000:NEXT
  1356. The error is actually caused by execline().
  1357. Note that the example is a delay loop.
  1358. Only NEXT has this issue, because it jumps to TOP->next.
  1359. All other loop structures jump to either TOP or BOTTOM->next.
  1360. */
  1361. r = r->next; /* line after FOR */
  1362. r->position = 0;
  1363. #endif
  1364. line_skip_eol (r);
  1365. return r;
  1366. }
  1367. /*
  1368. --------------------------------------------------------------------------------------------
  1369. STATIC UTILITY ROUTINES
  1370. --------------------------------------------------------------------------------------------
  1371. */
  1372. static int
  1373. FindTopLineOnStack (LineType * l)
  1374. {
  1375. /* since we are at the top of a loop, we MIGHT be on the stack */
  1376. StackType *StackItem;
  1377. assert (l != NULL);
  1378. assert(My != NULL);
  1379. for (StackItem = My->StackHead; StackItem != NULL;
  1380. StackItem = StackItem->next)
  1381. {
  1382. LineType *current;
  1383. current = StackItem->LoopTopLine;
  1384. if (current != NULL)
  1385. {
  1386. if (current == l)
  1387. {
  1388. /* FOUND */
  1389. while (My->StackHead != StackItem)
  1390. {
  1391. bwb_decexec ();
  1392. }
  1393. /* we are now the top item on the stack */
  1394. return TRUE;
  1395. }
  1396. /* do NOT cross a function/sub boundary */
  1397. switch (current->cmdnum)
  1398. {
  1399. case C_FUNCTION:
  1400. case C_SUB:
  1401. case C_GOSUB:
  1402. /* NOT FOUND */
  1403. return FALSE;
  1404. /* break; */
  1405. }
  1406. }
  1407. }
  1408. /* NOT FOUND */
  1409. return FALSE;
  1410. }
  1411. static LineType *
  1412. bwb_if_file (LineType * l, int ThenValue)
  1413. {
  1414. /* IF END # filenumber THEN linenumber */
  1415. /* IF MORE # filenumber THEN linenumber */
  1416. int Value;
  1417. int FileNumber;
  1418. assert (l != NULL);
  1419. if (line_skip_FilenumChar (l))
  1420. {
  1421. /* IF END # */
  1422. FileType *F;
  1423. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  1424. {
  1425. WARN_BAD_FILE_NUMBER;
  1426. return (l);
  1427. }
  1428. if (FileNumber < 0)
  1429. {
  1430. /* Printer is NOT EOF */
  1431. Value = FALSE;
  1432. }
  1433. else if (FileNumber == 0)
  1434. {
  1435. /* Console is NOT EOF */
  1436. Value = FALSE;
  1437. }
  1438. else
  1439. {
  1440. /* normal file */
  1441. F = find_file_by_number (FileNumber);
  1442. if (F == NULL)
  1443. {
  1444. WARN_BAD_FILE_NUMBER;
  1445. return (l);
  1446. }
  1447. /* if( TRUE ) */
  1448. {
  1449. /* actual file -- are we at the end? */
  1450. FILE *fp;
  1451. long current;
  1452. long total;
  1453. fp = F->cfp;
  1454. assert( fp != NULL );
  1455. current = ftell (fp);
  1456. fseek (fp, 0, SEEK_END);
  1457. total = ftell (fp);
  1458. if (total == current)
  1459. {
  1460. /* EOF */
  1461. Value = TRUE;
  1462. }
  1463. else
  1464. {
  1465. /* NOT EOF */
  1466. Value = FALSE;
  1467. fseek (fp, current, SEEK_SET);
  1468. }
  1469. }
  1470. }
  1471. }
  1472. else
  1473. {
  1474. WARN_SYNTAX_ERROR;
  1475. return (l);
  1476. }
  1477. if (Value == ThenValue)
  1478. {
  1479. /* expression is TRUE, take THEN path */
  1480. return bwb_then_else (l, TRUE);
  1481. }
  1482. /* expression is FALSE, take ELSE path */
  1483. return bwb_then_else (l, FALSE);
  1484. }
  1485. static LineType *
  1486. bwb_then_else (LineType * l, int Value)
  1487. {
  1488. /*
  1489. ... THEN 100
  1490. ... THEN 100 ELSE 200
  1491. The deciding expression has already been parsed and evaluated.
  1492. If Value != 0, then we want to take the THEN path.
  1493. If Value == 0, then we want to take the ELSE path.
  1494. */
  1495. int LineNumber;
  1496. LineType *x;
  1497. assert (l != NULL);
  1498. if (line_skip_seperator (l))
  1499. {
  1500. /* OK */
  1501. }
  1502. else
  1503. {
  1504. /* OPTIONAL */
  1505. }
  1506. if (line_skip_word (l, "THEN"))
  1507. {
  1508. /* OK */
  1509. }
  1510. else if (line_skip_word (l, "GOTO"))
  1511. {
  1512. /* OK */
  1513. }
  1514. else
  1515. {
  1516. /* REQUIRED */
  1517. WARN_SYNTAX_ERROR;
  1518. return (l);
  1519. }
  1520. /* read THEN's LineNumber */
  1521. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  1522. {
  1523. WARN_SYNTAX_ERROR;
  1524. return (l);
  1525. }
  1526. if (Value == 0)
  1527. {
  1528. /* expression is FALSE, take ELSE path */
  1529. if (line_is_eol (l))
  1530. {
  1531. /* OPTIONAL */
  1532. return (l);
  1533. }
  1534. if (line_skip_seperator (l))
  1535. {
  1536. /* OK */
  1537. }
  1538. else
  1539. {
  1540. /* OPTIONAL */
  1541. }
  1542. if (line_skip_word (l, "ELSE"))
  1543. {
  1544. /* OK */
  1545. }
  1546. else
  1547. {
  1548. /* REQUIRED */
  1549. WARN_SYNTAX_ERROR;
  1550. return (l);
  1551. }
  1552. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  1553. {
  1554. WARN_SYNTAX_ERROR;
  1555. return (l);
  1556. }
  1557. }
  1558. x = NULL;
  1559. #if THE_PRICE_IS_RIGHT
  1560. if (l->OtherLine != NULL)
  1561. {
  1562. /* look in the cache */
  1563. if (l->OtherLine->number == LineNumber)
  1564. {
  1565. x = l->OtherLine; /* found in cache */
  1566. }
  1567. }
  1568. #endif /* THE_PRICE_IS_RIGHT */
  1569. if (x == NULL)
  1570. {
  1571. x = find_line_number (LineNumber); /* bwb_then_else */
  1572. }
  1573. if (x != NULL)
  1574. {
  1575. line_skip_eol (l);
  1576. x->position = 0;
  1577. #if THE_PRICE_IS_RIGHT
  1578. l->OtherLine = x; /* save in cache */
  1579. #endif /* THE_PRICE_IS_RIGHT */
  1580. return x;
  1581. }
  1582. WARN_SYNTAX_ERROR;
  1583. return (l);
  1584. }
  1585. static int
  1586. IsTypeMismatch (char LeftTypeCode, char RightTypeCode)
  1587. {
  1588. if (LeftTypeCode == StringTypeCode && RightTypeCode == StringTypeCode)
  1589. {
  1590. /* both STRING */
  1591. return FALSE;
  1592. }
  1593. if (LeftTypeCode != StringTypeCode && RightTypeCode != StringTypeCode)
  1594. {
  1595. /* both NUMBER */
  1596. return FALSE;
  1597. }
  1598. /* TYPE MISMATCH */
  1599. return TRUE;
  1600. }
  1601. static int
  1602. for_limit_check (DoubleType Value, DoubleType Target, DoubleType Step)
  1603. {
  1604. if (Step > 0)
  1605. {
  1606. /* POSITIVE */
  1607. if (Value > Target)
  1608. {
  1609. /* FOR I = 3 TO 2 STEP 1 */
  1610. return TRUE;
  1611. }
  1612. }
  1613. else
  1614. {
  1615. /* NEGATIVE */
  1616. if (Value < Target)
  1617. {
  1618. /* FOR I = -3 TO -2 STEP -1 */
  1619. return TRUE;
  1620. }
  1621. }
  1622. return FALSE;
  1623. }
  1624. /* EOF */