Skip to content

Commit

Permalink
Merge pull request #1686 from eileencodes/redo-flags
Browse files Browse the repository at this point in the history
Move common flags to top bits
  • Loading branch information
kddnewton authored Oct 13, 2023
2 parents 117988e + 8955086 commit f76b487
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ flags:
comment: "x - ignores whitespace and allows comments in regular expressions"
- name: MULTI_LINE
comment: "m - allows $ to match the end of lines within strings"
- name: ONCE
comment: "o - only interpolates values into the regular expression once"
- name: EUC_JP
comment: "e - forces the EUC-JP encoding"
- name: ASCII_8BIT
Expand All @@ -369,8 +371,6 @@ flags:
comment: "s - forces the Windows-31J encoding"
- name: UTF_8
comment: "u - forces the UTF-8 encoding"
- name: ONCE
comment: "o - only interpolates values into the regular expression once"
- name: StringFlags
values:
- name: FROZEN
Expand Down
2 changes: 1 addition & 1 deletion templates/ext/prism/api_node.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pm_ast_new(pm_parser_t *parser, pm_node_t *node, rb_encoding *encoding) {
argv[<%= index %>] = ULONG2NUM(cast-><%= field.name %>);
<%- when Prism::FlagsField -%>
#line <%= __LINE__ + 1 %> "<%= File.basename(__FILE__) %>"
argv[<%= index %>] = ULONG2NUM(node->flags >> <%= Prism::COMMON_FLAGS %>);
argv[<%= index %>] = ULONG2NUM(node->flags & ~PM_NODE_FLAG_COMMON_MASK);
<%- else -%>
<%- raise -%>
<%- end -%>
Expand Down
8 changes: 5 additions & 3 deletions templates/include/prism/ast.h.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ typedef uint16_t pm_node_flags_t;

// We store the flags enum in every node in the tree. Some flags are common to
// all nodes (the ones listed below). Others are specific to certain node types.
static const pm_node_flags_t PM_NODE_FLAG_NEWLINE = 0x1;
static const pm_node_flags_t PM_NODE_FLAG_STATIC_LITERAL = 0x2;
#define PM_NODE_FLAG_BITS (sizeof(pm_node_flags_t) * 8)
static const pm_node_flags_t PM_NODE_FLAG_NEWLINE = (1 << (PM_NODE_FLAG_BITS - 1));
static const pm_node_flags_t PM_NODE_FLAG_STATIC_LITERAL = (1 << (PM_NODE_FLAG_BITS - 2));
static const pm_node_flags_t PM_NODE_FLAG_COMMON_MASK = PM_NODE_FLAG_NEWLINE | PM_NODE_FLAG_STATIC_LITERAL;

// For easy access, we define some macros to check node type
#define PM_NODE_TYPE(node) ((enum pm_node_type)node->type)
Expand Down Expand Up @@ -105,7 +107,7 @@ typedef struct pm_<%= node.human %> {

// <%= flag.name %>
typedef enum pm_<%= flag.human %> {
<%- flag.values.each.with_index(Prism::COMMON_FLAGS) do |value, index| -%>
<%- flag.values.each_with_index do |value, index| -%>
PM_<%= flag.human.upcase %>_<%= value.name %> = 1 << <%= index %>,
<%- end -%>
} pm_<%= flag.human %>_t;
Expand Down
2 changes: 1 addition & 1 deletion templates/src/prettyprint.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ prettyprint_node(pm_buffer_t *buffer, pm_parser_t *parser, pm_node_t *node) {
pm_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- when Prism::FlagsField -%>
char <%= field.name %>_buffer[12];
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", node->flags >> <%= Prism::COMMON_FLAGS %>);
snprintf(<%= field.name %>_buffer, sizeof(<%= field.name %>_buffer), "+%d", (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
pm_buffer_append_str(buffer, <%= field.name %>_buffer, strlen(<%= field.name %>_buffer));
<%- else -%>
<%- raise -%>
Expand Down
2 changes: 1 addition & 1 deletion templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
<%- when Prism::UInt32Field -%>
pm_buffer_append_u32(buffer, ((pm_<%= node.human %>_t *)node)-><%= field.name %>);
<%- when Prism::FlagsField -%>
pm_buffer_append_u32(buffer, node->flags >> <%= Prism::COMMON_FLAGS %>);
pm_buffer_append_u32(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
<%- else -%>
<%- raise -%>
<%- end -%>
Expand Down
2 changes: 0 additions & 2 deletions templates/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
require "yaml"

module Prism
COMMON_FLAGS = 2

SERIALIZE_ONLY_SEMANTICS_FIELDS = ENV.fetch("PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS", false)

JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "truffleruby"
Expand Down

0 comments on commit f76b487

Please sign in to comment.