csquare 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
1
+ class CSquare::Generator::Op
2
+ def initialize blueprint, id
3
+ @blueprint = blueprint
4
+ @id = id
5
+ @patterns = {}
6
+ end
7
+
8
+ attr_reader :blueprint, :id, :patterns
9
+
10
+ def inspect
11
+ obj_id = "0x#{(self.object_id << 1).to_s(16)}"
12
+ "#<#{self.class.to_s}:#{obj_id} id=#{@id.inspect} blueprint=#{@blueprint.id.inspect} keys=#{patterns.keys.inspect}>"
13
+ end
14
+
15
+ def decorated args_and_types, return_type, default_type_symbol
16
+
17
+ # If we're dealing with a blueprint containing only one type, convert all LONG typenames to regular typenames
18
+ args_types =
19
+ if blueprint.types.size == 1
20
+ h = {}
21
+ args_and_types.each_pair do |arg, type|
22
+ h[arg] = type == blueprint.long_key ? blueprint.key : type
23
+ end
24
+
25
+ # Also update the return_type if applicable
26
+ return_type = blueprint.key if return_type == blueprint.long_key
27
+ h
28
+ else
29
+ args_and_types
30
+ end
31
+
32
+ # Determine pattern and type symbol
33
+ pattern = select_pattern_clone(args_types, return_type)
34
+
35
+ return nil if pattern.nil? # no match, no need to deal with this expression
36
+
37
+ type_symbol = select_type_symbol(args_types.values, return_type, default_type_symbol)
38
+
39
+ # Replace $0, $1, ...
40
+ args_types.keys.each_with_index do |arg, i|
41
+ if arg =~ /^\*/ && pattern =~ /\$#{i}\./ # Flip *x.y to x->y
42
+ deref_arg = "#{arg[1...arg.size]}->"
43
+ pattern.gsub!(/\$#{i}\./, deref_arg)
44
+ else
45
+ pattern.gsub!(/\$#{i}/, arg.to_s)
46
+ end
47
+ end
48
+
49
+
50
+ #if self.is_a?(CSquare::Generator::AssignOp)
51
+ # STDERR.puts args_types
52
+ # if args_types.has_key?(0) && args_types[0] == :integer && return_type == "LONG_TYPE"
53
+ # require 'pry'
54
+ # binding.pry
55
+ # end
56
+ #end
57
+
58
+
59
+ begin
60
+ # Build an AST for the completed expression
61
+ ast = C::Expression.parse(pattern)
62
+ rescue C::ParseError => e
63
+ STDERR.puts "\nParseError:"
64
+ STDERR.puts pattern.inspect
65
+ raise(e)
66
+ end
67
+
68
+ ast.decorate_calls!(type_symbol, blueprint)
69
+
70
+ # Return type and AST get returned
71
+ if default_type_symbol == type_symbol
72
+ [ast, blueprint.key]
73
+ else
74
+ [ast, blueprint.long_key]
75
+ end
76
+ end
77
+
78
+ def [] t
79
+ @patterns[t]
80
+ end
81
+
82
+ def []= t_or_ary, pattern
83
+ if t_or_ary.is_a?(Array)
84
+ t_or_ary.each do |t|
85
+ @patterns[t] = pattern
86
+ end
87
+ else
88
+ @patterns[t_or_ary] = pattern
89
+ end
90
+ end
91
+
92
+ def has_key? t
93
+ @patterns.has_key?(t)
94
+ end
95
+
96
+ protected
97
+ def select_pattern_clone args_types, return_type
98
+ p = select_pattern(args_types, return_type)
99
+ return p if p.nil?
100
+ p.clone
101
+ end
102
+ end
@@ -0,0 +1,62 @@
1
+ class CSquare::Generator::Type
2
+ def initialize blueprint_obj, id, ctype, options
3
+ @blueprint = blueprint_obj
4
+ @id = id
5
+ @ctype = ctype
6
+ @long_id = options.delete(:long)
7
+ @min = options.delete(:min)
8
+ @max = options.delete(:max)
9
+
10
+ @keys = begin
11
+ k = {}
12
+ all_types = @blueprint.generator.types
13
+ options.each_pair do |typename, type_id|
14
+ k[typename] = type_id
15
+ k["LONG_#{typename}"] = all_types[type_id].long_id
16
+ end
17
+ k
18
+ end
19
+ end
20
+
21
+ attr_reader :id, :long_id, :ctype, :blueprint, :min, :max, :keys
22
+
23
+ def inspect
24
+ obj_id = "0x#{(self.object_id << 1).to_s(16)}"
25
+ min_part = " min=#{@min.inspect}" unless @min.nil?
26
+ max_part = " max=#{@max.inspect}" unless @max.nil?
27
+ "#<#{self.class.to_s}:#{obj_id} #{@id.inspect}=>#{@blueprint.id.inspect} ctype=#{@ctype.inspect} long_id=#{@long_id.inspect} keys=#{@keys.inspect}#{min_part.to_s}#{max_part.to_s}>"
28
+ end
29
+
30
+ def generator
31
+ @blueprint.generator
32
+ end
33
+
34
+ def long_id
35
+ @long_id || @id
36
+ end
37
+
38
+ def long
39
+ @blueprint.types[long_id]
40
+ end
41
+
42
+ # Returns the @keys hash but with a Type in the value instead of a Type id
43
+ def type_keys
44
+ h = {}
45
+ @keys.each_pair do |typename, type_id|
46
+ h[typename] = generator.types[type_id]
47
+ h["LONG_#{typename}"] = h[typename].long unless h[typename].long.nil?
48
+ end
49
+ h
50
+ end
51
+
52
+ # Same as type_keys but returns just the ctype field
53
+ def ctype_keys
54
+ h = {}
55
+ type_keys.each_pair do |typename, type|
56
+ h[typename] = type.ctype
57
+ h["LONG_#{typename}"] = type.long.ctype unless type.long.nil?
58
+ end
59
+ h
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csquare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Woods
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hoe
16
+ requirement: &77989090 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *77989090
25
+ - !ruby/object:Gem::Dependency
26
+ name: cast
27
+ requirement: &77988870 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.2.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *77988870
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &77988630 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.10'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *77988630
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &77988410 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *77988410
58
+ description: ! 'C, then D, C++, C# -- now C^2, simple C templates using Ruby.
59
+
60
+
61
+ Consider this to be a sort of carpenter''s square. We call it C^2, or csquare. It''s
62
+ a simple tool for simple jobs.
63
+
64
+
65
+ This gem was developed for use in NMatrix (part of the SciRuby Project). We wanted
66
+ to be able to write a single function
67
+
68
+ and have it be modified to produce C sources for each datatype (rational, complex,
69
+ integer, float, Ruby object, etc).
70
+
71
+
72
+ It also produces some rudimentary function pointer arrays if you so desire, so that
73
+ these functions can be accessed using
74
+
75
+ array notation.
76
+
77
+
78
+ Experimental! Use at your own risk. Actually, don''t use this at all! It''s extremely
79
+ buggy and probably won''t be useful
80
+
81
+ for your purposes. It''s really custom-designed to handle a specific use case: NMatrix
82
+ dtype templates.'
83
+ email:
84
+ - john.woods@marcottelab.org
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files:
88
+ - README.rdoc
89
+ - Manifest.txt
90
+ - History.txt
91
+ files:
92
+ - Rakefile
93
+ - Gemfile
94
+ - README.rdoc
95
+ - Manifest.txt
96
+ - History.txt
97
+ - lib/csquare.rb
98
+ - lib/csquare/base.rb
99
+ - lib/csquare/cerb.rb
100
+ - lib/csquare/function.rb
101
+ - lib/csquare/generator.rb
102
+ - lib/csquare/generator/type.rb
103
+ - lib/csquare/generator/blueprint.rb
104
+ - lib/csquare/generator/op.rb
105
+ - lib/csquare/generator/assign_op.rb
106
+ - lib/csquare/generator/binary_op.rb
107
+ - lib/csquare/generator/boolean_op.rb
108
+ - lib/csquare/generator/enum.rb
109
+ - lib/csquare/generator/enum/namer.rb
110
+ - lib/csquare/generator/enum/op_namer.rb
111
+ - lib/csquare/generator/enum/sparse_op_namer.rb
112
+ - lib/csquare/generator/index.rb
113
+ - .gemtest
114
+ homepage: http://github.com/mohawkjohn/csquare
115
+ licenses: []
116
+ post_install_message:
117
+ rdoc_options:
118
+ - --main
119
+ - README.rdoc
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project: csquare
136
+ rubygems_version: 1.8.10
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: C, then D, C++, C# -- now C^2, simple C templates using Ruby
140
+ test_files: []