/
fornext.c
140 lines (115 loc) · 3.09 KB
/
fornext.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* fornext.c - run_for and run_next
*
* part of dbasic
*
* (C) k theis <theis.kurt@gmail.com> 2022
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int run_for(char *line) {
extern char index_var;
extern float index_start;
extern float index_end;
extern float index_step;
extern float *NumVar[26];
char expr[40] = {'\0'};
int n=0;
float eval(char *);
// debug
// printf("run_for: line [%s]\n",line);
while (isdigit(*line)) line++; // skip the line number
while (isblank(*line)) line++; // skip spaces
while (isalpha(*line)) line++; // skip FOR command
if (isblank(*line)) while (isblank(*line)) line++; // skip more spaces
/* get index variable NOTE: will always be subscript 0 x(0)*/
if (*line >= 'a' && *line <= 'z' && *(line+1) != '$' && *(line+1) != '(')
index_var = *line;
else {
printf("Error - undefined index variable %c\n",*line);
return -1;
}
line++; // skip index variable
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
/* test = */
if (*line != '=') {
printf("Error - missing = in for/next\n");
return -1;
} else
line++; // skip =
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
/* get first index value */
memset(expr,0,sizeof(expr)); n=0;
while (1) {
if (isblank(*line)) break;
expr[n] = *line;
n++; line++;
if (n>=40) {
printf("Error - expression too long\n");
return -1;
}
}
index_start = eval(expr);
/* set up index variable */
NumVar[index_var - 'a'][0] = index_start;
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
/* test TO */
if (*line == 't' && *(line+1) == 'o') {
line += 2;
} else {
printf("Error - missing TO in for/next\n");
return -1;
}
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
/* get second index variable */
memset(expr,0,sizeof(expr)); n=0;
while (1) {
if (isblank(*line) || *line == '\n' || *line == '\0') break;
expr[n] = *line;
n++; line++;
if (n>40) {
printf("Error - expression too long\n");
return -1;
}
}
index_end = eval(expr);
/* look for 'S' in STEP or \n for EOL */
while (1) {
line++;
if (*line == '\n') break;
if (*line == '\0') break;
if (*line == 's') break;
}
// if no STEP command, set index_step to 1
index_step = 1;
if (*line == '\n' || *line == '\0') return 0; // normal return, STEP not used
if (*line=='s' && *(line+1)=='t' && *(line+2)=='e' && *(line+3)=='p') {
line += 4; // skip STEP keyword
} else {
printf("Error - unknown chars in FOR/NEXT statement\n");
return -1;
}
// skip spaces, get step variable
if (isblank(*line)) while (isblank(*line)) line++;
memset(expr,0,sizeof(expr)); n=0;
while (1) {
if (*line == ' ' || *line == '\0' || *line == '\n') break;
expr[n] = *line;
if (n>40) {
printf("Error - expression too long\n");
return -1;
}
n++; line++;
}
index_step = eval(expr);
/* test line end char */
if (*line == '\n' || *line == '\0') return 0;
printf("Error - excess characters in FOR/NEXT statement\n");
return -1;
}
int run_next(char *line) {
printf("run_next: line [%s]\n",line);
return 0;
}