Implement read file and extract tokens.

This commit is contained in:
Yannick Reiß 2023-09-21 07:47:19 +02:00
parent 14254b7db2
commit 1b517751f9
No known key found for this signature in database
GPG Key ID: 5A3AF456F0A0338C
3 changed files with 120 additions and 0 deletions

View File

@ -2,5 +2,17 @@
#define TOKENIZER_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @name extractTokens
* @return int
* @brief Extract only allowed characters from buffer;
*
* @param char* buffer: extractTokens_buffer
* @param char* tokens: extractTokens_tokens
*/
int extractTokens (char* buffer, char* tokens);
#endif//TOKENIZER_H

View File

@ -1,4 +1,5 @@
#include <stdlib.h>
#include <stdio.h>
#include "../include/analyzer.h"
#include "../include/assembling.h"
@ -20,5 +21,69 @@ int main (int argc, char** argv) {
exit(EXIT_FAILURE);
}
/* Parse arguments */
char* filename = argv[1];
/* read file into buffer */
FILE* fd = 0; /* File descriptor */
fd = fopen(filename, "r");
/* make sure the file was loaded successfully */
if (!fd) {
perror("ERROR: Could not open file!");
exit(EXIT_FAILURE);
}
/* create buffer and read file */
size_t chunk_size = 512;
char* buffer;
/* Allocate memory for buffer */
buffer = malloc(sizeof(char) * chunk_size);
if (!buffer) {
/* Error */
perror("malloc error on memory allocation of: buffer");
exit(EXIT_FAILURE);
}
size_t total_size;
size_t bytes_read;
size_t buffer_size = chunk_size;
for (;( bytes_read = fread(buffer + total_size, 1, chunk_size, fd) ) > 0;) {
total_size += bytes_read;
if ( total_size + chunk_size > buffer_size ) {
buffer_size += chunk_size;
buffer = (char*)realloc(buffer, buffer_size);
if (!buffer) {
perror("ERROR: Could not reallocate file buffer!");
exit(EXIT_FAILURE);
}
}
}
/* extract tokens from buffer */
char* tokens;
/* Allocate memory for tokens */
tokens = (char*)malloc(sizeof(char) * 1);
if (!tokens) {
/* Error */
perror("malloc error on memory allocation of: tokens");
exit(EXIT_FAILURE);
}
extractTokens(buffer, tokens);
/* end of lifetime for buffer */
free( buffer );
/* Just print the tokens as example. */
for (char* index = tokens; *index; index++) {
(void)printf("%c", *index);
}
(void)printf("\n");
return rv;
}

View File

@ -1 +1,44 @@
#include "../include/tokenizer.h"
static char operators[] = {'>', '<', '+', '-', ',', '.', '[', ']'};
/**
* @name extractTokens
* @return int
* @brief Extract only allowed tokens from buffer.
*
* @param char* buffer
* @param char* tokens
*/
int extractTokens (char* buffer, char* tokens) {
int rv = 0;
size_t token_size = 1;
size_t tokens_found = 0;
for (char* cursor = buffer; *cursor; cursor++) {
/* check tokens size and reallocate if necessary */
if (tokens_found >= token_size) {
token_size *= 2;
tokens = (char*)realloc(tokens, token_size);
if (!tokens) {
perror("ERROR: Could not expand token array!");
exit(-1);
}
}
/* check if current symbol is a token */
for (int i = 0; i < 8; i++) {
if (*cursor == operators[i]) {
tokens[tokens_found] = *cursor;
tokens_found++;
}
}
}
rv = tokens_found;
return rv;
}