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