Skip to content

Commit 8abe477

Browse files
Auto-fix logical operators implemented
1 parent a665994 commit 8abe477

8 files changed

Lines changed: 876 additions & 0 deletions

File tree

apps/test-tokenizer/src/test-maker/tests/auto-problem-fixing-tests.interface..ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,132 @@ export const AUTO_PROBLEM_FIXING_TESTS: IAutoAssemblerTest[] = [
419419
},
420420
],
421421
},
422+
{
423+
suiteName: `Verify we correctly fix logical and`,
424+
fileName: `code.110.logical-and.spec.ts`,
425+
tests: [
426+
{
427+
name: `for simple case`,
428+
code: [
429+
`compile_opt idl3`,
430+
``,
431+
`a = [1, 2, 4]`,
432+
`b = [1, 2, 4]`,
433+
``,
434+
`if myfunc(a and b) then print, 'WOW!'`,
435+
``,
436+
`if (!true and !false) then print, 'WOW!'`,
437+
``,
438+
`; handle switch`,
439+
`switch (!true and !true) of`,
440+
` myfunc(a and b): var = [1, 2, 3] and 5`,
441+
` (!true and !true): var = [1, 2, 3] and 5`,
442+
` else: var = [1, 2, 3] and 5`,
443+
`endswitch`,
444+
``,
445+
`; handle case`,
446+
`case (!true and !true) of`,
447+
` myfunc(a and b): var = [1, 2, 3] and 5`,
448+
` (!true and !true): var = [1, 2, 3] and 5`,
449+
` else: var = [1, 2, 3] and 5`,
450+
`endcase`,
451+
``,
452+
`repeat begin`,
453+
` var = [1, 2, 3] and 5`,
454+
`endrep until a and b`,
455+
``,
456+
`while (a and b) do begin`,
457+
` var = [1, 2, 3] and 5`,
458+
`endwhile`,
459+
`end`,
460+
],
461+
},
462+
],
463+
},
464+
{
465+
suiteName: `Verify we correctly fix logical or`,
466+
fileName: `code.111.logical-or.spec.ts`,
467+
tests: [
468+
{
469+
name: `for simple case`,
470+
code: [
471+
`compile_opt idl3`,
472+
``,
473+
`a = [1, 2, 4]`,
474+
`b = [1, 2, 4]`,
475+
``,
476+
`if myfunc(a or b) then print, 'WOW!'`,
477+
``,
478+
`if (!true or !false) then print, 'WOW!'`,
479+
``,
480+
`; handle switch`,
481+
`switch (!true or !true) of`,
482+
` myfunc(a or b): var = [1, 2, 3] or 5`,
483+
` (!true or !true): var = [1, 2, 3] or 5`,
484+
` else: var = [1, 2, 3] or 5`,
485+
`endswitch`,
486+
``,
487+
`; handle case`,
488+
`case (!true or !true) of`,
489+
` myfunc(a or b): var = [1, 2, 3] or 5`,
490+
` (!true or !true): var = [1, 2, 3] or 5`,
491+
` else: var = [1, 2, 3] or 5`,
492+
`endcase`,
493+
``,
494+
`repeat begin`,
495+
` var = [1, 2, 3] or 5`,
496+
`endrep until a or b`,
497+
``,
498+
`while (a or b) do begin`,
499+
` var = [1, 2, 3] or 5`,
500+
`endwhile`,
501+
`end`,
502+
],
503+
},
504+
],
505+
},
506+
{
507+
suiteName: `Verify we correctly fix logical not`,
508+
fileName: `code.112.logical-not.spec.ts`,
509+
tests: [
510+
{
511+
name: `for simple case`,
512+
code: [
513+
`compile_opt idl3`,
514+
``,
515+
`a = [1, 2, 4]`,
516+
`b = [1, 2, 4]`,
517+
``,
518+
`if myfunc(not a) then print, 'WOW!'`,
519+
``,
520+
`if (not !false) then print, 'WOW!'`,
521+
``,
522+
`; handle switch`,
523+
`switch (not !true) of`,
524+
` myfunc(not b): var = not 5`,
525+
` (not !true): var = not 5`,
526+
` else: var = not 5`,
527+
`endswitch`,
528+
``,
529+
`; handle case`,
530+
`case (not !true) of`,
531+
` myfunc(not b): var = not 5`,
532+
` (not !true): var = not 5`,
533+
` else: var = not 5`,
534+
`endcase`,
535+
``,
536+
`repeat begin`,
537+
` var = not 5`,
538+
`endrep until not b`,
539+
``,
540+
`while (not b) do begin`,
541+
` var = not 5`,
542+
`endwhile`,
543+
`end`,
544+
],
545+
},
546+
],
547+
},
422548
{
423549
suiteName: `Verify we remove excess args`,
424550
fileName: `dont-fix.when disabled.spec.ts`,

libs/assembling/fixers/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ export * from './lib/fixes-for/31.return-missing';
77
export * from './lib/fixes-for/38.no-comp-opt';
88
export * from './lib/fixes-for/76.init-method-pro';
99
export * from './lib/fixes-for/105.illegal-var-index';
10+
export * from './lib/fixes-for/110.logical-and';
11+
export * from './lib/fixes-for/111.logical-or';
12+
export * from './lib/fixes-for/112.logical-not';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ASSEMBLER_PROBLEM_FIXERS } from '@idl/assembling/tree-handlers';
2+
import { TOKEN_NAMES } from '@idl/tokenizer';
3+
import { IDL_PROBLEM_CODES } from '@idl/types/problem-codes';
4+
5+
import { HasProblem } from '../helpers/has-problem';
6+
7+
ASSEMBLER_PROBLEM_FIXERS.onBranchToken(
8+
TOKEN_NAMES.OPERATOR_LOGICAL,
9+
(token, parsed) => {
10+
if (
11+
HasProblem(token, IDL_PROBLEM_CODES.LOGICAL_AND, parsed.disabledProblems)
12+
) {
13+
token.match[0] = token.match[0].replace(/and/i, '&&');
14+
}
15+
}
16+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ASSEMBLER_PROBLEM_FIXERS } from '@idl/assembling/tree-handlers';
2+
import { TOKEN_NAMES } from '@idl/tokenizer';
3+
import { IDL_PROBLEM_CODES } from '@idl/types/problem-codes';
4+
5+
import { HasProblem } from '../helpers/has-problem';
6+
7+
ASSEMBLER_PROBLEM_FIXERS.onBranchToken(
8+
TOKEN_NAMES.OPERATOR_LOGICAL,
9+
(token, parsed) => {
10+
if (
11+
HasProblem(token, IDL_PROBLEM_CODES.LOGICAL_OR, parsed.disabledProblems)
12+
) {
13+
token.match[0] = token.match[0].replace(/or/i, '||');
14+
}
15+
}
16+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ASSEMBLER_PROBLEM_FIXERS } from '@idl/assembling/tree-handlers';
2+
import { TOKEN_NAMES } from '@idl/tokenizer';
3+
import { IDL_PROBLEM_CODES } from '@idl/types/problem-codes';
4+
5+
import { HasProblem } from '../helpers/has-problem';
6+
7+
ASSEMBLER_PROBLEM_FIXERS.onBranchToken(
8+
TOKEN_NAMES.OPERATOR_LOGICAL,
9+
(token, parsed) => {
10+
if (
11+
HasProblem(token, IDL_PROBLEM_CODES.LOGICAL_NOT, parsed.disabledProblems)
12+
) {
13+
token.match[0] = token.match[0].replace(/not/i, '~');
14+
15+
// change name because we dont need spaces around this operator anymore
16+
(token as any).name = TOKEN_NAMES.OPERATOR;
17+
}
18+
}
19+
);

0 commit comments

Comments
 (0)