-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint-bas.c
More file actions
382 lines (349 loc) · 11.9 KB
/
int-bas.c
File metadata and controls
382 lines (349 loc) · 11.9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* * PROJECT: BASIC++ (IB) Interpreter
* AUTHOR: BASIC++ Community
* FILENAME: ib.c
* VERSION: 1.0.0
*
* just a prototype
*
* SYSTEM: POSIX and FreeDOS
* DESCRIPTION: A minimal, portable interpreter core prioritizing source lucidity
* and a static memory footprint. Features 8-bit signed arithmetic, strict left-to-right
* parsing, and direct file I/O capabilities.
* FEATURES: Direct/Program modes, 26 static variables, LPRINT to file, REPL interface.
* ARCHITECTURAL DESIGN: Single-file procedural architecture utilizing statically
* allocated arrays for all program memory, variables, and the execution stack.
* Floating-point math is explicitly excluded.
* HOW TO COMPILE: See detailed numbered instructions in the documentation section.
* HOW TO PORT: Code relies strictly on ANSI C89 standard libraries. Memory limits
* are defined via macros and can be adjusted for systems with under 64KB RAM.
* PORTABILITY AND MODULARITY GUIDE:
* - Arduino: Reduce MAX_LINES to fit within available SRAM. Replace main() with setup()
* and loop(). Connect standard I/O to the Serial hardware.
* - Raspberry Pi: Standard GCC compilation. Memory is abundant; default macros apply.
* DEVELOPERS PHILOSOPHY: Unix-based philosophy, ANSI C89/C90 compliance, no external
* libraries. LICENSE CONSTRAINT: This code is strictly not for sale. It may not be
* taken, modified, and sold commercially under any circumstances.
* * PROJECT ROADMAP:
* COMPLIANCE STATUS:
* - [MET] 8-bit signed integer math and deterministic wrap-around.
* - [MET] Left-to-right parser without standard mathematical precedence.
* - [MET] Implementation of core directives (PRINT, LET, GOTO, IF, etc.).
* - [PENDING] Pillar 6.1: Addons (Inline foreign language compilation).
* - [PENDING] Pillar 6.2: Merge ($MERGE directive for code sharing).
* - [PENDING] Pillar 6.3: Modules (C-level syntax extensibility).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* =========================================================================
* CONSTANTS / CONFIGURATION
* ========================================================================= */
/* * MAX_LINES: Maximum number of stored BASIC lines.
* Safe for user modification: YES.
* Expected effect: Increases or decreases the maximum program size.
* Constraints: Memory footprint scales linearly. 500 lines uses ~64KB.
*/
#define MAX_LINES 500
/* * LINE_LEN: Maximum character length of a single BASIC line.
* Safe for user modification: YES.
* Expected effect: Allows longer string literals or complex equations.
* Constraints: Exceeding 127 may break assumed bounds on constrained systems.
*/
#define LINE_LEN 127
/* * NUM_VARIABLES: Fixed number of numeric variables (A-Z).
* Safe for user modification: NO.
* Expected effect: Code relies on exactly 26 alphabetical indexes.
*/
#define NUM_VARIABLES 26
/* * STACK_SIZE: Maximum depth for nested GOSUB calls.
* Safe for user modification: YES.
* Expected effect: Prevents or allows deeper subroutine recursion.
*/
#define STACK_SIZE 64
typedef struct {
int line_number;
char text[LINE_LEN + 1];
} Line;
static Line program_storage[MAX_LINES];
static signed char variables[NUM_VARIABLES];
static int gosub_stack[STACK_SIZE];
static int num_stored_lines = 0;
static int stack_pointer = 0;
static int current_exec_line = -1;
static int execution_halted = 0;
/* Function Prototypes */
void clear_variables(void);
void clear_program(void);
signed char parse_expression(char **ptr);
void execute_line(char *cmd);
void run_program(void);
/*
* clear_variables
* Purpose: Zeroes out the 26 static variables and resets the GOSUB stack.
*/
void clear_variables(void) {
memset(variables, 0, sizeof(variables));
stack_pointer = 0;
}
/*
* clear_program
* Purpose: Erases all stored lines from memory.
*/
void clear_program(void) {
num_stored_lines = 0;
clear_variables();
}
/*
* parse_term
* Purpose: Evaluates a single number, variable, or parenthesized group.
* Modularity: Isolates basic value extraction from the mathematical operators.
*/
signed char parse_term(char **ptr) {
signed char val = 0;
while (isspace(**ptr)) (*ptr)++;
if (**ptr == '(') {
(*ptr)++;
val = parse_expression(ptr);
while (isspace(**ptr)) (*ptr)++;
if (**ptr == ')') (*ptr)++;
} else if (isalpha(**ptr)) {
int idx = toupper(**ptr) - 'A';
if (idx >= 0 && idx < NUM_VARIABLES) {
val = variables[idx];
}
(*ptr)++;
} else if (isdigit(**ptr) || **ptr == '-') {
int temp = (int)strtol(*ptr, ptr, 10);
val = (signed char)temp; /* Enforces 8-bit wrap-around */
}
while (isspace(**ptr)) (*ptr)++;
return val;
}
/*
* parse_expression
* Purpose: Evaluates math strictly left-to-right without standard precedence.
* Example: 3+4*5 becomes 35, not 23.
*/
signed char parse_expression(char **ptr) {
signed char result = parse_term(ptr);
while (**ptr) {
char op;
while (isspace(**ptr)) (*ptr)++;
op = **ptr;
if (op == '+' || op == '-' || op == '*' || op == '/') {
signed char next_val;
(*ptr)++;
next_val = parse_term(ptr);
if (op == '+') result += next_val;
else if (op == '-') result -= next_val;
else if (op == '*') result *= next_val;
else if (op == '/') {
if (next_val != 0) result /= next_val;
else printf("DIVIDE BY ZERO ERROR\n");
}
} else {
break;
}
}
return result;
}
/*
* store_line
* Purpose: Inserts a new line into the program storage array, keeping it sorted.
*/
void store_line(int line_num, char *text) {
int i, j;
for (i = 0; i < num_stored_lines; i++) {
if (program_storage[i].line_number == line_num) {
strncpy(program_storage[i].text, text, LINE_LEN);
program_storage[i].text[LINE_LEN] = '\0';
return;
} else if (program_storage[i].line_number > line_num) {
break;
}
}
if (num_stored_lines >= MAX_LINES) {
printf("MEMORY FULL\n");
return;
}
for (j = num_stored_lines; j > i; j--) {
program_storage[j] = program_storage[j - 1];
}
program_storage[i].line_number = line_num;
strncpy(program_storage[i].text, text, LINE_LEN);
program_storage[i].text[LINE_LEN] = '\0';
num_stored_lines++;
}
/*
* execute_line
* Purpose: Dispatches a BASIC directive to the appropriate logic block.
*/
void execute_line(char *cmd) {
while (isspace(*cmd)) cmd++;
if (*cmd == '\0') return;
if (strncmp(cmd, "PRINT", 5) == 0) {
cmd += 5;
while (isspace(*cmd)) cmd++;
if (*cmd == '"') {
char *end = strchr(cmd + 1, '"');
if (end) {
*end = '\0';
printf("%s\n", cmd + 1);
*end = '"';
}
} else {
printf("%d\n", parse_expression(&cmd));
}
} else if (strncmp(cmd, "LPRINT", 6) == 0) {
FILE *lp = fopen("lprint.out", "a");
cmd += 6;
while (isspace(*cmd)) cmd++;
if (lp) {
if (*cmd == '"') {
char *end = strchr(cmd + 1, '"');
if (end) {
*end = '\0';
fprintf(lp, "%s\n", cmd + 1);
*end = '"';
}
} else {
fprintf(lp, "%d\n", parse_expression(&cmd));
}
fclose(lp);
}
} else if (strncmp(cmd, "LET", 3) == 0) {
char var_name;
cmd += 3;
while (isspace(*cmd)) cmd++;
var_name = toupper(*cmd);
if (var_name >= 'A' && var_name <= 'Z') {
cmd++;
while (isspace(*cmd)) cmd++;
if (*cmd == '=') {
cmd++;
variables[var_name - 'A'] = parse_expression(&cmd);
}
}
} else if (strncmp(cmd, "INPUT", 5) == 0) {
char var_name;
char in_buf[16];
cmd += 5;
while (isspace(*cmd)) cmd++;
var_name = toupper(*cmd);
if (var_name >= 'A' && var_name <= 'Z') {
printf("? ");
if (fgets(in_buf, sizeof(in_buf), stdin)) {
variables[var_name - 'A'] = (signed char)atoi(in_buf);
}
}
} else if (strncmp(cmd, "GOTO", 4) == 0) {
cmd += 4;
current_exec_line = parse_expression(&cmd);
} else if (strncmp(cmd, "GOSUB", 5) == 0) {
cmd += 5;
if (stack_pointer < STACK_SIZE) {
gosub_stack[stack_pointer++] = current_exec_line;
current_exec_line = parse_expression(&cmd);
} else {
printf("OUT OF MEMORY (STACK)\n");
execution_halted = 1;
}
} else if (strncmp(cmd, "RETURN", 6) == 0) {
if (stack_pointer > 0) {
current_exec_line = gosub_stack[--stack_pointer];
} else {
printf("RETURN WITHOUT GOSUB\n");
execution_halted = 1;
}
} else if (strncmp(cmd, "IF", 2) == 0) {
signed char expr_val;
cmd += 2;
expr_val = parse_expression(&cmd);
if (strncmp(cmd, "THEN", 4) == 0) {
cmd += 4;
if (expr_val != 0) {
execute_line(cmd);
}
}
} else if (strncmp(cmd, "REM", 3) == 0) {
/* Do nothing, it is a comment */
} else if (strncmp(cmd, "STOP", 4) == 0) {
printf("BREAK\n");
execution_halted = 1;
} else if (strncmp(cmd, "END", 3) == 0) {
execution_halted = 1;
} else if (strncmp(cmd, "BEEP", 4) == 0) {
printf("\a");
} else if (strncmp(cmd, "SYSTEM", 6) == 0) {
printf("SYSTEM RESERVED FOR MODULES\n");
} else if (strncmp(cmd, "QUIT", 4) == 0 || strncmp(cmd, "EXIT", 4) == 0) {
exit(0);
} else {
printf("SYNTAX ERROR\n");
}
}
/*
* run_program
* Purpose: Sequentially executes stored lines until halted by logic or end of memory.
*/
void run_program(void) {
int i;
clear_variables();
execution_halted = 0;
current_exec_line = -1;
if (num_stored_lines == 0) return;
for (i = 0; i < num_stored_lines; i++) {
if (execution_halted) break;
if (current_exec_line != -1) {
int found = 0;
int j;
for (j = 0; j < num_stored_lines; j++) {
if (program_storage[j].line_number >= current_exec_line) {
i = j;
found = 1;
break;
}
}
if (!found) break;
current_exec_line = -1;
}
execute_line(program_storage[i].text);
}
}
/*
* main
* Purpose: Initializes the interpreter environment and manages the central REPL.
*/
int main(void) {
char input_buffer[LINE_LEN + 16];
clear_program();
printf("BASIC++ (IB) Interpreter Core\n");
printf("63 kbytes Free\n");
while (1) {
printf("Ready.\n> ");
if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) break;
input_buffer[strcspn(input_buffer, "\r\n")] = 0;
if (strlen(input_buffer) == 0) continue;
if (isdigit((unsigned char)input_buffer[0])) {
int line_num = atoi(input_buffer);
char *text = strchr(input_buffer, ' ');
if (text) {
text++;
store_line(line_num, text);
}
} else if (strncmp(input_buffer, "RUN", 3) == 0) {
run_program();
} else if (strncmp(input_buffer, "LIST", 4) == 0) {
int i;
for (i = 0; i < num_stored_lines; i++) {
printf("%d %s\n", program_storage[i].line_number, program_storage[i].text);
}
} else if (strncmp(input_buffer, "NEW", 3) == 0) {
clear_program();
} else {
execute_line(input_buffer);
}
}
return 0;
}