1
1
import { CodeLine } from "../tokenizer" ;
2
2
3
3
export interface BlockContext {
4
- returnCalled : boolean ;
5
- breakCalled : boolean ;
6
- continueCalled : boolean ;
7
- returnObject : any ;
8
- currentLevel : string ;
9
- namelessFuncsCount : number ;
10
- blockScope : { [ index : string ] : any }
4
+ returnCalled : boolean ;
5
+ breakCalled : boolean ;
6
+ continueCalled : boolean ;
7
+ returnObject : any ;
8
+ currentLevel : string ;
9
+ namelessFuncsCount : number ;
10
+ blockScope : { [ index : string ] : any }
11
11
}
12
12
13
13
export type AnyFunc = ( ...args : any [ ] ) => void | any | Promise < any > ;
14
14
15
15
export interface FuncInfo {
16
- name : string ;
17
- params : string [ ] ;
18
- instructions : CodeLine [ ] ;
16
+ name : string ;
17
+ params : string [ ] ;
18
+ instructions : CodeLine [ ] ;
19
19
}
20
20
21
21
export const OPERATIONS : { [ index : string ] : any } = {
22
- '+' : ( a : any , b : any ) => a + b ,
23
- '-' : ( a : any , b : any ) => a - b ,
24
- '*' : ( a : any , b : any ) => a * b ,
25
- '/' : ( a : any , b : any ) => a / b ,
26
- '%' : ( a : any , b : any ) => a % b ,
27
- '**' : ( a : any , b : any ) => Math . pow ( a , b ) ,
28
- '==' : ( a : any , b : any ) => a === b ,
29
- '!=' : ( a : any , b : any ) => a !== b ,
30
- '<>' : ( a : any , b : any ) => a !== b ,
31
- '>' : ( a : any , b : any ) => a > b ,
32
- '<' : ( a : any , b : any ) => a < b ,
33
- '>=' : ( a : any , b : any ) => a >= b ,
34
- '<=' : ( a : any , b : any ) => a <= b ,
22
+ '+' : ( a : any , b : any ) => a + b ,
23
+ '-' : ( a : any , b : any ) => a - b ,
24
+ '*' : ( a : any , b : any ) => a * b ,
25
+ '/' : ( a : any , b : any ) => a / b ,
26
+ '%' : ( a : any , b : any ) => a % b ,
27
+ '**' : ( a : any , b : any ) => Math . pow ( a , b ) ,
28
+ '==' : ( a : any , b : any ) => a === b ,
29
+ '!=' : ( a : any , b : any ) => a !== b ,
30
+ '<>' : ( a : any , b : any ) => a !== b ,
31
+ '>' : ( a : any , b : any ) => a > b ,
32
+ '<' : ( a : any , b : any ) => a < b ,
33
+ '>=' : ( a : any , b : any ) => a >= b ,
34
+ '<=' : ( a : any , b : any ) => a <= b ,
35
35
} ;
36
36
37
37
export const INDENT_SIZE = 2 ;
38
38
39
39
export function lastItem ( arr : string | any [ ] ) : string | any {
40
- return ( arr ?. length ) ? arr [ arr . length - 1 ] : null ;
40
+ return ( arr ?. length ) ? arr [ arr . length - 1 ] : null ;
41
41
}
42
42
43
43
export function getLineIndent ( line : string ) : number {
44
- let cc = 0 ;
45
- while ( line [ cc ] === ' ' ) { cc ++ ; }
46
- return cc ;
44
+ let cc = 0 ;
45
+ while ( line [ cc ] === ' ' ) { cc ++ ; }
46
+ return cc ;
47
47
}
48
48
49
49
export function sliceBlock ( instuctionLines : CodeLine [ ] , start : number ) : CodeLine [ ] {
50
- const blockLineIndent = getLineIndent ( instuctionLines [ start ] . line ) ;
51
- const blockEndIndex = instuctionLines
52
- . findIndex ( ( cl , i ) => i > start && getLineIndent ( cl . line ) < blockLineIndent )
50
+ const blockLineIndent = getLineIndent ( instuctionLines [ start ] . line ) ;
51
+ const blockEndIndex = instuctionLines
52
+ . findIndex ( ( cl , i ) => i > start && getLineIndent ( cl . line ) < blockLineIndent )
53
53
54
- return instuctionLines . slice ( start , blockEndIndex > 0 ? blockEndIndex : undefined )
54
+ return instuctionLines . slice ( start , blockEndIndex > 0 ? blockEndIndex : undefined )
55
55
}
56
56
57
57
export function parseDatetimeOrNull ( value : string | Date ) : Date | null {
58
- if ( ! value ) { return null ; }
59
- if ( value instanceof Date && ! isNaN ( value . valueOf ( ) ) ) { return value ; }
60
- // only string values can be converted to Date
61
- if ( typeof value !== 'string' ) { return null ; }
62
-
63
- const strValue = String ( value ) ;
64
- if ( ! strValue . length ) { return null ; }
65
-
66
- const parseMonth = ( mm : string ) : number => {
67
- if ( ! mm || ! mm . length ) {
68
- return NaN ;
69
- }
70
-
71
- const m = parseInt ( mm , 10 ) ;
72
- if ( ! isNaN ( m ) ) {
73
- return m - 1 ;
74
- }
75
-
76
- // make sure english months are coming through
77
- if ( mm . startsWith ( 'jan' ) ) { return 0 ; }
78
- if ( mm . startsWith ( 'feb' ) ) { return 1 ; }
79
- if ( mm . startsWith ( 'mar' ) ) { return 2 ; }
80
- if ( mm . startsWith ( 'apr' ) ) { return 3 ; }
81
- if ( mm . startsWith ( 'may' ) ) { return 4 ; }
82
- if ( mm . startsWith ( 'jun' ) ) { return 5 ; }
83
- if ( mm . startsWith ( 'jul' ) ) { return 6 ; }
84
- if ( mm . startsWith ( 'aug' ) ) { return 7 ; }
85
- if ( mm . startsWith ( 'sep' ) ) { return 8 ; }
86
- if ( mm . startsWith ( 'oct' ) ) { return 9 ; }
87
- if ( mm . startsWith ( 'nov' ) ) { return 10 ; }
88
- if ( mm . startsWith ( 'dec' ) ) { return 11 ; }
89
-
58
+ if ( ! value ) { return null ; }
59
+ if ( value instanceof Date && ! isNaN ( value . valueOf ( ) ) ) { return value ; }
60
+ // only string values can be converted to Date
61
+ if ( typeof value !== 'string' ) { return null ; }
62
+
63
+ const strValue = String ( value ) ;
64
+ if ( ! strValue . length ) { return null ; }
65
+
66
+ const parseMonth = ( mm : string ) : number => {
67
+ if ( ! mm || ! mm . length ) {
90
68
return NaN ;
69
+ }
70
+
71
+ const m = parseInt ( mm , 10 ) ;
72
+ if ( ! isNaN ( m ) ) {
73
+ return m - 1 ;
74
+ }
75
+
76
+ // make sure english months are coming through
77
+ if ( mm . startsWith ( 'jan' ) ) { return 0 ; }
78
+ if ( mm . startsWith ( 'feb' ) ) { return 1 ; }
79
+ if ( mm . startsWith ( 'mar' ) ) { return 2 ; }
80
+ if ( mm . startsWith ( 'apr' ) ) { return 3 ; }
81
+ if ( mm . startsWith ( 'may' ) ) { return 4 ; }
82
+ if ( mm . startsWith ( 'jun' ) ) { return 5 ; }
83
+ if ( mm . startsWith ( 'jul' ) ) { return 6 ; }
84
+ if ( mm . startsWith ( 'aug' ) ) { return 7 ; }
85
+ if ( mm . startsWith ( 'sep' ) ) { return 8 ; }
86
+ if ( mm . startsWith ( 'oct' ) ) { return 9 ; }
87
+ if ( mm . startsWith ( 'nov' ) ) { return 10 ; }
88
+ if ( mm . startsWith ( 'dec' ) ) { return 11 ; }
89
+
90
+ return NaN ;
91
+ } ;
92
+
93
+ const correctYear = ( yy : number ) => {
94
+ if ( yy < 100 ) {
95
+ return yy < 68 ? yy + 2000 : yy + 1900 ;
96
+ } else {
97
+ return yy ;
98
+ }
99
+ } ;
100
+
101
+ const validDateOrNull =
102
+ ( yyyy : number , month : number , day : number , hours : number , mins : number , ss : number ) : Date | null => {
103
+ if ( month > 11 || day > 31 || hours >= 60 || mins >= 60 || ss >= 60 ) { return null ; }
104
+
105
+ const dd = new Date ( yyyy , month , day , hours , mins , ss , 0 ) ;
106
+ return ! isNaN ( dd . valueOf ( ) ) ? dd : null ;
91
107
} ;
92
-
93
- const correctYear = ( yy : number ) => {
94
- if ( yy < 100 ) {
95
- return yy < 68 ? yy + 2000 : yy + 1900 ;
96
- } else {
97
- return yy ;
98
- }
99
- } ;
100
-
101
- const validDateOrNull =
102
- ( yyyy : number , month : number , day : number , hours : number , mins : number , ss : number ) : Date | null => {
103
- if ( month > 11 || day > 31 || hours >= 60 || mins >= 60 || ss >= 60 ) { return null ; }
104
-
105
- const dd = new Date ( yyyy , month , day , hours , mins , ss , 0 ) ;
106
- return ! isNaN ( dd . valueOf ( ) ) ? dd : null ;
107
- } ;
108
-
109
- const strTokens = strValue . replace ( 'T' , ' ' ) . toLowerCase ( ) . split ( / [: / - ] / ) ;
110
- const dt = strTokens . map ( parseFloat ) ;
111
-
112
- // try ISO first
113
- let d = validDateOrNull ( dt [ 0 ] , dt [ 1 ] - 1 , dt [ 2 ] , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
114
- if ( d ) { return d ; }
115
-
116
- // then UK
117
- d = validDateOrNull ( correctYear ( dt [ 2 ] ) , parseMonth ( strTokens [ 1 ] ) , dt [ 0 ] , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
118
- if ( d ) { return d ; }
119
-
120
- // then US
121
- d = validDateOrNull ( correctYear ( dt [ 2 ] ) , parseMonth ( strTokens [ 0 ] ) , correctYear ( dt [ 1 ] ) , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
122
- if ( d ) { return d ; }
123
-
124
- return null ;
125
- }
126
-
108
+
109
+ const strTokens = strValue . replace ( 'T' , ' ' ) . toLowerCase ( ) . split ( / [: / - ] / ) ;
110
+ const dt = strTokens . map ( parseFloat ) ;
111
+
112
+ // try ISO first
113
+ let d = validDateOrNull ( dt [ 0 ] , dt [ 1 ] - 1 , dt [ 2 ] , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
114
+ if ( d ) { return d ; }
115
+
116
+ // then UK
117
+ d = validDateOrNull ( correctYear ( dt [ 2 ] ) , parseMonth ( strTokens [ 1 ] ) , dt [ 0 ] , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
118
+ if ( d ) { return d ; }
119
+
120
+ // then US
121
+ d = validDateOrNull ( correctYear ( dt [ 2 ] ) , parseMonth ( strTokens [ 0 ] ) , correctYear ( dt [ 1 ] ) , dt [ 3 ] || 0 , dt [ 4 ] || 0 , dt [ 5 ] || 0 ) ;
122
+ if ( d ) { return d ; }
123
+
124
+ return null ;
125
+ }
0 commit comments