summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c17
-rw-r--r--src/program.c31
-rw-r--r--src/program.h3
3 files changed, 26 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index ac5e0f7..20134cd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,13 +25,24 @@
/// % Clear screen
int main(int argc, char** argv) {
- if (argc != 2) {
+
+ FILE* file;
+
+ if (argc == 1) {
+ file = stdin;
+ } else if (argc == 2) {
+ file = fopen (argv[1], "r");
+ if (file == NULL) {
+ printf("error: failed to open %s (%s)\n", argv[1], strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ } else {
printf("usage: brainfucked infile\n");
return EXIT_FAILURE;
}
-
+
Program program;
- program_init(argv[1], &program);
+ program_init(file, &program);
run_program(&program);
program_free(&program);
diff --git a/src/program.c b/src/program.c
index dcec0f7..1d94da3 100644
--- a/src/program.c
+++ b/src/program.c
@@ -6,21 +6,17 @@
#include <string.h>
#include <errno.h>
-static FILE* f;
-static uint32_t count;
-
-static char next_char() {
+static char next_char(FILE* file) {
char c;
- if (fread(&c, 1, 1, f) != 1) {
+ if (fread(&c, 1, 1, file) != 1) {
return EOF;
} else {
- count++;
return c;
}
}
-static Symbol next_symbol() {
- char c = next_char();
+static Symbol next_symbol(FILE* file) {
+ char c = next_char(file);
retest:
switch (c) {
case '<':
@@ -56,28 +52,21 @@ retest:
case '\n':
case '\t':
case ' ':
- while(c = next_char(), c == '\n' || c == '\t' || c == ' ');
+ while(c = next_char(file), c == '\n' || c == '\t' || c == ' ');
goto retest;
case '/':
- while(c = next_char(), c != '\n' && c != EOF);
+ while(c = next_char(file), c != '\n' && c != EOF);
goto retest;
case EOF:
return Eof;
default:
- c = next_char();
+ c = next_char(file);
goto retest;
}
}
-void program_init(char* file_path, Program* program) {
- f = fopen (file_path, "r");
- if (f == NULL) {
- printf("error: failed to open %s (%s)\n", file_path, strerror(errno));
- exit(EXIT_FAILURE);
- }
-
+void program_init(FILE* file, Program* program) {
uint32_t capacity = 8;
- count = 0;
program->data = malloc(capacity * sizeof(Symbol));
program->len = 0;
@@ -85,7 +74,7 @@ void program_init(char* file_path, Program* program) {
Symbol s;
while(true) {
- s = next_symbol();
+ s = next_symbol(file);
if (program->len == capacity) {
capacity *= 2;
@@ -98,7 +87,7 @@ void program_init(char* file_path, Program* program) {
if (s == Eof) break;
}
- fclose(f);
+ fclose(file);
}
void program_peek(Program* program, Symbol* symbol) {
diff --git a/src/program.h b/src/program.h
index 23f4d74..ea29b6c 100644
--- a/src/program.h
+++ b/src/program.h
@@ -2,6 +2,7 @@
#include <stdbool.h>
#include <stdint.h>
+#include <stdio.h>
typedef enum {
MoveLeft,
@@ -28,7 +29,7 @@ typedef struct {
Symbol* data;
} Program;
-void program_init(char* file_path, Program* program);
+void program_init(FILE* file, Program* program);
void program_peek(Program* program, Symbol* symbol);
void program_next(Program* program, Symbol* symbol);
void program_last(Program* program, Symbol* symbol);