about summary refs log tree commit diff stats
path: root/forth/foreforthfourth/index.html
blob: a4400f363a50cddc9f7d06e45852a3f5d17206dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>4-Stack Forth</title>
    <style>
        body {
            font-family: 'Courier New', monospace;
            margin: 0;
            padding: 10px;
            background-color: #000;
            color: #fff;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        .container {
            max-width: 100%;
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        h1 {
            font-size: 18px;
            margin: 0 0 10px 0;
            text-align: center;
        }
        
        .help {
            background-color: #111;
            border: 1px solid #333;
            padding: 10px;
            margin-bottom: 10px;
            font-size: 12px;
            line-height: 1.3;
        }
        
        .help h2 {
            margin: 0 0 5px 0;
            font-size: 13px;
        }
        
        .help p {
            margin: 0;
        }
        
        .stacks-container {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 8px;
            margin-bottom: 10px;
            flex: 1;
            min-height: 200px;
        }
        
        .stack {
            border: 2px solid;
            padding: 8px;
            display: flex;
            flex-direction: column;
            background-color: #111;
            position: relative;
            transition: all 0.3s ease;
        }
        
        .stack.focused {
            border-width: 4px;
            box-shadow: 0 0 15px rgba(255, 255, 255, 0.3);
            transform: scale(1.02);
        }
        
        .stack-1 { border-color: #ff6b6b; }
        .stack-2 { border-color: #4ecdc4; }
        .stack-3 { border-color: #45b7d1; }
        .stack-4 { border-color: #f9ca24; }
        
        .stack h3 {
            margin: 0 0 8px 0;
            text-align: center;
            font-size: 12px;
            color: #fff;
        }
        
        .stack-items {
            flex: 1;
            display: flex;
            flex-direction: column-reverse;
            gap: 2px;
            align-items: stretch;
        }
        
        .stack-item {
            background-color: #222;
            padding: 4px 6px;
            text-align: center;
            border: 1px solid #555;
            font-size: 12px;
            word-break: break-all;
        }
        
        .input-section {
            margin-bottom: 10px;
        }
        
        .input-container {
            display: flex;
            gap: 5px;
            margin-bottom: 8px;
        }
        
        #forth-input {
            flex: 1;
            padding: 8px;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            border: 1px solid #555;
            background-color: #111;
            color: #fff;
        }
        
        #forth-input:focus {
            outline: none;
            border-color: #fff;
        }
        
        button {
            padding: 8px 12px;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            border: 1px solid #fff;
            background-color: #000;
            color: #fff;
            cursor: pointer;
        }
        
        button:hover, button:active {
            background-color: #fff;
            color: #000;
        }
        
        .output {
            background-color: #111;
            border: 1px solid #555;
            padding: 8px;
            height: 300px;
            overflow-y: auto;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            line-height: 1.3;
        }
        
        /* Semantic section styling */
        section {
            margin-bottom: 10px;
        }
        
        section h2 {
            margin: 0 0 8px 0;
            font-size: 14px;
            color: #fff;
        }
        
        .output-section h2 {
            margin-bottom: 5px;
        }
        
        .error {
            color: #fff;
        }
        
        .success {
            color: #fff;
        }
        
        /* Screen reader only content */
        .sr-only {
            position: absolute;
            width: 1px;
            height: 1px;
            padding: 0;
            margin: -1px;
            overflow: hidden;
            clip: rect(0, 0, 0, 0);
            white-space: nowrap;
            border: 0;
        }
        
        /* Mobile optimizations */
        @media (max-width: 768px) {
            body { padding: 5px; }
            
            h1 { font-size: 16px; }
            
            .help { 
                font-size: 11px; 
                padding: 8px;
            }
            
            .help h2 {
                font-size: 12px;
            }
            
            .stacks-container {
                gap: 4px;
                min-height: 150px;
            }
            
            .stack {
                padding: 6px;
            }
            
            .stack h3 {
                font-size: 11px;
                margin-bottom: 6px;
            }
            
            .stack-item {
                font-size: 11px;
                padding: 3px 4px;
            }
            
            #forth-input {
                font-size: 16px; /* Prevents zoom on iOS */
                padding: 6px;
            }
            
            button {
                font-size: 11px;
                padding: 6px 10px;
            }
            
            .output {
                height: 80px;
                font-size: 11px;
                padding: 6px;
            }
        }
        
        @media (max-width: 480px) {
            .stacks-container {
                grid-template-columns: repeat(2, 1fr);
                grid-template-rows: repeat(2, 1fr);
            }
            
            .input-container {
                flex-direction: column;
            }
            
            button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>4-Stack Forth</h1>
    </header>
    
    <main class="container">
        <section class="help" aria-label="Quick help">
            <h2>Quick Help</h2>
            <p>Type <strong>help</strong> to see all available words with documentation and stack effects. Add a word to the stack like <code>s" dup"</code> and then run <code>doc</code> to see documentation for that word.</p>
            <p><strong>Stack Focus:</strong> Use <code>focus.red</code>, <code>focus.teal</code>, <code>focus.blue</code>, or <code>focus.yellow</code> (or <code>focus.1</code>, <code>focus.2</code>, <code>focus.3</code>, <code>focus.4</code>) to switch which stack operations target. Currently focused: <span id="current-focus">Red (1)</span></p>
        </section>
        
        <section class="stacks-container" aria-label="Data stacks">
            <h2 class="sr-only">Data Stacks</h2>
            <div class="stack stack-1" role="region" aria-label="Stack 1 (Red)" id="stack-1">
                <h3>Stack 1 (Red)</h3>
                <div class="stack-items" id="stack-1-items" aria-live="polite"></div>
            </div>
            <div class="stack stack-2" role="region" aria-label="Stack 2 (Teal)" id="stack-2">
                <h3>Stack 2 (Teal)</h3>
                <div class="stack-items" id="stack-2-items" aria-live="polite"></div>
            </div>
            <div class="stack stack-3" role="region" aria-label="Stack 3 (Blue)" id="stack-3">
                <h3>Stack 3 (Blue)</h3>
                <div class="stack-items" id="stack-3-items" aria-live="polite"></div>
            </div>
            <div class="stack stack-4" role="region" aria-label="Stack 4 (Yellow)" id="stack-4">
                <h3>Stack 4 (Yellow)</h3>
                <div class="stack-items" id="stack-4-items" aria-live="polite"></div>
            </div>
        </section>
        
        <section class="input-section" aria-label="Command input">
            <h2 class="sr-only">Command Input</h2>
            <div class="input-container">
                <label for="forth-input" class="sr-only">Forth command input</label>
                <input type="text" id="forth-input" placeholder="Enter Forth commands here..." aria-describedby="input-help" />
                <button type="button" onclick="executeForth()" aria-label="Execute command">Run</button>
                <button type="button" onclick="clearAll()" aria-label="Clear all stacks">Clear</button>
            </div>
            <div id="input-help" class="sr-only">Press Enter to execute commands</div>
        </section>
        
        <section class="output-section" aria-label="Command output">
            <h2 class="sr-only">Output</h2>
            <div class="output" id="output" role="log" aria-live="polite" aria-label="Forth interpreter output"></div>
        </section>
    </main>

    <script src="forth.js"></script>
    <script>
        let forthState = ForthInterpreter.createInitialState();



        // Update visual display
        const updateDisplay = () => {
            // Update stacks
            forthState.stacks.forEach((stack, index) => {
                const container = document.getElementById(`stack-${index + 1}-items`);
                container.innerHTML = '';
                stack.forEach(item => {
                    const div = document.createElement('div');
                    div.className = 'stack-item';
                    div.textContent = item.toString();
                    container.appendChild(div);
                });
            });
            
            // Update focus indicator
            document.querySelectorAll('.stack').forEach((stack, index) => {
                if (index === forthState.focusedStack) {
                    stack.classList.add('focused');
                } else {
                    stack.classList.remove('focused');
                }
            });
            
            // Update focus text
            const focusNames = ['Red (1)', 'Teal (2)', 'Blue (3)', 'Yellow (4)'];
            document.getElementById('current-focus').textContent = focusNames[forthState.focusedStack];

            // Update output
            const outputElement = document.getElementById('output');
            outputElement.innerHTML = forthState.output
                .slice(-100) // Show last 100 messages for better help display
                .map(msg => {
                    const className = msg.startsWith('Error:') ? 'error' : 'success';
                    return `<div class="${className}">${msg}</div>`;
                })
                .join('');
            outputElement.scrollTop = outputElement.scrollHeight;
        };

        // Execute Forth commands
        const executeForth = () => {
            const input = document.getElementById('forth-input');
            const command = input.value.trim();
            
            if (command) {
                forthState = ForthInterpreter.parseAndExecute(forthState, command);
                updateDisplay();
                input.value = '';
            }
        };

        // Clear all stacks
        const clearAll = () => {
            forthState = ForthInterpreter.createInitialState();
            updateDisplay();
        };

        // Handle Enter key in input
        document.getElementById('forth-input').addEventListener('keypress', (e) => {
            if (e.key === 'Enter') {
                executeForth();
            }
        });

        // Initialize display
        updateDisplay();
    </script>
</body>
</html>