@@ -58,6 +58,55 @@ export class FnCall extends CodeElement implements Expression {
58
58
}
59
59
}
60
60
61
+ export class BinaryOperator extends CodeElement implements Expression {
62
+ public isExpression : true = true ;
63
+
64
+ private lhs : Expression ;
65
+ private operatorName : string ;
66
+ private rhs : Expression ;
67
+
68
+ public constructor ( lhs : Expression , operatorName : string , rhs : Expression ) {
69
+ super ( ) ;
70
+ this . lhs = lhs ;
71
+ this . operatorName = operatorName ;
72
+ this . rhs = rhs ;
73
+ }
74
+
75
+ public flatten ( ) : string [ ] {
76
+ const lhsInLines = this . lhs . flatten ( ) ;
77
+ const rhsInLines = this . rhs . flatten ( ) ;
78
+ if ( lhsInLines . length === 1 && rhsInLines . length === 1 ) {
79
+ const oneLine = `${ lhsInLines [ 0 ] } ${ this . operatorName } ${ rhsInLines [ 0 ] } ` ;
80
+ if ( oneLine . length < maxLineLength ) {
81
+ return [ oneLine ] ;
82
+ }
83
+ }
84
+ if ( lhsInLines . length === 1 ) {
85
+ const firstLine = `${ lhsInLines [ 0 ] } ${ this . operatorName } ` ;
86
+ if ( firstLine . length < maxLineLength ) {
87
+ return [
88
+ firstLine ,
89
+ ...rhsInLines . map ( indent ) ,
90
+ ] ;
91
+ }
92
+ }
93
+ if ( rhsInLines . length === 1 ) {
94
+ const lastLine = indent ( `${ this . operatorName } ${ rhsInLines [ 0 ] } ` ) ;
95
+ if ( lastLine . length < maxLineLength ) {
96
+ return [
97
+ ...lhsInLines ,
98
+ lastLine ,
99
+ ] ;
100
+ }
101
+ }
102
+ return [
103
+ ...lhsInLines ,
104
+ this . operatorName ,
105
+ ...rhsInLines . map ( indent ) ,
106
+ ] ;
107
+ }
108
+ }
109
+
61
110
export class BraceInitialiser extends CodeElement implements Expression {
62
111
public isExpression : true = true ;
63
112
@@ -254,7 +303,7 @@ class Braces extends CodeElement {
254
303
public flatten ( ) : string [ ] {
255
304
return [
256
305
this . intro ? this . intro + " {" : "{" ,
257
- ..._ . flatMap ( this . body , ( block ) => block . flatten ( ) ) . map ( indent ) ,
306
+ ..._ . flatMap ( this . body , ( stmt ) => stmt . flatten ( ) ) . map ( indent ) ,
258
307
"}" ,
259
308
] ;
260
309
}
@@ -286,6 +335,168 @@ export class Method extends Braces implements Member {
286
335
}
287
336
}
288
337
338
+ export class CatchBlock {
339
+ private exceptionType : string ;
340
+ private body : Statement [ ] ;
341
+ private exceptionName : string ;
342
+
343
+ public constructor ( exceptionType : string , body : Statement [ ] = [ ] , exceptionName : string = "ex" ) {
344
+ this . exceptionType = exceptionType ;
345
+ this . body = body ;
346
+ this . exceptionName = exceptionName ;
347
+ }
348
+
349
+ public flatten ( ) : string [ ] {
350
+ return [
351
+ `} catch (${ this . exceptionType } ${ this . exceptionName } ) {` ,
352
+ ..._ . flatMap ( this . body , ( stmt ) => stmt . flatten ( ) ) ,
353
+ ] ;
354
+ }
355
+ }
356
+
357
+ export class TryCatch extends CodeElement implements Statement {
358
+ public isStatement : true = true ;
359
+
360
+ private tryBody : Statement [ ] ;
361
+ private catchBlocks : CatchBlock [ ] ;
362
+ private finallyBody ?: Statement [ ] ;
363
+
364
+ public constructor ( tryBody : Statement [ ] = [ ] , catchBlocks : CatchBlock [ ] = [ ] , finallyBody ?: Statement [ ] ) {
365
+ super ( ) ;
366
+ this . tryBody = tryBody ;
367
+ this . catchBlocks = catchBlocks ;
368
+ this . finallyBody = finallyBody ;
369
+ }
370
+
371
+ private getFinallyLines ( ) : string [ ] {
372
+ if ( this . finallyBody === undefined ) {
373
+ return [ ] ;
374
+ }
375
+ return [
376
+ "} finally {" ,
377
+ ..._ . flatMap ( this . finallyBody , ( stmt ) => stmt . flatten ( ) ) ,
378
+ ] ;
379
+ }
380
+
381
+ public flatten ( ) : string [ ] {
382
+ return [
383
+ "try {" ,
384
+ ..._ . flatMap ( this . tryBody , ( stmt ) => stmt . flatten ( ) ) . map ( indent ) ,
385
+ ..._ . flatMap ( this . catchBlocks , ( catchBlock ) => catchBlock . flatten ( ) ) ,
386
+ ...this . getFinallyLines ( ) ,
387
+ "}" ,
388
+ ] ;
389
+ }
390
+ }
391
+
392
+ export class IfThenElse extends CodeElement implements Statement {
393
+ public isStatement : true = true ;
394
+
395
+ private guard : Expression ;
396
+ private thenBlock : Statement [ ] ;
397
+ private elseBlock ?: Statement [ ] ;
398
+
399
+ public constructor ( guard : Expression , thenBlock : Statement [ ] , elseBlock ?: Statement [ ] ) {
400
+ super ( ) ;
401
+ this . guard = guard ;
402
+ this . thenBlock = thenBlock ;
403
+ this . elseBlock = elseBlock ;
404
+ }
405
+
406
+ private getGuardLines ( ) : string [ ] {
407
+ const guardInLines = this . guard . flatten ( ) ;
408
+ if ( guardInLines . length === 1 ) {
409
+ const oneLine = `if (${ guardInLines [ 0 ] } ) {` ;
410
+ if ( oneLine . length < maxLineLength )
411
+ return [ oneLine ] ;
412
+ }
413
+ guardInLines [ guardInLines . length - 1 ] += ")" ;
414
+ return [
415
+ "if (" ,
416
+ ...guardInLines . map ( indent ) ,
417
+ "{" ,
418
+ ] ;
419
+ }
420
+
421
+ public flatten ( ) : string [ ] {
422
+ const thenLines = _ . flatMap ( this . thenBlock , ( stmt ) => stmt . flatten ( ) ) . map ( indent ) ;
423
+ if ( this . elseBlock === undefined ) {
424
+ return [
425
+ ...this . getGuardLines ( ) ,
426
+ ...thenLines ,
427
+ "}" ,
428
+ ] ;
429
+ } else {
430
+ return [
431
+ ...this . getGuardLines ( ) ,
432
+ ...thenLines ,
433
+ "} else {" ,
434
+ ..._ . flatMap ( this . elseBlock , ( stmt ) => stmt . flatten ( ) ) . map ( indent ) ,
435
+ "}" ,
436
+ ] ;
437
+ }
438
+ }
439
+ }
440
+
441
+ export class RangedFor extends CodeElement implements Statement {
442
+ public isStatement : true = true ;
443
+
444
+ private modifiedType : string ;
445
+ private rangeVariableName : string ;
446
+ private collection : Expression ;
447
+ private body : Statement [ ] ;
448
+
449
+ public constructor ( modifiedType : string , rangeVariableName : string , collection : Expression , body : Statement [ ] ) {
450
+ super ( ) ;
451
+ this . modifiedType = modifiedType ;
452
+ this . rangeVariableName = rangeVariableName ;
453
+ this . collection = collection ;
454
+ this . body = body ;
455
+ }
456
+
457
+ private getForLines ( ) : string [ ] {
458
+ const collectionInLines = this . collection . flatten ( ) ;
459
+ if ( collectionInLines . length === 1 ) {
460
+ const oneLine = `for (${ this . modifiedType } ${ this . rangeVariableName } : ${ collectionInLines [ 0 ] } ) {` ;
461
+ if ( oneLine . length < maxLineLength )
462
+ return [ oneLine ] ;
463
+ }
464
+ collectionInLines [ collectionInLines . length - 1 ] += ")" ;
465
+ const forLine = `for (${ this . modifiedType } ${ this . rangeVariableName } :` ;
466
+ if ( forLine . length < maxLineLength ) {
467
+ return [
468
+ forLine ,
469
+ ...collectionInLines . map ( indent ) ,
470
+ "{" ,
471
+ ] ;
472
+ }
473
+ const rangeVariableLine = indent ( `${ this . modifiedType } ${ this . rangeVariableName } :` ) ;
474
+ if ( rangeVariableLine . length < maxLineLength ) {
475
+ return [
476
+ "for (" ,
477
+ rangeVariableLine ,
478
+ ...collectionInLines . map ( indent ) ,
479
+ "{" ,
480
+ ] ;
481
+ }
482
+ return [
483
+ "for (" ,
484
+ indent ( this . modifiedType ) ,
485
+ indent ( indent ( `${ this . rangeVariableName } :` ) ) ,
486
+ ...collectionInLines . map ( indent ) ,
487
+ "{" ,
488
+ ] ;
489
+ }
490
+
491
+ public flatten ( ) : string [ ] {
492
+ return [
493
+ ...this . getForLines ( ) ,
494
+ ..._ . flatMap ( this . body , ( stmt ) => stmt . flatten ( ) ) . map ( indent ) ,
495
+ "}" ,
496
+ ] ;
497
+ }
498
+ }
499
+
289
500
export class Class extends CodeElement {
290
501
private modifiers : string ;
291
502
private name : string ;
0 commit comments