squiffy-compiler 7.0.0-beta.6 → 7.0.0-beta.7

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,13 +6,15 @@ export class Unconvertible extends Error {
6
6
  }
7
7
  const precedence = {
8
8
  "or": 1, "and": 2,
9
- "=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4,
9
+ "=": 4, "!=": 4, ">": 4, ">=": 4, "<": 4, "<=": 4, "contains": 4,
10
10
  "+": 5, "-": 5,
11
11
  "*": 6, "/": 6, "%": 6,
12
12
  };
13
13
  const notPrecedence = 3;
14
14
  const atomPrecedence = 8;
15
15
  function precedenceOf(e) {
16
+ if (e.k == "cond")
17
+ return 0;
16
18
  if (e.k == "bin")
17
19
  return precedence[e.op];
18
20
  if (e.k == "not")
@@ -43,12 +45,42 @@ export function quote(text, options = {}) {
43
45
  return `"${text}"`;
44
46
  }
45
47
  const reservedWords = new Set(["and", "or", "not", "true", "false", "null", "undefined"]);
46
- // An attribute name in an expression: the name, or [name] if it isn't one word
48
+ const word = /^[A-Za-z_]\w*$/;
49
+ // Whether an attribute name is a path to a value inside an object, written with dots, such as
50
+ // hero.hp (#442). The converters only make names of one word, so a dot can only be a path.
51
+ function isPath(name) {
52
+ const parts = name.split(".");
53
+ return parts.length > 1 && parts.every(part => word.test(part)) && !reservedWords.has(parts[0]);
54
+ }
55
+ // An attribute name in an expression: the name, or [name] if it isn't one word. A path to a
56
+ // value inside an object, hero.hp or inventory[i].name, is written as it is.
47
57
  export function attributeRef(name) {
48
- if (/^[A-Za-z_]\w*$/.test(name) && !reservedWords.has(name))
58
+ if ((word.test(name) && !reservedWords.has(name)) || isPath(name) || isDynamicTarget(name))
49
59
  return name;
50
60
  return `[${name}]`;
51
61
  }
62
+ // Whether a statement's target is a value inside an object found as the story runs, such as
63
+ // inventory[i].name, which only a setter can set: {{set}} and the like take a fixed name
64
+ export function isDynamicTarget(name) {
65
+ return /^[A-Za-z_]\w*[.[]/.test(name) && name.includes("[");
66
+ }
67
+ // A value inside an object or list: hero.hp for a fixed word inside an attribute, otherwise
68
+ // inventory[i] (#442)
69
+ export function member(e, key) {
70
+ if (e.k == "var" && key.k == "str" && word.test(key.v) && (word.test(e.name) || isPath(e.name)) && !reservedWords.has(e.name)) {
71
+ return { k: "var", name: `${e.name}.${key.v}` };
72
+ }
73
+ return { k: "member", e, key };
74
+ }
75
+ // The text of a setter's target, e.g. hero.hp or inventory[i].name, or null if it isn't
76
+ // something that can be set
77
+ export function targetName(e) {
78
+ if (e.k == "var")
79
+ return e.name;
80
+ if (e.k == "member" && targetName(e.e) !== null)
81
+ return print(e);
82
+ return null;
83
+ }
52
84
  export function print(e, options = {}) {
53
85
  switch (e.k) {
54
86
  case "num": return String(e.v);
@@ -62,9 +94,28 @@ export function print(e, options = {}) {
62
94
  // A link fills in its setters with only its own loop's item
63
95
  if (e.up)
64
96
  throw new Unconvertible("a link in a loop that sets a variable to an outer loop's item");
65
- return `'{{${loopRef(e)}}}'`;
97
+ // Filled in as text, so quoted, unless it's a number
98
+ return e.kind == "index" || e.numbers ? `{{${loopRef(e)}}}` : `'{{${loopRef(e)}}}'`;
66
99
  case "array": return `(array${e.items.map(i => " " + printArgument(i, options)).join("")})`;
67
- case "call": return `(${e.fn}${e.args.map(a => " " + printArgument(a, options)).join("")})`;
100
+ // Always a space before "}", so nested objects never have the "}}" that would end a tag
101
+ case "object":
102
+ if (!e.entries.length)
103
+ return "{ }";
104
+ return `{ ${e.entries.map(([key, value]) => `${word.test(key) ? key : quote(key, options)}: ${print(value, options)}`).join(", ")} }`;
105
+ case "member": {
106
+ if (e.e.k == "loop" && options.inLink)
107
+ throw new Unconvertible("a link in a loop that sets a variable to a value inside the loop's item");
108
+ const base = printArgument(e.e, options);
109
+ if (e.key.k == "str" && word.test(e.key.v) && (e.e.k == "member" || (e.e.k == "loop" && e.e.kind == "item"))) {
110
+ return `${base}.${e.key.v}`;
111
+ }
112
+ return `${base}[${print(e.key, options)}]`;
113
+ }
114
+ case "call":
115
+ // .indexOf() that isn't checking whether something is there (see sugarcube.ts) (#447)
116
+ if (e.fn == "__indexof")
117
+ return print({ ...e, fn: "indexof" }, options);
118
+ return `(${e.fn}${e.args.map(a => " " + printArgument(a, options)).join("")})`;
68
119
  case "neg": {
69
120
  const inner = print(e.e, options);
70
121
  return precedenceOf(e.e) < atomPrecedence ? `-(${inner})` : `-${inner}`;
@@ -73,6 +124,12 @@ export function print(e, options = {}) {
73
124
  const inner = print(e.e, options);
74
125
  return `not ${precedenceOf(e.e) < notPrecedence ? `(${inner})` : inner}`;
75
126
  }
127
+ case "cond": {
128
+ // Right-associative, so only a condition in the test or the first value needs brackets
129
+ const test = print(e.test, options);
130
+ const then = print(e.then, options);
131
+ return `${e.test.k == "cond" ? `(${test})` : test} ? ${e.then.k == "cond" ? `(${then})` : then} : ${print(e.otherwise, options)}`;
132
+ }
76
133
  case "bin": {
77
134
  const p = precedence[e.op];
78
135
  const comparison = p == 4;
@@ -91,6 +148,11 @@ function printArgument(e, options) {
91
148
  const text = print(e, options);
92
149
  return precedenceOf(e) < atomPrecedence || e.k == "neg" ? `(${text})` : text;
93
150
  }
151
+ // Whether an expression is a call to a helper that makes a new list (#447), which {{#each}} can
152
+ // go through as it can an attribute
153
+ export function isListCall(e) {
154
+ return e.k == "call" && ["range", "sort", "shuffle", "slice"].includes(e.fn);
155
+ }
94
156
  // Whether an expression can only be true or false, for Harlowe's "$a is 1 or 2"
95
157
  export function isBoolean(e) {
96
158
  if (e.k == "bool" || e.k == "not")
@@ -101,6 +163,21 @@ export function isBoolean(e) {
101
163
  return e.fn == "seen" || e.fn == "contains" || e.fn == "at";
102
164
  return false;
103
165
  }
166
+ // The length of a list or text, which Handlebars gives as its length property: name.length (#446)
167
+ export function lengthOf(e) {
168
+ if (e.k == "str")
169
+ return { k: "num", v: e.v.length };
170
+ if (e.k == "array")
171
+ return { k: "num", v: e.items.length };
172
+ return member(e, { k: "str", v: "length" });
173
+ }
174
+ // Harlowe's (cond: test, a, test2, b, otherwise), as a ? b : c
175
+ export function conditional(values, what) {
176
+ if (values.length < 3 || values.length % 2 == 0)
177
+ throw new Unconvertible(`${what} without a condition and two values for each`);
178
+ const [test, then, ...rest] = values;
179
+ return { k: "cond", test, then, otherwise: rest.length == 1 ? rest[0] : conditional(rest, what) };
180
+ }
104
181
  // A random whole number from min to max, as Squiffy's {{random}} picks from an array
105
182
  export function randomInteger(min, max) {
106
183
  if (min.k != "num" || max.k != "num")
@@ -122,14 +199,28 @@ export function display(e) {
122
199
  if (e.k == "num" || e.k == "bool")
123
200
  return String(e.v);
124
201
  if (e.k == "var") {
125
- return /^[A-Za-z_]\w*$/.test(e.name) && !reservedWords.has(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
202
+ return (word.test(e.name) && !reservedWords.has(e.name)) || isPath(e.name) ? `{{${e.name}}}` : `{{get ${quote(e.name)}}}`;
126
203
  }
127
204
  if (e.k == "loop")
128
205
  return `{{${loopRef(e)}}}`;
129
- if (e.k == "call")
206
+ // A helper call is written as it is, unless an argument needs an expression, such as
207
+ // (abs (-hp)), which only {{= }} rewrites
208
+ if (e.k == "call" && e.args.every(isPlain))
130
209
  return `{{${print(e).slice(1, -1)}}}`;
131
210
  return `{{= ${print(e)}}}`;
132
211
  }
212
+ // Whether a value is written the same in Handlebars as in an expression
213
+ function isPlain(e) {
214
+ if (e.k == "num" || e.k == "str" || e.k == "bool" || e.k == "null" || e.k == "loop")
215
+ return true;
216
+ if (e.k == "var")
217
+ return !isDynamicTarget(e.name);
218
+ if (e.k == "call")
219
+ return e.args.every(isPlain);
220
+ if (e.k == "array")
221
+ return e.items.every(isPlain);
222
+ return false;
223
+ }
133
224
  // A token stream for the expression parsers
134
225
  export class Tokens {
135
226
  constructor(items) {
@@ -1,4 +1,5 @@
1
1
  import type { Expr } from "./expression.js";
2
+ import type { StoryAudio } from "./audio.js";
2
3
  import type { ConvertedPassage, PassageContext } from "./convert.js";
3
4
  export declare function splitArgs(args: string): string[];
4
5
  export declare class HarloweExpressions {
@@ -15,4 +16,12 @@ export declare class HarloweExpressions {
15
16
  macro(name: string, raw: string, it?: Expr): Expr;
16
17
  visits(): Expr;
17
18
  }
19
+ export declare const harloweAudioPassages: Set<string>;
20
+ export declare function harloweAudio(story: PassageContext["story"]): StoryAudio;
21
+ export declare function harloweMacros(story: PassageContext["story"]): Map<string, {
22
+ section: string;
23
+ params: string[];
24
+ body: string;
25
+ defined: boolean;
26
+ }>;
18
27
  export declare function convertHarlowePassage(text: string, ctx: PassageContext): ConvertedPassage;