kurangu 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/bin/kurangu +5 -0
  3. data/lib/kurangu.rb +88 -0
  4. data/lib/trace.rb +52 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 98ec8f13829b78125818e91a6b78163f40b295a6
4
+ data.tar.gz: f06990a2e564dd4531e7813fa415911ddc5b40f1
5
+ SHA512:
6
+ metadata.gz: b9a82fe613d3926dc7acbd4a71bd4b88c910bbdf9b62a010d3d5eaf7f31dfb06ee6512aec26c531466fd40a74ff1350da7528a636ddb583e0f5179d0f3185274
7
+ data.tar.gz: 4594ab0b230bad1bd51a65ec995de2c42ba049eba823fdcbd7816800544c8e204235529c602ee67f6c470574816127f14461e673a40dbd243303352cd8912258
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kurangu'
4
+
5
+ Kurangu.new.run(ARGV[0])
@@ -0,0 +1,88 @@
1
+ require 'rbconfig'
2
+ require 'fileutils'
3
+
4
+ class Kurangu
5
+ def generate_annotations(input_file)
6
+ ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
7
+ trace_file = File.expand_path("lib/trace.rb")
8
+ puts "\ngenerating annotations\n"
9
+ system(ruby, "-r", trace_file, input_file)
10
+ end
11
+
12
+ def print_annotations
13
+ puts "\nthe annotations generated are:\n"
14
+ File.open("annotations_paths.txt", "r") do |f|
15
+ f.each_line do |annotation_file|
16
+ puts annotation_file
17
+ File.open(annotation_file, "r") do |f|
18
+ f.each_line.with_index do |line, index|
19
+ if index > 2
20
+ puts line
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def generate_annotated_files
29
+ File.open("annotations_paths.txt", "r") do |f|
30
+ f.each_line do |annotation_path|
31
+ original_path = annotation_path.chomp('.annotations')
32
+ annotated_path = "#{original_path}.annotated"
33
+ puts "\ngenerating annotated file #{annotated_path}\n"
34
+ lines = ["require 'rdl'\n", "require 'types/core'\n"]
35
+ annotations = Hash.new
36
+ File.open(annotation_path, "r") do |f|
37
+ f.each_line do |line|
38
+ split = line.split(" ", 2)
39
+ index = split[0].to_i
40
+ annotations[index] = "#{split[1]}\n"
41
+ end
42
+ end
43
+ lines << "\n"
44
+ File.open(original_path, "r") do |f|
45
+ f.each_line.with_index do |line, index|
46
+ whitespace = line.chomp(line.lstrip)
47
+ if annotations.key?(index + 1)
48
+ lines << "#{whitespace}extend RDL::Annotate\n"
49
+ lines << "#{whitespace}#{annotations[index + 1]}"
50
+ end
51
+ lines << line
52
+ end
53
+ end
54
+ IO.write(annotated_path, lines.join())
55
+ end
56
+ end
57
+ end
58
+
59
+ def apply_annotation(annotated_path, original_path)
60
+ puts "\napplying annotations for #{original_path}\n"
61
+ FileUtils.mv(annotated_path, original_path)
62
+ end
63
+
64
+ def apply_annotations
65
+ File.open("annotations_paths.txt", "r") do |f|
66
+ f.each_line do |annotation_path|
67
+ original_path = annotation_path.chomp('.annotations')
68
+ annotated_path = "#{original_path}.annotated"
69
+ apply_annotation(annotated_path, original_path)
70
+ end
71
+ end
72
+ end
73
+
74
+ def run_rdl(input_file)
75
+ puts "\nrunning rdl\n"
76
+ ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
77
+ system(ruby, input_file)
78
+ end
79
+
80
+ def run(input_file_path)
81
+ input_file = File.expand_path(input_file_path)
82
+ self.generate_annotations(input_file)
83
+ self.print_annotations
84
+ self.generate_annotated_files
85
+ self.apply_annotations
86
+ self.run_rdl(input_file)
87
+ end
88
+ end
@@ -0,0 +1,52 @@
1
+ require 'set'
2
+ require 'yaml'
3
+
4
+ stack = Hash.new Array.new
5
+ parameter_types = Hash.new Hash.new Set.new
6
+ return_types = Hash.new Array.new
7
+ parameter_list = Hash.new Array.new
8
+ signatures = Hash.new
9
+ paths = Set.new
10
+
11
+ def generate_signature(line, parameters, parameter_types, return_types)
12
+ joined_parameters = parameters.map { |arg| parameter_types[arg].to_a.join(" or ") }
13
+ "#{line} type '(#{joined_parameters.join(", ")}) -> #{return_types.join(" or ")}'"
14
+ end
15
+
16
+ def write_annotations_paths(paths)
17
+ IO.write("annotations_paths.txt", paths.to_a.join("\n"))
18
+ end
19
+
20
+ def write_annotations(path, signatures)
21
+ IO.write(path, signatures.values.join("\n"))
22
+ end
23
+
24
+ trace_return = TracePoint.new(:return) do |t|
25
+ s = "#{t.defined_class}, :#{t.method_id}"
26
+ args = stack[s].pop
27
+ if args
28
+ args.each do |arg, type|
29
+ parameter_types[s][arg].add(type)
30
+ end
31
+ return_types[s] << t.return_value.class
32
+ parameter_list[s] = t.self.method(t.method_id).parameters.map { |a | a[1] }
33
+ line = t.self.method(t.method_id).source_location[1]
34
+ signatures[s] = generate_signature(line, parameter_list[s], parameter_types[s], return_types[s])
35
+ path = "#{t.path}.annotations"
36
+ write_annotations_paths(paths.add(path))
37
+ write_annotations(path, signatures)
38
+ end
39
+ end
40
+
41
+ trace_call = TracePoint.new(:call) do |t|
42
+ s = "#{t.defined_class}, :#{t.method_id}"
43
+ args = t.binding.eval("local_variables").inject({}) do |vars, name|
44
+ value = t.binding.eval name.to_s
45
+ vars[name] = value.class
46
+ vars
47
+ end
48
+ stack[s] << args
49
+ end
50
+
51
+ trace_return.enable
52
+ trace_call.enable
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kurangu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arpith Siromoney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Runtime inferrence of RDL type annotations
14
+ email: arpith@feedreader.co
15
+ executables:
16
+ - kurangu
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/kurangu
21
+ - lib/kurangu.rb
22
+ - lib/trace.rb
23
+ homepage: https://github.com/arpith/kurangu
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.5.1
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Generate type annotations
47
+ test_files: []