| 1 |
autoload :Pathname, 'pathname' |
| 2 |
|
| 3 |
module MRuby |
| 4 |
module Documentation |
| 5 |
def Documentation.update_opcode_md |
| 6 |
mrubydir = Pathname(MRUBY_ROOT) |
| 7 |
path_ops_h = mrubydir + "include/mruby/ops.h" |
| 8 |
path_opcode_md = mrubydir + "doc/internal/opcode.md" |
| 9 |
|
| 10 |
opspecs = { |
| 11 |
"Z" => { prefix: "", modified: "-" }, |
| 12 |
"B" => { prefix: "\'" }, |
| 13 |
"BB" => { prefix: "\"" }, |
| 14 |
"BBB" => { prefix: "\"" }, |
| 15 |
"BS" => { prefix: "\'" }, |
| 16 |
"BSS" => { prefix: "\'" }, |
| 17 |
"S" => { prefix: "" }, |
| 18 |
"W" => { prefix: "" }, |
| 19 |
} |
| 20 |
|
| 21 |
diff = "" |
| 22 |
|
| 23 |
spliter = <<~'SPLITER' |
| 24 |
| Instruction Name | Operand type | Semantics | |
| 25 |
|--------------------|----------------|------------------------------------------------------------| |
| 26 |
SPLITER |
| 27 |
|
| 28 |
diff = path_opcode_md.read.sub(/^#{Regexp.escape spliter}.*?(?=\z|^$\n)/m) do |
| 29 |
repl = spliter |
| 30 |
|
| 31 |
ops = path_ops_h.read |
| 32 |
ops.scan(/^\s*OPCODE\s*\(\s*(\w+)\s*,\s*(\w+)\s*\)\s*(?:\/\*\s*(.*?)\s*\*\/\s*)?/).each do |ins, opr, cmt| |
| 33 |
if cmt |
| 34 |
cmt.sub!(/\s*#.*/, "") |
| 35 |
cmt.sub!(/\b(?=L_\w+\b)/, "OP_") |
| 36 |
cmt.gsub!(/\b(Irep|Pool|R|Syms)\[([^\[\]]+)\]/, "\\1(\\2)") |
| 37 |
cmt.gsub!(/[\\\|]/) { |m| "\\#{m}" } # Ruby-2.5 is not support "Numbered block parameter" |
| 38 |
end |
| 39 |
spec = opspecs[opr] or raise "unknown operand type: #{opr}" |
| 40 |
item = format(%(| %-18s | %-14s | %-58s |\n), "`OP_#{ins}`", "`#{spec[:modified] || opr}`", cmt && "`#{cmt}`") |
| 41 |
repl << item |
| 42 |
end |
| 43 |
|
| 44 |
repl |
| 45 |
end |
| 46 |
|
| 47 |
path_opcode_md.binwrite diff |
| 48 |
end |
| 49 |
end |
| 50 |
end |