Monorepo for Aesthetic.Computer
aesthetic.computer
1#!/usr/bin/env fish
2# KidLisp Source Tree Analyzer
3# Usage: ./source-tree.fish $cow
4# Shows the embedded layer structure and source code of KidLisp pieces
5
6function print_usage
7 echo "Usage: source-tree.fish <piece-name>"
8 echo "Example: source-tree.fish \$cow"
9 echo " source-tree.fish cow"
10 echo ""
11 echo "Analyzes KidLisp pieces and shows their embedded layer tree structure."
12end
13
14function fetch_source
15 set piece_name $argv[1]
16 # Remove $ prefix if present
17 set piece_name (string replace --regex '^\$' '' $piece_name)
18
19 # Fetch from local store-kidlisp API
20 set response (curl -s -k "https://localhost:8888/.netlify/functions/store-kidlisp?code=$piece_name" 2>/dev/null)
21
22 if test $status -ne 0
23 echo "❌ Error: Could not connect to local API (https://localhost:8888)"
24 echo " Make sure the dev server is running with: npm run dev"
25 return 1
26 end
27
28 # Check if we got an error response
29 if string match -q '*"error"*' $response
30 echo "❌ Error: Piece '\$$piece_name' not found"
31 return 1
32 end
33
34 # Extract source code using string manipulation (since we don't have jq)
35 set source (echo $response | string match -r '"source":"([^"]*)"' | string replace '"source":"' '' | string replace '"' '')
36
37 if test -z "$source"
38 echo "❌ Error: Could not parse source code from response"
39 return 1
40 end
41
42 echo $source
43end
44
45function extract_embedded_pieces
46 set source $argv[1]
47 # Find all $piece references like ($39i ...) or ($r2f ...)
48 set raw_matches (echo $source | string match -ra '\(\$[a-zA-Z0-9_-]+')
49 set pieces
50 for match in $raw_matches
51 set piece (echo $match | string replace '($' '')
52 if test -n "$piece"
53 set pieces $pieces $piece
54 end
55 end
56 # Remove duplicates and return as separate arguments
57 for piece in $pieces
58 echo $piece
59 end | sort -u
60end
61
62function print_tree_node
63 set piece_name $argv[1]
64 set depth $argv[2]
65 set prefix $argv[3]
66
67 # Create indentation
68 set indent ""
69 for i in (seq 1 $depth)
70 set indent "$indent "
71 end
72
73 # Fetch source code
74 set source (fetch_source $piece_name)
75 if test $status -ne 0
76 echo "$indent$prefix❌ \$$piece_name (not found)"
77 return
78 end
79
80 # Clean up source for display (replace \n with actual newlines and unescape)
81 set clean_source (echo $source | string replace -a '\\n' '\n' | string unescape)
82
83 # Extract embedded pieces
84 set embedded_pieces (extract_embedded_pieces $source)
85
86 # Print current piece
87 if test (count $embedded_pieces) -gt 0
88 echo "$indent$prefix📁 \$$piece_name"
89 else
90 echo "$indent$prefix📄 \$$piece_name"
91 end
92
93 # Print source code (first few lines)
94 set source_lines (echo $clean_source | head -n 3)
95 for line in $source_lines
96 if test -n "$line"
97 set truncated_line (echo $line | string sub -l 60)
98 if test (string length $line) -gt 60
99 set truncated_line "$truncated_line..."
100 end
101 echo "$indent │ $truncated_line"
102 end
103 end
104
105 # If source is longer than 3 lines, show indicator
106 set total_lines (echo $clean_source | wc -l)
107 if test $total_lines -gt 3
108 echo "$indent │ ... ($total_lines lines total)"
109 end
110
111 # Process embedded pieces recursively (with depth limit)
112 if test $depth -lt 5 # Prevent infinite recursion
113 set piece_count (count $embedded_pieces)
114 for i in (seq 1 $piece_count)
115 set embedded_piece $embedded_pieces[$i]
116 if test $i -eq $piece_count
117 print_tree_node $embedded_piece (math $depth + 1) "└─ "
118 else
119 print_tree_node $embedded_piece (math $depth + 1) "├─ "
120 end
121 end
122 else
123 if test (count $embedded_pieces) -gt 0
124 echo "$indent └─ ... (max depth reached)"
125 end
126 end
127end
128
129function analyze_performance_features
130 set source $argv[1]
131 echo ""
132 echo "🔍 Performance Analysis:"
133
134 # Check for expensive operations
135 set expensive_ops
136 if string match -q '*blur*' $source
137 set expensive_ops $expensive_ops "blur"
138 end
139 if string match -q '*zoom*' $source
140 set expensive_ops $expensive_ops "zoom"
141 end
142 if string match -q '*contrast*' $source
143 set expensive_ops $expensive_ops "contrast"
144 end
145 if string match -q '*spin*' $source
146 set expensive_ops $expensive_ops "spin"
147 end
148 if string match -q '*flood*' $source
149 set expensive_ops $expensive_ops "flood"
150 end
151
152 if test (count $expensive_ops) -gt 0
153 echo " ⚠️ Expensive operations detected: "(string join ", " $expensive_ops)
154 else
155 echo " ✅ No expensive operations detected"
156 end
157
158 # Check for timing expressions
159 if string match -q '*s(*' $source
160 echo " ⏱️ Contains timing expressions (animated)"
161 end
162
163 # Check for randomness
164 if string match -q '*?*' $source
165 echo " 🎲 Contains randomness (?)"
166 end
167
168 # Count embedded layers
169 set embedded_count (extract_embedded_pieces $source | count)
170 if test $embedded_count -gt 0
171 echo " 📁 Embeds $embedded_count layer(s)"
172 end
173end
174
175# Main script
176if test (count $argv) -eq 0
177 print_usage
178 exit 1
179end
180
181set piece_name $argv[1]
182
183# Remove $ prefix if present for display
184set display_name $piece_name
185if not string match -q '\$*' $display_name
186 set display_name "\$$display_name"
187end
188
189echo "🌳 KidLisp Source Tree for $display_name"
190echo "═══════════════════════════════════════════"
191
192# Build the tree
193print_tree_node $piece_name 0 ""
194
195# Get the main source for analysis
196set main_source (fetch_source $piece_name)
197if test $status -eq 0
198 analyze_performance_features $main_source
199end
200
201echo ""
202echo "💡 Use this analysis to understand performance bottlenecks!"
203echo " Run with different pieces: source-tree.fish \$39i"