cauterize 0.0.1.pre1 → 0.0.1.pre5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -1
  3. data/Gemfile +1 -1
  4. data/README.md +0 -2
  5. data/Rakefile +19 -3
  6. data/bin/cauterize +14 -6
  7. data/example/Cauterize +22 -10
  8. data/example/build.sh +15 -2
  9. data/lib/cauterize/base_type.rb +2 -5
  10. data/lib/cauterize/builders.rb +1 -3
  11. data/lib/cauterize/builders/c/buildable.rb +3 -2
  12. data/lib/cauterize/builders/c/builtin.rb +46 -0
  13. data/lib/cauterize/builders/c/enumeration.rb +23 -2
  14. data/lib/cauterize/builders/c/fixed_array.rb +16 -7
  15. data/lib/cauterize/builders/c/group.rb +32 -3
  16. data/lib/cauterize/builders/c/scalar.rb +5 -0
  17. data/lib/cauterize/builders/c/variable_array.rb +1 -1
  18. data/lib/cauterize/builders/cs/buildable.rb +59 -0
  19. data/lib/cauterize/builders/cs/builtin.rb +23 -0
  20. data/lib/cauterize/builders/cs/composite.rb +21 -0
  21. data/lib/cauterize/builders/cs/csarray.rb +32 -0
  22. data/lib/cauterize/builders/cs/enumeration.rb +21 -0
  23. data/lib/cauterize/builders/cs/fixed_array.rb +25 -0
  24. data/lib/cauterize/builders/cs/group.rb +33 -0
  25. data/lib/cauterize/builders/cs/scalar.rb +10 -0
  26. data/lib/cauterize/builders/cs/variable_array.rb +34 -0
  27. data/lib/cauterize/builtin.rb +52 -0
  28. data/lib/cauterize/c_builder.rb +15 -1
  29. data/lib/cauterize/cauterize.rb +44 -13
  30. data/lib/cauterize/composite.rb +3 -3
  31. data/lib/cauterize/cs_builder.rb +53 -0
  32. data/lib/cauterize/enumeration.rb +20 -3
  33. data/lib/cauterize/fixed_array.rb +3 -3
  34. data/lib/cauterize/formatter.rb +7 -3
  35. data/lib/cauterize/group.rb +5 -8
  36. data/lib/cauterize/scalar.rb +16 -7
  37. data/lib/cauterize/variable_array.rb +6 -6
  38. data/lib/cauterize/version.rb +1 -1
  39. data/spec/base_type_spec.rb +133 -125
  40. data/spec/builders/c/buildable_spec.rb +18 -18
  41. data/spec/builders/c/builtin_spec.rb +22 -0
  42. data/spec/builders/c/composite_spec.rb +37 -33
  43. data/spec/builders/c/enumeration_spec.rb +84 -21
  44. data/spec/builders/c/fixed_array_spec.rb +6 -6
  45. data/spec/builders/c/group_spec.rb +97 -90
  46. data/spec/builders/c/scalar_spec.rb +24 -6
  47. data/spec/builders/c/variable_array_spec.rb +37 -37
  48. data/spec/builders/cs/buildable_spec.rb +8 -0
  49. data/spec/builders/cs/composite_spec.rb +32 -0
  50. data/spec/builders/cs/enumeration_spec.rb +33 -0
  51. data/spec/builders/cs/fixed_array_spec.rb +40 -0
  52. data/spec/builders/cs/group_spec.rb +56 -0
  53. data/spec/builders/cs/scalar_spec.rb +7 -0
  54. data/spec/builders/cs/variable_array_spec.rb +46 -0
  55. data/spec/builders_spec.rb +38 -38
  56. data/spec/builtin_spec.rb +46 -0
  57. data/spec/c_builder_spec.rb +116 -102
  58. data/spec/cauterize_spec.rb +8 -1
  59. data/spec/composite_spec.rb +52 -48
  60. data/spec/cs_builder_spec.rb +113 -0
  61. data/spec/enumeration_spec.rb +55 -16
  62. data/spec/fixed_array_spec.rb +7 -9
  63. data/spec/group_spec.rb +81 -76
  64. data/spec/scalar_spec.rb +20 -10
  65. data/spec/spec_helper.rb +103 -94
  66. data/spec/support/shared_examples_for_c_buildables.rb +68 -64
  67. data/spec/variable_array_spec.rb +12 -17
  68. data/{c → support/c}/src/cauterize.c +8 -7
  69. data/support/c/src/cauterize.h +59 -0
  70. data/{c → support/c}/src/cauterize_debug.h +0 -0
  71. data/support/c/src/cauterize_util.h +49 -0
  72. data/{c → support/c}/test/greatest.h +0 -0
  73. data/{c → support/c}/test/test.c +0 -0
  74. data/support/cs/src/CauterizeCompositeFormatter.cs +34 -0
  75. data/support/cs/src/CauterizeContainerFormatter.cs +18 -0
  76. data/support/cs/src/CauterizeEnumFormatter.cs +67 -0
  77. data/support/cs/src/CauterizeException.cs +15 -0
  78. data/support/cs/src/CauterizeFixedArrayFormatter.cs +39 -0
  79. data/support/cs/src/CauterizeFormatter.cs +40 -0
  80. data/support/cs/src/CauterizeGroupFormatter.cs +46 -0
  81. data/support/cs/src/CauterizePrimitiveFormatter.cs +33 -0
  82. data/support/cs/src/CauterizeTypeFormatterFactory.cs +39 -0
  83. data/support/cs/src/CauterizeTypes.cs +107 -0
  84. data/support/cs/src/CauterizeVariableArrayFormatter.cs +49 -0
  85. data/support/cs/src/ICauterizeTypeFormatter.cs +12 -0
  86. data/support/cs/src/OrderAttribute.cs +50 -0
  87. data/support/cs/src/PrimitiveSupport.cs +134 -0
  88. data/support/cs/src/SerializedRepresentationAttribute.cs +24 -0
  89. data/support/cs/test/CauterizeCompositeFormatterTest.cs +59 -0
  90. data/support/cs/test/CauterizeEnumFormatterTest.cs +110 -0
  91. data/support/cs/test/CauterizeFixedArrayFormatterTest.cs +91 -0
  92. data/support/cs/test/CauterizeFormatterTest.cs +40 -0
  93. data/support/cs/test/CauterizeGroupFormatterTest.cs +147 -0
  94. data/support/cs/test/CauterizeIntegrationTest.cs +129 -0
  95. data/support/cs/test/CauterizePrimitiveFormatterTest.cs +98 -0
  96. data/support/cs/test/CauterizeTypeFormatterFactoryTest.cs +73 -0
  97. data/support/cs/test/CauterizeVariableArrayFormatterTest.cs +130 -0
  98. data/support/cs/test/OrderAttributeTest.cs +39 -0
  99. data/support/cs/test/SerializedRepresentationAttributeTest.cs +39 -0
  100. metadata +68 -10
  101. data/c/src/cauterize.h +0 -46
  102. data/c/src/cauterize_util.h +0 -7
