kuza-lang 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -0
- package/dist/i18n.js +9 -0
- package/dist/index.js +166 -9
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -52,10 +52,73 @@ log >> "Осталось: {len >> users}";
|
|
|
52
52
|
- строки с подстановками `"Привет, {name}!"`, лямбды `(x) -> x * 2`, цепочки `|>`;
|
|
53
53
|
- 100+ встроенных команд: строки, массивы, JSON, файлы, HTTP, время, DOM;
|
|
54
54
|
- таблицы: `table<...>`, `select`, `update`, `delete`, сохранение в JSON;
|
|
55
|
+
- сайты: `body`, `box`, `row`, `column`, `card`, `section`, `nav` (вложенные блоки),
|
|
56
|
+
а внутри — `heading`, `para`, `button`, `field`, `list`, `link`, `image`, `render`;
|
|
55
57
|
- модули `use math;`, `use time;`, `use "./helpers.kuza";`.
|
|
56
58
|
|
|
57
59
|
Полный список команд: `docs/commands.md` в репозитории языка.
|
|
58
60
|
|
|
61
|
+
## Сайты прямо на Kuza
|
|
62
|
+
|
|
63
|
+
Каркас — обычный `index.html`, а в `body` стоит метка, куда попадёт программа:
|
|
64
|
+
|
|
65
|
+
```html
|
|
66
|
+
<body>
|
|
67
|
+
{main.kuza}
|
|
68
|
+
</body>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Создать такой проект: `kuza init --web`. Дальше страница собирается из вложенных блоков —
|
|
72
|
+
как компоненты в React, только без сборщика и зависимостей:
|
|
73
|
+
|
|
74
|
+
```kuza
|
|
75
|
+
:count = 0;
|
|
76
|
+
|
|
77
|
+
func Card<t> >> [
|
|
78
|
+
card >> [
|
|
79
|
+
heading >> t;
|
|
80
|
+
para >> "Компонент — обычная функция Kuza.";
|
|
81
|
+
];
|
|
82
|
+
];
|
|
83
|
+
|
|
84
|
+
func draw<> >> [
|
|
85
|
+
page >> "Мой сайт";
|
|
86
|
+
|
|
87
|
+
body >> [
|
|
88
|
+
heading >> "Привет! Это сайт на Kuza";
|
|
89
|
+
|
|
90
|
+
row >> [
|
|
91
|
+
Card<"Первый">;
|
|
92
|
+
Card<"Второй">;
|
|
93
|
+
];
|
|
94
|
+
|
|
95
|
+
column >> [
|
|
96
|
+
heading >> "Счётчик: {count}";
|
|
97
|
+
button >> <"Прибавить", increase>;
|
|
98
|
+
];
|
|
99
|
+
];
|
|
100
|
+
|
|
101
|
+
render;
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
func increase<> >> [
|
|
105
|
+
count = count + 1;
|
|
106
|
+
draw<>;
|
|
107
|
+
];
|
|
108
|
+
|
|
109
|
+
draw<>;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```bash
|
|
113
|
+
kuza build main.kuza --web # → main.html, код вставляется в метку {main.kuza}
|
|
114
|
+
kuza watch main.kuza --web # пересборка при каждом сохранении
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Контейнеры: `body`, `section`, `nav`, `row`, `column` (алиас `col`), `box`, `card`.
|
|
118
|
+
Внутри — `heading`, `para`, `list`, `button`, `field` + `fieldValue`, `link`, `image`.
|
|
119
|
+
`page` задаёт заголовок вкладки, `render` рисует собранное после изменения состояния.
|
|
120
|
+
Обычный консольный код (`log`, `for`, `try`) работает в том же файле.
|
|
121
|
+
|
|
59
122
|
## Сборка
|
|
60
123
|
|
|
61
124
|
`kuza build main.kuza` создаёт самостоятельный JavaScript-файл с рантаймом —
|
package/dist/i18n.js
CHANGED
|
@@ -13,6 +13,9 @@ const dictionaries = {
|
|
|
13
13
|
running: "Запускаю",
|
|
14
14
|
built: "Собрано в",
|
|
15
15
|
runWithNode: "Запустить можно так",
|
|
16
|
+
openInBrowser: "Открой в браузере",
|
|
17
|
+
watching: "Слежу за файлом. Остановить: Ctrl+C",
|
|
18
|
+
watchChanged: "Файл изменён — пересобираю",
|
|
16
19
|
compileError: "Ошибка компиляции",
|
|
17
20
|
runtimeError: "Ошибка выполнения",
|
|
18
21
|
initCreated: "Создан файл",
|
|
@@ -67,6 +70,9 @@ const dictionaries = {
|
|
|
67
70
|
running: "Ishga tushiryapman",
|
|
68
71
|
built: "Yig'ildi",
|
|
69
72
|
runWithNode: "Bunday ishga tushirish mumkin",
|
|
73
|
+
openInBrowser: "Brauzerda oching",
|
|
74
|
+
watching: "Faylni kuzatyapman. To'xtatish: Ctrl+C",
|
|
75
|
+
watchChanged: "Fayl o'zgardi — qayta yig'aman",
|
|
70
76
|
compileError: "Kompilyatsiya xatosi",
|
|
71
77
|
runtimeError: "Bajarish xatosi",
|
|
72
78
|
initCreated: "Fayl yaratildi",
|
|
@@ -121,6 +127,9 @@ const dictionaries = {
|
|
|
121
127
|
running: "Running",
|
|
122
128
|
built: "Built to",
|
|
123
129
|
runWithNode: "Run it with",
|
|
130
|
+
openInBrowser: "Open it in a browser",
|
|
131
|
+
watching: "Watching the file. Stop with Ctrl+C",
|
|
132
|
+
watchChanged: "File changed — rebuilding",
|
|
124
133
|
compileError: "Compile error",
|
|
125
134
|
runtimeError: "Runtime error",
|
|
126
135
|
initCreated: "Created file",
|
package/dist/index.js
CHANGED
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* kuza открыть консоль (REPL)
|
|
6
6
|
* kuza run main.kuza скомпилировать и выполнить
|
|
7
7
|
* kuza build main.kuza собрать в main.js
|
|
8
|
+
* kuza build main.kuza --web собрать страницу в main.html
|
|
9
|
+
* kuza watch main.kuza пересобирать при сохранении
|
|
8
10
|
* kuza init создать main.kuza с примером
|
|
9
11
|
* kuza help справка
|
|
10
12
|
* kuza --version версия
|
|
11
13
|
*/
|
|
12
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { existsSync, readFileSync, watchFile, writeFileSync } from "node:fs";
|
|
13
15
|
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
14
16
|
import { FILE_EXT, NAME, VERSION, compile, formatDiagnostic, run, } from "kuza-core";
|
|
15
17
|
import { startRepl } from "./repl.js";
|
|
@@ -39,7 +41,10 @@ function usage() {
|
|
|
39
41
|
console.log(` ${primary("kuza")} ${dim("открыть консоль (REPL)")}`);
|
|
40
42
|
console.log(` ${primary("kuza")} run ${ex} ${dim("выполнить файл")}`);
|
|
41
43
|
console.log(` ${primary("kuza")} build ${ex} ${dim("собрать в main.js")}`);
|
|
44
|
+
console.log(` ${primary("kuza")} build ${ex} --web ${dim("собрать страницу в main.html")}`);
|
|
45
|
+
console.log(` ${primary("kuza")} watch ${ex} ${dim("пересобирать при сохранении")}`);
|
|
42
46
|
console.log(` ${primary("kuza")} init ${dim("создать main" + FILE_EXT)}`);
|
|
47
|
+
console.log(` ${primary("kuza")} init --web ${dim("создать сайт: main" + FILE_EXT + " + index.html")}`);
|
|
43
48
|
console.log(` ${primary("kuza")} help ${dim("эта справка")}`);
|
|
44
49
|
console.log("");
|
|
45
50
|
console.log(dim("Пример: ") + code(`${NAME.toLowerCase()} init && ${NAME.toLowerCase()} run ${ex}`));
|
|
@@ -74,7 +79,31 @@ async function runFile(file) {
|
|
|
74
79
|
printEntry(entry);
|
|
75
80
|
process.exitCode = result.ok ? 0 : 1;
|
|
76
81
|
}
|
|
77
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Куда вставить собранный код: в свой каркас index.html или в готовую страницу.
|
|
84
|
+
*
|
|
85
|
+
* Каркас — обычный HTML, внутри body которого стоит метка `{main.kuza}`.
|
|
86
|
+
* Если каркаса нет, генерируем минимальный сами.
|
|
87
|
+
*/
|
|
88
|
+
function pageHtml(code, scriptName, dir) {
|
|
89
|
+
const tag = `<script>\n${code}</script>`;
|
|
90
|
+
const shellPath = join(dir, "index.html");
|
|
91
|
+
if (existsSync(shellPath)) {
|
|
92
|
+
const shell = readFileSync(shellPath, "utf8");
|
|
93
|
+
const marker = `{${scriptName}}`;
|
|
94
|
+
if (shell.includes(marker))
|
|
95
|
+
return { html: shell.split(marker).join(tag), shell: "index.html" };
|
|
96
|
+
if (shell.includes("{kuza}"))
|
|
97
|
+
return { html: shell.split("{kuza}").join(tag), shell: "index.html" };
|
|
98
|
+
if (shell.includes("</body>")) {
|
|
99
|
+
return { html: shell.replace("</body>", `${tag}\n</body>`), shell: "index.html" };
|
|
100
|
+
}
|
|
101
|
+
return { html: `${shell}\n${tag}\n`, shell: "index.html" };
|
|
102
|
+
}
|
|
103
|
+
const fallback = `<!doctype html>\n<html lang="ru">\n<head>\n<meta charset="utf-8">\n<meta name="viewport" content="width=device-width, initial-scale=1">\n<title>${NAME}</title>\n</head>\n<body>\n${tag}\n</body>\n</html>\n`;
|
|
104
|
+
return { html: fallback };
|
|
105
|
+
}
|
|
106
|
+
function buildFile(file, web) {
|
|
78
107
|
const path = resolve(file);
|
|
79
108
|
if (!existsSync(path)) {
|
|
80
109
|
fail(`${t("fileNotFound")}: ${file}`);
|
|
@@ -92,11 +121,41 @@ function buildFile(file) {
|
|
|
92
121
|
process.exitCode = 1;
|
|
93
122
|
return;
|
|
94
123
|
}
|
|
95
|
-
const
|
|
124
|
+
const base = path.slice(0, path.length - extname(path).length);
|
|
125
|
+
const outPath = web ? `${base}.html` : `${base}.js`;
|
|
126
|
+
if (web) {
|
|
127
|
+
const page = pageHtml(compiled.code, basename(path), dirname(path));
|
|
128
|
+
writeFileSync(outPath, page.html, "utf8");
|
|
129
|
+
console.log(success(`✓ ${t("built")} ${outPath}`));
|
|
130
|
+
if (page.shell)
|
|
131
|
+
console.log(dim(`Каркас: ${page.shell}`));
|
|
132
|
+
console.log(dim(`${t("openInBrowser")}: `) + code(`"${basename(outPath)}"`));
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
96
135
|
writeFileSync(outPath, compiled.code, "utf8");
|
|
97
136
|
console.log(success(`✓ ${t("built")} ${outPath}`));
|
|
98
137
|
console.log(dim(`${t("runWithNode")}: `) + code(`node "${basename(outPath)}"`));
|
|
99
138
|
}
|
|
139
|
+
/** Пересобирать файл при каждом сохранении. */
|
|
140
|
+
function watchProject(file, web) {
|
|
141
|
+
const path = resolve(file);
|
|
142
|
+
if (!existsSync(path)) {
|
|
143
|
+
fail(`${t("fileNotFound")}: ${file}`);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const rebuild = () => {
|
|
147
|
+
process.exitCode = 0;
|
|
148
|
+
buildFile(file, web);
|
|
149
|
+
};
|
|
150
|
+
rebuild();
|
|
151
|
+
watchFile(path, { interval: 400 }, (current, previous) => {
|
|
152
|
+
if (current.mtimeMs === previous.mtimeMs)
|
|
153
|
+
return;
|
|
154
|
+
console.log(dim(`↻ ${t("watchChanged")}`));
|
|
155
|
+
rebuild();
|
|
156
|
+
});
|
|
157
|
+
console.log(dim(t("watching")));
|
|
158
|
+
}
|
|
100
159
|
const SAMPLE = `## main${FILE_EXT} — первая программа на ${NAME}
|
|
101
160
|
## Запуск: ${NAME.toLowerCase()} run main${FILE_EXT}
|
|
102
161
|
|
|
@@ -112,22 +171,108 @@ log >> "Сумма: " + sum >> numbers;
|
|
|
112
171
|
log >> "Чисел мало";
|
|
113
172
|
]
|
|
114
173
|
`;
|
|
115
|
-
|
|
174
|
+
/** Шаблон сайта: каркас index.html + компоненты в Kuza. */
|
|
175
|
+
const SITE_SAMPLE = `## main${FILE_EXT} — сайт на ${NAME}
|
|
176
|
+
## Сборка: ${NAME.toLowerCase()} build main${FILE_EXT} --web → main.html
|
|
177
|
+
## Живая пересборка: ${NAME.toLowerCase()} watch main${FILE_EXT} --web
|
|
178
|
+
|
|
179
|
+
:count = 0;
|
|
180
|
+
:greeting = "";
|
|
181
|
+
|
|
182
|
+
func Card<t> >> [
|
|
183
|
+
card >> [
|
|
184
|
+
heading >> t;
|
|
185
|
+
para >> "Компонент — обычная функция ${NAME}.";
|
|
186
|
+
];
|
|
187
|
+
];
|
|
188
|
+
|
|
189
|
+
func draw<> >> [
|
|
190
|
+
page >> "Мой сайт на ${NAME}";
|
|
191
|
+
|
|
192
|
+
body >> [
|
|
193
|
+
heading >> "Привет! Это сайт на ${NAME}";
|
|
194
|
+
para >> "Страница собирается из вложенных блоков, как в React, только без сборщика.";
|
|
195
|
+
|
|
196
|
+
row >> [
|
|
197
|
+
Card<"Первый">;
|
|
198
|
+
Card<"Второй">;
|
|
199
|
+
Card<"Третий">;
|
|
200
|
+
];
|
|
201
|
+
|
|
202
|
+
column >> [
|
|
203
|
+
heading >> "Счётчик: {count}";
|
|
204
|
+
button >> <"Прибавить", increase>;
|
|
205
|
+
button >> <"Сбросить", reset>;
|
|
206
|
+
];
|
|
207
|
+
|
|
208
|
+
field >> <"Как тебя зовут?", "name">;
|
|
209
|
+
button >> <"Поздороваться", greet>;
|
|
210
|
+
|
|
211
|
+
?greeting != "" >> [
|
|
212
|
+
card >> [ para >> greeting; ];
|
|
213
|
+
];
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
render;
|
|
217
|
+
];
|
|
218
|
+
|
|
219
|
+
func increase<> >> [
|
|
220
|
+
count = count + 1;
|
|
221
|
+
draw<>;
|
|
222
|
+
];
|
|
223
|
+
|
|
224
|
+
func reset<> >> [
|
|
225
|
+
count = 0;
|
|
226
|
+
draw<>;
|
|
227
|
+
];
|
|
228
|
+
|
|
229
|
+
func greet<> >> [
|
|
230
|
+
:name = fieldValue >> "name";
|
|
231
|
+
?len >> name > 0 >> [
|
|
232
|
+
greeting = "Привет, " + name + "!";
|
|
233
|
+
];
|
|
234
|
+
draw<>;
|
|
235
|
+
];
|
|
236
|
+
|
|
237
|
+
draw<>;
|
|
238
|
+
`;
|
|
239
|
+
/** Каркас страницы: почти пустой HTML с точкой вставки {main.kuza}. */
|
|
240
|
+
const SHELL_SAMPLE = `<!doctype html>
|
|
241
|
+
<html lang="ru">
|
|
242
|
+
<head>
|
|
243
|
+
<meta charset="utf-8">
|
|
244
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
245
|
+
<title>Мой сайт на ${NAME}</title>
|
|
246
|
+
</head>
|
|
247
|
+
<body>
|
|
248
|
+
<!-- Сюда попадёт собранный ${NAME}. Метку не удаляй. -->
|
|
249
|
+
{main${FILE_EXT}}
|
|
250
|
+
</body>
|
|
251
|
+
</html>
|
|
252
|
+
`;
|
|
253
|
+
function initProject(target, web) {
|
|
116
254
|
const dir = target ? resolve(target) : process.cwd();
|
|
117
255
|
const file = join(dir, `main${FILE_EXT}`);
|
|
256
|
+
const shellFile = join(dir, "index.html");
|
|
118
257
|
if (existsSync(file)) {
|
|
119
258
|
console.log(warning(`${t("initExists")}: ${file}`));
|
|
120
259
|
return;
|
|
121
260
|
}
|
|
122
261
|
try {
|
|
123
|
-
writeFileSync(file, SAMPLE, { encoding: "utf8", flag: "wx" });
|
|
262
|
+
writeFileSync(file, web ? SITE_SAMPLE : SAMPLE, { encoding: "utf8", flag: "wx" });
|
|
263
|
+
if (web && !existsSync(shellFile))
|
|
264
|
+
writeFileSync(shellFile, SHELL_SAMPLE, "utf8");
|
|
124
265
|
}
|
|
125
266
|
catch {
|
|
126
267
|
fail(`Не удалось создать ${file}. Проверь, что папка существует: ${dir}`);
|
|
127
268
|
return;
|
|
128
269
|
}
|
|
129
270
|
console.log(success(`✓ ${t("initCreated")} ${file}`));
|
|
130
|
-
|
|
271
|
+
if (web && existsSync(shellFile)) {
|
|
272
|
+
console.log(dim("Каркас страницы: ") + code("index.html") + dim(` — внутри body метка {main${FILE_EXT}}`));
|
|
273
|
+
}
|
|
274
|
+
console.log(dim(`${t("initNext")}: `) +
|
|
275
|
+
code(web ? `${NAME.toLowerCase()} build main${FILE_EXT} --web` : `${NAME.toLowerCase()} run main${FILE_EXT}`));
|
|
131
276
|
}
|
|
132
277
|
async function main() {
|
|
133
278
|
const [first, ...rest] = argv;
|
|
@@ -153,15 +298,27 @@ async function main() {
|
|
|
153
298
|
return;
|
|
154
299
|
}
|
|
155
300
|
if (first === "build") {
|
|
156
|
-
|
|
301
|
+
const web = rest.includes("--web");
|
|
302
|
+
const target = rest.find((arg) => !arg.startsWith("--"));
|
|
303
|
+
if (!target) {
|
|
157
304
|
fail(`Укажи файл: ${NAME.toLowerCase()} build main${FILE_EXT}`);
|
|
158
305
|
return;
|
|
159
306
|
}
|
|
160
|
-
buildFile(
|
|
307
|
+
buildFile(target, web);
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
if (first === "watch") {
|
|
311
|
+
const web = rest.includes("--web");
|
|
312
|
+
const target = rest.find((arg) => !arg.startsWith("--"));
|
|
313
|
+
if (!target) {
|
|
314
|
+
fail(`Укажи файл: ${NAME.toLowerCase()} watch main${FILE_EXT}`);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
watchProject(target, web);
|
|
161
318
|
return;
|
|
162
319
|
}
|
|
163
320
|
if (first === "init") {
|
|
164
|
-
initProject(rest
|
|
321
|
+
initProject(rest.find((arg) => !arg.startsWith("--")), rest.includes("--web"));
|
|
165
322
|
return;
|
|
166
323
|
}
|
|
167
324
|
if (first.endsWith(FILE_EXT)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kuza-lang",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Kuza — консоль языка: запуск, сборка, REPL и ИИ-помощник",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"start": "node dist/index.js"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"kuza-core": "^0.
|
|
30
|
+
"kuza-core": "^0.4.0",
|
|
31
31
|
"boxen": "^8.0.1",
|
|
32
32
|
"chalk": "^5.3.0",
|
|
33
33
|
"gradient-string": "^3.0.0"
|