-
Notifications
You must be signed in to change notification settings - Fork 1
/
goto.c
68 lines (57 loc) · 1.54 KB
/
goto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* goto.c
*
* part of dbasic
*
* (C) k theis <theis.kurt@gmail.com> 2022
*
* GOTO and ON GOTO, GOSUB and ON GOSUB routines
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dbasic.h"
/* GOTO */
int run_goto(char *line) { // get line number, return it
char ln[LNSIZE], word[6], linenum[LNSIZE];
float eval(char *);
sscanf(line,"%s %s %s",ln,word,linenum);
/* return value of expression after 'goto' as the line number */
return ((int)(eval(linenum)));
}
/* ON n GOTO x y z */
int run_ongoto(char *line) { // return line number based on variable
/* tokenize all chars on the line, verify
* on, goto are correct, evaluate the variable,
* determine the line numbers and return the
* correct one or 0 if no match.
*/
//char *opers;
char *tmp;
char temp[30][LNSIZE]; // max 30 line numbers
int n=0, res=0;
float eval(char *);
/* tokenize the line */
tmp = strtok(line," ");
while ((tmp = strtok(NULL," "))!=NULL) { // break on NULL
if (tmp != NULL) strcpy(temp[n],tmp);
n++;
if (n > 30) {
printf("Error - max of 30 addresses allowed\n");
return -1;
}
}
// element 0 is 'on'
// element 1 is the variable
res = (int)(eval(temp[1]));
if (res > n) return 0; // only 30 line numbers allowed
// element2 is 'goto'
if (!(strcmp(temp[2],"goto")==0 || strcmp(temp[2],"gosub")==0)) {
printf("Error - expected GOTO/GOSUB in statement\n");
return -1;
}
// elements 3...(end) are line numbers
return atoi(temp[res+2]);
// if return value < 1 or > highest number, return 0
}