@@ -959,6 +959,8 @@ const EasyCoder_Core = {
959
959
} ,
960
960
961
961
run : program => {
962
+ program . parent . run ( program . parent . nextPc ) ;
963
+ program . parent . nextPc = 0 ;
962
964
program . exit ( ) ;
963
965
return 0 ;
964
966
}
@@ -2146,6 +2148,55 @@ const EasyCoder_Core = {
2146
2148
}
2147
2149
} ,
2148
2150
2151
+ Split : {
2152
+
2153
+ compile : compiler => {
2154
+ const lino = compiler . getLino ( ) ;
2155
+ item = compiler . getNextValue ( ) ;
2156
+ let on = `\n` ;
2157
+ if ( compiler . tokenIs ( `on` ) ) {
2158
+ on = compiler . getNextValue ( ) ;
2159
+ }
2160
+ if ( [ `giving` , `into` ] . includes ( compiler . getToken ( ) ) ) {
2161
+ if ( compiler . nextIsSymbol ( ) ) {
2162
+ const targetRecord = compiler . getSymbolRecord ( ) ;
2163
+ if ( targetRecord . keyword === `variable` ) {
2164
+ compiler . next ( ) ;
2165
+ compiler . addCommand ( {
2166
+ domain : `core` ,
2167
+ keyword : `split` ,
2168
+ lino,
2169
+ item,
2170
+ on,
2171
+ target : targetRecord . name
2172
+ } ) ;
2173
+ return true ;
2174
+ }
2175
+ }
2176
+ }
2177
+ return false ;
2178
+ } ,
2179
+
2180
+ run : program => {
2181
+ let command = program [ program . pc ] ;
2182
+ let content = program . getValue ( command . item ) ;
2183
+ let on = program . getValue ( command . on ) ;
2184
+ content = content . split ( on ) ;
2185
+ let elements = content . length ;
2186
+ targetRecord = program . getSymbolRecord ( command . target ) ;
2187
+ targetRecord . elements = elements ;
2188
+ for ( let n = 0 ; n < elements ; n ++ ) {
2189
+ targetRecord . value [ n ] = {
2190
+ type : `constant` ,
2191
+ numeric : false ,
2192
+ content : content [ n ]
2193
+ } ;
2194
+ }
2195
+ targetRecord . index = 0 ;
2196
+ return command . pc + 1 ;
2197
+ }
2198
+ } ,
2199
+
2149
2200
Stop : {
2150
2201
2151
2202
compile : compiler => {
@@ -2506,6 +2557,8 @@ const EasyCoder_Core = {
2506
2557
return EasyCoder_Core . Set ;
2507
2558
case `sort` :
2508
2559
return EasyCoder_Core . Sort ;
2560
+ case `split` :
2561
+ return EasyCoder_Core . Split ;
2509
2562
case `stop` :
2510
2563
return EasyCoder_Core . Stop ;
2511
2564
case `take` :
@@ -2715,6 +2768,18 @@ const EasyCoder_Core = {
2715
2768
}
2716
2769
}
2717
2770
}
2771
+ if ( [ `character` , `char` ] . includes ( token ) ) {
2772
+ let index = compiler . getNextValue ( ) ;
2773
+ if ( compiler . tokenIs ( `of` ) ) {
2774
+ let value = compiler . getNextValue ( ) ;
2775
+ return {
2776
+ domain : `core` ,
2777
+ type : `char` ,
2778
+ index,
2779
+ value
2780
+ } ;
2781
+ }
2782
+ }
2718
2783
if ( compiler . tokenIs ( `the` ) ) {
2719
2784
compiler . next ( ) ;
2720
2785
}
@@ -2822,7 +2887,12 @@ const EasyCoder_Core = {
2822
2887
}
2823
2888
break ;
2824
2889
case `position` :
2825
- if ( compiler . nextTokenIs ( `of` ) ) {
2890
+ let nocase = false ;
2891
+ if ( compiler . nextTokenIs ( `nocase` ) ) {
2892
+ nocase = true ;
2893
+ compiler . next ( ) ;
2894
+ }
2895
+ if ( compiler . tokenIs ( `of` ) ) {
2826
2896
var last = false ;
2827
2897
if ( compiler . nextTokenIs ( `the` ) ) {
2828
2898
if ( compiler . nextTokenIs ( `last` ) ) {
@@ -2838,7 +2908,8 @@ const EasyCoder_Core = {
2838
2908
type : `position` ,
2839
2909
needle,
2840
2910
haystack,
2841
- last
2911
+ last,
2912
+ nocase
2842
2913
} ;
2843
2914
}
2844
2915
}
@@ -2956,8 +3027,12 @@ const EasyCoder_Core = {
2956
3027
content : to ? fstr . substr ( from , to ) : fstr . substr ( from )
2957
3028
} ;
2958
3029
case `position` :
2959
- const needle = program . getValue ( value . needle ) ;
2960
- const haystack = program . getValue ( value . haystack ) ;
3030
+ let needle = program . getValue ( value . needle ) ;
3031
+ let haystack = program . getValue ( value . haystack ) ;
3032
+ if ( value . nocase ) {
3033
+ needle = needle . toLowerCase ( ) ;
3034
+ haystack = haystack . toLowerCase ( ) ;
3035
+ }
2961
3036
return {
2962
3037
type : `constant` ,
2963
3038
numeric : true ,
@@ -2984,17 +3059,22 @@ const EasyCoder_Core = {
2984
3059
const spec = JSON . parse ( program . getValue ( value . value ) ) ;
2985
3060
switch ( spec . mode ) {
2986
3061
case `time` :
3062
+
2987
3063
return {
2988
3064
type : `constant` ,
2989
3065
numeric : true ,
2990
3066
content : new Date ( fmtValue ) . toLocaleTimeString ( spec . locale , spec . options )
2991
3067
} ;
2992
3068
case `date` :
2993
3069
default :
3070
+ const date = new Date ( fmtValue ) ;
3071
+ const content = ( spec . format === `iso` )
3072
+ ? `${ date . getFullYear ( ) } -${ date . getMonth ( ) + 1 } -${ date . getDate ( ) } `
3073
+ : date . toLocaleDateString ( spec . locale , spec . options ) ;
2994
3074
return {
2995
3075
type : `constant` ,
2996
3076
numeric : true ,
2997
- content : new Date ( fmtValue ) . toLocaleDateString ( spec . locale , spec . options )
3077
+ content
2998
3078
} ;
2999
3079
}
3000
3080
} catch ( err ) {
@@ -3022,10 +3102,15 @@ const EasyCoder_Core = {
3022
3102
content : Math . floor ( date . getTime ( ) / 1000 )
3023
3103
} ;
3024
3104
case `date` :
3105
+ content = Date . parse ( program . getValue ( value . value ) ) / 1000 ;
3106
+ if ( isNaN ( content ) ) {
3107
+ program . runtimeError ( program [ program . pc ] . lino , `Invalid date format; expecting 'yyyy-mm-dd'` ) ;
3108
+ return null ;
3109
+ }
3025
3110
return {
3026
3111
type : `constant` ,
3027
3112
numeric : true ,
3028
- content : Date . parse ( program . getValue ( value . value ) ) / 1000
3113
+ content
3029
3114
} ;
3030
3115
case `newline` :
3031
3116
return {
@@ -3152,6 +3237,14 @@ const EasyCoder_Core = {
3152
3237
numeric : ! isNaN ( content ) ,
3153
3238
content
3154
3239
} ;
3240
+ case `char` :
3241
+ let index = program . getValue ( value . index ) ;
3242
+ let string = program . getValue ( value . value ) ;
3243
+ return {
3244
+ type : `constant` ,
3245
+ numeric : false ,
3246
+ content : string [ index ]
3247
+ } ;
3155
3248
}
3156
3249
return null ;
3157
3250
} ,
@@ -3216,7 +3309,8 @@ const EasyCoder_Core = {
3216
3309
return {
3217
3310
domain : `core` ,
3218
3311
type : `numeric` ,
3219
- value1
3312
+ value1,
3313
+ negate
3220
3314
} ;
3221
3315
case `even` :
3222
3316
compiler . next ( ) ;
@@ -3291,7 +3385,9 @@ const EasyCoder_Core = {
3291
3385
case `boolean` :
3292
3386
return program . getValue ( condition . value ) ;
3293
3387
case `numeric` :
3294
- return ! isNaN ( program . getValue ( condition . value1 ) ) ;
3388
+ let v = program . getValue ( condition . value1 ) ;
3389
+ let test = v === ` ` || isNaN ( v ) ;
3390
+ return condition . negate ? test : ! test ;
3295
3391
case `even` :
3296
3392
return ( program . getValue ( condition . value1 ) % 2 ) === 0 ;
3297
3393
case `odd` :
@@ -3419,7 +3515,8 @@ const EasyCoder = {
3419
3515
if ( v . type === `boolean` ) {
3420
3516
return v . content ? `true` : `false` ;
3421
3517
}
3422
- if ( v . content . substr ( 0 , 2 ) === `{"` || v . content [ 0 ] === `[` ) {
3518
+ if ( typeof v . content !== `undefined` && v . content . length >= 2
3519
+ && ( v . content . substr ( 0 , 2 ) === `{"` || v . content [ 0 ] === `[` ) ) {
3423
3520
try {
3424
3521
const parsed = JSON . parse ( v . content ) ;
3425
3522
return JSON . stringify ( parsed , null , 2 ) ;
@@ -3497,8 +3594,11 @@ const EasyCoder = {
3497
3594
EasyCoder . reportError ( err , program , program . source ) ;
3498
3595
if ( program . onError ) {
3499
3596
program . run ( program . onError ) ;
3500
- } else if ( program . parent && program . parent . onError ) {
3501
- program . parent . run ( program . parent . onError ) ;
3597
+ } else {
3598
+ let parent = program . parent ;
3599
+ if ( parent && parent . onError ) {
3600
+ parent . run ( parent . onError ) ;
3601
+ }
3502
3602
}
3503
3603
return ;
3504
3604
}
@@ -3637,9 +3737,9 @@ const EasyCoder = {
3637
3737
`${ finishCompile - startCompile } ms` ) ;
3638
3738
} catch ( err ) {
3639
3739
if ( err . message !== `stop` ) {
3640
- this . reportError ( err , program , source ) ;
3641
- if ( program && program . onError ) {
3642
- program . run ( program . onError ) ;
3740
+ this . reportError ( err , parent , source ) ;
3741
+ if ( parent && parent . onError ) {
3742
+ parent . run ( parent . onError ) ;
3643
3743
}
3644
3744
}
3645
3745
}
0 commit comments