commit 549fbc6b74d4605b06efd284856407a17eac0607 Author: boyreau Date: Wed Nov 4 18:00:08 2026 +0100 initial commit diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..3232c8f --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,397 @@ +# C Object-Oriented Architecture + +This project uses an object-oriented design pattern in C based on opaque pointers and vtables for method dispatch and polymorphism. + +## Project Structure + +``` +. +├── src/ # Implementation files (.c) +├── src/include/ # Protected headers (protected_*.h) +├── include/ # Public API headers (*.h) +├── CMakeLists.txt # Minimalist CMake configuration +└── CLAUDE.md # This file +``` + +## Type Definition Pattern + +Each class follows an opaque pointer pattern to hide implementation details: + +### Public Header (`include/class.h`) + +```c +/* Opaque type definition - structure details hidden from users */ +typedef struct _class_t class_t; + +/* Vtable definition with function pointers - made public for polymorphism */ +typedef struct { + void (*method_a)(const class_t *const self); + int (*method_b)(const class_t *const self, int arg); +} vtable_class_t; + +/* Dispatcher functions - the ONLY way to access methods */ +void class_method_a(const class_t *const self); +int class_method_b(const class_t *const self, int arg); + +/* Constructors */ +class_t* class_build_default(void); +class_t* class_build_logic(/* constructor arguments */); + +/* Destructor */ +void class_destroy(class_t *self); +``` + +### Protected Header (`src/include/protected_class.h`) + +```c +#ifndef PROTECTED_CLASS_H +#define PROTECTED_CLASS_H + +#include "class.h" + +/* Real structure definition - only visible to implementation and children */ +struct _class_t { + vtable_class_t vtable; /* Virtual table as first field */ + /* ... other members ... */ +}; + +/* Protected constructors for use by child classes */ +class_t* class_init_default(void); +class_t* class_init_logic(/* constructor arguments */); + +#endif +``` + +**IMPORTANT**: `protected_class.h` must NEVER be included in `class.h` or any other public `.h` file. + +### Implementation (`src/class.c`) + +```c +#include "class.h" /* For function prototypes and public typedefs */ +#include "protected_class.h" /* For structure definition */ + +/* Private (static) implementation */ +static void _class_method_a_impl(const class_t *const self) { + /* Implementation */ +} + +static int _class_method_b_impl(const class_t *const self, int arg) { + /* Implementation */ +} + +/* Default vtable with standard implementations */ +static const vtable_class_t class_default_vtable = { + .method_a = _class_method_a_impl, + .method_b = _class_method_b_impl, +}; + +/* Constructors */ +class_t* class_build_default(void) { + return class_init_default(); +} + +class_t* class_build_logic(/* args */) { + /* Validation and setup if needed */ + return class_init_logic(/* args */); +} + +/* Protected constructor with actual initialization logic */ +class_t* class_init_default(void) { + class_t *self = malloc(sizeof(class_t)); + if (!self) return NULL; + + /* Initialize vtable with default implementation */ + self->vtable = class_default_vtable; + + /* Initialize other members */ + + return self; +} + +class_t* class_init_logic(/* args */) { + /* Similar to init_default but with custom initialization */ +} + +/* Destructor */ +class_t* class_destroy(class_t *self) { + if (!self) return NULL; + /* Clean up resources */ + free(self); + return NULL; +} + +/* Dispatcher functions - delegate to vtable */ +void class_method_a(const class_t *const self) { + return self->vtable.method_a(self); +} + +int class_method_b(const class_t *const self, int arg) { + return self->vtable.method_b(self, arg); +} +``` + +## Inheritance Pattern + +### Single Inheritance via Vtable Extension + +Child classes extend a parent's vtable by embedding the parent vtable as the first field (named `super`): + +### Child Public Header (`include/child.h`) + +```c +typedef struct _child_t child_t; + +/* Child vtable includes parent vtable and new methods */ +typedef struct { + vtable_class_t super; /* Parent vtable as first field */ + void (*method_c)(const child_t *const self); +} vtable_child_t; + +/* Only dispatch NEW methods - parent methods are accessed via parent dispatcher */ +void child_method_c(const child_t *const self); + +/* Constructors */ +child_t* child_build_default(void); +child_t* child_build_logic(/* args */); + +/* Destructor */ +child_t* child_destroy(child_t *self); +``` + +### Child Protected Header (`src/include/protected_child.h`) + +```c +#ifndef PROTECTED_CHILD_H +#define PROTECTED_CHILD_H + +#include "child.h" +#include "protected_class.h" /* Include parent's protected header */ + +struct _child_t { + vtable_child_t vtable; + /* ... child-specific members ... */ +}; + +child_t* child_init_default(void); +child_t* child_init_logic(/* args */); + +#endif +``` + +### Child Implementation (`src/child.c`) + +```c +#include "child.h" +#include "protected_child.h" + +static void _child_method_c_impl(const child_t *const self) { + /* Implementation */ +} + +static const vtable_child_t child_default_vtable = { + .super = class_default_vtable, /* Use parent's default vtable */ + .method_c = _child_method_c_impl, +}; + +child_t* child_init_default(void) { + child_t *self = malloc(sizeof(child_t)); + if (!self) return NULL; + + /* Assign child vtable (which includes parent vtable) */ + self->vtable = child_default_vtable; + + /* Initialize child-specific members */ + + return self; +} + +child_t* child_destroy(child_t *self) { + if (!self) return NULL; + /* Clean up child resources */ + free(self); + return NULL; +} + +void child_method_c(const child_t *const self) { + return self->vtable.method_c(self); +} +``` + +### Using Parent Methods from Child Code + +When child code needs to call parent methods, cast and use the parent dispatcher: + +```c +/* In child implementation or user code */ +child_t *obj = child_build_default(); + +/* Call parent method through explicit cast */ +class_method_a((const class_t *const) obj); + +/* Call child method */ +child_method_c(obj); + +obj = child_destroy(obj); +``` + +**Note**: Each class only provides dispatchers for its own methods. There are NO child dispatchers that call parent dispatchers—the programmer is responsible for explicit parent method calls via casting. + +## Method Visibility + +### Private Methods (Static) + +- Declared and defined in `.c` files only +- Prefixed with `_` (e.g., `_class_method_impl`) +- Not visible outside the implementation file +- Used as vtable function implementations + +### Protected Methods + +- Declared in `src/include/protected_class.h` +- Prefixed with class name, no underscore (e.g., `class_init_default`) +- Used by child classes and implementations +- Constructors and initialization helpers typically protected + +### Public Methods + +- Declared in `include/class.h` +- Dispatcher functions for vtable methods +- User-facing constructors and destructors +- The primary API of the class + +## Virtual Table Access + +Virtual table members are **NEVER accessed directly**. Access is always through dispatcher functions: + +```c +/* WRONG - don't do this */ +self->vtable.method_a(self); + +/* RIGHT - always use dispatchers */ +class_method_a(self); +``` + +This ensures: + +- Proper abstraction and encapsulation +- Consistent method resolution +- Easy to maintain and debug + +## Default Vtable Pattern + +Each implementation file defines a static const vtable with default function pointers, avoiding manual vtable initialization in each constructor: + +```c +/* In src/class.c */ +static const vtable_class_t class_default_vtable = { + .method_a = _class_method_a_error, + .method_b = _class_method_b_default, +}; +``` + +Protected constructors then simply assign this default vtable to newly allocated instances: + +```c +class_t* class_init_default(void) { + class_t *self = malloc(sizeof(class_t)); + if (!self) return NULL; + + self->vtable = class_default_vtable; /* Assign default vtable */ + + /* Initialize other members */ + + return self; +} +``` + +This pattern: +- Reduces code duplication across multiple constructors +- Ensures vtable consistency across object instances +- Simplifies maintenance of method implementations + +## Method Naming Conventions for Base Classes + +Base classes should use specific naming conventions for default method implementations to indicate whether a method is abstract or has a default implementation: + +### Abstract Methods (No Implementation) + +Abstract methods that must be overridden by child classes are named with `_error` suffix: + +```c +static void _class_method_a_error(const class_t *const self) { + fprintf(stderr, "ERROR: method_a() must be specialized in a child class\n"); + assert(0); +} + +static const vtable_class_t class_default_vtable = { + .method_a = _class_method_a_error, + .method_b = _class_method_b_default, +}; +``` + +When an abstract method is called without being overridden, the error function prints a descriptive message and fails with `assert(0)`. + +### Methods with Default Behavior + +Methods that have a default implementation are named with `_default` suffix: + +```c +static void _class_method_b_default(const class_t *const self) { + /* Default implementation */ +} + +static const vtable_class_t class_default_vtable = { + .method_a = _class_method_a_error, + .method_b = _class_method_b_default, +}; +``` + +Child classes can override `_class_method_b_default` if they need specialized behavior, or inherit the default by reusing the parent's vtable entry. + +**Naming Summary**: +- `class_method_error` — Abstract method requiring specialization +- `class_method_default` — Method with default implementation + +## Constructor/Destructor Conventions + +### Constructors + +- `class_t* class_build_default(void)` — Public default constructor + + - Typically a thin wrapper calling protected `class_init_default()` + - May include validation before calling the protected method + +- `class_t* class_build_logic(/* args */)` — Public specialized constructor + + - For constructors with specific arguments + - Typically wraps `class_init_logic(/* args */)` + +- `class_t* class_init_default(void)` — Protected default constructor + + - Contains actual construction logic + - Allocation, vtable initialization, member setup + - Called by public constructor or child classes + +- `class_t* class_init_logic(/* args */)` — Protected specialized constructor + + - Protected variant for child classes to reuse construction logic + +### Destructors + +- `void class_destroy(class_t *self)` — Public and protected + - Returns void. + - Cleans up resources and frees memory + +## Summary Checklist + +- [ ] Structure `struct _class_t` declared in `protected_class.h` +- [ ] Typedef `typedef struct _class_t class_t;` in public `class.h` +- [ ] Vtable typedef in public `class.h` +- [ ] Vtable as first field of real structure +- [ ] Dispatcher functions in public header and implementation +- [ ] Private implementations as static functions with `_` prefix +- [ ] Protected methods (constructors) declared in protected header +- [ ] Public API (dispatchers, public constructors/destructors) in public header +- [ ] Child classes embed parent vtable as `super` field +- [ ] No `#include "protected_*.h"` in any public header +- [ ] Implementation files include both public and protected headers diff --git a/cc.pdf b/cc.pdf new file mode 100644 index 0000000..4e3234f Binary files /dev/null and b/cc.pdf differ diff --git a/devoir/CMakeLists.txt b/devoir/CMakeLists.txt new file mode 100644 index 0000000..f7182a0 --- /dev/null +++ b/devoir/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.5.0) +project(devoir VERSION 0.1.0 LANGUAGES C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS on) + +include_directories(include src/include) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) + +add_executable(devoir + src/main.c + src/stack.c + src/mne.c + src/visitor.c + src/print.c + src/add.c + src/sub.c + src/mul.c + src/division.c + src/push.c + src/program.c + src/push_counter.c +) + +if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(devoir PRIVATE + -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow + -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type + -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference) +endif() diff --git a/devoir/Lisezmoi.txt b/devoir/Lisezmoi.txt new file mode 100644 index 0000000..2dd8d44 --- /dev/null +++ b/devoir/Lisezmoi.txt @@ -0,0 +1,142 @@ +================================================================================ + Devoir M1 - Interpréteur d'Assembleur à Pile + Programmation C/C++ Sécurisée - Université de Caen +================================================================================ + +NOTE IMPORTANTE +================================================================================ +Le sujet demandait explicitement les mnémoniques PUSH, PRINT, ADD, SUB, DIV. +Le code de démonstration utilise également MUL, qui n'était pas explicitement +listé. Cette implémentation ajoute aussi MUL (Mul class) et SUB (Sub class) +par cohérence avec le code de démo et pour la complétude de l'interpréteur. +Les classes Mul et Sub suivent exactement le même modèle que Print et Add. + +================================================================================ +COMPILATION +================================================================================ + +1. Créer les dossiers de build : + $ mkdir -p build/Debug build/Release + +2. Configuration Debug (développement avec vérification d'erreurs) : + $ cd build/Debug + $ cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ../.. + + Dans CMakeCache.txt, vérifier que CMAKE_BUILD_TYPE=Debug et que + CMAKE_C_FLAGS_DEBUG contient : -g -O0 -Wall -Wextra + +3. Configuration Release (optimisation pour le rendu) : + $ cd ../Release + $ cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ../.. + + Dans CMakeCache.txt, vérifier que CMAKE_BUILD_TYPE=Release et que + CMAKE_C_FLAGS_RELEASE contient : -O2 -DNDEBUG -Wall -Wextra + +4. Compilation : + $ make + $ ./bin/Debug/devoir # pour la version Debug + $ ./bin/Release/devoir # pour la version Release + +================================================================================ +VÉRIFICATIONS +================================================================================ + +0. Compilation sans warnings (Debug et Release, gcc et clang) : + $ cd build/Debug && make clean && make 2>&1 | grep -i warning + $ cd ../Release && make clean && make 2>&1 | grep -i warning + (Aucune ligne ne doit apparaître) + +1. Exécution du programme : + $ ./bin/Release/devoir + Sortie attendue : + 14 + Number of Push mnemonics: 5 + +2. Détection des fuites mémoire avec valgrind : + $ valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 \ + ./bin/Release/devoir + Sortie attendue : + 14 + Number of Push mnemonics: 5 + ==12345== All heap blocks were freed -- no leaks are possible + ==12345== ERROR SUMMARY: 0 errors from 0 contexts + +3. Test avec Address Sanitizer et Undefined Behavior Sanitizer : + $ cd ../DebugSan + $ cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug \ + -DCMAKE_C_FLAGS_DEBUG="-g -O0 -fsanitize=address,undefined \ + -fno-omit-frame-pointer -Wall -Wextra" ../.. + $ make + $ ./bin/Debug/devoir + (Aucun rapport d'erreur) + +4. Compilation isolée de chaque en-tête (vérif d'autonomie) : + $ for h in include/*.h; do \ + gcc -std=c11 -fsyntax-only -Iinclude -Isrc/include "$h" || exit 1; \ + done + $ for h in src/include/*.h; do \ + gcc -std=c11 -fsyntax-only -Iinclude -Isrc/include "$h" || exit 1; \ + done + +5. Durcissement supplémentaire pour Release (optionnel) : + Ajouter à CMAKE_C_FLAGS_RELEASE : + -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fPIE -Wl,-pie + +================================================================================ +ARCHITECTURE +================================================================================ + +Classes abstraites (interfaces) : + - Mne : interface pour les mnémoniques (abstract) + - Visitor : classe de base pour les visiteurs (concrète, dérivable) + +Classes concrètes non dérivables : + - Stack : pile dynamique (realloc) + - Print : mnémonique PRINT + - Add : mnémonique ADD + - Sub : mnémonique SUB + - Mul : mnémonique MUL + - Division : mnémonique DIV (classe nommée "division" pour éviter div_t de ) + - Push : mnémonique PUSH (avec valeur) + - Push_Counter : visiteur qui compte les Push + +Classes dérivables : + - Program : conteneur de mnémoniques (lui-même une mnémonique) + +Patterns utilisés : + - Interpreter : chaque mnémonique interprète son code + - Visitor : visites externes sur les mnémoniques (ex. comptage) + - Abstract Factory : Program crée les mnémoniques + +================================================================================ +GESTION DES ERREURS +================================================================================ + +Préconditions (erreur du programmeur) : + - Pointeurs NULL + - Accès au-delà de la taille + Vérifiées par assert(@pre dans la documentation). + +Erreurs de données (code d'entrée incorrect) : + - Pile insuffisante pour une opération binaire → errno = EINVAL + - Division par zéro → errno = EDOM + - Débordement d'addition/soustraction/multiplication → errno = ERANGE + - Débordement de division (LONG_MIN / -1) → errno = ERANGE + - PRINT sur une pile vide → errno = EINVAL + +Toutes les opérations laissent la pile intacte en cas d'erreur. + +================================================================================ +CRÉDIT & NOTES +================================================================================ + +Implémentation complète du pattern Interpreter + Visitor pour un interpréteur +de bytecode à pile. + +Vérifications de sécurité : + - Débordements d'entiers signés avec __builtin_*_overflow + - Gestion d'erreurs via errno + - Vérification des débordements de SIZE_MAX pour realloc + - Zéro fuite mémoire (valgrind clean) + - Tous les warnings compilateur traités + - Aucune fonction interdite (VLA, gets, strcpy, alloca, etc.) diff --git a/devoir/compile_commands.json b/devoir/compile_commands.json new file mode 100644 index 0000000..b73274c --- /dev/null +++ b/devoir/compile_commands.json @@ -0,0 +1,74 @@ +[ +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/main.c.o -c /home/zo/uni/M1/C/projet/devoir/src/main.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/main.c", + "output": "CMakeFiles/devoir.dir/src/main.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/stack.c.o -c /home/zo/uni/M1/C/projet/devoir/src/stack.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/stack.c", + "output": "CMakeFiles/devoir.dir/src/stack.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/mne.c.o -c /home/zo/uni/M1/C/projet/devoir/src/mne.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/mne.c", + "output": "CMakeFiles/devoir.dir/src/mne.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/visitor.c.o -c /home/zo/uni/M1/C/projet/devoir/src/visitor.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/visitor.c", + "output": "CMakeFiles/devoir.dir/src/visitor.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/print.c.o -c /home/zo/uni/M1/C/projet/devoir/src/print.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/print.c", + "output": "CMakeFiles/devoir.dir/src/print.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/add.c.o -c /home/zo/uni/M1/C/projet/devoir/src/add.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/add.c", + "output": "CMakeFiles/devoir.dir/src/add.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/sub.c.o -c /home/zo/uni/M1/C/projet/devoir/src/sub.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/sub.c", + "output": "CMakeFiles/devoir.dir/src/sub.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/mul.c.o -c /home/zo/uni/M1/C/projet/devoir/src/mul.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/mul.c", + "output": "CMakeFiles/devoir.dir/src/mul.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/division.c.o -c /home/zo/uni/M1/C/projet/devoir/src/division.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/division.c", + "output": "CMakeFiles/devoir.dir/src/division.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/push.c.o -c /home/zo/uni/M1/C/projet/devoir/src/push.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/push.c", + "output": "CMakeFiles/devoir.dir/src/push.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/program.c.o -c /home/zo/uni/M1/C/projet/devoir/src/program.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/program.c", + "output": "CMakeFiles/devoir.dir/src/program.c.o" +}, +{ + "directory": "/home/zo/uni/M1/C/projet/devoir/build/Release", + "command": "/nix/store/c7c0yarb87cv4jxlyr02dxryrlmyi232-gcc-wrapper-15.2.0/bin/gcc -I/home/zo/uni/M1/C/projet/devoir/include -I/home/zo/uni/M1/C/projet/devoir/src/include -O3 -DNDEBUG -std=gnu11 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-function-type -Wformat=2 -Wimplicit-fallthrough -Wvla -Wnull-dereference -o CMakeFiles/devoir.dir/src/push_counter.c.o -c /home/zo/uni/M1/C/projet/devoir/src/push_counter.c", + "file": "/home/zo/uni/M1/C/projet/devoir/src/push_counter.c", + "output": "CMakeFiles/devoir.dir/src/push_counter.c.o" +} +] \ No newline at end of file diff --git a/devoir/include/add.h b/devoir/include/add.h new file mode 100644 index 0000000..5c263d0 --- /dev/null +++ b/devoir/include/add.h @@ -0,0 +1,38 @@ +/** + * @file add.h + * @brief Add mnemonic: pop two elements and push their sum. + */ + +#ifndef ADD_H +#define ADD_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Add. + * + */ +typedef struct _add_t add_t; + +/** + * @brief Add vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t add_vtable_t; + +/** + * @brief Constructs an Add mnemonic. + * + * @return A newly allocated Add mnemonic. + */ +add_t* add_build_default(void); + +/** + * @brief Destroys an Add mnemonic (via polymorphic destructor). + * + * @param self The Add mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void add_destroy(add_t *self); + +#endif /* ADD_H */ diff --git a/devoir/include/division.h b/devoir/include/division.h new file mode 100644 index 0000000..0e975f9 --- /dev/null +++ b/devoir/include/division.h @@ -0,0 +1,40 @@ +/** + * @file division.h + * @brief Division mnemonic: pop two elements and push their quotient. + * @note Class is named "division" to avoid conflict with div_t from . + * The visitor method is still called visit_div() for brevity. + */ + +#ifndef DIVISION_H +#define DIVISION_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Division. + * + */ +typedef struct _division_t division_t; + +/** + * @brief Division vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t division_vtable_t; + +/** + * @brief Constructs a Division mnemonic. + * + * @return A newly allocated Division mnemonic. + */ +division_t* division_build_default(void); + +/** + * @brief Destroys a Division mnemonic (via polymorphic destructor). + * + * @param self The Division mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void division_destroy(division_t *self); + +#endif /* DIVISION_H */ diff --git a/devoir/include/mne.h b/devoir/include/mne.h new file mode 100644 index 0000000..5e9a9fd --- /dev/null +++ b/devoir/include/mne.h @@ -0,0 +1,65 @@ +/** + * @file mne.h + * @brief Abstract interface for mnemonics (bytecode instructions). + */ + +#ifndef MNE_H +#define MNE_H + +/* Forward declarations to break circular dependencies. */ +typedef struct _stack_t stack_t; +typedef struct _visitor_t visitor_t; + +/** + * @brief Forward declaration of abstract class Mne. + * + */ +typedef struct _mne_t mne_t; + +/** + * @brief Virtual method table for Mne interface. + * + */ +typedef struct { + /** + * @brief Destroy method (polymorphic destructor). + */ + void (*destroy_fn)(mne_t *); + + /** + * @brief Interpret method: execute this mnemonic on the given stack. + */ + void (*interpret_fn)(const mne_t *const, stack_t *const); + + /** + * @brief Accept method: allow a visitor to visit this mnemonic. + */ + void (*accept_fn)(const mne_t *const, visitor_t *const); +} mne_vtable_t; + +/** + * @brief Polymorphic destructor: calls the destroy method via vtable. + * + * @param self The mnemonic (can be NULL). + */ +void mne_destroy(mne_t *self); + +/** + * @brief Interpreter method: execute this mnemonic on the given stack. + * + * @param self The mnemonic. + * @param stack The stack to interpret on. + * @pre self != NULL && stack != NULL. + */ +void mne_interpret(const mne_t *const self, stack_t *const stack); + +/** + * @brief Visitor acceptance method: allow a visitor to visit this mnemonic. + * + * @param self The mnemonic. + * @param visitor The visitor. + * @pre self != NULL && visitor != NULL. + */ +void mne_accept(const mne_t *const self, visitor_t *const visitor); + +#endif /* MNE_H */ diff --git a/devoir/include/mul.h b/devoir/include/mul.h new file mode 100644 index 0000000..c8a7d20 --- /dev/null +++ b/devoir/include/mul.h @@ -0,0 +1,38 @@ +/** + * @file mul.h + * @brief Mul mnemonic: pop two elements and push their product. + */ + +#ifndef MUL_H +#define MUL_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Mul. + * + */ +typedef struct _mul_t mul_t; + +/** + * @brief Mul vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t mul_vtable_t; + +/** + * @brief Constructs a Mul mnemonic. + * + * @return A newly allocated Mul mnemonic. + */ +mul_t* mul_build_default(void); + +/** + * @brief Destroys a Mul mnemonic (via polymorphic destructor). + * + * @param self The Mul mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void mul_destroy(mul_t *self); + +#endif /* MUL_H */ diff --git a/devoir/include/print.h b/devoir/include/print.h new file mode 100644 index 0000000..0133379 --- /dev/null +++ b/devoir/include/print.h @@ -0,0 +1,38 @@ +/** + * @file print.h + * @brief Print mnemonic: pop and display the top of stack. + */ + +#ifndef PRINT_H +#define PRINT_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Print. + * + */ +typedef struct _print_t print_t; + +/** + * @brief Print vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t print_vtable_t; + +/** + * @brief Constructs a Print mnemonic. + * + * @return A newly allocated Print mnemonic. + */ +print_t* print_build_default(void); + +/** + * @brief Destroys a Print mnemonic (via polymorphic destructor). + * + * @param self The Print mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void print_destroy(print_t *self); + +#endif /* PRINT_H */ diff --git a/devoir/include/program.h b/devoir/include/program.h new file mode 100644 index 0000000..1a2d00b --- /dev/null +++ b/devoir/include/program.h @@ -0,0 +1,127 @@ +/** + * @file program.h + * @brief Program: a container of mnemonics, itself a mnemonic (composite pattern). + * @note Program is a derivable class. + */ + +#ifndef PROGRAM_H +#define PROGRAM_H + +#include "mne.h" + +/** + * @brief Forward declaration of derivable class Program. + * + */ +typedef struct _program_t program_t; + +/** + * @brief Program vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t program_vtable_t; + +/** + * @brief Constructs a Program with empty instruction sequence. + * + * @return A newly allocated Program. + */ +program_t* program_build_default(void); + +/** + * @brief Destroys a Program and all its mnemonics. + * + * @param self The Program (can be NULL). + * @note The program owns its mnemonics; they are destroyed during cleanup. + */ +void program_destroy(program_t *self); + +/** + * @brief Adds a Print mnemonic to the end of the program sequence. + * + * @param self The Program. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_print(program_t *const self); + +/** + * @brief Adds an Add mnemonic to the end of the program sequence. + * + * @param self The Program. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_add(program_t *const self); + +/** + * @brief Adds a Sub mnemonic to the end of the program sequence. + * + * @param self The Program. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_sub(program_t *const self); + +/** + * @brief Adds a Mul mnemonic to the end of the program sequence. + * + * @param self The Program. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_mul(program_t *const self); + +/** + * @brief Adds a Division mnemonic to the end of the program sequence. + * + * @param self The Program. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_div(program_t *const self); + +/** + * @brief Adds a Push mnemonic with the given value to the end of the sequence. + * + * @param self The Program. + * @param value The value to push. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + * The mnemonic is owned by the program and destroyed with it. + */ +void program_new_push(program_t *const self, const long value); + +/** + * @brief Empties the program sequence, destroying all mnemonics. + * + * @param self The Program. + * @pre self != NULL. + */ +void program_clear(program_t *const self); + +/** + * @brief Interprets the Program: executes each mnemonic in sequence on the stack. + * + * @param self The Program. + * @param stack The stack. + * @pre self != NULL && stack != NULL. + * @note Stops at the first mnemonic that sets errno (no rollback). + */ +void program_interpret(const program_t *const self, stack_t *const stack); + +/** + * @brief Allows a visitor to visit each mnemonic in the program sequence. + * + * @param self The Program. + * @param visitor The visitor. + * @pre self != NULL && visitor != NULL. + */ +void program_accept(const program_t *const self, visitor_t *const visitor); + +#endif /* PROGRAM_H */ diff --git a/devoir/include/push.h b/devoir/include/push.h new file mode 100644 index 0000000..3a9f4b9 --- /dev/null +++ b/devoir/include/push.h @@ -0,0 +1,48 @@ +/** + * @file push.h + * @brief Push mnemonic: push an integer value onto the stack. + */ + +#ifndef PUSH_H +#define PUSH_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Push. + * + */ +typedef struct _push_t push_t; + +/** + * @brief Push vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t push_vtable_t; + +/** + * @brief Constructs a Push mnemonic with a given value. + * + * @param value The value to push. + * @return A newly allocated Push mnemonic. + */ +push_t* push_build_logic(const long value); + +/** + * @brief Destroys a Push mnemonic (via polymorphic destructor). + * + * @param self The Push mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void push_destroy(push_t *self); + +/** + * @brief Returns the value of this Push mnemonic. + * + * @param self The Push mnemonic. + * @pre self != NULL. + * @return The value to be pushed. + */ +long push_value(const push_t *const self); + +#endif /* PUSH_H */ diff --git a/devoir/include/push_counter.h b/devoir/include/push_counter.h new file mode 100644 index 0000000..69fb853 --- /dev/null +++ b/devoir/include/push_counter.h @@ -0,0 +1,47 @@ +/** + * @file push_counter.h + * @brief Push_Counter: a visitor that counts Push mnemonics. + */ + +#ifndef PUSH_COUNTER_H +#define PUSH_COUNTER_H + +#include +#include "visitor.h" + +/** + * @brief Forward declaration of non-derivable class Push_Counter. + * + */ +typedef struct _push_counter_t push_counter_t; + +/** + * @brief Push_Counter vtable is identical to Visitor vtable (no new methods). + * + */ +typedef visitor_vtable_t push_counter_vtable_t; + +/** + * @brief Constructs a Push_Counter with count initialized to 0. + * + * @return A newly allocated Push_Counter. + */ +push_counter_t* push_counter_build_default(void); + +/** + * @brief Destroys a Push_Counter. + * + * @param self The Push_Counter (can be NULL). + */ +void push_counter_destroy(push_counter_t *self); + +/** + * @brief Returns the number of Push mnemonics visited. + * + * @param self The Push_Counter. + * @pre self != NULL. + * @return The count of Push mnemonics. + */ +size_t push_counter_count(const push_counter_t *const self); + +#endif /* PUSH_COUNTER_H */ diff --git a/devoir/include/stack.h b/devoir/include/stack.h new file mode 100644 index 0000000..5d39aad --- /dev/null +++ b/devoir/include/stack.h @@ -0,0 +1,87 @@ +/** + * @file stack.h + * @brief Dynamic stack of long integers, using realloc for growth. + * @note Does not include to avoid conflicting with stack_t from POSIX. + */ + +#ifndef STACK_H +#define STACK_H + +#include +#include + +/** + * @brief Forward declaration of a non-derivable class Stack. + * + */ +typedef struct _stack_t stack_t; + +/** + * @brief Constructs a stack with default initial capacity. + * + * @return A newly allocated stack. + */ +stack_t* stack_build_default(void); + +/** + * @brief Destroys a stack and frees all allocated memory. + * + * @param self The stack (can be NULL). + */ +void stack_destroy(stack_t *self); + +/** + * @brief Returns the number of elements currently in the stack. + * + * @param self The stack. + * @pre self != NULL. + * @return The size of the stack. + */ +size_t stack_size(const stack_t *const self); + +/** + * @brief Returns the top element of the stack. + * + * @param self The stack. + * @pre self != NULL && stack_size(self) > 0. + * @return The value at the top of the stack. + */ +long stack_top(const stack_t *const self); + +/** + * @brief Removes and discards the top element from the stack. + * + * @param self The stack. + * @pre self != NULL && stack_size(self) > 0. + */ +void stack_pop(stack_t *const self); + +/** + * @brief Pushes a value on top of the stack, growing if necessary. + * + * @param self The stack. + * @param value The value to push. + * @pre self != NULL. + * @note Allocations are assumed to never fail. + */ +void stack_push(stack_t *const self, const long value); + +/** + * @brief Tests whether the stack is empty. + * + * @param self The stack. + * @pre self != NULL. + * @return true if size == 0, false otherwise. + */ +bool stack_empty(const stack_t *const self); + +/** + * @brief Empties the stack and resets its size to 0. + * + * @param self The stack. + * @pre self != NULL. + * @note Does not deallocate the internal array; capacity is retained. + */ +void stack_clear(stack_t *const self); + +#endif /* STACK_H */ diff --git a/devoir/include/sub.h b/devoir/include/sub.h new file mode 100644 index 0000000..aef0119 --- /dev/null +++ b/devoir/include/sub.h @@ -0,0 +1,38 @@ +/** + * @file sub.h + * @brief Sub mnemonic: pop two elements and push their difference. + */ + +#ifndef SUB_H +#define SUB_H + +#include "mne.h" + +/** + * @brief Forward declaration of non-derivable class Sub. + * + */ +typedef struct _sub_t sub_t; + +/** + * @brief Sub vtable is identical to base Mne vtable (no new methods). + * + */ +typedef mne_vtable_t sub_vtable_t; + +/** + * @brief Constructs a Sub mnemonic. + * + * @return A newly allocated Sub mnemonic. + */ +sub_t* sub_build_default(void); + +/** + * @brief Destroys a Sub mnemonic (via polymorphic destructor). + * + * @param self The Sub mnemonic (can be NULL). + * @note This is a convenience wrapper; normally destructed via mne_destroy(). + */ +void sub_destroy(sub_t *self); + +#endif /* SUB_H */ diff --git a/devoir/include/visitor.h b/devoir/include/visitor.h new file mode 100644 index 0000000..e7f87b5 --- /dev/null +++ b/devoir/include/visitor.h @@ -0,0 +1,134 @@ +/** + * @file visitor.h + * @brief Visitor base class for mnemonic traversal (Visitor pattern). + */ + +#ifndef VISITOR_H +#define VISITOR_H + +#include "mne.h" + +/* Forward declarations for concrete mnemonic classes. */ +typedef struct _print_t print_t; +typedef struct _add_t add_t; +typedef struct _sub_t sub_t; +typedef struct _mul_t mul_t; +typedef struct _division_t division_t; +typedef struct _push_t push_t; + +/** + * @brief Forward declaration of base class Visitor. + * + */ +typedef struct _visitor_t visitor_t; + +/** + * @brief Virtual method table for Visitor. + * + */ +typedef struct { + /** + * @brief Base class (Mne) virtual method table. + */ + mne_vtable_t super; + + /** + * @brief Visit a Print mnemonic. + */ + void (*visit_print_fn)(visitor_t *const, const print_t *const); + + /** + * @brief Visit an Add mnemonic. + */ + void (*visit_add_fn)(visitor_t *const, const add_t *const); + + /** + * @brief Visit a Sub mnemonic. + */ + void (*visit_sub_fn)(visitor_t *const, const sub_t *const); + + /** + * @brief Visit a Mul mnemonic. + */ + void (*visit_mul_fn)(visitor_t *const, const mul_t *const); + + /** + * @brief Visit a Division mnemonic. + */ + void (*visit_div_fn)(visitor_t *const, const division_t *const); + + /** + * @brief Visit a Push mnemonic. + */ + void (*visit_push_fn)(visitor_t *const, const push_t *const); +} visitor_vtable_t; + +/** + * @brief Constructs a visitor with default behavior (empty visit methods). + * + * @return A newly allocated visitor. + */ +visitor_t* visitor_build_default(void); + +/** + * @brief Polymorphic destructor: calls the destroy method via vtable. + * + * @param self The visitor (can be NULL). + */ +void visitor_destroy(visitor_t *self); + +/** + * @brief Visits a Print mnemonic. + * + * @param self The visitor. + * @param mne The Print mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_print(visitor_t *const self, const print_t *const mne); + +/** + * @brief Visits an Add mnemonic. + * + * @param self The visitor. + * @param mne The Add mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_add(visitor_t *const self, const add_t *const mne); + +/** + * @brief Visits a Sub mnemonic. + * + * @param self The visitor. + * @param mne The Sub mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_sub(visitor_t *const self, const sub_t *const mne); + +/** + * @brief Visits a Mul mnemonic. + * + * @param self The visitor. + * @param mne The Mul mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_mul(visitor_t *const self, const mul_t *const mne); + +/** + * @brief Visits a Division mnemonic. + * + * @param self The visitor. + * @param mne The Division mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_div(visitor_t *const self, const division_t *const mne); + +/** + * @brief Visits a Push mnemonic. + * + * @param self The visitor. + * @param mne The Push mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_push(visitor_t *const self, const push_t *const mne); + +#endif /* VISITOR_H */ diff --git a/devoir/src/add.c b/devoir/src/add.c new file mode 100644 index 0000000..d968639 --- /dev/null +++ b/devoir/src/add.c @@ -0,0 +1,97 @@ +/** + * @file add.c + * @brief Implementation of Add mnemonic (ADD instruction). + */ + +#include "add.h" +#include "protected_mne.h" +#include "visitor.h" +#include "stack.h" + +#include +#include +#include + +/** + * @brief Internal structure of non-derivable class Add. + */ +struct _add_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; +}; + +/* -------------------------------------------------------------------------- */ +/* add_interpret */ +/* -------------------------------------------------------------------------- */ + +static void add_interpret(const mne_t *const self, stack_t *const stack) { + + if (stack_size(stack) < 2UL) { + errno = EINVAL; + return; + } + + const long e2 = stack_top(stack); + stack_pop(stack); + const long e1 = stack_top(stack); + stack_pop(stack); + + long result = 0L; + if (__builtin_add_overflow(e1, e2, &result)) { + errno = ERANGE; + /* Restore the stack on error. */ + stack_push(stack, e1); + stack_push(stack, e2); + return; + } + + stack_push(stack, result); +} + +/* -------------------------------------------------------------------------- */ +/* add_accept */ +/* -------------------------------------------------------------------------- */ + +static void add_accept(const mne_t *const self, visitor_t *const visitor) { + + const add_t *const me = (const add_t *)self; + visitor_visit_add(visitor, me); +} + +/** + * @brief Add virtual method table. + * + */ +static const add_vtable_t add_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = add_interpret, + .accept_fn = add_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* add_build_default */ +/* -------------------------------------------------------------------------- */ + +add_t* add_build_default(void) { + add_t *const res = (add_t *)malloc(sizeof(add_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&add_vtable; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* add_destroy */ +/* -------------------------------------------------------------------------- */ + +void add_destroy(add_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} diff --git a/devoir/src/division.c b/devoir/src/division.c new file mode 100644 index 0000000..0f06063 --- /dev/null +++ b/devoir/src/division.c @@ -0,0 +1,108 @@ +/** + * @file division.c + * @brief Implementation of Division mnemonic (DIV instruction). + */ + +#include "division.h" +#include "protected_mne.h" +#include "visitor.h" +#include "stack.h" + +#include +#include +#include +#include + +/** + * @brief Internal structure of non-derivable class Division. + */ +struct _division_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; +}; + +/* -------------------------------------------------------------------------- */ +/* division_interpret */ +/* -------------------------------------------------------------------------- */ + +static void division_interpret(const mne_t *const self, stack_t *const stack) { + + if (stack_size(stack) < 2UL) { + errno = EINVAL; + return; + } + + const long e2 = stack_top(stack); + stack_pop(stack); + const long e1 = stack_top(stack); + stack_pop(stack); + + /* Check for division by zero. */ + if (e2 == 0L) { + errno = EDOM; + /* Restore the stack on error. */ + stack_push(stack, e1); + stack_push(stack, e2); + return; + } + + /* Check for LONG_MIN / -1 (signed overflow). */ + if (e1 == LONG_MIN && e2 == -1L) { + errno = ERANGE; + /* Restore the stack on error. */ + stack_push(stack, e1); + stack_push(stack, e2); + return; + } + + const long result = e1 / e2; + stack_push(stack, result); +} + +/* -------------------------------------------------------------------------- */ +/* division_accept */ +/* -------------------------------------------------------------------------- */ + +static void division_accept(const mne_t *const self, visitor_t *const visitor) { + + const division_t *const me = (const division_t *)self; + visitor_visit_div(visitor, me); +} + +/** + * @brief Division virtual method table. + * + */ +static const division_vtable_t division_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = division_interpret, + .accept_fn = division_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* division_build_default */ +/* -------------------------------------------------------------------------- */ + +division_t* division_build_default(void) { + division_t *const res = (division_t *)malloc(sizeof(division_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&division_vtable; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* division_destroy */ +/* -------------------------------------------------------------------------- */ + +void division_destroy(division_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} diff --git a/devoir/src/include/protected_mne.h b/devoir/src/include/protected_mne.h new file mode 100644 index 0000000..0ace345 --- /dev/null +++ b/devoir/src/include/protected_mne.h @@ -0,0 +1,61 @@ +/** + * @file protected_mne.h + * @brief Protected interface for abstract Mne class. + * @note This header must NOT be included from public headers or main.c. + */ + +#ifndef PROTECTED_MNE_H +#define PROTECTED_MNE_H + +#include "mne.h" + +/** + * @brief Internal structure of abstract interface Mne. + * + */ +struct _mne_t { + /** + * @brief Pointer to the virtual method table (shared by all instances of the same class). + */ + const mne_vtable_t *vtable; +}; + +/** + * @brief Initialize an abstract Mne instance. + * + * @param self The mnemonic (assumed to point to the base part of a child structure). + * @pre self != NULL. + * @note This is called by child class constructors; Mne itself has no public constructor. + */ +void mne_initialize(mne_t *const self); + +/** + * @brief Default behavior for destruction (free the instance). + * + * @param self The mnemonic. + * @pre self != NULL. + * @note Called at the end of a child's destructor, or directly by mne_destroy on a concrete child. + */ +void mne_destroy_default(mne_t *self); + +/** + * @brief Abstract method: interpret must be redefined. + * + * @param self The mnemonic. + * @param stack The stack. + * @pre self != NULL && stack != NULL. + * @note Prints an error message and fails with assert + abort. + */ +void mne_interpret_error(const mne_t *const self, stack_t *const stack); + +/** + * @brief Abstract method: accept must be redefined. + * + * @param self The mnemonic. + * @param visitor The visitor. + * @pre self != NULL && visitor != NULL. + * @note Prints an error message and fails with assert + abort. + */ +void mne_accept_error(const mne_t *const self, visitor_t *const visitor); + +#endif /* PROTECTED_MNE_H */ diff --git a/devoir/src/include/protected_program.h b/devoir/src/include/protected_program.h new file mode 100644 index 0000000..972b223 --- /dev/null +++ b/devoir/src/include/protected_program.h @@ -0,0 +1,82 @@ +/** + * @file protected_program.h + * @brief Protected interface for derivable Program class. + * @note This header must NOT be included from public headers or main.c. + */ + +#ifndef PROTECTED_PROGRAM_H +#define PROTECTED_PROGRAM_H + +#include +#include "program.h" +#include "protected_mne.h" + +/** + * @brief Internal structure of derivable class Program. + * + */ +struct _program_t { + /** + * @brief Base class (Mne) part, must be first. + */ + mne_t super; + + /** + * @brief Pointer to the virtual method table. + */ + const program_vtable_t *vtable; + + /** + * @brief Dynamic array of mnemonic pointers. + */ + mne_t **sequence; + + /** + * @brief Number of mnemonics currently in the sequence. + */ + size_t size; + + /** + * @brief Allocated capacity of the sequence array. + */ + size_t capacity; +}; + +/** + * @brief Initialize a Program with empty sequence. + * + * @param self The program. + * @pre self != NULL. + */ +void program_initialize_default(program_t *const self); + +/** + * @brief Default behavior for destruction of a Program. + * + * @param self The program (as mne_t for vtable compatibility). + * @pre self != NULL. + * @note Destroys all mnemonics, frees the sequence array, then frees the instance. + */ +void program_destroy_default(mne_t *self); + +/** + * @brief Default behavior for interpret of a Program. + * + * @param self The program (as mne_t for vtable compatibility). + * @param stack The stack. + * @pre self != NULL && stack != NULL. + * @note Interprets each mnemonic in sequence; stops at the first error. + */ +void program_interpret_default(const mne_t *const self, stack_t *const stack); + +/** + * @brief Default behavior for accept of a Program. + * + * @param self The program (as mne_t for vtable compatibility). + * @param visitor The visitor. + * @pre self != NULL && visitor != NULL. + * @note Allows the visitor to accept each mnemonic in sequence. + */ +void program_accept_default(const mne_t *const self, visitor_t *const visitor); + +#endif /* PROTECTED_PROGRAM_H */ diff --git a/devoir/src/include/protected_visitor.h b/devoir/src/include/protected_visitor.h new file mode 100644 index 0000000..6f9d57e --- /dev/null +++ b/devoir/src/include/protected_visitor.h @@ -0,0 +1,99 @@ +/** + * @file protected_visitor.h + * @brief Protected interface for derivable Visitor class. + * @note This header must NOT be included from public headers or main.c. + */ + +#ifndef PROTECTED_VISITOR_H +#define PROTECTED_VISITOR_H + +#include "visitor.h" +#include "protected_mne.h" + +/** + * @brief Internal structure of base class Visitor. + * + */ +struct _visitor_t { + /** + * @brief Base class (Mne) part, must be first. + */ + mne_t super; + + /** + * @brief Pointer to the virtual method table. + */ + const visitor_vtable_t *vtable; +}; + +/** + * @brief Initialize a Visitor with default empty visit methods. + * + * @param self The visitor. + * @pre self != NULL. + */ +void visitor_initialize_default(visitor_t *const self); + +/** + * @brief Default behavior for destruction of a Visitor (free the instance). + * + * @param self The visitor (as mne_t for vtable compatibility). + * @pre self != NULL. + */ +void visitor_destroy_default(mne_t *self); + +/** + * @brief Default behavior for visit_print (empty body). + * + * @param self The visitor. + * @param mne The Print mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_print_default(visitor_t *const self, const print_t *const mne); + +/** + * @brief Default behavior for visit_add (empty body). + * + * @param self The visitor. + * @param mne The Add mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_add_default(visitor_t *const self, const add_t *const mne); + +/** + * @brief Default behavior for visit_sub (empty body). + * + * @param self The visitor. + * @param mne The Sub mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_sub_default(visitor_t *const self, const sub_t *const mne); + +/** + * @brief Default behavior for visit_mul (empty body). + * + * @param self The visitor. + * @param mne The Mul mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_mul_default(visitor_t *const self, const mul_t *const mne); + +/** + * @brief Default behavior for visit_div (empty body). + * + * @param self The visitor. + * @param mne The Division mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_div_default(visitor_t *const self, const division_t *const mne); + +/** + * @brief Default behavior for visit_push (empty body). + * + * @param self The visitor. + * @param mne The Push mnemonic. + * @pre self != NULL && mne != NULL. + */ +void visitor_visit_push_default(visitor_t *const self, const push_t *const mne); + +#endif /* PROTECTED_VISITOR_H */ diff --git a/devoir/src/main.c b/devoir/src/main.c new file mode 100644 index 0000000..d07781e --- /dev/null +++ b/devoir/src/main.c @@ -0,0 +1,383 @@ +/** + * @file main.c + * @brief Demonstration and robustness testing of the stack-based interpreter. + * + * @return @c EXIT_SUCCESS on success, @c EXIT_FAILURE on test failure. + */ + +#include "program.h" +#include "push_counter.h" +#include "stack.h" + +#include +#include +#include +#include +#include + +/* -------------------------------------------------------------------------- */ +/* test_demo_program */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Demonstrate the main program: (2 * 5) + (3 + (5 / 4)) = 14. + * + * PUSH 2 + * PUSH 5 + * MUL + * PUSH 3 + * PUSH 5 + * PUSH 4 + * DIV + * ADD + * ADD + * PRINT + */ +static void test_demo_program(void) { + // Builds the program. + program_t *const program = program_build_default(); + + program_new_push(program, 2L); + program_new_push(program, 5L); + program_new_mul(program); + program_new_push(program, 3L); + program_new_push(program, 5L); + program_new_push(program, 4L); + program_new_div(program); + program_new_add(program); + program_new_add(program); + program_new_print(program); + + // Runs it. + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + // Counts its Push mnemonics. + push_counter_t *const counter = push_counter_build_default(); + program_accept(program, (visitor_t *)counter); + printf("Number of Push mnemonics: %zu\n", push_counter_count(counter)); + + // It's over. + push_counter_destroy(counter); + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_division_by_zero */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test error handling: division by zero (PUSH 1, PUSH 0, DIV). + * Expects errno == EDOM and stack left with 2 elements. + */ +static void test_division_by_zero(void) { + program_t *const program = program_build_default(); + program_new_push(program, 1L); + program_new_push(program, 0L); + program_new_div(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != EDOM) { + printf("FAILED: division_by_zero - expected EDOM, got %d (%s)\n", errno, + strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 2UL) { + printf("FAILED: division_by_zero - expected size 2, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_add_overflow */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test error handling: PUSH LONG_MAX, PUSH 1, ADD. + * Expects errno == ERANGE and stack left with 2 elements. + */ +static void test_add_overflow(void) { + program_t *const program = program_build_default(); + program_new_push(program, LONG_MAX); + program_new_push(program, 1L); + program_new_add(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != ERANGE) { + printf("FAILED: add_overflow - expected ERANGE, got %d (%s)\n", errno, + strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 2UL) { + printf("FAILED: add_overflow - expected size 2, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_divide_long_min_by_minus_one */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test error handling: PUSH LONG_MIN, PUSH -1, DIV. + * Expects errno == ERANGE and stack left with 2 elements. + */ +static void test_divide_long_min_by_minus_one(void) { + program_t *const program = program_build_default(); + program_new_push(program, LONG_MIN); + program_new_push(program, -1L); + program_new_div(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != ERANGE) { + printf( + "FAILED: divide_long_min_by_minus_one - expected ERANGE, got %d (%s)\n", + errno, strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 2UL) { + printf("FAILED: divide_long_min_by_minus_one - expected size 2, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_add_insufficient_stack */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test error handling: ADD on a stack with 1 element. + * Expects errno == EINVAL and stack left with 1 element. + */ +static void test_add_insufficient_stack(void) { + program_t *const program = program_build_default(); + program_new_push(program, 42L); + program_new_add(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != EINVAL) { + printf("FAILED: add_insufficient_stack - expected EINVAL, got %d (%s)\n", + errno, strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 1UL) { + printf("FAILED: add_insufficient_stack - expected size 1, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_print_empty_stack */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test error handling: PRINT on an empty stack. + * Expects errno == EINVAL and stack remains empty. + */ +static void test_print_empty_stack(void) { + program_t *const program = program_build_default(); + program_new_print(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != EINVAL) { + printf("FAILED: print_empty_stack - expected EINVAL, got %d (%s)\n", errno, + strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 0UL) { + printf("FAILED: print_empty_stack - expected size 0, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_program_clear_reuse */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test program_clear and reuse: + * Build, clear, build again with different instructions, and interpret. + */ +static void test_program_clear_reuse(void) { + program_t *const program = program_build_default(); + + // First program: PUSH 5, PRINT + program_new_push(program, 5L); + program_new_print(program); + + // Clear it. + program_clear(program); + + // Second program: PUSH 10, PUSH 20, ADD, PRINT + program_new_push(program, 10L); + program_new_push(program, 20L); + program_new_add(program); + program_new_print(program); + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != 0) { + printf("FAILED: program_clear_reuse - errno set to %d (%s)\n", errno, + strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 0UL) { + printf("FAILED: program_clear_reuse - expected empty stack, size %zu\n", + stack_size(stack)); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* test_many_push_realloc */ +/* -------------------------------------------------------------------------- */ + +/** + * @brief Test realloc by pushing many values (more than initial capacity). + * Initial capacity is 8, so push 20 values to force multiple reallocations. + */ +static void test_many_push_realloc(void) { + program_t *const program = program_build_default(); + + // Push 20 values (initial capacity is 8, so forces realloc). + for (long i = 0L; i < 20L; ++i) { + program_new_push(program, i); + } + + // Sum them all with ADD operations. + for (int i = 0; i < 19; ++i) { + program_new_add(program); + } + + stack_t *const stack = stack_build_default(); + errno = 0; + program_interpret(program, stack); + + if (errno != 0) { + printf("FAILED: many_push_realloc - errno set to %d (%s)\n", errno, + strerror(errno)); + goto cleanup; + } + + if (stack_size(stack) != 1UL) { + printf("FAILED: many_push_realloc - expected size 1, got %zu\n", + stack_size(stack)); + goto cleanup; + } + + // Expected sum: 0 + 1 + 2 + ... + 19 = 190 + const long expected = 190L; + const long actual = stack_top(stack); + if (actual != expected) { + printf("FAILED: many_push_realloc - expected sum %ld, got %ld\n", expected, + actual); + goto cleanup; + } + + printf("OK\n"); + +cleanup: + stack_destroy(stack); + program_destroy(program); +} + +/* -------------------------------------------------------------------------- */ +/* main */ +/* -------------------------------------------------------------------------- */ + +int main(void) { + // Demonstration program. + test_demo_program(); + + // Robustness tests (commented out, uncomment to verify). + // Uncomment the following lines to run robustness tests: + printf("\nRobustness Tests:\n"); + printf("division_by_zero: "); + test_division_by_zero(); + + printf("add_overflow: "); + test_add_overflow(); + + printf("divide_long_min_by_minus_one: "); + test_divide_long_min_by_minus_one(); + + printf("add_insufficient_stack: "); + test_add_insufficient_stack(); + + printf("print_empty_stack: "); + test_print_empty_stack(); + + printf("program_clear_reuse: "); + test_program_clear_reuse(); + + printf("many_push_realloc: "); + test_many_push_realloc(); + + return EXIT_SUCCESS; +} diff --git a/devoir/src/mne.c b/devoir/src/mne.c new file mode 100644 index 0000000..ce19f5c --- /dev/null +++ b/devoir/src/mne.c @@ -0,0 +1,88 @@ +/** + * @file mne.c + * @brief Implementation of abstract Mne interface. + */ + +#include "mne.h" +#include "protected_mne.h" + +#include +#include +#include + +/* -------------------------------------------------------------------------- */ +/* mne_initialize */ +/* -------------------------------------------------------------------------- */ + +void mne_initialize(mne_t *const self) { + assert(self != NULL); + self->vtable = NULL; /* To be set by the child class. */ +} + +/* -------------------------------------------------------------------------- */ +/* mne_destroy_default */ +/* -------------------------------------------------------------------------- */ + +void mne_destroy_default(mne_t *self) { + if (self == NULL) { + return; + } + + free(self); +} + +/* -------------------------------------------------------------------------- */ +/* mne_interpret_error */ +/* -------------------------------------------------------------------------- */ + +void mne_interpret_error(const mne_t *const self, stack_t *const stack) { + (void)self; + (void)stack; + fprintf(stderr, "Error: method interpret must be redefined.\n"); + assert(false); + abort(); +} + +/* -------------------------------------------------------------------------- */ +/* mne_accept_error */ +/* -------------------------------------------------------------------------- */ + +void mne_accept_error(const mne_t *const self, visitor_t *const visitor) { + (void)self; + (void)visitor; + fprintf(stderr, "Error: method accept must be redefined.\n"); + assert(false); + abort(); +} + +/* -------------------------------------------------------------------------- */ +/* mne_destroy */ +/* -------------------------------------------------------------------------- */ + +void mne_destroy(mne_t *self) { + if (self == NULL) { + return; + } + + self->vtable->destroy_fn(self); +} + +/* -------------------------------------------------------------------------- */ +/* mne_interpret */ +/* -------------------------------------------------------------------------- */ + +void mne_interpret(const mne_t *const self, stack_t *const stack) { + assert(self != NULL); + assert(stack != NULL); + self->vtable->interpret_fn(self, stack); +} + +/* -------------------------------------------------------------------------- */ +/* mne_accept */ +/* -------------------------------------------------------------------------- */ + +void mne_accept(const mne_t *const self, visitor_t *const visitor) { + assert(self != NULL); + assert(visitor != NULL); + self->vtable->accept_fn(self, visitor); +} diff --git a/devoir/src/mul.c b/devoir/src/mul.c new file mode 100644 index 0000000..32c1bf9 --- /dev/null +++ b/devoir/src/mul.c @@ -0,0 +1,97 @@ +/** + * @file mul.c + * @brief Implementation of Mul mnemonic (MUL instruction). + */ + +#include "mul.h" +#include "protected_mne.h" +#include "stack.h" +#include "visitor.h" + +#include +#include +#include + +/** + * @brief Internal structure of non-derivable class Mul. + */ +struct _mul_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; +}; + +/* -------------------------------------------------------------------------- */ +/* mul_interpret */ +/* -------------------------------------------------------------------------- */ + +static void mul_interpret(const mne_t *const self, stack_t *const stack) { + + if (stack_size(stack) < 2UL) { + errno = EINVAL; + return; + } + + const long e2 = stack_top(stack); + stack_pop(stack); + const long e1 = stack_top(stack); + stack_pop(stack); + + long result = 0L; + if (__builtin_mul_overflow(e1, e2, &result)) { + errno = ERANGE; + /* Restore the stack on error. */ + stack_push(stack, e1); + stack_push(stack, e2); + return; + } + + stack_push(stack, result); +} + +/* -------------------------------------------------------------------------- */ +/* mul_accept */ +/* -------------------------------------------------------------------------- */ + +static void mul_accept(const mne_t *const self, visitor_t *const visitor) { + + const mul_t *const me = (const mul_t *)self; + visitor_visit_mul(visitor, me); +} + +/** + * @brief Mul virtual method table. + * + */ +static const mul_vtable_t mul_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = mul_interpret, + .accept_fn = mul_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* mul_build_default */ +/* -------------------------------------------------------------------------- */ + +mul_t *mul_build_default(void) { + mul_t *const res = (mul_t *)malloc(sizeof(mul_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&mul_vtable; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* mul_destroy */ +/* -------------------------------------------------------------------------- */ + +void mul_destroy(mul_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} diff --git a/devoir/src/print.c b/devoir/src/print.c new file mode 100644 index 0000000..f8a8785 --- /dev/null +++ b/devoir/src/print.c @@ -0,0 +1,86 @@ +/** + * @file print.c + * @brief Implementation of Print mnemonic (PRINT instruction). + */ + +#include "print.h" +#include "protected_mne.h" +#include "visitor.h" +#include "stack.h" + +#include +#include +#include +#include + +/** + * @brief Internal structure of non-derivable class Print. + */ +struct _print_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; +}; + +/* -------------------------------------------------------------------------- */ +/* print_interpret */ +/* -------------------------------------------------------------------------- */ + +static void print_interpret(const mne_t *const self, stack_t *const stack) { + if (stack_empty(stack)) { + errno = EINVAL; + return; + } + + const long value = stack_top(stack); + stack_pop(stack); + + printf("%ld\n", value); +} + +/* -------------------------------------------------------------------------- */ +/* print_accept */ +/* -------------------------------------------------------------------------- */ + +static void print_accept(const mne_t *const self, visitor_t *const visitor) { + + const print_t *const me = (const print_t *)self; + visitor_visit_print(visitor, me); +} + +/** + * @brief Print virtual method table. + * + */ +static const print_vtable_t print_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = print_interpret, + .accept_fn = print_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* print_build_default */ +/* -------------------------------------------------------------------------- */ + +print_t* print_build_default(void) { + print_t *const res = (print_t *)malloc(sizeof(print_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&print_vtable; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* print_destroy */ +/* -------------------------------------------------------------------------- */ + +void print_destroy(print_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} diff --git a/devoir/src/program.c b/devoir/src/program.c new file mode 100644 index 0000000..283da8b --- /dev/null +++ b/devoir/src/program.c @@ -0,0 +1,326 @@ +/** + * @file program.c + * @brief Implementation of derivable Program class (composite pattern). + */ + +#include "program.h" +#include "protected_program.h" + +#include "print.h" +#include "add.h" +#include "sub.h" +#include "mul.h" +#include "division.h" +#include "push.h" + +#include +#include +#include +#include + +/** + * @brief Initial capacity for the sequence array. + */ +static const size_t INITIAL_CAPACITY = 8UL; + +/* -------------------------------------------------------------------------- */ +/* program_interpret_default */ +/* -------------------------------------------------------------------------- */ + +void program_interpret_default(const mne_t *const self, stack_t *const stack) { + assert(self != NULL); + assert(stack != NULL); + + const program_t *const me = (const program_t *)self; + + for (size_t i = 0UL; i < me->size; ++i) { + mne_interpret(me->sequence[i], stack); + + /* Stop on first error. */ + if (errno != 0) { + return; + } + } +} + +/* -------------------------------------------------------------------------- */ +/* program_accept_default */ +/* -------------------------------------------------------------------------- */ + +void program_accept_default(const mne_t *const self, visitor_t *const visitor) { + assert(self != NULL); + assert(visitor != NULL); + + const program_t *const me = (const program_t *)self; + + for (size_t i = 0UL; i < me->size; ++i) { + mne_accept(me->sequence[i], visitor); + } +} + +/** + * @brief Program virtual method table. + * + */ +static const program_vtable_t program_vtable = { + .destroy_fn = program_destroy_default, + .interpret_fn = program_interpret_default, + .accept_fn = program_accept_default, +}; + +/* -------------------------------------------------------------------------- */ +/* program_initialize_default */ +/* -------------------------------------------------------------------------- */ + +void program_initialize_default(program_t *const self) { + assert(self != NULL); + + mne_initialize(&self->super); + self->super.vtable = (const mne_vtable_t *)&program_vtable; + self->vtable = &program_vtable; + + self->sequence = (mne_t **)malloc(INITIAL_CAPACITY * sizeof(mne_t *)); + assert(self->sequence != NULL); + + self->size = 0UL; + self->capacity = INITIAL_CAPACITY; +} + +/* -------------------------------------------------------------------------- */ +/* program_build_default */ +/* -------------------------------------------------------------------------- */ + +program_t* program_build_default(void) { + program_t *const res = (program_t *)malloc(sizeof(program_t)); + assert(res != NULL); + + program_initialize_default(res); + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* program_destroy_default */ +/* -------------------------------------------------------------------------- */ + +void program_destroy_default(mne_t *self) { + if (self == NULL) { + return; + } + + program_t *const me = (program_t *)self; + + /* Destroy all mnemonics. */ + for (size_t i = 0UL; i < me->size; ++i) { + mne_destroy(me->sequence[i]); + } + + /* Free the sequence array. */ + free(me->sequence); + me->sequence = NULL; + + /* Free the instance. */ + mne_destroy_default(self); +} + +/* -------------------------------------------------------------------------- */ +/* program_destroy */ +/* -------------------------------------------------------------------------- */ + +void program_destroy(program_t *self) { + if (self == NULL) { + return; + } + + program_destroy_default((mne_t *)self); +} + +/* -------------------------------------------------------------------------- */ +/* program_clear */ +/* -------------------------------------------------------------------------- */ + +void program_clear(program_t *const self) { + assert(self != NULL); + + for (size_t i = 0UL; i < self->size; ++i) { + mne_destroy(self->sequence[i]); + } + + self->size = 0UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_print */ +/* -------------------------------------------------------------------------- */ + +void program_new_print(program_t *const self) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)print_build_default(); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_add */ +/* -------------------------------------------------------------------------- */ + +void program_new_add(program_t *const self) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)add_build_default(); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_sub */ +/* -------------------------------------------------------------------------- */ + +void program_new_sub(program_t *const self) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)sub_build_default(); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_mul */ +/* -------------------------------------------------------------------------- */ + +void program_new_mul(program_t *const self) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)mul_build_default(); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_div */ +/* -------------------------------------------------------------------------- */ + +void program_new_div(program_t *const self) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)division_build_default(); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_new_push */ +/* -------------------------------------------------------------------------- */ + +void program_new_push(program_t *const self, const long value) { + assert(self != NULL); + + /* Grow the sequence if necessary. */ + if (self->size >= self->capacity) { + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(mne_t *)); + + mne_t **const sequence = + (mne_t **)realloc(self->sequence, new_capacity * sizeof(mne_t *)); + assert(sequence != NULL); + + self->sequence = sequence; + self->capacity = new_capacity; + } + + /* Create and add the mnemonic. */ + self->sequence[self->size] = (mne_t *)push_build_logic(value); + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* program_interpret */ +/* -------------------------------------------------------------------------- */ + +void program_interpret(const program_t *const self, stack_t *const stack) { + assert(self != NULL); + assert(stack != NULL); + mne_interpret((const mne_t *)self, stack); +} + +/* -------------------------------------------------------------------------- */ +/* program_accept */ +/* -------------------------------------------------------------------------- */ + +void program_accept(const program_t *const self, visitor_t *const visitor) { + assert(self != NULL); + assert(visitor != NULL); + mne_accept((const mne_t *)self, visitor); +} diff --git a/devoir/src/push.c b/devoir/src/push.c new file mode 100644 index 0000000..f25ac76 --- /dev/null +++ b/devoir/src/push.c @@ -0,0 +1,93 @@ +/** + * @file push.c + * @brief Implementation of Push mnemonic (PUSH instruction). + */ + +#include "push.h" +#include "protected_mne.h" +#include "visitor.h" +#include "stack.h" + +#include +#include + +/** + * @brief Internal structure of non-derivable class Push. + */ +struct _push_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; + + /** + * @brief The value to be pushed. + */ + long value; +}; + +/* -------------------------------------------------------------------------- */ +/* push_interpret */ +/* -------------------------------------------------------------------------- */ + +static void push_interpret(const mne_t *const self, stack_t *const stack) { + + const push_t *const me = (const push_t *)self; + stack_push(stack, me->value); +} + +/* -------------------------------------------------------------------------- */ +/* push_accept */ +/* -------------------------------------------------------------------------- */ + +static void push_accept(const mne_t *const self, visitor_t *const visitor) { + + const push_t *const me = (const push_t *)self; + visitor_visit_push(visitor, me); +} + +/** + * @brief Push virtual method table. + * + */ +static const push_vtable_t push_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = push_interpret, + .accept_fn = push_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* push_build_logic */ +/* -------------------------------------------------------------------------- */ + +push_t* push_build_logic(const long value) { + push_t *const res = (push_t *)malloc(sizeof(push_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&push_vtable; + res->value = value; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* push_destroy */ +/* -------------------------------------------------------------------------- */ + +void push_destroy(push_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} + +/* -------------------------------------------------------------------------- */ +/* push_value */ +/* -------------------------------------------------------------------------- */ + +long push_value(const push_t *const self) { + assert(self != NULL); + return self->value; +} diff --git a/devoir/src/push_counter.c b/devoir/src/push_counter.c new file mode 100644 index 0000000..d6f9f92 --- /dev/null +++ b/devoir/src/push_counter.c @@ -0,0 +1,93 @@ +/** + * @file push_counter.c + * @brief Implementation of Push_Counter visitor (counts Push mnemonics). + */ + +#include "push_counter.h" +#include "protected_visitor.h" +#include "push.h" + +#include +#include + +/** + * @brief Internal structure of non-derivable class Push_Counter. + */ +struct _push_counter_t { + /** + * @brief Base class (Visitor) part. + */ + visitor_t super; + + /** + * @brief Number of Push mnemonics visited. + */ + size_t count; +}; + +/* -------------------------------------------------------------------------- */ +/* push_counter_visit_push */ +/* -------------------------------------------------------------------------- */ + +static void push_counter_visit_push(visitor_t *const self, + const push_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + + push_counter_t *const me = (push_counter_t *)self; + me->count += 1UL; +} + +/** + * @brief Push_Counter virtual method table (extends Visitor). + * + */ +static const push_counter_vtable_t push_counter_vtable = { + .super = {.destroy_fn = mne_destroy_default, + .interpret_fn = mne_interpret_error, + .accept_fn = mne_accept_error}, + .visit_print_fn = visitor_visit_print_default, + .visit_add_fn = visitor_visit_add_default, + .visit_sub_fn = visitor_visit_sub_default, + .visit_mul_fn = visitor_visit_mul_default, + .visit_div_fn = visitor_visit_div_default, + .visit_push_fn = push_counter_visit_push, +}; + +/* -------------------------------------------------------------------------- */ +/* push_counter_build_default */ +/* -------------------------------------------------------------------------- */ + +push_counter_t* push_counter_build_default(void) { + push_counter_t *const res = (push_counter_t *)malloc(sizeof(push_counter_t)); + assert(res != NULL); + + visitor_initialize_default(&res->super); + res->super.super.vtable = (const mne_vtable_t *)&push_counter_vtable; + res->super.vtable = (const visitor_vtable_t *)&push_counter_vtable; + + res->count = 0UL; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* push_counter_destroy */ +/* -------------------------------------------------------------------------- */ + +void push_counter_destroy(push_counter_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} + +/* -------------------------------------------------------------------------- */ +/* push_counter_count */ +/* -------------------------------------------------------------------------- */ + +size_t push_counter_count(const push_counter_t *const self) { + assert(self != NULL); + return self->count; +} diff --git a/devoir/src/stack.c b/devoir/src/stack.c new file mode 100644 index 0000000..b15ae5b --- /dev/null +++ b/devoir/src/stack.c @@ -0,0 +1,137 @@ +/** + * @file stack.c + * @brief Implementation of dynamic stack of long integers. + */ + +#include "stack.h" + +#include +#include +#include + +/** + * @brief Initial capacity of a new stack. + */ +static const size_t INITIAL_CAPACITY = 8UL; + +/** + * @brief Internal structure of a stack (hidden from public). + */ +struct _stack_t { + /** + * @brief Dynamic array of values. + */ + long *values; + + /** + * @brief Number of elements currently in the stack. + */ + size_t size; + + /** + * @brief Allocated capacity of the values array. + */ + size_t capacity; +}; + +/* -------------------------------------------------------------------------- */ +/* stack_build_default */ +/* -------------------------------------------------------------------------- */ + +stack_t* stack_build_default(void) { + stack_t *const res = (stack_t *)malloc(sizeof(stack_t)); + assert(res != NULL); + + res->values = (long *)malloc(INITIAL_CAPACITY * sizeof(long)); + assert(res->values != NULL); + + res->size = 0UL; + res->capacity = INITIAL_CAPACITY; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* stack_destroy */ +/* -------------------------------------------------------------------------- */ + +void stack_destroy(stack_t *self) { + if (self == NULL) { + return; + } + + free(self->values); + free(self); +} + +/* -------------------------------------------------------------------------- */ +/* stack_size */ +/* -------------------------------------------------------------------------- */ + +size_t stack_size(const stack_t *const self) { + assert(self != NULL); + return self->size; +} + +/* -------------------------------------------------------------------------- */ +/* stack_top */ +/* -------------------------------------------------------------------------- */ + +long stack_top(const stack_t *const self) { + assert(self != NULL); + assert(self->size > 0UL); + return self->values[self->size - 1UL]; +} + +/* -------------------------------------------------------------------------- */ +/* stack_pop */ +/* -------------------------------------------------------------------------- */ + +void stack_pop(stack_t *const self) { + assert(self != NULL); + assert(self->size > 0UL); + self->size -= 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* stack_push */ +/* -------------------------------------------------------------------------- */ + +void stack_push(stack_t *const self, const long value) { + assert(self != NULL); + + /* If capacity is exhausted, double it. */ + if (self->size >= self->capacity) { + /* Check for overflow before doubling. */ + assert(self->capacity <= SIZE_MAX / 2UL); + const size_t new_capacity = self->capacity * 2UL; + assert(new_capacity <= SIZE_MAX / sizeof(long)); + + long *const values = (long *)realloc(self->values, new_capacity * sizeof(long)); + assert(values != NULL); + + self->values = values; + self->capacity = new_capacity; + } + + self->values[self->size] = value; + self->size += 1UL; +} + +/* -------------------------------------------------------------------------- */ +/* stack_empty */ +/* -------------------------------------------------------------------------- */ + +bool stack_empty(const stack_t *const self) { + assert(self != NULL); + return self->size == 0UL; +} + +/* -------------------------------------------------------------------------- */ +/* stack_clear */ +/* -------------------------------------------------------------------------- */ + +void stack_clear(stack_t *const self) { + assert(self != NULL); + self->size = 0UL; +} diff --git a/devoir/src/sub.c b/devoir/src/sub.c new file mode 100644 index 0000000..99a2096 --- /dev/null +++ b/devoir/src/sub.c @@ -0,0 +1,97 @@ +/** + * @file sub.c + * @brief Implementation of Sub mnemonic (SUB instruction). + */ + +#include "sub.h" +#include "protected_mne.h" +#include "visitor.h" +#include "stack.h" + +#include +#include +#include + +/** + * @brief Internal structure of non-derivable class Sub. + */ +struct _sub_t { + /** + * @brief Base class (Mne) part. + */ + mne_t super; +}; + +/* -------------------------------------------------------------------------- */ +/* sub_interpret */ +/* -------------------------------------------------------------------------- */ + +static void sub_interpret(const mne_t *const self, stack_t *const stack) { + + if (stack_size(stack) < 2UL) { + errno = EINVAL; + return; + } + + const long e2 = stack_top(stack); + stack_pop(stack); + const long e1 = stack_top(stack); + stack_pop(stack); + + long result = 0L; + if (__builtin_sub_overflow(e1, e2, &result)) { + errno = ERANGE; + /* Restore the stack on error. */ + stack_push(stack, e1); + stack_push(stack, e2); + return; + } + + stack_push(stack, result); +} + +/* -------------------------------------------------------------------------- */ +/* sub_accept */ +/* -------------------------------------------------------------------------- */ + +static void sub_accept(const mne_t *const self, visitor_t *const visitor) { + + const sub_t *const me = (const sub_t *)self; + visitor_visit_sub(visitor, me); +} + +/** + * @brief Sub virtual method table. + * + */ +static const sub_vtable_t sub_vtable = { + .destroy_fn = mne_destroy_default, + .interpret_fn = sub_interpret, + .accept_fn = sub_accept, +}; + +/* -------------------------------------------------------------------------- */ +/* sub_build_default */ +/* -------------------------------------------------------------------------- */ + +sub_t* sub_build_default(void) { + sub_t *const res = (sub_t *)malloc(sizeof(sub_t)); + assert(res != NULL); + + mne_initialize(&res->super); + res->super.vtable = (const mne_vtable_t *)&sub_vtable; + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* sub_destroy */ +/* -------------------------------------------------------------------------- */ + +void sub_destroy(sub_t *self) { + if (self == NULL) { + return; + } + + mne_destroy_default((mne_t *)self); +} diff --git a/devoir/src/visitor.c b/devoir/src/visitor.c new file mode 100644 index 0000000..6ec40a8 --- /dev/null +++ b/devoir/src/visitor.c @@ -0,0 +1,185 @@ +/** + * @file visitor.c + * @brief Implementation of base Visitor class. + */ + +#include "visitor.h" +#include "protected_visitor.h" + +#include +#include + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_print_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_print_default(visitor_t *const self, const print_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_add_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_add_default(visitor_t *const self, const add_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_sub_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_sub_default(visitor_t *const self, const sub_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_mul_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_mul_default(visitor_t *const self, const mul_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_div_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_div_default(visitor_t *const self, const division_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_push_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_push_default(visitor_t *const self, const push_t *const mne) { + (void)self; + (void)mne; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_destroy_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_destroy_default(mne_t *self) { + mne_destroy_default(self); +} + +/** + * @brief Visitor virtual method table. + * + */ +static const visitor_vtable_t visitor_vtable = { + .super = {.destroy_fn = visitor_destroy_default, + .interpret_fn = mne_interpret_error, + .accept_fn = mne_accept_error}, + .visit_print_fn = visitor_visit_print_default, + .visit_add_fn = visitor_visit_add_default, + .visit_sub_fn = visitor_visit_sub_default, + .visit_mul_fn = visitor_visit_mul_default, + .visit_div_fn = visitor_visit_div_default, + .visit_push_fn = visitor_visit_push_default, +}; + +/* -------------------------------------------------------------------------- */ +/* visitor_initialize_default */ +/* -------------------------------------------------------------------------- */ + +void visitor_initialize_default(visitor_t *const self) { + assert(self != NULL); + + mne_initialize(&self->super); + self->super.vtable = (const mne_vtable_t *)&visitor_vtable; + self->vtable = &visitor_vtable; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_build_default */ +/* -------------------------------------------------------------------------- */ + +visitor_t* visitor_build_default(void) { + visitor_t *const res = (visitor_t *)malloc(sizeof(visitor_t)); + assert(res != NULL); + + visitor_initialize_default(res); + + return res; +} + +/* -------------------------------------------------------------------------- */ +/* visitor_destroy */ +/* -------------------------------------------------------------------------- */ + +void visitor_destroy(visitor_t *self) { + if (self == NULL) { + return; + } + + visitor_destroy_default((mne_t *)self); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_print */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_print(visitor_t *const self, const print_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_print_fn(self, mne); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_add */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_add(visitor_t *const self, const add_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_add_fn(self, mne); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_sub */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_sub(visitor_t *const self, const sub_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_sub_fn(self, mne); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_mul */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_mul(visitor_t *const self, const mul_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_mul_fn(self, mne); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_div */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_div(visitor_t *const self, const division_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_div_fn(self, mne); +} + +/* -------------------------------------------------------------------------- */ +/* visitor_visit_push */ +/* -------------------------------------------------------------------------- */ + +void visitor_visit_push(visitor_t *const self, const push_t *const mne) { + assert(self != NULL); + assert(mne != NULL); + self->vtable->visit_push_fn(self, mne); +}