allow stdin
This commit is contained in:
parent
9085afb7c8
commit
d7ecc6b9ef
3 changed files with 26 additions and 25 deletions
15
src/main.c
15
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);
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue