Annotation of nono/util/merge_ops/merge_ops.php, revision 1.1.1.1

1.1       root        1: <?php
                      2: //
                      3: // nono
                      4: // Copyright (C) 2024 nono project
                      5: // Licensed under nono-license.txt
                      6: //
                      7: 
                      8: // usage: merge_ops.php ops.cpp.new dst_ops.cpp
                      9: 
                     10:        $deffile = preg_split("/\n/", file_get_contents($argv[1]));
                     11:        $cppfile = preg_split("/\n/", file_get_contents($argv[2]));
                     12:        // 末尾に空行が出てしまうので削っておく。
                     13:        if ($cppfile[count($cppfile) - 1] == "") {
                     14:                array_pop($cppfile);
                     15:        }
                     16: 
                     17:        // deffile から 関数名 => array(直前のコメント, ...) の連想配列を作る。
                     18:        // $def = array(
                     19:        //  "nop" => array("// ... NOP"),
                     20:        //  :
                     21:        // );
                     22:        $def = array();
                     23:        for ($n = 0; $n < count($deffile); $n++) {
                     24:                if (preg_match('/^OP_DEF\((\w+)\)/', $deffile[$n], $m)) {
                     25:                        $name = $m[1];
                     26:                        $comms = array();
                     27:                        for ($i = 1; $n - $i >= 0 ; $i++) {
                     28:                                $buf = $deffile[$n - $i];
                     29:                                if (preg_match("|^// |", $buf)) {
                     30:                                        array_unshift($comms, $buf);
                     31:                                } else {
                     32:                                        break;
                     33:                                }
                     34:                        }
                     35:                        $def[$name] = $comms;
                     36:                }
                     37:        }
                     38: 
                     39:        // 見つかった回数
                     40:        $founds = array();
                     41: 
                     42:        // cppfile を辿る。
                     43:        for ($c = 0; $c < count($cppfile); $c++) {
                     44:                if (preg_match('/^OP_DEF\((\w+)\)/', $cppfile[$c], $m)) {
                     45:                        $name = $m[1];
                     46:                        if (isset($def[$name])) {
                     47:                                // すでに処理したやつがもう一度来た?
                     48:                                if (isset($founds[$name])) {
                     49:                                        error("{$def[$name]} appeared again!\n");
                     50:                                }
                     51:                                $founds[$name] = true;
                     52: 
                     53:                                // この直前のコメントの行数を数える。
                     54:                                $i = 0;
                     55:                                while (isset($cppfile[$c - 1 - $i]) &&
                     56:                                       preg_match("|^// |", $cppfile[$c - 1 - $i])) {
                     57:                                        $i++;
                     58:                                }
                     59:                                array_splice($cppfile, $c - $i, $i, $def[$name]);
                     60:                                $c = $c - $i + count($def[$name]);
                     61:                        } else {
                     62:                                array_splice($cppfile, $c, 0, "// # removed (merge.sh)");
                     63:                                $c++;
                     64:                        }
                     65:                }
                     66:        }
                     67: 
                     68:        foreach ($cppfile as $buf) {
                     69:                print "{$buf}\n";
                     70:        }
                     71: 
                     72:        // 処理したやつを def[] から削除して、残りが出たら新規追加分。
                     73:        // 本当はそれっぽい位置に入れたい。
                     74:        foreach ($founds as $name => $dummy) {
                     75:                unset($def[$name]);
                     76:        }
                     77:        if (count($def) > 0) {
                     78:                foreach ($def as $name => $comms) {
                     79:                        print "\n";
                     80:                        print "// # added (merge.sh)\n";
                     81:                        foreach ($comms as $line) {
                     82:                                print "{$line}\n";
                     83:                        }
                     84:                        print "OP_DEF({$name})\n";
                     85:                        print "{\n";
                     86:                        print "\tOP_FUNC(unimpl);\n";
                     87:                        print "}\n";
                     88:                }
                     89:        }
                     90: ?>

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.