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.
 
 
 
 
 
 

1430 lines
41 KiB

  1. /***************************************************************
  2. bwb_stc.c Commands Related to Structured Programming
  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. /*---------------------------------------------------------------*/
  29. #include "bwbasic.h"
  30. /* prototypes */
  31. static int fslt_clear(void);
  32. static int scan_readargs(LookupType * f, LineType * l);
  33. /***************************************************************
  34. FUNCTION: fslt_init()
  35. DESCRIPTION: This function initializes the FUNCTION-SUB
  36. lookup table.
  37. ***************************************************************/
  38. int
  39. fslt_init( void )
  40. {
  41. bwx_DEBUG(__FUNCTION__);
  42. My->fslt_head = NULL;
  43. return TRUE;
  44. }
  45. /***************************************************************
  46. FUNCTION: bwb_scan()
  47. DESCRIPTION: This function scans all lines of the
  48. program in memory and creates a FUNCTION-
  49. SUB lookup table (fslt) for the program.
  50. ***************************************************************/
  51. static int ErrorMessage( LineType *current )
  52. {
  53. char tbuf[64];
  54. switch (current->cmdnum)
  55. {
  56. case C_FOR:
  57. bwb_strcpy( tbuf, "FOR without NEXT" );
  58. break;
  59. case C_EXIT_FOR:
  60. bwb_strcpy( tbuf, "EXIT FOR without FOR" );
  61. break;
  62. case C_NEXT:
  63. bwb_strcpy( tbuf, "NEXT without FOR" );
  64. break;
  65. case C_DO:
  66. bwb_strcpy( tbuf, "DO without LOOP" );
  67. break;
  68. case C_EXIT_DO:
  69. bwb_strcpy( tbuf, "EXIT DO without DO" );
  70. break;
  71. case C_LOOP:
  72. bwb_strcpy( tbuf, "LOOP without DO" );
  73. break;
  74. case C_UNTIL:
  75. bwb_strcpy( tbuf, "UNTIL without UEND" );
  76. break;
  77. case C_EXIT_UNTIL:
  78. bwb_strcpy( tbuf, "EXIT UNTIL without UNTIL" );
  79. break;
  80. case C_UEND:
  81. bwb_strcpy( tbuf, "UEND without UNTIL" );
  82. break;
  83. case C_WHILE:
  84. bwb_strcpy( tbuf, "WHILE without WEND" );
  85. break;
  86. case C_EXIT_WHILE:
  87. bwb_strcpy( tbuf, "EXIT WHILE without WHILE" );
  88. break;
  89. case C_WEND:
  90. bwb_strcpy( tbuf, "WEND without WHILE" );
  91. break;
  92. case C_SUB:
  93. bwb_strcpy( tbuf, "SUB without END SUB" );
  94. break;
  95. case C_EXIT_SUB:
  96. bwb_strcpy( tbuf, "EXIT SUB without SUB" );
  97. break;
  98. case C_END_SUB:
  99. bwb_strcpy( tbuf, "END SUB without SUB" );
  100. break;
  101. case C_FUNCTION:
  102. bwb_strcpy( tbuf, "FUNCTION without END FUNCTION" );
  103. break;
  104. case C_EXIT_FUNCTION:
  105. bwb_strcpy( tbuf, "EXIT FUNCTION without FUNCTION" );
  106. break;
  107. case C_END_FUNCTION:
  108. bwb_strcpy( tbuf, "END FUNCTION without FUNCTION" );
  109. break;
  110. case C_IF_THEN:
  111. bwb_strcpy( tbuf, "IF THEN without END IF" );
  112. break;
  113. case C_ELSEIF:
  114. bwb_strcpy( tbuf, "ELSEIF without IF THEN" );
  115. break;
  116. case C_ELSE:
  117. bwb_strcpy( tbuf, "ELSE without IF THEN" );
  118. break;
  119. case C_END_IF:
  120. bwb_strcpy( tbuf, "END IF without IF THEN" );
  121. break;
  122. case C_SELECT_CASE:
  123. bwb_strcpy( tbuf, "SELECT CASE without END SELECT" );
  124. break;
  125. case C_CASE:
  126. bwb_strcpy( tbuf, "CASE without SELECT CASE" );
  127. break;
  128. case C_CASE_ELSE:
  129. bwb_strcpy( tbuf, "CASE ELSE without SELECT CASE" );
  130. break;
  131. case C_END_SELECT:
  132. bwb_strcpy( tbuf, "END SELECT without SELECT CASE" );
  133. break;
  134. default:
  135. bwb_strcpy( tbuf, "UNKNOWN COMMAND" );
  136. break;
  137. }
  138. sprintf( My->bwb_ebuf, "%s: %d %s\n", tbuf, current->number, current->buffer );
  139. prn_xprintf( My->bwb_ebuf );
  140. return FALSE;
  141. }
  142. static LineType * find_BottomLineInCode(LineType * l)
  143. {
  144. bwx_DEBUG(__FUNCTION__);
  145. if( l == NULL )
  146. {
  147. return NULL;
  148. }
  149. while( l->OtherLine != NULL )
  150. {
  151. switch( l->cmdnum )
  152. {
  153. case C_NEXT:
  154. case C_LOOP:
  155. case C_UEND:
  156. case C_WEND:
  157. case C_END_SUB:
  158. case C_END_FUNCTION:
  159. case C_END_IF:
  160. case C_END_SELECT:
  161. return l;
  162. }
  163. l = l->OtherLine;
  164. }
  165. /* l->OtherLine == NULL */
  166. return l;
  167. }
  168. static int MissingBottomLine( LineType * current, int cmdnum )
  169. {
  170. LineType * BottomLineInCode;
  171. BottomLineInCode = find_BottomLineInCode( current );
  172. if( BottomLineInCode == NULL )
  173. {
  174. return TRUE;
  175. }
  176. if( BottomLineInCode->cmdnum != cmdnum )
  177. {
  178. return TRUE;
  179. }
  180. return FALSE;
  181. }
  182. static LineType * FindParentCommand( int cmdnum, unsigned int Indention, LineType *Previous[ /* EXECLEVELS + 1 */ ] )
  183. {
  184. if( Indention > 0 )
  185. {
  186. unsigned int i;
  187. for( i = Indention - 1; /* i >= 0 */; i-- )
  188. {
  189. if( Previous[ i ]->cmdnum == cmdnum )
  190. {
  191. /* FOUND */
  192. return Previous[ i ]; /* EXIT_FOR->OtherLine == FOR */
  193. }
  194. if( i == 0 )
  195. {
  196. /* NOT FOUND */
  197. }
  198. }
  199. }
  200. /* NOT FOUND */
  201. return NULL;
  202. }
  203. int
  204. bwb_scan(void)
  205. {
  206. /*
  207. STATIC ANALYSIS
  208. Background.
  209. This routine began as a way to record the line numbers associated with the cmdnum of FUNCTION, SUB, or LABEL.
  210. Pretty-printing.
  211. Indention was added for pretty-printing by LIST, based upon the cmdnum (Indetion++, Indention--).
  212. Mismatched structured commands.
  213. When reviewing a properly indented listing, mismatched structured commands are easier to visually indentify
  214. (FOR without NEXT and so on), so Previous[] was added to record the previous cmdnum at a given Indention.
  215. Comparing Current->cmdnum with Previous->cmdnum allows mismatched structured commands to be detected.
  216. Reduce stack usage for structured loops.
  217. OtherLine, which was previously determined at runtime for loops, could now be determined during the scan.
  218. Previously all loops used the stack so the EXIT command could find the loop's bottom line.
  219. The EXIT commands could now look in Previous[] to determine their loop's top line and follow that to the loop's bottom line.
  220. As a result, now the FOR loops use the stack, but all other loops do not.
  221. Reduce stack usaage for structured IF/SELECT.
  222. Previuosly the structured IF/SELECT command used the stack to hold the results of comparisons and intermediate values.
  223. OtherLine is now used to link these commands to their next occurring command.
  224. As a result, the path thru the structure is now chosen at the IF/SELECT command, and the stack is no longer used.
  225. The RESUME command knows about this linkage, so a simple "RESUME" jumps to the "IF THEN" or "SELECT CASE"
  226. and "RESUME NEXT" jumps to the "END IF" or "END SELECT".
  227. Caching for unstructured commands.
  228. OtherLine was not previously used for any purpose for the unstructured GOTO, GOSUB, IF and ON commands.
  229. It is now used to cache the line last executed by these commands to reduce the time required to find the target line.
  230. The cache reduces execution time because the target line is usually (but not always) the same.
  231. For the simple commands "GOTO 100", "GOSUB 100" and "IF x THEN 100", the cache will always succeed.
  232. For the command "IF x THEN 100 ELSE 200", the cache will succeed for the last taken path.
  233. Because programs are typically written so one path is more likely, the cache usually succeeds.
  234. For the "ON x GOTO ..." and "ON x GOSUB ...", the cache succeeds when the result of the test expression repeats, such as:
  235. FOR I = 1 TO 100
  236. ON INT(I/10) GOSUB ...
  237. NEXT I
  238. In this example, the cache will succeed 90 times and fail 10 times.
  239. Checking FOR/NEXT variable names.
  240. If a variable name is provided for a NEXT command, then it is compared against the variable name of the matching FOR command.
  241. This detects the following kind of mismatch:
  242. FOR I = ...
  243. NEXT J
  244. Linking DATA commands.
  245. OtherLine was not previously used for any purpose for the DATA commands.
  246. It is now used to point to the next DATA command after the current DATA command.
  247. This reduces the time required for READ to find the next DATA command.
  248. The last DATA command has an OtherLine of NULL.
  249. RESTORE knows about the DATA command linkage and uses this information to find the specified DATA command.
  250. READ knows about the DATA command linkage and uses this information to find the next DATA command.
  251. */
  252. /*
  253. OtherLine is used for different purposes depending upon the command.
  254. For structured IF_THEN and SELECT_CASE, OtherLine is used to form a one-way list:
  255. IF_THEN->OtherLine == next occuring ELSE_IF, ELSE, END_IF
  256. ELSE_IF->Otherline == next occuring ELSE_IF, ELSE, END_IF
  257. ELSE->OtherLine == END_IF
  258. END_IF->OtherLine == NULL
  259. For the structured loops, OtherLine is uses as a circular list:
  260. WHILE->OtherLine == WEND
  261. EXIT_WHILE->OtherLine == WHILE
  262. WEND->OtherLine == WHILE
  263. For unstructured commands, OtherLine is used as a one-entry cache.
  264. It contains a pointer to the Most Recently Used line returned by the command:
  265. GOTO->OtherLine == MRU target line
  266. GOSUB->OtherLine == MRU target line
  267. IF->OtherLine == MRU target line
  268. ON->OtherLine == MRU target line
  269. For other commands, OtherLine is not used.
  270. */
  271. LineType *current;
  272. unsigned int Indention;
  273. LineType *Previous[ EXECLEVELS + 1 ]; /* previous part of structured command */
  274. bwx_DEBUG(__FUNCTION__);
  275. if ( My->rescan != TRUE)
  276. {
  277. /* program is clean, no scan required */
  278. return TRUE;
  279. }
  280. /* program needs to be scanned again, because a line was added or deleted */
  281. /* reset these whenever a SCAN occurs */
  282. My->data_line = My->bwb_start.next;
  283. My->data_pos = My->data_line->Startpos;
  284. My->err_line = NULL;
  285. My->ContinueLine = NULL;
  286. /* first run through the FUNCTION - SUB loopkup table and free any existing memory */
  287. fslt_clear();
  288. for( Indention = 0; Indention <= EXECLEVELS; Indention++)
  289. {
  290. Previous[ Indention ] = NULL;
  291. }
  292. Indention = 0;
  293. for (current = My->bwb_start.next; current != &My->bwb_end; current = current->next)
  294. {
  295. current->OtherLine = NULL;
  296. switch (current->cmdnum)
  297. {
  298. case C_DEF:
  299. case C_FUNCTION:
  300. case C_SUB:
  301. case C_USER_LBL:
  302. fslt_add(current);
  303. }
  304. /* verify the 'current' command is consistent with a 'previous' command at a lower indention */
  305. switch (current->cmdnum)
  306. {
  307. case C_EXIT_FOR:
  308. current->OtherLine = FindParentCommand( C_FOR, Indention, Previous ); /* EXIT_FOR->OtherLine == FOR */
  309. if( current->OtherLine == NULL )
  310. {
  311. return ErrorMessage( current );
  312. }
  313. break;
  314. case C_EXIT_WHILE:
  315. current->OtherLine = FindParentCommand( C_WHILE, Indention, Previous ); /* EXIT_WHILE->OtherLine == WHILE */
  316. if( current->OtherLine == NULL )
  317. {
  318. return ErrorMessage( current );
  319. }
  320. break;
  321. case C_EXIT_UNTIL:
  322. current->OtherLine = FindParentCommand( C_UNTIL, Indention, Previous ); /* EXIT_UNTIL->OtherLine == UNTIL */
  323. if( current->OtherLine == NULL )
  324. {
  325. return ErrorMessage( current );
  326. }
  327. break;
  328. case C_EXIT_FUNCTION:
  329. current->OtherLine = FindParentCommand( C_FUNCTION, Indention, Previous ); /* EXIT_FUNCTION->OtherLine == FUNCTION */
  330. if( current->OtherLine == NULL )
  331. {
  332. return ErrorMessage( current );
  333. }
  334. break;
  335. case C_EXIT_SUB:
  336. current->OtherLine = FindParentCommand( C_SUB, Indention, Previous ); /* EXIT_SUB->OtherLine == SUB */
  337. if( current->OtherLine == NULL )
  338. {
  339. return ErrorMessage( current );
  340. }
  341. break;
  342. case C_EXIT_DO:
  343. current->OtherLine = FindParentCommand( C_DO, Indention, Previous ); /* EXIT_DO->OtherLine == DO */
  344. if( current->OtherLine == NULL )
  345. {
  346. return ErrorMessage( current );
  347. }
  348. break;
  349. }
  350. /* verify the 'current' command is consistent with a 'previous' command at the same indention */
  351. switch (current->cmdnum)
  352. {
  353. case C_NEXT:
  354. if( Indention == 0 )
  355. {
  356. return ErrorMessage( current );
  357. }
  358. Indention--;
  359. switch( Previous[ Indention ]->cmdnum )
  360. {
  361. case C_FOR:
  362. /* if( TRUE ) */
  363. {
  364. /* compare the 'NEXT' variable with the 'FOR' variable */
  365. current->position = current->Startpos;
  366. Previous[ Indention ]->position = Previous[ Indention ]->Startpos;
  367. if( line_is_eol( current ) )
  368. {
  369. /* NEXT */
  370. /* there is no variable to compare */
  371. }
  372. else
  373. {
  374. /* NEXT variable */
  375. char NextVarName[ BasicNameLengthMax + 1 ];
  376. char ForVarName[ BasicNameLengthMax + 1 ];
  377. if( line_read_varname( current, NextVarName ) == FALSE)
  378. {
  379. return ErrorMessage( current );
  380. }
  381. if( line_read_varname( Previous[ Indention ], ForVarName ) == FALSE)
  382. {
  383. return ErrorMessage( current );
  384. }
  385. if( bwb_stricmp( ForVarName, NextVarName ) != 0 )
  386. {
  387. return ErrorMessage( current );
  388. }
  389. }
  390. /* MATCHED */
  391. current->Startpos = current->position;
  392. Previous[ Indention ]->position = Previous[ Indention ]->Startpos;
  393. }
  394. /* OK */
  395. Previous[ Indention ]->OtherLine = current; /* FOR->OtherLine = NEXT */
  396. current->OtherLine = Previous[ Indention ]; /* NEXT->OtherLine = FOR */
  397. Previous[ Indention ] = current; /* last command at this level = NEXT */
  398. break;
  399. default:
  400. return ErrorMessage( current );
  401. }
  402. break;
  403. case C_LOOP:
  404. if( Indention == 0 )
  405. {
  406. return ErrorMessage( current );
  407. }
  408. Indention--;
  409. switch( Previous[ Indention ]->cmdnum )
  410. {
  411. case C_DO:
  412. /* OK */
  413. Previous[ Indention ]->OtherLine = current; /* DO->OtherLine = LOOP */
  414. current->OtherLine = Previous[ Indention ]; /* LOOP->OtherLine = DO */
  415. Previous[ Indention ] = current; /* last command at this level = LOOP */
  416. break;
  417. default:
  418. return ErrorMessage( current );
  419. }
  420. break;
  421. case C_UEND:
  422. if( Indention == 0 )
  423. {
  424. return ErrorMessage( current );
  425. }
  426. Indention--;
  427. switch( Previous[ Indention ]->cmdnum )
  428. {
  429. case C_UNTIL:
  430. /* OK */
  431. Previous[ Indention ]->OtherLine = current; /* UNTIL->OtherLine = UEND */
  432. current->OtherLine = Previous[ Indention ]; /* UEND->OtherLine = UNTIL */
  433. Previous[ Indention ] = current; /* last command at this level = UEND */
  434. break;
  435. default:
  436. return ErrorMessage( current );
  437. }
  438. break;
  439. case C_WEND:
  440. if( Indention == 0 )
  441. {
  442. sprintf( My->bwb_ebuf, "Unmatched command %d %s", current->number, current->buffer );
  443. prn_xprintf( My->bwb_ebuf );
  444. return FALSE;
  445. }
  446. Indention--;
  447. switch( Previous[ Indention ]->cmdnum )
  448. {
  449. case C_WHILE:
  450. /* OK */
  451. Previous[ Indention ]->OtherLine = current; /* WHILE->OtherLine = WEND */
  452. current->OtherLine = Previous[ Indention ]; /* WEND->OtherLine = WHILE */
  453. Previous[ Indention ] = current; /* last command at this level = WEND */
  454. break;
  455. default:
  456. return ErrorMessage( current );
  457. }
  458. break;
  459. case C_END_SUB:
  460. if( Indention == 0 )
  461. {
  462. return ErrorMessage( current );
  463. }
  464. Indention--;
  465. switch( Previous[ Indention ]->cmdnum )
  466. {
  467. case C_SUB:
  468. /* OK */
  469. Previous[ Indention ]->OtherLine = current; /* SUB->OtherLine = END_SUB */
  470. current->OtherLine = Previous[ Indention ]; /* END_SUB->OtherLine = SUB */
  471. Previous[ Indention ] = current; /* last command at this level = END_SUB */
  472. break;
  473. default:
  474. return ErrorMessage( current );
  475. }
  476. break;
  477. case C_END_FUNCTION:
  478. if( Indention == 0 )
  479. {
  480. return ErrorMessage( current );
  481. }
  482. Indention--;
  483. switch( Previous[ Indention ]->cmdnum )
  484. {
  485. case C_FUNCTION:
  486. /* OK */
  487. Previous[ Indention ]->OtherLine = current; /* FUNCTION->OtherLine = END_FUNCTION */
  488. current->OtherLine = Previous[ Indention ]; /* END_FUNCTION->OtherLine = FUNCTION */
  489. Previous[ Indention ] = current; /* last command at this level = END_FUNCTION */
  490. break;
  491. default:
  492. return ErrorMessage( current );
  493. }
  494. break;
  495. case C_ELSEIF:
  496. if( Indention == 0 )
  497. {
  498. return ErrorMessage( current );
  499. }
  500. Indention--;
  501. switch( Previous[ Indention ]->cmdnum )
  502. {
  503. case C_IF_THEN:
  504. /* OK */
  505. Previous[ Indention ]->OtherLine = current; /* IF_THEN->OtherLine = ELSEIF */
  506. Previous[ Indention ] = current; /* last command at this level = ELSEIF */
  507. break;
  508. case C_ELSEIF:
  509. /* OK */
  510. Previous[ Indention ]->OtherLine = current; /* ELSEIF->OtherLine = ELSEIF */
  511. Previous[ Indention ] = current; /* last command at this level = ELSEIF */
  512. break;
  513. default:
  514. return ErrorMessage( current );
  515. }
  516. break;
  517. case C_ELSE:
  518. if( Indention == 0 )
  519. {
  520. return ErrorMessage( current );
  521. }
  522. Indention--;
  523. switch( Previous[ Indention ]->cmdnum )
  524. {
  525. case C_IF_THEN:
  526. /* OK */
  527. Previous[ Indention ]->OtherLine = current; /* IF_THEN->OtherLine = ELSE */
  528. Previous[ Indention ] = current; /* last command at this level = ELSE */
  529. break;
  530. case C_ELSEIF:
  531. /* OK */
  532. Previous[ Indention ]->OtherLine = current; /* ELSEIF->OtherLine = ELSE */
  533. Previous[ Indention ] = current; /* last command at this level = ELSE */
  534. break;
  535. default:
  536. return ErrorMessage( current );
  537. }
  538. break;
  539. case C_END_IF:
  540. if( Indention == 0 )
  541. {
  542. return ErrorMessage( current );
  543. }
  544. Indention--;
  545. switch( Previous[ Indention ]->cmdnum )
  546. {
  547. case C_IF_THEN:
  548. /* OK */
  549. Previous[ Indention ]->OtherLine = current; /* IF_THEN->OtherLine = END_IF */
  550. Previous[ Indention ] = current; /* last command at this level = END_IF */
  551. break;
  552. case C_ELSEIF:
  553. /* OK */
  554. Previous[ Indention ]->OtherLine = current; /* ELSEIF->OtherLine = END_IF */
  555. Previous[ Indention ] = current; /* last command at this level = END_IF */
  556. break;
  557. case C_ELSE:
  558. /* OK */
  559. Previous[ Indention ]->OtherLine = current; /* ELSE->OtherLine = END_IF */
  560. Previous[ Indention ] = current; /* last command at this level = END_IF */
  561. break;
  562. default:
  563. return ErrorMessage( current );
  564. }
  565. break;
  566. case C_CASE:
  567. if( Indention == 0 )
  568. {
  569. return ErrorMessage( current );
  570. }
  571. Indention--;
  572. switch( Previous[ Indention ]->cmdnum )
  573. {
  574. case C_SELECT_CASE:
  575. /* OK */
  576. Previous[ Indention ]->OtherLine = current; /* C_SELECT_CASE->OtherLine = C_CASE */
  577. Previous[ Indention ] = current; /* last command at this level = C_CASE */
  578. break;
  579. case C_CASE:
  580. /* OK */
  581. Previous[ Indention ]->OtherLine = current; /* CASE->OtherLine = C_CASE */
  582. Previous[ Indention ] = current; /* last command at this level = C_CASE */
  583. break;
  584. default:
  585. return ErrorMessage( current );
  586. }
  587. break;
  588. case C_CASE_ELSE:
  589. if( Indention == 0 )
  590. {
  591. return ErrorMessage( current );
  592. }
  593. Indention--;
  594. switch( Previous[ Indention ]->cmdnum )
  595. {
  596. case C_CASE:
  597. /* OK */
  598. Previous[ Indention ]->OtherLine = current; /* CASE->OtherLine = C_CASE_ELSE */
  599. Previous[ Indention ] = current; /* last command at this level = C_CASE_ELSE */
  600. break;
  601. default:
  602. return ErrorMessage( current );
  603. }
  604. break;
  605. case C_END_SELECT:
  606. if( Indention == 0 )
  607. {
  608. return ErrorMessage( current );
  609. }
  610. Indention--;
  611. switch( Previous[ Indention ]->cmdnum )
  612. {
  613. case C_CASE:
  614. /* OK */
  615. Previous[ Indention ]->OtherLine = current; /* CASE->OtherLine = END_SELECT */
  616. Previous[ Indention ] = current; /* last command at this level = END_SELECT */
  617. break;
  618. case C_CASE_ELSE:
  619. /* OK */
  620. Previous[ Indention ]->OtherLine = current; /* CASE_ELSE->OtherLine = END_SELECT */
  621. Previous[ Indention ] = current; /* last command at this level = END_SELECT */
  622. break;
  623. default:
  624. return ErrorMessage( current );
  625. }
  626. break;
  627. }
  628. /* OK */
  629. current->Indention = Indention;
  630. /* record the 'current' command as the 'previous' command at this indention */
  631. switch (current->cmdnum)
  632. {
  633. case C_FUNCTION:
  634. case C_SUB:
  635. /* this 'command' can NOT be inside the structure of another 'command' */
  636. if( Indention > 0 )
  637. {
  638. return ErrorMessage( Previous[ Indention - 1 ] );
  639. }
  640. case C_FOR:
  641. case C_DO:
  642. case C_UNTIL:
  643. case C_WHILE:
  644. case C_IF_THEN:
  645. case C_SELECT_CASE:
  646. if( Previous[ Indention ] != NULL )
  647. {
  648. /* we are NOT the first command at this indention level */
  649. /* verify the 'previous' command at this level was properly closed */
  650. switch( Previous[ Indention ]->cmdnum )
  651. {
  652. case C_FOR:
  653. case C_DO:
  654. case C_UNTIL:
  655. case C_WHILE:
  656. case C_FUNCTION:
  657. case C_SUB:
  658. case C_IF_THEN:
  659. case C_ELSEIF:
  660. case C_ELSE:
  661. case C_SELECT_CASE:
  662. case C_CASE:
  663. case C_CASE_ELSE:
  664. /* there is an existing unclosed structure */
  665. return ErrorMessage( Previous[ Indention ] );
  666. }
  667. }
  668. Previous[ Indention ] = current;
  669. Indention++;
  670. if( Indention == EXECLEVELS )
  671. {
  672. prn_xprintf( "Program is nested too deep" );
  673. return FALSE;
  674. }
  675. Previous[ Indention ] = NULL;
  676. break;
  677. case C_ELSEIF:
  678. case C_ELSE:
  679. case C_CASE:
  680. case C_CASE_ELSE:
  681. /*
  682. Previous[ Indention ] was already checked and assigned above, just indent.
  683. */
  684. Indention++;
  685. if( Indention == EXECLEVELS )
  686. {
  687. prn_xprintf( "Program is nested too deep" );
  688. return FALSE;
  689. }
  690. Previous[ Indention ] = NULL;
  691. break;
  692. }
  693. }
  694. if( Indention > 0 )
  695. {
  696. return ErrorMessage( Previous[ Indention - 1 ] );
  697. }
  698. /* verify the OtherLine chain terminates correctly; we should find the bottom command */
  699. for (current = My->bwb_start.next; current != &My->bwb_end; current = current->next)
  700. {
  701. switch(current->cmdnum)
  702. {
  703. case C_FOR:
  704. case C_EXIT_FOR:
  705. if( MissingBottomLine( current, C_NEXT ) )
  706. {
  707. return ErrorMessage( current );
  708. }
  709. break;
  710. case C_DO:
  711. case C_EXIT_DO:
  712. if( MissingBottomLine( current, C_LOOP ) )
  713. {
  714. return ErrorMessage( current );
  715. }
  716. break;
  717. case C_UNTIL:
  718. case C_EXIT_UNTIL:
  719. if( MissingBottomLine( current, C_UEND ) )
  720. {
  721. return ErrorMessage( current );
  722. }
  723. break;
  724. case C_WHILE:
  725. case C_EXIT_WHILE:
  726. if( MissingBottomLine( current, C_WEND ) )
  727. {
  728. return ErrorMessage( current );
  729. }
  730. break;
  731. case C_FUNCTION:
  732. case C_EXIT_FUNCTION:
  733. if( MissingBottomLine( current, C_END_FUNCTION ) )
  734. {
  735. return ErrorMessage( current );
  736. }
  737. break;
  738. case C_SUB:
  739. case C_EXIT_SUB:
  740. if( MissingBottomLine( current, C_END_SUB ) )
  741. {
  742. return ErrorMessage( current );
  743. }
  744. break;
  745. case C_IF_THEN:
  746. if( MissingBottomLine( current, C_END_IF ) )
  747. {
  748. return ErrorMessage( current );
  749. }
  750. break;
  751. case C_SELECT_CASE:
  752. if( MissingBottomLine( current, C_END_SELECT ) )
  753. {
  754. return ErrorMessage( current );
  755. }
  756. break;
  757. }
  758. }
  759. /* return */
  760. My->rescan = FALSE;
  761. return TRUE;
  762. }
  763. /***************************************************************
  764. FUNCTION: fslt_clear()
  765. DESCRIPTION: This C function clears all existing memory
  766. in the FUNCTION-SUB lookup table.
  767. ***************************************************************/
  768. static int fslt_clear(void)
  769. {
  770. LookupType *current;
  771. bwx_DEBUG(__FUNCTION__);
  772. /* run through table and clear memory */
  773. for (current = My->fslt_head; current != NULL; )
  774. {
  775. LookupType *next;
  776. next = current->next;
  777. /* check for local variables and free them */
  778. if( current->local_variable != NULL )
  779. {
  780. var_free( current->local_variable );
  781. current->local_variable = NULL;
  782. }
  783. /* Revised to FREE pass-thru calls by JBV */
  784. if (current->name != NULL)
  785. {
  786. FREE(current->name, "fslt_clear"); /* JBV */
  787. current->name = NULL; /* JBV */
  788. }
  789. FREE(current, "fslt_clear");
  790. current = next;
  791. }
  792. My->fslt_head = NULL;
  793. return TRUE;
  794. }
  795. /***************************************************************
  796. FUNCTION: fslt_add()
  797. DESCRIPTION: This C function adds an entry to the
  798. FUNCTION-SUB lookup table.
  799. ***************************************************************/
  800. int
  801. fslt_add(LineType * l /* , int *position , int code */ )
  802. {
  803. char *name;
  804. LookupType *f;
  805. char TypeChar;
  806. char varname[BasicNameLengthMax + 1];
  807. bwx_DEBUG(__FUNCTION__);
  808. /* get the element for name */
  809. switch (l->cmdnum)
  810. {
  811. case C_DEF:
  812. case C_FUNCTION:
  813. case C_SUB:
  814. l->position = l->Startpos;
  815. if( line_read_varname(l, varname) == FALSE )
  816. {
  817. WARN_SYNTAX_ERROR;
  818. return FALSE;
  819. }
  820. break;
  821. case C_USER_LBL:
  822. l->position = 0;
  823. if( line_read_label(l, varname) == FALSE )
  824. {
  825. WARN_SYNTAX_ERROR;
  826. return FALSE;
  827. }
  828. l->position = l->Startpos;
  829. break;
  830. default:
  831. WARN_SYNTAX_ERROR;
  832. return FALSE;
  833. }
  834. line_skip_spaces( l );
  835. /* get memory for name buffer */
  836. /* Revised to CALLOC pass-thru call by JBV */
  837. if ((name = CALLOC(1, bwb_strlen(varname) + 1, "fslt_add")) == NULL)
  838. {
  839. WARN_OUT_OF_MEMORY;
  840. return FALSE;
  841. }
  842. bwb_strcpy(name, varname);
  843. /* get memory for fslt structure */
  844. if ((f = CALLOC(1, sizeof(LookupType), "fslt_add")) == NULL)
  845. {
  846. WARN_OUT_OF_MEMORY;
  847. return FALSE;
  848. }
  849. /* fill in structure */
  850. f->line = l;
  851. f->name = name;
  852. f->local_variable = NULL;
  853. f->ParameterCount = 0; /* 0..32, 255 == VARIAMT */
  854. f->ParameterTypes = 0; /* bit 0 is first parameter */
  855. f->startpos = l->position;
  856. /* read arguments */
  857. switch (l->cmdnum)
  858. {
  859. case C_DEF:
  860. case C_FUNCTION:
  861. case C_SUB:
  862. TypeChar = var_nametype( varname );
  863. if ( line_peek_char( l, BasicLeftParenChar) )
  864. {
  865. if( scan_readargs(f, l) )
  866. {
  867. f->startpos = l->position;
  868. }
  869. }
  870. /* determine function type */
  871. if( TypeChar == BasicNulChar )
  872. {
  873. /* function has no explicit type char */
  874. TypeChar = line_read_type_declaration(l);
  875. if( TypeChar == BasicNulChar )
  876. {
  877. /* function has no declared type */
  878. TypeChar = My->DefaultVariableType[VarTypeIndex(varname[0])];
  879. }
  880. }
  881. break;
  882. case C_USER_LBL:
  883. TypeChar = BasicLongSuffix;
  884. break;
  885. default:
  886. WARN_SYNTAX_ERROR;
  887. return FALSE;
  888. }
  889. /* establish linkages */
  890. f->next = My->fslt_head;
  891. My->fslt_head = f;
  892. fnc_add_deffn(f, TypeChar);
  893. return TRUE;
  894. }
  895. /***************************************************************
  896. FUNCTION: scan_readargs()
  897. DESCRIPTION: This C function reads arguments (variable
  898. names for an entry added to the FUNCTION-
  899. SUB lookup table.
  900. ***************************************************************/
  901. static int scan_readargs(LookupType * f, LineType * l)
  902. {
  903. bwx_DEBUG(__FUNCTION__);
  904. f->ParameterCount = 0; /* 0..254, 255 == ... */
  905. f->ParameterTypes = 0; /* bit 0 is first parameter */
  906. /* we should be at begin paren */
  907. if ( line_skip_char(l, BasicLeftParenChar) == FALSE)
  908. {
  909. WARN_SYNTAX_ERROR;
  910. return FALSE;
  911. }
  912. if( line_skip_char(l, BasicRightParenChar) )
  913. {
  914. /* end of NO argument list */
  915. /* FUNCTION ABC() */
  916. return TRUE;
  917. }
  918. if (line_skip_word(l, "...") )
  919. {
  920. /* FUNCTION FRED( ... ) */
  921. if ( line_skip_char( l, BasicRightParenChar))
  922. {
  923. f->ParameterCount = 0xFF; /* VARIANT */
  924. f->ParameterTypes = 0;
  925. return TRUE;
  926. }
  927. WARN_SYNTAX_ERROR;
  928. return FALSE;
  929. }
  930. /* loop through looking for arguments */
  931. do
  932. {
  933. VariableType *v;
  934. char TypeChar;
  935. char varname[BasicNameLengthMax + 1];
  936. /* presume beginning of argument == variable name */
  937. if( line_read_varname(l, varname ) == FALSE)
  938. {
  939. WARN_SYNTAX_ERROR;
  940. return FALSE;
  941. }
  942. /* determine variable type */
  943. TypeChar = var_nametype( varname );
  944. if( TypeChar == BasicNulChar )
  945. {
  946. /* variable has no explicit type char */
  947. TypeChar = line_read_type_declaration(l);
  948. if( TypeChar == BasicNulChar )
  949. {
  950. /* variable has no declared type */
  951. TypeChar = My->DefaultVariableType[VarTypeIndex(varname[0])];
  952. }
  953. }
  954. /* initialize the variable and add it to local chain */
  955. v = var_new(varname, TypeChar );
  956. fslt_addlocalvar(f, v);
  957. if ( VAR_IS_STRING( v ) )
  958. {
  959. f->ParameterTypes |= (1 << f->ParameterCount);
  960. }
  961. f->ParameterCount++; /* 0..32, 255 == VARIANT */
  962. if (f->ParameterCount > 32)
  963. {
  964. /* should have been declared VARIANT */
  965. WARN_SYNTAX_ERROR;
  966. return FALSE;
  967. }
  968. }
  969. while( line_skip_comma(l) );
  970. if( line_skip_char(l, BasicRightParenChar) )
  971. {
  972. /* end of argument list */
  973. return TRUE;
  974. }
  975. /* FUNCTION ABC( A$, B$, */
  976. WARN_SYNTAX_ERROR;
  977. return FALSE;
  978. }
  979. /***************************************************************
  980. FUNCTION: fslt_addlocalvar()
  981. DESCRIPTION: This function adds a local variable
  982. to the FUNCTION-SUB lookup table at
  983. a specific level.
  984. ***************************************************************/
  985. int
  986. fslt_addlocalvar(LookupType * f, VariableType * v)
  987. {
  988. bwx_DEBUG(__FUNCTION__);
  989. /* find end of local chain */
  990. if (f->local_variable == NULL)
  991. {
  992. f->local_variable = v;
  993. }
  994. else
  995. {
  996. VariableType *p;
  997. VariableType *c;
  998. p = f->local_variable;
  999. for (c = f->local_variable->next; c != NULL; c = c->next)
  1000. {
  1001. p = c;
  1002. }
  1003. p->next = v;
  1004. }
  1005. v->next = NULL;
  1006. return TRUE;
  1007. }
  1008. LineType *
  1009. bwb_USER_LBL(LineType * l)
  1010. {
  1011. bwx_DEBUG(__FUNCTION__);
  1012. line_skip_eol(l);
  1013. return bwb_zline(l);
  1014. }
  1015. /***************************************************************
  1016. FUNCTION: bwb_def()
  1017. DESCRIPTION: This C function implements the BASIC
  1018. DEF statement.
  1019. SYNTAX: DEF FNname(arg...)] = expression
  1020. NOTE: It is not a strict requirement that the
  1021. function name should begin with "FN".
  1022. ***************************************************************/
  1023. LineType *
  1024. bwb_DEF(LineType * l)
  1025. {
  1026. bwx_DEBUG(__FUNCTION__);
  1027. /* this line will be executed by fnc_deffn() in bwb_fnc.c */
  1028. line_skip_eol(l);
  1029. return bwb_zline(l);
  1030. }
  1031. /***************************************************************
  1032. FUNCTION: bwb_call()
  1033. DESCRIPTION: This C function implements the BASIC
  1034. CALL subroutine command.
  1035. SYNTAX: CALL subroutine-name( param1, param2 )
  1036. ***************************************************************/
  1037. LineType *
  1038. bwb_CALL(LineType * l)
  1039. {
  1040. VariantType x; /* no leaks */
  1041. VariantType * X = &x; /* no leaks */
  1042. bwx_DEBUG(__FUNCTION__);
  1043. CLEAR_VARIANT( X );
  1044. /* Call the expression interpreter to evaluate the function */
  1045. if( line_read_expression( l, X ) == FALSE )
  1046. {
  1047. WARN_SYNTAX_ERROR;
  1048. goto EXIT;
  1049. }
  1050. EXIT:
  1051. RELEASE( X );
  1052. return bwb_zline(l);
  1053. }
  1054. /***************************************************************
  1055. FUNCTION: find_label()
  1056. DESCRIPTION: This C function finds a program line that
  1057. begins with the label included in <buffer>.
  1058. ***************************************************************/
  1059. LineType *
  1060. find_line_number( int number, int IsCache )
  1061. {
  1062. /* LABELS are resolved to their line number by the expresson parser */
  1063. if (number > 0)
  1064. {
  1065. LineType *x;
  1066. LineType *StartHere;
  1067. /* this is the default */
  1068. StartHere = &My->bwb_start;
  1069. #if THE_PRICE_IS_RIGHT
  1070. if( IsCache )
  1071. {
  1072. /*
  1073. This is a brute force search.
  1074. We can reduce the time required by using the various
  1075. existing pointers to start closer to the target.
  1076. Note that these pointers usually have NOTHING to do with the
  1077. line we are searching for.
  1078. We want to use the closest pointer that is BEFORE the target.
  1079. We can NOT use '==' because of multi-statement lines.
  1080. We do NOT require these pointers. We are only using
  1081. them because they ALREADY exist for other purposes.
  1082. */
  1083. LineType *CheckHere;
  1084. /* see if the 'THIS' pointer HAPPENS to be closer */
  1085. CheckHere = My->ThisLine;
  1086. if( CheckHere != NULL )
  1087. {
  1088. if( CheckHere->LineFlags & LINE_NUMBERED && CheckHere->number == number )
  1089. {
  1090. /* exact match */
  1091. return CheckHere;
  1092. }
  1093. if( CheckHere->number > StartHere->number && CheckHere->number < number )
  1094. {
  1095. /* it is closer than where we are now */
  1096. StartHere = CheckHere;
  1097. }
  1098. }
  1099. /* see if the 'DATA' pointer HAPPENS to be closer */
  1100. CheckHere = My->data_line;
  1101. if( CheckHere != NULL )
  1102. {
  1103. if( CheckHere->LineFlags & LINE_NUMBERED && CheckHere->number == number )
  1104. {
  1105. /* exact match */
  1106. return CheckHere;
  1107. }
  1108. if( CheckHere->number > StartHere->number && CheckHere->number < number )
  1109. {
  1110. /* it is closer than where we are now */
  1111. StartHere = CheckHere;
  1112. }
  1113. }
  1114. /* see if the 'ERL' pointer HAPPENS to be closer */
  1115. CheckHere = My->err_line;
  1116. if( CheckHere != NULL )
  1117. {
  1118. if( CheckHere->LineFlags & LINE_NUMBERED && CheckHere->number == number )
  1119. {
  1120. /* exact match */
  1121. return CheckHere;
  1122. }
  1123. if( CheckHere->number > StartHere->number && CheckHere->number < number )
  1124. {
  1125. /* it is closer than where we are now */
  1126. StartHere = CheckHere;
  1127. }
  1128. }
  1129. /* see if any pointer on the stack HAPPENS to be closer */
  1130. if( My->stack_head != NULL )
  1131. {
  1132. StackType * stack_item;
  1133. for (stack_item = My->stack_head; stack_item != NULL; stack_item = stack_item->next)
  1134. {
  1135. CheckHere = stack_item->line;
  1136. if( CheckHere != NULL )
  1137. {
  1138. if( CheckHere->LineFlags & LINE_NUMBERED && CheckHere->number == number )
  1139. {
  1140. /* exact match */
  1141. return CheckHere;
  1142. }
  1143. if( CheckHere->number > StartHere->number && CheckHere->number < number )
  1144. {
  1145. /* it is closer than where we are now */
  1146. StartHere = CheckHere;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. }
  1152. #endif
  1153. /* brute force search */
  1154. for (x = StartHere; x != &My->bwb_end; x = x->next)
  1155. {
  1156. if (x->number == number)
  1157. {
  1158. /* FOUND */
  1159. return x;
  1160. }
  1161. #if THE_PRICE_IS_RIGHT
  1162. if( IsCache )
  1163. {
  1164. /* see if the 'OtherLine' pointer HAPPENS to be closer */
  1165. LineType *CheckHere;
  1166. CheckHere = x->OtherLine;
  1167. if( CheckHere != NULL )
  1168. {
  1169. if( CheckHere->LineFlags & LINE_NUMBERED && CheckHere->number == number )
  1170. {
  1171. /* exact match */
  1172. return CheckHere;
  1173. }
  1174. if( CheckHere->number > x->number && CheckHere->number < number )
  1175. {
  1176. /* it is closer than where we are now */
  1177. x = CheckHere;
  1178. }
  1179. }
  1180. }
  1181. #endif
  1182. }
  1183. }
  1184. /* NOT FOUND */
  1185. WARN_UNDEFINED_LINE;
  1186. return NULL;
  1187. }
  1188. BasicNumberType
  1189. VerifyNumeric(BasicNumberType Value)
  1190. {
  1191. bwx_DEBUG(__FUNCTION__);
  1192. /* check Value */
  1193. if (isnan(Value))
  1194. {
  1195. /*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
  1196. WARN_INTERNAL_ERROR;
  1197. return 0;
  1198. }
  1199. else
  1200. if (isinf(Value))
  1201. {
  1202. /* - Evaluation of an expression results in an overflow
  1203. * (nonfatal, the recommended recovery procedure is to supply
  1204. * machine in- finity with the algebraically correct sign and
  1205. * continue). */
  1206. if (Value < 0)
  1207. {
  1208. Value = -DBL_MAX;
  1209. }
  1210. else
  1211. {
  1212. Value = DBL_MAX;
  1213. }
  1214. if( bwb_Warning_Overflow("*** Arithmetic Overflow ***") )
  1215. {
  1216. /* ERROR */
  1217. }
  1218. /* CONTINUE */
  1219. }
  1220. /* OK */
  1221. return Value;
  1222. }
  1223. VariableType *
  1224. var_chain(VariableType * argv)
  1225. {
  1226. /* create a variable chain */
  1227. VariableType *argn;
  1228. bwx_DEBUG(__FUNCTION__);
  1229. if (argv == NULL)
  1230. {
  1231. /* we are the first variable in the chain */
  1232. argn = (VariableType *) CALLOC(1, sizeof(VariableType), "var_chain");
  1233. }
  1234. else
  1235. {
  1236. /* find the end of the chain */
  1237. for (argn = argv; argn->next != NULL; argn = argn->next);
  1238. /* add ourself to the end */
  1239. argn->next = (VariableType *) CALLOC(1, sizeof(VariableType), "var_chain");
  1240. argn = argn->next;
  1241. }
  1242. argn->next = NULL;
  1243. /* return pointer to the variable just created */
  1244. return argn;
  1245. }
  1246. /* EOF */