@@ -597,7 +597,20 @@ impl Compiler {
597
597
self . in_loop = false ;
598
598
self . in_function_def = true ;
599
599
let mut flags = self . enter_function ( name, args) ?;
600
- self . compile_statements ( body) ?;
600
+
601
+ let doc = get_doc ( body) ;
602
+ let ( new_body, doc_str) = match doc {
603
+ Some ( val) => {
604
+ if let Some ( ( _, body_rest) ) = body. split_first ( ) {
605
+ ( body_rest, val)
606
+ } else {
607
+ ( body, "" . to_string ( ) )
608
+ }
609
+ }
610
+ None => ( body, "" . to_string ( ) ) ,
611
+ } ;
612
+
613
+ self . compile_statements ( new_body) ?;
601
614
602
615
// Emit None at end:
603
616
self . emit ( Instruction :: LoadConst {
@@ -644,6 +657,10 @@ impl Compiler {
644
657
} ) ;
645
658
}
646
659
660
+ self . emit ( Instruction :: LoadConst {
661
+ value : bytecode:: Constant :: String { value : doc_str } ,
662
+ } ) ;
663
+
647
664
self . emit ( Instruction :: LoadConst {
648
665
value : bytecode:: Constant :: Code {
649
666
code : Box :: new ( code) ,
@@ -1511,6 +1528,19 @@ impl Compiler {
1511
1528
}
1512
1529
}
1513
1530
1531
+ fn get_doc ( body : & [ ast:: LocatedStatement ] ) -> Option < String > {
1532
+ if let Some ( val) = body. get ( 0 ) {
1533
+ if let ast:: Statement :: Expression { ref expression } = val. node {
1534
+ if let ast:: Expression :: String { ref value } = expression {
1535
+ if let ast:: StringGroup :: Constant { ref value } = value {
1536
+ return Some ( value. to_string ( ) ) ;
1537
+ }
1538
+ }
1539
+ }
1540
+ }
1541
+ None
1542
+ }
1543
+
1514
1544
#[ cfg( test) ]
1515
1545
mod tests {
1516
1546
use super :: Compiler ;
0 commit comments