Skip to content

Commit 77cc5b8

Browse files
committed
Support for generating with conditionals
Signed-off-by: worksofliam <[email protected]>
1 parent 6814321 commit 77cc5b8

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

src/ui/dspf.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ export class DisplayFile {
230230
static parseConditionals(conditionColumns: string): Conditional[] {
231231
if (conditionColumns.trim() === "") {return [];}
232232

233-
/** @type {Conditional[]} */
234-
let conditionals = [];
233+
let conditionals: Conditional[] = [];
235234

236235
//TODO: something with condition
237236
//const condition = conditionColumns.substring(0, 1); //A (and) or O (or)
@@ -351,6 +350,37 @@ export class DisplayFile {
351350
return result;
352351
}
353352

353+
private static conditionalGroups(conditions: Conditional[]) {
354+
return conditions.reduce((acc, curr, index) => {
355+
if (index % 3 === 0) {
356+
acc.push([curr]);
357+
} else {
358+
acc[acc.length - 1].push(curr);
359+
}
360+
return acc;
361+
}, [] as Conditional[][]);
362+
}
363+
364+
private static getLinesForKeyword(keyword: Keyword): string[] {
365+
const lines: string[] = [];
366+
367+
// Convert array into groups of three
368+
const condition = this.conditionalGroups(keyword.conditions);
369+
370+
const firstConditions = condition[0] || [];
371+
const conditionStrings = firstConditions.map(c => `${c.negate ? 'N' : ' '}${c.indicator}`).join('').padEnd(9);
372+
373+
lines.push(` A ${conditionStrings} ${keyword.name}${keyword.value ? `(${keyword.value})` : ``}`);
374+
375+
for (let g = 1; g < condition.length; g++) {
376+
const group = condition[g];
377+
const conditionStrings = group.map(c => `${c.negate ? 'N' : ' '}${c.indicator}`).join('');
378+
lines.push(` A ${conditionStrings}`);
379+
}
380+
381+
return lines;
382+
}
383+
354384
public static getLinesForField(field: FieldInfo): string[] {
355385
const newLines: string[] = [];
356386

@@ -366,25 +396,27 @@ export class DisplayFile {
366396
const y = String(field.position.y).padStart(3, ` `);
367397
const displayType = FIELD_TYPE[field.displayType!];
368398

399+
// Convert array into groups of three
400+
const condition = this.conditionalGroups(field.conditions);
401+
const firstConditions = condition[0] || [];
402+
const conditionStrings = firstConditions.map(c => `${c.negate ? 'N' : ' '}${c.indicator}`).join('').padEnd(9);
403+
369404
if (field.displayType === `const`) {
370405
const value = field.value;
371406
newLines.push(
372-
` A ${y}${x}'${value}'`,
407+
` A ${conditionStrings} ${y}${x}'${value}'`,
373408
);
374409
} else if (displayType && field.name) {
375410
const definitionType = field.type;
376411
const length = String(field.length).padStart(5);
377412
const decimals = String(field.decimals).padStart(2);
378413
newLines.push(
379-
` A ${field.name.padEnd(10)} ${length}${definitionType}${decimals}${displayType}${y}${x}`,
414+
` A ${conditionStrings} ${field.name.padEnd(10)} ${length}${definitionType}${decimals}${displayType}${y}${x}`,
380415
);
381416
}
382417

383418
for (const keyword of field.keywords) {
384-
// TODO: support conditions
385-
newLines.push(
386-
` A ${keyword.name}${keyword.value ? `(${keyword.value})` : ``}`,
387-
);
419+
newLines.push(...DisplayFile.getLinesForKeyword(keyword));
388420
}
389421

390422
return newLines;
@@ -446,9 +478,7 @@ export class DisplayFile {
446478

447479
for (const keyword of keywords) {
448480
// TODO: support conditions
449-
lines.push(
450-
` A ${keyword.name}${keyword.value ? `(${keyword.value})` : ``}`,
451-
);
481+
lines.push(...DisplayFile.getLinesForKeyword(keyword));
452482
}
453483

454484
return lines;

0 commit comments

Comments
 (0)