initial commit

This commit is contained in:
boyreau 2026-11-04 18:00:08 +01:00
commit 549fbc6b74
31 changed files with 3377 additions and 0 deletions

38
devoir/include/add.h Normal file
View 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
View 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
View 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
View 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
View 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
View 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
View 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 */

View 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
View 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
View 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
View 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 */