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.
Files changed (60) hide show
  1. package/README.md +47 -134
  2. package/bin/mhs.js +89 -81
  3. package/examples/demo_lengkap.mhs +12 -9
  4. package/examples/halo.mhs +7 -7
  5. package/examples/python_native.mhs +5 -4
  6. package/examples/rumus_elektronika.mhs +6 -6
  7. package/examples/tes_semua_modul.mhs +7 -7
  8. package/examples/utama_impor.mhs +8 -7
  9. package/package.json +6 -7
  10. package/src/index.js +84 -28
  11. package/src/language_server.js +58 -28
  12. package/src/lexer.js +444 -0
  13. package/src/modules/data_json.js +16 -4
  14. package/src/modules/index.js +0 -18
  15. package/src/parser.js +1523 -0
  16. package/src/py_modules/data_json.py +13 -2
  17. package/src/py_transpiler.js +1279 -202
  18. package/src/rust_runtime.js +443 -0
  19. package/src/safety_analyzer.js +2139 -0
  20. package/src/stdlib.js +123 -77
  21. package/src/transpiler.js +813 -337
  22. package/LICENSE +0 -21
  23. package/assets/mhs-file-icons.vsix +0 -0
  24. package/assets/mhs.ico +0 -0
  25. package/assets/mhs.svg +0 -3
  26. package/examples/__pycache__/alat_py.cpython-312.pyc +0 -0
  27. package/examples/tes_modul_baru.mhs +0 -44
  28. package/scripts/postinstall.js +0 -276
  29. package/src/modules/antrian.js +0 -37
  30. package/src/modules/arsip.js +0 -31
  31. package/src/modules/basis_data.js +0 -47
  32. package/src/modules/iterasi.js +0 -136
  33. package/src/modules/jalur.js +0 -77
  34. package/src/modules/proses.js +0 -43
  35. package/src/modules/statistika.js +0 -79
  36. package/src/modules/tabel.js +0 -83
  37. package/src/py_modules/__pycache__/antrian.cpython-312.pyc +0 -0
  38. package/src/py_modules/__pycache__/basis_data.cpython-312.pyc +0 -0
  39. package/src/py_modules/__pycache__/berkas.cpython-312.pyc +0 -0
  40. package/src/py_modules/__pycache__/data_json.cpython-312.pyc +0 -0
  41. package/src/py_modules/__pycache__/iterasi.cpython-312.pyc +0 -0
  42. package/src/py_modules/__pycache__/jalur.cpython-312.pyc +0 -0
  43. package/src/py_modules/__pycache__/jaringan.cpython-312.pyc +0 -0
  44. package/src/py_modules/__pycache__/koleksi.cpython-312.pyc +0 -0
  45. package/src/py_modules/__pycache__/kripto.cpython-312.pyc +0 -0
  46. package/src/py_modules/__pycache__/matematika.cpython-312.pyc +0 -0
  47. package/src/py_modules/__pycache__/modul_acak.cpython-312.pyc +0 -0
  48. package/src/py_modules/__pycache__/pola.cpython-312.pyc +0 -0
  49. package/src/py_modules/__pycache__/sistem.cpython-312.pyc +0 -0
  50. package/src/py_modules/__pycache__/statistika.cpython-312.pyc +0 -0
  51. package/src/py_modules/__pycache__/uji.cpython-312.pyc +0 -0
  52. package/src/py_modules/__pycache__/waktu.cpython-312.pyc +0 -0
  53. package/src/py_modules/antrian.py +0 -20
  54. package/src/py_modules/arsip.py +0 -37
  55. package/src/py_modules/basis_data.py +0 -29
  56. package/src/py_modules/iterasi.py +0 -37
  57. package/src/py_modules/jalur.py +0 -47
  58. package/src/py_modules/proses.py +0 -35
  59. package/src/py_modules/statistika.py +0 -29
  60. 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
