-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (175 loc) · 5.63 KB
/
script.js
File metadata and controls
221 lines (175 loc) · 5.63 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
const examples = {
hello: `@module "main"
@use "io" as io
pub const main -> fn () int {
let message: *byte = "Hello, Luma!";
io::print("%s\\n", [io::str_arg(message)]);
return 0;
}`,
structs: `@module "main"
@use "io" as io
const Point -> struct {
x: int,
y: int,
};
pub const main -> fn () int {
let origin: Point = Point { x: 0, y: 0 };
io::print("Point: (%d, %d)\\n", [io::int_arg(origin.x), io::int_arg(origin.y)]);
return 0;
}`,
memory: `@module "main"
@use "io" as io
pub const main -> fn () int {
let ptr: *int = cast<*int>(alloc(sizeof<int>));
defer { free(ptr); }
*ptr = 42;
io::print("Value: %d\\n", [io::int_arg(*ptr)]);
return 0;
}`,
functions: `@module "main"
@use "io" as io
const fibonacci -> fn (n: int) int {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
pub const main -> fn () int {
io::print("Fib(10): %d\\n", [io::int_arg(fibonacci(10))]);
return 0;
}`
};
// Syntax highlighter
function highlight(code) {
const tokens = [];
function addToken(match, type, index) {
for (let token of tokens) {
if (index >= token.start && index < token.end) return;
}
tokens.push({
start: index,
end: index + match.length,
text: match,
type: type
});
}
const stringRegex = /"([^"\\]|\\.)*"/g;
let match;
while ((match = stringRegex.exec(code)) !== null) {
addToken(match[0], 'string', match.index);
}
const commentRegex = /\/\/.*/g;
while ((match = commentRegex.exec(code)) !== null) {
addToken(match[0], 'comment', match.index);
}
const moduleRegex = /@(module|use)\b/g;
while ((match = moduleRegex.exec(code)) !== null) {
addToken(match[0], 'module', match.index);
}
const functionRegex = /\b(alloc|free|sizeof|cast|print_str|print_int|fibonacci)(?=\s*\()/g;
while ((match = functionRegex.exec(code)) !== null) {
addToken(match[1], 'function', match.index);
}
const keywordRegex = /\b(pub|const|fn|let|struct|defer|if|return|as)\b/g;
while ((match = keywordRegex.exec(code)) !== null) {
addToken(match[1], 'keyword', match.index);
}
const typeRegex = /\b(int|str|void|Point)\b/g;
while ((match = typeRegex.exec(code)) !== null) {
addToken(match[1], 'type', match.index);
}
const numberRegex = /\b\d+\b/g;
while ((match = numberRegex.exec(code)) !== null) {
addToken(match[0], 'number', match.index);
}
tokens.sort((a, b) => a.start - b.start);
let result = '';
let lastIndex = 0;
for (let token of tokens) {
result += code.substring(lastIndex, token.start);
result += `<span class="${token.type}">${token.text}</span>`;
lastIndex = token.end;
}
result += code.substring(lastIndex);
return result;
}
// Initialize code examples
function initExamples() {
Object.keys(examples).forEach(key => {
const code = examples[key]
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
const highlighted = highlight(code);
document.getElementById(`code-${key}`).innerHTML = highlighted;
});
}
// Fetch latest version from GitHub
async function fetchLatestVersion() {
const versionBadge = document.getElementById('versionBadge');
const versionText = document.getElementById('versionText');
try {
const response = await fetch('https://api.github.com/repos/TheDevConnor/Luma/releases/latest');
const data = await response.json();
if (data.tag_name) {
versionText.textContent = data.tag_name;
versionBadge.classList.remove('loading');
versionBadge.href = data.html_url;
} else {
versionText.textContent = 'v1.0.0';
versionBadge.classList.remove('loading');
}
} catch (error) {
console.error('Failed to fetch version:', error);
versionText.textContent = 'Version';
versionBadge.classList.remove('loading');
}
}
// Seasonal mascot switcher
function getSeasonalMascot() {
const now = new Date();
const month = now.getMonth();
if (month === 11) {
return 'img/luma_christmas.png';
}
return 'img/luma.png';
}
function initSeasonalMascot() {
const mascotImg = document.querySelector('.mascot');
if (mascotImg) {
mascotImg.src = getSeasonalMascot();
}
}
// Theme Toggle
const themeBtn = document.getElementById('themeBtn');
const html = document.documentElement;
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (prefersDark) {
html.setAttribute('data-theme', 'dark');
}
themeBtn.addEventListener('click', () => {
const current = html.getAttribute('data-theme');
html.setAttribute('data-theme', current === 'dark' ? 'light' : 'dark');
});
// Code Example Tabs
const tabBtns = document.querySelectorAll('.tab-btn');
const codeContents = document.querySelectorAll('.code-content');
const codeLabel = document.getElementById('codeLabel');
const exampleLabels = {
'hello': 'hello.lx',
'structs': 'structs.lx',
'memory': 'memory.lx',
'functions': 'functions.lx'
};
tabBtns.forEach(btn => {
btn.addEventListener('click', () => {
const example = btn.dataset.example;
tabBtns.forEach(b => b.classList.remove('active'));
codeContents.forEach(c => c.classList.remove('active'));
btn.classList.add('active');
document.querySelector(`.code-content[data-example="${example}"]`).classList.add('active');
codeLabel.textContent = exampleLabels[example];
});
});
// Initialize on load
initExamples();
initSeasonalMascot();
fetchLatestVersion();