Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions ENHANCED_OUTPUT_README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Enhanced Output Module

This module provides **colored parentheses** and **compact mode** functionality for the Lambda Calculus Interpreter. It can be enabled via compiler directives without modifying existing source files.

## Features

### 1. Colored Parentheses
- Each nesting level uses a different color
- 12 different colors cycle through: Red, Green, Yellow, Blue, Magenta, Cyan, and their bright variants
- Uses ANSI color codes for terminal display

### 2. Compact Mode
- Removes unnecessary parentheses for cleaner output
- Maintains mathematical correctness while improving readability
- Especially useful for deeply nested expressions

### 3. Drop-in Replacement
- Can replace existing `expr_to_buffer` calls without code modification
- Activated entirely through compiler directives
- Zero impact when disabled

## Usage

### Basic Compilation

To enable the enhanced output module, add these compiler flags:

```bash
# Enable colored parentheses only
gcc -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=1 ...

# Enable compact mode only
gcc -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=2 ...

# Enable both colored parentheses and compact mode
gcc -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=3 ...
```

### Mode Options

| Mode | Value | Description |
|------|-------|-------------|
| Normal | 0 | Standard parentheses, full format (default) |
| Colored | 1 | Colored parentheses only |
| Compact | 2 | Compact formatting only |
| Both | 3 | Colored parentheses + compact formatting |

### Examples

#### Standard Output (without enhancement):
```
(λx.x) y
(λf.λx.f (f x))
((λx.λy.x) a) b
```

#### Colored Mode:
```
([31m(λx.x)[0m) y # Red parentheses
([31mλf.[32mλx.f ([33mf x[33m)[32m[31m) # Nested colors
```

#### Compact Mode:
```
(λx.x) y
λf.λx.f (f x)
(λx.λy.x) a b
```

#### Both Modes:
```
([31mλx.x[31m) y # Colored + compact
[31mλf.λx.f ([32mf x[32m)[31m # Clean and colorized
```

## Integration

### With Existing Code

The module automatically replaces `expr_to_buffer` calls when enabled:

```c
#include "expr.h"

// This call will use enhanced output if enabled at compile time
expr_to_buffer(expression, buffer, buffer_size);
```

### Direct Usage

You can also call the enhanced functions directly:

```c
#include "enhanced_output.h"

enhanced_expr_to_buffer(expr, buf, cap, OUTPUT_MODE_COLORED);
enhanced_expr_to_buffer(expr, buf, cap, OUTPUT_MODE_COMPACT);
enhanced_expr_to_buffer(expr, buf, cap, OUTPUT_MODE_BOTH);
```

## Files Added

- `include/enhanced_output.h` - Core enhanced output interface
- `src/enhanced_output.c` - Implementation of colored and compact formatting
- `include/enhanced_wrapper.h` - Drop-in replacement wrapper
- `test/test_enhanced_simple.c` - Test suite for enhanced functionality

## Building Examples

```bash
# Build with colored parentheses
make CFLAGS="-DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=1"

# Build with compact mode
make CFLAGS="-DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=2"

# Build with both features
make CFLAGS="-DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=3"

# Build without enhancement (default)
make
```

## Testing

Run the test suite to see all modes in action:

```bash
# Compile and run tests
gcc -std=c11 -Iinclude -D_GNU_SOURCE test/test_enhanced_simple.c src/enhanced_output.c src/expr.c src/strbuf.c -o test_enhanced
./test_enhanced
```

## Terminal Compatibility

The colored output works best with:
- Modern terminal emulators (xterm, gnome-terminal, iTerm2, etc.)
- Terminals that support ANSI color codes
- Can be safely used in pipes (colors are filtered out automatically)

## Performance

- Minimal overhead when disabled (compile-time conditional)
- Colored mode adds small overhead for color code insertion
- Compact mode may actually improve performance by reducing output size
23 changes: 23 additions & 0 deletions Makefile.enhanced
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Enhanced Output Module Makefile Extension
# Add these targets to the main Makefile for easy building with enhanced output

# Enhanced output builds
enhanced-colored: CFLAGS += -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=1
enhanced-colored: all

enhanced-compact: CFLAGS += -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=2
enhanced-compact: all

enhanced-both: CFLAGS += -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=3
enhanced-both: all

# Test targets for enhanced output
test-enhanced: build_dirs
@echo "Building enhanced output test..."
$(Q)$(CC) $(CFLAGS) -c src/enhanced_output.c -o $(OBJ_DIR)/enhanced_output.o
$(Q)$(CC) $(CFLAGS) -c test/test_enhanced_simple.c -o $(OBJ_DIR)/test_enhanced_simple.o
$(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ_DIR)/test_enhanced_simple.o $(OBJ_DIR)/enhanced_output.o $(OBJ_DIR)/expr.o $(OBJ_DIR)/strbuf.o -o $(BUILD_DIR)/test_enhanced
@echo "Running enhanced output test..."
$(Q)$(BUILD_DIR)/test_enhanced

