@bablr/agast-vm-helpers 0.1.0 → 0.1.1

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 (2) hide show
  1. package/lib/builders.js +41 -10
  2. package/package.json +2 -2
package/lib/builders.js CHANGED
@@ -261,7 +261,11 @@ export const buildSpace = () => {
261
261
  };
262
262
 
263
263
  export const buildIdentifier = (name) => {
264
- return t.node(l.Instruction, 'Identifier', [t.lit(name)]);
264
+ return t.s_node(l.Instruction, 'Identifier', name);
265
+ };
266
+
267
+ export const buildKeyword = (name) => {
268
+ return t.s_node(l.Instruction, 'Identifier', name);
265
269
  };
266
270
 
267
271
  export const buildCall = (verb, ...args) => {
@@ -295,8 +299,8 @@ export const buildDigit = (value) => {
295
299
  return t.s_node(l.CSTML, 'Digit', value);
296
300
  };
297
301
 
298
- export const buildInteger = (value) => {
299
- const digits = value.toString(10).split('');
302
+ export const buildInteger = (value, base = 10) => {
303
+ const digits = value.toString(base).split('');
300
304
 
301
305
  return t.node(
302
306
  l.CSTML,
@@ -330,6 +334,8 @@ export const buildNumber = (value) => {
330
334
  }
331
335
  };
332
336
 
337
+ const buildLiteralTerminal = (lit) => freeze({ type: 'Literal', value: lit });
338
+
333
339
  export const buildString = (value) => {
334
340
  const pieces = isArray(value) ? value : [value];
335
341
  const terminals = [];
@@ -359,22 +365,45 @@ export const buildString = (value) => {
359
365
  chr === '\n' ||
360
366
  chr === '\r' ||
361
367
  chr === '\t' ||
362
- chr === '\0'
368
+ chr === '\0' ||
369
+ chr.charCodeAt(0) < 32
363
370
  ) {
364
371
  if (lit) {
365
- terminals.push(freeze({ type: 'Literal', value: lit }));
372
+ terminals.push(buildLiteralTerminal(lit));
366
373
  lit = '';
367
374
  }
368
375
 
376
+ let value;
377
+
378
+ if (escapables[chr]) {
379
+ value = t.node(l.CSTML, 'EscapeCode', [t.ref`sigilToken`], {
380
+ sigilToken: buildKeyword(escapables[chr]),
381
+ digits: null,
382
+ });
383
+ } else if (chr.charCodeAt(0) < 32) {
384
+ const hexDigits = chr.charCodeAt(0).toString(16).padStart(4, '0');
385
+ value = t.node(
386
+ l.CSTML,
387
+ 'EscapeCode',
388
+ [t.ref`sigilToken`, ...[...hexDigits].map((d) => t.ref`digits[]`)],
389
+ {
390
+ sigilToken: buildKeyword('u'),
391
+ digits: [...hexDigits].map((digit) => buildDigit(digit)),
392
+ },
393
+ );
394
+ } else {
395
+ value = buildKeyword(chr);
396
+ }
397
+
369
398
  terminals.push(
370
399
  t.buildEmbedded(
371
400
  t.e_node(
372
401
  l.CSTML,
373
- 'Escape',
374
- [t.ref`escape`, t.ref`escapee`],
402
+ 'EscapeSequence',
403
+ [t.ref`escape`, t.ref`value`],
375
404
  {
376
405
  escape: t.s_node(l.CSTML, 'Punctuator', '\\'),
377
- escapee: t.s_node(l.CSTML, 'Literal', escapables[chr] || chr),
406
+ value,
378
407
  },
379
408
  { cooked: chr },
380
409
  ),
@@ -385,7 +414,7 @@ export const buildString = (value) => {
385
414
  }
386
415
  }
387
416
  } else {
388
- terminals.push(freeze({ type: 'Literal', value: lit }));
417
+ terminals.push(buildLiteralTerminal(lit));
389
418
  lit = '';
390
419
 
391
420
  if (piece == null) {
@@ -398,7 +427,7 @@ export const buildString = (value) => {
398
427
  }
399
428
  }
400
429
 
401
- if (lit) terminals.push(freeze({ type: 'Literal', value: lit }));
430
+ if (lit) terminals.push(buildLiteralTerminal(lit));
402
431
  lit = '';
403
432
 
404
433
  return t.node(l.CSTML, 'String', [t.ref`open`, t.ref`content`, t.ref`close`], {
@@ -521,6 +550,8 @@ export const buildExpression = (expr) => {
521
550
  return buildBoolean(expr);
522
551
  case 'string':
523
552
  return buildString(expr);
553
+ case 'number':
554
+ return buildInteger(expr);
524
555
  case 'object': {
525
556
  switch (getPrototypeOf(expr)) {
526
557
  case Array.prototype:
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/agast-vm-helpers",
3
3
  "description": "Helper functions for working with the BABLR VM",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "sideEffects": false,
17
17
  "dependencies": {
18
- "@bablr/agast-helpers": "0.1.0"
18
+ "@bablr/agast-helpers": "0.1.1"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#49f5952efed27f94ee9b94340eb1563c440bf64e",