You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### 2025-11-09: Macro System Enhancements & REPL Improvements
877
+
878
+
**Status:** ✅ Completed
879
+
880
+
**Summary:**
881
+
Major enhancements to the macro system and REPL, adding conditional macro expansion, macro libraries, persistent state, and introspection commands.
882
+
883
+
**Motivation:**
884
+
The macro system needed more flexibility for real-world use cases like debug logging and feature flags. The REPL needed persistent state and better introspection to be practical for interactive development and testing.
885
+
886
+
**Implementation:**
887
+
888
+
#### Part 1: Macro System Enhancements
889
+
890
+
**Features Added:**
891
+
892
+
1.**Conditional Macro Expansion**
893
+
- Syntax: `macro name if condition param do ... end`
894
+
- Macros only expand when condition variable is non-zero
895
+
- Checked at macro expansion time using `InterpreterState` variables
896
+
- Useful for debug logging, feature flags, and conditional compilation
897
+
898
+
2.**Macro Library Loading**
899
+
- New static method: `MacroHandler.load_macro_library(library_name, state, base_dir)`
900
+
- Loads macro definitions from external `.tl` files
901
+
- Merges macros into state's macro collection
902
+
- Enables code reuse and organization
903
+
904
+
3.**Enhanced Macro Definition**
905
+
- Extended `MacroDefinition` dataclass with optional `condition` field
906
+
- Enhanced `_collect_macros` to parse `if condition` syntax
907
+
- Added `_check_condition` method for conditional evaluation
908
+
- Maintains backward compatibility with unconditional macros
909
+
910
+
**Code Changes:**
911
+
912
+
```python
913
+
# Enhanced MacroDefinition
914
+
@dataclass
915
+
classMacroDefinition:
916
+
name: str
917
+
parameters: List[str]
918
+
body: List[str]
919
+
condition: Optional[str] =None# NEW: for conditional expansion
- Comprehensive macro library demonstrating new features
1052
+
- 12+ utility macros:
1053
+
-`debug_log` - Conditional debug logging
1054
+
-`repeat_cmd` - Repeat commands N times
1055
+
-`assert` - Simple assertion macro
1056
+
-`inc_by`, `dec_by` - Increment/decrement by amount
1057
+
-`swap` - Swap two variables
1058
+
-`log_info`, `log_error`, `log_warn` - Logging with prefixes
1059
+
-`safe_div` - Division with zero-check
1060
+
-`clamp_value` - Clamp between min/max
1061
+
1062
+
3.**docs/macros-advanced.md** (~300 lines):
1063
+
- Complete guide to enhanced macro system
1064
+
- Conditional macros documentation
1065
+
- Macro libraries guide
1066
+
- Nested macro examples
1067
+
- Best practices and patterns
1068
+
- Common use cases and limitations
1069
+
1070
+
4.**docs/repl-guide.md** (~400 lines):
1071
+
- Comprehensive REPL documentation
1072
+
- All meta-commands with examples
1073
+
- Persistent state explanation
1074
+
- Keyboard shortcuts reference
1075
+
- Common workflows and tips
1076
+
- Troubleshooting guide
1077
+
1078
+
#### Validation
1079
+
1080
+
- ✅ All 6 macro tests passing (including new conditional test)
1081
+
- ✅ Full test suite: 233+ tests passing (no regressions)
1082
+
- ✅ REPL features tested manually
1083
+
- ✅ Macro library example runs successfully
1084
+
- ✅ Documentation complete and accurate
1085
+
1086
+
#### Technical Notes
1087
+
1088
+
**Macro Expansion Timing:**
1089
+
- Macros are processed at compile time (before execution)
1090
+
- Conditional macros check variables that exist when `process_macros()` is called
1091
+
- In regular file execution, variables don't exist yet (all processed at once)
1092
+
- In REPL with persistent state, variables from previous commands are available
1093
+
- This means conditional macros are most useful in REPL or with persistent state
1094
+
1095
+
**REPL Execution Model:**
1096
+
```
1097
+
Before (v1.0): run(code) → new state each time
1098
+
After (v1.1): parse → process_macros → execute → persist state
1099
+
```
1100
+
1101
+
**Design Decisions:**
1102
+
1103
+
1.**Compile-time conditional checking**: Decided to keep macros as compile-time feature rather than runtime, accepting limitation that conditionals check pre-execution state
1104
+
1105
+
2.**REPL state persistence**: Changed from creating new state per command to maintaining single state across session - major usability improvement
1106
+
1107
+
3.**Meta-command prefix**: Used `:` prefix for REPL commands (matches common REPL conventions like IPython)
1108
+
1109
+
4.**Library loading**: `load_macro_library` only registers macros, doesn't execute other commands (unlike `:load` which runs entire file)
1110
+
1111
+
5.**Documentation separation**: Created dedicated docs for macros and REPL rather than embedding in existing files
1112
+
1113
+
#### Known Limitations
1114
+
1115
+
1.**Conditional macro timing**: Conditions checked at macro processing time, not at inline expansion time
1116
+
2.**Simple condition checking**: Only checks if variable is non-zero (no complex expressions like `>`, `<`, etc.)
1117
+
3.**No macro overloading**: Cannot define multiple macros with same name
1118
+
4.**Fixed parameters**: No variadic macros (variable number of arguments)
1119
+
5.**No runtime expansion**: Macros cannot be defined or expanded during execution
0 commit comments