Compare commits
No commits in common. "5ce1fddc8d7af8b82385aec007056ca0d8a45db7" and "6538e1d7f324eb5ae8f7828a49a8f03b14db85bd" have entirely different histories.
5ce1fddc8d
...
6538e1d7f3
|
|
@ -1,18 +0,0 @@
|
||||||
{
|
|
||||||
"version": 4,
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "gcc",
|
|
||||||
"compilerPath": "/usr/bin/gcc",
|
|
||||||
|
|
||||||
"cStandard": "c17",
|
|
||||||
"cppStandard": "c++17",
|
|
||||||
|
|
||||||
"browse": {
|
|
||||||
"path": [
|
|
||||||
"${workspaceFolder}/src"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
487
src/main.c
487
src/main.c
|
|
@ -1,7 +1,7 @@
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
@ -9,27 +9,10 @@
|
||||||
#define BRACKETS_NB_MAX 1024
|
#define BRACKETS_NB_MAX 1024
|
||||||
#define DATA_SIZE_MAX 65536
|
#define DATA_SIZE_MAX 65536
|
||||||
|
|
||||||
// The advanced version of operations should always have value = simple version + 1
|
|
||||||
enum operations_t {
|
|
||||||
NONE,
|
|
||||||
MEM_POINTER_ADD,
|
|
||||||
MEM_POINTER_ADD_NB,
|
|
||||||
MEM_POINTER_SUB,
|
|
||||||
MEM_POINTER_SUB_NB,
|
|
||||||
MEM_ADD,
|
|
||||||
MEM_ADD_NB,
|
|
||||||
MEM_SUB,
|
|
||||||
MEM_SUB_NB,
|
|
||||||
JUMP_IF_0,
|
|
||||||
JUMP_IF_NOT_0,
|
|
||||||
PRINT_CHAR,
|
|
||||||
GET_CHAR
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef uint8_t operation_t;
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
clock_t start_time = clock();
|
||||||
|
|
||||||
int status = EXIT_FAILURE;
|
int status = EXIT_FAILURE;
|
||||||
|
|
||||||
if(argc != 2)
|
if(argc != 2)
|
||||||
|
|
@ -39,7 +22,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *program_file = NULL;
|
FILE *program_file = NULL;
|
||||||
FILE *intermediate_representation_file = NULL;
|
FILE *stripped_file = NULL;
|
||||||
|
|
||||||
program_file = fopen(argv[1], "r");
|
program_file = fopen(argv[1], "r");
|
||||||
if(program_file == NULL)
|
if(program_file == NULL)
|
||||||
|
|
@ -48,8 +31,8 @@ int main(int argc, char *argv[])
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
intermediate_representation_file = tmpfile();
|
stripped_file = tmpfile();
|
||||||
if(intermediate_representation_file == NULL)
|
if(stripped_file == NULL)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error creating tmp file: %d\n", errno);
|
fprintf(stderr, "Error creating tmp file: %d\n", errno);
|
||||||
goto end;
|
goto end;
|
||||||
|
|
@ -67,165 +50,30 @@ int main(int argc, char *argv[])
|
||||||
if(err)
|
if(err)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(program_file));
|
fprintf(stderr, "Error reading file: %d\n", ferror(program_file));
|
||||||
goto end;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
operation_t operation = NONE;
|
|
||||||
static operation_t last_operation = NONE;
|
|
||||||
|
|
||||||
switch(c)
|
switch(c)
|
||||||
{
|
{
|
||||||
case '>':
|
case '>':
|
||||||
operation = MEM_POINTER_ADD;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '<':
|
case '<':
|
||||||
operation = MEM_POINTER_SUB;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '+':
|
case '+':
|
||||||
operation = MEM_ADD;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '-':
|
case '-':
|
||||||
operation = MEM_SUB;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '[':
|
case '[':
|
||||||
operation = JUMP_IF_0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ']':
|
case ']':
|
||||||
operation = JUMP_IF_NOT_0;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case '.':
|
case '.':
|
||||||
operation = PRINT_CHAR;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ',':
|
case ',':
|
||||||
operation = GET_CHAR;
|
fputc(c, stripped_file);
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(operation == NONE)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(operation)
|
rewind(stripped_file);
|
||||||
{
|
|
||||||
case MEM_POINTER_ADD:
|
|
||||||
case MEM_POINTER_SUB:
|
|
||||||
case MEM_ADD:
|
|
||||||
case MEM_SUB:
|
|
||||||
if(operation == last_operation)
|
|
||||||
{
|
|
||||||
operation++; // Go to advanced version of operation
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, -1, SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t nb = 2;
|
|
||||||
|
|
||||||
if(fwrite(&nb, sizeof(nb), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if(operation + 1 == last_operation)
|
|
||||||
{
|
|
||||||
operation++;
|
|
||||||
|
|
||||||
size_t nb;
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, -sizeof(nb), SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fread(&nb, sizeof(nb), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
nb++;
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, -sizeof(nb), SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fwrite(&nb, sizeof(nb), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JUMP_IF_0:
|
|
||||||
case JUMP_IF_NOT_0:
|
|
||||||
if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t jump_pos = 0;
|
|
||||||
|
|
||||||
if(fwrite(&jump_pos, sizeof(jump_pos), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PRINT_CHAR:
|
|
||||||
case GET_CHAR:
|
|
||||||
if(fwrite(&operation, sizeof(operation), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
last_operation = operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fclose(program_file) == EOF)
|
if(fclose(program_file) == EOF)
|
||||||
{
|
{
|
||||||
|
|
@ -238,295 +86,186 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
program_file = NULL;
|
program_file = NULL;
|
||||||
|
|
||||||
|
size_t brackets_pos[BRACKETS_NB_MAX];
|
||||||
|
size_t brackets_correspondances[BRACKETS_NB_MAX];
|
||||||
|
|
||||||
// Parse brackets
|
// Parse brackets
|
||||||
rewind(intermediate_representation_file);
|
size_t bracket_nb = 0;
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
operation_t operation;
|
int c = fgetc(stripped_file);
|
||||||
size_t nb_read = fread(&operation, sizeof(operation), 1, intermediate_representation_file);
|
|
||||||
|
|
||||||
if(nb_read != 1)
|
if(c == EOF)
|
||||||
{
|
{
|
||||||
if(feof(intermediate_representation_file))
|
int err = ferror(stripped_file);
|
||||||
|
|
||||||
|
if(err)
|
||||||
{
|
{
|
||||||
|
fprintf(stderr, "Error reading file: %d\n", ferror(stripped_file));
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(operation == JUMP_IF_0)
|
if(c == '[' || c == ']')
|
||||||
{
|
{
|
||||||
long bracket_pos = ftell(intermediate_representation_file);
|
brackets_pos[bracket_nb] = ftell(stripped_file);
|
||||||
|
|
||||||
if(bracket_pos == -1)
|
if(c == '[')
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error getting file pointer: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find corresponding closing bracket index
|
// Find corresponding closing bracket index
|
||||||
|
size_t corresponding_bracket_nb = bracket_nb;
|
||||||
|
|
||||||
for(size_t bracket_index = 1; bracket_index;)
|
for(size_t bracket_index = 1; bracket_index;)
|
||||||
{
|
{
|
||||||
operation_t operation_search;
|
int d = fgetc(stripped_file);
|
||||||
nb_read = fread(&operation_search, sizeof(operation_search), 1, intermediate_representation_file);
|
|
||||||
|
|
||||||
if(nb_read != 1)
|
if(d == EOF)
|
||||||
{
|
{
|
||||||
if(feof(intermediate_representation_file))
|
int err = ferror(stripped_file);
|
||||||
|
|
||||||
|
if(err)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Reached end of file before finding closing bracket.\n");
|
fprintf(stderr, "Error reading file: %d\n", argv[1], ferror(stripped_file));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
fprintf(stderr, "Reached EOF before finding closing bracket.");
|
||||||
}
|
}
|
||||||
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(operation_search)
|
if(d == '[' || d == ']')
|
||||||
{
|
{
|
||||||
case MEM_POINTER_ADD_NB:
|
corresponding_bracket_nb++;
|
||||||
case MEM_POINTER_SUB_NB:
|
|
||||||
case MEM_ADD_NB:
|
|
||||||
case MEM_SUB_NB:
|
|
||||||
case JUMP_IF_NOT_0:
|
|
||||||
if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(operation_search == JUMP_IF_0)
|
if(d == '[')
|
||||||
{
|
{
|
||||||
bracket_index++;
|
bracket_index++;
|
||||||
}
|
}
|
||||||
else if(operation_search == JUMP_IF_NOT_0)
|
else if(d == ']')
|
||||||
{
|
{
|
||||||
bracket_index--;
|
bracket_index--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long corresponding_bracket_pos = ftell(intermediate_representation_file);
|
brackets_correspondances[bracket_nb] = corresponding_bracket_nb;
|
||||||
|
brackets_correspondances[corresponding_bracket_nb] = bracket_nb;
|
||||||
|
|
||||||
if(corresponding_bracket_pos == -1)
|
fseek(stripped_file, brackets_pos[bracket_nb], SEEK_SET);
|
||||||
{
|
|
||||||
fprintf(stderr, "Error getting file pointer: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, -sizeof(size_t), SEEK_CUR))
|
bracket_nb++;
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t bracket_pos_size_t = (size_t)bracket_pos + sizeof(size_t);
|
|
||||||
|
|
||||||
if(fwrite(&bracket_pos_size_t, sizeof(bracket_pos_size_t), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, bracket_pos, SEEK_SET))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t corresponding_bracket_pos_size_t = corresponding_bracket_pos;
|
|
||||||
if(fwrite(&corresponding_bracket_pos_size_t, sizeof(corresponding_bracket_pos_size_t), 1, intermediate_representation_file) != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error writing file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(operation)
|
rewind(stripped_file);
|
||||||
{
|
|
||||||
case MEM_POINTER_ADD_NB:
|
|
||||||
case MEM_POINTER_SUB_NB:
|
|
||||||
case MEM_ADD_NB:
|
|
||||||
case MEM_SUB_NB:
|
|
||||||
case JUMP_IF_NOT_0:
|
|
||||||
fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rewind(intermediate_representation_file);
|
|
||||||
|
|
||||||
// Program memory
|
// Program memory
|
||||||
uint8_t mem[DATA_SIZE_MAX] = {0};
|
uint8_t mem[DATA_SIZE_MAX] = {0};
|
||||||
size_t mem_pointer = 0;
|
size_t mem_pointer = 0;
|
||||||
|
|
||||||
size_t nb_read;
|
size_t bracket_index = 0;
|
||||||
size_t operand;
|
|
||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
operation_t operation;
|
int c = fgetc(stripped_file);
|
||||||
nb_read = fread(&operation, sizeof(operation), 1, intermediate_representation_file);
|
|
||||||
|
|
||||||
if(nb_read != 1)
|
if(c == EOF)
|
||||||
{
|
{
|
||||||
if(feof(intermediate_representation_file))
|
int err = ferror(stripped_file);
|
||||||
|
|
||||||
|
if(err)
|
||||||
{
|
{
|
||||||
break;
|
fprintf(stderr, "Error reading file: %d\n", ferror(stripped_file));
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch(operation)
|
if(c == '[' || c == ']')
|
||||||
|
{
|
||||||
|
if(c == '[' ? !mem[mem_pointer] : mem[mem_pointer])
|
||||||
|
{
|
||||||
|
bracket_index = brackets_correspondances[bracket_index];
|
||||||
|
fseek(stripped_file, brackets_pos[bracket_index], SEEK_SET);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(c == '>')
|
||||||
{
|
{
|
||||||
case MEM_POINTER_ADD:
|
|
||||||
mem_pointer++;
|
mem_pointer++;
|
||||||
break;
|
|
||||||
|
|
||||||
case MEM_POINTER_ADD_NB:
|
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
mem_pointer += operand;
|
else if(c == '<')
|
||||||
break;
|
{
|
||||||
|
|
||||||
case MEM_POINTER_SUB:
|
|
||||||
mem_pointer--;
|
mem_pointer--;
|
||||||
break;
|
|
||||||
|
|
||||||
case MEM_POINTER_SUB_NB:
|
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
mem_pointer -= operand;
|
else if(c == '+')
|
||||||
break;
|
{
|
||||||
|
|
||||||
case MEM_ADD:
|
|
||||||
mem[mem_pointer]++;
|
mem[mem_pointer]++;
|
||||||
break;
|
|
||||||
|
|
||||||
case MEM_ADD_NB:
|
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
mem[mem_pointer] += operand;
|
else if(c == '-')
|
||||||
break;
|
{
|
||||||
|
|
||||||
case MEM_SUB:
|
|
||||||
mem[mem_pointer]--;
|
mem[mem_pointer]--;
|
||||||
break;
|
|
||||||
|
|
||||||
case MEM_SUB_NB:
|
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
}
|
||||||
mem[mem_pointer] -= operand;
|
else if(c == '.')
|
||||||
break;
|
|
||||||
|
|
||||||
case JUMP_IF_0:
|
|
||||||
if(mem[mem_pointer] == 0)
|
|
||||||
{
|
{
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, operand, SEEK_SET))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JUMP_IF_NOT_0:
|
|
||||||
if(mem[mem_pointer])
|
|
||||||
{
|
|
||||||
nb_read = fread(&operand, sizeof(operand), 1, intermediate_representation_file);
|
|
||||||
if(nb_read != 1)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error reading file: %d\n", ferror(intermediate_representation_file));
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(fseek(intermediate_representation_file, operand, SEEK_SET))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(fseek(intermediate_representation_file, sizeof(size_t), SEEK_CUR))
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Error seeking file: %d\n", errno);
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PRINT_CHAR:
|
|
||||||
putchar(mem[mem_pointer]);
|
putchar(mem[mem_pointer]);
|
||||||
break;
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
else if(c == ',')
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
static int next_input = -1;
|
||||||
|
int input;
|
||||||
|
|
||||||
case GET_CHAR:
|
if(next_input != -1)
|
||||||
|
{
|
||||||
|
input = next_input;
|
||||||
|
next_input = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
input = getchar();
|
||||||
|
}
|
||||||
|
|
||||||
|
next_input = getchar();
|
||||||
|
|
||||||
|
if(input != '\n')
|
||||||
|
{
|
||||||
|
mem[mem_pointer] = input;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(next_input == '\n')
|
||||||
|
{
|
||||||
|
mem[mem_pointer] = '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
next_input = -1;
|
||||||
|
}
|
||||||
|
*/
|
||||||
mem[mem_pointer] = getchar();
|
mem[mem_pointer] = getchar();
|
||||||
break;
|
}
|
||||||
|
|
||||||
default:
|
if(c == '[' || c == ']')
|
||||||
fprintf(stderr, "Invalid operation : %02d\n", operation);
|
{
|
||||||
goto end;
|
bracket_index++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
status = EXIT_SUCCESS;
|
status = EXIT_SUCCESS;
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if(intermediate_representation_file != NULL)
|
if(stripped_file != NULL)
|
||||||
{
|
{
|
||||||
if(fclose(intermediate_representation_file))
|
if(fclose(stripped_file))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error closing tmp file: %d\n", errno);
|
fprintf(stderr, "Failed to close tmp file: %d\n", errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -534,9 +273,13 @@ end:
|
||||||
{
|
{
|
||||||
if(fclose(program_file))
|
if(fclose(program_file))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Error closing tmp file: %d\n", errno);
|
fprintf(stderr, "Failed to close tmp file: %d\n", errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clock_t end_time = clock();
|
||||||
|
|
||||||
|
fprintf(stderr, "\nExecution time : %fs\n", (float)(end_time - start_time) / (float)CLOCKS_PER_SEC);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue