-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdim.c
94 lines (79 loc) · 2.13 KB
/
dim.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
/* dim.c - dbasic numeric arrays
*
* part of dbasic
*
* (C) k theis <theis.kurt@gmail.com> 2022
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define _ISOC99_SOURCE
#include <math.h>
#include "dbasic.h"
#include <string.h>
int dim(char *line) {
extern float *NumBase[26];
extern float *NumVar[26];
extern int NumSize[26];
float eval(char *);
int memsize=0; // calculated array size for later
int dimsize=0; // user-supplied array size
char STRING[LINESIZE]; // holds user supplied array size
int n=0;
// format 100 dim a(1000)
while (isdigit(*line)) line++; // skip line number
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
if (isalpha(*line)) while (isalpha(*line)) line++; // skip 'dim'
while (1) {
if (isblank(*line)) while (isblank(*line)) line++; // skip spaces
if (*line == '\0' || *line == '\n') return 0;
if (*line == ',') {
line++; // skip commas
continue;
}
// should be pointing to variable to be re-dimmed
if (isalpha(*line)) {
void *temp;
char charvar = *line;
line++; // skip variable
if (*line != '(') {
printf("Error - Syntax Error in dim statement\n");
return -1;
}
line++; // skip (
// get the numeric value of all between ()
memset(STRING,0,LINESIZE);
n=0;
while (1) {
if (*line == ')') break;
STRING[n] = *line;
n++; line++;
if (n > LINESIZE) {
printf("Error - line too long in dim statement\n");
return -1;
}
}
dimsize = eval(STRING);
if (dimsize <= NumSize[charvar-'a']) { // trying to make array smaller
printf("Error - attempting to decrease array size\n");
return -1;
}
NumSize[charvar-'a'] = dimsize; // store change
memsize = dimsize*sizeof(float);
// see which variable to re-dim
temp = realloc(NumBase[charvar-'a'],memsize);
if (temp == NULL) {
printf("Error - out of memory\n");
return -1;
}
NumBase[charvar-'a'] = temp;
NumVar[charvar-'a'] = NumBase[charvar-'a'];
// dim statement executed
continue;
}
line++; // get next char
}
printf("Error - bad variable in dim statement\n");
return -1;
}