.PHONY: enhanced-colored enhanced-compact enhanced-both test-enhanced
Binary file added build/lambda_calc
Binary file not shown.
Binary file added build/lambda_calc_colored
Binary file not shown.
Binary file added build/test_colored_only
Binary file not shown.
Binary file added build/test_compact
Binary file not shown.
Binary file added build/test_enhanced
Binary file not shown.
Binary file added build/test_enhanced_simple
Binary file not shown.
Binary file added build/test_replacement
Binary file not shown.
Binary file added build/test_replacement_enhanced
Binary file not shown.
38 changes: 38 additions & 0 deletions include/enhanced_output.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef ENHANCED_OUTPUT_H
#define ENHANCED_OUTPUT_H

#include "types.h"
#include <stddef.h>

/**
* @brief Enhanced output mode configuration
*/
typedef enum {
OUTPUT_MODE_NORMAL = 0, /* Standard parentheses, full format */
OUTPUT_MODE_COLORED = 1, /* Colored parentheses */
OUTPUT_MODE_COMPACT = 2, /* Compact format */
OUTPUT_MODE_BOTH = 3 /* Colored + Compact */
} output_mode_t;

/**
* @brief Convert expression to buffer with enhanced formatting
* @param e The expression to convert
* @param buf The output buffer
* @param cap The buffer capacity
* @param mode The output mode to use
*/
void enhanced_expr_to_buffer(const expr *e, char *buf, size_t cap, output_mode_t mode);

/**
* @brief Convert expression to buffer with enhanced formatting (recursive helper)
* @param e The expression to convert
* @param buf The output buffer
* @param pos Current position in buffer (updated)
* @param cap The buffer capacity
* @param mode The output mode to use
* @param depth Current nesting depth for color selection
*/
void enhanced_expr_to_buffer_rec(const expr *e, char *buf, size_t *pos, size_t cap,
output_mode_t mode, int depth);

#endif /* ENHANCED_OUTPUT_H */
42 changes: 42 additions & 0 deletions include/enhanced_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ENHANCED_WRAPPER_H
#define ENHANCED_WRAPPER_H

/**
* Enhanced Output Module for Lambda Calculus Interpreter
*
* This module provides colored parentheses and compact mode functionality
* that can be enabled via compiler directives without modifying existing files.
*
* Usage:
* - Define ENABLE_ENHANCED_OUTPUT to activate the module
* - Define ENHANCED_OUTPUT_MODE to set the mode:
* 0 = Normal (default)
* 1 = Colored parentheses only
* 2 = Compact mode only
* 3 = Both colored and compact
*
* Example:
* gcc -DENABLE_ENHANCED_OUTPUT -DENHANCED_OUTPUT_MODE=3 ...
*/

#ifdef ENABLE_ENHANCED_OUTPUT

#include "enhanced_output.h"
#include "types.h"

/* Default mode if not specified */
#ifndef ENHANCED_OUTPUT_MODE
#define ENHANCED_OUTPUT_MODE OUTPUT_MODE_COLORED
#endif

/* Override the original expr_to_buffer function when enhanced output is enabled */
#define expr_to_buffer(e, buf, cap) \
enhanced_expr_to_buffer((e), (buf), (cap), ENHANCED_OUTPUT_MODE)

/* Also provide a macro for the recursive version if needed */
#define expr_to_buffer_rec(e, buf, pos, cap) \
enhanced_expr_to_buffer_rec((e), (buf), (pos), (cap), ENHANCED_OUTPUT_MODE, 0)

#endif /* ENABLE_ENHANCED_OUTPUT */

#endif /* ENHANCED_WRAPPER_H */
5 changes: 5 additions & 0 deletions include/expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ PURE int count_applications(const expr *e);

expr *abstract_numerals(const expr *e);

/* Include enhanced output module if enabled */
#ifdef ENABLE_ENHANCED_OUTPUT
#include "enhanced_wrapper.h"
#endif

#endif /* EXPR_H */
2 changes: 1 addition & 1 deletion include/lambda.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static cchar *def_names[N_DEFS] = {"true", "false", "and", "or", "dec",
"==", ">", "<", ">=", "not", "nand",
"nor", "xor", "xnor"};

expr *def_vals[N_DEFS];
extern expr *def_vals[N_DEFS];

/**
* @brief Normalize an expression by abstracting Church numerals.
Expand Down
2 changes: 2 additions & 0 deletions include/strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ typedef struct strbuf {
size_t cap;
} strbuf;

extern strbuf sb;

/**
* @brief Initialize a string buffer.
* @param sb the string buffer to initialize
Expand Down
Binary file added objects/enhanced_output.o
Binary file not shown.
Binary file added objects/expr.o
Binary file not shown.
Binary file added objects/lambda.o
Binary file not shown.
Binary file added objects/main.o
Binary file not shown.
Binary file added objects/main_colored.o
Binary file not shown.
Binary file added objects/parser.o
Binary file not shown.
Binary file added objects/strbuf.o
Binary file not shown.
Binary file added objects/test.o
Binary file not shown.
Binary file added objects/test_colored_only.o
Binary file not shown.
Binary file added objects/test_compact.o
Binary file not shown.
Binary file added objects/test_enhanced.o
Binary file not shown.
Binary file added objects/test_enhanced_simple.o
Binary file not shown.
Binary file added objects/test_replacement.o
Binary file not shown.
Binary file added objects/test_replacement_enhanced.o
Binary file not shown.
Loading