@@ -6,6 +6,11 @@ module Cauterize
6
6
  @blueprint.name.to_s
7
7
  end
8
8
 
9
+ def typedef_decl(formatter)
10
+ tn_bldr = Builders.get(:c, @blueprint.type_name)
11
+ formatter << "typedef #{tn_bldr.render} #{render};"
12
+ end
13
+
9
14
  def declare(formatter, sym)
10
15
  formatter << "#{render} #{sym};"
11
16
  end
@@ -7,7 +7,7 @@ module Cauterize
7
7
  end
8
8
 
9
9
  def declare(formatter, sym)
10
- formatter << "struct #{@blueprint.name} #{sym};"
10
+ formatter << "#{render} #{sym};"
11
11
  end
12
12
 
13
13
  def packer_defn(formatter)
@@ -0,0 +1,59 @@
1
+ module Cauterize
2
+ module Builders
3
+ module CS
4
+ # sym - symbol - the lexical symbol associated with the generated entity
5
+ # sig - signature - the C signature associated with the entity
6
+ # proto - prototype - the C prototype associated with the entity
7
+ # defn - definition - the C definition associated with the entity
8
+ REQUIRED_METHODS = [
9
+ ]
10
+
11
+ class BuildableException < Exception; end
12
+
13
+ class Buildable
14
+ def initialize(blueprint)
15
+ @blueprint = blueprint
16
+ end
17
+
18
+ alias_method :orig_method_missing, :method_missing
19
+
20
+ def method_missing(sym, *args)
21
+ sym_required = REQUIRED_METHODS.include?(sym)
22
+
23
+ if sym_required
24
+ raise BuildableException.new("Classes deriving Buildable must implement the method #{sym}.")
25
+ else
26
+ orig_method_missing(sym, *args)
27
+ end
28
+ end
29
+
30
+ # Things below here are tested in shared_examples_for_c_buildables #
31
+ ####################################################################
32
+
33
+ # Methods that are pretty much the same for everyone.
34
+ # WFIXME what do I do with these???
35
+ # def packer_sym; "Pack_#{@blueprint.name}" end
36
+ # def packer_sig; "CAUTERIZE_STATUS_T #{packer_sym}(struct Cauterize * dst, #{render} * src)" end
37
+ # def packer_proto(formatter)
38
+ # formatter << packer_sig + ";"
39
+ # end
40
+
41
+ # def unpacker_sym; "Unpack_#{@blueprint.name}" end
42
+ # def unpacker_sig; "CAUTERIZE_STATUS_T #{unpacker_sym}(struct Cauterize * src, #{render} * dst)" end
43
+ # def unpacker_proto(formatter)
44
+ # formatter << unpacker_sig + ";"
45
+ # end
46
+
47
+ # These are only different in a few type varieties.
48
+ def render
49
+ @blueprint.name.to_s.camel
50
+ end
51
+ def declare(formatter, sym)
52
+ formatter << "public #{render} #{sym.to_s.camel} { get; set; }"
53
+ end
54
+ def class_defn(formatter); nil end
55
+ def enum_defn(formatter); nil end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ module Cauterize::Builders::CS
2
+ class BuiltIn < Buildable
3
+ @@CS_TYPE_MAPPING = {
4
+ 1 => {signed: "SByte", unsigned: "Byte"},
5
+ 2 => {signed: "Int16", unsigned: "UInt16"},
6
+ 4 => {signed: "Int32", unsigned: "UInt32"},
7
+ 8 => {signed: "Int64", unsigned: "UInt64"},
8
+ }
9
+
10
+ def render
11
+ render_cstype
12
+ end
13
+
14
+ private
15
+
16
+ def render_cstype
17
+ s_key = @blueprint.is_signed ? :signed : :unsigned
18
+ @@CS_TYPE_MAPPING[@blueprint.byte_length][s_key]
19
+ end
20
+ end
21
+ end
22
+
23
+ Cauterize::Builders.register(:cs, Cauterize::BuiltIn, Cauterize::Builders::CS::BuiltIn)
@@ -0,0 +1,21 @@
1
+ module Cauterize::Builders::CS
2
+ class Composite < Buildable
3
+
4
+ def render_parent
5
+ "CauterizeComposite"
6
+ end
7
+
8
+ def class_defn(formatter)
9
+ formatter << "public class #{render} : #{render_parent}"
10
+ formatter.braces do
11
+ @blueprint.fields.values.each_with_index do |field, i|
12
+ formatter << "[Order(#{i})]"
13
+ Cauterize::Builders.get(:cs, field.type).declare(formatter, field.name)
14
+ end
15
+ end
16
+ formatter.blank_line
17
+ end
18
+ end
19
+ end
20
+
21
+ Cauterize::Builders.register(:cs, Cauterize::Composite, Cauterize::Builders::CS::Composite)
@@ -0,0 +1,32 @@
1
+ module Cauterize::Builders::CS
2
+ class CSArray < Buildable
3
+ def class_defn(formatter)
4
+ formatter << "public class #{render} : #{render_parent}"
5
+ formatter.braces do
6
+ extra_array_declarations(formatter)
7
+ formatter.blank_line
8
+ formatter << simple_constructor_line
9
+ formatter.braces do
10
+ formatter << "Allocate(#{size});"
11
+ end
12
+ formatter.blank_line
13
+ formatter << "public #{render}(#{ty_bldr.render}[] data)"
14
+ formatter.braces do
15
+ formatter << "Allocate(data);"
16
+ end
17
+ formatter.blank_line
18
+ size_defn(formatter)
19
+ end
20
+ formatter.blank_line
21
+ end
22
+
23
+ protected
24
+
25
+ def extra_array_declarations(formatter)
26
+ end
27
+
28
+ def ty_bldr
29
+ @ty_bldr ||= Cauterize::Builders.get(:cs, @blueprint.array_type)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ module Cauterize
2
+ module Builders
3
+ module CS
4
+ class Enumeration < Buildable
5
+ def enum_defn(formatter)
6
+ rep_builder = Builders.get(:cs, @blueprint.representation)
7
+ formatter << "[SerializedRepresentation(typeof(#{rep_builder.render}))]"
8
+ formatter << "public enum #{render}"
9
+ formatter.braces do
10
+ @blueprint.values.values.each do |v|
11
+ formatter << "#{v.name.to_s.camel} = #{v.value},"
12
+ end
13
+ end
14
+ formatter.append(";")
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ Cauterize::Builders.register(:cs, Cauterize::Enumeration, Cauterize::Builders::CS::Enumeration)
@@ -0,0 +1,25 @@
1
+ module Cauterize::Builders::CS
2
+ class FixedArray < CSArray
3
+
4
+ def render_parent
5
+ "CauterizeFixedArrayTyped<#{ty_bldr.render}>"
6
+ end
7
+
8
+ def simple_constructor_line
9
+ "public #{render}()"
10
+ end
11
+
12
+ def size_defn(formatter)
13
+ formatter << "protected override int Size"
14
+ formatter.braces do
15
+ formatter << "get { return #{size}; }"
16
+ end
17
+ end
18
+
19
+ def size
20
+ @blueprint.array_size
21
+ end
22
+ end
23
+ end
24
+
25
+ Cauterize::Builders.register(:cs, Cauterize::FixedArray, Cauterize::Builders::CS::FixedArray)
@@ -0,0 +1,33 @@
1
+ module Cauterize::Builders::CS
2
+ class Group < Buildable
3
+ def initialize(blueprint)
4
+ super(blueprint)
5
+ @tag_enum = blueprint.tag_enum
6
+ end
7
+
8
+ def render_parent
9
+ "CauterizeGroup"
10
+ end
11
+
12
+ def class_defn(formatter)
13
+ formatter << "public class #{render} : #{render_parent}"
14
+ formatter.braces do
15
+ formatter << "[Order(0)]"
16
+ Cauterize::Builders.get(:cs, @tag_enum).declare(formatter, "Type")
17
+ formatter.blank_line
18
+ @blueprint.fields.values.each_with_index do |field, i|
19
+ b = Cauterize::Builders.get(:cs, field.type)
20
+ if b
21
+ formatter << "[Order(#{i+1})]"
22
+ b.declare(formatter, field.name)
23
+ else
24
+ formatter << "/* No data associated with '#{field.name}'. */"
25
+ end
26
+ end
27
+ end
28
+ formatter.blank_line
29
+ end
30
+ end
31
+ end
32
+
33
+ Cauterize::Builders.register(:cs, Cauterize::Group, Cauterize::Builders::CS::Group)
@@ -0,0 +1,10 @@
1
+ module Cauterize::Builders::CS
2
+ class Scalar < Buildable
3
+ def render
4
+ bldr = Cauterize::Builders.get(:cs, @blueprint.type_name)
5
+ bldr.render
6
+ end
7
+ end
8
+ end
9
+
10
+ Cauterize::Builders.register(:cs, Cauterize::Scalar, Cauterize::Builders::CS::Scalar)
@@ -0,0 +1,34 @@
1
+ module Cauterize::Builders::CS
2
+ class VariableArray < CSArray
3
+
4
+ def render_parent
5
+ "CauterizeVariableArrayTyped<#{ty_bldr.render}>"
6
+ end
7
+
8
+ def simple_constructor_line
9
+ "public #{render}(int size)"
10
+ end
11
+
12
+ def size_defn(formatter)
13
+ formatter << "protected override int MaxSize"
14
+ formatter.braces do
15
+ formatter << "get { return #{size_type}.MaxValue; }"
16
+ end
17
+ end
18
+
19
+ def size
20
+ return "size"
21
+ end
22
+
23
+ protected
24
+ def extra_array_declarations(formatter)
25
+ formatter << "public static Type SizeType = typeof(#{size_type});"
26
+ end
27
+
28
+ def size_type
29
+ Cauterize::Builders.get(:cs, @blueprint.size_type).render
30
+ end
31
+ end
32
+ end
33
+
34
+ Cauterize::Builders.register(:cs, Cauterize::VariableArray, Cauterize::Builders::CS::VariableArray)
@@ -0,0 +1,52 @@
1
+ module Cauterize
2
+ module_function
3
+
4
+ BUILT_IN_TYPES = [
5
+ { :name => :int8, :size => 1, :signed => true },
6
+ { :name => :int16, :size => 2, :signed => true },
7
+ { :name => :int32, :size => 4, :signed => true },
8
+ { :name => :int64, :size => 8, :signed => true },
9
+ { :name => :uint8, :size => 1, :signed => false },
10
+ { :name => :uint16, :size => 2, :signed => false },
11
+ { :name => :uint32, :size => 4, :signed => false },
12
+ { :name => :uint64, :size => 8, :signed => false },
13
+ ]
14
+
15
+ def builtins
16
+ @builtins ||= {}
17
+ end
18
+
19
+ def create_builtins
20
+ BUILT_IN_TYPES.each do |b|
21
+ _b = BuiltIn.new(b[:name])
22
+ _b.byte_length(b[:size])
23
+ _b.is_signed(b[:signed])
24
+ builtins[b[:name]] = _b
25
+ end
26
+ end
27
+
28
+ class BuiltIn < BaseType
29
+ def initialize(name)
30
+ super
31
+ end
32
+
33
+ def is_signed(signed = nil)
34
+ unless signed.nil?
35
+ @is_signed = signed
36
+ else
37
+ @is_signed
38
+ end
39
+ end
40
+
41
+ def byte_length(len = nil)
42
+ unless len.nil?
43
+ @byte_length = len
44
+ else
45
+ @byte_length
46
+ end
47
+ end
48
+ end
49
+
50
+ # Create all the builtin types.
51
+ create_builtins
52
+ end
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  module Cauterize
2
4
  class CBuilder
