changeset 5:6f5c1e8db40e

completion change_OP_to_cbc.pl
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Fri, 05 Oct 2018 20:34:17 +0900
parents 352169b06523
children 065d32ea977f
files cbctools/change_OP_to_cbc.pl
diffstat 1 files changed, 34 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/cbctools/change_OP_to_cbc.pl	Fri Oct 05 19:31:02 2018 +0900
+++ b/cbctools/change_OP_to_cbc.pl	Fri Oct 05 20:34:17 2018 +0900
@@ -2,80 +2,59 @@
 use strict;
 use warnings;
 
-my $cbc_file = shift or die;
+my $cbc_file = shift or die; # src/core/cbc-interp.cbc
 
 open my $fh, '<',$cbc_file;
 
 my @cbc_lines= <$fh>;
 my @rewritec = ();
+my $indent = " " x 12;
+my $none_left_blanket = 0;
 
 for (my $i=0; $i < scalar(@cbc_lines); $i++){
 
-    # add check 
-    my $check_left_blanket = 0;
-    my $is_OP_declaration = 0;
-
     # check OP(.*) {  codes
-    if ($cbc_lines[$i] =~ /OP\((.*)\):/ ){
+    if ($cbc_lines[$i] =~ /^\s+OP\((.*)\):/ ){
         my $opcode = $1;
-        $check_left_blanket = 1 if ($cbc_lines[$i] =~ /{/);
+        $none_left_blanket = $cbc_lines[$i] =~ /{/ ? 0 : 1;
         # transrate OP(HOGE) to __code hoge(INTERP *i){
-        $cbc_lines[$i]  = "__code ".$opcode."(INTERP *i){\n";
-        $i++;
-        $is_OP_declaration++;
+        $cbc_lines[$i]  = $indent. "__code ".$opcode."(INTERP *i){\n";
+        push @rewritec,$cbc_lines[$i];
 
-        #
-        # gotoしない場合の関数を数え上げて関数の内容を保存する(案1)
-        # 次のopcodeにgotoする
-        #
-        my $j = $i+1;
-        #my @not_break_opcodes = ();
-        my $next_opcode = 0;
+        # 次の行に移動
+        $i++;
 
-        while ($j < @cbc_lines && $cbc_lines[$j] !~ /goto/){
-            if ($cbc_lines[$j] =~ /OP\(/){
-                #        print "$j || $cbc_lines[$j]";
-                $next_opcode = $j;
-                last;
-                #push @not_break_opcodes,$j;
-            }
-            $j++;
-        }
-        insert_next_code_seg(\@cbc_lines,\@rewritec,$i,$next_opcode);
-        #print $j-$i."\n";
-    }
-
-
-    # OP(hoge):
-    # OP(foo):
-    # OP(oiyp) { fooo; goto NEXT} なコードの場合,hogeとfooに同じ内容の関数を追加する
+        # この行がOP()だった場合
+        #  OP(DEPRECATED_4):
+        #  OP(DEPRECATED_5): <- $i
+        # このような宣言になっているので$iにgotoするように関数を書き直し
+        # $iの部分の関数定義を次ループでするために一行戻して再ループ
 
-    #if ($is_OP_declaration){
-    #    my $same_code_count = 0;
-    #    my $j = $i+1;
-    #    while ($j < @cbc_lines && $cbc_lines[$j] =~ /OP\(/){
-    #        #print "$cbc_lines[$j]";
-    #        #print "cbc_lines[j]";
-    #        $same_code_count++;
-    #        $j++;
-    #    }
+        if ($cbc_lines[$i] =~ /^\s+OP\((.*)\):/){
+            push @rewritec,"$indent   goto $1(i);\n";
+            insert_right_blanket();
+            $i--;
+            next;
+        }
 
-    #    my @same_function_ops = ();
-    #    while ($same_code_count < 0){
-    #        push @same_function_ops,$cbc_lines[$j-$same_code_count];
-    #        $same_code_count--;
-    #    }
-    #}
-
-    if ($check_left_blanket){
-
+        # 例外だったらかえってこないはずなのでgoto
+        if ( $cbc_lines[$i] =~ /MVM_exception_throw_adhoc/ && $cbc_lines[$i+1] =~ /OP\(/){
+            push @rewritec, change_i($cbc_lines[$i]);
+            insert_right_blanket();
+            next;
+        }
     }
 
     push @rewritec,change_i($cbc_lines[$i]);
+
+    if ($cbc_lines[$i+1] =~ /OP/ && $none_left_blanket){
+        insert_right_blanket();
+        $none_left_blanket = 0;
+    }
 }
 
 for (@rewritec){
-    #print "$_";
+    print "$_";
 }
 
 sub change_i {
@@ -87,9 +66,6 @@
     return $str;
 }
 
-sub insert_next_code_seg {
-    my ($cbc_lines,$rewritec,$now_line,$next_opcode_line) = @_;;
-
-    if (($next_opcode_line - $now_line) == 1){
-    }
+sub insert_right_blanket {
+    push @rewritec,"$indent}\n";
 }