-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
84 lines (68 loc) · 1.67 KB
/
Rakefile
File metadata and controls
84 lines (68 loc) · 1.67 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'json'
Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.test_files = FileList['test/*_test.rb']
end
task test: :docs
desc 'Generate standard library documentation.'
task :docs do |t|
docs = {}
Dir.chdir('lib/spitewaste/libspw') do |d|
Dir['*.spw'].sort.each do |path|
mod = File.basename path, '.spw'
docs[mod] = extract_docs path
end
File.open('docs.json', ?w) { |f|
f.write JSON.pretty_generate docs
}
end
end
def extract_docs path
docs = {}
buffer = ''
File.open(path).each_line do |line|
if line.strip.empty?
buffer.clear
elsif line[/^([^_]\S+):/]
docs[$1] = parse_doc buffer.dup unless buffer.empty?
buffer.clear
elsif line[/^;/]
buffer << line
end
end
docs.sort.to_h
end
def strpack s
s.bytes.zip(0..).sum { |b, e| b * 128 ** e }
end
def parse_stack s
s.scan(/"[^"]*"|\S+/).map { |v|
begin
Integer v
rescue
if v[/R\((-?\d+),(-?\d+)\)/]
n, d = $1.to_i, $2.to_i
s = (n<=>0) * (d<=>0)
(n.abs * 2**31 + d.abs) * 2 + (s == -1 ? 0 : 1)
else
strpack v.delete %('")
end
end
}
end
StackRx = /(?<=\[)[^\]]*(?=\])/ # match [.*], but don't capture the brackets
def parse_doc doc
# strip leading comment to margin and any implementation details (!)
doc.gsub!(/^; */, '').gsub!(/^!.+\n/, '')
head, specs = doc.split "\n\n"
*desc, effect = head.split ?\n
desc *= ?\n
cases = []
specs.scan(StackRx).each_slice 2 do |spec|
cases << spec.map { parse_stack _1 }
end if specs
{full: doc, desc: desc, effect: effect, cases: cases}
end
task default: :test