Session 0: Modularity Exercise Guide
Session 0: Modularity Exercise Guide
To add support for the '*' operator, both the term and evaluation modules' interfaces and implementations need modification. The term module requires provisions to recognize '*' as a term, while the evaluation module must adjust logic to handle multiplication operations during stack manipulations .
The evaluation function involves creating a stack, extracting tokens from the stream, transforming them into terms, and handling constant and '+' terms accordingly. For constants, values are pushed onto the stack. If a '+' is encountered, two values are popped, summed, and the result pushed back. This continues until the expression is evaluated and displayed .
Several functionalities are necessary, including reading the input as a string, isolating elements separated by spaces (tokens), typing tokens to classify them as either numbers or operators, and using a data structure, specifically a stack, to store and compute expressions .
If the stream interface is modified, all modules dependent on it, particularly the evaluation module, require recompilation to ensure compatibility with the updated interface. Changes in defined interfaces can affect how other modules interact with the stream .
Dependencies between the modules include the stream module needing to provide tokens for the evaluation module, which relies on the term module for token interpretation. The stack module is fundamental to both the term and evaluation modules, providing the necessary data structure for expression computation .
The Makefile should define build targets for each module, specify their dependencies, and include rules for compiling and linking the project. It must also produce a final executable called 'evaluate'. Make adjustments for changes in source files or dependencies to ensure accurate builds .
Recompilation criteria include direct dependency on the changed interface, such as using modified data types or function signatures. Recompilation is necessary if a module directly imports, relies on behaviors, or uses features altered by the interface updates .
To compile the stream module manually, the necessary command typically involves using a C compiler (e.g., gcc) with the appropriate source files and any dependencies specified. Example: 'gcc -c stream.c' .
The stream module should include an abstract type for streams, a stream_open function to return a stream from a file, a stream_close function to terminate the stream, and a stream_get_token function to retrieve successive tokens from the stream until none are left (returns NULL).
The term module involves an abstract type to represent tokens (both integer constants and operators), a string_to_term function to convert strings to terms, and is_constant and is_plus functions to check if a term is a number or the plus operator, respectively. These functionalities aid in token classification and management .