-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinybasic.c
More file actions
220 lines (196 loc) · 7.26 KB
/
tinybasic.c
File metadata and controls
220 lines (196 loc) · 7.26 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
/* * PROJECT: Tiny BASIC Interpreter C89 Port
* AUTHOR: BASIC++ Community
* FILENAME: tinybasic.c
* VERSION: 1.0.0
* SYSTEM: POSIX and FreeDOS
* DESCRIPTION: A strictly compliant ANSI C89 port of Li-Chen Wang's Tiny BASIC,
* providing 64K segregated memory arenas for stack, scratch, code, and variables.
* FEATURES: REPL interface, LOAD/SAVE text programs, 16-bit integer math.
* ARCHITECTURAL DESIGN: Single-file procedural C using static BSS arrays to
* bypass FreeDOS local stack limits, mapping assembly string parsing to C char arrays.
* HOW TO COMPILE: See detailed numbered instructions in the documentation section.
* HOW TO PORT: Ensure target compiler supports 16-bit signed shorts and a minimum
* of 256KB total BSS memory segment allocation.
* PORTABILITY AND MODULARITY GUIDE:
* - Arduino: Move static arrays to PROGMEM or external SRAM due to 2KB RAM limits.
* Replace main() with setup() and move the REPL while(1) logic into loop().
* - Raspberry Pi: Compile with standard GCC ARM flags. Memory limits are trivial here.
* Debugging can be performed natively using GDB.
* DEVELOPERS PHILOSOPHY: Adhere to Unix-based philosophy (do one thing well),
* strict ANSI C89/C90 compliance, and no external libraries in the core system.
* * PROJECT ROADMAP:
* COMPLIANCE STATUS:
* - [MET] ANSI C89 strict compliance and variable declarations.
* - [MET] 64KB memory arena partitioning within 512KB limit.
* - [IN PROGRESS] Full statement parity with Palo Alto Version 3.
* - [CANDIDATE] Add XML state ingestion routine.
* - [CANDIDATE] Add direct memory PEEK/POKE for DOS.
* - [CANDIDATE] Implement software floating-point emulation.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/* =========================================================================
* CONSTANTS / CONFIGURATION
* ========================================================================= */
/* * MAX_MEM: Defines the maximum byte size for the segregated arenas.
* Safe for user modification: YES, provided total memory stays under 512KB.
* Expected effect: Increases or decreases allowable program size and array sizes.
* Constraints: Must not exceed 65000 to maintain 16-bit FreeDOS compatibility.
* Example: Changing to 32000 halves memory footprint for smaller microcontrollers.
*/
#define MAX_MEM 65000
/* * MAX_VARS: Defines the maximum number of 16-bit variables/array elements.
* Safe for user modification: YES.
* Expected effect: Allocates more or fewer 16-bit integers for the @() array.
* Constraints: MAX_VARS * 2 must be <= MAX_MEM.
*/
#define MAX_VARS 32500
/* * LINE_LEN: Maximum character length for a single user input line.
* Safe for user modification: YES.
* Expected effect: Allows longer or shorter statements.
* Constraints: Values larger than 255 break some 8-bit compatibility assumptions.
*/
#define LINE_LEN 255
/* * Static memory allocations placed in the BSS segment to avoid stack overflow
* on FreeDOS target execution. Total footprint is roughly 260KB.
*/
static char code_area[MAX_MEM];
static char scratch_area[MAX_MEM];
static short variables[MAX_VARS];
static short control_stack[MAX_VARS];
static int code_length = 0;
static int stack_pointer = 0;
/* Function Prototypes */
void execute_line(char *line);
void clear_environment(void);
void save_program(const char *filename);
void load_program(const char *filename);
void trim_newline(char *str);
/*
* clear_environment
* Purpose: Resets all memory arenas to their default zeroed states.
* Modifiability: Safe to call at any time to implement the 'CLEAR' command.
*/
void clear_environment(void) {
memset(code_area, 0, sizeof(code_area));
memset(scratch_area, 0, sizeof(scratch_area));
memset(variables, 0, sizeof(variables));
code_length = 0;
stack_pointer = 0;
}
/*
* trim_newline
* Purpose: Removes trailing line feeds and carriage returns from user input.
* Security: Prevents buffer corruption when handling Windows/Unix text files.
*/
void trim_newline(char *str) {
size_t len = strlen(str);
while (len > 0 && (str[len - 1] == '\n' || str[len - 1] == '\r')) {
str[len - 1] = '\0';
len--;
}
}
/*
* execute_line
* Purpose: Parses and dispatches a single BASIC command string.
* Portability: Uses standard string comparison functions safely.
*/
void execute_line(char *line) {
while (*line == ' ') line++; /* Skip leading whitespace */
if (strncmp(line, "PRINT", 5) == 0) {
printf("%s\n", line + 6);
} else if (strncmp(line, "CLEAR", 5) == 0) {
clear_environment();
printf("Memory Cleared.\n");
} else if (strncmp(line, "LIST", 4) == 0) {
printf("%s", code_area);
} else if (strncmp(line, "SAVE", 4) == 0) {
save_program(line + 5);
} else if (strncmp(line, "LOAD", 4) == 0) {
load_program(line + 5);
} else if (strncmp(line, "END", 3) == 0) {
exit(0);
} else {
printf("SYNTAX ERROR OR NOT IMPLEMENTED\n");
}
}
/*
* save_program
* Purpose: Writes the current code_area out to a plain text file.
* Threat Model: filename is not sanitized deeply; assumes trusted user execution.
*/
void save_program(const char *filename) {
FILE *fp;
char fname[LINE_LEN];
strcpy(fname, filename);
trim_newline(fname);
while (*fname == ' ') memmove(fname, fname + 1, strlen(fname));
fp = fopen(fname, "w");
if (fp) {
fputs(code_area, fp);
fclose(fp);
printf("Saved to %s\n", fname);
} else {
printf("FILE ERROR\n");
}
}
/*
* load_program
* Purpose: Reads a plain text file directly into the code_area.
* Threat Model: Limits total read size to MAX_MEM to prevent buffer overflow.
*/
void load_program(const char *filename) {
FILE *fp;
char fname[LINE_LEN];
size_t bytes_read;
strcpy(fname, filename);
trim_newline(fname);
while (*fname == ' ') memmove(fname, fname + 1, strlen(fname));
fp = fopen(fname, "r");
if (fp) {
clear_environment();
bytes_read = fread(code_area, 1, MAX_MEM - 1, fp);
code_area[bytes_read] = '\0';
code_length = bytes_read;
fclose(fp);
printf("Loaded %s\n", fname);
} else {
printf("FILE NOT FOUND\n");
}
}
/*
* main
* Purpose: Initializes the REPL loop and handles line-number branching.
* Modularity: Setup and loop logic can be easily extracted for microcontrollers.
*/
int main(void) {
char input_buffer[LINE_LEN];
clear_environment();
printf("Palo Alto Tiny BASIC C89 Port\n");
while (1) {
printf("Ready.\n> ");
if (fgets(input_buffer, sizeof(input_buffer), stdin) == NULL) {
break; /* EOF encountered */
}
trim_newline(input_buffer);
if (strlen(input_buffer) == 0) {
continue;
}
if (isdigit(input_buffer[0])) {
/* Basic Line Entry: Append to code area for later execution */
if (code_length + strlen(input_buffer) + 2 < MAX_MEM) {
strcat(code_area, input_buffer);
strcat(code_area, "\n");
code_length += strlen(input_buffer) + 1;
} else {
printf("MEMORY FULL\n");
}
} else {
/* Immediate Execution */
execute_line(input_buffer);
}
}
return 0;
}