3
5
  attr_reader :h, :c
@@ -23,18 +25,30 @@ module Cauterize
23
25
  f << "#define #{excluder}"
24
26
  f.blank_line
25
27
  f << %Q{#include <cauterize.h>}
26
- f << %Q{#include <stdint.h>}
28
+ f.blank_line
29
+ f << "#define GEN_VERSION (\"#{Cauterize.get_version}\")"
30
+ f << "#define GEN_DATE (\"#{DateTime.now.to_s}\")"
27
31
  f.blank_line
28
32
 
29
33
  instances = BaseType.all_instances
30
34
  builders = instances.map {|i| Builders.get(:c, i)}
31
35
 
36
+ builders.each { |b| b.typedef_decl(f) }
32
37
  builders.each { |b| b.enum_defn(f) }
33
38
  builders.each { |b| b.struct_proto(f) }
34
39
  builders.each { |b| b.struct_defn(f) }
40
+
41
+ f << "#ifdef __cplusplus"
42
+ f << "extern \"C\" {"
43
+ f << "#endif"
44
+
35
45
  builders.each { |b| b.packer_proto(f) }
36
46
  builders.each { |b| b.unpacker_proto(f) }
37
47
 
48
+ f << "#ifdef __cplusplus"
49
+ f << "}"
50
+ f << "#endif"
51
+
38
52
  f.blank_line
39
53
  f << "#endif /* #{excluder} */"
40
54
 
@@ -4,30 +4,61 @@ lib_path = File.dirname(__FILE__) + "/.."
4
4
  require_all Dir[lib_path + "/**/*.rb"]
5
5
 
6
6
  module Cauterize
7
- # Genearte the C code corresponding to the generated configuration
8
- def self.generate_c(target_dir, desc_file)
9
- Object.new.extend(Cauterize).instance_exec do
10
- # this magic allows us to emit useful exception messages when evaling the
11
- # file. if your description file has errors, you'll be able to find them
12
- # because of this magic.
13
- p = Proc.new {}
14
- eval(File.read(desc_file), p.binding, desc_file)
15
- end
16
- output_prefix = get_name || "generated_interface"
17
7
 
8
+ module_function
9
+ def generate(language, target_dir, desc_file)
10
+ parse_dsl(desc_file)
18
11
  FileUtils.mkdir_p(target_dir)
12
+ output_prefix = get_name || "generated_interface"
13
+ builder = send "make_builder_#{language}".to_sym, target_dir, output_prefix
14
+ builder.build
15
+ end
16
+
17
+ # Generate the C code corresponding to the generated configuration
18
+ def make_builder_c(target_dir, output_prefix)
19
19
  h_file = File.join(target_dir, "#{output_prefix}.h")
20
20
  c_file = File.join(target_dir, "#{output_prefix}.c")
21
21
 
22
- builder = Cauterize::CBuilder.new(h_file, c_file, output_prefix)
23
- builder.build
22
+ Cauterize::CBuilder.new(h_file, c_file, output_prefix)
23
+ end
24
+
25
+ # Generate the CS code corresponding to the generated configuration
26
+ def make_builder_cs(target_dir, output_prefix)
27
+ cs_file = File.join(target_dir, "#{output_prefix}.cs")
28
+
29
+ Cauterize::CSBuilder.new(cs_file, output_prefix)
24
30
  end
25
31
 
26
- def self.get_name
32
+ def get_name
27
33
  @@description_name
28
34
  end
29
35
 
36
+ def get_version
37
+ if defined? @@version
38
+ @@version
39
+ else
40
+ "UNDEFINED"
41
+ end
42
+ end
43
+
30
44
  def set_name(desc_name)
31
45
  @@description_name = desc_name
32
46
  end
47
+
48
+ def set_version(version)
49
+ @@version = version
50
+ end
51
+
52
+ private
53
+ def self.parse_dsl(desc_file)
54
+ Object.new.extend(Cauterize).instance_exec do
55
+ # this magic allows us to emit useful exception messages when evaling the
56
+ # file. if your description file has errors, you'll be able to find them
57
+ # because of this magic.
58
+ p = Proc.new {}
59
+ eval(File.read(desc_file), p.binding, desc_file)
60
+ end
61
+
62
+ end
63
+
33
64
  end