mhs-lang 2.3.0 → 3.0.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 +47 -134
- package/bin/mhs.js +89 -81
- package/examples/demo_lengkap.mhs +12 -9
- package/examples/halo.mhs +7 -7
- package/examples/python_native.mhs +5 -4
- package/examples/rumus_elektronika.mhs +6 -6
- package/examples/tes_semua_modul.mhs +7 -7
- package/examples/utama_impor.mhs +8 -7
- package/package.json +6 -7
- package/src/index.js +84 -28
- package/src/language_server.js +58 -28
- package/src/lexer.js +444 -0
- package/src/modules/data_json.js +16 -4
- package/src/modules/index.js +0 -18
- package/src/parser.js +1523 -0
- package/src/py_modules/data_json.py +13 -2
- package/src/py_transpiler.js +1279 -202
- package/src/rust_runtime.js +443 -0
- package/src/safety_analyzer.js +2139 -0
- package/src/stdlib.js +123 -77
- package/src/transpiler.js +813 -337
- package/LICENSE +0 -21
- package/assets/mhs-file-icons.vsix +0 -0
- package/assets/mhs.ico +0 -0
- package/assets/mhs.svg +0 -3
- package/examples/__pycache__/alat_py.cpython-312.pyc +0 -0
- package/examples/tes_modul_baru.mhs +0 -44
- package/scripts/postinstall.js +0 -276
- package/src/modules/antrian.js +0 -37
- package/src/modules/arsip.js +0 -31
- package/src/modules/basis_data.js +0 -47
- package/src/modules/iterasi.js +0 -136
- package/src/modules/jalur.js +0 -77
- package/src/modules/proses.js +0 -43
- package/src/modules/statistika.js +0 -79
- package/src/modules/tabel.js +0 -83
- package/src/py_modules/__pycache__/antrian.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/basis_data.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/berkas.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/data_json.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/iterasi.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/jalur.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/jaringan.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/koleksi.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/kripto.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/matematika.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/modul_acak.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/pola.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/sistem.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/statistika.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/uji.cpython-312.pyc +0 -0
- package/src/py_modules/__pycache__/waktu.cpython-312.pyc +0 -0
- package/src/py_modules/antrian.py +0 -20
- package/src/py_modules/arsip.py +0 -37
- package/src/py_modules/basis_data.py +0 -29
- package/src/py_modules/iterasi.py +0 -37
- package/src/py_modules/jalur.py +0 -47
- package/src/py_modules/proses.py +0 -35
- package/src/py_modules/statistika.py +0 -29
- package/src/py_modules/tabel.py +0 -29
package/src/stdlib.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const rustRuntime = require('./rust_runtime');
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
__mhs_unwrap_ref,
|
|
6
|
+
MhsPanicError,
|
|
7
|
+
MhsOpsi,
|
|
8
|
+
MhsHasil,
|
|
9
|
+
formatRuntimeVal,
|
|
10
|
+
} = rustRuntime;
|
|
2
11
|
|
|
3
12
|
function formatValue(val) {
|
|
4
|
-
|
|
5
|
-
if (val === true) return 'Benar';
|
|
6
|
-
if (val === false) return 'Salah';
|
|
7
|
-
if (Array.isArray(val)) {
|
|
8
|
-
return '[' + val.map((v) => (typeof v === 'string' ? `"${v}"` : formatValue(v))).join(', ') + ']';
|
|
9
|
-
}
|
|
10
|
-
if (typeof val === 'object') {
|
|
11
|
-
return JSON.stringify(val);
|
|
12
|
-
}
|
|
13
|
-
return String(val);
|
|
13
|
+
return formatRuntimeVal(val);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
function cetak(...args) {
|
|
17
|
-
const out = args.map((a) =>
|
|
17
|
+
const out = args.map((a) => {
|
|
18
|
+
const raw = __mhs_unwrap_ref(a);
|
|
19
|
+
return typeof raw === 'string' ? raw : formatValue(raw);
|
|
20
|
+
}).join(' ');
|
|
18
21
|
console.log(out);
|
|
19
22
|
}
|
|
20
23
|
|
|
@@ -57,7 +60,8 @@ function rentang(start, stop, step = 1) {
|
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
function __mhs_slice(obj, start, end, step) {
|
|
60
|
-
const
|
|
63
|
+
const raw = __mhs_unwrap_ref(obj);
|
|
64
|
+
const len = raw.length;
|
|
61
65
|
const s = step === null || step === undefined ? 1 : step;
|
|
62
66
|
if (s === 0) throw new Error('Langkah irisan (slice step) tidak boleh 0');
|
|
63
67
|
|
|
@@ -66,34 +70,61 @@ function __mhs_slice(obj, start, end, step) {
|
|
|
66
70
|
|
|
67
71
|
const out = [];
|
|
68
72
|
if (s > 0) {
|
|
69
|
-
for (let i = a; i < b; i += s) out.push(
|
|
73
|
+
for (let i = a; i < b; i += s) out.push(raw[i]);
|
|
70
74
|
} else {
|
|
71
75
|
if (start === null || start === undefined) a = len - 1;
|
|
72
|
-
for (let i = a; i > b; i += s) out.push(
|
|
76
|
+
for (let i = a; i > b; i += s) out.push(raw[i]);
|
|
73
77
|
}
|
|
74
|
-
return typeof
|
|
78
|
+
return typeof raw === 'string' ? out.join('') : out;
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
function panjang(item) {
|
|
78
|
-
|
|
79
|
-
if (
|
|
80
|
-
if (typeof
|
|
81
|
-
|
|
82
|
+
const raw = __mhs_unwrap_ref(item);
|
|
83
|
+
if (raw === null || raw === undefined) return 0;
|
|
84
|
+
if (typeof raw === 'string' || Array.isArray(raw)) return raw.length;
|
|
85
|
+
if (typeof raw === 'object') return Object.keys(raw).length;
|
|
86
|
+
throw new Error(`Tipe ${tipe(raw)} tidak memiliki panjang()`);
|
|
82
87
|
}
|
|
83
88
|
|
|
84
89
|
function tipe(item) {
|
|
85
|
-
|
|
86
|
-
if (
|
|
87
|
-
|
|
90
|
+
const raw = __mhs_unwrap_ref(item);
|
|
91
|
+
if (raw === null || raw === undefined) return 'kosong';
|
|
92
|
+
if (raw instanceof MhsOpsi) return 'Opsi';
|
|
93
|
+
if (raw instanceof MhsHasil) return 'Hasil';
|
|
94
|
+
if (raw && raw.__mhsEnum) return raw.__mhsEnum;
|
|
95
|
+
if (Array.isArray(raw)) return 'daftar';
|
|
96
|
+
switch (typeof raw) {
|
|
88
97
|
case 'string': return 'teks';
|
|
89
|
-
case 'number': return '
|
|
98
|
+
case 'number': return Number.isInteger(raw) ? 'bulat' : 'desimal';
|
|
90
99
|
case 'boolean': return 'boolean';
|
|
91
100
|
case 'function': return 'fungsi';
|
|
92
101
|
case 'object': return 'kamus';
|
|
93
|
-
default: return typeof
|
|
102
|
+
default: return typeof raw;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function ensureMutableRef(target, actionName) {
|
|
107
|
+
if (target && target.__isMhsRef && !target.__mut) {
|
|
108
|
+
throw new MhsPanicError(`Tidak dapat memanggil ${actionName}() melalui pinjaman tetap (&)`);
|
|
94
109
|
}
|
|
95
110
|
}
|
|
96
111
|
|
|
112
|
+
function __mhs_ambil(target, idx) {
|
|
113
|
+
const raw = __mhs_unwrap_ref(target);
|
|
114
|
+
if (raw && typeof raw.ambil === 'function' && !Array.isArray(raw)) {
|
|
115
|
+
return raw.ambil(idx);
|
|
116
|
+
}
|
|
117
|
+
return rustRuntime.ambil_indeks(raw, idx);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function __mhs_ambil_opsi(target, key) {
|
|
121
|
+
const raw = __mhs_unwrap_ref(target);
|
|
122
|
+
if (raw && typeof raw.ambil_opsi === 'function') {
|
|
123
|
+
return raw.ambil_opsi(key);
|
|
124
|
+
}
|
|
125
|
+
return rustRuntime.ambil_kunci(raw, key);
|
|
126
|
+
}
|
|
127
|
+
|
|
97
128
|
const stdlib = {
|
|
98
129
|
cetak,
|
|
99
130
|
masukan,
|
|
@@ -101,83 +132,98 @@ const stdlib = {
|
|
|
101
132
|
panjang,
|
|
102
133
|
tipe,
|
|
103
134
|
__mhs_slice,
|
|
104
|
-
|
|
135
|
+
__mhs_ambil,
|
|
136
|
+
__mhs_ambil_opsi,
|
|
137
|
+
...rustRuntime,
|
|
138
|
+
teks: (x) => String(__mhs_unwrap_ref(x)),
|
|
105
139
|
angka: (x) => {
|
|
106
|
-
const n = Number(x);
|
|
107
|
-
if (Number.isNaN(n)) throw new
|
|
140
|
+
const n = Number(__mhs_unwrap_ref(x));
|
|
141
|
+
if (Number.isNaN(n) || !Number.isFinite(n)) throw new MhsPanicError(`Tidak bisa mengubah "${x}" menjadi angka`);
|
|
142
|
+
return n;
|
|
143
|
+
},
|
|
144
|
+
bulat: (x) => {
|
|
145
|
+
const n = parseInt(__mhs_unwrap_ref(x), 10);
|
|
146
|
+
if (Number.isNaN(n)) throw new MhsPanicError(`Tidak bisa mengubah "${x}" menjadi bulat`);
|
|
147
|
+
return n;
|
|
148
|
+
},
|
|
149
|
+
desimal: (x) => {
|
|
150
|
+
const n = parseFloat(__mhs_unwrap_ref(x));
|
|
151
|
+
if (Number.isNaN(n)) throw new MhsPanicError(`Tidak bisa mengubah "${x}" menjadi desimal`);
|
|
108
152
|
return n;
|
|
109
153
|
},
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
ada_yang: (koleksi) => Array.from(koleksi).some(Boolean),
|
|
118
|
-
enumerasi: (koleksi, mulai = 0) => Array.from(koleksi).map((v, i) => [i + mulai, v]),
|
|
119
|
-
cacah: (koleksi, mulai = 0) => Array.from(koleksi).map((v, i) => [i + mulai, v]),
|
|
154
|
+
daftar: (x = []) => Array.from(__mhs_unwrap_ref(x)),
|
|
155
|
+
kamus: (x = {}) => ({ ...__mhs_unwrap_ref(x) }),
|
|
156
|
+
himpunan: (x = []) => Array.from(new Set(__mhs_unwrap_ref(x))),
|
|
157
|
+
tupel: (x = []) => Object.freeze(Array.from(__mhs_unwrap_ref(x))),
|
|
158
|
+
semua: (koleksi) => Array.from(__mhs_unwrap_ref(koleksi)).every(Boolean),
|
|
159
|
+
ada_yang: (koleksi) => Array.from(__mhs_unwrap_ref(koleksi)).some(Boolean),
|
|
160
|
+
enumerasi: (koleksi, mulai = 0) => Array.from(__mhs_unwrap_ref(koleksi)).map((v, i) => [i + mulai, v]),
|
|
120
161
|
pasangkan: (a, b) => {
|
|
121
|
-
const
|
|
162
|
+
const ra = __mhs_unwrap_ref(a);
|
|
163
|
+
const rb = __mhs_unwrap_ref(b);
|
|
164
|
+
const len = Math.min(ra.length, rb.length);
|
|
122
165
|
const out = [];
|
|
123
|
-
for (let i = 0; i < len; i++) out.push([
|
|
166
|
+
for (let i = 0; i < len; i++) out.push([ra[i], rb[i]]);
|
|
124
167
|
return out;
|
|
125
168
|
},
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
tambah: (d, item) => { d.push(item); return d; },
|
|
147
|
-
hapus: (d, indeks) => (indeks === undefined ? d.pop() : d.splice(indeks, 1)[0]),
|
|
169
|
+
petakan: (fn, koleksi) => Array.from(__mhs_unwrap_ref(koleksi)).map(fn),
|
|
170
|
+
saring: (fn, koleksi) => Array.from(__mhs_unwrap_ref(koleksi)).filter(fn),
|
|
171
|
+
tambah: (d, item) => {
|
|
172
|
+
ensureMutableRef(d, 'tambah');
|
|
173
|
+
const raw = __mhs_unwrap_ref(d);
|
|
174
|
+
raw.push(item);
|
|
175
|
+
return raw;
|
|
176
|
+
},
|
|
177
|
+
hapus: (d, indeks) => {
|
|
178
|
+
ensureMutableRef(d, 'hapus');
|
|
179
|
+
const raw = __mhs_unwrap_ref(d);
|
|
180
|
+
if (indeks === undefined) {
|
|
181
|
+
if (raw.length === 0) throw new MhsPanicError('Tidak bisa menghapus dari daftar kosong');
|
|
182
|
+
return raw.pop();
|
|
183
|
+
}
|
|
184
|
+
if (indeks < 0 || indeks >= raw.length) {
|
|
185
|
+
throw new MhsPanicError(`Indeks hapus ${indeks} di luar batas panjang ${raw.length}`);
|
|
186
|
+
}
|
|
187
|
+
return raw.splice(indeks, 1)[0];
|
|
188
|
+
},
|
|
148
189
|
urutkan: (d, menurun = false) => {
|
|
149
|
-
const
|
|
190
|
+
const raw = __mhs_unwrap_ref(d);
|
|
191
|
+
const salinan = [...raw].sort((a, b) => (typeof a === 'number' && typeof b === 'number' ? a - b : String(a).localeCompare(String(b))));
|
|
150
192
|
return menurun ? salinan.reverse() : salinan;
|
|
151
193
|
},
|
|
152
|
-
balik: (d) => [...d].reverse(),
|
|
153
|
-
gabung: (d, pemisah = '') => d.join(pemisah),
|
|
154
|
-
pisah: (str, pemisah = ' ') => String(str).split(pemisah),
|
|
155
|
-
huruf_besar: (str) => String(str).toUpperCase(),
|
|
156
|
-
huruf_kecil: (str) => String(str).toLowerCase(),
|
|
157
|
-
potong: (str) => String(str).trim(),
|
|
158
|
-
ganti: (str, lama, baru) => String(str).split(lama).join(baru),
|
|
194
|
+
balik: (d) => [...__mhs_unwrap_ref(d)].reverse(),
|
|
195
|
+
gabung: (d, pemisah = '') => __mhs_unwrap_ref(d).join(pemisah),
|
|
196
|
+
pisah: (str, pemisah = ' ') => String(__mhs_unwrap_ref(str)).split(pemisah),
|
|
197
|
+
huruf_besar: (str) => String(__mhs_unwrap_ref(str)).toUpperCase(),
|
|
198
|
+
huruf_kecil: (str) => String(__mhs_unwrap_ref(str)).toLowerCase(),
|
|
199
|
+
potong: (str) => String(__mhs_unwrap_ref(str)).trim(),
|
|
200
|
+
ganti: (str, lama, baru) => String(__mhs_unwrap_ref(str)).split(lama).join(baru),
|
|
159
201
|
mengandung: (koleksi, item) => {
|
|
160
|
-
|
|
161
|
-
if (
|
|
202
|
+
const raw = __mhs_unwrap_ref(koleksi);
|
|
203
|
+
if (typeof raw === 'string' || Array.isArray(raw)) return raw.includes(item);
|
|
204
|
+
if (raw && typeof raw === 'object') return Object.prototype.hasOwnProperty.call(raw, item);
|
|
162
205
|
return false;
|
|
163
206
|
},
|
|
164
207
|
acak: (min = 0, max = 100) => Math.floor(Math.random() * (max - min + 1)) + min,
|
|
165
|
-
akar: (x) =>
|
|
208
|
+
akar: (x) => {
|
|
209
|
+
if (x < 0) throw new MhsPanicError('akar() tidak menerima bilangan negatif');
|
|
210
|
+
return Math.sqrt(x);
|
|
211
|
+
},
|
|
166
212
|
pangkat: (a, b) => Math.pow(a, b),
|
|
167
213
|
mutlak: (x) => Math.abs(x),
|
|
168
214
|
bulatkan: (x) => Math.round(x),
|
|
169
215
|
bawah: (x) => Math.floor(x),
|
|
170
216
|
atas: (x) => Math.ceil(x),
|
|
171
|
-
min_nilai: (...args) => (Array.isArray(args[0]) ? Math.min(...args[0]) : Math.min(...args)),
|
|
172
|
-
maks_nilai: (...args) => (Array.isArray(args[0]) ? Math.max(...args[0]) : Math.max(...args)),
|
|
173
|
-
jumlah: (d) => d.reduce((acc, v) => acc + v, 0),
|
|
217
|
+
min_nilai: (...args) => (Array.isArray(__mhs_unwrap_ref(args[0])) ? Math.min(...__mhs_unwrap_ref(args[0])) : Math.min(...args)),
|
|
218
|
+
maks_nilai: (...args) => (Array.isArray(__mhs_unwrap_ref(args[0])) ? Math.max(...__mhs_unwrap_ref(args[0])) : Math.max(...args)),
|
|
219
|
+
jumlah: (d) => __mhs_unwrap_ref(d).reduce((acc, v) => acc + v, 0),
|
|
174
220
|
waktu_sekarang: () => Date.now(),
|
|
175
221
|
tunggu: (ms) => {
|
|
176
222
|
const end = Date.now() + ms;
|
|
177
223
|
while (Date.now() < end) {}
|
|
178
224
|
},
|
|
179
|
-
kunci: (k) => Object.keys(k),
|
|
180
|
-
nilai: (k) => Object.values(k),
|
|
225
|
+
kunci: (k) => Object.keys(__mhs_unwrap_ref(k)),
|
|
226
|
+
nilai: (k) => Object.values(__mhs_unwrap_ref(k)),
|
|
181
227
|
};
|
|
182
228
|
|
|
183
229
|
module.exports = { stdlib, formatValue };
|