initial commit
This commit is contained in:
commit
549fbc6b74
31 changed files with 3377 additions and 0 deletions
32
devoir/CMakeLists.txt
Normal file
32
devoir/CMakeLists.txt
Normal file
|
|
@ -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()
|
||||
142
devoir/Lisezmoi.txt
Normal file
142
devoir/Lisezmoi.txt
Normal file
|
|
@ -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 <stdlib.h>)
|
||||
- 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.)
|
||||
74
devoir/compile_commands.json
Normal file
74
devoir/compile_commands.json
Normal file
|
|
@ -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"
|
||||
}
|
||||
]
|
||||
38
devoir/include/add.h
Normal file
38
devoir/include/add.h
Normal file
|
|
@ -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 */
|
||||
40
devoir/include/division.h
Normal file
40
devoir/include/division.h
Normal file
|
|
@ -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 <stdlib.h>.
|
||||
* 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 */
|
||||
65
devoir/include/mne.h
Normal file
65
devoir/include/mne.h
Normal file
|
|
@ -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 */
|
||||
38
devoir/include/mul.h
Normal file
38
devoir/include/mul.h
Normal file
|
|
@ -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 */
|
||||
38
devoir/include/print.h
Normal file
38
devoir/include/print.h
Normal file
|
|
@ -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 */
|
||||
127
devoir/include/program.h
Normal file
127
devoir/include/program.h
Normal file
|
|
@ -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 */
|
||||
48
devoir/include/push.h
Normal file
48
devoir/include/push.h
Normal file
|
|
@ -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 */
|
||||
47
devoir/include/push_counter.h
Normal file
47
devoir/include/push_counter.h
Normal file
|
|
@ -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 <stddef.h>
|
||||
#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 */
|
||||
87
devoir/include/stack.h
Normal file
87
devoir/include/stack.h
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/**
|
||||
* @file stack.h
|
||||
* @brief Dynamic stack of long integers, using realloc for growth.
|
||||
* @note Does not include <signal.h> to avoid conflicting with stack_t from POSIX.
|
||||
*/
|
||||
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @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 */
|
||||
38
devoir/include/sub.h
Normal file
38
devoir/include/sub.h
Normal file
|
|
@ -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 */
|
||||
134
devoir/include/visitor.h
Normal file
134
devoir/include/visitor.h
Normal file
|
|
@ -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 */
|
||||
97
devoir/src/add.c
Normal file
97
devoir/src/add.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
108
devoir/src/division.c
Normal file
108
devoir/src/division.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
61
devoir/src/include/protected_mne.h
Normal file
61
devoir/src/include/protected_mne.h
Normal file
|
|
@ -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 */
|
||||
82
devoir/src/include/protected_program.h
Normal file
82
devoir/src/include/protected_program.h
Normal file
|
|
@ -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 <stddef.h>
|
||||
#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 */
|
||||
99
devoir/src/include/protected_visitor.h
Normal file
99
devoir/src/include/protected_visitor.h
Normal file
|
|
@ -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 */
|
||||
383
devoir/src/main.c
Normal file
383
devoir/src/main.c
Normal file
|
|
@ -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 <errno.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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;
|
||||
}
|
||||
88
devoir/src/mne.c
Normal file
88
devoir/src/mne.c
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* @file mne.c
|
||||
* @brief Implementation of abstract Mne interface.
|
||||
*/
|
||||
|
||||
#include "mne.h"
|
||||
#include "protected_mne.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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);
|
||||
}
|
||||
97
devoir/src/mul.c
Normal file
97
devoir/src/mul.c
Normal file
|
|
@ -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 <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
86
devoir/src/print.c
Normal file
86
devoir/src/print.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
326
devoir/src/program.c
Normal file
326
devoir/src/program.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include <errno.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
93
devoir/src/push.c
Normal file
93
devoir/src/push.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
93
devoir/src/push_counter.c
Normal file
93
devoir/src/push_counter.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
137
devoir/src/stack.c
Normal file
137
devoir/src/stack.c
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
/**
|
||||
* @file stack.c
|
||||
* @brief Implementation of dynamic stack of long integers.
|
||||
*/
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
97
devoir/src/sub.c
Normal file
97
devoir/src/sub.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
185
devoir/src/visitor.c
Normal file
185
devoir/src/visitor.c
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
/**
|
||||
* @file visitor.c
|
||||
* @brief Implementation of base Visitor class.
|
||||
*/
|
||||
|
||||
#include "visitor.h"
|
||||
#include "protected_visitor.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue