以下のようにC言語に変換されます。
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include <time.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
void runtime_error(const char *msg);
double input_number(void);
double read_line_as_number(void);
void set_read_delimiter(const char *delimiter);
int split_read_line(double *values, int max_values);
void read_line_into_vars(int expected_count, ...);
int read_line_into_array(int array_idx);
void read_binary_byte_into_var(int var_idx);
int read_binary_into_array(int array_idx);
double safe_div(double left, double right);
double safe_sqr(double value);
double rnd_basic(double arg);
double date_part(double selector);
unsigned long long mask_for_bits(int bits);
void basic_spc(double count, const char *text);
void print_binary(unsigned long long value, int width);
void print_array_full(int array_idx);
void write_array_bytes(int array_idx);
void free_arrays(void);
void close_input_file(void);
int checked_array_index(int array_idx, double subscript);
double get_array_value(int array_idx, double subscript);
double *array_element_ptr(int array_idx, double subscript);
void dim_array(int array_idx, double upper_bound);
int is_eof(void);
double vars[26];
double loop_limit[26];
double loop_step[26];
double *arrays[26];
int array_sizes[26];
int array_read_counts[26];
double args_buffer[26];
int args_count = 0;
int stack[256];
int sp = 0;
double var_stack[1024][26];
int var_sp = 0;
double return_reg = 0.0;
int current_line = 0;
FILE *input_fp = NULL;
char read_delimiter[16] = ",";
void runtime_error(const char *msg) {
fprintf(stderr, "Runtime error at BASIC line %d: %s\n", current_line, msg);
exit(1);
}
double input_number(void) {
char buffer[1024];
if (!fgets(buffer, sizeof(buffer), stdin)) {
runtime_error("INPUT failed");
}
return atof(buffer);
}
double read_line_as_number(void) {
char buffer[1024];
if (input_fp == NULL) {
runtime_error("READ input file is not open");
}
if (!fgets(buffer, sizeof(buffer), input_fp)) {
if (feof(input_fp)) {
runtime_error("READ attempted at EOF");
}
runtime_error("READ failed");
}
return atof(buffer);
}
void set_read_delimiter(const char *delimiter) {
size_t len;
if (delimiter == NULL || delimiter[0] == '\0') {
runtime_error("Delimiter must not be empty");
}
len = strlen(delimiter);
if (len >= sizeof(read_delimiter)) {
runtime_error("Delimiter is too long");
}
memcpy(read_delimiter, delimiter, len 1);
}
int split_read_line(double *values, int max_values) {
char buffer[1024];
char *cursor;
size_t delim_len;
int count;
if (input_fp == NULL) {
runtime_error("READ input file is not open");
}
if (!fgets(buffer, sizeof(buffer), input_fp)) {
if (feof(input_fp)) {
runtime_error("READ attempted at EOF");
}
runtime_error("READ failed");
}
buffer[strcspn(buffer, "\r\n")] = '\0';
if (buffer[0] == '\0') {
return 0;
}
delim_len = strlen(read_delimiter);
cursor = buffer;
count = 0;
while (1) {
char *sep = strstr(cursor, read_delimiter);
char *token = cursor;
char *endptr;
double value;
if (sep != NULL) {
*sep = '\0';
cursor = sep delim_len;
} else {
cursor = NULL;
}
value = strtod(token, &endptr);
if (endptr == token) {
value = 0.0;
}
if (count < max_values) {
values[count] = value;
}
count;
if (cursor == NULL) {
break;
}
}
return count;
}
void read_line_into_vars(int expected_count, ...) {
double values[256];
va_list ap;
int count;
int i;
if (expected_count < 0 || expected_count > 256) {
runtime_error("READ variable count out of range");
}
count = split_read_line(values, expected_count);
if (count < expected_count) {
runtime_error("READ line has fewer values than expected");
}
va_start(ap, expected_count);
for (i = 0; i < expected_count; i) {
double *dest = va_arg(ap, double *);
*dest = values[i];
}
va_end(ap);
}
int read_line_into_array(int array_idx) {
double values[2048];
int count;
int i;
int limit;
if (arrays[array_idx] == NULL) {
runtime_error("Array used before DIM");
}
count = split_read_line(values, 2048);
limit = array_sizes[array_idx];
if (count > limit) {
count = limit;
}
for (i = 0; i < count; i) {
arrays[array_idx][i 1] = values[i];
}
return count;
}
void read_binary_byte_into_var(int var_idx) {
int byte_value;
if (input_fp == NULL) {
runtime_error("READBIN input file is not open");
}
byte_value = fgetc(input_fp);
if (byte_value == EOF) {
if (ferror(input_fp)) {
runtime_error("READBIN failed");
}
return;
}
vars[var_idx] = (double)((unsigned char)byte_value);
}
int read_binary_into_array(int array_idx) {
int count;
int byte_value;
if (input_fp == NULL) {
runtime_error("READBIN input file is not open");
}
if (arrays[array_idx] == NULL) {
runtime_error("Array used before DIM");
}
count = 0;
while (count < array_sizes[array_idx]) {
byte_value = fgetc(input_fp);
if (byte_value == EOF) {
if (ferror(input_fp)) {
runtime_error("READBIN failed");
}
break;
}
count;
arrays[array_idx][count] = (double)((unsigned char)byte_value);
}
return count;
}
double safe_div(double left, double right) {
if (right == 0.0) {
runtime_error("Division by zero");
}
return left / right;
}
double safe_sqr(double value) {
if (value < 0.0) {
runtime_error("SQR of negative value");
}
return sqrt(value);
}
double rnd_basic(double arg) {
(void)arg;
return (double)rand() / (double)RAND_MAX;
}
double date_part(double selector) {
time_t now;
struct tm local_tm;
struct tm *tm_ptr;
int part;
part = (int)selector;
now = time(NULL);
tm_ptr = localtime(&now);
if (tm_ptr == NULL) {
runtime_error("DATE failed");
}
local_tm = *tm_ptr;
switch (part) {
case 0: return (double)(local_tm.tm_year 1900);
case 1: return (double)(local_tm.tm_mon 1);
case 2: return (double)local_tm.tm_mday;
case 3: return (double)local_tm.tm_wday;
case 4: return (double)local_tm.tm_hour;
case 5: return (double)local_tm.tm_min;
case 6: return (double)local_tm.tm_sec;
case 7:
#ifdef TIME_UTC
{
struct timespec ts;
if (timespec_get(&ts, TIME_UTC) == 0) {
runtime_error("DATE failed");
}
return (double)(
ts.tv_nsec / 1000000L);
}
#else
return 0.0;
#endif
default:
runtime_error("DATE argument out of range");
}
return 0.0;
}
unsigned long long mask_for_bits(int bits) {
if (bits <= 0) {
return 0ULL;
}
if (bits >= (int)(sizeof(unsigned long long) * 8)) {
return ~0ULL;
}
return (1ULL << bits) - 1ULL;
}
void basic_spc(double count, const char *text) {
int n;
int i;
size_t len;
if (text == NULL || text[0] == '\0') {
text = " ";
}
n = (int)count;
if (n <= 0) {
return;
}
len = strlen(text);
for (i = 0; i < n; i) {
fwrite(text, 1, len, stdout);
}
}
void print_binary(unsigned long long value, int width) {
int i;
value &= mask_for_bits(width);
for (i = width - 1; i >= 0; --i) {
printf("%c", (value & (1ULL << i)) ? '1' : '0');
}
}
void print_array_full(int array_idx) {
int count;
int i;
if (arrays[array_idx] == NULL) {
runtime_error("Array used before DIM");
}
count = array_read_counts[array_idx];
if (count <= 0) {
count = array_sizes[array_idx];
}
if (count > array_sizes[array_idx]) {
count = array_sizes[array_idx];
}
for (i = 1; i <= count; i) {
if (i > 1) {
printf("%s", ", ");
}
printf("%g", arrays[array_idx][i]);
}
}
void write_array_bytes(int array_idx) {
int count;
int i;
if (arrays[array_idx] == NULL) {
runtime_error("Array used before DIM");
}
count = array_read_counts[array_idx];
if (count <= 0) {
count = array_sizes[array_idx];
}
if (count > array_sizes[array_idx]) {
count = array_sizes[array_idx];
}
for (i = 1; i <= count; i) {
fputc((unsigned char)((long long)(arrays[array_idx][i])), stdout);
}
}
void close_input_file(void) {
if (input_fp != NULL && input_fp != stdin) {
fclose(input_fp);
}
input_fp = NULL;
}
void free_arrays(void) {
int i;
for (i = 0; i < 26; i) {
free(arrays[i]);
arrays[i] = NULL;
array_sizes[i] = 0;
}
}
int checked_array_index(int array_idx, double subscript) {
int index;
if (arrays[array_idx] == NULL) {
runtime_error("Array used before DIM");
}
if (subscript < 0.0 || floor(subscript) != subscript) {
runtime_error("Array index must be a non-negative integer");
}
index = (int)subscript;
if (index > array_sizes[array_idx]) {
runtime_error("Array index out of bounds");
}
return index;
}
double get_array_value(int array_idx, double subscript) {
return arrays[array_idx][checked_array_index(array_idx, subscript)];
}
double *array_element_ptr(int array_idx, double subscript) {
return &arrays[array_idx][checked_array_index(array_idx, subscript)];
}
void dim_array(int array_idx, double upper_bound) {
size_t count;
if (upper_bound < 0.0 || floor(upper_bound) != upper_bound) {
runtime_error("DIM size must be a non-negative integer");
}
count = (size_t)((int)upper_bound 1);
free(arrays[array_idx]);
arrays[array_idx] = (double *)calloc(count, sizeof(double));
if (arrays[array_idx] == NULL) {
runtime_error("Out of memory during DIM");
}
array_sizes[array_idx] = (int)upper_bound;
}
int is_eof(void) {
int ch;
if (input_fp == NULL) {
return 1;
}
ch = fgetc(input_fp);
if (ch == EOF) {
return 1;
}
if (ungetc(ch, input_fp) == EOF) {
runtime_error("Failed to unread input stream");
}
return 0;
}
int main(int argc, char *argv[]) {
int i;
int line = 10;
srand((unsigned int)time(NULL));
for (i = 0; i < 26; i) {
arrays[i] = NULL;
array_sizes[i] = 0;
array_read_counts[i] = 0;
}
if (argc > 1) {
input_fp = fopen(argv[1], "r");
if (input_fp == NULL) {
runtime_error("Could not open input file");
}
} else {
input_fp = stdin;
}
atexit(free_arrays);
atexit(close_input_file);
while (line != 0) {
switch (line) {
case 10:
current_line = 10;
dim_array(3, 16.0);
line = 20;
break;
line = 20;
break;
case 20:
current_line = 20;
vars[0] = 0.0;
line = 30;
break;
line = 30;
break;
case 30:
current_line = 30;
line = 40;
break;
line = 40;
break;
case 40:
current_line = 40;
if (is_eof()) {
line = 0;
break;
} else {
line = 50;
break;
}
case 50:
current_line = 50;
if (arrays[3] != NULL) {
array_read_counts[3] = read_binary_into_array(3);
} else {
read_binary_byte_into_var(3);
array_read_counts[3] = 0;
}
line = 60;
break;
line = 60;
break;
case 60:
current_line = 60;
if ((((double)(array_read_counts[3])) == 0.0)) {
line = 0;
break;
} else {
line = 70;
break;
}
case 70:
current_line = 70;
{
char format_buffer[1024];
snprintf(format_buffer, sizeof(format_buffer), "X ", (unsigned int)(vars[0]));
printf("%s", format_buffer);
}
line = 80;
break;
line = 80;
break;
case 80:
current_line = 80;
vars[8] = 1.0;
loop_limit[8] = ((double)(array_read_counts[3]));
loop_step[8] = 1.0;
if (loop_step[8] == 0.0) runtime_error("FOR STEP cannot be zero");
if (((loop_step[8] > 0.0) && (vars[8] <= loop_limit[8])) || ((loop_step[8] < 0.0) && (vars[8] >= loop_limit[8]))) {
line = 90;
} else {
line = 110;
break;
}
break;
line = 90;
break;
case 90:
current_line = 90;
{
char format_buffer[1024];
snprintf(format_buffer, sizeof(format_buffer), "X ", (unsigned int)(get_array_value(3, vars[8])));
printf("%s", format_buffer);
}
line = 100;
break;
line = 100;
break;
case 100:
current_line = 100;
vars[8] = loop_step[8];
if (((loop_step[8] > 0.0) && (vars[8] <= loop_limit[8])) || ((loop_step[8] < 0.0) && (vars[8] >= loop_limit[8]))) {
line = 90;
} else {
line = 110;
}
break;
case 110:
current_line = 110;
basic_spc(((16.0 - ((double)(array_read_counts[3]))) * 3.0), " ");
line = 120;
break;
line = 120;
break;
case 120:
current_line = 120;
vars[8] = 1.0;
loop_limit[8] = ((double)(array_read_counts[3]));
loop_step[8] = 1.0;
if (loop_step[8] == 0.0) runtime_error("FOR STEP cannot be zero");
if (((loop_step[8] > 0.0) && (vars[8] <= loop_limit[8])) || ((loop_step[8] < 0.0) && (vars[8] >= loop_limit[8]))) {
line = 130;
} else {
line = 150;
break;
}
break;
line = 130;
break;
case 130:
current_line = 130;
if ((get_array_value(3, vars[8]) < 32.0)) {
printf("%s", ".");
line = 140;
break;
} else {
putchar((unsigned char)(get_array_value(3, vars[8])));
line = 140;
break;
}
case 140:
current_line = 140;
vars[8] = loop_step[8];
if (((loop_step[8] > 0.0) && (vars[8] <= loop_limit[8])) || ((loop_step[8] < 0.0) && (vars[8] >= loop_limit[8]))) {
line = 130;
} else {
line = 150;
}
break;
case 150:
current_line = 150;
printf("\n");
line = 160;
break;
line = 160;
break;
case 160:
current_line = 160;
vars[0] = (vars[0] 16.0);
line = 170;
break;
line = 170;
break;
case 170:
current_line = 170;
line = 30;
break;
default:
fprintf(stderr, "Line %d not found\n", line);
return 1;
}
}
return 0;
}