From efb9f269cd81055c231ecd05956cce2ea31f9278 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 12:54:47 +0000 Subject: [PATCH 001/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index cb3e8ae7b..e17df4c2c 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1250,7 +1250,7 @@ int roll_full_options( int enable_verbosity, int enable_introspection, int enable_mocking, - // int enable_builtins, + int enable_builtins, int mocking_type, int mocking_seed ){ @@ -1290,20 +1290,20 @@ int roll_full_options( } initialize(); - /* + if(enable_builtins){ load_builtins("builtins/"); } - */ + YY_BUFFER_STATE buffer = yy_scan_string(roll_request); yyparse(); yy_delete_buffer(buffer); return gnoll_errno; } -/*void load_builtins(char* root){ +void load_builtins(char* root){ return; -}*/ +} // The following are legacy functions to be deprecated in the future // in favor of the general roll_full_options() fn. From eb8be0300d61d62d1f22cfec51ef543e5e0da671 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 12:56:22 +0000 Subject: [PATCH 002/138] Update shared_header.h Signed-off-by: Ian Hunter --- src/grammar/shared_header.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/grammar/shared_header.h b/src/grammar/shared_header.h index 0a0747420..e6193717d 100644 --- a/src/grammar/shared_header.h +++ b/src/grammar/shared_header.h @@ -15,6 +15,7 @@ int roll_full_options( int enable_verbosity, int enable_introspection, int enable_mocking, + int enable_builtins, int mocking_type, int mocking_seed ); @@ -25,6 +26,8 @@ int roll_and_write(char* s, char* f); void roll_and_write_R(int* return_code, char** s, char** f ); int mock_roll(char* s, char* f, int mock_value, int mock_const); +void load_builtins(char* root); + #ifdef __cplusplus } #endif From 97a948b4da2fa637941b4f0f39eefff7dc99d3a7 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 12:59:46 +0000 Subject: [PATCH 003/138] Create tinydir.h Signed-off-by: Ian Hunter --- src/grammar/external/tinydir.h | 838 +++++++++++++++++++++++++++++++++ 1 file changed, 838 insertions(+) create mode 100644 src/grammar/external/tinydir.h diff --git a/src/grammar/external/tinydir.h b/src/grammar/external/tinydir.h new file mode 100644 index 000000000..ba20c3e49 --- /dev/null +++ b/src/grammar/external/tinydir.h @@ -0,0 +1,838 @@ +/* +Copyright (c) 2013-2021, tinydir authors: +- Cong Xu +- Lautis Sun +- Baudouin Feildel +- Andargor +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +#ifndef TINYDIR_H +#define TINYDIR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if ((defined _UNICODE) && !(defined UNICODE)) +#define UNICODE +#endif + +#if ((defined UNICODE) && !(defined _UNICODE)) +#define _UNICODE +#endif + +#include +#include +#include +#ifdef _MSC_VER +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +# include +# pragma warning(push) +# pragma warning (disable : 4996) +#else +# include +# include +# include +# include +#endif +#ifdef __MINGW32__ +# include +#endif + + +/* types */ + +/* Windows UNICODE wide character support */ +#if defined _MSC_VER || defined __MINGW32__ +# define _tinydir_char_t TCHAR +# define TINYDIR_STRING(s) _TEXT(s) +# define _tinydir_strlen _tcslen +# define _tinydir_strcpy _tcscpy +# define _tinydir_strcat _tcscat +# define _tinydir_strcmp _tcscmp +# define _tinydir_strrchr _tcsrchr +# define _tinydir_strncmp _tcsncmp +#else +# define _tinydir_char_t char +# define TINYDIR_STRING(s) s +# define _tinydir_strlen strlen +# define _tinydir_strcpy strcpy +# define _tinydir_strcat strcat +# define _tinydir_strcmp strcmp +# define _tinydir_strrchr strrchr +# define _tinydir_strncmp strncmp +#endif + +#if (defined _MSC_VER || defined __MINGW32__) +# include +# define _TINYDIR_PATH_MAX MAX_PATH +#elif defined __linux__ +# include +# ifdef PATH_MAX +# define _TINYDIR_PATH_MAX PATH_MAX +# endif +#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) +# include +# if defined(BSD) +# include +# ifdef PATH_MAX +# define _TINYDIR_PATH_MAX PATH_MAX +# endif +# endif +#endif + +#ifndef _TINYDIR_PATH_MAX +#define _TINYDIR_PATH_MAX 4096 +#endif + +#ifdef _MSC_VER +/* extra chars for the "\\*" mask */ +# define _TINYDIR_PATH_EXTRA 2 +#else +# define _TINYDIR_PATH_EXTRA 0 +#endif + +#define _TINYDIR_FILENAME_MAX 256 + +#if (defined _MSC_VER || defined __MINGW32__) +#define _TINYDIR_DRIVE_MAX 3 +#endif + +#ifdef _MSC_VER +# define _TINYDIR_FUNC static __inline +#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L +# define _TINYDIR_FUNC static __inline__ +#elif defined(__cplusplus) +# define _TINYDIR_FUNC static inline +#elif defined(__GNUC__) +/* Suppress unused function warning */ +# define _TINYDIR_FUNC __attribute__((unused)) static +#else +# define _TINYDIR_FUNC static +#endif + +/* readdir_r usage; define TINYDIR_USE_READDIR_R to use it (if supported) */ +#ifdef TINYDIR_USE_READDIR_R + +/* readdir_r is a POSIX-only function, and may not be available under various + * environments/settings, e.g. MinGW. Use readdir fallback */ +#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||\ + _POSIX_SOURCE +# define _TINYDIR_HAS_READDIR_R +#endif +#if _POSIX_C_SOURCE >= 200112L +# define _TINYDIR_HAS_FPATHCONF +# include +#endif +#if _BSD_SOURCE || _SVID_SOURCE || \ + (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) +# define _TINYDIR_HAS_DIRFD +# include +#endif +#if defined _TINYDIR_HAS_FPATHCONF && defined _TINYDIR_HAS_DIRFD &&\ + defined _PC_NAME_MAX +# define _TINYDIR_USE_FPATHCONF +#endif +#if defined __MINGW32__ || !defined _TINYDIR_HAS_READDIR_R ||\ + !(defined _TINYDIR_USE_FPATHCONF || defined NAME_MAX) +# define _TINYDIR_USE_READDIR +#endif + +/* Use readdir by default */ +#else +# define _TINYDIR_USE_READDIR +#endif + +/* MINGW32 has two versions of dirent, ASCII and UNICODE*/ +#ifndef _MSC_VER +#if (defined __MINGW32__) && (defined _UNICODE) +#define _TINYDIR_DIR _WDIR +#define _tinydir_dirent _wdirent +#define _tinydir_opendir _wopendir +#define _tinydir_readdir _wreaddir +#define _tinydir_closedir _wclosedir +#else +#define _TINYDIR_DIR DIR +#define _tinydir_dirent dirent +#define _tinydir_opendir opendir +#define _tinydir_readdir readdir +#define _tinydir_closedir closedir +#endif +#endif + +/* Allow user to use a custom allocator by defining _TINYDIR_MALLOC and _TINYDIR_FREE. */ +#if defined(_TINYDIR_MALLOC) && defined(_TINYDIR_FREE) +#elif !defined(_TINYDIR_MALLOC) && !defined(_TINYDIR_FREE) +#else +#error "Either define both alloc and free or none of them!" +#endif + +#if !defined(_TINYDIR_MALLOC) + #define _TINYDIR_MALLOC(_size) malloc(_size) + #define _TINYDIR_FREE(_ptr) free(_ptr) +#endif /* !defined(_TINYDIR_MALLOC) */ + +typedef struct tinydir_file +{ + _tinydir_char_t path[_TINYDIR_PATH_MAX]; + _tinydir_char_t name[_TINYDIR_FILENAME_MAX]; + _tinydir_char_t *extension; + int is_dir; + int is_reg; + +#ifndef _MSC_VER +#ifdef __MINGW32__ + struct _stat _s; +#else + struct stat _s; +#endif +#endif +} tinydir_file; + +typedef struct tinydir_dir +{ + _tinydir_char_t path[_TINYDIR_PATH_MAX]; + int has_next; + size_t n_files; + + tinydir_file *_files; +#ifdef _MSC_VER + HANDLE _h; + WIN32_FIND_DATA _f; +#else + _TINYDIR_DIR *_d; + struct _tinydir_dirent *_e; +#ifndef _TINYDIR_USE_READDIR + struct _tinydir_dirent *_ep; +#endif +#endif +} tinydir_dir; + + +/* declarations */ + +_TINYDIR_FUNC +int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path); +_TINYDIR_FUNC +int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path); +_TINYDIR_FUNC +void tinydir_close(tinydir_dir *dir); + +_TINYDIR_FUNC +int tinydir_next(tinydir_dir *dir); +_TINYDIR_FUNC +int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file); +_TINYDIR_FUNC +int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, size_t i); +_TINYDIR_FUNC +int tinydir_open_subdir_n(tinydir_dir *dir, size_t i); + +_TINYDIR_FUNC +int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path); +_TINYDIR_FUNC +void _tinydir_get_ext(tinydir_file *file); +_TINYDIR_FUNC +int _tinydir_file_cmp(const void *a, const void *b); +#ifndef _MSC_VER +#ifndef _TINYDIR_USE_READDIR +_TINYDIR_FUNC +size_t _tinydir_dirent_buf_size(_TINYDIR_DIR *dirp); +#endif +#endif + + +/* definitions*/ + +_TINYDIR_FUNC +int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path) +{ +#ifndef _MSC_VER +#ifndef _TINYDIR_USE_READDIR + int error; + int size; /* using int size */ +#endif +#else + _tinydir_char_t path_buf[_TINYDIR_PATH_MAX]; +#endif + _tinydir_char_t *pathp; + + if (dir == NULL || path == NULL || _tinydir_strlen(path) == 0) + { + errno = EINVAL; + return -1; + } + if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) + { + errno = ENAMETOOLONG; + return -1; + } + + /* initialise dir */ + dir->_files = NULL; +#ifdef _MSC_VER + dir->_h = INVALID_HANDLE_VALUE; +#else + dir->_d = NULL; +#ifndef _TINYDIR_USE_READDIR + dir->_ep = NULL; +#endif +#endif + tinydir_close(dir); + + _tinydir_strcpy(dir->path, path); + /* Remove trailing slashes */ + pathp = &dir->path[_tinydir_strlen(dir->path) - 1]; + while (pathp != dir->path && (*pathp == TINYDIR_STRING('\\') || *pathp == TINYDIR_STRING('/'))) + { + *pathp = TINYDIR_STRING('\0'); + pathp++; + } +#ifdef _MSC_VER + _tinydir_strcpy(path_buf, dir->path); + _tinydir_strcat(path_buf, TINYDIR_STRING("\\*")); +#if (defined WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP) + dir->_h = FindFirstFileEx(path_buf, FindExInfoStandard, &dir->_f, FindExSearchNameMatch, NULL, 0); +#else + dir->_h = FindFirstFile(path_buf, &dir->_f); +#endif + if (dir->_h == INVALID_HANDLE_VALUE) + { + errno = ENOENT; +#else + dir->_d = _tinydir_opendir(path); + if (dir->_d == NULL) + { +#endif + goto bail; + } + + /* read first file */ + dir->has_next = 1; +#ifndef _MSC_VER +#ifdef _TINYDIR_USE_READDIR + dir->_e = _tinydir_readdir(dir->_d); +#else + /* allocate dirent buffer for readdir_r */ + size = _tinydir_dirent_buf_size(dir->_d); /* conversion to int */ + if (size == -1) return -1; + dir->_ep = (struct _tinydir_dirent*)_TINYDIR_MALLOC(size); + if (dir->_ep == NULL) return -1; + + error = readdir_r(dir->_d, dir->_ep, &dir->_e); + if (error != 0) return -1; +#endif + if (dir->_e == NULL) + { + dir->has_next = 0; + } +#endif + + return 0; + +bail: + tinydir_close(dir); + return -1; +} + +_TINYDIR_FUNC +int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path) +{ + /* Count the number of files first, to pre-allocate the files array */ + size_t n_files = 0; + if (tinydir_open(dir, path) == -1) + { + return -1; + } + while (dir->has_next) + { + n_files++; + if (tinydir_next(dir) == -1) + { + goto bail; + } + } + tinydir_close(dir); + + if (n_files == 0 || tinydir_open(dir, path) == -1) + { + return -1; + } + + dir->n_files = 0; + dir->_files = (tinydir_file *)_TINYDIR_MALLOC(sizeof *dir->_files * n_files); + if (dir->_files == NULL) + { + goto bail; + } + while (dir->has_next) + { + tinydir_file *p_file; + dir->n_files++; + + p_file = &dir->_files[dir->n_files - 1]; + if (tinydir_readfile(dir, p_file) == -1) + { + goto bail; + } + + if (tinydir_next(dir) == -1) + { + goto bail; + } + + /* Just in case the number of files has changed between the first and + second reads, terminate without writing into unallocated memory */ + if (dir->n_files == n_files) + { + break; + } + } + + qsort(dir->_files, dir->n_files, sizeof(tinydir_file), _tinydir_file_cmp); + + return 0; + +bail: + tinydir_close(dir); + return -1; +} + +_TINYDIR_FUNC +void tinydir_close(tinydir_dir *dir) +{ + if (dir == NULL) + { + return; + } + + memset(dir->path, 0, sizeof(dir->path)); + dir->has_next = 0; + dir->n_files = 0; + _TINYDIR_FREE(dir->_files); + dir->_files = NULL; +#ifdef _MSC_VER + if (dir->_h != INVALID_HANDLE_VALUE) + { + FindClose(dir->_h); + } + dir->_h = INVALID_HANDLE_VALUE; +#else + if (dir->_d) + { + _tinydir_closedir(dir->_d); + } + dir->_d = NULL; + dir->_e = NULL; +#ifndef _TINYDIR_USE_READDIR + _TINYDIR_FREE(dir->_ep); + dir->_ep = NULL; +#endif +#endif +} + +_TINYDIR_FUNC +int tinydir_next(tinydir_dir *dir) +{ + if (dir == NULL) + { + errno = EINVAL; + return -1; + } + if (!dir->has_next) + { + errno = ENOENT; + return -1; + } + +#ifdef _MSC_VER + if (FindNextFile(dir->_h, &dir->_f) == 0) +#else +#ifdef _TINYDIR_USE_READDIR + dir->_e = _tinydir_readdir(dir->_d); +#else + if (dir->_ep == NULL) + { + return -1; + } + if (readdir_r(dir->_d, dir->_ep, &dir->_e) != 0) + { + return -1; + } +#endif + if (dir->_e == NULL) +#endif + { + dir->has_next = 0; +#ifdef _MSC_VER + if (GetLastError() != ERROR_SUCCESS && + GetLastError() != ERROR_NO_MORE_FILES) + { + tinydir_close(dir); + errno = EIO; + return -1; + } +#endif + } + + return 0; +} + +_TINYDIR_FUNC +int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file) +{ + const _tinydir_char_t *filename; + if (dir == NULL || file == NULL) + { + errno = EINVAL; + return -1; + } +#ifdef _MSC_VER + if (dir->_h == INVALID_HANDLE_VALUE) +#else + if (dir->_e == NULL) +#endif + { + errno = ENOENT; + return -1; + } + filename = +#ifdef _MSC_VER + dir->_f.cFileName; +#else + dir->_e->d_name; +#endif + if (_tinydir_strlen(dir->path) + + _tinydir_strlen(filename) + 1 + _TINYDIR_PATH_EXTRA >= + _TINYDIR_PATH_MAX) + { + /* the path for the file will be too long */ + errno = ENAMETOOLONG; + return -1; + } + if (_tinydir_strlen(filename) >= _TINYDIR_FILENAME_MAX) + { + errno = ENAMETOOLONG; + return -1; + } + + _tinydir_strcpy(file->path, dir->path); + if (_tinydir_strcmp(dir->path, TINYDIR_STRING("/")) != 0) + _tinydir_strcat(file->path, TINYDIR_STRING("/")); + _tinydir_strcpy(file->name, filename); + _tinydir_strcat(file->path, filename); +#ifndef _MSC_VER +#ifdef __MINGW32__ + if (_tstat( +#elif (defined _BSD_SOURCE) || (defined _DEFAULT_SOURCE) \ + || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \ + || ((defined _POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \ + || ((defined __APPLE__) && (defined __MACH__)) \ + || (defined BSD) + if (lstat( +#else + if (stat( +#endif + file->path, &file->_s) == -1) + { + return -1; + } +#endif + _tinydir_get_ext(file); + + file->is_dir = +#ifdef _MSC_VER + !!(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); +#else + S_ISDIR(file->_s.st_mode); +#endif + file->is_reg = +#ifdef _MSC_VER + !!(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || + ( + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) && + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) && +#ifdef FILE_ATTRIBUTE_INTEGRITY_STREAM + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_INTEGRITY_STREAM) && +#endif +#ifdef FILE_ATTRIBUTE_NO_SCRUB_DATA + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_NO_SCRUB_DATA) && +#endif + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) && + !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)); +#else + S_ISREG(file->_s.st_mode); +#endif + + return 0; +} + +_TINYDIR_FUNC +int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, size_t i) +{ + if (dir == NULL || file == NULL) + { + errno = EINVAL; + return -1; + } + if (i >= dir->n_files) + { + errno = ENOENT; + return -1; + } + + memcpy(file, &dir->_files[i], sizeof(tinydir_file)); + _tinydir_get_ext(file); + + return 0; +} + +_TINYDIR_FUNC +int tinydir_open_subdir_n(tinydir_dir *dir, size_t i) +{ + _tinydir_char_t path[_TINYDIR_PATH_MAX]; + if (dir == NULL) + { + errno = EINVAL; + return -1; + } + if (i >= dir->n_files || !dir->_files[i].is_dir) + { + errno = ENOENT; + return -1; + } + + _tinydir_strcpy(path, dir->_files[i].path); + tinydir_close(dir); + if (tinydir_open_sorted(dir, path) == -1) + { + return -1; + } + + return 0; +} + +/* Open a single file given its path */ +_TINYDIR_FUNC +int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) +{ + tinydir_dir dir; + int result = 0; + int found = 0; + _tinydir_char_t dir_name_buf[_TINYDIR_PATH_MAX]; + _tinydir_char_t file_name_buf[_TINYDIR_FILENAME_MAX]; + _tinydir_char_t *dir_name; + _tinydir_char_t *base_name; +#if (defined _MSC_VER || defined __MINGW32__) + _tinydir_char_t drive_buf[_TINYDIR_PATH_MAX]; + _tinydir_char_t ext_buf[_TINYDIR_FILENAME_MAX]; +#endif + + if (file == NULL || path == NULL || _tinydir_strlen(path) == 0) + { + errno = EINVAL; + return -1; + } + if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) + { + errno = ENAMETOOLONG; + return -1; + } + + /* Get the parent path */ +#if (defined _MSC_VER || defined __MINGW32__) +#if ((defined _MSC_VER) && (_MSC_VER >= 1400)) + errno = _tsplitpath_s( + path, + drive_buf, _TINYDIR_DRIVE_MAX, + dir_name_buf, _TINYDIR_FILENAME_MAX, + file_name_buf, _TINYDIR_FILENAME_MAX, + ext_buf, _TINYDIR_FILENAME_MAX); +#else + _tsplitpath( + path, + drive_buf, + dir_name_buf, + file_name_buf, + ext_buf); +#endif + + if (errno) + { + return -1; + } + +/* _splitpath_s not work fine with only filename and widechar support */ +#ifdef _UNICODE + if (drive_buf[0] == L'\xFEFE') + drive_buf[0] = '\0'; + if (dir_name_buf[0] == L'\xFEFE') + dir_name_buf[0] = '\0'; +#endif + + /* Emulate the behavior of dirname by returning "." for dir name if it's + empty */ + if (drive_buf[0] == '\0' && dir_name_buf[0] == '\0') + { + _tinydir_strcpy(dir_name_buf, TINYDIR_STRING(".")); + } + /* Concatenate the drive letter and dir name to form full dir name */ + _tinydir_strcat(drive_buf, dir_name_buf); + dir_name = drive_buf; + /* Concatenate the file name and extension to form base name */ + _tinydir_strcat(file_name_buf, ext_buf); + base_name = file_name_buf; +#else + _tinydir_strcpy(dir_name_buf, path); + dir_name = dirname(dir_name_buf); + _tinydir_strcpy(file_name_buf, path); + base_name = basename(file_name_buf); +#endif + + /* Special case: if the path is a root dir, open the parent dir as the file */ +#if (defined _MSC_VER || defined __MINGW32__) + if (_tinydir_strlen(base_name) == 0) +#else + if ((_tinydir_strcmp(base_name, TINYDIR_STRING("/"))) == 0) +#endif + { + memset(file, 0, sizeof * file); + file->is_dir = 1; + file->is_reg = 0; + _tinydir_strcpy(file->path, dir_name); + file->extension = file->path + _tinydir_strlen(file->path); + return 0; + } + + /* Open the parent directory */ + if (tinydir_open(&dir, dir_name) == -1) + { + return -1; + } + + /* Read through the parent directory and look for the file */ + while (dir.has_next) + { + if (tinydir_readfile(&dir, file) == -1) + { + result = -1; + goto bail; + } + if (_tinydir_strcmp(file->name, base_name) == 0) + { + /* File found */ + found = 1; + break; + } + tinydir_next(&dir); + } + if (!found) + { + result = -1; + errno = ENOENT; + } + +bail: + tinydir_close(&dir); + return result; +} + +_TINYDIR_FUNC +void _tinydir_get_ext(tinydir_file *file) +{ + _tinydir_char_t *period = _tinydir_strrchr(file->name, TINYDIR_STRING('.')); + if (period == NULL) + { + file->extension = &(file->name[_tinydir_strlen(file->name)]); + } + else + { + file->extension = period + 1; + } +} + +_TINYDIR_FUNC +int _tinydir_file_cmp(const void *a, const void *b) +{ + const tinydir_file *fa = (const tinydir_file *)a; + const tinydir_file *fb = (const tinydir_file *)b; + if (fa->is_dir != fb->is_dir) + { + return -(fa->is_dir - fb->is_dir); + } + return _tinydir_strncmp(fa->name, fb->name, _TINYDIR_FILENAME_MAX); +} + +#ifndef _MSC_VER +#ifndef _TINYDIR_USE_READDIR +/* +The following authored by Ben Hutchings +from https://womble.decadent.org.uk/readdir_r-advisory.html +*/ +/* Calculate the required buffer size (in bytes) for directory * +* entries read from the given directory handle. Return -1 if this * +* this cannot be done. * +* * +* This code does not trust values of NAME_MAX that are less than * +* 255, since some systems (including at least HP-UX) incorrectly * +* define it to be a smaller value. */ +_TINYDIR_FUNC +size_t _tinydir_dirent_buf_size(_TINYDIR_DIR *dirp) +{ + long name_max; + size_t name_end; + /* parameter may be unused */ + (void)dirp; + +#if defined _TINYDIR_USE_FPATHCONF + name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX); + if (name_max == -1) +#if defined(NAME_MAX) + name_max = (NAME_MAX > 255) ? NAME_MAX : 255; +#else + return (size_t)(-1); +#endif +#elif defined(NAME_MAX) + name_max = (NAME_MAX > 255) ? NAME_MAX : 255; +#else +#error "buffer size for readdir_r cannot be determined" +#endif + name_end = (size_t)offsetof(struct _tinydir_dirent, d_name) + name_max + 1; + return (name_end > sizeof(struct _tinydir_dirent) ? + name_end : sizeof(struct _tinydir_dirent)); +} +#endif +#endif + +#ifdef __cplusplus +} +#endif + +# if defined (_MSC_VER) +# pragma warning(pop) +# endif + +#endif From 1aaa19e76f36e71beb70a34266d85c0c616b72d2 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 13:17:25 +0000 Subject: [PATCH 004/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index e17df4c2c..b26faeec9 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1302,6 +1302,25 @@ int roll_full_options( } void load_builtins(char* root){ + tinydir_dir dir; + tinydir_open(&dir, root); + + while (dir.has_next) + { + tinydir_file file; + tinydir_readfile(&dir, &file); + + printf("%s", file.name); + if (file.is_dir) + { + printf("/"); + } + printf("\n"); + + tinydir_next(&dir); + } + + tinydir_close(&dir); return; } @@ -1309,23 +1328,23 @@ void load_builtins(char* root){ // in favor of the general roll_full_options() fn. int roll(char * s){ - return roll_full_options(s, NULL, 1, 0, 0, 0, 0); + return roll_full_options(s, NULL, 1, 0, 0, 0, 0, 0); } int roll_with_breakdown(char * s, char* f){ - return roll_full_options(s, f, 0, 1, 0, 0, 0); + return roll_full_options(s, f, 0, 1, 0, 0, 0, 0); } int roll_and_write(char* s, char* f){ - return roll_full_options(s, f, 0, 0, 0, 0, 0); + return roll_full_options(s, f, 0, 0, 0, 0, 0, 0); } void roll_and_write_R(int* return_code, char** s, char** f){ - (*return_code) = roll_full_options(s[0], f[0], 0, 0, 0, 0, 0); + (*return_code) = roll_full_options(s[0], f[0], 0, 0, 0, 0, 0, 0); } int mock_roll(char * s, char * f, int mock_value, int mock_const){ - return roll_full_options(s, f, 0, 0, 1, mock_value, mock_const); + return roll_full_options(s, f, 0, 0, 1, 0, mock_value, mock_const); } char * concat_strings(char ** s, int num_s){ From efa0d9078b245d71cea5cb25c28bf466382fe338 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 13:32:19 +0000 Subject: [PATCH 005/138] Update parser.py Signed-off-by: Ian Hunter --- src/python/code/gnoll/parser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 92a9c4836..56d286dad 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -45,7 +45,7 @@ def raise_gnoll_error(value): raise err -def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False): +def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=False): """ Parse some dice notation with GNOLL. @param s the string to parse @@ -103,6 +103,7 @@ def extract_from_dice_file(lines, seperator): mock is not None, # enable_mock mock, mock_const, + builtins ) if verbose: From d60f94fcc39ce23d4374567ae938dab446cee399 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 13:32:30 +0000 Subject: [PATCH 006/138] Format code with black, gofmt, yapf, autopep8, isort and gofumpt --- src/python/code/gnoll/parser.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 56d286dad..2e3afaa2b 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -45,7 +45,12 @@ def raise_gnoll_error(value): raise err -def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=False): +def roll(s, + verbose=False, + mock=None, + mock_const=3, + breakdown=False, + builtins=False): """ Parse some dice notation with GNOLL. @param s the string to parse @@ -103,7 +108,7 @@ def extract_from_dice_file(lines, seperator): mock is not None, # enable_mock mock, mock_const, - builtins + builtins, ) if verbose: From 2253878d87dad978e4d16a2b4042b9d37be80b9a Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 13:38:11 +0000 Subject: [PATCH 007/138] Update util.py Signed-off-by: Ian Hunter --- tests/python/util.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/python/util.py b/tests/python/util.py index 5b481e31d..1096cc2dc 100644 --- a/tests/python/util.py +++ b/tests/python/util.py @@ -52,7 +52,7 @@ def make_all(): raise ValueError -def roll(s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True): +def roll(s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, builtins=False): global first_run if first_run: @@ -67,7 +67,9 @@ def roll(s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True): mock=mock_mode.value, mock_const=mock_const, verbose=verbose, - breakdown=True) + breakdown=True, + builtins=builtins + ) exit_code = dt_return[0] result = dt_return[1] dice_breakdown = dt_return[2] if len(dt_return) > 2 else [] From b3235c693c2d9d2ee75283627259fb88c6d8168d Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 13:38:23 +0000 Subject: [PATCH 008/138] Format code with black, gofmt, yapf, autopep8, isort and gofumpt --- tests/python/util.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/python/util.py b/tests/python/util.py index 1096cc2dc..ed815691c 100644 --- a/tests/python/util.py +++ b/tests/python/util.py @@ -52,7 +52,12 @@ def make_all(): raise ValueError -def roll(s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, builtins=False): +def roll(s, + mock_mode=Mock.NO_MOCK, + mock_const=3, + verbose=False, + squeeze=True, + builtins=False): global first_run if first_run: @@ -63,13 +68,14 @@ def roll(s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, b # Get module now - post make dice_tower_roll = get_roll() - dt_return = dice_tower_roll(s, - mock=mock_mode.value, - mock_const=mock_const, - verbose=verbose, - breakdown=True, - builtins=builtins - ) + dt_return = dice_tower_roll( + s, + mock=mock_mode.value, + mock_const=mock_const, + verbose=verbose, + breakdown=True, + builtins=builtins, + ) exit_code = dt_return[0] result = dt_return[1] dice_breakdown = dt_return[2] if len(dt_return) > 2 else [] From 319a0ec5dbcc420ad0fff64c62e6efeb8cfeffb7 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 13:41:48 +0000 Subject: [PATCH 009/138] Update test_macros.py Signed-off-by: Ian Hunter --- tests/python/test_macros.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index 522f35444..cb1c7de64 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -54,6 +54,9 @@ def test_undefined_macro(): except Exception as e: error_handled_by_gnoll(e) +def test_undefined_macro(): + r = roll("@ORACLE", builtins=True) + assert(r in ["YES","YES_AND","YES_BUT","NO","NO_AND","NO_BUT"]) def test_builtins(): # Check that builtins are valid calls From b4a129b388b3dd328209d50eceff412b91beb420 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 13:42:00 +0000 Subject: [PATCH 010/138] Format code with black, gofmt, yapf, autopep8, isort and gofumpt --- tests/python/test_macros.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index cb1c7de64..bee30877f 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -54,9 +54,11 @@ def test_undefined_macro(): except Exception as e: error_handled_by_gnoll(e) + def test_undefined_macro(): r = roll("@ORACLE", builtins=True) - assert(r in ["YES","YES_AND","YES_BUT","NO","NO_AND","NO_BUT"]) + assert r in ["YES", "YES_AND", "YES_BUT", "NO", "NO_AND", "NO_BUT"] + def test_builtins(): # Check that builtins are valid calls From bd1cec45b6a67694cd44d5578ede74f5a718fa59 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 14:09:22 +0000 Subject: [PATCH 011/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index b26faeec9..024421149 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -19,6 +19,7 @@ #include "operations/condition_checking.h" #include #include "external/pcg_basic.h" +#include "external/tinydir.h" #define UNUSED(x) (void)(x) #define MAX(x, y) (((x) > (y)) ? (x) : (y)) From 8344e5f165dc375a1d7cf7cd1e4d437685522dba Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 14:16:03 +0000 Subject: [PATCH 012/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 024421149..a53f4809e 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1310,7 +1310,10 @@ void load_builtins(char* root){ { tinydir_file file; tinydir_readfile(&dir, &file); - + if(dir == NULL){ + gnoll_errno = 3; // TODO + return; + } printf("%s", file.name); if (file.is_dir) { From 59e72a81d98596f9331cdb950edd758c3adbd8d9 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 14:20:56 +0000 Subject: [PATCH 013/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a53f4809e..f79a14e07 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1303,17 +1303,13 @@ int roll_full_options( } void load_builtins(char* root){ - tinydir_dir dir; + tinydir_dir dir = NULL; tinydir_open(&dir, root); while (dir.has_next) { tinydir_file file; tinydir_readfile(&dir, &file); - if(dir == NULL){ - gnoll_errno = 3; // TODO - return; - } printf("%s", file.name); if (file.is_dir) { From 7efdcc2056187d9ac0595ade11c9afad1b73b126 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 14:22:03 +0000 Subject: [PATCH 014/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index f79a14e07..704eac2bc 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1303,7 +1303,7 @@ int roll_full_options( } void load_builtins(char* root){ - tinydir_dir dir = NULL; + tinydir_dir dir = {}; tinydir_open(&dir, root); while (dir.has_next) From e817ed83f9391aa30b1e71c9e2758ba9fbdba317 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 14:24:59 +0000 Subject: [PATCH 015/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 704eac2bc..7958c9df8 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1303,7 +1303,7 @@ int roll_full_options( } void load_builtins(char* root){ - tinydir_dir dir = {}; + tinydir_dir dir; tinydir_open(&dir, root); while (dir.has_next) From a44b37b36caca4cdf77fcffcf586848888f2d16c Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 22:52:28 +0000 Subject: [PATCH 016/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 7958c9df8..6ed1b8b07 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -8,6 +8,7 @@ #include #include #include +#include #include "yacc_header.h" #include "util/vector_functions.h" #include "shared_header.h" @@ -1305,7 +1306,8 @@ int roll_full_options( void load_builtins(char* root){ tinydir_dir dir; tinydir_open(&dir, root); - + + assert(dir); while (dir.has_next) { tinydir_file file; From 3364ac976cd07ca0c9e14f8e32941cef8297839a Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 22:52:39 +0000 Subject: [PATCH 017/138] Format code with gofumpt, yapf, black, gofmt, isort and autopep8 --- src/python/code/gnoll/parser.py | 19 ++++++++----------- tests/python/test_macros.py | 6 ++---- tests/python/util.py | 15 +++++++-------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 2e3afaa2b..46bfcd9e5 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -45,12 +45,7 @@ def raise_gnoll_error(value): raise err -def roll(s, - verbose=False, - mock=None, - mock_const=3, - breakdown=False, - builtins=False): +def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=False): """ Parse some dice notation with GNOLL. @param s the string to parse @@ -60,9 +55,9 @@ def roll(s, @param breakdown get the details of each dice rolled, not just the final result @return return code, final result, dice breakdown (None if disabled) """ - temp = tempfile.NamedTemporaryFile(prefix="gnoll_roll_", - suffix=".die", - delete=False) + temp = tempfile.NamedTemporaryFile( + prefix="gnoll_roll_", suffix=".die", delete=False + ) def make_native_type(v): """ @@ -133,9 +128,11 @@ def extract_from_dice_file(lines, seperator): arg = "".join(sys.argv[1:]) arg = arg if arg != "" else "1d20" code, r, detailed_r = roll(arg, verbose=False) - print(f""" + print( + f""" [[GNOLL Results]] Dice Roll: {arg} Result: {r} Exit Code: {code}, -Dice Breakdown: {detailed_r}""") +Dice Breakdown: {detailed_r}""" + ) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index bee30877f..4e67006c0 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -19,15 +19,13 @@ def test_macro_storage(r, out, mock): assert result == out -@pytest.mark.parametrize("r,out,mock", - [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) +@pytest.mark.parametrize("r,out,mock", [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) def test_macro_usage(r, out, mock): result, _ = roll(r, mock_mode=mock) assert result == out -@pytest.mark.skip("Currently no support for rerolling operations like Addition" - ) +@pytest.mark.skip("Currently no support for rerolling operations like Addition") def test_d66(): r = "#DSIXTYSIX=(d6*10)+d6;@DSIXTYSIX" result, _ = roll(r, mock_mode=Mock.RETURN_CONSTANT, mock_const=3) diff --git a/tests/python/util.py b/tests/python/util.py index ed815691c..d530892be 100644 --- a/tests/python/util.py +++ b/tests/python/util.py @@ -6,9 +6,11 @@ import numpy as np GRAMMAR_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/grammar")) + os.path.join(os.path.dirname(__file__), "../../src/grammar") +) SRC_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/")) + os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/") +) MK_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) first_run = True @@ -52,12 +54,9 @@ def make_all(): raise ValueError -def roll(s, - mock_mode=Mock.NO_MOCK, - mock_const=3, - verbose=False, - squeeze=True, - builtins=False): +def roll( + s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, builtins=False +): global first_run if first_run: From 59c0808eb597209c7d1c960365d713b1277fc311 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 22:54:18 +0000 Subject: [PATCH 018/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 6ed1b8b07..2b824425f 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1307,7 +1307,7 @@ void load_builtins(char* root){ tinydir_dir dir; tinydir_open(&dir, root); - assert(dir); + assert(&dir); while (dir.has_next) { tinydir_file file; From bec67f532064db82b46249c12a3d7c5aad5ac069 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 22:56:14 +0000 Subject: [PATCH 019/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 2b824425f..be1c576ae 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1307,7 +1307,7 @@ void load_builtins(char* root){ tinydir_dir dir; tinydir_open(&dir, root); - assert(&dir); + assert(dir.has_next); while (dir.has_next) { tinydir_file file; From 698f8404af9b32f65d498c093c0f1008e8cd5d40 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 23:01:04 +0000 Subject: [PATCH 020/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index be1c576ae..8c750a531 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1304,10 +1304,9 @@ int roll_full_options( } void load_builtins(char* root){ - tinydir_dir dir; + tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); - assert(dir.has_next); while (dir.has_next) { tinydir_file file; From 32a930357483186a6bac3f2649db6e0d1664b487 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 23:02:33 +0000 Subject: [PATCH 021/138] Update main.cpp Signed-off-by: Ian Hunter --- src/C++/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/C++/main.cpp b/src/C++/main.cpp index 8bf53ac84..3160ed3c6 100644 --- a/src/C++/main.cpp +++ b/src/C++/main.cpp @@ -10,6 +10,7 @@ int roll_full_options( int enable_verbosity, int enable_introspection, int enable_mocking, + int enable_builtins, int mocking_type, int mocking_seed ); @@ -27,6 +28,7 @@ int main() 0, 0, 0, + 0, 0 ); assert(err_code == 0); From c9c651001f50300b2e7dc5bebb19f6738a4c0ce4 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 23:03:56 +0000 Subject: [PATCH 022/138] Update test_macros.py Signed-off-by: Ian Hunter --- tests/python/test_macros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index 4e67006c0..c5218517c 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -53,7 +53,7 @@ def test_undefined_macro(): error_handled_by_gnoll(e) -def test_undefined_macro(): +def test_predefined_macro(): r = roll("@ORACLE", builtins=True) assert r in ["YES", "YES_AND", "YES_BUT", "NO", "NO_AND", "NO_BUT"] From e1b2366317f55ee594e237b7c8629acb426ed83f Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 23:10:03 +0000 Subject: [PATCH 023/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 8c750a531..9dd7250f3 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1315,7 +1315,16 @@ void load_builtins(char* root){ if (file.is_dir) { printf("/"); - } + }else{ + FILE* fp =fopen(file.name, "r"); + char stored_s[1000]; + while (fgets(stored_str,1000, fp)!=NULL); + fclose(fp); + YY_BUFFER_STATE buffer = yy_scan_string(stored_str); + yyparse(); + yy_delete_buffer(buffer); + + } printf("\n"); tinydir_next(&dir); From cf5c324369feb7f071775fe9173c322fd88f41f2 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 13 Nov 2022 23:35:33 +0000 Subject: [PATCH 024/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 9dd7250f3..93ca8ae29 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1317,7 +1317,7 @@ void load_builtins(char* root){ printf("/"); }else{ FILE* fp =fopen(file.name, "r"); - char stored_s[1000]; + char stored_str[1000]; while (fgets(stored_str,1000, fp)!=NULL); fclose(fp); YY_BUFFER_STATE buffer = yy_scan_string(stored_str); From 9e5ed36dc37efce440ec713ce5088b56f2b18170 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 08:27:22 +0000 Subject: [PATCH 025/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 93ca8ae29..177426208 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -23,9 +23,10 @@ #include "external/tinydir.h" #define UNUSED(x) (void)(x) -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) -#define ABS(x) (((x) < 0) ? (-x) : (x)) +// Avoid conflicts with MacOs predefined macros +#define MAXV(x, y) (((x) > (y)) ? (x) : (y)) +#define MINV(x, y) (((x) < (y)) ? (x) : (y)) +#define ABSV(x) (((x) < 0) ? (-x) : (x)) int yylex(void); int yyerror(const char* s); @@ -1199,7 +1200,7 @@ function: FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); - int vmax = MAX( + int vmax = MAXV( $3.content[0], $5.content[0] ); @@ -1212,7 +1213,7 @@ function: FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); - new_vec.content[0] = MIN( + new_vec.content[0] = MINV( $3.content[0], $5.content[0] ); @@ -1224,7 +1225,7 @@ function: FN_ABS LBRACE function RBRACE{ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); - new_vec.content[0] = ABS( + new_vec.content[0] = ABSV( $3.content[0] ); $$ = new_vec; From a5ef66579cb305298904ad2e262ff2cf6258373e Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 08:30:20 +0000 Subject: [PATCH 026/138] Delete lint_SuperLinter.yml Signed-off-by: Ian Hunter --- .github/workflows/lint_SuperLinter.yml | 29 -------------------------- 1 file changed, 29 deletions(-) delete mode 100644 .github/workflows/lint_SuperLinter.yml diff --git a/.github/workflows/lint_SuperLinter.yml b/.github/workflows/lint_SuperLinter.yml deleted file mode 100644 index d01a9efe9..000000000 --- a/.github/workflows/lint_SuperLinter.yml +++ /dev/null @@ -1,29 +0,0 @@ -# This workflow executes several linters on changed files based on languages used in your code base whenever -# you push a code or open a pull request. -# -# You can adjust the behavior by modifying this file. -# For more information, see: -# https://github.com/github/super-linter -name: "Quality: SuperLinter" - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] -jobs: - run-lint: - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v3 - with: - # Full git history is needed to get a proper list of changed files within `super-linter` - fetch-depth: 0 - - - name: Lint Code Base - uses: github/super-linter@v4 - env: - VALIDATE_ALL_CODEBASE: false - DEFAULT_BRANCH: main - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From 17681a2700c736d4f17bceffe8d232dc29794945 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 21:13:49 +0000 Subject: [PATCH 027/138] works locally --- reqs/requirements.txt | 1 + src/grammar/dice.yacc | 60 ++++++++++++++++++++++----------- src/python/code/gnoll/parser.py | 2 +- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/reqs/requirements.txt b/reqs/requirements.txt index ce237862d..2864cfc13 100644 --- a/reqs/requirements.txt +++ b/reqs/requirements.txt @@ -4,6 +4,7 @@ cppyy # tests pytest numpy>=1.22.2 +wurlitzer # distribution build diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 177426208..a7e8200be 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1310,25 +1310,35 @@ void load_builtins(char* root){ while (dir.has_next) { - tinydir_file file; - tinydir_readfile(&dir, &file); - printf("%s", file.name); - if (file.is_dir) - { - printf("/"); - }else{ - FILE* fp =fopen(file.name, "r"); - char stored_str[1000]; - while (fgets(stored_str,1000, fp)!=NULL); - fclose(fp); - YY_BUFFER_STATE buffer = yy_scan_string(stored_str); - yyparse(); - yy_delete_buffer(buffer); - + tinydir_file file; + tinydir_readfile(&dir, &file); + if(verbose){ + printf("%s", file.name); + } + if (file.is_dir) + { + if(verbose){ + printf("/\n"); + } + }else{ + if(verbose){ + printf("\n"); + } + char* path = calloc(sizeof(char), 1000); + strcat(path, "builtins/"); + strcat(path, file.name); + // TODO: Check filename for length + FILE* fp = fopen(path, "r"); + char stored_str[1000]; + while (fgets(stored_str, 1000, fp)!=NULL); + /* printf("Contents: %s\n",stored_str); */ + fclose(fp); + YY_BUFFER_STATE buffer = yy_scan_string(stored_str); + yyparse(); + yy_delete_buffer(buffer); } - printf("\n"); - tinydir_next(&dir); + tinydir_next(&dir); } tinydir_close(&dir); @@ -1386,12 +1396,22 @@ int main(int argc, char **str){ char * s = concat_strings(str, argc - 1); remove("output.dice"); - verbose = 1; - #ifdef BREAKDOWN + roll_full_options( + s, + "output.dice", + 0, // Verbose + 1, // Introspect + 0, // Mocking + 1, // Builtins + 0, + 0 + ); + + /* #ifdef BREAKDOWN return roll_with_breakdown(s, "output.dice"); #else return roll(s); - #endif + #endif */ } int yyerror(s) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 46bfcd9e5..d355e5e0f 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -101,9 +101,9 @@ def extract_from_dice_file(lines, seperator): False, # enable_verbose breakdown, # enable_introspect mock is not None, # enable_mock + builtins, # enable_builtins mock, mock_const, - builtins, ) if verbose: From 5f6b30ec4dc4f0e9ada560e16a9097091976fc88 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 21:37:11 +0000 Subject: [PATCH 028/138] Format code with gofumpt, yapf, black, gofmt, isort and autopep8 --- src/python/code/gnoll/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index d355e5e0f..73357f00c 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -101,7 +101,7 @@ def extract_from_dice_file(lines, seperator): False, # enable_verbose breakdown, # enable_introspect mock is not None, # enable_mock - builtins, # enable_builtins + builtins, # enable_builtins mock, mock_const, ) From 585f8868dd9a8e1c95d6db8921d5638b685bb483 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 21:44:39 +0000 Subject: [PATCH 029/138] Update test_macros.py Signed-off-by: Ian Hunter --- tests/python/test_macros.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index c5218517c..af3d31e6d 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -54,7 +54,7 @@ def test_undefined_macro(): def test_predefined_macro(): - r = roll("@ORACLE", builtins=True) + r = roll("@ORACLE", builtins=True)[0] assert r in ["YES", "YES_AND", "YES_BUT", "NO", "NO_AND", "NO_BUT"] From 7d4142bc89f0947ad3897932e8e0e5cbb609f951 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 21:52:44 +0000 Subject: [PATCH 030/138] Update main.cpp Signed-off-by: Ian Hunter --- src/C++/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/C++/main.cpp b/src/C++/main.cpp index 3160ed3c6..9fba3773b 100644 --- a/src/C++/main.cpp +++ b/src/C++/main.cpp @@ -4,6 +4,7 @@ #include #include "shared_header.h" +/* int roll_full_options( char* roll_request, char* log_file, @@ -14,6 +15,7 @@ int roll_full_options( int mocking_type, int mocking_seed ); +*/ int main() { From fb49c8b9a287dcdfa53c390bd36236dd6c6f62f3 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:05:11 +0000 Subject: [PATCH 031/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a7e8200be..994f68424 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1336,6 +1336,7 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); + free(stored_str); } tinydir_next(&dir); From 992d437853d6a3f1ce32d2c647593d073ae485bb Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:08:25 +0000 Subject: [PATCH 032/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 994f68424..a7e8200be 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1336,7 +1336,6 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); - free(stored_str); } tinydir_next(&dir); From 21feef05fb6188cfad67c6fecc9c17cc04ec781c Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:25:58 +0000 Subject: [PATCH 033/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a7e8200be..8a125c08b 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1336,6 +1336,7 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); + free(path); } tinydir_next(&dir); From 7b7a8e29b17f9649499f26cfa1da9b70bed96b96 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:27:32 +0000 Subject: [PATCH 034/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 8a125c08b..590dde4e4 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1400,7 +1400,7 @@ int main(int argc, char **str){ roll_full_options( s, "output.dice", - 0, // Verbose + 1, // Verbose 1, // Introspect 0, // Mocking 1, // Builtins From 89a4c2711136f66ca35d0c397657aa608b78ddea Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:37:37 +0000 Subject: [PATCH 035/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 590dde4e4..3e1f79163 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1305,12 +1305,14 @@ int roll_full_options( } void load_builtins(char* root){ + printf("load builtins\n"); tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); while (dir.has_next) { tinydir_file file; + printf("read nexts\n"); tinydir_readfile(&dir, &file); if(verbose){ printf("%s", file.name); @@ -1396,7 +1398,7 @@ char * concat_strings(char ** s, int num_s){ int main(int argc, char **str){ char * s = concat_strings(str, argc - 1); remove("output.dice"); - + printf("main\n"); roll_full_options( s, "output.dice", From 414a0d7f5e12a6dbe5b2f2b56e938318d3e23c16 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:39:57 +0000 Subject: [PATCH 036/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 3e1f79163..62afdba0a 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1,4 +1,4 @@ -/* Uncomment for better errors! (non-POSIX compliant) */ +Pip p PPP/* Uncomment for better errors! (non-POSIX compliant) */ /* %define parse.error verbose */ %{ @@ -1396,9 +1396,10 @@ char * concat_strings(char ** s, int num_s){ } int main(int argc, char **str){ + printf("main\n"); char * s = concat_strings(str, argc - 1); remove("output.dice"); - printf("main\n"); + printf("main 2\n"); roll_full_options( s, "output.dice", @@ -1409,12 +1410,6 @@ int main(int argc, char **str){ 0, 0 ); - - /* #ifdef BREAKDOWN - return roll_with_breakdown(s, "output.dice"); - #else - return roll(s); - #endif */ } int yyerror(s) From 3e5cb4d962a9b5bcaefbf690bec1c257d653e801 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:40:50 +0000 Subject: [PATCH 037/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 62afdba0a..dd9cdf3f2 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1,4 +1,4 @@ -Pip p PPP/* Uncomment for better errors! (non-POSIX compliant) */ +/* Uncomment for better errors! (non-POSIX compliant) */ /* %define parse.error verbose */ %{ From 10fa5ecf51716a2b3d33840df4142ab3194daf8a Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:42:11 +0000 Subject: [PATCH 038/138] Update test_binding_cpp.yml Signed-off-by: Ian Hunter --- .github/workflows/test_binding_cpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_binding_cpp.yml b/.github/workflows/test_binding_cpp.yml index 203062445..3d8c67088 100644 --- a/.github/workflows/test_binding_cpp.yml +++ b/.github/workflows/test_binding_cpp.yml @@ -16,7 +16,7 @@ jobs: - uses: actions/checkout@v3 - name: make run: | - make CC=clang all + make CC=clang DEBUG=1 all ./build/dice d20 build_cpp: From 9a72e9aba1aabbc41cb0975150f4b55efb15d5f0 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:44:39 +0000 Subject: [PATCH 039/138] Update test_binding_cpp.yml Signed-off-by: Ian Hunter --- .github/workflows/test_binding_cpp.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test_binding_cpp.yml b/.github/workflows/test_binding_cpp.yml index 3d8c67088..3a29f7f82 100644 --- a/.github/workflows/test_binding_cpp.yml +++ b/.github/workflows/test_binding_cpp.yml @@ -16,7 +16,7 @@ jobs: - uses: actions/checkout@v3 - name: make run: | - make CC=clang DEBUG=1 all + make CC=clang DEBUG=0 all ./build/dice d20 build_cpp: From ad99cb2fabed69963dbc9b9b558f02861b49d9cb Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:48:06 +0000 Subject: [PATCH 040/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0d8ec2393..10faa9f4f 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O0 -g # Valgrind info +OPT=-O3 -g # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= From 6e5464602b4505932b179021af4b31b272f67f0a Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:49:09 +0000 Subject: [PATCH 041/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 10faa9f4f..2d6abe25b 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O3 -g # Valgrind info +OPT=-O2 -g # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= From be3ba59d2f204bbd4636d2fceb306f415fd1ea43 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:50:01 +0000 Subject: [PATCH 042/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2d6abe25b..9efc40918 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O2 -g # Valgrind info +OPT=-O1 -g # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= From ba9a337486d9d4d8a2da2564c0fa9ceb45932175 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:52:34 +0000 Subject: [PATCH 043/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9efc40918..0d8ec2393 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O1 -g # Valgrind info +OPT=-O0 -g # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= From 3adca55764678816d393881b317523042d93622a Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:53:25 +0000 Subject: [PATCH 044/138] Update main.cpp Signed-off-by: Ian Hunter --- src/C++/main.cpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/C++/main.cpp b/src/C++/main.cpp index 9fba3773b..23effbe56 100644 --- a/src/C++/main.cpp +++ b/src/C++/main.cpp @@ -4,19 +4,6 @@ #include #include "shared_header.h" -/* -int roll_full_options( - char* roll_request, - char* log_file, - int enable_verbosity, - int enable_introspection, - int enable_mocking, - int enable_builtins, - int mocking_type, - int mocking_seed -); -*/ - int main() { const char* fn = "out.dice"; From 90f15be329e41fe399a4c9e162b6039eb91c388d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:57:21 +0000 Subject: [PATCH 045/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index dd9cdf3f2..c0f01e957 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1326,6 +1326,7 @@ void load_builtins(char* root){ if(verbose){ printf("\n"); } + /* char* path = calloc(sizeof(char), 1000); strcat(path, "builtins/"); strcat(path, file.name); @@ -1333,12 +1334,13 @@ void load_builtins(char* root){ FILE* fp = fopen(path, "r"); char stored_str[1000]; while (fgets(stored_str, 1000, fp)!=NULL); - /* printf("Contents: %s\n",stored_str); */ + // printf("Contents: %s\n",stored_str); fclose(fp); YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); free(path); + */ } tinydir_next(&dir); From 1e84de95cafb707dc3eee5d20325d60ac4f33fc5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 22:58:30 +0000 Subject: [PATCH 046/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index c0f01e957..c4498913f 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1326,10 +1326,11 @@ void load_builtins(char* root){ if(verbose){ printf("\n"); } - /* + char* path = calloc(sizeof(char), 1000); strcat(path, "builtins/"); strcat(path, file.name); + /* // TODO: Check filename for length FILE* fp = fopen(path, "r"); char stored_str[1000]; @@ -1339,8 +1340,8 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); - free(path); */ + free(path); } tinydir_next(&dir); From 8665750566ef5577d531c6d2e4f974662fb4b741 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:00:19 +0000 Subject: [PATCH 047/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index c4498913f..ca8d3d11b 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1330,13 +1330,14 @@ void load_builtins(char* root){ char* path = calloc(sizeof(char), 1000); strcat(path, "builtins/"); strcat(path, file.name); - /* + // TODO: Check filename for length FILE* fp = fopen(path, "r"); char stored_str[1000]; while (fgets(stored_str, 1000, fp)!=NULL); // printf("Contents: %s\n",stored_str); fclose(fp); + /* YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); From 7c6cd22288df54c59f21fa36a4d9ea6816f6c770 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:01:16 +0000 Subject: [PATCH 048/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index ca8d3d11b..aa0f93861 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1337,11 +1337,11 @@ void load_builtins(char* root){ while (fgets(stored_str, 1000, fp)!=NULL); // printf("Contents: %s\n",stored_str); fclose(fp); - /* + YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); - */ + free(path); } From 72df24881ae09ca48918f57cc6930277947ab8c1 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:03:28 +0000 Subject: [PATCH 049/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index aa0f93861..afb514d47 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1339,7 +1339,7 @@ void load_builtins(char* root){ fclose(fp); YY_BUFFER_STATE buffer = yy_scan_string(stored_str); - yyparse(); + //yyparse(); yy_delete_buffer(buffer); free(path); From f4758790cd0aa374e03286e765e2fa7693150688 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:05:04 +0000 Subject: [PATCH 050/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index afb514d47..c9122c474 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -142,7 +142,7 @@ macro_statement: vec key = $2; vec value = $4; - register_macro(&key, &value.source); + //register_macro(&key, &value.source); if(gnoll_errno){ YYABORT; @@ -1339,7 +1339,7 @@ void load_builtins(char* root){ fclose(fp); YY_BUFFER_STATE buffer = yy_scan_string(stored_str); - //yyparse(); + yyparse(); yy_delete_buffer(buffer); free(path); From cd24f91c2c7963e559b521eda43e4a377a32c0a2 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:06:12 +0000 Subject: [PATCH 051/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index c9122c474..aa0f93861 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -142,7 +142,7 @@ macro_statement: vec key = $2; vec value = $4; - //register_macro(&key, &value.source); + register_macro(&key, &value.source); if(gnoll_errno){ YYABORT; From f72e5c9615411badf275f84d74e912b33824b631 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:08:52 +0000 Subject: [PATCH 052/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 96a78a807..661c15a96 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -74,6 +74,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); } + free(s); //new } void search_macros(char *skey, roll_params *to_store) { From 5fa3bc8a6ed78bb7ffee49ebfd65b85b405b491d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:10:22 +0000 Subject: [PATCH 053/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 661c15a96..2d50a412a 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -52,7 +52,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { struct macro_struct *s; unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - + /* HASH_FIND_INT(macros, &k, s); /* id already in the hash? */ if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -74,7 +74,9 @@ void register_macro(vec *macro_name, roll_params *to_store) { &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); } + */ free(s); //new + free(skey); //new } void search_macros(char *skey, roll_params *to_store) { From b755239a1802df979be6ba6f53dd91b4c90db09c Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:11:22 +0000 Subject: [PATCH 054/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 2d50a412a..f256c648b 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -53,7 +53,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; /* - HASH_FIND_INT(macros, &k, s); /* id already in the hash? */ + HASH_FIND_INT(macros, &k, s); // id already in the hash? if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -61,7 +61,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { return; } s->id = (int)key; - HASH_ADD_INT(macros, id, s); /* id: name of key field */ + HASH_ADD_INT(macros, id, s); // id: name of key field } memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); From 97ce3c601af939e2f528d0f41b42b799943ec6bc Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:13:11 +0000 Subject: [PATCH 055/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index f256c648b..172d2e6cf 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -52,8 +52,8 @@ void register_macro(vec *macro_name, roll_params *to_store) { struct macro_struct *s; unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - /* HASH_FIND_INT(macros, &k, s); // id already in the hash? + /* if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); From ed9478edc6adba38f195e6a409337ed5ede734a4 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:14:25 +0000 Subject: [PATCH 056/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 172d2e6cf..7703e74ed 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -53,7 +53,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; HASH_FIND_INT(macros, &k, s); // id already in the hash? - /* + if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -63,7 +63,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { s->id = (int)key; HASH_ADD_INT(macros, id, s); // id: name of key field } - + /* memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); s->stored_dice_roll.symbol_pool = NULL; From dbaa36353a60a515e7d8bab8d5129c3cee31d5c0 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 14 Nov 2022 23:15:45 +0000 Subject: [PATCH 057/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 7703e74ed..1a30504ac 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -54,6 +54,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; HASH_FIND_INT(macros, &k, s); // id already in the hash? + /* if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -63,7 +64,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { s->id = (int)key; HASH_ADD_INT(macros, id, s); // id: name of key field } - /* + memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); s->stored_dice_roll.symbol_pool = NULL; From bcd2f849ce501e96559f0ca966de3a17af4958ef Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:14:00 +0000 Subject: [PATCH 058/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 1a30504ac..7b4e65907 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -54,7 +54,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; HASH_FIND_INT(macros, &k, s); // id already in the hash? - /* + if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -62,9 +62,10 @@ void register_macro(vec *macro_name, roll_params *to_store) { return; } s->id = (int)key; - HASH_ADD_INT(macros, id, s); // id: name of key field + //HASH_ADD_INT(macros, id, s); // id: name of key field } + /* memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); s->stored_dice_roll.symbol_pool = NULL; From 32608db18b6ee344ae144ff516e288205cafcd36 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:26:23 +0000 Subject: [PATCH 059/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 7b4e65907..37694394d 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -62,7 +62,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { return; } s->id = (int)key; - //HASH_ADD_INT(macros, id, s); // id: name of key field + HASH_ADD_INT(macros, id, s); // id: name of key field } /* @@ -77,8 +77,8 @@ void register_macro(vec *macro_name, roll_params *to_store) { to_store->die_sides, MAX_SYMBOL_LENGTH); } */ - free(s); //new - free(skey); //new + //free(s); //new + //free(skey); //new } void search_macros(char *skey, roll_params *to_store) { From 49b825246c7ee26508f592d28f8a93adc34d37b5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:27:28 +0000 Subject: [PATCH 060/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 37694394d..1c0ff9452 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -65,10 +65,10 @@ void register_macro(vec *macro_name, roll_params *to_store) { HASH_ADD_INT(macros, id, s); // id: name of key field } - /* + memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); s->stored_dice_roll.symbol_pool = NULL; - + /* if (is_symbolic) { free_2d_array(&s->stored_dice_roll.symbol_pool, s->stored_dice_roll.die_sides); From ea0957b2361b2b3ae9703e330c783aef16e82718 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:28:20 +0000 Subject: [PATCH 061/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 1c0ff9452..8b42e86b7 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -68,7 +68,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); s->stored_dice_roll.symbol_pool = NULL; - /* + if (is_symbolic) { free_2d_array(&s->stored_dice_roll.symbol_pool, s->stored_dice_roll.die_sides); @@ -76,7 +76,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); } - */ + //free(s); //new //free(skey); //new } From be4e1388ce2431cece5c800517e77e6f0bf2ba90 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:29:07 +0000 Subject: [PATCH 062/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 8b42e86b7..0bb2235e2 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -70,8 +70,8 @@ void register_macro(vec *macro_name, roll_params *to_store) { s->stored_dice_roll.symbol_pool = NULL; if (is_symbolic) { - free_2d_array(&s->stored_dice_roll.symbol_pool, - s->stored_dice_roll.die_sides); + //free_2d_array(&s->stored_dice_roll.symbol_pool, + // s->stored_dice_roll.die_sides); safe_copy_2d_chararray_with_allocation( &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); From 89cc6c949b513c9a93ce59a785c4b3bab06ebb94 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:31:02 +0000 Subject: [PATCH 063/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 0bb2235e2..37407a2f9 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -70,11 +70,11 @@ void register_macro(vec *macro_name, roll_params *to_store) { s->stored_dice_roll.symbol_pool = NULL; if (is_symbolic) { - //free_2d_array(&s->stored_dice_roll.symbol_pool, - // s->stored_dice_roll.die_sides); - safe_copy_2d_chararray_with_allocation( - &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, - to_store->die_sides, MAX_SYMBOL_LENGTH); + free_2d_array(&s->stored_dice_roll.symbol_pool, + s->stored_dice_roll.die_sides); + // safe_copy_2d_chararray_with_allocation( + // &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, + // to_store->die_sides, MAX_SYMBOL_LENGTH); } //free(s); //new From 98976767c72c610d59e47ed4c19d06599c8a8b31 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:36:37 +0000 Subject: [PATCH 064/138] Update macro_logic.c Signed-off-by: Ian Hunter --- src/grammar/operations/macro_logic.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index 37407a2f9..d1c412be1 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -72,9 +72,9 @@ void register_macro(vec *macro_name, roll_params *to_store) { if (is_symbolic) { free_2d_array(&s->stored_dice_roll.symbol_pool, s->stored_dice_roll.die_sides); - // safe_copy_2d_chararray_with_allocation( - // &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, - // to_store->die_sides, MAX_SYMBOL_LENGTH); + safe_copy_2d_chararray_with_allocation( + &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, + to_store->die_sides, MAX_SYMBOL_LENGTH); } //free(s); //new From d4ea17c6e1b7aa8cc68c10ae40dd430fe7e681e2 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:38:35 +0000 Subject: [PATCH 065/138] Update safe_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/safe_functions.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index bf1ed9dcc..8a5812386 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -57,6 +57,8 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, * @param item * @param max_size */ + printf("alloc: %d\n", items); + printf("*alloc: %d\n", max_size); *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; From ee516787693ccca1894232036745f51a9902d4cd Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:40:37 +0000 Subject: [PATCH 066/138] Update safe_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/safe_functions.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 8a5812386..fd0713772 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -59,6 +59,7 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, */ printf("alloc: %d\n", items); printf("*alloc: %d\n", max_size); + /* *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; @@ -71,6 +72,7 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, } memcpy((*dst)[i], src[i], max_size); } + */ } void *safe_calloc(size_t nitems, size_t size) { From 21259b48d70b307204177e245168e18e2265bbb5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:42:00 +0000 Subject: [PATCH 067/138] Update safe_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/safe_functions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index fd0713772..6ca364ae6 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -57,8 +57,8 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, * @param item * @param max_size */ - printf("alloc: %d\n", items); - printf("*alloc: %d\n", max_size); + printf("alloc: %u\n", items); + printf("*alloc: %u\n", max_size); /* *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { From 4e11f72f536aac2cca7292264984799be4a8bc1f Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:46:42 +0000 Subject: [PATCH 068/138] Update safe_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/safe_functions.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 6ca364ae6..8029c066a 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -59,6 +59,8 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, */ printf("alloc: %u\n", items); printf("*alloc: %u\n", max_size); + (void)dst; + (void)src; /* *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { From b304c39c354f3015bae40880e0c0f66dc7cc2b40 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:51:38 +0000 Subject: [PATCH 069/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index aa0f93861..2ab21bb0c 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1335,7 +1335,9 @@ void load_builtins(char* root){ FILE* fp = fopen(path, "r"); char stored_str[1000]; while (fgets(stored_str, 1000, fp)!=NULL); - // printf("Contents: %s\n",stored_str); + if(verbose){ + printf("Contents: %s\n",stored_str); + } fclose(fp); YY_BUFFER_STATE buffer = yy_scan_string(stored_str); From 97b40e56f5ab1eda845f05c6515f04f5ee45775e Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:53:55 +0000 Subject: [PATCH 070/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 2ab21bb0c..b16bec4c1 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1308,9 +1308,14 @@ void load_builtins(char* root){ printf("load builtins\n"); tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); + int count = 0; while (dir.has_next) { + if(count == 1){ + break; + } + count++; tinydir_file file; printf("read nexts\n"); tinydir_readfile(&dir, &file); From 5e9db44c9ceee587464410be2f50781833309794 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:54:21 +0000 Subject: [PATCH 071/138] Update safe_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/safe_functions.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 8029c066a..773f0ba57 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -59,9 +59,9 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, */ printf("alloc: %u\n", items); printf("*alloc: %u\n", max_size); - (void)dst; - (void)src; - /* + //(void)dst; + //(void)src; + *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; @@ -74,7 +74,7 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, } memcpy((*dst)[i], src[i], max_size); } - */ + } void *safe_calloc(size_t nitems, size_t size) { From b8e5b3f23e3ba652bc0ff74e486102a01c81915d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:55:22 +0000 Subject: [PATCH 072/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index b16bec4c1..24998b501 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 1){ + if(count == 10){ break; } count++; From 29bc77b662be5c686394c15f2c55d89ff34ff4fc Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:57:59 +0000 Subject: [PATCH 073/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 24998b501..ada7156a8 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 10){ + if(count == 2){ break; } count++; From 1609a70752bf1214d1d840febaa7a4447c02fd14 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 08:59:35 +0000 Subject: [PATCH 074/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index ada7156a8..9f49edcca 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 2){ + if(count == 4){ break; } count++; From 066a88b3bb2e00cf661debd943eed1026fbd4e2b Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:00:44 +0000 Subject: [PATCH 075/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 9f49edcca..ee3ad31c0 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 4){ + if(count == 7){ break; } count++; From 09d4a8b639881fa99eeeb48b889f15371b065acc Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:02:49 +0000 Subject: [PATCH 076/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index ee3ad31c0..54bebf0eb 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 7){ + if(count == 9){ break; } count++; From 69414cf9275c72af9e48b53c29c0d4159b7f624b Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:06:35 +0000 Subject: [PATCH 077/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 54bebf0eb..a1c938084 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 9){ + if(count == 8){ break; } count++; From 815275fedf99961cad80b0934f8c8fecc5935122 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:10:19 +0000 Subject: [PATCH 078/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a1c938084..ee3ad31c0 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1312,7 +1312,7 @@ void load_builtins(char* root){ while (dir.has_next) { - if(count == 8){ + if(count == 7){ break; } count++; From 9189824de97d204d14f85a8cf3cbec3c010cf588 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:56:56 +0000 Subject: [PATCH 079/138] working clang locally --- Makefile | 4 ++-- src/grammar/dice.yacc | 26 ++++++++++++++------------ src/grammar/util/safe_functions.c | 10 ++++++---- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 0d8ec2393..10209d309 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O0 -g # Valgrind info +OPT=-O0 -g -fsanitize=address -gdwarf-4 # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= @@ -62,7 +62,7 @@ LEXER:=flex -f -Ca -Ce -Cr endif # add flags and the include paths -DEFS=-DUSE_SECURE_RANDOM=${USE_SECURE_RANDOM} -DJUST_YACC=${YACC_FALLBACK} -DBREAKDOWN +DEFS=-DUSE_SECURE_RANDOM=${USE_SECURE_RANDOM} -DJUST_YACC=${YACC_FALLBACK} CFLAGS=$(foreach D,$(INCDIRS),-I$(D)) $(OPT) $(DEFS) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index ee3ad31c0..ced7bbefd 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -39,7 +39,7 @@ char * concat_strings(char ** s, int num_s); int yydebug=1; #endif -int verbose = 1; +int verbose = 0; int dice_breakdown = 0; int seeded = 0; int write_to_file = 0; @@ -143,6 +143,9 @@ macro_statement: vec value = $4; register_macro(&key, &value.source); + + // We no longer need the setting key + free(key.symbols); if(gnoll_errno){ YYABORT; @@ -1305,19 +1308,12 @@ int roll_full_options( } void load_builtins(char* root){ - printf("load builtins\n"); tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); - int count = 0; while (dir.has_next) { - if(count == 7){ - break; - } - count++; - tinydir_file file; - printf("read nexts\n"); + tinydir_file file; tinydir_readfile(&dir, &file); if(verbose){ printf("%s", file.name); @@ -1407,20 +1403,26 @@ char * concat_strings(char ** s, int num_s){ } int main(int argc, char **str){ - printf("main\n"); char * s = concat_strings(str, argc - 1); remove("output.dice"); - printf("main 2\n"); roll_full_options( s, "output.dice", - 1, // Verbose + 0, // Verbose 1, // Introspect 0, // Mocking 1, // Builtins 0, 0 ); + FILE *f = fopen("output.dice","r"); + int c; + if (f){ + while((c = getc(f)) != EOF){ + putchar(c); + } + fclose(f); + } } int yyerror(s) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 773f0ba57..ed7230f81 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -57,11 +57,13 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, * @param item * @param max_size */ - printf("alloc: %u\n", items); - printf("*alloc: %u\n", max_size); - //(void)dst; - //(void)src; + // printf("alloc: %u\n", items); + // printf("*alloc: %u\n", max_size); + // printf("%p\n", (void *)*dst); + if(*dst != 0){ + free(*dst); + } *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; From 23cd66e9a44bb2c9ef3ab90e593ac23921377fec Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Tue, 15 Nov 2022 09:59:02 +0000 Subject: [PATCH 080/138] Update test_binding_cpp.yml Signed-off-by: Ian Hunter --- .github/workflows/test_binding_cpp.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test_binding_cpp.yml b/.github/workflows/test_binding_cpp.yml index 3a29f7f82..932fcb738 100644 --- a/.github/workflows/test_binding_cpp.yml +++ b/.github/workflows/test_binding_cpp.yml @@ -16,6 +16,7 @@ jobs: - uses: actions/checkout@v3 - name: make run: | + clang --version make CC=clang DEBUG=0 all ./build/dice d20 From d40bd9dea869fa4317e343b9551be4e690a8972f Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 19 Nov 2022 09:48:51 +0000 Subject: [PATCH 081/138] WIP changes.. memory leak exist --- src/grammar/dice.yacc | 58 ++++++++++++++++++---------- src/grammar/operations/macro_logic.c | 27 +++++++------ src/grammar/util/safe_functions.c | 14 +++++++ src/grammar/util/safe_functions.h | 3 ++ 4 files changed, 69 insertions(+), 33 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index ced7bbefd..91e509b92 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -142,10 +142,14 @@ macro_statement: vec key = $2; vec value = $4; + printf("Register\n"); register_macro(&key, &value.source); + printf("Registered\n"); - // We no longer need the setting key - free(key.symbols); + + // Cleanup + free_vector(key); + free_vector(value); if(gnoll_errno){ YYABORT; @@ -493,8 +497,6 @@ collapsing_dice_operations: if (vector.dtype == SYMBOLIC){ // Symbolic, Impossible to collapse $$ = vector; - - } else{ // Collapse if Necessary @@ -502,8 +504,8 @@ collapsing_dice_operations: vec new_vector; initialize_vector(&new_vector, NUMERIC, 1); new_vector.content[0] = sum(vector.content, vector.length); - $$ = new_vector; + free_vector(vector); }else{ $$ = vector; } @@ -735,8 +737,6 @@ dice_operations: } | die_roll - { - } ; die_roll: @@ -987,10 +987,11 @@ die_roll: NUMBER ; + custom_symbol_dice: NUMBER die_symbol SYMBOL_LBRACE csd SYMBOL_RBRACE { - + // Nd{SYMB} vec left = $1; vec right = $4; @@ -1009,12 +1010,12 @@ custom_symbol_dice: | die_symbol SYMBOL_LBRACE csd SYMBOL_RBRACE { + // d{SYM} vec csd = $3; vec result_vec; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; - if (csd.dtype == NUMERIC){ vec dice_sides; @@ -1039,6 +1040,7 @@ custom_symbol_dice: }else{ initialize_vector(&result_vec, SYMBOLIC, 1); + printf("YO\n"); roll_params rp = { .number_of_dice=(unsigned int)number_of_dice.content[0], @@ -1050,7 +1052,6 @@ custom_symbol_dice: for(unsigned int i = 0; i != csd.length; i++){ rp.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); memcpy(rp.symbol_pool[i], csd.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); - // rp.symbol_pool[i] = csd.symbols[i]; } result_vec.source = rp; @@ -1060,6 +1061,7 @@ custom_symbol_dice: &csd, &result_vec ); + printf("YO2\n"); } $$ = result_vec; } @@ -1201,6 +1203,7 @@ die_symbol: function: FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ + // max(a,b) vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); int vmax = MAXV( @@ -1209,11 +1212,12 @@ function: ); new_vec.content[0] = vmax; $$ = new_vec; - free($3.content); - free($5.content); + free_vector($3); + free_vector($5); } | FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ + // min(a,b) vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = MINV( @@ -1226,7 +1230,8 @@ function: } | FN_ABS LBRACE function RBRACE{ - vec new_vec; + // abs(__) + vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = ABSV( $3.content[0] @@ -1311,9 +1316,10 @@ void load_builtins(char* root){ tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); + int count = 0; while (dir.has_next) { - tinydir_file file; + tinydir_file file; tinydir_readfile(&dir, &file); if(verbose){ printf("%s", file.name); @@ -1324,18 +1330,25 @@ void load_builtins(char* root){ printf("/\n"); } }else{ + count++; if(verbose){ printf("\n"); } - char* path = calloc(sizeof(char), 1000); + int max_file_path_length = 1000; + int max_macro_length = 1000; + + char* path = calloc(sizeof(char), max_file_path_length); + char* stored_str = calloc(sizeof(char), max_macro_length); + + // Get full path strcat(path, "builtins/"); strcat(path, file.name); // TODO: Check filename for length FILE* fp = fopen(path, "r"); - char stored_str[1000]; - while (fgets(stored_str, 1000, fp)!=NULL); + while (fgets(stored_str, max_macro_length, fp)!=NULL); + if(verbose){ printf("Contents: %s\n",stored_str); } @@ -1344,10 +1357,13 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); - + free(path); + free(stored_str); + } + if(count >= 1){ + break; } - tinydir_next(&dir); } @@ -1408,10 +1424,10 @@ int main(int argc, char **str){ roll_full_options( s, "output.dice", - 0, // Verbose + 1, // Verbose 1, // Introspect 0, // Mocking - 1, // Builtins + 0, // Builtins 0, 0 ); diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index d1c412be1..fcec50181 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -38,20 +38,19 @@ void register_macro(vec *macro_name, roll_params *to_store) { * referenced to by the key */ - if (gnoll_errno) { - return; - } + if (gnoll_errno) {return;} + struct macro_struct *s; char *skey = macro_name->symbols[0]; + + printf("Macro:: Get ID\n"); unsigned long key = hash_function((unsigned char *)skey); int k = (int)key; - if (gnoll_errno) { - return; - } - - struct macro_struct *s; unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; + + + printf("Macro:: Check existancee\n"); HASH_FIND_INT(macros, &k, s); // id already in the hash? @@ -67,14 +66,18 @@ void register_macro(vec *macro_name, roll_params *to_store) { memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); - s->stored_dice_roll.symbol_pool = NULL; + // s->stored_dice_roll.symbol_pool = NULL; if (is_symbolic) { - free_2d_array(&s->stored_dice_roll.symbol_pool, - s->stored_dice_roll.die_sides); - safe_copy_2d_chararray_with_allocation( + // free symbols from roll in S + // free_2d_array(&s->stored_dice_roll.symbol_pool, + // s->stored_dice_roll.die_sides); + safe_copy_2d_chararray_with_allocation( &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); + + // free_roll_params(to_store); //new + // free_2d_array(&to_store->symbol_pool, to_store->die_sides); } //free(s); //new diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index ed7230f81..4486094f0 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -30,6 +30,20 @@ void *safe_malloc(size_t size) { return malloc_result; } +void free_vector(vec v){ + if(v.dtype == NUMERIC){ + printf("vec: %p\n", (void*)v.content); + free(v.content); + }else{ + printf("vec: %p\n", (void*)v.symbols); + free_2d_array(&v.symbols, v.length); + } +} +void free_roll_params(roll_params* rp){ + (void *)rp; + // free_2d_array(&rp->symbol_pool, rp->die_sides); +} + void free_2d_array(char ***arr, unsigned int items) { /** * @brief Free a 2d char array in a repeatable manner. diff --git a/src/grammar/util/safe_functions.h b/src/grammar/util/safe_functions.h index 4b1ddc328..2633ff163 100644 --- a/src/grammar/util/safe_functions.h +++ b/src/grammar/util/safe_functions.h @@ -33,6 +33,9 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, unsigned int max_size); void free_2d_array(char ***arr, unsigned int items); +void free_vector(vec v); +void free_roll_params(roll_params* rp); + int fast_atoi(const char *str); #endif From cf35df9d49734f9d54154f8b29fabd76ec8cdc64 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:37:02 +0000 Subject: [PATCH 082/138] Format code with black, gofmt, gofumpt, yapf, autopep8 and isort --- src/python/code/gnoll/parser.py | 19 +++++++++++-------- tests/python/test_macros.py | 6 ++++-- tests/python/util.py | 15 ++++++++------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 73357f00c..f0d5c11d8 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -45,7 +45,12 @@ def raise_gnoll_error(value): raise err -def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=False): +def roll(s, + verbose=False, + mock=None, + mock_const=3, + breakdown=False, + builtins=False): """ Parse some dice notation with GNOLL. @param s the string to parse @@ -55,9 +60,9 @@ def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=Fa @param breakdown get the details of each dice rolled, not just the final result @return return code, final result, dice breakdown (None if disabled) """ - temp = tempfile.NamedTemporaryFile( - prefix="gnoll_roll_", suffix=".die", delete=False - ) + temp = tempfile.NamedTemporaryFile(prefix="gnoll_roll_", + suffix=".die", + delete=False) def make_native_type(v): """ @@ -128,11 +133,9 @@ def extract_from_dice_file(lines, seperator): arg = "".join(sys.argv[1:]) arg = arg if arg != "" else "1d20" code, r, detailed_r = roll(arg, verbose=False) - print( - f""" + print(f""" [[GNOLL Results]] Dice Roll: {arg} Result: {r} Exit Code: {code}, -Dice Breakdown: {detailed_r}""" - ) +Dice Breakdown: {detailed_r}""") diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index af3d31e6d..efa6a395a 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -19,13 +19,15 @@ def test_macro_storage(r, out, mock): assert result == out -@pytest.mark.parametrize("r,out,mock", [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) +@pytest.mark.parametrize("r,out,mock", + [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) def test_macro_usage(r, out, mock): result, _ = roll(r, mock_mode=mock) assert result == out -@pytest.mark.skip("Currently no support for rerolling operations like Addition") +@pytest.mark.skip("Currently no support for rerolling operations like Addition" + ) def test_d66(): r = "#DSIXTYSIX=(d6*10)+d6;@DSIXTYSIX" result, _ = roll(r, mock_mode=Mock.RETURN_CONSTANT, mock_const=3) diff --git a/tests/python/util.py b/tests/python/util.py index d530892be..ed815691c 100644 --- a/tests/python/util.py +++ b/tests/python/util.py @@ -6,11 +6,9 @@ import numpy as np GRAMMAR_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/grammar") -) + os.path.join(os.path.dirname(__file__), "../../src/grammar")) SRC_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/") -) + os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/")) MK_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) first_run = True @@ -54,9 +52,12 @@ def make_all(): raise ValueError -def roll( - s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, builtins=False -): +def roll(s, + mock_mode=Mock.NO_MOCK, + mock_const=3, + verbose=False, + squeeze=True, + builtins=False): global first_run if first_run: From 125a2daeefe7145f2f1c6a967e698f6c0259b9ab Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 20 Nov 2022 03:22:42 +0000 Subject: [PATCH 083/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 91e509b92..7e4ddf88a 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1335,11 +1335,12 @@ void load_builtins(char* root){ printf("\n"); } - int max_file_path_length = 1000; + unsigned long max_file_path_length = 1000; int max_macro_length = 1000; - char* path = calloc(sizeof(char), max_file_path_length); - char* stored_str = calloc(sizeof(char), max_macro_length); + char* path = safe_calloc(sizeof(char), max_file_path_length); + char* stored_str = safe_calloc(sizeof(char), (unsigned long)max_macro_length); + if(gnoll_errno){return;} // Get full path strcat(path, "builtins/"); From b13da53c3b3f4b193b19890275384ecc1a529e45 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 20 Nov 2022 03:24:11 +0000 Subject: [PATCH 084/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 7e4ddf88a..f82b6790a 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1358,6 +1358,7 @@ void load_builtins(char* root){ YY_BUFFER_STATE buffer = yy_scan_string(stored_str); yyparse(); yy_delete_buffer(buffer); + if(gnoll_errno){return;} free(path); free(stored_str); From e56dd2d50b3d94afd31738c787149293ac900184 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Wed, 23 Nov 2022 09:40:31 +0000 Subject: [PATCH 085/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 10209d309..ddb001503 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ -Wcast-qual -Wdisabled-optimization -Winit-self \ -Wmissing-declarations -Wmissing-include-dirs \ -Wredundant-decls -Wshadow -Wsign-conversion \ - -Wundef -Wno-unused -Wformat=2 + -Wundef -Wno-unused -Wformat=2 -fsanitize=address # -ffast-math # Problematic for Python @@ -28,7 +28,7 @@ OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O0 -g -fsanitize=address -gdwarf-4 # Valgrind info +OPT=-O0 -g -gdwarf-4 # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= From cfde366c51c1b8871d526d44359626530c2f696a Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 23 Nov 2022 09:40:44 +0000 Subject: [PATCH 086/138] Format code with yapf, black, gofmt, gofumpt, autopep8 and isort --- src/python/code/gnoll/parser.py | 19 ++++++++----------- tests/python/test_macros.py | 6 ++---- tests/python/util.py | 15 +++++++-------- 3 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index f0d5c11d8..73357f00c 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -45,12 +45,7 @@ def raise_gnoll_error(value): raise err -def roll(s, - verbose=False, - mock=None, - mock_const=3, - breakdown=False, - builtins=False): +def roll(s, verbose=False, mock=None, mock_const=3, breakdown=False, builtins=False): """ Parse some dice notation with GNOLL. @param s the string to parse @@ -60,9 +55,9 @@ def roll(s, @param breakdown get the details of each dice rolled, not just the final result @return return code, final result, dice breakdown (None if disabled) """ - temp = tempfile.NamedTemporaryFile(prefix="gnoll_roll_", - suffix=".die", - delete=False) + temp = tempfile.NamedTemporaryFile( + prefix="gnoll_roll_", suffix=".die", delete=False + ) def make_native_type(v): """ @@ -133,9 +128,11 @@ def extract_from_dice_file(lines, seperator): arg = "".join(sys.argv[1:]) arg = arg if arg != "" else "1d20" code, r, detailed_r = roll(arg, verbose=False) - print(f""" + print( + f""" [[GNOLL Results]] Dice Roll: {arg} Result: {r} Exit Code: {code}, -Dice Breakdown: {detailed_r}""") +Dice Breakdown: {detailed_r}""" + ) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index efa6a395a..af3d31e6d 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -19,15 +19,13 @@ def test_macro_storage(r, out, mock): assert result == out -@pytest.mark.parametrize("r,out,mock", - [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) +@pytest.mark.parametrize("r,out,mock", [("#MY_DIE=d{A};@MY_DIE", "A", Mock.NO_MOCK)]) def test_macro_usage(r, out, mock): result, _ = roll(r, mock_mode=mock) assert result == out -@pytest.mark.skip("Currently no support for rerolling operations like Addition" - ) +@pytest.mark.skip("Currently no support for rerolling operations like Addition") def test_d66(): r = "#DSIXTYSIX=(d6*10)+d6;@DSIXTYSIX" result, _ = roll(r, mock_mode=Mock.RETURN_CONSTANT, mock_const=3) diff --git a/tests/python/util.py b/tests/python/util.py index ed815691c..d530892be 100644 --- a/tests/python/util.py +++ b/tests/python/util.py @@ -6,9 +6,11 @@ import numpy as np GRAMMAR_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/grammar")) + os.path.join(os.path.dirname(__file__), "../../src/grammar") +) SRC_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/")) + os.path.join(os.path.dirname(__file__), "../../src/python/code/gnoll/") +) MK_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) first_run = True @@ -52,12 +54,9 @@ def make_all(): raise ValueError -def roll(s, - mock_mode=Mock.NO_MOCK, - mock_const=3, - verbose=False, - squeeze=True, - builtins=False): +def roll( + s, mock_mode=Mock.NO_MOCK, mock_const=3, verbose=False, squeeze=True, builtins=False +): global first_run if first_run: From b9c9b959964ff9b84f75ce612a33bd636a1d788b Mon Sep 17 00:00:00 2001 From: Ian Date: Sat, 26 Nov 2022 12:13:58 +0000 Subject: [PATCH 087/138] remove lots of memory leaks + some smaller bugs --- Makefile | 8 +- src/grammar/dice.yacc | 164 +++++++++++++++++---------- src/grammar/operations/macro_logic.c | 2 +- src/grammar/rolls/vec.h | 2 + src/grammar/util/safe_functions.c | 102 +++++++++++++++-- src/grammar/util/safe_functions.h | 2 +- src/grammar/util/string_functions.c | 41 +++++++ src/grammar/util/string_functions.h | 1 + src/grammar/util/vector_functions.c | 3 + 9 files changed, 252 insertions(+), 73 deletions(-) create mode 100644 src/grammar/util/string_functions.c create mode 100644 src/grammar/util/string_functions.h diff --git a/Makefile b/Makefile index ddb001503..d83f3ac47 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,15 @@ else STANDARD= -std=c99 endif -OPT=-O3 $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ + +ADDRESS_SANITIZER= -fsanitize=address -fno-omit-frame-pointer -static-libasan -g + +OPT=-O0 \ + $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ -Wcast-qual -Wdisabled-optimization -Winit-self \ -Wmissing-declarations -Wmissing-include-dirs \ -Wredundant-decls -Wshadow -Wsign-conversion \ - -Wundef -Wno-unused -Wformat=2 -fsanitize=address + -Wundef -Wno-unused -Wformat=2 $(ADDRESS_SANITIZER) # -ffast-math # Problematic for Python diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index f82b6790a..c75175fb7 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -21,6 +21,7 @@ #include #include "external/pcg_basic.h" #include "external/tinydir.h" +#include "util/string_functions.h" #define UNUSED(x) (void)(x) // Avoid conflicts with MacOs predefined macros @@ -33,7 +34,6 @@ int yyerror(const char* s); int yywrap(); //TODO: move to external file -char * concat_strings(char ** s, int num_s); #ifdef JUST_YACC int yydebug=1; @@ -742,25 +742,36 @@ dice_operations: die_roll: NUMBER die_symbol NUMBER EXPLOSION ONCE { - int start_from = $2.content[0]; + vec numA = $1; + vec ds = $2; + vec numB = $3; + + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; roll_plain_sided_dice( - &$1, - &$3, + &numA, + &numB, &$$, ONLY_ONCE_EXPLOSION, start_from ); + free_vector(numA); + free_vector(ds); + free_vector(numB); } | die_symbol NUMBER EXPLOSION ONCE { + + vec numA = $1; + vec ds = $2; + vec numB = $3; - int start_from = $1.content[0]; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); @@ -768,34 +779,47 @@ die_roll: roll_plain_sided_dice( &number_of_dice, - &$2, - &$$, + &ds, + &numB, ONLY_ONCE_EXPLOSION, start_from ); + free_vector(numA); + free_vector(ds); + free_vector(numB); } | NUMBER die_symbol NUMBER EXPLOSION PENETRATE { - int start_from = $2.content[0]; + vec numA = $1; + vec ds = $2; + vec numB = $3; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; roll_plain_sided_dice( - &$1, - &$3, + &numA, + &numB, &$$, PENETRATING_EXPLOSION, start_from ); + + free_vector(numA); + free_vector(ds); + free_vector(numB); } | die_symbol NUMBER EXPLOSION PENETRATE { - int start_from = $1.content[0]; + vec ds = $1; + vec numB = $2; + + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); @@ -803,35 +827,46 @@ die_roll: roll_plain_sided_dice( &number_of_dice, - &$2, + &numB, &$$, PENETRATING_EXPLOSION, start_from ); + free_vector(number_of_dice); + free_vector(ds); + free_vector(numB); } | NUMBER die_symbol NUMBER EXPLOSION { - int start_from = $2.content[0]; + vec numA = $1; + vec ds = $2; + vec numB = $3; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; roll_plain_sided_dice( - &$1, - &$3, + &numA, + &numB, &$$, PENETRATING_EXPLOSION, start_from ); + free_vector(numA); + free_vector(ds); + free_vector(numB); } | die_symbol NUMBER EXPLOSION { - int start_from = $1.content[0]; + vec ds = $1; + vec numB = $2; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); @@ -839,34 +874,44 @@ die_roll: roll_plain_sided_dice( &number_of_dice, - &$2, + &numB, &$$, STANDARD_EXPLOSION, start_from ); + free_vector(numB); + free_vector(ds); + free_vector(number_of_dice); } | NUMBER die_symbol NUMBER { - int start_from = $2.content[0]; + vec ds = $2; + vec numB = $3; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; roll_plain_sided_dice( - &$1, - &$3, + &number_of_dice, + &numB, &$$, NO_EXPLOSION, start_from ); + free_vector(numB); + free_vector(ds); + free_vector(number_of_dice); } | die_symbol NUMBER { + vec ds = $1; + vec numB = $2; - int start_from = $1.content[0]; + int start_from = ds.content[0]; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); @@ -874,11 +919,14 @@ die_roll: roll_plain_sided_dice( &number_of_dice, - &$2, + &numB, &$$, NO_EXPLOSION, start_from ); + free_vector(number_of_dice); + free_vector(ds); + free_vector(numB); } | NUMBER die_symbol MODULO @@ -1005,19 +1053,22 @@ custom_symbol_dice: &right, &result_vec ); + + free_vector(left); + free_vector(right); $$ = result_vec; } | die_symbol SYMBOL_LBRACE csd SYMBOL_RBRACE { // d{SYM} - vec csd = $3; + vec csd_vec = $3; vec result_vec; vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; - if (csd.dtype == NUMERIC){ + if (csd_vec.dtype == NUMERIC){ vec dice_sides; vec num_dice; initialize_vector(&dice_sides, NUMERIC, 1); @@ -1025,8 +1076,8 @@ custom_symbol_dice: initialize_vector(&result_vec, NUMERIC, 1); num_dice.content[0] = 1; - int start_value = csd.content[0]; - int end_value = csd.content[csd.length-1]; + int start_value = csd_vec.content[0]; + int end_value = csd_vec.content[csd_vec.length-1]; dice_sides.content[0] = end_value - start_value + 1; // Range @@ -1040,29 +1091,35 @@ custom_symbol_dice: }else{ initialize_vector(&result_vec, SYMBOLIC, 1); - printf("YO\n"); roll_params rp = { .number_of_dice=(unsigned int)number_of_dice.content[0], - .die_sides=csd.length, + .die_sides=csd_vec.length, .dtype=SYMBOLIC, .start_value=0, - .symbol_pool=(char **)safe_calloc(csd.length , sizeof(char *)) + .symbol_pool=(char **)safe_calloc(csd_vec.length , sizeof(char *)) }; - for(unsigned int i = 0; i != csd.length; i++){ + for(unsigned int i = 0; i != csd_vec.length; i++){ rp.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); - memcpy(rp.symbol_pool[i], csd.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); + memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); } result_vec.source = rp; + result_vec.has_source = true; // Custom Symbol roll_symbolic_dice( &number_of_dice, - &csd, + &csd_vec, &result_vec ); printf("YO2\n"); } + + print_vec(csd_vec); + // printf("CSD IS SYMBOLIC? %i\n", (int)csd_vec.dtype); + + free_vector(number_of_dice); + free_vector(csd_vec); $$ = result_vec; } | @@ -1137,6 +1194,8 @@ csd: r.symbols, r.length, new_vector.symbols ); + free_vector(l); + free_vector(r); $$ = new_vector; } | @@ -1313,6 +1372,10 @@ int roll_full_options( } void load_builtins(char* root){ + + int db_setting = dice_breakdown; + dice_breakdown = 0; // Dont want dice breakdown for all the macro loading + tinydir_dir dir = (tinydir_dir){0}; tinydir_open(&dir, root); @@ -1363,13 +1426,14 @@ void load_builtins(char* root){ free(path); free(stored_str); } - if(count >= 1){ + /* if(count >= 1){ break; - } + } */ tinydir_next(&dir); } tinydir_close(&dir); + dice_breakdown = db_setting; return; } @@ -1396,32 +1460,10 @@ int mock_roll(char * s, char * f, int mock_value, int mock_const){ return roll_full_options(s, f, 0, 0, 1, 0, mock_value, mock_const); } -char * concat_strings(char ** s, int num_s){ - unsigned int size_total = 0; - int spaces = 0; - for(int i = 1; i != num_s + 1; i++){ - size_total += strlen(s[i]) + 1; - } - if (num_s > 1){ - spaces = 1; - size_total -= 1; // no need for trailing space - } - - char * result; - result = (char *)safe_calloc(sizeof(char), (size_total+1)); - if(gnoll_errno){return NULL;} - - for(int i = 1; i != num_s + 1; i++){ - strcat(result, s[i]); - if (spaces && i < num_s){ - strcat(result, " "); // Add spaces - } - } - return result; -} - int main(int argc, char **str){ - char * s = concat_strings(str, argc - 1); + // Join arguments if they came in as seperate strings + char * s = concat_strings(&str[1], (unsigned int)(argc - 1)); + remove("output.dice"); roll_full_options( s, @@ -1429,12 +1471,14 @@ int main(int argc, char **str){ 1, // Verbose 1, // Introspect 0, // Mocking - 0, // Builtins + 1, // Builtins 0, 0 ); + print_gnoll_errors(); FILE *f = fopen("output.dice","r"); int c; + printf("Result:\n"); if (f){ while((c = getc(f)) != EOF){ putchar(c); diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macro_logic.c index fcec50181..defd45066 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macro_logic.c @@ -50,7 +50,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - printf("Macro:: Check existancee\n"); + printf("Macro:: Check existance\n"); HASH_FIND_INT(macros, &k, s); // id already in the hash? diff --git a/src/grammar/rolls/vec.h b/src/grammar/rolls/vec.h index 13ffa1b59..9d15b19cc 100644 --- a/src/grammar/rolls/vec.h +++ b/src/grammar/rolls/vec.h @@ -3,6 +3,7 @@ #include "dice_enums.h" #include "dice_roll_structs.h" +#include typedef struct vec { DIE_TYPE dtype; @@ -12,6 +13,7 @@ typedef struct vec { // maybe use union? If it exists in c char** symbols; roll_params source; + bool has_source; } vec; #endif diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 4486094f0..54014695b 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -9,6 +9,80 @@ #include "shared_header.h" int gnoll_errno = 0; +extern int verbose; + + +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_RESET "\x1b[0m" + + +void print_gnoll_errors(){ + /** + * @brief A human-readable translation of the gnoll error codes + * + */ + if(verbose){ + switch(gnoll_errno){ + case SUCCESS:{ + printf("%sErrorCheck: No Errors.%s\n",ANSI_COLOR_GREEN, ANSI_COLOR_RESET); + break; + } + case BAD_ALLOC:{ + printf("%sErrorCheck: Bad Allocation.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case BAD_FILE:{ + printf("%sErrorCheck: Bad File.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case NOT_IMPLEMENTED:{ + printf("%sErrorCheck: Not Implemented.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case INTERNAL_ASSERT:{ + printf("%sErrorCheck: Internal Assertion.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case UNDEFINED_BEHAVIOUR:{ + printf("%sErrorCheck: Undefined Behaviour.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case BAD_STRING:{ + printf("%sErrorCheck: Bad String.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case OUT_OF_RANGE:{ + printf("%sErrorCheck: Out of Range.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case IO_ERROR:{ + printf("%sErrorCheck: I/O Error.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case MAX_LOOP_LIMIT_HIT:{ + printf("%sErrorCheck: Max Loop Limit Reached.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case SYNTAX_ERROR:{ + printf("%sErrorCheck: Syntax Error.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case DIVIDE_BY_ZERO:{ + printf("%sErrorCheck: Divide by Zero.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + case UNDEFINED_MACRO:{ + printf("%sErrorCheck: Undefined Macro.%s\n",ANSI_COLOR_RED, ANSI_COLOR_RESET); + break; + } + default:{ + printf("%sErrorCheck: Error (Undetermined. Code %i).%s\n",ANSI_COLOR_RED, gnoll_errno, ANSI_COLOR_RESET); + break; + } + } + } +} void *safe_malloc(size_t size) { /** @@ -37,12 +111,13 @@ void free_vector(vec v){ }else{ printf("vec: %p\n", (void*)v.symbols); free_2d_array(&v.symbols, v.length); + + if (v.has_source){ + // Should be always the same as length (But not sure that's true!) + free_2d_array(&v.source.symbol_pool, v.length); + } } } -void free_roll_params(roll_params* rp){ - (void *)rp; - // free_2d_array(&rp->symbol_pool, rp->die_sides); -} void free_2d_array(char ***arr, unsigned int items) { /** @@ -74,26 +149,35 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, // printf("alloc: %u\n", items); // printf("*alloc: %u\n", max_size); - // printf("%p\n", (void *)*dst); - if(*dst != 0){ - free(*dst); - } + printf("dst: %p (%i)\n", (void *)*dst, *dst != 0); + // if(*dst != 0){ + // free(*dst); + // } + printf("Allocate New Base Pointer...\n"); *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; } + + printf("Allocate 2nd Dimension... (%u times)\n", items); for (unsigned int i = 0; i != items; i++) { + + printf("Allocate 2nd Dimension...\n"); + printf("Old Address: %p\n", (void *)(*dst)); + printf("Old Address: %p\n", (void *)(*dst)[i]); (*dst)[i] = (char*)safe_calloc(sizeof(char), max_size); if (gnoll_errno) { return; } + + printf("Copy from Src... %p %p (size: %u)\n", (*dst)[i], src[i], max_size); memcpy((*dst)[i], src[i], max_size); } } -void *safe_calloc(size_t nitems, size_t size) { +void * safe_calloc(size_t nitems, size_t size) { /** * @brief Safe version of calloc. Populates gnoll_errno on error * @param size diff --git a/src/grammar/util/safe_functions.h b/src/grammar/util/safe_functions.h index 2633ff163..b967e5874 100644 --- a/src/grammar/util/safe_functions.h +++ b/src/grammar/util/safe_functions.h @@ -22,6 +22,7 @@ typedef enum { UNDEFINED_MACRO = 12 } ERROR_CODES; +void print_gnoll_errors(); void *safe_malloc(size_t size); void *safe_calloc(size_t nitems, size_t size); FILE *safe_fopen(const char *filename, const char *mode); @@ -34,7 +35,6 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, void free_2d_array(char ***arr, unsigned int items); void free_vector(vec v); -void free_roll_params(roll_params* rp); int fast_atoi(const char *str); diff --git a/src/grammar/util/string_functions.c b/src/grammar/util/string_functions.c new file mode 100644 index 000000000..aa91f0f52 --- /dev/null +++ b/src/grammar/util/string_functions.c @@ -0,0 +1,41 @@ + +#include "util/string_functions.h" +#include +#include "util/safe_functions.h" + +extern int gnoll_errno; + +char * concat_strings(char ** s, unsigned int num_s){ + /** + * @brief Given an array of strings, join them together. + * @param s array of strings + * @param num_s length of above array + * @return a string containing both substrings + */ + if (num_s == 1){ + return s[0]; + } + unsigned int size_total = 0; + int spaces = 0; + for(int i = 1; i != num_s + 1; i++){ + size_total += strlen(s[i]) + 1; + } + if (num_s > 1){ + spaces = 1; + size_total -= 1; // no need for trailing space + } + + printf("Size Total %u\n", size_total); + + char * result = (char *)safe_calloc((size_total+1), sizeof(char)); + if(gnoll_errno){return NULL;} + + for(int i = 1; i != num_s + 1; i++){ + // printf() + strcat(result, s[i]); + if (spaces && i < num_s){ + strcat(result, " "); // Add spaces + } + } + return result; +} \ No newline at end of file diff --git a/src/grammar/util/string_functions.h b/src/grammar/util/string_functions.h new file mode 100644 index 000000000..a10510293 --- /dev/null +++ b/src/grammar/util/string_functions.h @@ -0,0 +1 @@ +char * concat_strings(char ** s, unsigned int num_s); \ No newline at end of file diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 12f846a3a..2939107ee 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -23,6 +23,7 @@ void light_initialize_vector(vec *vector, DIE_TYPE dt, */ vector->dtype = dt; vector->length = number_of_items; + vector->has_source = false; if (dt == NUMERIC) { vector->content = (int*)safe_calloc(number_of_items, sizeof(int)); @@ -45,8 +46,10 @@ void initialize_vector(vec *vector, DIE_TYPE dt, unsigned int number_of_items) { vector->dtype = dt; vector->length = number_of_items; + vector->has_source = false; if (dt == NUMERIC) { + printf("Valid Already? %p\n", (void *)vector->content); vector->content = (int*)safe_calloc(number_of_items, sizeof(int)); if (gnoll_errno) return; } else if (dt == SYMBOLIC) { From 96042a2bab340261dcc9f13063882f50c96b4b46 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:33:31 +0000 Subject: [PATCH 088/138] Rearrange and cleanup --- Makefile | 7 +- .../{rolls => constructs}/dice_enums.h | 36 +-- .../roll_parameters.h} | 30 +-- src/grammar/{rolls => constructs}/vec.h | 38 +-- src/grammar/dice.lex | 2 +- src/grammar/dice.yacc | 41 +-- .../{condition_checking.c => conditionals.c} | 212 +++++++-------- .../{condition_checking.h => conditionals.h} | 48 ++-- .../operations/{macro_logic.c => macros.c} | 252 +++++++++--------- .../operations/{macro_logic.h => macros.h} | 42 +-- .../rolls/{dice_logic.c => dice_core.c} | 8 +- .../rolls/{dice_logic.h => dice_core.h} | 2 +- .../rolls/{sided_dice.c => dice_frontend.c} | 144 +++++----- .../rolls/{sided_dice.h => dice_frontend.h} | 28 +- src/grammar/shared_header.h | 6 +- src/grammar/util/array_functions.c | 12 + src/grammar/util/array_functions.h | 8 + src/grammar/{rolls => util}/mocking.c | 114 ++++---- src/grammar/{rolls => util}/mocking.h | 34 +-- src/grammar/util/safe_functions.c | 15 +- src/grammar/util/vector_functions.c | 5 +- src/grammar/util/vector_functions.h | 4 +- src/grammar/yacc_header.h | 16 -- 23 files changed, 544 insertions(+), 560 deletions(-) rename src/grammar/{rolls => constructs}/dice_enums.h (94%) rename src/grammar/{rolls/dice_roll_structs.h => constructs/roll_parameters.h} (86%) rename src/grammar/{rolls => constructs}/vec.h (80%) rename src/grammar/operations/{condition_checking.c => conditionals.c} (92%) rename src/grammar/operations/{condition_checking.h => conditionals.h} (90%) rename src/grammar/operations/{macro_logic.c => macros.c} (91%) rename src/grammar/operations/{macro_logic.h => macros.h} (87%) rename src/grammar/rolls/{dice_logic.c => dice_core.c} (96%) rename src/grammar/rolls/{dice_logic.h => dice_core.h} (92%) rename src/grammar/rolls/{sided_dice.c => dice_frontend.c} (94%) rename src/grammar/rolls/{sided_dice.h => dice_frontend.h} (82%) create mode 100644 src/grammar/util/array_functions.c create mode 100644 src/grammar/util/array_functions.h rename src/grammar/{rolls => util}/mocking.c (94%) rename src/grammar/{rolls => util}/mocking.h (87%) delete mode 100644 src/grammar/yacc_header.h diff --git a/Makefile b/Makefile index d83f3ac47..6c6928daa 100644 --- a/Makefile +++ b/Makefile @@ -14,12 +14,13 @@ endif ADDRESS_SANITIZER= -fsanitize=address -fno-omit-frame-pointer -static-libasan -g -OPT=-O0 \ +OPT=-O3 \ $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ -Wcast-qual -Wdisabled-optimization -Winit-self \ -Wmissing-declarations -Wmissing-include-dirs \ -Wredundant-decls -Wshadow -Wsign-conversion \ - -Wundef -Wno-unused -Wformat=2 $(ADDRESS_SANITIZER) + -Wundef -Wno-unused -Wformat=2 +# $(ADDRESS_SANITIZER) # -ffast-math # Problematic for Python @@ -32,7 +33,7 @@ OPT=-O0 \ # === DEBUG OPTIONS ==== DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O0 -g -gdwarf-4 # Valgrind info +OPT=-O0 -g -gdwarf-4 $(ADDRESS_SANITIZER) # Valgrind info PARSER_DEBUG:=--debug --verbose else PARSER_DEBUG:= diff --git a/src/grammar/rolls/dice_enums.h b/src/grammar/constructs/dice_enums.h similarity index 94% rename from src/grammar/rolls/dice_enums.h rename to src/grammar/constructs/dice_enums.h index 04990f270..d183043bf 100644 --- a/src/grammar/rolls/dice_enums.h +++ b/src/grammar/constructs/dice_enums.h @@ -1,18 +1,18 @@ -#ifndef __DICE_ENUMS_H__ -#define __DICE_ENUMS_H__ - -typedef enum { - // 0 is invalid - SYMBOLIC = 1, - NUMERIC = 2 -} DIE_TYPE; - -typedef enum { - NO_EXPLOSION = 0, - STANDARD_EXPLOSION = 1, - ONLY_ONCE_EXPLOSION = 2, - PENETRATING_EXPLOSION = 3, - DIMINISHING_EXPLOSION = 4 -} EXPLOSION_TYPE; - -#endif +#ifndef __DICE_ENUMS_H__ +#define __DICE_ENUMS_H__ + +typedef enum { + // 0 is invalid + SYMBOLIC = 1, + NUMERIC = 2 +} DIE_TYPE; + +typedef enum { + NO_EXPLOSION = 0, + STANDARD_EXPLOSION = 1, + ONLY_ONCE_EXPLOSION = 2, + PENETRATING_EXPLOSION = 3, + DIMINISHING_EXPLOSION = 4 +} EXPLOSION_TYPE; + +#endif diff --git a/src/grammar/rolls/dice_roll_structs.h b/src/grammar/constructs/roll_parameters.h similarity index 86% rename from src/grammar/rolls/dice_roll_structs.h rename to src/grammar/constructs/roll_parameters.h index f01e86bfd..cac7e04c1 100644 --- a/src/grammar/rolls/dice_roll_structs.h +++ b/src/grammar/constructs/roll_parameters.h @@ -1,15 +1,15 @@ -#ifndef __DICE_ROLL_STRUCTS_H__ -#define __DICE_ROLL_STRUCTS_H__ - -#include "dice_enums.h" - -typedef struct roll_params { - unsigned int number_of_dice; - unsigned int die_sides; - EXPLOSION_TYPE explode; - char** symbol_pool; - int start_value; - DIE_TYPE dtype; -} roll_params; - -#endif +#ifndef __DICE_ROLL_STRUCTS_H__ +#define __DICE_ROLL_STRUCTS_H__ + +#include "constructs/dice_enums.h" + +typedef struct roll_params { + unsigned int number_of_dice; + unsigned int die_sides; + EXPLOSION_TYPE explode; + char** symbol_pool; + int start_value; + DIE_TYPE dtype; +} roll_params; + +#endif diff --git a/src/grammar/rolls/vec.h b/src/grammar/constructs/vec.h similarity index 80% rename from src/grammar/rolls/vec.h rename to src/grammar/constructs/vec.h index 9d15b19cc..ed114d1d6 100644 --- a/src/grammar/rolls/vec.h +++ b/src/grammar/constructs/vec.h @@ -1,19 +1,19 @@ -#ifndef __VEC_H__ -#define __VEC_H__ - -#include "dice_enums.h" -#include "dice_roll_structs.h" -#include - -typedef struct vec { - DIE_TYPE dtype; - int* content; - unsigned int length; - // TODO: Split length into content_length and symbol length - // maybe use union? If it exists in c - char** symbols; - roll_params source; - bool has_source; -} vec; - -#endif +#ifndef __VEC_H__ +#define __VEC_H__ + +#include "constructs/dice_enums.h" +#include "constructs/roll_parameters.h" +#include + +typedef struct vec { + DIE_TYPE dtype; + int* content; + unsigned int length; + // TODO: Split length into content_length and symbol length + // maybe use union? If it exists in c + char** symbols; + roll_params source; + bool has_source; +} vec; + +#endif diff --git a/src/grammar/dice.lex b/src/grammar/dice.lex index 7899f071e..b2bc84023 100644 --- a/src/grammar/dice.lex +++ b/src/grammar/dice.lex @@ -6,7 +6,7 @@ #include #include "shared_header.h" #include "util/safe_functions.h" - #include "operations/condition_checking.h" + #include "operations/conditionals.h" #include "y.tab.h" #include diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index c75175fb7..dde826e76 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -9,18 +9,18 @@ #include #include #include -#include "yacc_header.h" -#include "util/vector_functions.h" -#include "shared_header.h" -#include "rolls/dice_logic.h" -#include "util/safe_functions.h" -#include "operations/macro_logic.h" -#include "rolls/sided_dice.h" -#include "rolls/mocking.h" -#include "operations/condition_checking.h" #include +#include "shared_header.h" #include "external/pcg_basic.h" #include "external/tinydir.h" +#include "operations/macros.h" +#include "operations/conditionals.h" +#include "rolls/dice_core.h" +#include "rolls/dice_frontend.h" +#include "util/mocking.h" +#include "util/safe_functions.h" +#include "util/array_functions.h" +#include "util/vector_functions.h" #include "util/string_functions.h" #define UNUSED(x) (void)(x) @@ -49,10 +49,10 @@ extern int gnoll_errno; extern struct macro_struct *macros; pcg32_random_t rng; -// Registers - -// TODO: It would be better to fit arbitrary length strings. +// Function Signatures for this file +int initialize(); +// Functions int initialize(){ if (!seeded){ unsigned long int tick = (unsigned long)time(0)+(unsigned long)clock(); @@ -66,16 +66,6 @@ int initialize(){ return 0; } -int collapse(int * arr, unsigned int len){ - return sum(arr, len); -} - -int sum(int * arr, unsigned int len){ - int result = 0; - for(unsigned int i = 0; i != len; i++) result += arr[i]; - return result; -} - %} @@ -142,10 +132,7 @@ macro_statement: vec key = $2; vec value = $4; - printf("Register\n"); register_macro(&key, &value.source); - printf("Registered\n"); - // Cleanup free_vector(key); @@ -1112,10 +1099,8 @@ custom_symbol_dice: &csd_vec, &result_vec ); - printf("YO2\n"); } - print_vec(csd_vec); // printf("CSD IS SYMBOLIC? %i\n", (int)csd_vec.dtype); free_vector(number_of_dice); @@ -1468,7 +1453,7 @@ int main(int argc, char **str){ roll_full_options( s, "output.dice", - 1, // Verbose + 0, // Verbose 1, // Introspect 0, // Mocking 1, // Builtins diff --git a/src/grammar/operations/condition_checking.c b/src/grammar/operations/conditionals.c similarity index 92% rename from src/grammar/operations/condition_checking.c rename to src/grammar/operations/conditionals.c index 3db17a98d..cb3607ba5 100644 --- a/src/grammar/operations/condition_checking.c +++ b/src/grammar/operations/conditionals.c @@ -1,106 +1,106 @@ -#include "condition_checking.h" - -#include -#include -#include - -#include "rolls/dice_logic.h" -#include "shared_header.h" -#include "util/safe_functions.h" -#include "util/vector_functions.h" -#include "yacc_header.h" - -extern int gnoll_errno; - -/** - * @brief Comparision of a collapsed vector to a value - * - * @param x vector containing dice rolls - * @param y vector containing 1 comparision value - * @param c enum indicating comparsion type - * @return true - the condition is True - * @return false - the condition is False - */ -int check_condition(vec* x, vec* y, COMPARATOR c) { - if (gnoll_errno) return 1; - - if(c == IS_UNIQUE || c == IF_ODD || c == IF_EVEN){ - return check_condition_vector(x, c); - }else{ - - int xvalue = collapse(x->content, x->length); - int yvalue = y->content[0]; - return check_condition_scalar(xvalue, yvalue, c); - } -} - -int check_condition_vector(vec* v, COMPARATOR c) { - switch (c){ - case IS_UNIQUE: { - gnoll_errno = NOT_IMPLEMENTED; - return 1; - } - case IF_EVEN:{ - int x = collapse(v->content, v->length); - return (x+1) % 2; - } - case IF_ODD: { - int x = collapse(v->content, v->length); - return x % 2; - } - default: { - gnoll_errno = NOT_IMPLEMENTED; - return 0; - } - } -} - -int check_condition_scalar(int x, int y, COMPARATOR c) { - if (gnoll_errno) return 1; - - int xvalue = x; - int yvalue = y; - switch (c) { - case EQUALS: { - return xvalue == yvalue; - } - case NOT_EQUAL: { - return xvalue != yvalue; - } - case LESS_THAN: { - return xvalue < yvalue; - } - case GREATER_THAN: { - return xvalue > yvalue; - } - case LESS_OR_EQUALS: { - return xvalue <= yvalue; - } - case GREATER_OR_EQUALS: { - return xvalue >= yvalue; - } - case IS_UNIQUE: { - // Unique by the fact that it is scalar - return 1; - } - case IF_ODD: { - return x % 2; - } - case IF_EVEN: { - return (x+1) % 2; - } - case INVALID: { - printf("Invalid Conditional\n"); - gnoll_errno = UNDEFINED_BEHAVIOUR; - return 0; - } - default: { - printf("Unknown Conditional\n"); - gnoll_errno = UNDEFINED_BEHAVIOUR; - return 0; - } - } - printf("Unknown Conditional\n"); - gnoll_errno = UNDEFINED_BEHAVIOUR; - return 1; -} +#include "conditionals.h" + +#include +#include +#include + +#include "rolls/dice_core.h" +#include "shared_header.h" +#include "util/safe_functions.h" +#include "util/vector_functions.h" +#include "util/array_functions.h" + +extern int gnoll_errno; + +/** + * @brief Comparision of a collapsed vector to a value + * + * @param x vector containing dice rolls + * @param y vector containing 1 comparision value + * @param c enum indicating comparsion type + * @return true - the condition is True + * @return false - the condition is False + */ +int check_condition(vec* x, vec* y, COMPARATOR c) { + if (gnoll_errno) return 1; + + if(c == IS_UNIQUE || c == IF_ODD || c == IF_EVEN){ + return check_condition_vector(x, c); + }else{ + + int xvalue = collapse(x->content, x->length); + int yvalue = y->content[0]; + return check_condition_scalar(xvalue, yvalue, c); + } +} + +int check_condition_vector(vec* v, COMPARATOR c) { + switch (c){ + case IS_UNIQUE: { + gnoll_errno = NOT_IMPLEMENTED; + return 1; + } + case IF_EVEN:{ + int x = collapse(v->content, v->length); + return (x+1) % 2; + } + case IF_ODD: { + int x = collapse(v->content, v->length); + return x % 2; + } + default: { + gnoll_errno = NOT_IMPLEMENTED; + return 0; + } + } +} + +int check_condition_scalar(int x, int y, COMPARATOR c) { + if (gnoll_errno) return 1; + + int xvalue = x; + int yvalue = y; + switch (c) { + case EQUALS: { + return xvalue == yvalue; + } + case NOT_EQUAL: { + return xvalue != yvalue; + } + case LESS_THAN: { + return xvalue < yvalue; + } + case GREATER_THAN: { + return xvalue > yvalue; + } + case LESS_OR_EQUALS: { + return xvalue <= yvalue; + } + case GREATER_OR_EQUALS: { + return xvalue >= yvalue; + } + case IS_UNIQUE: { + // Unique by the fact that it is scalar + return 1; + } + case IF_ODD: { + return x % 2; + } + case IF_EVEN: { + return (x+1) % 2; + } + case INVALID: { + printf("Invalid Conditional\n"); + gnoll_errno = UNDEFINED_BEHAVIOUR; + return 0; + } + default: { + printf("Unknown Conditional\n"); + gnoll_errno = UNDEFINED_BEHAVIOUR; + return 0; + } + } + printf("Unknown Conditional\n"); + gnoll_errno = UNDEFINED_BEHAVIOUR; + return 1; +} diff --git a/src/grammar/operations/condition_checking.h b/src/grammar/operations/conditionals.h similarity index 90% rename from src/grammar/operations/condition_checking.h rename to src/grammar/operations/conditionals.h index 5534ffed0..a571aa15b 100644 --- a/src/grammar/operations/condition_checking.h +++ b/src/grammar/operations/conditionals.h @@ -1,24 +1,24 @@ - -#ifndef __ROLL_CONDITION_CHECKING_H__ -#define __ROLL_CONDITION_CHECKING_H__ - -#include "rolls/vec.h" - -typedef enum { - INVALID = 0, - EQUALS = 1, - GREATER_THAN = 2, - LESS_THAN = 3, - GREATER_OR_EQUALS = 4, - LESS_OR_EQUALS = 5, - NOT_EQUAL = 6, - IS_UNIQUE = 7, - IF_EVEN = 8, - IF_ODD = 9, -} COMPARATOR; - -int check_condition(vec* x, vec* y, COMPARATOR c); - -int check_condition_scalar(int x, int y, COMPARATOR c); -int check_condition_vector(vec* v, COMPARATOR c); -#endif + +#ifndef __ROLL_CONDITION_CHECKING_H__ +#define __ROLL_CONDITION_CHECKING_H__ + +#include "constructs/vec.h" + +typedef enum { + INVALID = 0, + EQUALS = 1, + GREATER_THAN = 2, + LESS_THAN = 3, + GREATER_OR_EQUALS = 4, + LESS_OR_EQUALS = 5, + NOT_EQUAL = 6, + IS_UNIQUE = 7, + IF_EVEN = 8, + IF_ODD = 9, +} COMPARATOR; + +int check_condition(vec* x, vec* y, COMPARATOR c); + +int check_condition_scalar(int x, int y, COMPARATOR c); +int check_condition_vector(vec* v, COMPARATOR c); +#endif diff --git a/src/grammar/operations/macro_logic.c b/src/grammar/operations/macros.c similarity index 91% rename from src/grammar/operations/macro_logic.c rename to src/grammar/operations/macros.c index defd45066..c7e025ac5 100644 --- a/src/grammar/operations/macro_logic.c +++ b/src/grammar/operations/macros.c @@ -1,125 +1,127 @@ - -#include "operations/macro_logic.h" - -#include "external/uthash.h" -#include "shared_header.h" -#include "util/safe_functions.h" -#include "util/vector_functions.h" -#include "yacc_header.h" - -// Initialized to NULL (Important) -struct macro_struct *macros = NULL; - -extern int gnoll_errno; - -unsigned long hash_function(unsigned char *str) { - /** - * @brief DJB2 hashing algorithm from http://www.cse.yorku.ca/~oz/hash.html - * - * @param str string to encode - * @return numeric hash encoding - */ - - unsigned long hash = 5381; - int c; - - while ((c = *str++)) { - hash = ((hash << 5) + hash) + (unsigned int)c; /* hash * 33 + c */ - } - return hash; -} - -void register_macro(vec *macro_name, roll_params *to_store) { - /** - * @brief Register a Macro in GNOLL - * - * @param macro_name - Unique name for macro reference - * @param to_store - Structure containing information on how to roll the dice - * referenced to by the key - */ - - if (gnoll_errno) {return;} - - struct macro_struct *s; - char *skey = macro_name->symbols[0]; - - printf("Macro:: Get ID\n"); - unsigned long key = hash_function((unsigned char *)skey); - int k = (int)key; - - unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - - - printf("Macro:: Check existance\n"); - HASH_FIND_INT(macros, &k, s); // id already in the hash? - - - if (s == NULL) { - s = (struct macro_struct *)safe_malloc(sizeof *s); - - if (gnoll_errno) { - return; - } - s->id = (int)key; - HASH_ADD_INT(macros, id, s); // id: name of key field - } - - - memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); - // s->stored_dice_roll.symbol_pool = NULL; - - if (is_symbolic) { - // free symbols from roll in S - // free_2d_array(&s->stored_dice_roll.symbol_pool, - // s->stored_dice_roll.die_sides); - safe_copy_2d_chararray_with_allocation( - &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, - to_store->die_sides, MAX_SYMBOL_LENGTH); - - // free_roll_params(to_store); //new - // free_2d_array(&to_store->symbol_pool, to_store->die_sides); - } - - //free(s); //new - //free(skey); //new -} - -void search_macros(char *skey, roll_params *to_store) { - /** - * @brief Search for a registered macro - * - * @param macro_name - Unique name for macro reference - * @param to_store - Where to store the macro, if found - */ - if (gnoll_errno) { - return; - } - - unsigned long key = hash_function((unsigned char *)skey); - int k = (int)key; - - if (gnoll_errno) { - return; - } - - struct macro_struct *s; - - HASH_FIND_INT(macros, &k, s); /* s: output pointer */ - - if (s == NULL) { - gnoll_errno = UNDEFINED_MACRO; - return; - } - - *to_store = s->stored_dice_roll; - to_store->symbol_pool = NULL; - - unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - - if (is_symbolic) { - free_2d_array(&to_store->symbol_pool, to_store->die_sides); - safe_copy_2d_chararray_with_allocation( - &to_store->symbol_pool, s->stored_dice_roll.symbol_pool, - to_store->die_sides, MAX_SYMBOL_LENGTH); - } -} + +#include "operations/macros.h" + +#include "external/uthash.h" +#include "shared_header.h" +#include "util/safe_functions.h" +#include "util/vector_functions.h" +#include "shared_header.h" + +extern int verbose; + +// Initialized to NULL (Important) +struct macro_struct *macros = NULL; + +extern int gnoll_errno; + +unsigned long hash_function(unsigned char *str) { + /** + * @brief DJB2 hashing algorithm from http://www.cse.yorku.ca/~oz/hash.html + * + * @param str string to encode + * @return numeric hash encoding + */ + + unsigned long hash = 5381; + int c; + + while ((c = *str++)) { + hash = ((hash << 5) + hash) + (unsigned int)c; /* hash * 33 + c */ + } + return hash; +} + +void register_macro(vec *macro_name, roll_params *to_store) { + /** + * @brief Register a Macro in GNOLL + * + * @param macro_name - Unique name for macro reference + * @param to_store - Structure containing information on how to roll the dice + * referenced to by the key + */ + + if (gnoll_errno) {return;} + + struct macro_struct *s; + char *skey = macro_name->symbols[0]; + + if (verbose) printf("Macro:: Get ID\n"); + unsigned long key = hash_function((unsigned char *)skey); + int k = (int)key; + + unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; + + + if (verbose) printf("Macro:: Check existance\n"); + HASH_FIND_INT(macros, &k, s); // id already in the hash? + + + if (s == NULL) { + s = (struct macro_struct *)safe_malloc(sizeof *s); + + if (gnoll_errno) { + return; + } + s->id = (int)key; + HASH_ADD_INT(macros, id, s); // id: name of key field + } + + + memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); + // s->stored_dice_roll.symbol_pool = NULL; + + if (is_symbolic) { + // free symbols from roll in S + // free_2d_array(&s->stored_dice_roll.symbol_pool, + // s->stored_dice_roll.die_sides); + safe_copy_2d_chararray_with_allocation( + &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, + to_store->die_sides, MAX_SYMBOL_LENGTH); + + // free_roll_params(to_store); //new + // free_2d_array(&to_store->symbol_pool, to_store->die_sides); + } + + //free(s); //new + //free(skey); //new +} + +void search_macros(char *skey, roll_params *to_store) { + /** + * @brief Search for a registered macro + * + * @param macro_name - Unique name for macro reference + * @param to_store - Where to store the macro, if found + */ + if (gnoll_errno) { + return; + } + + unsigned long key = hash_function((unsigned char *)skey); + int k = (int)key; + + if (gnoll_errno) { + return; + } + + struct macro_struct *s; + + HASH_FIND_INT(macros, &k, s); /* s: output pointer */ + + if (s == NULL) { + gnoll_errno = UNDEFINED_MACRO; + return; + } + + *to_store = s->stored_dice_roll; + to_store->symbol_pool = NULL; + + unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; + + if (is_symbolic) { + free_2d_array(&to_store->symbol_pool, to_store->die_sides); + safe_copy_2d_chararray_with_allocation( + &to_store->symbol_pool, s->stored_dice_roll.symbol_pool, + to_store->die_sides, MAX_SYMBOL_LENGTH); + } +} diff --git a/src/grammar/operations/macro_logic.h b/src/grammar/operations/macros.h similarity index 87% rename from src/grammar/operations/macro_logic.h rename to src/grammar/operations/macros.h index fb3fe8c77..96a4f018e 100644 --- a/src/grammar/operations/macro_logic.h +++ b/src/grammar/operations/macros.h @@ -1,21 +1,21 @@ - -#ifndef __MACRO_LOGIC_H -#define __MACRO_LOGIC_H - -#include "external/uthash.h" -#include "rolls/dice_roll_structs.h" -#include "util/vector_functions.h" - -unsigned long hash_function(unsigned char *str); - -struct macro_struct { - int id; /* key */ - roll_params stored_dice_roll; - UT_hash_handle hh; /* makes this structure hashable */ -}; - -void register_macro(vec *macro_name, roll_params *to_store); - -void search_macros(char *skey, roll_params *to_store); - -#endif + +#ifndef __MACRO_LOGIC_H +#define __MACRO_LOGIC_H + +#include "external/uthash.h" +#include "constructs/roll_parameters.h" +#include "util/vector_functions.h" + +unsigned long hash_function(unsigned char *str); + +struct macro_struct { + int id; /* key */ + roll_params stored_dice_roll; + UT_hash_handle hh; /* makes this structure hashable */ +}; + +void register_macro(vec *macro_name, roll_params *to_store); + +void search_macros(char *skey, roll_params *to_store); + +#endif diff --git a/src/grammar/rolls/dice_logic.c b/src/grammar/rolls/dice_core.c similarity index 96% rename from src/grammar/rolls/dice_logic.c rename to src/grammar/rolls/dice_core.c index 2e4cb7464..71c525824 100644 --- a/src/grammar/rolls/dice_logic.c +++ b/src/grammar/rolls/dice_core.c @@ -1,4 +1,4 @@ -#include "rolls/dice_logic.h" +#include "rolls/dice_core.h" #include #include @@ -6,11 +6,11 @@ #include #include "external/pcg_basic.h" -#include "rolls/dice_enums.h" -#include "rolls/mocking.h" +#include "constructs/dice_enums.h" +#include "util/mocking.h" #include "shared_header.h" #include "util/safe_functions.h" -#include "yacc_header.h" +#include "shared_header.h" #if USE_SECURE_RANDOM == 1 #include diff --git a/src/grammar/rolls/dice_logic.h b/src/grammar/rolls/dice_core.h similarity index 92% rename from src/grammar/rolls/dice_logic.h rename to src/grammar/rolls/dice_core.h index cb428fde1..883fa6582 100644 --- a/src/grammar/rolls/dice_logic.h +++ b/src/grammar/rolls/dice_core.h @@ -1,7 +1,7 @@ #ifndef __DIE_LOGIC_H__ #define __DIE_LOGIC_H__ -#include "rolls/dice_enums.h" +#include "constructs/dice_enums.h" #include "shared_header.h" // #ifdef __cplusplus diff --git a/src/grammar/rolls/sided_dice.c b/src/grammar/rolls/dice_frontend.c similarity index 94% rename from src/grammar/rolls/sided_dice.c rename to src/grammar/rolls/dice_frontend.c index 57e697a41..9ed044afd 100644 --- a/src/grammar/rolls/sided_dice.c +++ b/src/grammar/rolls/dice_frontend.c @@ -1,72 +1,72 @@ -#include -#include -#include -#include - -#include "rolls/dice_logic.h" -#include "shared_header.h" -#include "util/safe_functions.h" -#include "util/vector_functions.h" -#include "yacc_header.h" - -extern int gnoll_errno; - -void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, - int start_offset) { - /** - * @brief Roll numeric dice - * @param x - Amount of dice (xDy) - * @param y - Dice Sides (xDy) - * @param result - Where to store the dice roll result - * @param explode Explosion logic to apply (if applicable) - * @param start_offset offset each dice roll by this amount - */ - if (gnoll_errno) return; - - // XdY - unsigned int num_dice = (unsigned int)x->content[0]; - unsigned int sides = (unsigned int)y->content[0]; - - // e.g. d4, it is implied that it is a single dice - roll_params rp; - rp.dtype = NUMERIC; - rp.number_of_dice = num_dice; - rp.die_sides = sides; - rp.explode = explode; - rp.start_value = start_offset; - int* roll_result = do_roll(rp); - initialize_vector(result, NUMERIC, num_dice); - result->content = roll_result; - result->source = rp; -} - -void roll_symbolic_dice(vec* x, vec* y, vec* result) { - /** - * @brief Roll symbolic dice - * @param x - Amount of dice (xDy) - * @param y - Dice Sides (xDy) - * @param result - Where to store the dice roll result - */ - if (gnoll_errno) return; - - unsigned int num_dice = (unsigned int)x->content[0]; - - // e.g. d4, it is implied that it is a single dice - roll_params rp; - rp.dtype = SYMBOLIC; - rp.number_of_dice = num_dice; - rp.die_sides = y->length; - rp.explode = (EXPLOSION_TYPE)0; - rp.symbol_pool = NULL; - - // Copy over memory to Symbol Pool for reloading - - free_2d_array(&rp.symbol_pool, rp.die_sides); - safe_copy_2d_chararray_with_allocation(&rp.symbol_pool, y->symbols, y->length, - MAX_SYMBOL_LENGTH); - rp.start_value = 0; // First index of array - - int* indexes = do_roll(rp); - - extract_symbols(y->symbols, result->symbols, indexes, rp.number_of_dice); -} +#include +#include +#include +#include + +#include "rolls/dice_core.h" +#include "shared_header.h" +#include "util/safe_functions.h" +#include "util/vector_functions.h" +#include "shared_header.h" + +extern int gnoll_errno; + +void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, + int start_offset) { + /** + * @brief Roll numeric dice + * @param x - Amount of dice (xDy) + * @param y - Dice Sides (xDy) + * @param result - Where to store the dice roll result + * @param explode Explosion logic to apply (if applicable) + * @param start_offset offset each dice roll by this amount + */ + if (gnoll_errno) return; + + // XdY + unsigned int num_dice = (unsigned int)x->content[0]; + unsigned int sides = (unsigned int)y->content[0]; + + // e.g. d4, it is implied that it is a single dice + roll_params rp; + rp.dtype = NUMERIC; + rp.number_of_dice = num_dice; + rp.die_sides = sides; + rp.explode = explode; + rp.start_value = start_offset; + int* roll_result = do_roll(rp); + initialize_vector(result, NUMERIC, num_dice); + result->content = roll_result; + result->source = rp; +} + +void roll_symbolic_dice(vec* x, vec* y, vec* result) { + /** + * @brief Roll symbolic dice + * @param x - Amount of dice (xDy) + * @param y - Dice Sides (xDy) + * @param result - Where to store the dice roll result + */ + if (gnoll_errno) return; + + unsigned int num_dice = (unsigned int)x->content[0]; + + // e.g. d4, it is implied that it is a single dice + roll_params rp; + rp.dtype = SYMBOLIC; + rp.number_of_dice = num_dice; + rp.die_sides = y->length; + rp.explode = (EXPLOSION_TYPE)0; + rp.symbol_pool = NULL; + + // Copy over memory to Symbol Pool for reloading + + free_2d_array(&rp.symbol_pool, rp.die_sides); + safe_copy_2d_chararray_with_allocation(&rp.symbol_pool, y->symbols, y->length, + MAX_SYMBOL_LENGTH); + rp.start_value = 0; // First index of array + + int* indexes = do_roll(rp); + + extract_symbols(y->symbols, result->symbols, indexes, rp.number_of_dice); +} diff --git a/src/grammar/rolls/sided_dice.h b/src/grammar/rolls/dice_frontend.h similarity index 82% rename from src/grammar/rolls/sided_dice.h rename to src/grammar/rolls/dice_frontend.h index c93401e99..d2cd01f3a 100644 --- a/src/grammar/rolls/sided_dice.h +++ b/src/grammar/rolls/dice_frontend.h @@ -1,14 +1,14 @@ - -#ifndef __ROLL_SIDED_DICE_H__ -#define __ROLL_SIDED_DICE_H__ - -#include "../util/vector_functions.h" -#include "dice_enums.h" -#include "vec.h" - -void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, - int start_offset); - -void roll_symbolic_dice(vec* x, vec* y, vec* result); - -#endif + +#ifndef __ROLL_SIDED_DICE_H__ +#define __ROLL_SIDED_DICE_H__ + +#include "../util/vector_functions.h" +#include "constructs/dice_enums.h" +#include "constructs/vec.h" + +void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, + int start_offset); + +void roll_symbolic_dice(vec* x, vec* y, vec* result); + +#endif diff --git a/src/grammar/shared_header.h b/src/grammar/shared_header.h index e6193717d..a2d095812 100644 --- a/src/grammar/shared_header.h +++ b/src/grammar/shared_header.h @@ -6,9 +6,13 @@ extern "C" { #endif -#include "rolls/sided_dice.h" +#include "rolls/dice_frontend.h" #include "util/vector_functions.h" + +#define MAX_SYMBOL_LENGTH 256 +#define MAX_ITERATION 20 + int roll_full_options( char* roll_request, char* log_file, diff --git a/src/grammar/util/array_functions.c b/src/grammar/util/array_functions.c new file mode 100644 index 000000000..4a1430e71 --- /dev/null +++ b/src/grammar/util/array_functions.c @@ -0,0 +1,12 @@ + +#include "util/array_functions.h" + +int collapse(int * arr, unsigned int len){ + return sum(arr, len); +} + +int sum(int * arr, unsigned int len){ + int result = 0; + for(unsigned int i = 0; i != len; i++) result += arr[i]; + return result; +} \ No newline at end of file diff --git a/src/grammar/util/array_functions.h b/src/grammar/util/array_functions.h new file mode 100644 index 000000000..c7d4c2cbd --- /dev/null +++ b/src/grammar/util/array_functions.h @@ -0,0 +1,8 @@ + +#ifndef ARRAY_FN_HEADER +#define ARRAY_FN_HEADER + +int sum(int* arr, unsigned int len); +int collapse(int* arr, unsigned int len); + +#endif \ No newline at end of file diff --git a/src/grammar/rolls/mocking.c b/src/grammar/util/mocking.c similarity index 94% rename from src/grammar/rolls/mocking.c rename to src/grammar/util/mocking.c index bef52a568..5103de908 100644 --- a/src/grammar/rolls/mocking.c +++ b/src/grammar/util/mocking.c @@ -1,57 +1,57 @@ -#include "rolls/mocking.h" - -int random_fn_run_count = 0; -int global_mock_value = 0; -int secondary_mock_value = 0; -MOCK_METHOD global_mock_style = NO_MOCK; - - -void reset_mocking() { - /** - * @brief Resets various globals for test mocking - */ - random_fn_run_count = 0; - global_mock_value = 0; - global_mock_style = NO_MOCK; -} -void init_mocking(MOCK_METHOD mock_style, int starting_value) { - /** - * @brief Initializes test mocking with given settings - * @param mock_style How to apply mocking - * @param starting_value Where mocking is applied, sets the value for the - * first roll on the system - */ - random_fn_run_count = 0; - global_mock_value = starting_value; - global_mock_style = mock_style; -} - -void mocking_tick() { - /** - * @brief Every time a dice is rolled, this function is called so that the - * mocking logic can update - */ - switch (global_mock_style) { - case RETURN_INCREMENTING: { - global_mock_value = global_mock_value + 1; - break; - } - case RETURN_DECREMENTING: { - global_mock_value = global_mock_value - 1; - break; - } - case RETURN_CONSTANT_TWICE_ELSE_CONSTANT_ONE: { - if (random_fn_run_count == 1) { - secondary_mock_value = global_mock_value; - } - if (random_fn_run_count < 2) { - global_mock_value = secondary_mock_value; - } else { - global_mock_value = 1; - } - break; - } - default: - break; - } -} +#include "util/mocking.h" + +int random_fn_run_count = 0; +int global_mock_value = 0; +int secondary_mock_value = 0; +MOCK_METHOD global_mock_style = NO_MOCK; + + +void reset_mocking() { + /** + * @brief Resets various globals for test mocking + */ + random_fn_run_count = 0; + global_mock_value = 0; + global_mock_style = NO_MOCK; +} +void init_mocking(MOCK_METHOD mock_style, int starting_value) { + /** + * @brief Initializes test mocking with given settings + * @param mock_style How to apply mocking + * @param starting_value Where mocking is applied, sets the value for the + * first roll on the system + */ + random_fn_run_count = 0; + global_mock_value = starting_value; + global_mock_style = mock_style; +} + +void mocking_tick() { + /** + * @brief Every time a dice is rolled, this function is called so that the + * mocking logic can update + */ + switch (global_mock_style) { + case RETURN_INCREMENTING: { + global_mock_value = global_mock_value + 1; + break; + } + case RETURN_DECREMENTING: { + global_mock_value = global_mock_value - 1; + break; + } + case RETURN_CONSTANT_TWICE_ELSE_CONSTANT_ONE: { + if (random_fn_run_count == 1) { + secondary_mock_value = global_mock_value; + } + if (random_fn_run_count < 2) { + global_mock_value = secondary_mock_value; + } else { + global_mock_value = 1; + } + break; + } + default: + break; + } +} diff --git a/src/grammar/rolls/mocking.h b/src/grammar/util/mocking.h similarity index 87% rename from src/grammar/rolls/mocking.h rename to src/grammar/util/mocking.h index 2075216e4..f9e80c79e 100644 --- a/src/grammar/rolls/mocking.h +++ b/src/grammar/util/mocking.h @@ -1,17 +1,17 @@ - -#include "rolls/dice_enums.h" -#include "shared_header.h" - - -typedef enum { - NO_MOCK = 0, - RETURN_CONSTANT = 1, - RETURN_INCREMENTING = 2, - RETURN_DECREMENTING = 3, - RETURN_CONSTANT_TWICE_ELSE_CONSTANT_ONE = 4 -} MOCK_METHOD; - -// Mocking Util -void reset_mocking(); -void init_mocking(MOCK_METHOD mock_style, int starting_value); -void mocking_tick(); + +#include "constructs/dice_enums.h" +#include "shared_header.h" + + +typedef enum { + NO_MOCK = 0, + RETURN_CONSTANT = 1, + RETURN_INCREMENTING = 2, + RETURN_DECREMENTING = 3, + RETURN_CONSTANT_TWICE_ELSE_CONSTANT_ONE = 4 +} MOCK_METHOD; + +// Mocking Util +void reset_mocking(); +void init_mocking(MOCK_METHOD mock_style, int starting_value); +void mocking_tick(); diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 54014695b..2aca1bbbe 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -106,10 +106,8 @@ void *safe_malloc(size_t size) { void free_vector(vec v){ if(v.dtype == NUMERIC){ - printf("vec: %p\n", (void*)v.content); free(v.content); }else{ - printf("vec: %p\n", (void*)v.symbols); free_2d_array(&v.symbols, v.length); if (v.has_source){ @@ -146,32 +144,23 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, * @param item * @param max_size */ - // printf("alloc: %u\n", items); - // printf("*alloc: %u\n", max_size); - - printf("dst: %p (%i)\n", (void *)*dst, *dst != 0); + // TODO: Maybe Re-Add? + // if(*dst != 0){ // free(*dst); // } - printf("Allocate New Base Pointer...\n"); *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; } - - printf("Allocate 2nd Dimension... (%u times)\n", items); for (unsigned int i = 0; i != items; i++) { - printf("Allocate 2nd Dimension...\n"); - printf("Old Address: %p\n", (void *)(*dst)); - printf("Old Address: %p\n", (void *)(*dst)[i]); (*dst)[i] = (char*)safe_calloc(sizeof(char), max_size); if (gnoll_errno) { return; } - printf("Copy from Src... %p %p (size: %u)\n", (*dst)[i], src[i], max_size); memcpy((*dst)[i], src[i], max_size); } diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 2939107ee..20884b88d 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -5,10 +5,10 @@ #include #include -#include "operations/condition_checking.h" +#include "operations/conditionals.h" #include "shared_header.h" #include "util/safe_functions.h" -#include "yacc_header.h" +#include "shared_header.h" extern int gnoll_errno; @@ -49,7 +49,6 @@ void initialize_vector(vec *vector, DIE_TYPE dt, unsigned int number_of_items) { vector->has_source = false; if (dt == NUMERIC) { - printf("Valid Already? %p\n", (void *)vector->content); vector->content = (int*)safe_calloc(number_of_items, sizeof(int)); if (gnoll_errno) return; } else if (dt == SYMBOLIC) { diff --git a/src/grammar/util/vector_functions.h b/src/grammar/util/vector_functions.h index 1f2ac5aac..187d19f61 100644 --- a/src/grammar/util/vector_functions.h +++ b/src/grammar/util/vector_functions.h @@ -1,8 +1,8 @@ #ifndef VECFN_HEADER #define VECFN_HEADER -#include "rolls/dice_enums.h" -#include "rolls/vec.h" +#include "constructs/dice_enums.h" +#include "constructs/vec.h" #include "shared_header.h" void initialize_vector(vec* vector, DIE_TYPE dt, unsigned int number_of_items); diff --git a/src/grammar/yacc_header.h b/src/grammar/yacc_header.h deleted file mode 100644 index 5d0d9e0c9..000000000 --- a/src/grammar/yacc_header.h +++ /dev/null @@ -1,16 +0,0 @@ - -#ifndef __YACC_HEADER__ -#define __YACC_HEADER__ - -#define MAX_SYMBOL_LENGTH 256 -#define MAX_ITERATION 20 - -int initialize(); -// int resolve_dice(dice die); - -int sum(int* arr, unsigned int len); -int collapse(int* arr, unsigned int len); -int roll_numeric_die(int small, int big); -int roll_symbolic_die(unsigned int length_of_symbolic_array); - -#endif From 7f595542ba54eb5eb0a7cbba28161ec5f9d009f5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:36:04 +0000 Subject: [PATCH 089/138] Update array_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/array_functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/util/array_functions.c b/src/grammar/util/array_functions.c index 4a1430e71..18399c5eb 100644 --- a/src/grammar/util/array_functions.c +++ b/src/grammar/util/array_functions.c @@ -9,4 +9,4 @@ int sum(int * arr, unsigned int len){ int result = 0; for(unsigned int i = 0; i != len; i++) result += arr[i]; return result; -} \ No newline at end of file +} From 651f875583ffc1b6a0c9bc3413f57ac8164f1270 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:37:25 +0000 Subject: [PATCH 090/138] Update array_functions.h Signed-off-by: Ian Hunter --- src/grammar/util/array_functions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/util/array_functions.h b/src/grammar/util/array_functions.h index c7d4c2cbd..f3d0ffa2c 100644 --- a/src/grammar/util/array_functions.h +++ b/src/grammar/util/array_functions.h @@ -5,4 +5,4 @@ int sum(int* arr, unsigned int len); int collapse(int* arr, unsigned int len); -#endif \ No newline at end of file +#endif From a4622a2e2ce8d8254e8af8f660d4306ce5930709 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:39:25 +0000 Subject: [PATCH 091/138] Update string_functions.h Signed-off-by: Ian Hunter --- src/grammar/util/string_functions.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/grammar/util/string_functions.h b/src/grammar/util/string_functions.h index a10510293..eec2610bb 100644 --- a/src/grammar/util/string_functions.h +++ b/src/grammar/util/string_functions.h @@ -1 +1,4 @@ -char * concat_strings(char ** s, unsigned int num_s); \ No newline at end of file +#ifndef __STR_FNS_H__ +#def __STR_FNS_H__ +char * concat_strings(char ** s, unsigned int num_s); +#endif From f52caf3e90cf157cc9d22250214943fbe00c5541 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:39:34 +0000 Subject: [PATCH 092/138] Update string_functions.c Signed-off-by: Ian Hunter --- src/grammar/util/string_functions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/util/string_functions.c b/src/grammar/util/string_functions.c index aa91f0f52..dc450cae5 100644 --- a/src/grammar/util/string_functions.c +++ b/src/grammar/util/string_functions.c @@ -38,4 +38,4 @@ char * concat_strings(char ** s, unsigned int num_s){ } } return result; -} \ No newline at end of file +} From 41a1516d05108acad71ebefcf86a7d93798b3004 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:43:03 +0000 Subject: [PATCH 093/138] Update string_functions.h Signed-off-by: Ian Hunter --- src/grammar/util/string_functions.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/util/string_functions.h b/src/grammar/util/string_functions.h index eec2610bb..9d88c00e9 100644 --- a/src/grammar/util/string_functions.h +++ b/src/grammar/util/string_functions.h @@ -1,4 +1,4 @@ #ifndef __STR_FNS_H__ -#def __STR_FNS_H__ +#define __STR_FNS_H__ char * concat_strings(char ** s, unsigned int num_s); #endif From 16912918361ab371ebaca66416499c8b51457187 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:51:11 +0000 Subject: [PATCH 094/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index dde826e76..6e9c3a736 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -754,9 +754,8 @@ die_roll: die_symbol NUMBER EXPLOSION ONCE { - vec numA = $1; - vec ds = $2; - vec numB = $3; + vec ds = $1; + vec numB = $2; int start_from = ds.content[0]; @@ -766,8 +765,8 @@ die_roll: roll_plain_sided_dice( &number_of_dice, - &ds, &numB, + &$$, ONLY_ONCE_EXPLOSION, start_from ); From a25b3f17acc584a454dff1108928636a24158f8b Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:53:39 +0000 Subject: [PATCH 095/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 6e9c3a736..3ea9055b9 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -770,7 +770,7 @@ die_roll: ONLY_ONCE_EXPLOSION, start_from ); - free_vector(numA); + free_vector(number_of_dice); free_vector(ds); free_vector(numB); } From e5fec7ed3cef5f20e2550c28ba3954dae2692e33 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 12:55:35 +0000 Subject: [PATCH 096/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 3ea9055b9..687943612 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -872,16 +872,13 @@ die_roll: | NUMBER die_symbol NUMBER { + vec numA = $1; vec ds = $2; vec numB = $3; int start_from = ds.content[0]; - vec number_of_dice; - initialize_vector(&number_of_dice, NUMERIC, 1); - number_of_dice.content[0] = 1; - roll_plain_sided_dice( - &number_of_dice, + &numA, &numB, &$$, NO_EXPLOSION, @@ -889,7 +886,7 @@ die_roll: ); free_vector(numB); free_vector(ds); - free_vector(number_of_dice); + free_vector(numA); } | die_symbol NUMBER From dfd6920e2f599992b9e79cbdfaed29f3ed3b58c7 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 14:44:08 +0000 Subject: [PATCH 097/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 687943612..5d7d95db8 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1270,14 +1270,19 @@ function: } | FN_ABS LBRACE function RBRACE{ - // abs(__) + /** @brief performs the abs(__) function + * @FN_ABS the symbol "abs" + * @LBRACE the symbol "(" + * function The target vector + * @RBRACE the symbol ")" + */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = ABSV( $3.content[0] ); $$ = new_vec; - free($3.content); + free_vector($3); } | /* FN_POOL LBRACE dice_statement SYMBOL_SEPERATOR dice_statement RBRACE{ From e02cc0e44794dc2b8d115092bd51fe4c06d9ecef Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 16:22:38 +0000 Subject: [PATCH 098/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 57 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 5d7d95db8..946a657e3 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1162,10 +1162,14 @@ custom_symbol_dice: ; csd: csd SYMBOL_SEPERATOR csd{ - vec l; - vec r; - l = $1; - r = $3; + /** + * csd a vector containing custom symbols + * SYMBOL_SEPERATOR the symbol ',' + * csd a vector containing custom symbols + * return A vector with all the symbols + */ + vec l = $1; + vec r = $3; vec new_vector; initialize_vector(&new_vector, SYMBOLIC, l.length + r.length); @@ -1181,6 +1185,12 @@ csd: } | NUMBER RANGE NUMBER{ + /** + * NUMBER The symbol 0-9+ + * RANGE The symbol '..' + * NUMBER The symbol 0-9+ + * return A vector containing the numeric values as symbols + */ vec start = $1; vec end = $3; @@ -1213,8 +1223,12 @@ csd: CAPITAL_STRING | NUMBER{ + /** + * NUMBER The symbol 0-9+ + * return A vector containing the numeric values as symbols + */ vec in = $1; - // Max/Min int has 10 characters + // INT_MAX/INT_MIN has 10 characters in.symbols = safe_calloc(1, sizeof(char *)); in.symbols[0] = safe_calloc(10, sizeof(char)); sprintf(in.symbols[0], "%d", in.content[0]); @@ -1227,6 +1241,10 @@ condition: EQ | LT | GT | LE | GE | NE ; die_symbol: SIDED_DIE{ + /** + * SIDED_DIE The symbol 'd' + * return A vector containing '1', the start index + */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = 1; @@ -1234,6 +1252,10 @@ die_symbol: } | SIDED_DIE_ZERO{ + /** + * SIDED_DIE The symbol 'z' + * return A vector containing '0', the start index + */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = 0; @@ -1243,7 +1265,15 @@ die_symbol: function: FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ - // max(a,b) + /** @brief performs the min(__, __) function + * @FN_MAX the symbol "max" + * @LBRACE the symbol "(" + * function The target vector + * SYMBOL_SEPERATOR the symbol "," + * function The target vector + * @RBRACE the symbol ")" + * return vector + */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); int vmax = MAXV( @@ -1257,7 +1287,15 @@ function: } | FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ - // min(a,b) + /** @brief performs the min(__, __) function + * @FN_MIN the symbol "min" + * @LBRACE the symbol "(" + * function The target vector + * SYMBOL_SEPERATOR the symbol "," + * function The target vector + * @RBRACE the symbol ")" + * return vector + */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = MINV( @@ -1265,8 +1303,8 @@ function: $5.content[0] ); $$ = new_vec; - free($3.content); - free($5.content); + free_vector($3); + free_vector($5); } | FN_ABS LBRACE function RBRACE{ @@ -1275,6 +1313,7 @@ function: * @LBRACE the symbol "(" * function The target vector * @RBRACE the symbol ")" + * return vector */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); From 999985f52effaf1bf31b3b4b95a4aff909a4de8e Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 16:29:04 +0000 Subject: [PATCH 099/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 946a657e3..a7578cf93 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1105,8 +1105,12 @@ custom_symbol_dice: } | MACRO_ACCESSOR CAPITAL_STRING{ - vec vector; - vector = $2; + /** + * MACRO_ACCESSOR the symbol '@' + * CAPITAL_STRING A vector containing a macro identifier + * return A vector containing rollparams for the selected macro + */ + vec vector = $2; char * name = vector.symbols[0]; vec new_vector; @@ -1115,19 +1119,21 @@ custom_symbol_dice: // Resolve Roll vec number_of_dice; + vec die_sides; + + // Set Num Dice initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = (int)new_vector.source.number_of_dice; - - vec die_sides; - // TODO: Extract to function. + + // Set Die Sides light_initialize_vector(&die_sides, NUMERIC, 1); die_sides.content[0] = (int)new_vector.source.die_sides; die_sides.length = new_vector.source.die_sides; die_sides.symbols = NULL; + // Roll according to the stored values + // Careful: Newvector used already if (new_vector.source.dtype == NUMERIC){ - // Careful, Newvector used already - initialize_vector(&new_vector, new_vector.source.dtype, 1); roll_plain_sided_dice( &number_of_dice, @@ -1145,9 +1151,7 @@ custom_symbol_dice: MAX_SYMBOL_LENGTH ); - // Careful, Newvector used already initialize_vector(&new_vector, new_vector.source.dtype, 1); - roll_symbolic_dice( &number_of_dice, &die_sides, @@ -1157,6 +1161,9 @@ custom_symbol_dice: printf("Complex Dice Equation. Only dice definitions supported. No operations\n"); gnoll_errno = NOT_IMPLEMENTED; } + free_vector(vector); + free_vector(number_of_dice); + free_vector(die_sides); $$ = new_vector; } ; From 2f0d3ad0028ddea29b1ef7f9313ae48a366c90e5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 16:34:30 +0000 Subject: [PATCH 100/138] Update and rename manual_valgrind.yml to manual_leakcheck.yml Signed-off-by: Ian Hunter --- .../{manual_valgrind.yml => manual_leakcheck.yml} | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename .github/workflows/{manual_valgrind.yml => manual_leakcheck.yml} (71%) diff --git a/.github/workflows/manual_valgrind.yml b/.github/workflows/manual_leakcheck.yml similarity index 71% rename from .github/workflows/manual_valgrind.yml rename to .github/workflows/manual_leakcheck.yml index 92fe86fe4..4efa60a97 100644 --- a/.github/workflows/manual_valgrind.yml +++ b/.github/workflows/manual_leakcheck.yml @@ -18,7 +18,12 @@ jobs: - name: Lib Install run: | sudo apt-get install valgrind - make all DEBUG=1 + - name: Run Asan + run: | + make all DEBUG=2 + ./build/dice d20 - name: Run Valgrind - run: valgrind --leak-check=full ./build/dice d20 + run: | + make all DEBUG=1 + valgrind --leak-check=full ./build/dice d20 From 7fbb20df9873112d0dccc473698d9f03996e85e8 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 16:37:48 +0000 Subject: [PATCH 101/138] Update Makefile Signed-off-by: Ian Hunter --- Makefile | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6c6928daa..2eb5e82c0 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ OPT=-O3 \ -Wmissing-declarations -Wmissing-include-dirs \ -Wredundant-decls -Wshadow -Wsign-conversion \ -Wundef -Wno-unused -Wformat=2 -# $(ADDRESS_SANITIZER) + # -ffast-math # Problematic for Python @@ -31,12 +31,21 @@ OPT=-O3 \ # -Wlogical-op # === DEBUG OPTIONS ==== + DEBUG=0 ifeq ($(DEBUG), 1) -OPT=-O0 -g -gdwarf-4 $(ADDRESS_SANITIZER) # Valgrind info +# Valgrind +OPT=-O0 -g -gdwarf-4 PARSER_DEBUG:=--debug --verbose else +ifeq ($(DEBUG), 2) +# ASAN +OPT=-O0 $(ADDRESS_SANITIZER) PARSER_DEBUG:= +else +# USUAL +PARSER_DEBUG:= +endif endif USE_SECURE_RANDOM=0 From ce7989e952e02707f10acad6976b775919d27b29 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 20:42:09 +0000 Subject: [PATCH 102/138] fix a leak --- src/grammar/dice.yacc | 161 +++++++++++++++++++++++------------------- 1 file changed, 89 insertions(+), 72 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a7578cf93..61ae134e4 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -128,6 +128,10 @@ sub_statement: macro_statement: MACRO_STORAGE CAPITAL_STRING ASSIGNMENT math{ + /** + * + * returns - 0 + */ vec key = $2; vec value = $4; @@ -142,10 +146,15 @@ macro_statement: YYABORT; yyclearin; } + // &$$ = NULL; } ; dice_statement: functions{ + /** + * functions a vector + * return NULL + */ vec vector = $1; vec new_vec = vector; // Code Smell. @@ -199,12 +208,83 @@ dice_statement: functions{ if(write_to_file){ fclose(fp); } + // free_vector(new_vec); + free_vector(vector); + $$ = new_vec; }; functions: function ; +function: + FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ + /** @brief performs the min(__, __) function + * @FN_MAX the symbol "max" + * @LBRACE the symbol "(" + * function The target vector + * SYMBOL_SEPERATOR the symbol "," + * function The target vector + * @RBRACE the symbol ")" + * return vector + */ + vec new_vec; + initialize_vector(&new_vec, NUMERIC, 1); + int vmax = MAXV( + $3.content[0], + $5.content[0] + ); + new_vec.content[0] = vmax; + $$ = new_vec; + free_vector($3); + free_vector($5); + } + | + FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ + /** @brief performs the min(__, __) function + * @FN_MIN the symbol "min" + * @LBRACE the symbol "(" + * function The target vector + * SYMBOL_SEPERATOR the symbol "," + * function The target vector + * @RBRACE the symbol ")" + * return vector + */ + vec new_vec; + initialize_vector(&new_vec, NUMERIC, 1); + new_vec.content[0] = MINV( + $3.content[0], + $5.content[0] + ); + $$ = new_vec; + free_vector($3); + free_vector($5); + } + | + FN_ABS LBRACE function RBRACE{ + /** @brief performs the abs(__) function + * @FN_ABS the symbol "abs" + * @LBRACE the symbol "(" + * function The target vector + * @RBRACE the symbol ")" + * return vector + */ + vec new_vec; + initialize_vector(&new_vec, NUMERIC, 1); + new_vec.content[0] = ABSV( + $3.content[0] + ); + $$ = new_vec; + free_vector($3); + } + | + /* FN_POOL LBRACE dice_statement SYMBOL_SEPERATOR dice_statement RBRACE{ + make_pool($2, $4); + } */ + | + math +; + math: LBRACE math RBRACE{ $$ = $2; @@ -477,9 +557,12 @@ collapsing_dice_operations: } | dice_operations{ + /** + * dice_operations a vector + * returns a vector + */ - vec vector; - vector = $1; + vec vector = $1; if (vector.dtype == SYMBOLIC){ // Symbolic, Impossible to collapse @@ -496,7 +579,6 @@ collapsing_dice_operations: }else{ $$ = vector; } - } } ; @@ -1239,6 +1321,8 @@ csd: in.symbols = safe_calloc(1, sizeof(char *)); in.symbols[0] = safe_calloc(10, sizeof(char)); sprintf(in.symbols[0], "%d", in.content[0]); + free(in.content); + in.dtype = SYMBOLIC; $$ = in; } ; @@ -1249,8 +1333,8 @@ condition: EQ | LT | GT | LE | GE | NE ; die_symbol: SIDED_DIE{ /** - * SIDED_DIE The symbol 'd' - * return A vector containing '1', the start index + * @brief SIDED_DIE The symbol 'd' + * @param return A vector containing '1', the start index */ vec new_vec; initialize_vector(&new_vec, NUMERIC, 1); @@ -1270,73 +1354,6 @@ die_symbol: } ; -function: - FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ - /** @brief performs the min(__, __) function - * @FN_MAX the symbol "max" - * @LBRACE the symbol "(" - * function The target vector - * SYMBOL_SEPERATOR the symbol "," - * function The target vector - * @RBRACE the symbol ")" - * return vector - */ - vec new_vec; - initialize_vector(&new_vec, NUMERIC, 1); - int vmax = MAXV( - $3.content[0], - $5.content[0] - ); - new_vec.content[0] = vmax; - $$ = new_vec; - free_vector($3); - free_vector($5); - } - | - FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ - /** @brief performs the min(__, __) function - * @FN_MIN the symbol "min" - * @LBRACE the symbol "(" - * function The target vector - * SYMBOL_SEPERATOR the symbol "," - * function The target vector - * @RBRACE the symbol ")" - * return vector - */ - vec new_vec; - initialize_vector(&new_vec, NUMERIC, 1); - new_vec.content[0] = MINV( - $3.content[0], - $5.content[0] - ); - $$ = new_vec; - free_vector($3); - free_vector($5); - } - | - FN_ABS LBRACE function RBRACE{ - /** @brief performs the abs(__) function - * @FN_ABS the symbol "abs" - * @LBRACE the symbol "(" - * function The target vector - * @RBRACE the symbol ")" - * return vector - */ - vec new_vec; - initialize_vector(&new_vec, NUMERIC, 1); - new_vec.content[0] = ABSV( - $3.content[0] - ); - $$ = new_vec; - free_vector($3); - } - | - /* FN_POOL LBRACE dice_statement SYMBOL_SEPERATOR dice_statement RBRACE{ - make_pool($2, $4); - } */ - | - math -; %% From 50fea77edf60fad7c35c7ac9a5cbf71ff5b93030 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 21:54:18 +0000 Subject: [PATCH 103/138] has_source defaulted to 0 (undefined), which is true. mistakenly freeing --- Makefile | 7 +++++++ src/grammar/dice.lex | 14 ++++++++++++++ src/grammar/dice.yacc | 32 +++++++++++++++++++++++++++---- src/grammar/operations/macros.c | 9 +++++++-- src/grammar/rolls/dice_frontend.c | 8 ++++++-- src/grammar/util/safe_functions.c | 4 ++-- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 2eb5e82c0..353cbda45 100644 --- a/Makefile +++ b/Makefile @@ -43,10 +43,17 @@ ifeq ($(DEBUG), 2) OPT=-O0 $(ADDRESS_SANITIZER) PARSER_DEBUG:= else +ifeq ($(DEBUG), 3) +# ASAN +OPT=-O0 -lefence -g +PARSER_DEBUG:= +else # USUAL PARSER_DEBUG:= endif endif +endif + USE_SECURE_RANDOM=0 ifeq ($(USE_SECURE_RANDOM), 1) diff --git a/src/grammar/dice.lex b/src/grammar/dice.lex index b2bc84023..fb1ce6dc2 100644 --- a/src/grammar/dice.lex +++ b/src/grammar/dice.lex @@ -31,6 +31,7 @@ vector.dtype = SYMBOLIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return CAPITAL_STRING; @@ -43,6 +44,7 @@ vector.content[0] = fast_atoi(yytext); + vector.has_source = false; vector.dtype = NUMERIC; vector.length = 1; yylval.values = vector; @@ -80,6 +82,7 @@ d { vector.symbols[4] = zero; vector.symbols[5] = minus; vector.length = 6; + vector.has_source = false; yylval.values = vector; return(FATE_DIE); @@ -100,6 +103,7 @@ d { vector.symbols[0] = plus; vector.symbols[1] = minus; vector.length = 2; + vector.has_source = false; yylval.values = vector; return(FATE_DIE); @@ -123,6 +127,7 @@ d { vector.symbols[0] = plus; vector.symbols[1] = zero; vector.symbols[2] = minus; + vector.has_source = false; vector.length = 3; yylval.values = vector; @@ -197,6 +202,7 @@ u { vector.content[0] = IS_UNIQUE; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return (UNIQUE); } @@ -223,6 +229,7 @@ o { vector.content[0] = NOT_EQUAL; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return NE; } @@ -233,6 +240,7 @@ o { vector.content[0] = EQUALS; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return EQ; } @@ -243,6 +251,7 @@ o { vector.content[0] = LESS_THAN; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return LT; } @@ -252,6 +261,7 @@ o { if(gnoll_errno){yyerror("Memory Err");} vector.content[0] = GREATER_THAN; vector.dtype = NUMERIC; + vector.has_source = false; vector.length = 1; yylval.values = vector; return GT; @@ -263,6 +273,7 @@ o { vector.content[0] = LESS_OR_EQUALS; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return(LE); } @@ -273,6 +284,7 @@ o { vector.content[0] = GREATER_OR_EQUALS; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return(GE); } @@ -283,6 +295,7 @@ is_even { vector.content[0] = IF_EVEN; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return(IS_EVEN); } @@ -293,6 +306,7 @@ is_odd { vector.content[0] = IF_ODD; vector.dtype = NUMERIC; vector.length = 1; + vector.has_source = false; yylval.values = vector; return(IS_ODD); } diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 61ae134e4..5611a7888 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -547,12 +547,17 @@ math: collapsing_dice_operations: dice_operations DO_COUNT{ + /** + * dice_operations - a vector + * DO_COUNT - a symbol 'c' + */ vec new_vec; vec dice = $1; initialize_vector(&new_vec, NUMERIC, 1); new_vec.content[0] = (int)dice.length; + free_vector(dice); $$ = new_vec; } | @@ -1104,8 +1109,16 @@ die_roll: custom_symbol_dice: NUMBER die_symbol SYMBOL_LBRACE csd SYMBOL_RBRACE { + /** + * NUMBER - vector + * die_symbol - vector + * SYMBOL_LBRACE - the symbol { + * csd - vector + * SYMBOL_RBRACE - the symbol } + */ // Nd{SYMB} vec left = $1; + vec dsymb = $2; vec right = $4; // TODO: Multiple ranges @@ -1121,15 +1134,22 @@ custom_symbol_dice: free_vector(left); free_vector(right); + free_vector(dsymb); $$ = result_vec; } | die_symbol SYMBOL_LBRACE csd SYMBOL_RBRACE { - // d{SYM} + /** @brief + * @param die_symbol a vector + * @param SYMBOL_LBRACE the symbol "{" + * @param csd a vector + * @param SYMBOL_LBRACE the symbol "}" + * returns a vector + */ vec csd_vec = $3; - vec result_vec; vec number_of_dice; + vec result_vec; initialize_vector(&number_of_dice, NUMERIC, 1); number_of_dice.content[0] = 1; @@ -1153,7 +1173,8 @@ custom_symbol_dice: NO_EXPLOSION, start_value ); - + free_vector(dice_sides); + free_vector(num_dice); }else{ initialize_vector(&result_vec, SYMBOLIC, 1); @@ -1169,6 +1190,8 @@ custom_symbol_dice: memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); } result_vec.source = rp; + printf("--%p\n", (void *)&result_vec.source.symbol_pool); + printf("--%p\n", (void *)&result_vec.source.symbol_pool[0]); result_vec.has_source = true; // Custom Symbol @@ -1183,6 +1206,7 @@ custom_symbol_dice: free_vector(number_of_dice); free_vector(csd_vec); + free_vector($1); $$ = result_vec; } | @@ -1517,7 +1541,7 @@ int main(int argc, char **str){ roll_full_options( s, "output.dice", - 0, // Verbose + 1, // Verbose 1, // Introspect 0, // Mocking 1, // Builtins diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index c7e025ac5..e29306b04 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -115,13 +115,18 @@ void search_macros(char *skey, roll_params *to_store) { *to_store = s->stored_dice_roll; to_store->symbol_pool = NULL; + // free_2d_array(&to_store->symbol_pool, to_store->die_sides); unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; if (is_symbolic) { free_2d_array(&to_store->symbol_pool, to_store->die_sides); safe_copy_2d_chararray_with_allocation( - &to_store->symbol_pool, s->stored_dice_roll.symbol_pool, - to_store->die_sides, MAX_SYMBOL_LENGTH); + &to_store->symbol_pool, + s->stored_dice_roll.symbol_pool, + to_store->die_sides, + MAX_SYMBOL_LENGTH + ); } + free(s); } diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 9ed044afd..44a012674 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -62,8 +62,12 @@ void roll_symbolic_dice(vec* x, vec* y, vec* result) { // Copy over memory to Symbol Pool for reloading free_2d_array(&rp.symbol_pool, rp.die_sides); - safe_copy_2d_chararray_with_allocation(&rp.symbol_pool, y->symbols, y->length, - MAX_SYMBOL_LENGTH); + safe_copy_2d_chararray_with_allocation( + &rp.symbol_pool, + y->symbols, + y->length, + MAX_SYMBOL_LENGTH + ); rp.start_value = 0; // First index of array int* indexes = do_roll(rp); diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 2aca1bbbe..93116c6ae 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -109,7 +109,6 @@ void free_vector(vec v){ free(v.content); }else{ free_2d_array(&v.symbols, v.length); - if (v.has_source){ // Should be always the same as length (But not sure that's true!) free_2d_array(&v.source.symbol_pool, v.length); @@ -124,8 +123,9 @@ void free_2d_array(char ***arr, unsigned int items) { * @param items */ if (*arr) { + printf("Arr: %p\n", (void *)arr); for (unsigned int i = 0; i != items; i++) { - // printf("[%u] Try to free: %p\n",i, (*arr)[i]); + printf("[%u] Try to free: %p\n",i, (*arr)[i]); if ((*arr)[i]) { free((*arr)[i]); } From db8f7a194df0937020d7ccda5cd63e371339c2c3 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 21:57:15 +0000 Subject: [PATCH 104/138] fix clang --- src/grammar/util/safe_functions.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 93116c6ae..b6d3d707d 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -123,9 +123,9 @@ void free_2d_array(char ***arr, unsigned int items) { * @param items */ if (*arr) { - printf("Arr: %p\n", (void *)arr); + // printf("Arr: %p\n", (void *)arr); for (unsigned int i = 0; i != items; i++) { - printf("[%u] Try to free: %p\n",i, (*arr)[i]); + // printf("[%u] Try to free: %p\n",i, (*arr)[i]); if ((*arr)[i]) { free((*arr)[i]); } From 638219631568f821d6e1adead3658d576666566d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 22:11:27 +0000 Subject: [PATCH 105/138] emcc errs --- Makefile | 13 +++++++++---- src/grammar/dice.yacc | 4 ---- src/grammar/util/string_functions.c | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 353cbda45..a4f3de903 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,6 @@ else endif -ADDRESS_SANITIZER= -fsanitize=address -fno-omit-frame-pointer -static-libasan -g OPT=-O3 \ $(STANDARD) -Wall -Wextra -Werror -pedantic -Wcast-align \ @@ -32,20 +31,26 @@ OPT=-O3 \ # === DEBUG OPTIONS ==== +ASAN_FLAGS= -fsanitize=address \ + -fsanitize-recover=address \ + -fsanitize-address-use-after-scope \ + -fno-omit-frame-pointer -static-libasan -g +GDB_FLAGS= -g -gdwarf-5 + DEBUG=0 ifeq ($(DEBUG), 1) # Valgrind -OPT=-O0 -g -gdwarf-4 +OPT=-O0 $(GDB_FLAGS) PARSER_DEBUG:=--debug --verbose else ifeq ($(DEBUG), 2) # ASAN -OPT=-O0 $(ADDRESS_SANITIZER) +OPT=-O0 $(ASAN_FLAGS) PARSER_DEBUG:= else ifeq ($(DEBUG), 3) # ASAN -OPT=-O0 -lefence -g +OPT=-O0 $(GDB_FLAGS) -lefence PARSER_DEBUG:= else # USUAL diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 5611a7888..eab4343b1 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1190,8 +1190,6 @@ custom_symbol_dice: memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); } result_vec.source = rp; - printf("--%p\n", (void *)&result_vec.source.symbol_pool); - printf("--%p\n", (void *)&result_vec.source.symbol_pool[0]); result_vec.has_source = true; // Custom Symbol @@ -1202,8 +1200,6 @@ custom_symbol_dice: ); } - // printf("CSD IS SYMBOLIC? %i\n", (int)csd_vec.dtype); - free_vector(number_of_dice); free_vector(csd_vec); free_vector($1); diff --git a/src/grammar/util/string_functions.c b/src/grammar/util/string_functions.c index dc450cae5..1a74e1e15 100644 --- a/src/grammar/util/string_functions.c +++ b/src/grammar/util/string_functions.c @@ -16,8 +16,8 @@ char * concat_strings(char ** s, unsigned int num_s){ return s[0]; } unsigned int size_total = 0; - int spaces = 0; - for(int i = 1; i != num_s + 1; i++){ + unsigned int spaces = 0; + for(unsigned int i = 1; i != num_s + 1; i++){ size_total += strlen(s[i]) + 1; } if (num_s > 1){ @@ -30,7 +30,7 @@ char * concat_strings(char ** s, unsigned int num_s){ char * result = (char *)safe_calloc((size_total+1), sizeof(char)); if(gnoll_errno){return NULL;} - for(int i = 1; i != num_s + 1; i++){ + for(unsigned int i = 1; i != num_s + 1; i++){ // printf() strcat(result, s[i]); if (spaces && i < num_s){ From 60ece23d78e881d36ae8bb267094708561b227b7 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 22:18:34 +0000 Subject: [PATCH 106/138] dunno why the test was passing before! --- src/grammar/dice.yacc | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index eab4343b1..d859f1800 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -150,7 +150,7 @@ macro_statement: } ; -dice_statement: functions{ +dice_statement: math{ /** * functions a vector * return NULL @@ -213,12 +213,9 @@ dice_statement: functions{ $$ = new_vec; }; -functions: - function -; -function: - FN_MAX LBRACE function SYMBOL_SEPERATOR function RBRACE{ +math: + FN_MAX LBRACE math SYMBOL_SEPERATOR math RBRACE{ /** @brief performs the min(__, __) function * @FN_MAX the symbol "max" * @LBRACE the symbol "(" @@ -240,7 +237,7 @@ function: free_vector($5); } | - FN_MIN LBRACE function SYMBOL_SEPERATOR function RBRACE{ + FN_MIN LBRACE math SYMBOL_SEPERATOR math RBRACE{ /** @brief performs the min(__, __) function * @FN_MIN the symbol "min" * @LBRACE the symbol "(" @@ -261,7 +258,7 @@ function: free_vector($5); } | - FN_ABS LBRACE function RBRACE{ + FN_ABS LBRACE math RBRACE{ /** @brief performs the abs(__) function * @FN_ABS the symbol "abs" * @LBRACE the symbol "(" @@ -278,14 +275,6 @@ function: free_vector($3); } | - /* FN_POOL LBRACE dice_statement SYMBOL_SEPERATOR dice_statement RBRACE{ - make_pool($2, $4); - } */ - | - math -; - -math: LBRACE math RBRACE{ $$ = $2; } From b60ac5ad9c896ed59ce218243b6ab895540b2a4d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 22:35:17 +0000 Subject: [PATCH 107/138] free for math --- src/grammar/dice.yacc | 70 ++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index d859f1800..a0335ddb1 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -281,10 +281,8 @@ math: | math MULT math{ // Collapse both sides and subtract - vec vector1; - vec vector2; - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if (vector1.dtype == SYMBOLIC || vector2.dtype == SYMBOLIC){ printf("Multiplication not implemented for symbolic dice.\n"); @@ -303,14 +301,15 @@ math: $$ = new_vec; } + + free_vector(vector1); + free_vector(vector2); } | math DIVIDE_ROUND_UP math{ // Collapse both sides and subtract - vec vector1; - vec vector2; - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if (vector1.dtype == SYMBOLIC || vector2.dtype == SYMBOLIC){ printf("Division unsupported for symbolic dice.\n"); @@ -336,14 +335,15 @@ math: $$ = new_vec; } + + free_vector(vector1); + free_vector(vector2); } | math DIVIDE_ROUND_DOWN math{ // Collapse both sides and subtract - vec vector1; - vec vector2; - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if (vector1.dtype == SYMBOLIC || vector2.dtype == SYMBOLIC){ printf("Division unsupported for symbolic dice.\n"); @@ -371,15 +371,15 @@ math: $$ = new_vec; } + + free_vector(vector1); + free_vector(vector2); } | math MODULO math{ // Collapse both sides and subtract - vec vector1; - vec vector2; - - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if (vector1.dtype == SYMBOLIC || vector2.dtype == SYMBOLIC){ printf("Modulo unsupported for symbolic dice.\n"); @@ -403,14 +403,20 @@ math: $$ = new_vec; } + + free_vector(vector1); + free_vector(vector2); } | math PLUS math{ + /** @brief + * math vector + * PLUS symbol "+" + * math vector + */ // Collapse both sides and subtract - vec vector1; - vec vector2; - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if ( (vector1.dtype == SYMBOLIC && vector2.dtype == NUMERIC) || @@ -443,11 +449,7 @@ math: vector2.symbols, vector2.length, new_vec.symbols ); - // free(vector1.symbols); - // free(vector2.symbols); - $$ = new_vec; - }else{ int v1 = collapse(vector1.content, vector1.length); int v2 = collapse(vector2.content, vector2.length); @@ -464,14 +466,14 @@ math: $$ = new_vec; } + free_vector(vector1); + free_vector(vector2); } | math MINUS math{ - vec vector1; - vec vector2; - vector1 = $1; - vector2 = $3; + vec vector1 = $1; + vec vector2 = $3; if ( (vector1.dtype == SYMBOLIC || vector2.dtype == SYMBOLIC) ){ @@ -499,13 +501,18 @@ math: $$ = new_vec; } + free_vector(vector1); + free_vector(vector2); } | MINUS math %prec UMINUS{ + /** + * MINUS a symbol '-' + * math a vector + */ // Eltwise Negation - vec vector; - vector = $2; + vec vector = $2; if (vector.dtype == SYMBOLIC){ printf("Symbolic Dice, Cannot negate. Consider using Numeric dice or post-processing.\n"); @@ -529,6 +536,7 @@ math: $$ = new_vec; } + free_vector(vector); } | collapsing_dice_operations From 5678214937836d9717bfe50b135df1cf3c58b82e Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 22:45:15 +0000 Subject: [PATCH 108/138] unfree type --- src/grammar/operations/macros.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index e29306b04..fdcd63b25 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -128,5 +128,10 @@ void search_macros(char *skey, roll_params *to_store) { MAX_SYMBOL_LENGTH ); } + /* DO NOT UNCOMMENT. + We need to keep 's' + Otherwise macros are lost after usage + free(s); + */ } From edeff30918d278b57b04c404d6016d8bc605cd05 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 23:27:57 +0000 Subject: [PATCH 109/138] add a step to cleanup final symbols in bison/yacc --- src/grammar/dice.yacc | 51 ++++++++++++++++++++--------- src/grammar/util/vector_functions.c | 8 +++-- src/python/code/gnoll/parser.py | 14 ++++---- tests/python/test_macros.py | 7 ++-- 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a0335ddb1..2e80639c1 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -69,7 +69,8 @@ int initialize(){ %} -%start gnoll_statement +%start gnoll_entry +/* %start gnoll_statement */ %token NUMBER SIDED_DIE FATE_DIE REPEAT SIDED_DIE_ZERO %token EXPLOSION IMPLOSION PENETRATE ONCE @@ -102,6 +103,12 @@ int initialize(){ %% /* Rules Section */ +gnoll_entry: + gnoll_statement{ + // printf("A: %i\n", new_vector->content[0]); + free_vector($1); + } +; gnoll_statement: gnoll_statement STATEMENT_SEPERATOR gnoll_statement @@ -157,8 +164,9 @@ dice_statement: math{ */ vec vector = $1; - vec new_vec = vector; // Code Smell. - // Target Vector should be empty + vec new_vec; + // vec new_vec = vector; // Code Smell. + // // Target Vector should be empty // Step 1: Collapse pool to a single value if nessicary collapse_vector(&vector, &new_vec); @@ -811,8 +819,7 @@ dice_operations: ; die_roll: - NUMBER die_symbol NUMBER EXPLOSION ONCE - { + NUMBER die_symbol NUMBER EXPLOSION ONCE{ vec numA = $1; vec ds = $2; vec numB = $3; @@ -835,8 +842,7 @@ die_roll: free_vector(numB); } | - die_symbol NUMBER EXPLOSION ONCE - { + die_symbol NUMBER EXPLOSION ONCE{ vec ds = $1; vec numB = $2; @@ -859,8 +865,7 @@ die_roll: free_vector(numB); } | - NUMBER die_symbol NUMBER EXPLOSION PENETRATE - { + NUMBER die_symbol NUMBER EXPLOSION PENETRATE{ vec numA = $1; vec ds = $2; @@ -1182,12 +1187,24 @@ custom_symbol_dice: .start_value=0, .symbol_pool=(char **)safe_calloc(csd_vec.length , sizeof(char *)) }; - for(unsigned int i = 0; i != csd_vec.length; i++){ - rp.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); - memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); - } result_vec.source = rp; result_vec.has_source = true; + for(unsigned int i = 0; i != csd_vec.length; i++){ + result_vec.source.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); + memcpy( + result_vec.source.symbol_pool[i], + csd_vec.symbols[i], + MAX_SYMBOL_LENGTH*sizeof(char) + ); + } + printf("custom_symbol_dice\n"); + + // for(unsigned int i = 0; i != csd_vec.length; i++){ + // rp.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); + // memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); + // } + // result_vec.source = rp; + // result_vec.has_source = true; // Custom Symbol roll_symbolic_dice( @@ -1427,6 +1444,8 @@ int roll_full_options( initialize(); + enable_builtins = 0; + if(enable_builtins){ load_builtins("builtins/"); } @@ -1492,9 +1511,9 @@ void load_builtins(char* root){ free(path); free(stored_str); } - /* if(count >= 1){ + if(count >= 1){ break; - } */ + } tinydir_next(&dir); } @@ -1551,6 +1570,8 @@ int main(int argc, char **str){ } fclose(f); } + // Final Freeing + free(macros); } int yyerror(s) diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 20884b88d..5c578e011 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -213,8 +213,8 @@ void collapse_vector(vec *vector, vec *new_vector) { if (vector->dtype == SYMBOLIC) { new_vector = vector; - return; - } else { + } + else { int c = 0; for (unsigned int i = 0; i != vector->length; i++) { c += vector->content[i]; @@ -223,9 +223,11 @@ void collapse_vector(vec *vector, vec *new_vector) { new_vector->content = (int*)safe_calloc(sizeof(int), 1); if (gnoll_errno) return; new_vector->content[0] = c; + printf("Value: %i\n", new_vector->content[0]); new_vector->length = 1; - new_vector->dtype = vector->dtype; + new_vector->dtype = NUMERIC; } + return; } void keep_logic(vec *vector, vec *new_vector, unsigned int number_to_keep, diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 73357f00c..9b6ae96ed 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -88,11 +88,13 @@ def extract_from_dice_file(lines, seperator): os.remove(die_file) out_file = str(die_file).encode("ascii") + verbose = 1 if verbose: print("Rolling: ", s) print("Output in:", out_file) - with pipes() as (out, err): + # with pipes() as (out, err): + if True: s = s.encode("ascii") return_code = libc.roll_full_options( @@ -106,11 +108,11 @@ def extract_from_dice_file(lines, seperator): mock_const, ) - if verbose: - print("---stdout---") - print(out.read()) - print("---stderr---") - print(err.read()) + # if verbose: + # print("---stdout---") + # print(out.read()) + # print("---stderr---") + # print(err.read()) if return_code != 0: raise_gnoll_error(return_code) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index af3d31e6d..79c691cb0 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -32,17 +32,20 @@ def test_d66(): assert result == 33 +# @pytest.mark.skip("TEMP") def test_multiple_internal_calls_macros(): r = "#TEST=d{A,B,C,D,E,F,G,H};@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;" result, _ = roll(r) assert not all(r == result[0] for r in result) - +# @pytest.mark.skip("TEMP") def test_multiple_external_calls_macros(): result = [] r = "#TEST=d{A,B,C,D};@TEST;" for _ in range(20): - result.append(roll(r)) + x= roll(r) + result.append(x) + print(x) assert not all(r == result[0] for r in result) From 3166e0f04969d973342e458310f03dd1b255ca89 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 26 Nov 2022 23:28:19 +0000 Subject: [PATCH 110/138] Format code with yapf, black, gofmt, gofumpt, autopep8 and isort --- tests/python/test_macros.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index 79c691cb0..b8523a3c8 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -38,12 +38,13 @@ def test_multiple_internal_calls_macros(): result, _ = roll(r) assert not all(r == result[0] for r in result) + # @pytest.mark.skip("TEMP") def test_multiple_external_calls_macros(): result = [] r = "#TEST=d{A,B,C,D};@TEST;" for _ in range(20): - x= roll(r) + x = roll(r) result.append(x) print(x) assert not all(r == result[0] for r in result) From b96316022e104ce02fb372acaf3c072a0a355ec4 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sat, 26 Nov 2022 23:55:17 +0000 Subject: [PATCH 111/138] fix minor leak --- src/grammar/dice.yacc | 79 ++++++++++------------------- src/grammar/rolls/dice_frontend.c | 2 + src/grammar/util/vector_functions.c | 5 +- 3 files changed, 33 insertions(+), 53 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 2e80639c1..2ec7d6fbb 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -217,6 +217,7 @@ dice_statement: math{ fclose(fp); } // free_vector(new_vec); + free_vector(vector); $$ = new_vec; }; @@ -595,7 +596,6 @@ collapsing_dice_operations: dice_operations: - die_roll REROLL REROLL condition NUMBER{ vec dice = $1; @@ -636,7 +636,8 @@ dice_operations: yyclearin; } } - |die_roll REROLL condition NUMBER{ + | + die_roll REROLL condition NUMBER{ vec dice = $1; int check = $3.content[0]; @@ -728,8 +729,7 @@ dice_operations: } } | - dice_operations KEEP_HIGHEST NUMBER - { + dice_operations KEEP_HIGHEST NUMBER{ vec keep_vector = $3; vec new_vec; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; @@ -739,8 +739,7 @@ dice_operations: $$ = new_vec; } | - dice_operations DROP_HIGHEST NUMBER - { + dice_operations DROP_HIGHEST NUMBER{ vec keep_vector = $3; vec new_vec; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; @@ -750,8 +749,7 @@ dice_operations: $$ = new_vec; } | - dice_operations KEEP_LOWEST NUMBER - { + dice_operations KEEP_LOWEST NUMBER{ vec keep_vector; keep_vector = $3; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; @@ -762,8 +760,7 @@ dice_operations: $$ = new_vec; } | - dice_operations DROP_LOWEST NUMBER - { + dice_operations DROP_LOWEST NUMBER{ vec keep_vector; keep_vector = $3; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; @@ -774,8 +771,7 @@ dice_operations: $$ = new_vec; } | - dice_operations KEEP_HIGHEST - { + dice_operations KEEP_HIGHEST{ unsigned int num_to_hold = 1; vec new_vec; keep_highest_values(&$1, &new_vec, num_to_hold); @@ -783,8 +779,7 @@ dice_operations: $$ = new_vec; } | - dice_operations DROP_HIGHEST - { + dice_operations DROP_HIGHEST{ vec roll_vec = $1; unsigned int num_to_hold = 1; @@ -794,8 +789,7 @@ dice_operations: $$ = new_vec; } | - dice_operations KEEP_LOWEST - { + dice_operations KEEP_LOWEST{ unsigned int num_to_hold = 1; vec new_vec; @@ -804,8 +798,7 @@ dice_operations: $$ = new_vec; } | - dice_operations DROP_LOWEST - { + dice_operations DROP_LOWEST{ vec roll_vec = $1; unsigned int num_to_hold = 1; @@ -819,7 +812,7 @@ dice_operations: ; die_roll: - NUMBER die_symbol NUMBER EXPLOSION ONCE{ + NUMBER die_symbol NUMBER EXPLOSION ONCE{ vec numA = $1; vec ds = $2; vec numB = $3; @@ -865,7 +858,7 @@ die_roll: free_vector(numB); } | - NUMBER die_symbol NUMBER EXPLOSION PENETRATE{ + NUMBER die_symbol NUMBER EXPLOSION PENETRATE{ vec numA = $1; vec ds = $2; @@ -889,8 +882,7 @@ die_roll: free_vector(numB); } | - die_symbol NUMBER EXPLOSION PENETRATE - { + die_symbol NUMBER EXPLOSION PENETRATE{ vec ds = $1; vec numB = $2; @@ -912,8 +904,7 @@ die_roll: free_vector(numB); } | - NUMBER die_symbol NUMBER EXPLOSION - { + NUMBER die_symbol NUMBER EXPLOSION{ vec numA = $1; vec ds = $2; @@ -936,8 +927,7 @@ die_roll: free_vector(numB); } | - die_symbol NUMBER EXPLOSION - { + die_symbol NUMBER EXPLOSION{ vec ds = $1; vec numB = $2; @@ -959,8 +949,7 @@ die_roll: free_vector(number_of_dice); } | - NUMBER die_symbol NUMBER - { + NUMBER die_symbol NUMBER{ vec numA = $1; vec ds = $2; vec numB = $3; @@ -978,10 +967,10 @@ die_roll: free_vector(numA); } | - die_symbol NUMBER - { + die_symbol NUMBER{ vec ds = $1; vec numB = $2; + vec new_vec; int start_from = ds.content[0]; @@ -992,17 +981,18 @@ die_roll: roll_plain_sided_dice( &number_of_dice, &numB, - &$$, + &new_vec, NO_EXPLOSION, start_from ); + $$ = new_vec; free_vector(number_of_dice); free_vector(ds); free_vector(numB); + } | - NUMBER die_symbol MODULO - { + NUMBER die_symbol MODULO{ vec dice_sides; initialize_vector(&dice_sides, NUMERIC, 1); dice_sides.content[0] = 100; @@ -1016,8 +1006,7 @@ die_roll: ); } | - die_symbol MODULO - { + die_symbol MODULO{ vec num_dice; initialize_vector(&num_dice, NUMERIC, 1); @@ -1035,8 +1024,7 @@ die_roll: ); } | - NUMBER die_symbol DO_COUNT - { + NUMBER die_symbol DO_COUNT{ int start_from = $2.content[0]; @@ -1053,8 +1041,7 @@ die_roll: ); } | - die_symbol DO_COUNT - { + die_symbol DO_COUNT{ int start_from = $1.content[0]; vec num_dice; @@ -1073,8 +1060,7 @@ die_roll: ); } | - NUMBER FATE_DIE - { + NUMBER FATE_DIE{ vec result_vec; initialize_vector(&result_vec, SYMBOLIC, (unsigned int)$1.content[0]); @@ -1086,8 +1072,7 @@ die_roll: $$ = result_vec; } | - FATE_DIE - { + FATE_DIE{ vec result_vec; vec number_of_dice; initialize_vector(&result_vec, SYMBOLIC, 1); @@ -1197,14 +1182,6 @@ custom_symbol_dice: MAX_SYMBOL_LENGTH*sizeof(char) ); } - printf("custom_symbol_dice\n"); - - // for(unsigned int i = 0; i != csd_vec.length; i++){ - // rp.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); - // memcpy(rp.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char)); - // } - // result_vec.source = rp; - // result_vec.has_source = true; // Custom Symbol roll_symbolic_dice( diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 44a012674..2699c5c09 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -36,8 +36,10 @@ void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, rp.start_value = start_offset; int* roll_result = do_roll(rp); initialize_vector(result, NUMERIC, num_dice); + free(result->content); result->content = roll_result; result->source = rp; + result->has_source = true; } void roll_symbolic_dice(vec* x, vec* y, vec* result) { diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 5c578e011..bb77c64a3 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -51,7 +51,9 @@ void initialize_vector(vec *vector, DIE_TYPE dt, unsigned int number_of_items) { if (dt == NUMERIC) { vector->content = (int*)safe_calloc(number_of_items, sizeof(int)); if (gnoll_errno) return; - } else if (dt == SYMBOLIC) { + } + else if (dt == SYMBOLIC) + { vector->symbols = (char**)safe_calloc(number_of_items, sizeof(char *)); if (gnoll_errno) return; @@ -223,7 +225,6 @@ void collapse_vector(vec *vector, vec *new_vector) { new_vector->content = (int*)safe_calloc(sizeof(int), 1); if (gnoll_errno) return; new_vector->content[0] = c; - printf("Value: %i\n", new_vector->content[0]); new_vector->length = 1; new_vector->dtype = NUMERIC; } From 53181be20b0278660838524bcba04c33d79e6430 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 00:04:33 +0000 Subject: [PATCH 112/138] safe_strdup to have consistent length --- src/grammar/dice.yacc | 1 + src/grammar/util/safe_functions.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 2ec7d6fbb..9dee830d8 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1180,6 +1180,7 @@ custom_symbol_dice: result_vec.source.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char) + // MAX_SYMBOL_LENGTH*sizeof(char) ); } diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index b6d3d707d..c92652b1b 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -220,7 +220,8 @@ char *safe_strdup(const char *str1) { } char *result; unsigned int l = strlen(str1) + 1; //+1 for \0 - result = (char*) safe_calloc(sizeof(char), l); + result = (char*) safe_calloc(sizeof(char), MAX_SYMBOL_LENGTH); + // result = (char*) safe_calloc(sizeof(char), l); result = strcpy(result, str1); if (result == 0) { gnoll_errno = BAD_STRING; From 224730d5aa79d7c136369c2c7c23b5b37bb9e662 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 00:10:23 +0000 Subject: [PATCH 113/138] remove more memory leaks --- src/grammar/dice.yacc | 3 +++ src/grammar/operations/macros.c | 9 +++++++++ src/grammar/operations/macros.h | 2 ++ tests/python/test_macros.py | 4 ---- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 9dee830d8..97063141e 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1431,6 +1431,9 @@ int roll_full_options( YY_BUFFER_STATE buffer = yy_scan_string(roll_request); yyparse(); yy_delete_buffer(buffer); + + delete_all_macros(); + return gnoll_errno; } diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index fdcd63b25..4ebd35601 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -31,6 +31,15 @@ unsigned long hash_function(unsigned char *str) { return hash; } +void delete_all_macros() { + struct macro_struct *current_macro, *tmp; + + HASH_ITER(hh, macros, current_macro, tmp) { + HASH_DEL(macros, current_macro); /* delete; users advances to next */ + free(current_macro); /* optional- if you want to free */ + } +} + void register_macro(vec *macro_name, roll_params *to_store) { /** * @brief Register a Macro in GNOLL diff --git a/src/grammar/operations/macros.h b/src/grammar/operations/macros.h index 96a4f018e..f09c3d12b 100644 --- a/src/grammar/operations/macros.h +++ b/src/grammar/operations/macros.h @@ -14,6 +14,8 @@ struct macro_struct { UT_hash_handle hh; /* makes this structure hashable */ }; +void delete_all_macros(); + void register_macro(vec *macro_name, roll_params *to_store); void search_macros(char *skey, roll_params *to_store); diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index b8523a3c8..aafe9a2c6 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -31,15 +31,11 @@ def test_d66(): result, _ = roll(r, mock_mode=Mock.RETURN_CONSTANT, mock_const=3) assert result == 33 - -# @pytest.mark.skip("TEMP") def test_multiple_internal_calls_macros(): r = "#TEST=d{A,B,C,D,E,F,G,H};@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;" result, _ = roll(r) assert not all(r == result[0] for r in result) - -# @pytest.mark.skip("TEMP") def test_multiple_external_calls_macros(): result = [] r = "#TEST=d{A,B,C,D};@TEST;" From 70964ba3c8ffca5a6755a5dc406149132c34ff4f Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 27 Nov 2022 00:10:44 +0000 Subject: [PATCH 114/138] Format code with yapf, black, gofmt, gofumpt, autopep8 and isort --- tests/python/test_macros.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index aafe9a2c6..8911207aa 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -31,11 +31,13 @@ def test_d66(): result, _ = roll(r, mock_mode=Mock.RETURN_CONSTANT, mock_const=3) assert result == 33 + def test_multiple_internal_calls_macros(): r = "#TEST=d{A,B,C,D,E,F,G,H};@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;@TEST;" result, _ = roll(r) assert not all(r == result[0] for r in result) + def test_multiple_external_calls_macros(): result = [] r = "#TEST=d{A,B,C,D};@TEST;" From 31f5afdb2aaaf50611e7153f8a19ee25f9e47799 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 00:24:30 +0000 Subject: [PATCH 115/138] fix another leak --- src/grammar/rolls/dice_frontend.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 2699c5c09..5a3318e7d 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -70,9 +70,13 @@ void roll_symbolic_dice(vec* x, vec* y, vec* result) { y->length, MAX_SYMBOL_LENGTH ); + rp.start_value = 0; // First index of array int* indexes = do_roll(rp); + free_2d_array(&rp.symbol_pool, y->length); + extract_symbols(y->symbols, result->symbols, indexes, rp.number_of_dice); + free(indexes); } From 8e8f38b2f23eb29a0710df047f4262b5b99f8831 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 00:30:17 +0000 Subject: [PATCH 116/138] saf --- src/grammar/operations/macros.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index 4ebd35601..c6e699a9b 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -36,6 +36,7 @@ void delete_all_macros() { HASH_ITER(hh, macros, current_macro, tmp) { HASH_DEL(macros, current_macro); /* delete; users advances to next */ + // free_2d_array(current_macro.stored_dice_roll.symbol_pool); free(current_macro); /* optional- if you want to free */ } } From 3f43884376458073babf63f55fdbc86ee0ac1385 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 10:30:36 +0000 Subject: [PATCH 117/138] boop --- src/grammar/dice.lex | 32 +++++++++++++-------------- src/grammar/dice.yacc | 34 +++++++++++++++++++---------- src/grammar/rolls/dice_frontend.c | 6 +++-- src/grammar/util/safe_functions.c | 4 ---- src/grammar/util/vector_functions.c | 12 +++++++++- 5 files changed, 54 insertions(+), 34 deletions(-) diff --git a/src/grammar/dice.lex b/src/grammar/dice.lex index fb1ce6dc2..086ae1962 100644 --- a/src/grammar/dice.lex +++ b/src/grammar/dice.lex @@ -61,15 +61,15 @@ d { (dF|df)\.1 { char * plus, *minus, *zero; - plus = (char *)safe_malloc(sizeof(char *)); + plus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - plus = "+"; - zero = (char *)safe_malloc(sizeof(char *)); + plus[0] = '+'; + zero = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - zero = "0"; - minus = (char *)safe_malloc(sizeof(char *)); + zero[0] = '0'; + minus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - minus = "-"; + minus[0] = '-'; vec vector; vector.dtype = SYMBOLIC; @@ -89,12 +89,12 @@ d { } (dF|df)\.[3-9] { char * plus, *minus; - plus = (char *)safe_malloc(sizeof(char *)); + plus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - plus = "+"; - minus = (char *)safe_malloc(sizeof(char *)); + plus[0] = '+'; + minus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - minus = "-"; + minus[0] = '-'; vec vector; vector.dtype = SYMBOLIC; @@ -110,15 +110,15 @@ d { } (dF|df)(\.2)? { char * plus, *minus, *zero; - plus = (char *)safe_malloc(sizeof(char *)); + plus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - plus = "+"; - zero = (char *)safe_malloc(sizeof(char *)); + plus[0] = '+'; + zero = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - zero = "0"; - minus = (char *)safe_malloc(sizeof(char *)); + zero[0] = '0'; + minus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - minus = "-"; + minus[0] = '-'; vec vector; vector.dtype = SYMBOLIC; diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 97063141e..414a1ac3b 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -137,7 +137,7 @@ macro_statement: MACRO_STORAGE CAPITAL_STRING ASSIGNMENT math{ /** * - * returns - 0 + * returns - ?? */ vec key = $2; @@ -153,7 +153,6 @@ macro_statement: YYABORT; yyclearin; } - // &$$ = NULL; } ; @@ -165,8 +164,6 @@ dice_statement: math{ vec vector = $1; vec new_vec; - // vec new_vec = vector; // Code Smell. - // // Target Vector should be empty // Step 1: Collapse pool to a single value if nessicary collapse_vector(&vector, &new_vec); @@ -216,10 +213,10 @@ dice_statement: math{ if(write_to_file){ fclose(fp); } - // free_vector(new_vec); free_vector(vector); $$ = new_vec; + }; @@ -1061,18 +1058,30 @@ die_roll: } | NUMBER FATE_DIE{ + /** + * NUMBER - + */ + vec number_of_dice = $1; + vec symb = $2; vec result_vec; - initialize_vector(&result_vec, SYMBOLIC, (unsigned int)$1.content[0]); + initialize_vector(&result_vec, SYMBOLIC, (unsigned int)number_of_dice.content[0]); roll_symbolic_dice( - &$1, - &$2, + &number_of_dice, + &symb, &result_vec ); $$ = result_vec; + free_vector(symb); + free_vector(number_of_dice); + } | FATE_DIE{ + /** + * FATE_DIE - Vector + */ + vec symb = $1; vec result_vec; vec number_of_dice; initialize_vector(&result_vec, SYMBOLIC, 1); @@ -1081,10 +1090,14 @@ die_roll: roll_symbolic_dice( &number_of_dice, - &$1, + &symb, &result_vec ); $$ = result_vec; + free_vector(symb); + free_vector(number_of_dice); + + print_vec(result_vec); } | custom_symbol_dice @@ -1111,7 +1124,7 @@ custom_symbol_dice: // TODO: Multiple ranges vec result_vec; - initialize_vector(&result_vec, SYMBOLIC, (unsigned int)$1.content[0]); + initialize_vector(&result_vec, SYMBOLIC, (unsigned int)left.content[0]); roll_symbolic_dice( &left, @@ -1180,7 +1193,6 @@ custom_symbol_dice: result_vec.source.symbol_pool[i], csd_vec.symbols[i], MAX_SYMBOL_LENGTH*sizeof(char) - // MAX_SYMBOL_LENGTH*sizeof(char) ); } diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 5a3318e7d..1efe01601 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -62,8 +62,10 @@ void roll_symbolic_dice(vec* x, vec* y, vec* result) { rp.symbol_pool = NULL; // Copy over memory to Symbol Pool for reloading - - free_2d_array(&rp.symbol_pool, rp.die_sides); + printf("dst %p\n", (void*)&rp.symbol_pool); + printf("src %p , %p\n", (void*)y->symbols, (void *) y->symbols[0]); + printf("len %u\n", y->length); + printf("MAX_SYMBOL_LENGTH %u\n", MAX_SYMBOL_LENGTH); safe_copy_2d_chararray_with_allocation( &rp.symbol_pool, y->symbols, diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index c92652b1b..ca4eea5a1 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -144,11 +144,7 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, * @param item * @param max_size */ - // TODO: Maybe Re-Add? - // if(*dst != 0){ - // free(*dst); - // } *dst = (char**)safe_calloc(items, sizeof(char **)); if (gnoll_errno) { return; diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index bb77c64a3..270e1d9a8 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -214,7 +214,16 @@ void collapse_vector(vec *vector, vec *new_vector) { } if (vector->dtype == SYMBOLIC) { - new_vector = vector; + safe_copy_2d_chararray_with_allocation( + &new_vector->symbols, + vector->symbols, + vector->length, + MAX_SYMBOL_LENGTH + ); + + new_vector->length = vector->length; + new_vector->dtype = vector->dtype; + new_vector->has_source = false; } else { int c = 0; @@ -227,6 +236,7 @@ void collapse_vector(vec *vector, vec *new_vector) { new_vector->content[0] = c; new_vector->length = 1; new_vector->dtype = NUMERIC; + new_vector->has_source = false; } return; } From 76e7e8f085b9aa4e72f07ad18c6dddb6dc69a761 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 12:07:03 +0000 Subject: [PATCH 118/138] fix delete_all leak --- src/grammar/operations/macros.c | 6 +++++- src/grammar/rolls/dice_frontend.c | 4 ---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index c6e699a9b..a0fa5ced3 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -36,7 +36,11 @@ void delete_all_macros() { HASH_ITER(hh, macros, current_macro, tmp) { HASH_DEL(macros, current_macro); /* delete; users advances to next */ - // free_2d_array(current_macro.stored_dice_roll.symbol_pool); + + free_2d_array( + ¤t_macro->stored_dice_roll.symbol_pool, + current_macro->stored_dice_roll.die_sides + ); free(current_macro); /* optional- if you want to free */ } } diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 1efe01601..8f6c46b3d 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -62,10 +62,6 @@ void roll_symbolic_dice(vec* x, vec* y, vec* result) { rp.symbol_pool = NULL; // Copy over memory to Symbol Pool for reloading - printf("dst %p\n", (void*)&rp.symbol_pool); - printf("src %p , %p\n", (void*)y->symbols, (void *) y->symbols[0]); - printf("len %u\n", y->length); - printf("MAX_SYMBOL_LENGTH %u\n", MAX_SYMBOL_LENGTH); safe_copy_2d_chararray_with_allocation( &rp.symbol_pool, y->symbols, From 0f3c74915a27e4e1816987d1eb5386021475e37d Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 16:02:33 +0000 Subject: [PATCH 119/138] fix a big mem leak --- src/grammar/dice.yacc | 18 ++++++++++++++---- src/grammar/operations/macros.c | 5 +++-- src/grammar/util/safe_functions.c | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 414a1ac3b..8a6cff36a 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -105,7 +105,6 @@ int initialize(){ gnoll_entry: gnoll_statement{ - // printf("A: %i\n", new_vector->content[0]); free_vector($1); } ; @@ -136,8 +135,11 @@ sub_statement: macro_statement: MACRO_STORAGE CAPITAL_STRING ASSIGNMENT math{ /** - * - * returns - ?? + * MACRO_STORAGE - the symbol '#'' + * CAPITAL_STRING - vector + * ASSIGNMENT - the symbol '=' + * math - vector dice roll assignment + * returns - nothing. */ vec key = $2; @@ -153,6 +155,7 @@ macro_statement: YYABORT; yyclearin; } + // $$ = } ; @@ -1188,7 +1191,7 @@ custom_symbol_dice: result_vec.source = rp; result_vec.has_source = true; for(unsigned int i = 0; i != csd_vec.length; i++){ - result_vec.source.symbol_pool[i] = malloc(MAX_SYMBOL_LENGTH); + result_vec.source.symbol_pool[i] = (char*)safe_calloc(sizeof(char),MAX_SYMBOL_LENGTH); memcpy( result_vec.source.symbol_pool[i], csd_vec.symbols[i], @@ -1221,6 +1224,7 @@ custom_symbol_dice: vec new_vector; search_macros(name, &new_vector.source); + if(gnoll_errno){YYABORT;yyclearin;} // Resolve Roll @@ -1240,6 +1244,7 @@ custom_symbol_dice: // Roll according to the stored values // Careful: Newvector used already if (new_vector.source.dtype == NUMERIC){ + die_sides.dtype = NUMERIC; initialize_vector(&new_vector, new_vector.source.dtype, 1); roll_plain_sided_dice( &number_of_dice, @@ -1249,6 +1254,7 @@ custom_symbol_dice: 1 ); }else if (new_vector.source.dtype == SYMBOLIC){ + die_sides.dtype = SYMBOLIC; free_2d_array(&die_sides.symbols, die_sides.length); safe_copy_2d_chararray_with_allocation( &die_sides.symbols, @@ -1256,6 +1262,9 @@ custom_symbol_dice: die_sides.length, MAX_SYMBOL_LENGTH ); + printf("die_sides.symbols: %p\n", die_sides.symbols); + + free_2d_array(&new_vector.source.symbol_pool, new_vector.source.die_sides); initialize_vector(&new_vector, new_vector.source.dtype, 1); roll_symbolic_dice( @@ -1269,6 +1278,7 @@ custom_symbol_dice: } free_vector(vector); free_vector(number_of_dice); + print_vec(die_sides); free_vector(die_sides); $$ = new_vector; } diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index a0fa5ced3..e5bf22b13 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -141,11 +141,12 @@ void search_macros(char *skey, roll_params *to_store) { to_store->die_sides, MAX_SYMBOL_LENGTH ); + } /* DO NOT UNCOMMENT. We need to keep 's' Otherwise macros are lost after usage - - free(s); + free(s); */ + } diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index ca4eea5a1..eda242b11 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -111,7 +111,7 @@ void free_vector(vec v){ free_2d_array(&v.symbols, v.length); if (v.has_source){ // Should be always the same as length (But not sure that's true!) - free_2d_array(&v.source.symbol_pool, v.length); + free_2d_array(&v.source.symbol_pool, v.source.die_sides); } } } @@ -146,6 +146,7 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, */ *dst = (char**)safe_calloc(items, sizeof(char **)); + printf("Dst: %p\n", *dst); if (gnoll_errno) { return; } From 9b156826242bff8d9969cc8ef1771d2eeb62aa37 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 16:13:53 +0000 Subject: [PATCH 120/138] fix another --- src/grammar/dice.yacc | 11 +++++++++-- src/grammar/util/vector_functions.c | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 8a6cff36a..5679d9c78 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -105,12 +105,17 @@ int initialize(){ gnoll_entry: gnoll_statement{ + printf("end: %p\n", $1.symbols); + free_vector($1); } ; gnoll_statement: - gnoll_statement STATEMENT_SEPERATOR gnoll_statement + gnoll_statement STATEMENT_SEPERATOR gnoll_statement{ + free_vector($3); + // vec1 freed at root. + } | /* Allow ending with ; */ gnoll_statement STATEMENT_SEPERATOR @@ -170,6 +175,7 @@ dice_statement: math{ // Step 1: Collapse pool to a single value if nessicary collapse_vector(&vector, &new_vec); + printf("yacc: %p\n", new_vec.symbols); if(gnoll_errno){ YYABORT; yyclearin; @@ -218,8 +224,9 @@ dice_statement: math{ } free_vector(vector); + + printf("end of yacc: %p\n", new_vec.symbols); $$ = new_vec; - }; diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 270e1d9a8..2b55284fa 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -220,9 +220,10 @@ void collapse_vector(vec *vector, vec *new_vector) { vector->length, MAX_SYMBOL_LENGTH ); + printf("vf %p\n", new_vector->symbols); new_vector->length = vector->length; - new_vector->dtype = vector->dtype; + new_vector->dtype = SYMBOLIC; new_vector->has_source = false; } else { From c29975fc468f75f36ad6aa03a83a1e6b56d7d416 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 17:08:39 +0000 Subject: [PATCH 121/138] fix most?? --- src/grammar/dice.yacc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 5679d9c78..a29c3864f 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1243,15 +1243,15 @@ custom_symbol_dice: number_of_dice.content[0] = (int)new_vector.source.number_of_dice; // Set Die Sides - light_initialize_vector(&die_sides, NUMERIC, 1); - die_sides.content[0] = (int)new_vector.source.die_sides; - die_sides.length = new_vector.source.die_sides; - die_sides.symbols = NULL; + // die_sides.content[0] = (int)new_vector.source.die_sides; + // die_sides.symbols = NULL; // Roll according to the stored values // Careful: Newvector used already if (new_vector.source.dtype == NUMERIC){ - die_sides.dtype = NUMERIC; + light_initialize_vector(&die_sides, NUMERIC, 1); + die_sides.length = new_vector.source.die_sides; + die_sides.content[0] = (int)new_vector.source.die_sides; initialize_vector(&new_vector, new_vector.source.dtype, 1); roll_plain_sided_dice( &number_of_dice, @@ -1261,8 +1261,9 @@ custom_symbol_dice: 1 ); }else if (new_vector.source.dtype == SYMBOLIC){ - die_sides.dtype = SYMBOLIC; - free_2d_array(&die_sides.symbols, die_sides.length); + light_initialize_vector(&die_sides, SYMBOLIC, 1); + die_sides.length = new_vector.source.die_sides; + free(die_sides.symbols); safe_copy_2d_chararray_with_allocation( &die_sides.symbols, new_vector.source.symbol_pool, From 6d90576638e341f28fe61df8b613dd9ecf106f50 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 17:12:52 +0000 Subject: [PATCH 122/138] ! --- src/grammar/dice.yacc | 5 ----- src/grammar/util/safe_functions.c | 1 - src/grammar/util/vector_functions.c | 1 - 3 files changed, 7 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a29c3864f..9473e51ba 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -105,8 +105,6 @@ int initialize(){ gnoll_entry: gnoll_statement{ - printf("end: %p\n", $1.symbols); - free_vector($1); } ; @@ -175,7 +173,6 @@ dice_statement: math{ // Step 1: Collapse pool to a single value if nessicary collapse_vector(&vector, &new_vec); - printf("yacc: %p\n", new_vec.symbols); if(gnoll_errno){ YYABORT; yyclearin; @@ -225,7 +222,6 @@ dice_statement: math{ free_vector(vector); - printf("end of yacc: %p\n", new_vec.symbols); $$ = new_vec; }; @@ -1270,7 +1266,6 @@ custom_symbol_dice: die_sides.length, MAX_SYMBOL_LENGTH ); - printf("die_sides.symbols: %p\n", die_sides.symbols); free_2d_array(&new_vector.source.symbol_pool, new_vector.source.die_sides); diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index eda242b11..6473fd314 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -146,7 +146,6 @@ void safe_copy_2d_chararray_with_allocation(char ***dst, char **src, */ *dst = (char**)safe_calloc(items, sizeof(char **)); - printf("Dst: %p\n", *dst); if (gnoll_errno) { return; } diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 2b55284fa..64e11f57b 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -220,7 +220,6 @@ void collapse_vector(vec *vector, vec *new_vector) { vector->length, MAX_SYMBOL_LENGTH ); - printf("vf %p\n", new_vector->symbols); new_vector->length = vector->length; new_vector->dtype = SYMBOLIC; From 5f20ffc2b4f257a4187853a46462cef89f2af1dc Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 17:19:04 +0000 Subject: [PATCH 123/138] remove print --- src/grammar/dice.yacc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 9473e51ba..fcd7b6cd9 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1275,13 +1275,13 @@ custom_symbol_dice: &die_sides, &new_vector ); + }else{ printf("Complex Dice Equation. Only dice definitions supported. No operations\n"); gnoll_errno = NOT_IMPLEMENTED; } free_vector(vector); free_vector(number_of_dice); - print_vec(die_sides); free_vector(die_sides); $$ = new_vector; } From a18b2a10bdce415078f683f61a256371faa65795 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 17:33:22 +0000 Subject: [PATCH 124/138] more --- src/grammar/dice.yacc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index fcd7b6cd9..b2509c37f 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1256,6 +1256,8 @@ custom_symbol_dice: new_vector.source.explode, 1 ); + free_vector(die_sides); + }else if (new_vector.source.dtype == SYMBOLIC){ light_initialize_vector(&die_sides, SYMBOLIC, 1); die_sides.length = new_vector.source.die_sides; @@ -1275,14 +1277,14 @@ custom_symbol_dice: &die_sides, &new_vector ); - + free_vector(die_sides); + }else{ printf("Complex Dice Equation. Only dice definitions supported. No operations\n"); gnoll_errno = NOT_IMPLEMENTED; } free_vector(vector); free_vector(number_of_dice); - free_vector(die_sides); $$ = new_vector; } ; From fcc05c28340b8e7e729fc4331507ab0f70521dce Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 18:04:33 +0000 Subject: [PATCH 125/138] a --- src/grammar/operations/macros.c | 9 ++++----- src/python/code/gnoll/parser.py | 36 +++++++++++---------------------- 2 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index e5bf22b13..2101a9561 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -64,12 +64,9 @@ void register_macro(vec *macro_name, roll_params *to_store) { int k = (int)key; unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - if (verbose) printf("Macro:: Check existance\n"); HASH_FIND_INT(macros, &k, s); // id already in the hash? - - if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -80,7 +77,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { HASH_ADD_INT(macros, id, s); // id: name of key field } - + memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); // s->stored_dice_roll.symbol_pool = NULL; @@ -95,7 +92,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { // free_roll_params(to_store); //new // free_2d_array(&to_store->symbol_pool, to_store->die_sides); } - + //free(s); //new //free(skey); //new } @@ -122,7 +119,9 @@ void search_macros(char *skey, roll_params *to_store) { HASH_FIND_INT(macros, &k, s); /* s: output pointer */ + if (s == NULL) { + if (verbose) printf("Macro:: UNDEFINED (macros.c)\n"); gnoll_errno = UNDEFINED_MACRO; return; } diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 9b6ae96ed..9314c71d0 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -3,8 +3,6 @@ import tempfile from ctypes import cdll -from wurlitzer import pipes - BUILD_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "c_build")) C_SHARED_LIB = os.path.join(BUILD_DIR, "dice.so") @@ -88,32 +86,22 @@ def extract_from_dice_file(lines, seperator): os.remove(die_file) out_file = str(die_file).encode("ascii") - verbose = 1 if verbose: print("Rolling: ", s) print("Output in:", out_file) - # with pipes() as (out, err): - if True: - s = s.encode("ascii") - - return_code = libc.roll_full_options( - s, - out_file, - False, # enable_verbose - breakdown, # enable_introspect - mock is not None, # enable_mock - builtins, # enable_builtins - mock, - mock_const, - ) - - # if verbose: - # print("---stdout---") - # print(out.read()) - # print("---stderr---") - # print(err.read()) - + s = s.encode("ascii") + + return_code = libc.roll_full_options( + s, + out_file, + True, # enable_verbose + breakdown, # enable_introspect + mock is not None, # enable_mock + builtins, # enable_builtins + mock, + mock_const, + ) if return_code != 0: raise_gnoll_error(return_code) From 1f1073f36efb70f4605d6c4a76a893787c6d83ac Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 18:59:56 +0000 Subject: [PATCH 126/138] fix for df.2 --- src/grammar/dice.lex | 29 +++++++++++++++++++++-------- src/grammar/dice.yacc | 3 +-- src/grammar/util/safe_functions.c | 2 -- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/grammar/dice.lex b/src/grammar/dice.lex index 086ae1962..6f821d5b0 100644 --- a/src/grammar/dice.lex +++ b/src/grammar/dice.lex @@ -60,13 +60,25 @@ d { } (dF|df)\.1 { - char * plus, *minus, *zero; + char *plus, *minus; + char *zeroA, *zeroB, *zeroC, *zeroD; plus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} plus[0] = '+'; - zero = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); + + zeroA = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} - zero[0] = '0'; + zeroA[0] = '0'; + zeroB = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); + if(gnoll_errno){yyerror("Memory Err");} + zeroB[0] = '0'; + zeroC = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); + if(gnoll_errno){yyerror("Memory Err");} + zeroC[0] = '0'; + zeroD = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); + if(gnoll_errno){yyerror("Memory Err");} + zeroD[0] = '0'; + minus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} minus[0] = '-'; @@ -76,10 +88,10 @@ d { vector.symbols = (char**)safe_malloc(sizeof(char **) * 6); if(gnoll_errno){yyerror("Memory Err");} vector.symbols[0] = plus; - vector.symbols[1] = zero; - vector.symbols[2] = zero; - vector.symbols[3] = zero; - vector.symbols[4] = zero; + vector.symbols[1] = zeroA; + vector.symbols[2] = zeroB; + vector.symbols[3] = zeroC; + vector.symbols[4] = zeroD; vector.symbols[5] = minus; vector.length = 6; vector.has_source = false; @@ -109,7 +121,7 @@ d { return(FATE_DIE); } (dF|df)(\.2)? { - char * plus, *minus, *zero; + char *plus, *minus, *zero; plus = (char *)safe_calloc(sizeof(char *),MAX_SYMBOL_LENGTH); if(gnoll_errno){yyerror("Memory Err");} plus[0] = '+'; @@ -129,6 +141,7 @@ d { vector.symbols[2] = minus; vector.has_source = false; vector.length = 3; + vector.has_source = false; yylval.values = vector; return(FATE_DIE); diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index b2509c37f..1abca4c44 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1103,7 +1103,6 @@ die_roll: free_vector(symb); free_vector(number_of_dice); - print_vec(result_vec); } | custom_symbol_dice @@ -1564,7 +1563,7 @@ int main(int argc, char **str){ 1, // Verbose 1, // Introspect 0, // Mocking - 1, // Builtins + 0, // Builtins 0, 0 ); diff --git a/src/grammar/util/safe_functions.c b/src/grammar/util/safe_functions.c index 6473fd314..dfdd6f644 100644 --- a/src/grammar/util/safe_functions.c +++ b/src/grammar/util/safe_functions.c @@ -123,9 +123,7 @@ void free_2d_array(char ***arr, unsigned int items) { * @param items */ if (*arr) { - // printf("Arr: %p\n", (void *)arr); for (unsigned int i = 0; i != items; i++) { - // printf("[%u] Try to free: %p\n",i, (*arr)[i]); if ((*arr)[i]) { free((*arr)[i]); } From e0ad92115ea0eac06fa6219606813971054ceec0 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 22:04:39 +0000 Subject: [PATCH 127/138] more more more! --- src/grammar/dice.yacc | 86 +++++++++++++++++++++++++++---- src/grammar/rolls/dice_core.c | 1 - src/grammar/rolls/dice_frontend.c | 1 + 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 1abca4c44..e29debbd1 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -696,9 +696,14 @@ dice_operations: } | dice_operations FILTER singular_condition{ - vec new_vec; + /** + * dice_operations vector + * FILTER symbol 'f' + * singular_condition symbol + */ vec dice = $1; int check = $3.content[0]; + vec new_vec; if(dice.dtype == NUMERIC){ initialize_vector(&new_vec, NUMERIC, dice.length); @@ -711,11 +716,15 @@ dice_operations: YYABORT; yyclearin;; } + free_vector(dice); } | dice_operations UNIQUE{ - // TODO + /** + * dice_operations vector + * UNIQUE symbol 'u' + */ vec new_vec; vec dice = $1; @@ -730,59 +739,102 @@ dice_operations: YYABORT; yyclearin;; } + free_vector(dice); } | dice_operations KEEP_HIGHEST NUMBER{ + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + * NUMBER vector + */ + vec do_vec = $1; vec keep_vector = $3; vec new_vec; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; - keep_highest_values(&$1, &new_vec, num_to_hold); + keep_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(do_vec); + free_vector(keep_vector); } | dice_operations DROP_HIGHEST NUMBER{ + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + * NUMBER vector + */ + vec do_vec = $1; vec keep_vector = $3; vec new_vec; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; - drop_highest_values(&$1, &new_vec, num_to_hold); + drop_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(do_vec); + free_vector(keep_vector); + } | dice_operations KEEP_LOWEST NUMBER{ - vec keep_vector; - keep_vector = $3; + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + * NUMBER vector + */ + + vec do_vec = $1; + vec keep_vector = $3; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; vec new_vec; keep_lowest_values(&$1, &new_vec, num_to_hold); $$ = new_vec; + free_vector(do_vec); + free_vector(keep_vector); } | dice_operations DROP_LOWEST NUMBER{ - vec keep_vector; - keep_vector = $3; + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + * NUMBER vector + */ + vec do_vec = $1; + vec keep_vector = $3; unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; vec new_vec; - drop_lowest_values(&$1, &new_vec, num_to_hold); + drop_lowest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(do_vec); + free_vector(keep_vector); } | dice_operations KEEP_HIGHEST{ + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + */ + vec do_vec = $1; unsigned int num_to_hold = 1; vec new_vec; - keep_highest_values(&$1, &new_vec, num_to_hold); + keep_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(do_vec); } | dice_operations DROP_HIGHEST{ + /** + * dice_operations vector + * KEEP_HIGHEST symbol 'kh' + */ vec roll_vec = $1; unsigned int num_to_hold = 1; @@ -790,18 +842,29 @@ dice_operations: drop_highest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(roll_vec); } | dice_operations KEEP_LOWEST{ + /** + * dice_operations vector + * KEEP_LOWEST symbol 'kh' + */ + vec roll_vec = $1; unsigned int num_to_hold = 1; vec new_vec; - keep_lowest_values(&$1, &new_vec, num_to_hold); + keep_lowest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; + free_vector(roll_vec); } | dice_operations DROP_LOWEST{ + /** + * dice_operations vector + * DROP_LOWEST symbol 'dl' + */ vec roll_vec = $1; unsigned int num_to_hold = 1; @@ -809,6 +872,7 @@ dice_operations: drop_lowest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; + free(roll_vec); } | die_roll diff --git a/src/grammar/rolls/dice_core.c b/src/grammar/rolls/dice_core.c index 71c525824..7194620ab 100644 --- a/src/grammar/rolls/dice_core.c +++ b/src/grammar/rolls/dice_core.c @@ -137,7 +137,6 @@ int* perform_roll(unsigned int number_of_dice, unsigned int die_sides, fprintf(fp, "\n"); fclose(fp); } - return all_dice_roll; } diff --git a/src/grammar/rolls/dice_frontend.c b/src/grammar/rolls/dice_frontend.c index 8f6c46b3d..2c3fcdf54 100644 --- a/src/grammar/rolls/dice_frontend.c +++ b/src/grammar/rolls/dice_frontend.c @@ -35,6 +35,7 @@ void roll_plain_sided_dice(vec* x, vec* y, vec* result, EXPLOSION_TYPE explode, rp.explode = explode; rp.start_value = start_offset; int* roll_result = do_roll(rp); + initialize_vector(result, NUMERIC, num_dice); free(result->content); result->content = roll_result; From b361b59744b471273bfdf4b067d03909884dcd64 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 22:31:13 +0000 Subject: [PATCH 128/138] even more leaks --- src/grammar/dice.yacc | 83 ++++++++++++++++++++++++++++----- src/python/code/gnoll/parser.py | 1 + 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index e29debbd1..1dcea5d37 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -872,7 +872,7 @@ dice_operations: drop_lowest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; - free(roll_vec); + free_vector(roll_vec); } | die_roll @@ -880,6 +880,13 @@ dice_operations: die_roll: NUMBER die_symbol NUMBER EXPLOSION ONCE{ + /** + * NUMBER vector + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + * ONCE symbol 'o' + */ vec numA = $1; vec ds = $2; vec numB = $3; @@ -903,6 +910,12 @@ die_roll: } | die_symbol NUMBER EXPLOSION ONCE{ + /** + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + * ONCE symbol 'o' + */ vec ds = $1; vec numB = $2; @@ -950,6 +963,12 @@ die_roll: } | die_symbol NUMBER EXPLOSION PENETRATE{ + /** + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + * PENETRATE symbol 'p' + */ vec ds = $1; vec numB = $2; @@ -972,16 +991,18 @@ die_roll: } | NUMBER die_symbol NUMBER EXPLOSION{ + /** + * NUMBER vector + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + */ vec numA = $1; vec ds = $2; vec numB = $3; int start_from = ds.content[0]; - vec number_of_dice; - initialize_vector(&number_of_dice, NUMERIC, 1); - number_of_dice.content[0] = 1; - roll_plain_sided_dice( &numA, &numB, @@ -995,6 +1016,11 @@ die_roll: } | die_symbol NUMBER EXPLOSION{ + /** + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + */ vec ds = $1; vec numB = $2; @@ -1017,6 +1043,11 @@ die_roll: } | NUMBER die_symbol NUMBER{ + /** + * NUMBER vector + * die_symbol vector + * NUMBER vector + */ vec numA = $1; vec ds = $2; vec numB = $3; @@ -1035,6 +1066,10 @@ die_roll: } | die_symbol NUMBER{ + /** + * die_symbol vector + * NUMBER vector + */ vec ds = $1; vec numB = $2; vec new_vec; @@ -1056,25 +1091,39 @@ die_roll: free_vector(number_of_dice); free_vector(ds); free_vector(numB); - } | - NUMBER die_symbol MODULO{ + NUMBER die_symbol MODULO{ + /** + * NUMBER vector + * die_symbol vector - d or z + * MODULE symbol % + */ + + // TODO: z% is not functional! + + vec num_dice = $1; vec dice_sides; initialize_vector(&dice_sides, NUMERIC, 1); dice_sides.content[0] = 100; roll_plain_sided_dice( - &$1, + &num_dice, &dice_sides, &$$, NO_EXPLOSION, 1 ); + free_vector(num_dice); + free_vector(dice_sides); } | die_symbol MODULO{ - + /** + * die_symbol vector + * NUMBER vector + */ + // TODO: z% is not possible yet. vec num_dice; initialize_vector(&num_dice, NUMERIC, 1); num_dice.content[0] = 1; @@ -1089,23 +1138,33 @@ die_roll: NO_EXPLOSION, 1 ); + free_vector(num_dice); + free_vector(dice_sides); } | NUMBER die_symbol DO_COUNT{ - - int start_from = $2.content[0]; + /** + * NUMBER vector + * die_symbol vector + * DO_COUNT symbol 'c' + */ + vec num = $1; + vec die_sym = $2; + int start_from = die_sym.content[0]; vec dice_sides; initialize_vector(&dice_sides, NUMERIC, 1); dice_sides.content[0] = 2; roll_plain_sided_dice( - &$1, + &num, &dice_sides, &$$, NO_EXPLOSION, start_from ); + free_vector(num); + free_vector(die_sym); } | die_symbol DO_COUNT{ diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 9314c71d0..90657e596 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -86,6 +86,7 @@ def extract_from_dice_file(lines, seperator): os.remove(die_file) out_file = str(die_file).encode("ascii") + verbose = 1 if verbose: print("Rolling: ", s) print("Output in:", out_file) From a82dd7c587b7633b828d78eb01f58f78203343f3 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 23:00:24 +0000 Subject: [PATCH 129/138] It never ends Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 1dcea5d37..2cc77ba14 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -676,10 +676,18 @@ dice_operations: } | dice_operations FILTER condition NUMBER{ + /* + * dice_operations vector + * Filter symbol 'f' + * condition vector + * Number vector + */ vec new_vec; vec dice = $1; vec condition = $4; - int check = $3.content[0]; + vec cv = $3; + + int check = cv.content[0]; if(dice.dtype == NUMERIC){ initialize_vector(&new_vec, NUMERIC, dice.length); @@ -690,9 +698,11 @@ dice_operations: printf("No support for Symbolic die rerolling yet!\n"); gnoll_errno = NOT_IMPLEMENTED; YYABORT; - yyclearin;; + yyclearin; } - + free_vector(dice); + free_vector(condition); + free_vector(cv); } | dice_operations FILTER singular_condition{ @@ -1168,7 +1178,12 @@ die_roll: } | die_symbol DO_COUNT{ - int start_from = $1.content[0]; + /** + * die_symbol vector + * DO_COUNT symbol 'c' + */ + vec ds= $1; + int start_from = ds.content[0]; vec num_dice; initialize_vector(&num_dice, NUMERIC, 1); @@ -1184,6 +1199,9 @@ die_roll: NO_EXPLOSION, start_from ); + free_vector(ds); + free_vector(num_dice); + free_vector(dice_sides); } | NUMBER FATE_DIE{ From ef12ceb35d035baafc76b01cf75440efc5ff1dde Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Sun, 27 Nov 2022 23:01:15 +0000 Subject: [PATCH 130/138] Update requirements.txt Signed-off-by: Ian Hunter --- reqs/requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/reqs/requirements.txt b/reqs/requirements.txt index 3de2dc5e4..f8a39c484 100644 --- a/reqs/requirements.txt +++ b/reqs/requirements.txt @@ -4,7 +4,6 @@ cppyy # tests pytest numpy>=1.22.2 -wurlitzer # distribution build From 240a711bd08a07c1210522f085b92cf06d75a540 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 00:21:20 +0000 Subject: [PATCH 131/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 2cc77ba14..a87762ec0 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -292,7 +292,11 @@ math: } | math MULT math{ - // Collapse both sides and subtract + /** @brief Collapse both sides and multiply + * Math vector + * MULT symbol '*' + * Math vector + */ vec vector1 = $1; vec vector2 = $3; @@ -319,6 +323,11 @@ math: } | math DIVIDE_ROUND_UP math{ + /** @brief Collapse both sides and divide + * Math vector + * Divide symbol '/' + * Math vector + */ // Collapse both sides and subtract vec vector1 = $1; vec vector2 = $3; @@ -353,6 +362,11 @@ math: } | math DIVIDE_ROUND_DOWN math{ + /** @brief Collapse both sides and divide + * Math vector + * Divide symbol '\' + * Math vector + */ // Collapse both sides and subtract vec vector1 = $1; vec vector2 = $3; @@ -389,6 +403,11 @@ math: } | math MODULO math{ + /** @brief Collapse both sides and modulo + * Math vector + * MULT symbol '%' + * Math vector + */ // Collapse both sides and subtract vec vector1 = $1; vec vector2 = $3; @@ -484,6 +503,11 @@ math: } | math MINUS math{ + /** @brief Collapse both sides and subtract + * Math vector + * MINUS symbol '-' + * Math vector + */ vec vector1 = $1; vec vector2 = $3; if ( From b4c27e0fc6ad19668942434c03481e0ca2af4d5e Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 00:28:48 +0000 Subject: [PATCH 132/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index a87762ec0..92026a855 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -665,12 +665,20 @@ dice_operations: } | die_roll REROLL condition NUMBER{ + /* + * die_roll vector + * Reroll symbol + * condition vector + * Number vector + */ vec dice = $1; - int check = $3.content[0]; + vec comp = $3; + int check = comp.content[0]; + vec numv = $4; if(dice.dtype == NUMERIC){ - if (check_condition(&dice, &$4, (COMPARATOR)check)){ + if (check_condition(&dice, &numv, (COMPARATOR)check)){ vec number_of_dice; initialize_vector(&number_of_dice, NUMERIC, 1); @@ -687,9 +695,11 @@ dice_operations: dice.source.explode, 1 ); + free_vector(dice); + free_vector(number_of_dice); }else{ // No need to reroll - $$ = $1; + $$ = dice; } }else{ printf("No support for Symbolic die rerolling yet!"); @@ -697,6 +707,8 @@ dice_operations: YYABORT; yyclearin;; } + free_vector(numv); + free_vector(comp); } | dice_operations FILTER condition NUMBER{ @@ -973,16 +985,18 @@ die_roll: } | NUMBER die_symbol NUMBER EXPLOSION PENETRATE{ - + /** + * NUMBER vector + * die_symbol vector + * NUMBER vector + * EXPLOSION symbol 'e' or similar + * PENETRATE symbol 'p' + */ vec numA = $1; vec ds = $2; vec numB = $3; int start_from = ds.content[0]; - vec number_of_dice; - initialize_vector(&number_of_dice, NUMERIC, 1); - number_of_dice.content[0] = 1; - roll_plain_sided_dice( &numA, &numB, From 7fcbf6067e16a285c43d132f7def2cb9227469d4 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 09:07:50 +0000 Subject: [PATCH 133/138] Update dice.yacc Signed-off-by: Ian Hunter --- src/grammar/dice.yacc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 92026a855..8e7a378f2 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -624,13 +624,24 @@ collapsing_dice_operations: dice_operations: die_roll REROLL REROLL condition NUMBER{ + /** + * dice_roll a vector + * REROLL symbol 'r' + * REROLL symbol 'r' + * condition vector + * Number vector + * returns a vector + */ vec dice = $1; - int check = $4.content[0]; + vec cv = $4; + vec cvno = $5; + + int check = cv.content[0]; if(dice.dtype == NUMERIC){ int count = 0; - while (! check_condition(&dice, &$5, (COMPARATOR)check)){ + while (! check_condition(&dice, &cvno, (COMPARATOR)check)){ if (count > MAX_ITERATION){ printf("MAX ITERATION LIMIT EXCEEDED: REROLL\n"); gnoll_errno = MAX_LOOP_LIMIT_HIT; @@ -656,12 +667,17 @@ dice_operations: count ++; } $$ = dice; + + free_vector(die_sides); + free_vector(number_of_dice); }else{ printf("No support for Symbolic die rerolling yet!\n"); gnoll_errno = NOT_IMPLEMENTED; YYABORT; yyclearin; } + free_vector(cv); + free_vector(cvno); } | die_roll REROLL condition NUMBER{ From fc4a82f9c109ade0db3c29ed5ee32bd7f162810a Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 10:48:57 +0000 Subject: [PATCH 134/138] fix doublefree --- src/grammar/dice.yacc | 24 ++++++++++---------- src/grammar/util/vector_functions.c | 34 +++++++++++++++++++---------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 8e7a378f2..0f2f94c25 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -665,11 +665,11 @@ dice_operations: 1 ); count ++; + free_vector(die_sides); + free_vector(number_of_dice); } $$ = dice; - free_vector(die_sides); - free_vector(number_of_dice); }else{ printf("No support for Symbolic die rerolling yet!\n"); gnoll_errno = NOT_IMPLEMENTED; @@ -737,7 +737,7 @@ dice_operations: vec new_vec; vec dice = $1; vec condition = $4; - vec cv = $3; + vec cv = $3; int check = cv.content[0]; @@ -818,7 +818,7 @@ dice_operations: keep_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(do_vec); + // free_vector(do_vec); free_vector(keep_vector); } | @@ -836,7 +836,7 @@ dice_operations: drop_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(do_vec); + // free_vector(do_vec); free_vector(keep_vector); } @@ -853,10 +853,10 @@ dice_operations: unsigned int num_to_hold = (unsigned int)keep_vector.content[0]; vec new_vec; - keep_lowest_values(&$1, &new_vec, num_to_hold); + keep_lowest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(do_vec); + // free_vector(do_vec); free_vector(keep_vector); } | @@ -874,7 +874,7 @@ dice_operations: drop_lowest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(do_vec); + // free_vector(do_vec); free_vector(keep_vector); } | @@ -889,7 +889,7 @@ dice_operations: keep_highest_values(&do_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(do_vec); + // free_vector(do_vec); } | dice_operations DROP_HIGHEST{ @@ -904,7 +904,7 @@ dice_operations: drop_highest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(roll_vec); + // free_vector(roll_vec); } | dice_operations KEEP_LOWEST{ @@ -919,7 +919,7 @@ dice_operations: keep_lowest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(roll_vec); + // free_vector(roll_vec); } | dice_operations DROP_LOWEST{ @@ -934,7 +934,7 @@ dice_operations: drop_lowest_values(&roll_vec, &new_vec, num_to_hold); $$ = new_vec; - free_vector(roll_vec); + // free_vector(roll_vec); } | die_roll diff --git a/src/grammar/util/vector_functions.c b/src/grammar/util/vector_functions.c index 64e11f57b..a5682e288 100644 --- a/src/grammar/util/vector_functions.c +++ b/src/grammar/util/vector_functions.c @@ -241,12 +241,12 @@ void collapse_vector(vec *vector, vec *new_vector) { return; } -void keep_logic(vec *vector, vec *new_vector, unsigned int number_to_keep, +void keep_logic(vec *vector, vec *output_vector, unsigned int number_to_keep, int keep_high) { /** * @brief Collapses multiple Numeric dice to one value by summing - * @param vector source - * @param new_vector dest + * @param vector source (Freed at end) + * @param output_vector dest * @param number_to_keep how many values to keep or drop * @param keep_high Whether to keep the highest (1) or Lowest (0) values */ @@ -254,6 +254,8 @@ void keep_logic(vec *vector, vec *new_vector, unsigned int number_to_keep, return; } + unsigned int available_amount = vector->length; + if (vector->dtype == SYMBOLIC) { printf( "Symbolic Dice, Cannot determine value. Consider using filters " @@ -261,18 +263,23 @@ void keep_logic(vec *vector, vec *new_vector, unsigned int number_to_keep, gnoll_errno = UNDEFINED_BEHAVIOUR; return; } - unsigned int available_amount = vector->length; + if (available_amount > number_to_keep) { - new_vector->content = (int*)safe_calloc(sizeof(int), number_to_keep); - if (gnoll_errno) { - return; - } - new_vector->length = number_to_keep; + initialize_vector(output_vector, vector->dtype, number_to_keep); + + // output_vector->content = (int*)safe_calloc(sizeof(int), number_to_keep); + // if (gnoll_errno) { + // return; + // } + // output_vector->length = number_to_keep; int *arr = vector->content; int *new_arr; unsigned int length = vector->length; + // while (number needed) + // Get Max/Min from vector + // Store in output for (unsigned int i = 0; i != number_to_keep; i++) { int m; if (keep_high) { @@ -280,22 +287,25 @@ void keep_logic(vec *vector, vec *new_vector, unsigned int number_to_keep, } else { m = min_in_vec(arr, length); } - new_vector->content[i] = m; + output_vector->content[i] = m; new_arr = (int*)safe_calloc(sizeof(int), length - 1); if (gnoll_errno) { return; } + // Take 'm' out of the array (put in new_array) pop(arr, length, m, new_arr); free(arr); arr = new_arr; length -= 1; } - new_vector->dtype = vector->dtype; + free(arr); + // output_vector->content = arr; + output_vector->dtype = vector->dtype; } else { // e.g. 2d20k4 / 2d20kh2 printf("Warning: KeepHighest: Keeping <= produced amount"); - new_vector = vector; + output_vector = vector; } } From c19b7dad0c7d72e1e07fb33a992998a7769f65b1 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 11:22:12 +0000 Subject: [PATCH 135/138] fix re-registration --- src/grammar/dice.yacc | 10 ++++++---- src/grammar/operations/macros.c | 25 +++++++++---------------- tests/python/test_macros.py | 1 + 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 0f2f94c25..d4d6c2ce4 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -158,7 +158,9 @@ macro_statement: YYABORT; yyclearin; } - // $$ = + vec null_vec; + light_initialize_vector(&null_vec, NUMERIC, 0); + $$ = null_vec; } ; @@ -1151,10 +1153,10 @@ die_roll: NO_EXPLOSION, start_from ); - $$ = new_vec; free_vector(number_of_dice); free_vector(ds); free_vector(numB); + $$ = new_vec; } | NUMBER die_symbol MODULO{ @@ -1759,8 +1761,8 @@ int main(int argc, char **str){ 1, // Introspect 0, // Mocking 0, // Builtins - 0, - 0 + 1, // Mocking + 3 // Mocking Seed ); print_gnoll_errors(); FILE *f = fopen("output.dice","r"); diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index 2101a9561..c29cda1d6 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -75,26 +75,19 @@ void register_macro(vec *macro_name, roll_params *to_store) { } s->id = (int)key; HASH_ADD_INT(macros, id, s); // id: name of key field - } - - memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); - // s->stored_dice_roll.symbol_pool = NULL; - - if (is_symbolic) { - // free symbols from roll in S - // free_2d_array(&s->stored_dice_roll.symbol_pool, - // s->stored_dice_roll.die_sides); - safe_copy_2d_chararray_with_allocation( - &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, - to_store->die_sides, MAX_SYMBOL_LENGTH); + memcpy(&s->stored_dice_roll, to_store, sizeof(*to_store)); + + if (is_symbolic) { + safe_copy_2d_chararray_with_allocation( + &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, + to_store->die_sides, MAX_SYMBOL_LENGTH); + } - // free_roll_params(to_store); //new - // free_2d_array(&to_store->symbol_pool, to_store->die_sides); } + + - //free(s); //new - //free(skey); //new } void search_macros(char *skey, roll_params *to_store) { diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index 8911207aa..b7b6fb3c4 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -42,6 +42,7 @@ def test_multiple_external_calls_macros(): result = [] r = "#TEST=d{A,B,C,D};@TEST;" for _ in range(20): + print("~~~~ ROLL~~~~") x = roll(r) result.append(x) print(x) From c0d06f0d612a8f478b03587dbf6e5a1dc8cf56f5 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 11:27:23 +0000 Subject: [PATCH 136/138] builtins --- src/grammar/dice.yacc | 11 +++-------- src/python/code/gnoll/parser.py | 1 - 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index d4d6c2ce4..4ff69f44d 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1645,8 +1645,6 @@ int roll_full_options( initialize(); - enable_builtins = 0; - if(enable_builtins){ load_builtins("builtins/"); } @@ -1715,9 +1713,6 @@ void load_builtins(char* root){ free(path); free(stored_str); } - if(count >= 1){ - break; - } tinydir_next(&dir); } @@ -1760,9 +1755,9 @@ int main(int argc, char **str){ 1, // Verbose 1, // Introspect 0, // Mocking - 0, // Builtins - 1, // Mocking - 3 // Mocking Seed + 1, // Builtins + 0, // Mocking + 0 // Mocking Seed ); print_gnoll_errors(); FILE *f = fopen("output.dice","r"); diff --git a/src/python/code/gnoll/parser.py b/src/python/code/gnoll/parser.py index 90657e596..9314c71d0 100644 --- a/src/python/code/gnoll/parser.py +++ b/src/python/code/gnoll/parser.py @@ -86,7 +86,6 @@ def extract_from_dice_file(lines, seperator): os.remove(die_file) out_file = str(die_file).encode("ascii") - verbose = 1 if verbose: print("Rolling: ", s) print("Output in:", out_file) From 0cd6a9f4466621bd5aba47fe5286aae9a0f31c64 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 11:57:57 +0000 Subject: [PATCH 137/138] fix delete of macros --- src/grammar/dice.yacc | 5 ++--- src/grammar/operations/macros.c | 18 +++++++++--------- tests/python/test_macros.py | 1 + 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/grammar/dice.yacc b/src/grammar/dice.yacc index 4ff69f44d..cc90a0d6c 100644 --- a/src/grammar/dice.yacc +++ b/src/grammar/dice.yacc @@ -1652,7 +1652,6 @@ int roll_full_options( YY_BUFFER_STATE buffer = yy_scan_string(roll_request); yyparse(); yy_delete_buffer(buffer); - delete_all_macros(); return gnoll_errno; @@ -1752,8 +1751,8 @@ int main(int argc, char **str){ roll_full_options( s, "output.dice", - 1, // Verbose - 1, // Introspect + 0, // Verbose + 0, // Introspect 0, // Mocking 1, // Builtins 0, // Mocking diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index c29cda1d6..e478f8d13 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -37,10 +37,12 @@ void delete_all_macros() { HASH_ITER(hh, macros, current_macro, tmp) { HASH_DEL(macros, current_macro); /* delete; users advances to next */ - free_2d_array( - ¤t_macro->stored_dice_roll.symbol_pool, - current_macro->stored_dice_roll.die_sides - ); + if (current_macro->stored_dice_roll.dtype == SYMBOLIC){ + free_2d_array( + ¤t_macro->stored_dice_roll.symbol_pool, + current_macro->stored_dice_roll.die_sides + ); + } free(current_macro); /* optional- if you want to free */ } } @@ -65,7 +67,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { unsigned short int is_symbolic = to_store->dtype == SYMBOLIC; - if (verbose) printf("Macro:: Check existance\n"); + if (verbose) printf("Macro:: Check existance of %i\n", k); HASH_FIND_INT(macros, &k, s); // id already in the hash? if (s == NULL) { s = (struct macro_struct *)safe_malloc(sizeof *s); @@ -83,11 +85,9 @@ void register_macro(vec *macro_name, roll_params *to_store) { &s->stored_dice_roll.symbol_pool, to_store->symbol_pool, to_store->die_sides, MAX_SYMBOL_LENGTH); } - + }else{ + if(verbose){printf("Already Exists\n")}; } - - - } void search_macros(char *skey, roll_params *to_store) { diff --git a/tests/python/test_macros.py b/tests/python/test_macros.py index b7b6fb3c4..9c481998f 100644 --- a/tests/python/test_macros.py +++ b/tests/python/test_macros.py @@ -56,6 +56,7 @@ def test_undefined_macro(): error_handled_by_gnoll(e) +# @pytest.mark.skip("Temporary Failure") def test_predefined_macro(): r = roll("@ORACLE", builtins=True)[0] assert r in ["YES", "YES_AND", "YES_BUT", "NO", "NO_AND", "NO_BUT"] From a66da46099f1b5e584aefacaa83542cbce24cc77 Mon Sep 17 00:00:00 2001 From: Ian Hunter Date: Mon, 28 Nov 2022 11:59:09 +0000 Subject: [PATCH 138/138] fix delete of macros --- src/grammar/operations/macros.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grammar/operations/macros.c b/src/grammar/operations/macros.c index e478f8d13..01045d1a0 100644 --- a/src/grammar/operations/macros.c +++ b/src/grammar/operations/macros.c @@ -86,7 +86,7 @@ void register_macro(vec *macro_name, roll_params *to_store) { to_store->die_sides, MAX_SYMBOL_LENGTH); } }else{ - if(verbose){printf("Already Exists\n")}; + if(verbose){printf("Already Exists\n");} } }