kodovani (splint mrdka)

This commit is contained in:
tovjemam 2025-01-04 22:24:52 +01:00
parent 01ee2257a4
commit 82e865e0d4
9 changed files with 117 additions and 119 deletions

View File

@ -1,45 +1,45 @@
#include "errors.h" #include "errors.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
void error_buffer_init(struct error_buffer *eb) { void error_buffer_init(struct error_buffer *eb) {
eb->err = ERR_NO_ERR; eb->err = ERR_NO_ERR;
eb->text_len = 0; eb->text_len = 0;
eb->text[0] = 0; eb->text[0] = 0;
} }
void error_set(struct error_buffer *eb, enum error_code err) { void error_set(struct error_buffer *eb, enum error_code err) {
eb->err = err; eb->err = err;
} }
enum error_code error_get(const struct error_buffer *eb) { enum error_code error_get(const struct error_buffer *eb) {
return eb->err; return eb->err;
} }
void error_printf(struct error_buffer *eb, const char *format, ...) { void error_printf(struct error_buffer *eb, const char *format, ...) {
va_list args; va_list args;
int space = MAX_ERROR_MESSAGE_LENGTH - eb->text_len; int space = MAX_ERROR_MESSAGE_LENGTH - eb->text_len;
int write_size; int write_size;
if (space == 0) if (space == 0)
return; return;
va_start(args, format); va_start(args, format);
write_size = vsnprintf(eb->text + eb->text_len, MAX_ERROR_MESSAGE_LENGTH - eb->text_len, format, args); write_size = vsnprintf(eb->text + eb->text_len, MAX_ERROR_MESSAGE_LENGTH - eb->text_len, format, args);
va_end(args); va_end(args);
if (write_size < 0) if (write_size < 0)
return; return;
if (write_size < space) { if (write_size < space) {
eb->text_len += write_size; eb->text_len += write_size;
} }
else { else {
eb->text_len = MAX_ERROR_MESSAGE_LENGTH; eb->text_len = MAX_ERROR_MESSAGE_LENGTH;
} }
} }
const char *error_get_text(const struct error_buffer *eb) { const char *error_get_text(const struct error_buffer *eb) {
return eb->text; return eb->text;
} }

132
errors.h
View File

@ -1,67 +1,67 @@
#ifndef ERRORS_H #ifndef ERRORS_H
#define ERRORS_H #define ERRORS_H
#include <stdlib.h> #include <stdlib.h>
#define MAX_ERROR_MESSAGE_LENGTH 512 #define MAX_ERROR_MESSAGE_LENGTH 512
/** /**
* @brief Chybové kódy * @brief Chybové kódy
*/ */
enum error_code { enum error_code {
ERR_NO_ERR = 0, /* Žádná chyba */ ERR_NO_ERR = 0, /* Žádná chyba */
ERR_INVALID_ARGS = 1, /* Neplatné argumenty programu */ ERR_INVALID_ARGS = 1, /* Neplatné argumenty programu */
ERR_INVALID_FUNCTION = 2, /* Zadaná matematická funkce je neplatná */ ERR_INVALID_FUNCTION = 2, /* Zadaná matematická funkce je neplatná */
ERR_INVALID_FILENAME = 3, /* Zadaný název souboru není platný */ ERR_INVALID_FILENAME = 3, /* Zadaný název souboru není platný */
ERR_INVALID_LIMITS = 4, /* Zadané hranice jsou ve špatném formátu */ ERR_INVALID_LIMITS = 4, /* Zadané hranice jsou ve špatném formátu */
ERR_BAD_ALLOC = 5 /* Při alokaci paměti nastala chyba */ ERR_BAD_ALLOC = 5 /* Při alokaci paměti nastala chyba */
}; };
/** /**
* @brief Zásobník pro chybový kód a řetězec popisující chybu * @brief Zásobník pro chybový kód a řetězec popisující chybu
*/ */
struct error_buffer { struct error_buffer {
enum error_code err; enum error_code err;
char text[MAX_ERROR_MESSAGE_LENGTH]; char text[MAX_ERROR_MESSAGE_LENGTH];
size_t text_len; size_t text_len;
}; };
/** /**
* @brief Inicializuje zásobník * @brief Inicializuje zásobník
* *
* @param eb Zásobník * @param eb Zásobník
*/ */
void error_buffer_init(struct error_buffer *eb); void error_buffer_init(struct error_buffer *eb);
/** /**
* @brief Nastaví chybový kód * @brief Nastaví chybový kód
* *
* @param eb Zásobník * @param eb Zásobník
* @param err Chybový kód * @param err Chybový kód
*/ */
void error_set(struct error_buffer *eb, enum error_code err); void error_set(struct error_buffer *eb, enum error_code err);
/** /**
* @brief Přidá do zásobníku formátovaný řetězec * @brief Přidá do zásobníku formátovaný řetězec
* *
* @param eb Zásobník * @param eb Zásobník
*/ */
void error_printf(struct error_buffer *eb, const char *format, ...); void error_printf(struct error_buffer *eb, const char *format, ...);
/** /**
* Vrátí chybový kód * Vrátí chybový kód
* *
* @param eb Zásobník * @param eb Zásobník
* @return Chybový kód * @return Chybový kód
*/ */
enum error_code error_get(const struct error_buffer *eb); enum error_code error_get(const struct error_buffer *eb);
/** /**
* Vrátí řetězec popisující chybu * Vrátí řetězec popisující chybu
* *
* @param eb Zásobník * @param eb Zásobník
* @return Řetězec popisující chybu * @return Řetězec popisující chybu
*/ */
const char *error_get_text(const struct error_buffer *eb); const char *error_get_text(const struct error_buffer *eb);
#endif /* ERRORS_H */ #endif /* ERRORS_H */

2
lex.c
View File

@ -1,4 +1,4 @@
#include "lex.h" #include "lex.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>

4
lex.h
View File

@ -1,10 +1,8 @@
#ifndef LEX_H #ifndef LEX_H
#define LEX_H #define LEX_H
#include "errors.h" #include "errors.h"
#define LEX_DEBUG
/** /**
* @brief Typ tokenu * @brief Typ tokenu
*/ */

View File

@ -1,4 +1,4 @@
#ifndef MATH_FUNCTIONS_H #ifndef MATH_FUNCTIONS_H
#define MATH_FUNCTIONS_H #define MATH_FUNCTIONS_H
#include <stdlib.h> #include <stdlib.h>

View File

@ -1,4 +1,4 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include "parser.h" #include "parser.h"

View File

@ -1,4 +1,4 @@
#ifndef PARSER_H #ifndef PARSER_H
#define PARSER_H #define PARSER_H
#include "tree.h" #include "tree.h"

View File

@ -11,4 +11,4 @@ struct graph_range {
void ps_generate_graph(FILE *file, const struct expr_node *node, const struct graph_range *range, const char *function); void ps_generate_graph(FILE *file, const struct expr_node *node, const struct graph_range *range, const char *function);
#endif #endif /* PS_GRAPH_H */

2
tree.h
View File

@ -1,4 +1,4 @@
#ifndef TREE_H #ifndef TREE_H
#define TREE_H #define TREE_H
#include "lex.h" #include "lex.h"