allow stdin

This commit is contained in:
Freya Murphy 2023-04-14 15:15:10 -04:00
parent 9085afb7c8
commit d7ecc6b9ef
3 changed files with 26 additions and 25 deletions

View file

@ -25,13 +25,24 @@
/// % Clear screen /// % Clear screen
int main(int argc, char** argv) { 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"); printf("usage: brainfucked infile\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
Program program; Program program;
program_init(argv[1], &program); program_init(file, &program);
run_program(&program); run_program(&program);
program_free(&program); program_free(&program);

View file

@ -6,21 +6,17 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
static FILE* f; static char next_char(FILE* file) {
static uint32_t count;
static char next_char() {
char c; char c;
if (fread(&c, 1, 1, f) != 1) { if (fread(&c, 1, 1, file) != 1) {
return EOF; return EOF;
} else { } else {
count++;
return c; return c;
} }
} }
static Symbol next_symbol() { static Symbol next_symbol(FILE* file) {
char c = next_char(); char c = next_char(file);
retest: retest:
switch (c) { switch (c) {
case '<': case '<':
@ -56,28 +52,21 @@ retest:
case '\n': case '\n':
case '\t': case '\t':
case ' ': case ' ':
while(c = next_char(), c == '\n' || c == '\t' || c == ' '); while(c = next_char(file), c == '\n' || c == '\t' || c == ' ');
goto retest; goto retest;
case '/': case '/':
while(c = next_char(), c != '\n' && c != EOF); while(c = next_char(file), c != '\n' && c != EOF);
goto retest; goto retest;
case EOF: case EOF:
return Eof; return Eof;
default: default:
c = next_char(); c = next_char(file);
goto retest; goto retest;
} }
} }
void program_init(char* file_path, Program* program) { void program_init(FILE* file, 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);
}
uint32_t capacity = 8; uint32_t capacity = 8;
count = 0;
program->data = malloc(capacity * sizeof(Symbol)); program->data = malloc(capacity * sizeof(Symbol));
program->len = 0; program->len = 0;
@ -85,7 +74,7 @@ void program_init(char* file_path, Program* program) {
Symbol s; Symbol s;
while(true) { while(true) {
s = next_symbol(); s = next_symbol(file);
if (program->len == capacity) { if (program->len == capacity) {
capacity *= 2; capacity *= 2;
@ -98,7 +87,7 @@ void program_init(char* file_path, Program* program) {
if (s == Eof) break; if (s == Eof) break;
} }
fclose(f); fclose(file);
} }
void program_peek(Program* program, Symbol* symbol) { void program_peek(Program* program, Symbol* symbol) {

View file

@ -2,6 +2,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
typedef enum { typedef enum {
MoveLeft, MoveLeft,
@ -28,7 +29,7 @@ typedef struct {
Symbol* data; Symbol* data;
} Program; } 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_peek(Program* program, Symbol* symbol);
void program_next(Program* program, Symbol* symbol); void program_next(Program* program, Symbol* symbol);
void program_last(Program* program, Symbol* symbol); void program_last(Program* program, Symbol* symbol);