Method: Packcr::Node::QuantityNode#get_many_code

Defined in:
lib/packcr/generated/node/quantity_node.rb

#get_many_code(gen, onfail, indent, unwrap, oncut) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/packcr/generated/node/quantity_node.rb', line 4

def get_many_code(gen, onfail, indent, unwrap, oncut)
  case gen.lang
  when :c
    erbout = +""
    erbout << "{\n".freeze

    r = expr.reachability
    if min > 0
      erbout << "    const size_t p0 = ctx->position_offset;\n".freeze

      if gen.location
        erbout << "    const packcr_location_t p0_loc = ctx->position_offset_loc;\n".freeze
      end
      erbout << "    const size_t n0 = chunk->thunks.len;\n".freeze
    end
    erbout << "    int i;\n".freeze

    if max < 0
      erbout << "    for (i = 0;; i++) {\n".freeze

    else
      erbout << "    for (i = 0; i < #{max}; i++) {\n".freeze
    end
    erbout << "        const size_t p = ctx->position_offset;\n".freeze

    if r != Packcr::CODE_REACH__ALWAYS_SUCCEED
      if gen.location
        erbout << "        const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze
      end
      erbout << "        const size_t n = chunk->thunks.len;\n".freeze
    end
    l = gen.next_label
    erbout << "#{gen.generate_code(expr, l, 8, false)}        if (ctx->position_offset == p) break;\n".freeze

    if r != Packcr::CODE_REACH__ALWAYS_SUCCEED
      erbout << "        continue;\n    L#{format("%04d", l)}:;\n        ctx->position_offset = p;\n".freeze

      if gen.location
        erbout << "        ctx->position_offset_loc = p_loc;\n".freeze
      end
      erbout << "        packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n        break;\n".freeze
    end
    erbout << "    }\n".freeze

    if min > 0
      erbout << "    if (i < #{min}) {\n        ctx->position_offset = p0;\n".freeze

      if gen.location
        erbout << "        ctx->position_offset_loc = p0_loc;\n".freeze
      end
      erbout << "        packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);\n        goto L#{format("%04d", onfail)};\n    }\n".freeze
    end
    erbout << "}\n".freeze

    erbout
  when :rb
    erbout = +""
    if min > 0
      erbout << "q#{gen.level} = @position_offset\n".freeze

      if gen.location
        erbout << "q_loc#{gen.level} = @position_offset_loc\n".freeze
      end
      erbout << "m#{gen.level} = answer.thunks.length\n".freeze
    end
    erbout << "i#{gen.level} = 0\npos#{gen.level} = nil\n".freeze

    if gen.location
      erbout << "p_loc#{gen.level} = nil\n".freeze
    end
    erbout << "n#{gen.level} = nil\n".freeze

    l = gen.next_label
    erbout << "catch(#{l}) do\n  pos#{gen.level} = @position_offset\n".freeze

    if gen.location
      erbout << "  p_loc#{gen.level} = @position_offset_loc\n".freeze
    end
    erbout << "  n#{gen.level} = answer.thunks.length\n".freeze

    r = expr.reachability

    erbout << "#{gen.generate_code(expr, l, 2, false)}  i#{gen.level} += 1\n  if @position_offset != pos#{gen.level}".freeze
    if max >= 0
      erbout << " || i#{gen.level} < #{max}".freeze
    end
    erbout << "\n    redo\n  end\n  pos#{gen.level} = nil\nend\n".freeze

    if r != Packcr::CODE_REACH__ALWAYS_SUCCEED
      erbout << "if pos#{gen.level}\n  @position_offset = pos#{gen.level}\n".freeze

      if gen.location
        erbout << "  @position_offset_loc = p_loc#{gen.level}\n".freeze
      end
      erbout << "  answer.thunks[n#{gen.level}..-1] = []\nend\n".freeze
    end
    if min > 0
      erbout << "if i#{gen.level} < #{min}\n  @position_offset = q#{gen.level}\n".freeze

      if gen.location
        erbout << "  @position_offset_loc = q_loc#{gen.level}\n".freeze
      end
      erbout << "  answer.thunks[m#{gen.level}..-1] = []\n  throw(#{onfail})\nend\n".freeze
    end
    erbout
  when :rs
    erbout = +""
    if min > 0
      erbout << "let p0 = self.input.position_offset;\n".freeze

      if gen.location
        erbout << "TODO\n".freeze
      end
    end
    use_count = max >= 0 || min > 0
    if use_count
      erbout << "let mut i = -1;\n".freeze
    end
    m = gen.next_label
    erbout << "catch(#{m}, || {\n    loop {\n".freeze

    if use_count
      erbout << "        i += 1;\n".freeze
    end
    if max >= 0
      erbout << "        if i >= #{max} {\n            return NOP;\n        }\n".freeze
    end
    erbout << "        let p = self.input.position_offset;\n".freeze

    if (r != Packcr::CODE_REACH__ALWAYS_SUCCEED) && gen.location
      erbout << "        TODO\n".freeze
    end
    l = gen.next_label
    r = expr.reachability
    if r == Packcr::CODE_REACH__ALWAYS_SUCCEED
      erbout << "        TODO\n        match (|| {\n#{gen.generate_code(expr, l, 12, false)}            if self.input.position_offset == p {\n                return throw(#{m});\n            }\n        })() {\n            NOP => continue,\n            Err(label) if label != #{l} => return throw(label),\n            _ => {\n                self.input.position_offset = p;\n".freeze

    else
      erbout << "        match (|| {\n#{gen.generate_code(expr, l, 12, false)}            if self.input.position_offset == p {\n                return throw(#{m});\n            }\n            NOP\n        })() {\n            NOP => continue,\n            Err(label) if label != #{l} => return throw(label),\n            _ => {\n                self.input.position_offset = p;\n".freeze

    end
    if gen.location
      erbout << "                TODO\n".freeze
    end
    erbout << "                return throw(#{m});\n            }\n        }\n    }\n})?;\n".freeze

    if min > 0
      erbout << "if i < #{min} {\n    self.input.position_offset = p0;\n".freeze

      if gen.location
        erbout << "    TODO\n".freeze
      end
      erbout << "    return throw(#{onfail});\n}\n".freeze
    end
    erbout
  else
    raise "unknown lang #{gen.lang}"
  end
end