-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.c
153 lines (135 loc) · 3.43 KB
/
print.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* print.c
*
* part of dbasic
* (C) k theis <theis.kurt@gmail.com>
*
* BASIC PRINT statement
*
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define _ISOC99_SOURCE
#include <math.h>
#include "dbasic.h"
int run_print(char *line) {
char expr[LINESIZE];
int n=0;
float eval(char *);
extern char CharVars[][80];
extern char *evalstring(char *);
//extern char tempCharVar[MAXLINESIZE];
//extern float *NumVar[26];
extern int error;
extern FILE *fd[8];
extern int fdnumber;
int ISFILE=0; // flag for file operations
while (isdigit(*line++)); // skip past line number
if (isblank(*line)) while (isblank(*line)) line++;; // skip spaces
while (isalpha(*line++)); // skip 'print'
// test for file#
if (isblank(*line)) while (isblank(*line)) line++;
if (*line == '#') { // print to a file
ISFILE=1;
line++; // skip past #
if (!(isdigit(*line))) {
printf("Error - bad syntax in PRINT\n");
return -1;
}
fdnumber = atoi(line);
if (fdnumber < 1 || fdnumber > 7) { // 1-7 allowed
printf("Error - bad file number %d in PRINT\n",fdnumber);
return -1;
}
/* make sure it's already opened */
if (fd[fdnumber] == NULL) {
printf("Error - file number %d is not opened\n",fdnumber);
return -1;
}
line++; // skip file number
}
while (1) {
if (*line == '\0') break;
if (isblank(*line) || *line==';') { // ignore blanks (space and tabs) and ;
line++;
continue;
}
/* print all between double quotes */
if (*line == '"') {
line++; // skip initial quote
while (1) {
if (*line == '"') break;
if (*line == '\0') { // unequal quotes
printf("\nError - unequal quotes in PRINT. \n");
return -1;
}
if (!ISFILE) printf("%c",*line);
if (ISFILE) fprintf(fd[fdnumber],"%c",*line);
line++;
}
line++; // skip closing "
continue;
}
/* comma shows 3 spaces */
if (*line == ',') {
if (!ISFILE) printf(" ");
if (ISFILE) fprintf(fd[fdnumber]," ");
line++;
continue;
}
/* SPC() function */
if (*line == 's' && *(line+1)=='p' && *(line+2)=='c' && *(line+3)=='(') {
line += 4;
char TEMP[LINESIZE]={'\0'}; int n=0; int pc=0;
while (1) {
if (*line == ')' && pc == 0) break;
if (*line == '(') pc++;
if (*line == ')') pc--;
if (*line == '\0') {
printf("Error - syntax error in SPC()\n");
return -1;
}
TEMP[n] = *line;
n++; line++;
}
line++; // skip )
n = eval(TEMP);
for (int i=0; i<n; i++) {
if (!ISFILE) printf(" ");
if (ISFILE) fprintf(fd[fdnumber]," ");
}
continue;
}
/* strings a$ - z$ */
if (*line >= 'a' && *line <= 'z' && *(line+1) == '$') {
if (!ISFILE) printf("%s",CharVars[*line-'a']);
if (ISFILE) fprintf(fd[fdnumber],"%s",CharVars[*line-'a']);
line +=2;
continue;
}
// anything else is an expression
memset(expr,0,sizeof(expr)); n=0;
float res=0;
while (1) {
if (*line==',' || *line==';' || *line=='\0' || *line=='\n') break;
expr[n] = *line;
n++; line++;
}
/* test numeric expression */
res = eval(expr);
/* SPC() returns NAN - ignore this */
if (strncmp(expr,"spc(",4)==0 && isnan(res)) continue;
if (error == -1) {
printf("\nError - eval error in print()\n");
return -1;
}
if (!ISFILE) printf("%g",res);
if (ISFILE) fprintf(fd[fdnumber],"%g",res);
continue;
}
if (*(line-1) == ';') return 0;
if (!ISFILE) printf("\n");
if (ISFILE) fprintf(fd[fdnumber],"\n");
return 0;
}