Browse Source

fixed stuff

master
Ludovic 'Archivist' Lagouardette 5 years ago
parent
commit
32d1895f0b
6 changed files with 121 additions and 28 deletions
  1. +11
    -9
      src/andrew.cr
  2. +45
    -12
      src/views/class.ecr
  3. +7
    -0
      src/views/file.ecr
  4. +19
    -0
      src/views/repeat.ecr
  5. +24
    -0
      tests/db_file.yml
  6. +15
    -7
      tests/record.yml

+ 11
- 9
src/andrew.cr View File

@ -11,7 +11,11 @@ end
class FileGenerator class FileGenerator
YAML.mapping( YAML.mapping(
classes: Array(ClassGenerator), classes: Array(ClassGenerator),
includes: Array(String)
includes: Array(String),
namespace: {
type: String,
nilable: true
}
) )
def initialize def initialize
@ -40,6 +44,10 @@ class ClassGenerator
attributeafters: { attributeafters: {
type: Array(AttributeAfterGenerator), type: Array(AttributeAfterGenerator),
nilable: true nilable: true
},
size: {
type: Int32,
nilable: true
} }
) )
@ -50,14 +58,6 @@ class ClassGenerator
@attributeafters = Array(AttributeAfterGenerator).new @attributeafters = Array(AttributeAfterGenerator).new
end end
def push(attr : AttributeGenerator)
attr.class_name = @name
@attributes.push(attr)
end
def push(bitf : BitfieldGenerator)
end
ECR.def_to_s "src/views/class.ecr" ECR.def_to_s "src/views/class.ecr"
end end
@ -112,6 +112,8 @@ class RepeatGenerator
def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String) def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String)
@class_name = "no_class" @class_name = "no_class"
end end
ECR.def_to_s "src/views/repeat.ecr"
end end
class AttributeAfterGenerator class AttributeAfterGenerator

+ 45
- 12
src/views/class.ecr View File

@ -1,24 +1,57 @@
class <%= @name %> { class <%= @name %> {
virtual ~<%= @name %>() = 0; virtual ~<%= @name %>() = 0;
public:
<% if !(@attributes.nil?) %> <% if !(@attributes.nil?) %>
<%- @attributes.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%= attr.to_s %>
<%- end -%>
<%- @attributes.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%-= attr.to_s %>
<%- end -%>
<% end %> <% end %>
<% if !(@attributeafters.nil?) %> <% if !(@attributeafters.nil?) %>
<%- @attributeafters.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%= attr.to_s %>
<%- end -%>
<%- @attributeafters.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%-= attr.to_s %>
<%- end -%>
<% end %> <% end %>
<% if !(@bitfields.nil?) %> <% if !(@bitfields.nil?) %>
<%- @bitfields.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%= attr.to_s %>
<%- end -%>
<%- @bitfields.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%-= attr.to_s %>
<%- end -%>
<% end %> <% end %>
<% if !(@repeats.nil?) %>
<%- @repeats.not_nil!.each do |attr| -%>
<%- attr.class_name = @name -%>
<%-= attr.to_s %>
<%- end -%>
<% end %>
private:
void dummy_function()
{
<% if !@size.nil? %>
<%- sz = @size.not_nil! -%>
<% if !(@attributes.nil?) %>
<%- @attributes.not_nil!.each do |attr|-%>
<%- if attr.start > sz -%>
<%- raise "attribute starts after end: "+attr.name+" in class "+@name -%>
<%- end -%>
static_assert(<%= attr.name %>_end_position()<=<%= sz%>, "attribute past the end");
<%- end -%>
<%- end -%>
<% if !(@bitfields.nil?) %>
<%- @bitfields.not_nil!.each do |attr|-%>
<%- if attr.start > sz -%>
<%- raise "bitfield starts after end: "+attr.name+" in class "+@name -%>
<%- end -%>
static_assert(<%= attr.name %>_end_position()<=<%= sz%>, "attribute past the end");
<%- end -%>
<%- end -%>
<% end %>
}
}; };

+ 7
- 0
src/views/file.ecr View File

@ -3,7 +3,14 @@
#include <%= v%> #include <%= v%>
<%- end -%> <%- end -%>
<%- if !(@namespace.nil?) -%>
namespace <%= @namespace.to_s %> {
<%- end -%>
<%- @classes.each do |v| -%> <%- @classes.each do |v| -%>
<%= v.to_s %> <%= v.to_s %>
<%- end -%>
<%- if !(@namespace.nil?) -%>
}
<%- end -%> <%- end -%>

+ 19
- 0
src/views/repeat.ecr View File

@ -0,0 +1,19 @@
static std::span<<%= @out_type %>> get_<%= @name %>(const <%= class_name %>& alter) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
return std::span<<%= @out_type %>>((<%= @out_type %>*)ptr, get_<%= @count_name %>(alter));
}
static void set_<%= @name %>(<%= class_name %>& alter, size_t index, const <%= @out_type %>& param) {
auto ptr = (char*)&alter;
ptr+=<%= @start %>;
*(<%= @out_type %>*)ptr = param;
}
static constexpr size_t <%= @name %>_position() {
return <%= @start %>;
}
static size_t <%= @name %>_end_position(<%= class_name %>& alter) {
return <%= @start %>+get_<%= @count_name %>(alter)*sizeof(<%= @out_type %>);
}

+ 24
- 0
tests/db_file.yml View File

@ -0,0 +1,24 @@
includes:
- <array>
- <span>
namespace: db
classes:
-
name: database
attributes:
-
name: metadata
start: 0
out_type: std::array<uint8_t, 4096>
-
name: record_cnt
start: 0
out_type: uint64_t
repeats:
-
name: record_list
start: 4096
out_type: std::array<uint8_t, 64>
count_name: record_cnt

+ 15
- 7
tests/record.yml View File

@ -1,8 +1,10 @@
includes: includes:
- <array> - <array>
namespace: db
classes: classes:
- -
name: record_identifier name: record_identifier
size: 24
attributes: attributes:
- -
name: uuid name: uuid
@ -18,6 +20,7 @@ classes:
start: 20 start: 20
- -
name: record name: record
size: 64
attributes: attributes:
- -
name: record_id name: record_id
@ -37,35 +40,40 @@ classes:
name: flags name: flags
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000111
mask: 0b1110000000000000
- -
name: v_flag name: v_flag
out_type: uint16_t out_type: uint16_t
after_name: offset after_name: offset
start: 40 start: 40
mask: 0b0000000000000100
mask: 0b1000000000000000
- -
name: r_flags name: r_flags
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000010
mask: 0b0100000000000000
- -
name: c_flags name: c_flags
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000001
mask: 0b0010000000000000
- -
name: confirmed name: confirmed
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000001
mask: 0b0010000000000000
- -
name: validated name: validated
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000101
mask: 0b1010000000000000
- -
name: removed name: removed
out_type: uint16_t out_type: uint16_t
start: 40 start: 40
mask: 0b0000000000000010
mask: 0b0100000000000000
-
name: next_jump
out_type: uint16_t
start: 40
mask: 0b0000111111111111

Loading…
Cancel
Save