-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainmuk.c
144 lines (111 loc) · 3.66 KB
/
brainmuk.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <bf_alloc.h>
#include <bf_version.h>
#include <bf_runtime.h>
#include <bf_arguments.h>
#include <bf_compile.h>
#include <bf_slurp.h>
#define REPL_LINE_LENGTH 1024
static char* program_name = NULL;
static void run_file(bf_options *options);
static void repl(bf_options *options);
int main(int argc, char *argv[]) {
program_name = argv[0];
bf_options options = parse_arguments(argc, argv);
/* Check if a file has been provided. */
if (options.filename == NULL) {
repl(&options);
} else {
run_file(&options);
}
return 0;
}
static uint8_t* create_universe(bf_options *options) {
uint8_t* universe =
calloc(options->minimum_universe_size, sizeof(uint8_t));
if (universe == NULL) {
fprintf(stderr, "%s: could not create universe (%lu bytes): ",
program_name, options->minimum_universe_size);
perror(NULL);
exit(-1);
}
return universe;
}
static struct bf_runtime_context normal_context(uint8_t *universe) {
return ((struct bf_runtime_context) {
.universe = universe,
.output_byte = bf_runtime_output_byte,
.input_byte = bf_runtime_input_byte
});
}
static void prompt(const char *herp) {
printf("%s ", herp);
fflush(stdout);
}
static void repl(bf_options *options) {
char line[REPL_LINE_LENGTH];
/* Warn if stdin doesn't appear to be a terminal. */
if (!isatty(fileno(stdin))) {
fprintf(stderr,
"%s: warning: input is not from a terminal\n",
program_name);
}
printf("brainmuk repl " BF_VERSION "\n"
"Press ctrl+d to exit\n");
/* Prepare universe and executable space. */
const size_t exec_mem_size = sysconf(_SC_PAGESIZE);
uint8_t *universe = create_universe(options);
uint8_t *exec_mem = allocate_executable_space(exec_mem_size);
do {
prompt("#%@!>");
/* Read. */
if (fgets(line, REPL_LINE_LENGTH, stdin) == NULL) {
break;
}
/* Eval. */
bf_compile_result result = bf_compile_no_alloc(line, exec_mem);
if (result.status != BF_COMPILE_SUCCESS) {
fprintf(stderr, "compile error (check brackets?)\n");
continue;
}
/* Run! */
result.program(normal_context(universe));
/* (The program should print stuff itself... */
} while (!feof(stdin));
free(universe);
}
static void run_program(program_t program, bf_options *options) {
/* Allocate the ENTIRE UNIVERSE and run. */
uint8_t *universe = calloc(options->minimum_universe_size, sizeof(uint8_t));
assert(universe != NULL);
program((struct bf_runtime_context) {
.universe = universe,
.output_byte = bf_runtime_output_byte,
.input_byte = bf_runtime_input_byte
});
free(universe);
}
static void run_file(bf_options *options) {
char *contents = slurp(options->filename);
/* Try to open the file... */
if (contents == NULL) {
fprintf(stderr, "%s: Could not open '%s': ",
program_name, options->filename);
perror(NULL);
exit(-1);
}
/* Compile and forget the source. */
bf_compile_result compilation = bf_compile(contents);
unslurp(contents);
if (compilation.status == BF_COMPILE_SUCCESS) {
run_program(compilation.program, options);
free_executable_space((void *) compilation.program, compilation.program_size);
} else {
fprintf(stderr, "%s: %s: compilation failed!\n",
program_name, options->filename);
}
exit(compilation.status);
}