@@ -90,6 +90,7 @@ enum Type {
90
90
String ( String ) ,
91
91
Bool ( bool ) ,
92
92
List ( Vec < Type > ) ,
93
+ Error ( String ) ,
93
94
}
94
95
95
96
/// Implement methods
@@ -104,6 +105,7 @@ impl Type {
104
105
let syntax: Vec < String > = list. iter ( ) . map ( |token| token. display ( ) ) . collect ( ) ;
105
106
format ! ( "[{}]" , syntax. join( " " ) )
106
107
}
108
+ Type :: Error ( err) => format ! ( "error:{err}" ) ,
107
109
}
108
110
}
109
111
@@ -114,6 +116,7 @@ impl Type {
114
116
Type :: Number ( i) => i. to_string ( ) ,
115
117
Type :: Bool ( b) => b. to_string ( ) ,
116
118
Type :: List ( l) => Type :: List ( l. to_owned ( ) ) . display ( ) ,
119
+ Type :: Error ( err) => format ! ( "error:{err}" ) ,
117
120
}
118
121
}
119
122
@@ -130,6 +133,7 @@ impl Type {
130
133
}
131
134
}
132
135
Type :: List ( l) => l. len ( ) as f64 ,
136
+ Type :: Error ( _) => 0f64 ,
133
137
}
134
138
}
135
139
@@ -140,6 +144,7 @@ impl Type {
140
144
Type :: Number ( i) => * i != 0.0 ,
141
145
Type :: Bool ( b) => * b,
142
146
Type :: List ( l) => !l. is_empty ( ) ,
147
+ Type :: Error ( _) => false ,
143
148
}
144
149
}
145
150
@@ -154,6 +159,7 @@ impl Type {
154
159
Type :: Number ( i) => vec ! [ Type :: Number ( * i) ] ,
155
160
Type :: Bool ( b) => vec ! [ Type :: Bool ( * b) ] ,
156
161
Type :: List ( l) => l. to_vec ( ) ,
162
+ Type :: Error ( e) => vec ! [ Type :: Error ( e. to_string( ) ) ] ,
157
163
}
158
164
}
159
165
}
@@ -456,7 +462,7 @@ impl Executor {
456
462
Some ( c) => self . stack . push ( Type :: String ( c. to_string ( ) ) ) ,
457
463
None => {
458
464
self . log_print ( "Error! failed of number decoding\n " . to_string ( ) ) ;
459
- self . stack . push ( Type :: Number ( code ) ) ;
465
+ self . stack . push ( Type :: Error ( "number-decoding" . to_string ( ) ) ) ;
460
466
}
461
467
}
462
468
}
@@ -468,7 +474,7 @@ impl Executor {
468
474
self . stack . push ( Type :: Number ( ( first_char as u32 ) as f64 ) ) ;
469
475
} else {
470
476
self . log_print ( "Error! failed of string encoding\n " . to_string ( ) ) ;
471
- self . stack . push ( Type :: String ( string) )
477
+ self . stack . push ( Type :: Error ( " string-encoding" . to_string ( ) ) ) ;
472
478
}
473
479
}
474
480
@@ -523,7 +529,8 @@ impl Executor {
523
529
let pattern: Regex = match Regex :: new ( pattern. as_str ( ) ) {
524
530
Ok ( i) => i,
525
531
Err ( e) => {
526
- self . log_print ( format ! ( "Error! {e}\n " ) ) ;
532
+ self . log_print ( format ! ( "Error! {}\n " , e. to_string( ) . replace( "Error" , "" ) ) ) ;
533
+ self . stack . push ( Type :: Error ( "regex" . to_string ( ) ) ) ;
527
534
return ;
528
535
}
529
536
} ;
@@ -541,10 +548,17 @@ impl Executor {
541
548
542
549
// Write string in the file
543
550
"write-file" => {
544
- let mut file =
545
- File :: create ( self . pop_stack ( ) . get_string ( ) ) . expect ( "Failed to create file" ) ;
551
+ let mut file = match File :: create ( self . pop_stack ( ) . get_string ( ) ) {
552
+ Ok ( file) => file,
553
+ Err ( e) => {
554
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
555
+ self . stack . push ( Type :: Error ( "create-file" . to_string ( ) ) ) ;
556
+ return ;
557
+ }
558
+ } ;
546
559
if let Err ( e) = file. write_all ( self . pop_stack ( ) . get_string ( ) . as_bytes ( ) ) {
547
- self . log_print ( format ! ( "Error! {}\n " , e) )
560
+ self . log_print ( format ! ( "Error! {}\n " , e) ) ;
561
+ self . stack . push ( Type :: Error ( "write-file" . to_string ( ) ) ) ;
548
562
}
549
563
}
550
564
@@ -553,7 +567,10 @@ impl Executor {
553
567
let name = self . pop_stack ( ) . get_string ( ) ;
554
568
match get_file_contents ( name) {
555
569
Ok ( s) => self . stack . push ( Type :: String ( s) ) ,
556
- Err ( e) => self . log_print ( format ! ( "Error! {}\n " , e) ) ,
570
+ Err ( e) => {
571
+ self . log_print ( format ! ( "Error! {}\n " , e) ) ;
572
+ self . stack . push ( Type :: Error ( "read-file" . to_string ( ) ) ) ;
573
+ }
557
574
} ;
558
575
}
559
576
@@ -670,7 +687,7 @@ impl Executor {
670
687
self . stack . push ( list[ index] . clone ( ) ) ;
671
688
} else {
672
689
self . log_print ( "Error! Index specification is out of range\n " . to_string ( ) ) ;
673
- self . stack . push ( Type :: List ( list ) ) ;
690
+ self . stack . push ( Type :: Error ( "index-out-range" . to_string ( ) ) ) ;
674
691
}
675
692
}
676
693
@@ -684,7 +701,7 @@ impl Executor {
684
701
self . stack . push ( Type :: List ( list) ) ;
685
702
} else {
686
703
self . log_print ( "Error! Index specification is out of range\n " . to_string ( ) ) ;
687
- self . stack . push ( Type :: List ( list ) ) ;
704
+ self . stack . push ( Type :: Error ( "index-out-range" . to_string ( ) ) ) ;
688
705
}
689
706
}
690
707
@@ -697,7 +714,7 @@ impl Executor {
697
714
self . stack . push ( Type :: List ( list) ) ;
698
715
} else {
699
716
self . log_print ( "Error! Index specification is out of range\n " . to_string ( ) ) ;
700
- self . stack . push ( Type :: List ( list ) ) ;
717
+ self . stack . push ( Type :: Error ( "index-out-range" . to_string ( ) ) ) ;
701
718
}
702
719
}
703
720
@@ -883,6 +900,7 @@ impl Executor {
883
900
Type :: String ( _) => "string" ,
884
901
Type :: Bool ( _) => "bool" ,
885
902
Type :: List ( _) => "list" ,
903
+ Type :: Error ( _) => "error" ,
886
904
}
887
905
. to_string ( ) ;
888
906
self . stack . push ( Type :: String ( result) ) ;
@@ -960,14 +978,16 @@ impl Executor {
960
978
// Open the file or url
961
979
"open" => {
962
980
if let Err ( e) = opener:: open ( self . pop_stack ( ) . get_string ( ) ) {
963
- self . log_print ( format ! ( "Error! {e}\n " ) )
981
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
982
+ self . stack . push ( Type :: Error ( "open" . to_string ( ) ) ) ;
964
983
}
965
984
}
966
985
967
986
// Change current directory
968
987
"cd" => {
969
988
if let Err ( err) = std:: env:: set_current_dir ( self . pop_stack ( ) . get_string ( ) ) {
970
989
self . log_print ( format ! ( "Error! {}\n " , err) ) ;
990
+ self . stack . push ( Type :: Error ( "cd" . to_string ( ) ) ) ;
971
991
}
972
992
}
973
993
@@ -984,7 +1004,8 @@ impl Executor {
984
1004
"mkdir" => {
985
1005
let name = self . pop_stack ( ) . get_string ( ) ;
986
1006
if let Err ( e) = fs:: create_dir ( name) {
987
- self . log_print ( format ! ( "Error! {e}\n " ) )
1007
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
1008
+ self . stack . push ( Type :: Error ( "mkdir" . to_string ( ) ) ) ;
988
1009
}
989
1010
}
990
1011
@@ -993,10 +1014,12 @@ impl Executor {
993
1014
let name = self . pop_stack ( ) . get_string ( ) ;
994
1015
if Path :: new ( name. as_str ( ) ) . is_dir ( ) {
995
1016
if let Err ( e) = fs:: remove_dir ( name) {
996
- self . log_print ( format ! ( "Error! {e}\n " ) )
1017
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
1018
+ self . stack . push ( Type :: Error ( "rm" . to_string ( ) ) ) ;
997
1019
}
998
1020
} else if let Err ( e) = fs:: remove_file ( name) {
999
- self . log_print ( format ! ( "Error! {e}\n " ) )
1021
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
1022
+ self . stack . push ( Type :: Error ( "rm" . to_string ( ) ) ) ;
1000
1023
}
1001
1024
}
1002
1025
@@ -1005,7 +1028,8 @@ impl Executor {
1005
1028
let to = self . pop_stack ( ) . get_string ( ) ;
1006
1029
let from = self . pop_stack ( ) . get_string ( ) ;
1007
1030
if let Err ( e) = fs:: rename ( from, to) {
1008
- self . log_print ( format ! ( "Error! {e}\n " ) )
1031
+ self . log_print ( format ! ( "Error! {e}\n " ) ) ;
1032
+ self . stack . push ( Type :: Error ( "rename" . to_string ( ) ) ) ;
1009
1033
}
1010
1034
}
1011
1035
0 commit comments