-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
836 lines (727 loc) · 24.4 KB
/
script.js
File metadata and controls
836 lines (727 loc) · 24.4 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// MeowLang Interpreter and Website Functionality
class MeowLangInterpreter {
constructor() {
this.memory = new Array(30000).fill(0);
this.pointer = 0;
this.loopStack = [];
this.output = '';
this.isRunning = false;
this.executionSteps = 0;
this.currentCommand = '';
this.status = 'Ready';
}
// Reset interpreter state
reset() {
this.memory = new Array(30000).fill(0);
this.pointer = 0;
this.loopStack = [];
this.output = '';
this.isRunning = false;
this.executionSteps = 0;
this.currentCommand = '';
this.status = 'Ready';
this.updateUI();
}
// Parse and execute MeowLang code
execute(code) {
if (this.isRunning) return;
this.reset();
this.isRunning = true;
this.status = 'Running';
this.updateUI();
const commands = this.parseCode(code);
let i = 0;
const executeStep = () => {
if (i >= commands.length || !this.isRunning) {
this.isRunning = false;
this.status = 'Completed';
this.updateUI();
return;
}
const command = commands[i];
this.currentCommand = command;
this.executionSteps++;
this.updateUI();
try {
this.executeCommand(command);
} catch (error) {
this.status = 'Error: ' + error.message;
this.isRunning = false;
this.updateUI();
return;
}
i++;
setTimeout(executeStep, 50); // Add delay for visualization
};
executeStep();
}
// Execute a single command
executeCommand(command) {
switch (command) {
case 'meow':
this.memory[this.pointer]++;
break;
case 'hiss':
this.memory[this.pointer]--;
break;
case 'purr':
this.output += String.fromCharCode(this.memory[this.pointer]);
break;
case 'left':
this.pointer = (this.pointer - 1 + this.memory.length) % this.memory.length;
break;
case 'right':
this.pointer = (this.pointer + 1) % this.memory.length;
break;
case 'yowl':
if (this.memory[this.pointer] !== 0) {
this.loopStack.push(this.executionSteps);
} else {
// Skip to matching paw
let depth = 1;
let i = this.executionSteps;
while (depth > 0 && i < this.commands.length) {
if (this.commands[i] === 'yowl') depth++;
if (this.commands[i] === 'paw') depth--;
i++;
}
this.executionSteps = i;
}
break;
case 'paw':
if (this.loopStack.length > 0) {
this.executionSteps = this.loopStack.pop() - 1;
}
break;
case 'knead':
const nextPointer = (this.pointer + 1) % this.memory.length;
this.memory[nextPointer] += this.memory[this.pointer];
break;
case 'lick':
this.memory[this.pointer] *= 2;
break;
case 'stretch':
this.memory[this.pointer] = Math.abs(this.memory[this.pointer]);
break;
case 'chase':
this.memory[this.pointer] = Math.floor(Math.random() * 10);
break;
default:
// Ignore unknown commands (comments, etc.)
break;
}
}
// Parse code into commands
parseCode(code) {
const lines = code.split('\n');
const commands = [];
for (const line of lines) {
// Remove comments (everything after 🐾)
const commentIndex = line.indexOf('🐾');
const cleanLine = commentIndex !== -1 ? line.substring(0, commentIndex) : line;
// Split by whitespace and filter out empty strings
const lineCommands = cleanLine.trim().split(/\s+/).filter(cmd => cmd.length > 0);
commands.push(...lineCommands);
}
this.commands = commands;
return commands;
}
// Update UI elements
updateUI() {
// Update status
const statusElement = document.getElementById('debug-status');
if (statusElement) {
statusElement.textContent = this.status;
statusElement.className = 'debug-value ' + this.getStatusClass();
}
// Update current command
const commandElement = document.getElementById('debug-command');
if (commandElement) {
commandElement.textContent = this.currentCommand || '-';
}
// Update loop stack
const loopsElement = document.getElementById('debug-loops');
if (loopsElement) {
loopsElement.textContent = JSON.stringify(this.loopStack);
}
// Update execution steps
const stepsElement = document.getElementById('debug-steps');
if (stepsElement) {
stepsElement.textContent = this.executionSteps;
}
// Update pointer position
const pointerElement = document.getElementById('pointer-pos');
if (pointerElement) {
pointerElement.textContent = this.pointer;
}
// Update memory size
const sizeElement = document.getElementById('memory-size');
if (sizeElement) {
sizeElement.textContent = this.memory.length;
}
// Update memory visualization
this.updateMemoryVisualization();
// Update output
this.updateOutput();
}
// Get status CSS class
getStatusClass() {
switch (this.status) {
case 'Running': return 'status-info';
case 'Completed': return 'status-success';
case 'Ready': return '';
default: return 'status-error';
}
}
// Update memory visualization
updateMemoryVisualization() {
const memoryContainer = document.getElementById('memory-visualizer');
if (!memoryContainer) return;
// Clear existing cells
memoryContainer.innerHTML = '';
// Find the range of memory to display (around current pointer)
const start = Math.max(0, this.pointer - 10);
const end = Math.min(this.memory.length, this.pointer + 11);
for (let i = start; i < end; i++) {
const cell = document.createElement('div');
cell.className = 'memory-cell';
cell.setAttribute('data-index', i);
cell.textContent = this.memory[i];
// Add special classes
if (i === this.pointer) {
cell.classList.add('current');
}
if (this.memory[i] !== 0) {
cell.classList.add('non-zero');
} else {
cell.classList.add('zero');
}
memoryContainer.appendChild(cell);
}
}
// Update output display
updateOutput() {
const outputElement = document.getElementById('output-display');
if (!outputElement) return;
if (this.output === '') {
outputElement.innerHTML = `
<div class="output-placeholder">
<i class="fas fa-terminal"></i>
<p>Program output will appear here</p>
</div>
`;
} else {
outputElement.textContent = this.output;
}
}
// Step execution
step() {
if (this.isRunning) return;
const code = document.getElementById('code-editor').value;
const commands = this.parseCode(code);
if (this.executionSteps >= commands.length) {
this.status = 'Completed';
this.updateUI();
return;
}
this.isRunning = true;
this.status = 'Stepping';
this.currentCommand = commands[this.executionSteps];
this.executeCommand(this.currentCommand);
this.executionSteps++;
this.isRunning = false;
this.status = 'Ready';
this.updateUI();
}
}
// Global variables
let interpreter = new MeowLangInterpreter();
let currentSection = 'home';
let isDarkMode = false;
// Initialize the website
document.addEventListener('DOMContentLoaded', function() {
// Hide loading screen
setTimeout(() => {
const loadingScreen = document.getElementById('loading-screen');
if (loadingScreen) {
loadingScreen.classList.add('hidden');
setTimeout(() => {
loadingScreen.style.display = 'none';
}, 500);
}
}, 1500);
// Initialize components
initializeNavigation();
initializeTheme();
initializePlayground();
initializeAnimations();
initializeExamples();
initializeStats();
initializeKeyboardShortcuts();
initializeEasterEggs();
// Initialize Prism.js for syntax highlighting
if (typeof Prism !== 'undefined') {
Prism.highlightAll();
}
});
// Navigation functionality
function initializeNavigation() {
const navLinks = document.querySelectorAll('.nav-link');
const hamburger = document.getElementById('hamburger');
const navMenu = document.getElementById('nav-menu');
// Navigation link clicks
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const section = link.getAttribute('data-section');
switchSection(section);
// Close mobile menu
if (hamburger && navMenu) {
hamburger.classList.remove('active');
navMenu.classList.remove('active');
}
});
});
// Mobile menu toggle
if (hamburger && navMenu) {
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navMenu.classList.toggle('active');
});
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
// Switch between sections
function switchSection(sectionName) {
// Hide all sections
document.querySelectorAll('.section').forEach(section => {
section.classList.remove('active');
});
// Show target section
const targetSection = document.getElementById(sectionName);
if (targetSection) {
targetSection.classList.add('active');
}
// Update navigation
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
});
const activeLink = document.querySelector(`[data-section="${sectionName}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
currentSection = sectionName;
// Special handling for playground
if (sectionName === 'playground') {
initializePlayground();
}
}
// Theme functionality
function initializeTheme() {
const themeToggle = document.getElementById('theme-toggle');
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
isDarkMode = savedTheme === 'dark';
updateTheme();
}
if (themeToggle) {
themeToggle.addEventListener('click', () => {
isDarkMode = !isDarkMode;
updateTheme();
localStorage.setItem('theme', isDarkMode ? 'dark' : 'light');
});
}
}
function updateTheme() {
const themeToggle = document.getElementById('theme-toggle');
const icon = themeToggle?.querySelector('i');
if (isDarkMode) {
document.documentElement.setAttribute('data-theme', 'dark');
if (icon) icon.className = 'fas fa-sun';
} else {
document.documentElement.setAttribute('data-theme', 'light');
if (icon) icon.className = 'fas fa-moon';
}
}
// Playground functionality
function initializePlayground() {
const runBtn = document.getElementById('run-btn');
const stepBtn = document.getElementById('step-btn');
const resetBtn = document.getElementById('reset-btn');
const clearBtn = document.getElementById('clear-btn');
const clearOutputBtn = document.getElementById('clear-output-btn');
const codeEditor = document.getElementById('code-editor');
if (runBtn) {
runBtn.addEventListener('click', () => {
const code = codeEditor.value;
if (code.trim()) {
interpreter.execute(code);
}
});
}
if (stepBtn) {
stepBtn.addEventListener('click', () => {
interpreter.step();
});
}
if (resetBtn) {
resetBtn.addEventListener('click', () => {
interpreter.reset();
});
}
if (clearBtn) {
clearBtn.addEventListener('click', () => {
codeEditor.value = '';
updateEditorInfo();
});
}
if (clearOutputBtn) {
clearOutputBtn.addEventListener('click', () => {
interpreter.output = '';
interpreter.updateOutput();
});
}
if (codeEditor) {
codeEditor.addEventListener('input', updateEditorInfo);
codeEditor.addEventListener('keydown', handleEditorKeydown);
}
// Initialize editor info
updateEditorInfo();
}
// Update editor information
function updateEditorInfo() {
const codeEditor = document.getElementById('code-editor');
const lineCount = document.getElementById('line-count');
const charCount = document.getElementById('char-count');
if (!codeEditor) return;
const text = codeEditor.value;
const lines = text.split('\n').length;
const chars = text.length;
if (lineCount) lineCount.textContent = `${lines} lines`;
if (charCount) charCount.textContent = `${chars} characters`;
}
// Handle editor keyboard shortcuts
function handleEditorKeydown(e) {
if (e.ctrlKey || e.metaKey) {
switch (e.key) {
case 'Enter':
e.preventDefault();
interpreter.execute(document.getElementById('code-editor').value);
break;
case 's':
e.preventDefault();
saveCode();
break;
case 'l':
e.preventDefault();
loadCode();
break;
}
}
}
// Save code to localStorage
function saveCode() {
const code = document.getElementById('code-editor').value;
localStorage.setItem('meowlang_code', code);
showNotification('Code saved!', 'success');
}
// Load code from localStorage
function loadCode() {
const savedCode = localStorage.getItem('meowlang_code');
if (savedCode) {
document.getElementById('code-editor').value = savedCode;
updateEditorInfo();
showNotification('Code loaded!', 'success');
}
}
// Examples functionality
function initializeExamples() {
const exampleButtons = document.querySelectorAll('.example-btn');
exampleButtons.forEach(btn => {
btn.addEventListener('click', () => {
const example = btn.getAttribute('data-example');
loadExample(example);
});
});
}
// Load example code
function loadExample(exampleName) {
const examples = {
hello: `🐾 Hello World - Countdown from 5
meow meow meow meow meow 🐾 Set to 5
yowl 🐾 Start loop
purr 🐾 Output current number
hiss 🐾 Decrement
paw 🐾 End loop when reaches 0`,
fibonacci: `🐾 Fibonacci Sequence
meow meow meow meow meow meow meow meow meow meow 🐾 Set to 10
right
meow
right
meow
left left
yowl
right right
knead
left
knead
left
hiss
paw
right right
purr`,
calculator: `🐾 Calculator - Add 5 + 3
meow meow meow meow meow 🐾 Set first number to 5
right
meow meow meow 🐾 Set second number to 3
left
knead 🐾 Add them together
right
purr 🐾 Output result`,
pattern: `🐾 Print a pattern
meow meow meow meow meow meow meow meow meow meow 🐾 Set to 10
yowl
purr 🐾 Output current number
meow meow meow meow meow meow meow meow meow meow 🐾 Add 10
purr 🐾 Output new number
hiss hiss hiss hiss hiss hiss hiss hiss hiss hiss 🐾 Subtract 10
hiss 🐾 Decrement counter
paw`,
random: `🐾 Generate random numbers
chase 🐾 Generate random number
purr 🐾 Output it
meow meow meow meow meow meow meow meow meow meow 🐾 Set to 10
yowl
chase 🐾 Generate new random
purr 🐾 Output it
hiss 🐾 Decrement counter
paw`,
advanced: `🐾 Advanced example with multiple operations
meow meow meow meow meow meow meow meow meow meow 🐾 Set to 10
right
meow meow meow meow meow 🐾 Set second cell to 5
left
knead 🐾 Add to next cell
right
lick 🐾 Multiply by 2
purr 🐾 Output result
left
stretch 🐾 Absolute value
purr 🐾 Output again`
};
const codeEditor = document.getElementById('code-editor');
if (codeEditor && examples[exampleName]) {
codeEditor.value = examples[exampleName];
updateEditorInfo();
showNotification(`Loaded ${exampleName} example!`, 'success');
}
}
// Animations
function initializeAnimations() {
// Animate stats on scroll
const stats = document.querySelectorAll('.stat-number');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const target = entry.target;
const finalValue = parseInt(target.getAttribute('data-target'));
animateNumber(target, 0, finalValue, 2000);
}
});
});
stats.forEach(stat => observer.observe(stat));
// Parallax effect for hero section
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const parallax = document.querySelector('.hero-visual');
if (parallax) {
const speed = scrolled * 0.5;
parallax.style.transform = `translateY(${speed}px)`;
}
});
}
// Animate number counting
function animateNumber(element, start, end, duration) {
const startTime = performance.now();
function updateNumber(currentTime) {
const elapsed = currentTime - startTime;
const progress = Math.min(elapsed / duration, 1);
const current = Math.floor(start + (end - start) * progress);
element.textContent = current.toLocaleString();
if (progress < 1) {
requestAnimationFrame(updateNumber);
}
}
requestAnimationFrame(updateNumber);
}
// Keyboard shortcuts
function initializeKeyboardShortcuts() {
document.addEventListener('keydown', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return;
switch (e.key) {
case '1':
switchSection('home');
break;
case '2':
switchSection('playground');
break;
case '3':
switchSection('docs');
break;
case '4':
switchSection('examples');
break;
case '5':
switchSection('about');
break;
case 't':
if (e.ctrlKey || e.metaKey) {
e.preventDefault();
document.getElementById('theme-toggle').click();
}
break;
}
});
}
// Easter eggs
function initializeEasterEggs() {
let meowCount = 0;
let lastMeowTime = 0;
document.addEventListener('keydown', (e) => {
if (e.key === 'm' || e.key === 'M') {
const now = Date.now();
if (now - lastMeowTime < 1000) {
meowCount++;
if (meowCount === 5) {
showNotification('🐱 Meow! You found the secret!', 'info');
meowCount = 0;
}
} else {
meowCount = 1;
}
lastMeowTime = now;
}
});
// Konami code
let konamiCode = [];
const konamiSequence = ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'KeyB', 'KeyA'];
document.addEventListener('keydown', (e) => {
konamiCode.push(e.code);
if (konamiCode.length > konamiSequence.length) {
konamiCode.shift();
}
if (konamiCode.join(',') === konamiSequence.join(',')) {
showNotification('🎮 Konami code activated!', 'info');
document.body.style.animation = 'rainbow 2s infinite';
setTimeout(() => {
document.body.style.animation = '';
}, 5000);
}
});
}
// Show notification
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification notification-${type}`;
notification.textContent = message;
// Add styles
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: var(--bg-card);
color: var(--text-primary);
padding: var(--spacing-md) var(--spacing-lg);
border-radius: var(--radius-lg);
box-shadow: var(--shadow-lg);
z-index: 10000;
transform: translateX(100%);
transition: transform var(--transition-normal);
border-left: 4px solid var(--${type === 'success' ? 'success' : type === 'error' ? 'error' : 'info'}-color);
`;
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 100);
// Remove after 3 seconds
setTimeout(() => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
document.body.removeChild(notification);
}, 300);
}, 3000);
}
// Auto-save functionality
setInterval(() => {
const codeEditor = document.getElementById('code-editor');
if (codeEditor && codeEditor.value.trim()) {
localStorage.setItem('meowlang_autosave', codeEditor.value);
}
}, 30000); // Auto-save every 30 seconds
// Load auto-saved code on page load
window.addEventListener('load', () => {
const savedCode = localStorage.getItem('meowlang_autosave');
const codeEditor = document.getElementById('code-editor');
if (savedCode && codeEditor && !codeEditor.value.trim()) {
codeEditor.value = savedCode;
updateEditorInfo();
}
});
// Add CSS for notifications and animations
const style = document.createElement('style');
style.textContent = `
@keyframes rainbow {
0% { filter: hue-rotate(0deg); }
100% { filter: hue-rotate(360deg); }
}
.notification {
font-weight: 500;
}
.notification-success {
border-left-color: var(--success-color) !important;
}
.notification-error {
border-left-color: var(--error-color) !important;
}
.notification-info {
border-left-color: var(--info-color) !important;
}
.notification-warning {
border-left-color: var(--warning-color) !important;
}
`;
document.head.appendChild(style);
// Performance monitoring
if ('performance' in window) {
window.addEventListener('load', () => {
setTimeout(() => {
const perfData = performance.getEntriesByType('navigation')[0];
console.log('Page load time:', perfData.loadEventEnd - perfData.loadEventStart, 'ms');
}, 0);
});
}
// Service Worker for offline functionality (if supported)
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('SW registered: ', registration);
})
.catch(registrationError => {
console.log('SW registration failed: ', registrationError);
});
});
}