1- use heraclitus_compiler :: prelude :: * ;
2- use crate :: docs :: module :: DocumentationModule ;
1+ use crate :: fragments ;
2+ use crate :: raw_fragment ;
33use crate :: modules:: expression:: expr:: Expr ;
44use crate :: modules:: types:: { Type , Typed } ;
55use crate :: translate:: module:: TranslateModule ;
66use crate :: utils:: metadata:: { ParserMetadata , TranslateMetadata } ;
7+ use crate :: modules:: prelude:: * ;
8+ use heraclitus_compiler:: prelude:: * ;
79
810#[ derive( Debug , Clone ) ]
911pub struct LinesInvocation {
@@ -12,7 +14,7 @@ pub struct LinesInvocation {
1214
1315impl Typed for LinesInvocation {
1416 fn get_type ( & self ) -> Type {
15- Type :: Array ( Box :: new ( Type :: Text ) )
17+ Type :: array_of ( Type :: Text )
1618 }
1719}
1820
@@ -21,7 +23,7 @@ impl SyntaxModule<ParserMetadata> for LinesInvocation {
2123
2224 fn new ( ) -> Self {
2325 LinesInvocation {
24- path : Box :: new ( None )
26+ path : Box :: new ( None ) ,
2527 }
2628 }
2729
@@ -33,7 +35,10 @@ impl SyntaxModule<ParserMetadata> for LinesInvocation {
3335 syntax ( meta, & mut path) ?;
3436 token ( meta, ")" ) ?;
3537 if path. get_type ( ) != Type :: Text {
36- let msg = format ! ( "Expected value of type 'Text' but got '{}'" , path. get_type( ) ) ;
38+ let msg = format ! (
39+ "Expected value of type 'Text' but got '{}'" ,
40+ path. get_type( )
41+ ) ;
3742 return error ! ( meta, tok, msg) ;
3843 }
3944 self . path = Box :: new ( Some ( path) ) ;
@@ -42,34 +47,30 @@ impl SyntaxModule<ParserMetadata> for LinesInvocation {
4247}
4348
4449impl TranslateModule for LinesInvocation {
45- fn translate ( & self , meta : & mut TranslateMetadata ) -> String {
46- let name = format ! ( "__AMBER_ARRAY_{}" , meta. gen_value_id( ) ) ;
50+ fn translate ( & self , meta : & mut TranslateMetadata ) -> FragmentKind {
4751 let temp = format ! ( "__AMBER_LINE_{}" , meta. gen_value_id( ) ) ;
48- let path = ( * self . path ) . as_ref ( )
49- . map ( |p| p. translate_eval ( meta, false ) )
50- . unwrap_or_default ( ) ;
51- let quote = meta. gen_quote ( ) ;
52- let dollar = meta. gen_dollar ( ) ;
52+ let path = ( * self . path )
53+ . as_ref ( )
54+ . map ( |p| p. translate ( meta) )
55+ . expect ( "Cannot read lines without provided path" ) ;
5356 let indent = TranslateMetadata :: single_indent ( ) ;
54- let block = [
55- format ! ( "{name}=()" ) ,
56- format ! ( "while IFS= read -r {temp}; do" ) ,
57- format ! ( "{indent}{name}+=( \" $ {temp}\" ) " ) ,
58- format ! ( "done <{path}" ) ,
59- ] . join ( " \n " ) ;
60- meta . stmt_queue . push_back ( block ) ;
61- format ! ( "{quote}{dollar}{{{name}[@]}}{quote}" )
57+ let id = meta . gen_value_id ( ) ;
58+ let value = meta . push_intermediate_variable_lazy ( "__array" , Some ( id ) , Type :: array_of ( Type :: Text ) , FragmentKind :: Empty ) ;
59+ meta . stmt_queue . extend ( [
60+ raw_fragment ! ( "while IFS= read -r {temp}; do " ) ,
61+ raw_fragment ! ( "{indent}{}+=( \" ${} \" )" , value . get_name ( ) , temp ) ,
62+ fragments ! ( "done <" , path ) ,
63+ ] ) ;
64+ value . to_frag ( )
6265 }
6366}
6467
6568impl LinesInvocation {
66- pub fn surround_iter ( & self , meta : & mut TranslateMetadata , name : & str ) -> ( String , String ) {
67- let path = ( * self . path ) . as_ref ( )
69+ pub fn translate_path ( & self , meta : & mut TranslateMetadata ) -> FragmentKind {
70+ ( * self . path )
71+ . as_ref ( )
6872 . map ( |p| p. translate ( meta) )
69- . unwrap_or_default ( ) ;
70- let prefix = format ! ( "while IFS= read -r {name}; do" ) ;
71- let suffix = format ! ( "done <{path}" ) ;
72- ( prefix, suffix)
73+ . expect ( "Cannot read lines without provided path in iterator loop" )
7374 }
7475}
7576
0 commit comments