Class: Expressir::Express::Builders::StatementBuilder
- Inherits:
-
Object
- Object
- Expressir::Express::Builders::StatementBuilder
show all
- Includes:
- Helpers
- Defined in:
- lib/expressir/express/builders/statement_builder.rb
Overview
Builds statement nodes (assignment, if, case, repeat, etc.).
Instance Method Summary
collapse
Methods included from Helpers
#apply_qualifier, #extract_id_ref, #extract_interval_op, #extract_nested_text, #extract_operator, #extract_rel_op, #extract_text, #extract_unary_op, #first_value
Instance Method Details
#build_alias_stmt(ast_data) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 54
def build_alias_stmt(ast_data)
var_id = Builder.build_optional(ast_data[:variable_id])
expression = Builder.build_optional(ast_data[:general_ref])
statements = Builder.build_children(ast_data[:stmt])
if ast_data[:qualifier] && expression
Builder.ensure_array(ast_data[:qualifier]).each do |qual|
qual_result = Builder.build({ qualifier: qual })
expression = apply_qualifier(expression, qual_result)
end
end
Expressir::Model::Statements::Alias.new(
id: var_id,
expression: expression,
statements: statements.compact,
)
end
|
#build_assignment_stmt(ast_data) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 37
def build_assignment_stmt(ast_data)
ref = Builder.build_optional(ast_data[:general_ref])
expression = if ast_data[:expression]
Builder.build({ expression: ast_data[:expression] })
end
if ast_data[:qualifier]
Builder.ensure_array(ast_data[:qualifier]).each do |qual|
qual_result = Builder.build({ qualifier: qual })
ref = apply_qualifier(ref, qual_result)
end
end
Expressir::Model::Statements::Assignment.new(ref: ref,
expression: expression)
end
|
#build_case_action(ast_data) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 105
def build_case_action(ast_data)
label_data = ast_data[:list_of_case_label]
labels = if label_data.is_a?(Hash)
Builder.build_children(label_data[:case_label])
elsif label_data.is_a?(Array)
label_data.flat_map do |item|
Builder.build_children(item.is_a?(Hash) ? item[:case_label] : item)
end
else
[]
end
stmt = if ast_data[:stmt].is_a?(Hash)
Builder.build({ stmt: ast_data[:stmt] })
end
Expressir::Model::Statements::CaseAction.new(
labels: labels.compact,
statement: stmt,
)
end
|
#build_case_label(ast_data) ⇒ Object
126
127
128
129
130
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 126
def build_case_label(ast_data)
if ast_data[:expression]
Builder.build({ expression: ast_data[:expression] })
end
end
|
#build_case_stmt(ast_data) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 85
def build_case_stmt(ast_data)
selector = Builder.build_optional(ast_data[:selector])
actions = Builder.build_children(ast_data[:case_action])
otherwise_stmt = if ast_data[:t_otherwise] && ast_data[:stmt].is_a?(Hash)
Builder.build({ stmt: ast_data[:stmt] })
end
Expressir::Model::Statements::Case.new(
expression: selector,
actions: actions.compact,
otherwise_statement: otherwise_stmt,
)
end
|
#build_compound_stmt(ast_data) ⇒ Object
#build_escape_stmt(_ast_data) ⇒ Object
#build_if_stmt(ast_data) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 73
def build_if_stmt(ast_data)
expression = Builder.build_optional(ast_data[:logical_expression])
then_stmts = Builder.build_children(ast_data[:if_stmt_statements]&.dig(:stmt))
else_stmts = Builder.build_children(ast_data[:if_stmt_else_statements]&.dig(:stmt))
Expressir::Model::Statements::If.new(
expression: expression,
statements: then_stmts.compact,
else_statements: else_stmts.compact,
)
end
|
#build_increment(ast_data) ⇒ Object
179
180
181
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 179
def build_increment(ast_data)
Builder.build_optional(ast_data[:numeric_expression])
end
|
#build_increment_control(_ast_data) ⇒ Object
175
176
177
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 175
def build_increment_control(_ast_data)
nil
end
|
#build_null_stmt(_ast_data) ⇒ Object
206
207
208
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 206
def build_null_stmt(_ast_data)
Expressir::Model::Statements::Null.new
end
|
#build_procedure_call_stmt(ast_data) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 210
def build_procedure_call_stmt(ast_data)
proc_ref = if ast_data[:procedure_ref]
Builder.build({ procedure_ref: ast_data[:procedure_ref] })
elsif ast_data[:built_in_procedure]
Builder.build({ built_in_procedure: ast_data[:built_in_procedure] })
end
params = if ast_data[:actual_parameter_list]
Builder.build({ actual_parameter_list: ast_data[:actual_parameter_list] })
else
[]
end
Expressir::Model::Statements::ProcedureCall.new(
procedure: proc_ref,
parameters: Builder.ensure_array(params),
)
end
|
#build_repeat_stmt(ast_data) ⇒ Object
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
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 137
def build_repeat_stmt(ast_data)
control = ast_data[:repeat_control]
statements = Builder.build_children(ast_data[:stmt])
var_id = nil
bound1 = nil
bound2 = nil
increment = nil
increment_control = control[:increment_control] if control.is_a?(Hash)
if increment_control.is_a?(Hash)
var_id = Builder.build_optional(increment_control[:variable_id])
bound1 = Builder.build_optional(increment_control[:bound1])
bound2 = Builder.build_optional(increment_control[:bound2])
increment = Builder.build_optional(increment_control[:increment])
end
while_ctrl = control.is_a?(Hash) ? control[:while_control] : nil
until_ctrl = control.is_a?(Hash) ? control[:until_control] : nil
while_expression = if while_ctrl.is_a?(Hash)
Builder.build_optional(while_ctrl[:logical_expression])
end
until_expression = if until_ctrl.is_a?(Hash)
Builder.build_optional(until_ctrl[:logical_expression])
end
Expressir::Model::Statements::Repeat.new(
id: var_id,
bound1: bound1,
bound2: bound2,
increment: increment,
while_expression: while_expression,
until_expression: until_expression,
statements: statements.compact,
)
end
|
#build_return_stmt(ast_data) ⇒ Object
191
192
193
194
195
196
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 191
def build_return_stmt(ast_data)
expression = if ast_data[:expression]
Builder.build({ expression: ast_data[:expression] })
end
Expressir::Model::Statements::Return.new(expression: expression)
end
|
#build_selector(ast_data) ⇒ Object
99
100
101
102
103
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 99
def build_selector(ast_data)
if ast_data[:expression]
Builder.build({ expression: ast_data[:expression] })
end
end
|
#build_skip_stmt(_ast_data) ⇒ Object
202
203
204
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 202
def build_skip_stmt(_ast_data)
Expressir::Model::Statements::Skip.new
end
|
#build_stmt(ast_data) ⇒ Object
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
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 11
def build_stmt(ast_data)
if ast_data[:assignment_stmt]
Builder.build({ assignment_stmt: ast_data[:assignment_stmt] })
elsif ast_data[:alias_stmt]
Builder.build({ alias_stmt: ast_data[:alias_stmt] })
elsif ast_data[:if_stmt]
Builder.build({ if_stmt: ast_data[:if_stmt] })
elsif ast_data[:case_stmt]
Builder.build({ case_stmt: ast_data[:case_stmt] })
elsif ast_data[:compound_stmt]
Builder.build({ compound_stmt: ast_data[:compound_stmt] })
elsif ast_data[:repeat_stmt]
Builder.build({ repeat_stmt: ast_data[:repeat_stmt] })
elsif ast_data[:return_stmt]
Builder.build({ return_stmt: ast_data[:return_stmt] })
elsif ast_data[:escape_stmt]
Builder.build({ escape_stmt: ast_data[:escape_stmt] })
elsif ast_data[:skip_stmt]
Builder.build({ skip_stmt: ast_data[:skip_stmt] })
elsif ast_data[:null_stmt]
Builder.build({ null_stmt: ast_data[:null_stmt] })
elsif ast_data[:procedure_call_stmt]
Builder.build({ procedure_call_stmt: ast_data[:procedure_call_stmt] })
end
end
|
#build_type_label(ast_data) ⇒ Object
228
229
230
231
232
233
234
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 228
def build_type_label(ast_data)
if ast_data[:type_label_id]
Builder.build({ type_label_id: ast_data[:type_label_id] })
elsif ast_data[:type_label_ref]
Builder.build({ type_label_ref: ast_data[:type_label_ref] })
end
end
|
#build_type_label_ref(ast_data) ⇒ Object
236
237
238
239
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 236
def build_type_label_ref(ast_data)
id = (ast_data[:str] || ast_data[:type_label_id]&.dig(:str))
Expressir::Model::References::SimpleReference.new(id: id)
end
|
#build_until_control(ast_data) ⇒ Object
187
188
189
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 187
def build_until_control(ast_data)
Builder.build_optional(ast_data[:logical_expression])
end
|
#build_while_control(ast_data) ⇒ Object
183
184
185
|
# File 'lib/expressir/express/builders/statement_builder.rb', line 183
def build_while_control(ast_data)
Builder.build_optional(ast_data[:logical_expression])
end
|