porffor 0.17.0-05070e1f0 → 0.17.0-0564424f4

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.
@@ -6,6 +6,8 @@ import { join } from 'node:path';
6
6
 
7
7
  import { fileURLToPath } from 'node:url';
8
8
  const __dirname = fileURLToPath(new URL('.', import.meta.url));
9
+ globalThis.precompileCompilerPath = __dirname;
10
+ globalThis.precompile = true;
9
11
 
10
12
  const argv = process.argv.slice();
11
13
 
@@ -14,7 +16,7 @@ const compile = async (file, [ _funcs, _globals ]) => {
14
16
  let first = source.slice(0, source.indexOf('\n'));
15
17
 
16
18
  if (first.startsWith('export default')) {
17
- source = (await import(file)).default();
19
+ source = await (await import(file)).default();
18
20
  first = source.slice(0, source.indexOf('\n'));
19
21
  }
20
22
 
@@ -117,7 +119,7 @@ ${funcs.map(x => {
117
119
  locals: ${JSON.stringify(Object.values(x.locals).slice(x.params.length).map(x => x.type))},
118
120
  localNames: ${JSON.stringify(Object.keys(x.locals))},
119
121
  ${x.data && x.data.length > 0 ? ` data: ${JSON.stringify(x.data)},` : ''}
120
- ${x.table ? ` table: true` : ''}
122
+ ${x.table ? ` table: true,` : ''}${x.constr ? ` constr: true,` : ''}
121
123
  };`.replaceAll('\n\n', '\n').replaceAll('\n\n', '\n').replaceAll('\n\n', '\n');
122
124
  }).join('\n')}
123
125
  };`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "porffor",
3
3
  "description": "a basic experimental wip aot optimizing js -> wasm engine/compiler/runtime in js",
4
- "version": "0.17.0-05070e1f0",
4
+ "version": "0.17.0-0564424f4",
5
5
  "author": "CanadaHonk",
6
6
  "license": "MIT",
7
7
  "scripts": {},
package/rhemyn/compile.js CHANGED
@@ -6,34 +6,50 @@ import { TYPES } from '../compiler/types.js';
6
6
 
7
7
  // local indexes
8
8
  const BasePointer = 0; // base string pointer
9
- const IterPointer = 1; // this iteration base pointer
10
9
  const Counter = 2; // what char we are running on
11
10
  const Pointer = 3; // next char pointer
12
11
  const Length = 4;
13
12
  const Tmp = 5;
14
13
  const QuantifierTmp = 6; // the temporary variable used for quanitifers
15
14
 
15
+ const doesSucceedZero = (node) => {
16
+ for (const n of node.body) {
17
+ if (n.type === "Group") {
18
+ if (!doesSucceedZero(node)) return false;
19
+ }
20
+ if (!n.quantifier || n.quantifier[0] > 0) {
21
+ return false;
22
+ }
23
+ }
24
+ return true;
25
+ }
26
+
16
27
  const generate = (node, negated = false, get = true, stringSize = 2, func = 'test') => {
17
28
  let out = [];
18
29
  switch (node.type) {
19
30
  case 'Expression':
31
+ let succeedsZero = doesSucceedZero(node);
32
+
20
33
  out = [
21
34
  // set length local
22
35
  [ Opcodes.local_get, BasePointer ],
23
36
  [ Opcodes.i32_load, Math.log2(ValtypeSize.i32) - 1, 0 ],
24
- [ Opcodes.local_set, Length ],
37
+ [ Opcodes.local_tee, Length ],
25
38
 
26
- // set iter pointer local as base + sizeof i32 initially
39
+ ...number(0, Valtype.i32),
40
+ [ Opcodes.i32_eq ],
41
+ [ Opcodes.if, Blocktype.void ],
42
+ ...number(succeedsZero ? 1 : 0, Valtype.i32),
43
+ [ Opcodes.return ],
44
+ [ Opcodes.end ],
45
+
46
+ // pointer = base + sizeof i32
27
47
  [ Opcodes.local_get, BasePointer ],
28
48
  ...number(ValtypeSize.i32, Valtype.i32),
29
49
  [ Opcodes.i32_add ],
30
- [ Opcodes.local_set, IterPointer ],
50
+ [ Opcodes.local_set, Pointer ],
31
51
 
32
52
  [ Opcodes.loop, Blocktype.void ],
33
- // reset pointer as iter pointer
34
- [ Opcodes.local_get, IterPointer ],
35
- [ Opcodes.local_set, Pointer ],
36
-
37
53
  [ Opcodes.block, Blocktype.void ],
38
54
  // generate checks
39
55
  ...node.body.flatMap(x => generate(x, negated, true, stringSize, func)),
@@ -48,20 +64,15 @@ const generate = (node, negated = false, get = true, stringSize = 2, func = 'tes
48
64
  [ Opcodes.return ],
49
65
  [ Opcodes.end ],
50
66
 
51
- // increment iter pointer by string size
52
- [ Opcodes.local_get, IterPointer ],
53
- ...number(stringSize, Valtype.i32),
54
- [ Opcodes.i32_add ],
55
- [ Opcodes.local_set, IterPointer ],
67
+ // counter++, if length > counter, loop
68
+ [ Opcodes.local_get, Length ],
56
69
 
57
- // increment counter by 1, check if eq length, if not loop
58
70
  [ Opcodes.local_get, Counter ],
59
71
  ...number(1, Valtype.i32),
60
72
  [ Opcodes.i32_add ],
61
73
  [ Opcodes.local_tee, Counter ],
62
74
 
63
- [ Opcodes.local_get, Length ],
64
- [ Opcodes.i32_ne ],
75
+ [ Opcodes.i32_gt_s ],
65
76
 
66
77
  [ Opcodes.br_if, 0 ],
67
78
  [ Opcodes.end ],
@@ -133,6 +144,18 @@ const wrapQuantifier = (node, method, get, stringSize) => {
133
144
  ...number(0, Valtype.i32),
134
145
  [Opcodes.local_set, QuantifierTmp],
135
146
 
147
+ // if len - counter == 0, if min == 0, succeed, else fail
148
+ [ Opcodes.local_get, Length ],
149
+ [ Opcodes.local_get, Counter ],
150
+ [ Opcodes.i32_sub ],
151
+ ...number(0, Valtype.i32),
152
+ [ Opcodes.i32_eq ],
153
+ ...(min == 0 ? [
154
+ [ Opcodes.if, Blocktype.void ],
155
+ ] : [
156
+ [ Opcodes.br_if, 0 ],
157
+ ]),
158
+
136
159
  // start loop
137
160
  [Opcodes.loop, Blocktype.void],
138
161
  [ Opcodes.block, Blocktype.void ],
@@ -177,13 +200,7 @@ const wrapQuantifier = (node, method, get, stringSize) => {
177
200
  [Opcodes.i32_lt_s],
178
201
  ...(get ? checkFailure(): []),
179
202
 
180
- // counter += tmp - 1
181
- [ Opcodes.local_get, QuantifierTmp ],
182
- ...number(1, Valtype.i32),
183
- [ Opcodes.i32_sub ],
184
- [ Opcodes.local_get, Counter ],
185
- [ Opcodes.i32_add ],
186
- [ Opcodes.local_set, Counter ]
203
+ ...(min == 0 ? [ [ Opcodes.end ] ] : []),
187
204
  ];
188
205
  }
189
206
 
@@ -201,7 +218,7 @@ const generateChar = (node, negated, get, stringSize) => {
201
218
 
202
219
  return [
203
220
  ...out,
204
- ...(get ? checkFailure(): [])
221
+ ...(get ? checkFailure(): []),
205
222
  ];
206
223
  };
207
224
 
@@ -236,7 +253,7 @@ const generateSet = (node, negated, get, stringSize) => {
236
253
 
237
254
  return [
238
255
  ...out,
239
- ...checkFailure()
256
+ ...checkFailure(),
240
257
  ];
241
258
  };
242
259
 
@@ -268,7 +285,7 @@ const wrapFunc = (regex, func, name, index) => {
268
285
  const parsed = parse(regex);
269
286
 
270
287
  return outputFunc([
271
- [ Opcodes.local_get, IterPointer ],
288
+ [ Opcodes.local_get, 1 ],
272
289
  ...number(TYPES.string, Valtype.i32),
273
290
  [ Opcodes.i32_eq ],
274
291
  [ Opcodes.if, Valtype.i32 ],
@@ -278,24 +295,29 @@ const wrapFunc = (regex, func, name, index) => {
278
295
  // bytestring
279
296
  ...generate(parsed, false, true, 1, func),
280
297
  [ Opcodes.end ]
281
- ], name, index);
298
+ ], name, index, types[func]);
282
299
  };
283
300
 
284
301
  export const test = (regex, index = 0, name = 'regex_test_' + regex) => wrapFunc(regex, 'test', name, index);
285
302
  export const search = (regex, index = 0, name = 'regex_search_' + regex) => wrapFunc(regex, 'search', name, index);
286
303
 
287
- const outputFunc = (wasm, name, index) => ({
304
+ export const types = {
305
+ test: TYPES.boolean,
306
+ search: TYPES.number
307
+ };
308
+
309
+ const outputFunc = (wasm, name, index, returnType) => ({
288
310
  name,
289
311
  index,
290
312
  wasm,
313
+ returnType,
291
314
 
292
315
  export: true,
293
316
  params: [ Valtype.i32, Valtype.i32 ],
294
317
  returns: [ Valtype.i32 ],
295
- returnType: TYPES.boolean,
296
318
  locals: {
297
319
  basePointer: { idx: 0, type: Valtype.i32 },
298
- iterPointer: { idx: 1, type: Valtype.i32 },
320
+ inputType: { idx: 1, type: Valtype.i32 },
299
321
  counter: { idx: 2, type: Valtype.i32 },
300
322
  pointer: { idx: 3, type: Valtype.i32 },
301
323
  length: { idx: 4, type: Valtype.i32 },