- if (val === null || val === undefined) return 'Kosong';
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) => (typeof a === 'string' ? a : formatValue(a))).join(' ');
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 len = obj.length;
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(obj[i]);
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(obj[i]);
76
+ for (let i = a; i > b; i += s) out.push(raw[i]);
73
77
  }
74
- return typeof obj === 'string' ? out.join('') : out;
78
+ return typeof raw === 'string' ? out.join('') : out;
75
79
  }
76
80
 
77
81
  function panjang(item) {
78
- if (item === null || item === undefined) return 0;
79
- if (typeof item === 'string' || Array.isArray(item)) return item.length;
80
- if (typeof item === 'object') return Object.keys(item).length;
81
- throw new Error(`Tipe ${tipe(item)} tidak memiliki panjang()`);
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
- if (item === null || item === undefined) return 'kosong';
86
- if (Array.isArray(item)) return 'daftar';
87
- switch (typeof item) {
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 'angka';
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 item;
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
- teks: (x) => String(x),
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 Error(`Tidak bisa mengubah "${x}" menjadi angka`);
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
- bulat: (x) => parseInt(x, 10),
111
- desimal: (x) => parseFloat(x),
112
- daftar: (x = []) => Array.from(x),
113
- kamus: (x = {}) => ({ ...x }),
114
- himpunan: (x = []) => Array.from(new Set(x)),
115
- tupel: (x = []) => Object.freeze(Array.from(x)),
116
- semua: (koleksi) => Array.from(koleksi).every(Boolean),
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 len = Math.min(a.length, b.length);
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([a[i], b[i]]);
166
+ for (let i = 0; i < len; i++) out.push([ra[i], rb[i]]);
124
167
  return out;
125
168
  },
126
- bisa_dipanggil: (fn) => typeof fn === 'function',
127
- punya_atribut: (obj, prop) => obj != null && prop in obj,
128
- ambil_atribut: (obj, prop, defVal) => (obj != null && prop in obj ? obj[prop] : defVal),
129
- atur_atribut: (obj, prop, val) => { if (obj != null) obj[prop] = val; },
130
- hapus_atribut: (obj, prop) => { if (obj != null) delete obj[prop]; },
131
- adalah_turunan: (obj, cls) => (cls === Array ? Array.isArray(obj) : obj instanceof cls),
132
- turunan_dari: (obj, cls) => (cls === Array ? Array.isArray(obj) : obj instanceof cls),
133
- nilai_karakter: (ch) => String(ch).charCodeAt(0),
134
- karakter: (code) => String.fromCharCode(code),
135
- format_teks: (val) => String(val),
136
- teks_biner: (n) => '0b' + Number(n).toString(2),
137
- teks_heksa: (n) => '0x' + Number(n).toString(16),
138
- teks_oktal: (n) => '0o' + Number(n).toString(8),
139
- daftar_anggota: (obj) => (obj != null ? Object.keys(obj) : []),
140
- keluar: (code = 0) => process.exit(code),
141
- salin: (x) => (Array.isArray(x) ? [...x] : typeof x === 'object' && x !== null ? { ...x } : x),
142
- salin_dalam: (x) => JSON.parse(JSON.stringify(x)),
143
- bekukan: (x) => Object.freeze(Array.isArray(x) ? [...x] : { ...x }),
144
- petakan: (fn, koleksi) => Array.from(koleksi).map(fn),
145
- saring: (fn, koleksi) => Array.from(koleksi).filter(fn),
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 salinan = [...d].sort((a, b) => (typeof a === 'number' && typeof b === 'number' ? a - b : String(a).localeCompare(String(b))));
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
- if (typeof koleksi === 'string' || Array.isArray(koleksi)) return koleksi.includes(item);
161
- if (koleksi && typeof koleksi === 'object') return Object.prototype.hasOwnProperty.call(koleksi, item);
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) => Math.sqrt(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 };