mhs-lang 2.2.3 → 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/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,64 +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]),
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]),
119
161
  pasangkan: (a, b) => {
120
- 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);
121
165
  const out = [];
122
- 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]]);
123
167
  return out;
124
168
  },
125
- petakan: (fn, koleksi) => Array.from(koleksi).map(fn),
126
- saring: (fn, koleksi) => Array.from(koleksi).filter(fn),
127
- tambah: (d, item) => { d.push(item); return d; },
128
- 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
+ },
129
189
  urutkan: (d, menurun = false) => {
130
- 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))));
131
192
  return menurun ? salinan.reverse() : salinan;
132
193
  },
133
- balik: (d) => [...d].reverse(),
134
- gabung: (d, pemisah = '') => d.join(pemisah),
135
- pisah: (str, pemisah = ' ') => String(str).split(pemisah),
136
- huruf_besar: (str) => String(str).toUpperCase(),
137
- huruf_kecil: (str) => String(str).toLowerCase(),
138
- potong: (str) => String(str).trim(),
139
- 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),
140
201
  mengandung: (koleksi, item) => {
141
- if (typeof koleksi === 'string' || Array.isArray(koleksi)) return koleksi.includes(item);
142
- 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);
143
205
  return false;
144
206
  },
145
207
  acak: (min = 0, max = 100) => Math.floor(Math.random() * (max - min + 1)) + min,
146
- 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
+ },
147
212
  pangkat: (a, b) => Math.pow(a, b),
148
213
  mutlak: (x) => Math.abs(x),
149
214
  bulatkan: (x) => Math.round(x),
150
215
  bawah: (x) => Math.floor(x),
151
216
  atas: (x) => Math.ceil(x),
152
- min_nilai: (...args) => (Array.isArray(args[0]) ? Math.min(...args[0]) : Math.min(...args)),
153
- maks_nilai: (...args) => (Array.isArray(args[0]) ? Math.max(...args[0]) : Math.max(...args)),
154
- 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),
155
220
  waktu_sekarang: () => Date.now(),
156
221
  tunggu: (ms) => {
157
222
  const end = Date.now() + ms;
158
223
  while (Date.now() < end) {}
159
224
  },
160
- kunci: (k) => Object.keys(k),
161
- nilai: (k) => Object.values(k),
225
+ kunci: (k) => Object.keys(__mhs_unwrap_ref(k)),
226
+ nilai: (k) => Object.values(__mhs_unwrap_ref(k)),
162
227
  };
163
228
 
164
229
  module.exports = { stdlib, formatValue };