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.
 
 
 
 
 
 

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