kaiser-ruby 0.5.1 → 0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module KaiserRuby
2
- VERSION = "0.5.1"
2
+ VERSION = "0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaiser-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Ruszkiewicz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-29 00:00:00.000000000 Z
11
+ date: 2018-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,33 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.11'
69
69
  - !ruby/object:Gem::Dependency
70
- name: parslet
70
+ name: thor
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.8'
75
+ version: '0.20'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.8'
82
+ version: '0.20'
83
83
  - !ruby/object:Gem::Dependency
84
- name: thor
84
+ name: hashie
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: '0.20'
89
+ version: '0'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
- version: '0.20'
96
+ version: '0'
97
97
  description:
98
98
  email:
99
99
  - marcin.ruszkiewicz@polcode.net
@@ -119,8 +119,9 @@ files:
119
119
  - kaiser-ruby.gemspec
120
120
  - lib/kaiser_ruby.rb
121
121
  - lib/kaiser_ruby/cli.rb
122
- - lib/kaiser_ruby/rockstar_parser.rb
123
- - lib/kaiser_ruby/rockstar_transform.rb
122
+ - lib/kaiser_ruby/parser.rb
123
+ - lib/kaiser_ruby/refinements.rb
124
+ - lib/kaiser_ruby/transformer.rb
124
125
  - lib/kaiser_ruby/version.rb
125
126
  homepage: https://github.com/marcinruszkiewicz/kaiser-ruby
126
127
  licenses:
@@ -1,310 +0,0 @@
1
- module KaiserRuby
2
- class RockstarParser < Parslet::Parser
3
- # ORDER OF THESE RULES ABSOLUTELY MATTERS
4
- # DON'T MESS WITH IT
5
-
6
- # all the keywords separately, so that variable names can't use them
7
-
8
- rule(:reserved) do
9
- mysterious_value_keywords | null_value_keywords | true_value_keywords | false_value_keywords |
10
- plus_keywords | minus_keywords | times_keywords | over_keywords | increment_keywords |
11
- assignment_keywords | poetic_string_keywords | poetic_number_keywords |
12
- say_keywords | flow_keywords | comparison_keywords | function_keywords
13
- end
14
-
15
- rule(:mysterious_value_keywords) { str('mysterious') }
16
- rule(:null_value_keywords) { str('nothing') | str('nowhere') | str('nobody') | str('empty') | str('gone') }
17
- rule(:false_value_keywords) { str('false') | str('wrong') | str('no') | str('lies') }
18
- rule(:true_value_keywords) { str('true') | str('right') | str('yes') | str('ok') }
19
- rule(:plus_keywords) { str('plus') | str('with') }
20
- rule(:minus_keywords) { str('minus') | str('without') }
21
- rule(:minus_keywords) { str('minus') | str('without') }
22
- rule(:times_keywords) { str('times') | str('of') }
23
- rule(:over_keywords) { str('over') }
24
- rule(:poetic_number_keywords) { str('is') | str('was') | str('were') }
25
- rule(:say_keywords) { str('Say') | str('Shout') | str('Scream') | str('Whisper') }
26
- rule(:flow_keywords) { str('If') | str('Else') | str('While') | str('Until') }
27
- rule(:increment_keywords) { str('Knock') | str('Build') }
28
- rule(:assignment_keywords) { str('Put') | str('into') }
29
- rule(:poetic_string_keywords) { str('says') }
30
- rule(:comparison_keywords) { str("is") | not_keywords | gt_keywords | gte_keywords | lt_keywords | lte_keywords }
31
- rule(:function_keywords) { str('Break it down') | str('Take it to the top') | str('Give back') | str('takes') | str('taking') | str('Listen to') }
32
-
33
- rule(:pronouns) { (str('he') | str('she') | str('it') | str('they') | str('them') | str('her') | str('him') | str('its')).as(:pronoun) }
34
-
35
- # variable names
36
- # using [[:upper:]] etc here allows for metal umlauts and other UTF characters
37
-
38
- rule(:proper_word) { reserved.absent? >> match['[[:upper:]]'] >> match['[[:alpha:]]'].repeat }
39
- rule(:common_variable_name) do
40
- (
41
- str('A ') | str('a ') |
42
- str('An ') | str('an ') |
43
- str('The ') | str('the ') |
44
- str('My ') | str('my ') |
45
- str('Your ') | str('your ')
46
- ) >> reserved.absent? >> match['[[:lower:]]'].repeat >> match[','].maybe.ignore
47
- end
48
- rule(:proper_variable_name) do
49
- (proper_word >> (space >> proper_word).repeat) >> match[','].maybe.ignore
50
- end
51
-
52
- rule(:variable_names) do
53
- (pronouns | common_variable_name | proper_variable_name).as(:variable_name)
54
- end
55
-
56
- # all the different value types (except Object for now)
57
-
58
- rule(:mysterious_value) { mysterious_value_keywords.as(:mysterious_value) }
59
- rule(:null_value) { null_value_keywords.as(:null_value) }
60
- rule(:true_value) { true_value_keywords.as(:true_value) }
61
- rule(:false_value) { false_value_keywords.as(:false_value) }
62
- rule(:string_value) { (str('"') >> match['^"'].repeat >> str('"')).as(:string_value) }
63
- rule(:numeric_value) { match['0-9\.'].repeat.as(:numeric_value) }
64
- rule(:unquoted_string) { match['^\n'].repeat.as(:unquoted_string) }
65
- rule(:string_as_number) { reserved.absent? >> match['^\n'].repeat.as(:string_as_number) }
66
-
67
- # assignment
68
-
69
- rule(:basic_assignment_expression) do
70
- match('Put ').present? >>
71
- (
72
- str('Put ') >> string_input.as(:right) >> str(' into ') >> variable_names.as(:left)
73
- ).as(:assignment)
74
- end
75
-
76
- # math operations
77
-
78
- rule(:increment) do
79
- (
80
- str('Build ') >> variable_names.as(:variable_name) >> str(' up')
81
- ).as(:increment)
82
- end
83
-
84
- rule(:decrement) do
85
- (
86
- str('Knock ') >> variable_names.as(:variable_name) >> str(' down')
87
- ).as(:decrement)
88
- end
89
-
90
- rule(:addition) do
91
- (match('.*? plus') | match('.*? with')).present? >>
92
- (
93
- value_or_variable.as(:left) >> space >> plus_keywords >> space >> value_or_variable.as(:right)
94
- ).as(:addition)
95
- end
96
-
97
- rule(:subtraction) do
98
- (match('.*? minus') | match('.*? without')).present? >>
99
- (
100
- value_or_variable.as(:left) >> space >> minus_keywords >> space >> value_or_variable.as(:right)
101
- ).as(:subtraction)
102
- end
103
-
104
- rule(:division) do
105
- match('.*? over').present? >>
106
- (
107
- value_or_variable.as(:left) >> space >> over_keywords >> space >> value_or_variable.as(:right)
108
- ).as(:division)
109
- end
110
-
111
- rule(:multiplication) do
112
- (match('.*? times') | match('.*? of')).present? >>
113
- (
114
- value_or_variable.as(:left) >> space >> times_keywords >> space >> value_or_variable.as(:right)
115
- ).as(:multiplication)
116
- end
117
-
118
- # functions
119
-
120
- rule(:function) do
121
- match('.*? takes').present? >>
122
- (
123
- variable_names.as(:function_name) >> str(' takes') >>
124
- (space >> str('and ').maybe >> variable_names.as(:argument_name)).repeat.as(:arguments) >> eol >>
125
- scope {
126
- inner_block_line.repeat.as(:function_block) >>
127
- (eol | eof).as(:enddef)
128
- }
129
- ).as(:function_definition)
130
- end
131
-
132
- rule(:function_call) do
133
- (
134
- variable_names.as(:function_name) >> str(' taking') >>
135
- (space >> str('and ').maybe >> variable_names.as(:argument_name)).repeat.as(:arguments)
136
- ).as(:function_call)
137
- end
138
-
139
- # poetic assignments
140
-
141
- rule(:poetic_type_literal) do
142
- (flow_keywords.absent?) >>
143
- (
144
- variable_names.as(:left) >> str(' is ') >> (mysterious_value | null_value | false_value | true_value).as(:right)
145
- ).as(:assignment)
146
- end
147
-
148
- rule(:poetic_string_literal) do
149
- (
150
- variable_names.as(:left) >> str(' says ') >> unquoted_string.as(:right)
151
- ).as(:assignment)
152
- end
153
-
154
- rule(:poetic_number_literal) do
155
- (flow_keywords.absent?) >>
156
- (
157
- variable_names.as(:left) >> space >> poetic_number_keywords >> space >> string_as_number.as(:right)
158
- ).as(:assignment)
159
- end
160
-
161
- # single statements
162
-
163
- rule(:print_function) do
164
- (
165
- say_keywords >> space >> (function_call | value_or_variable).as(:output)
166
- ).as(:print)
167
- end
168
-
169
- rule(:break_function) do
170
- str('Break it down').as(:break)
171
- end
172
-
173
- rule(:continue_function) do
174
- str('Take it to the top').as(:continue)
175
- end
176
-
177
- rule(:return_function) do
178
- str('Give back ') >> (math_operations | value_or_variable).as(:return_value)
179
- end
180
-
181
- rule(:input) do
182
- str('Listen to ') >> variable_names.as(:input_variable)
183
- end
184
-
185
- # comparisons
186
-
187
- rule(:gte_keywords) { str(' is as ') >> (str('high') | str('great') | str('big') | str('strong')) >> str(' as ') }
188
- rule(:gte) do
189
- (
190
- value_or_variable.as(:left) >> gte_keywords >> (math_operations | value_or_variable).as(:right)
191
- ).as(:gte)
192
- end
193
-
194
- rule(:lte_keywords) { str(' is as ') >> (str('low') | str('little') | str('small') | str('weak')) >> str(' as ') }
195
- rule(:lte) do
196
- (
197
- value_or_variable.as(:left) >> lte_keywords >> (math_operations | value_or_variable).as(:right)
198
- ).as(:lte)
199
- end
200
-
201
- rule(:equality) do
202
- (
203
- value_or_variable.as(:left) >> str(' is ') >> (math_operations | value_or_variable).as(:right)
204
- ).as(:equals)
205
- end
206
-
207
- rule(:not_keywords) { str(' is not ') | str(" ain't ")}
208
- rule(:inequality) do
209
- (
210
- value_or_variable.as(:left) >> not_keywords >> (math_operations | value_or_variable).as(:right)
211
- ).as(:not_equals)
212
- end
213
-
214
- rule(:gt_keywords) { str(' is ') >> (str('higher') | str('greater') | str('bigger') | str('stronger')) >> str(' than ') }
215
- rule(:gt) do
216
- (
217
- value_or_variable.as(:left) >> gt_keywords >> (math_operations | value_or_variable).as(:right)
218
- ).as(:gt)
219
- end
220
-
221
- rule(:lt_keywords) { str(' is ') >> (str('lower') | str('less') | str('smaller') | str('weaker')) >> str(' than ') }
222
- rule(:lt) do
223
- (
224
- value_or_variable.as(:left) >> lt_keywords >> (math_operations | value_or_variable).as(:right)
225
- ).as(:lt)
226
- end
227
-
228
-
229
-
230
- # flow control - if, else, while, until
231
-
232
- rule(:if_else_block) do
233
- (
234
- str('If ') >> comparisons.as(:if_condition) >>
235
- (space >> (str('and') | str('or')).as(:and_or) >> space >> comparisons.as(:second_condition)).maybe >> eol >>
236
- scope {
237
- inner_block_line.repeat.as(:if_block)
238
- } >>
239
- str('Else') >> eol >>
240
- scope {
241
- inner_block_line.repeat.as(:else_block)
242
- } >> (eol | eof).as(:endif)
243
- ).as(:if_else)
244
- end
245
-
246
- rule(:if_block) do
247
- (
248
- str('If ') >> comparisons.as(:if_condition) >>
249
- (space >> (str('and') | str('or')).as(:and_or) >> space >> comparisons.as(:second_condition)).maybe >> eol >>
250
- scope {
251
- inner_block_line.repeat.as(:if_block)
252
- } >> (eol | eof).as(:endif)
253
- ).as(:if)
254
- end
255
-
256
- rule(:while_block) do
257
- (
258
- str('While ') >> comparisons.as(:while_condition) >>
259
- (space >> (str('and') | str('or')).as(:and_or) >> space >> comparisons.as(:second_condition)).maybe >> eol >>
260
- scope {
261
- inner_block_line.repeat.as(:while_block)
262
- } >> (eol | eof).as(:endwhile)
263
- ).as(:while)
264
- end
265
-
266
- rule(:until_block) do
267
- (
268
- str('Until ') >> comparisons.as(:until_condition) >>
269
- (space >> (str('and') | str('or')).as(:and_or) >> space >> comparisons.as(:second_condition)).maybe >> eol >>
270
- scope {
271
- inner_block_line.repeat.as(:until_block)
272
-
273
- } >> (eol | eof).as(:enduntil)
274
- ).as(:until)
275
- end
276
-
277
- # sets of rules
278
-
279
- rule(:simple_values) { mysterious_value | null_value | false_value | true_value | string_value | numeric_value }
280
- rule(:value_or_variable) { variable_names | simple_values }
281
- rule(:math_operations) { increment | decrement | addition | subtraction | multiplication | division }
282
- rule(:expressions) { basic_assignment_expression | math_operations }
283
- rule(:comparisons) { gte | gt | lte | lt | inequality | equality }
284
- rule(:flow_control) { if_block | if_else_block | while_block | until_block }
285
- rule(:poetics) { poetic_type_literal | poetic_string_literal | poetic_number_literal }
286
- rule(:functions) { function_call | function | print_function | break_function | continue_function | return_function | input }
287
- rule(:line_elements) { flow_control | poetics | expressions | functions | eol }
288
-
289
- # handle multiple lines in a file
290
-
291
- rule(:string_input) { line_elements | value_or_variable }
292
- rule(:line) { (line_elements >> eol.maybe).as(:line) }
293
- rule(:inner_block_line) { ( (flow_control | poetics | expressions | functions) >> eol.maybe).as(:line) }
294
- rule(:lyrics) { line.repeat.as(:lyrics) }
295
- root(:lyrics)
296
-
297
- # DRY helpers
298
-
299
- rule(:eol) { match["\n"] }
300
- rule(:eof) { any.absent? }
301
- rule(:space) { match[' \t'].repeat(1) }
302
- end
303
-
304
- # this is an alternative parser that enables all the RSpec tests to pass
305
- # it specifically has a single line rule that matches a string instead of lines
306
- # it also matches the value_or_variable rule, so that the value creation can be tested
307
- class RockstarSingleLineParser < KaiserRuby::RockstarParser
308
- root(:string_input)
309
- end
310
- end
@@ -1,224 +0,0 @@
1
- module KaiserRuby
2
- class RockstarTransform < Parslet::Transform
3
- @@last_variable = nil
4
- @@indent = 0
5
-
6
- class << self
7
- def last_variable=(value)
8
- @@last_variable = value
9
- end
10
-
11
- def up_indent
12
- @@indent += 2
13
- end
14
-
15
- def down_indent
16
- @@indent -= 2
17
- end
18
- end
19
-
20
- rule(variable_name: simple(:str)) do |context|
21
- self.last_variable = parameterize(context[:str])
22
- parameterize(context[:str])
23
- end
24
- rule(pronoun: simple(:_)) { @@last_variable }
25
-
26
- rule(mysterious_value: simple(:_)) { 'nil' }
27
- rule(null_value: simple(:_)) { '0' }
28
- rule(true_value: simple(:_)) { 'true' }
29
- rule(false_value: simple(:_)) { 'false' }
30
- rule(string_value: simple(:str)) { str }
31
- rule(numeric_value: simple(:num)) { num }
32
- rule(unquoted_string: simple(:str)) { "\"#{str}\"" }
33
- rule(string_as_number: simple(:str)) do |context|
34
- if context[:str].to_s.include?('.')
35
- context[:str].to_s.gsub(/[^A-Za-z\s\.]/, '').split('.').map do |sub|
36
- str_to_num(sub.strip)
37
- end.join('.').to_f
38
- else
39
- str_to_num(context[:str]).to_i
40
- end
41
- end
42
-
43
- rule(assignment: { left: simple(:left), right: simple(:right) }) { "#{left} = #{right}" }
44
- rule(increment: simple(:str)) { "#{str} += 1" }
45
- rule(decrement: simple(:str)) { "#{str} -= 1" }
46
- rule(addition: { left: simple(:left), right: simple(:right) }) { "#{left} + #{right}" }
47
- rule(subtraction: { left: simple(:left), right: simple(:right) }) { "#{left} - #{right}" }
48
- rule(multiplication: { left: simple(:left), right: simple(:right) }) { "#{left} * #{right}" }
49
- rule(division: { left: simple(:left), right: simple(:right) }) { "#{left} / #{right}" }
50
-
51
- rule(print: { output: simple(:output) }) { "puts #{output}" }
52
- rule(continue: simple(:_)) { "next" }
53
- rule(break: simple(:_)) { "break" }
54
- rule(input_variable: simple(:var)) { "print '> '\n__input = STDIN.gets.chomp\n#{var} = Integer(__input) rescue input" }
55
-
56
- rule(equals: { left: simple(:left), right: simple(:right) }) { "#{left} == #{right}" }
57
- rule(not_equals: { left: simple(:left), right: simple(:right) }) { "#{left} != #{right}" }
58
- rule(gt: { left: simple(:left), right: simple(:right) }) { "#{left} > #{right}" }
59
- rule(gte: { left: simple(:left), right: simple(:right) }) { "#{left} >= #{right}" }
60
- rule(lte: { left: simple(:left), right: simple(:right) }) { "#{left} <= #{right}" }
61
- rule(lt: { left: simple(:left), right: simple(:right) }) { "#{left} < #{right}" }
62
-
63
- rule(if: {
64
- if_condition: simple(:if_condition),
65
- if_block: sequence(:if_block_lines),
66
- endif: simple(:_)
67
- } ) do |context|
68
- output = "#{' ' * @@indent}if #{context[:if_condition]}\n"
69
- self.up_indent
70
- output += context[:if_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
71
- self.down_indent
72
- output += "#{' ' * @@indent}end # endif"
73
- output
74
- end
75
-
76
- rule(if: {
77
- if_condition: simple(:if_condition),
78
- and_or: simple(:and_or),
79
- second_condition: simple(:second_condition),
80
- if_block: sequence(:if_block_lines),
81
- endif: simple(:_)
82
- } ) do |context|
83
- proper_and_or = context[:and_or] == 'and' ? '&&' : '||'
84
- output = "#{' ' * @@indent}if #{context[:if_condition]} #{proper_and_or} #{context[:second_condition]}\n"
85
- self.up_indent
86
- output += context[:if_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
87
- self.down_indent
88
- output += "#{' ' * @@indent}end # endif"
89
- output
90
- end
91
-
92
- rule(if_else: {
93
- if_condition: simple(:if_condition),
94
- if_block: sequence(:if_block_lines),
95
- else_block: sequence(:else_block_lines),
96
- endif: simple(:_)
97
- } ) do |context|
98
- output = "#{' ' * @@indent}if #{context[:if_condition]}\n"
99
- self.up_indent
100
- output += context[:if_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
101
- self.down_indent
102
- output += "#{' ' * @@indent}else\n"
103
- self.up_indent
104
- output += context[:else_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
105
- self.down_indent
106
- output += "#{' ' * @@indent}end # endifelse"
107
- output
108
- end
109
-
110
- rule(if_else: {
111
- if_condition: simple(:if_condition),
112
- and_or: simple(:and_or),
113
- second_condition: simple(:second_condition),
114
- if_block: sequence(:if_block_lines),
115
- else_block: sequence(:else_block_lines),
116
- endif: simple(:_)
117
- } ) do |context|
118
- proper_and_or = context[:and_or] == 'and' ? '&&' : '||'
119
- output = "#{' ' * @@indent}if #{context[:if_condition]} #{proper_and_or} #{context[:second_condition]}\n"
120
- self.up_indent
121
- output += context[:if_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
122
- self.down_indent
123
- output += "#{' ' * @@indent}else\n"
124
- self.up_indent
125
- output += context[:else_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
126
- self.down_indent
127
- output += "#{' ' * @@indent}end # endifelse"
128
- output
129
- end
130
-
131
- rule(while: {
132
- while_condition: simple(:while_condition),
133
- while_block: sequence(:while_block_lines),
134
- endwhile: simple(:_)
135
- } ) do |context|
136
- output = "#{' ' * @@indent}while #{context[:while_condition]}\n"
137
- self.up_indent
138
- output += context[:while_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
139
- self.down_indent
140
- output += "#{' ' * @@indent}end # endwhile"
141
- output
142
- end
143
-
144
- rule(while: {
145
- while_condition: simple(:while_condition),
146
- and_or: simple(:and_or),
147
- second_condition: simple(:second_condition),
148
- while_block: sequence(:while_block_lines),
149
- endwhile: simple(:_)
150
- } ) do |context|
151
- proper_and_or = context[:and_or] == 'and' ? '&&' : '||'
152
- output = "#{' ' * @@indent}while #{context[:while_condition]} #{proper_and_or} #{context[:second_condition]}\n"
153
- self.up_indent
154
- output += context[:while_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
155
- self.down_indent
156
- output += "#{' ' * @@indent}end # endwhile"
157
- output
158
- end
159
-
160
- rule(until: {
161
- until_condition: simple(:until_condition),
162
- until_block: sequence(:until_block_lines),
163
- enduntil: simple(:_)
164
- } ) do |context|
165
- output = "#{' ' * @@indent}until #{context[:until_condition]}\n"
166
- self.up_indent
167
- output += context[:until_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
168
- self.down_indent
169
- output += "#{' ' * @@indent}end # enduntil"
170
- output
171
- end
172
-
173
- rule(until: {
174
- until_condition: simple(:until_condition),
175
- and_or: simple(:and_or),
176
- second_condition: simple(:second_condition),
177
- until_block: sequence(:until_block_lines),
178
- enduntil: simple(:_)
179
- } ) do |context|
180
- proper_and_or = context[:and_or] == 'and' ? '&&' : '||'
181
- output = "#{' ' * @@indent}until #{context[:until_condition]} #{proper_and_or} #{context[:second_condition]}\n"
182
- self.up_indent
183
- output += context[:until_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
184
- self.down_indent
185
- output += "#{' ' * @@indent}end # enduntil"
186
- output
187
- end
188
-
189
- rule(argument_name: simple(:str)) { str }
190
- rule(return_value: simple(:value)) { "return #{value}" }
191
-
192
- rule(function_definition: {
193
- function_name: simple(:function_name),
194
- arguments: sequence(:arguments),
195
- function_block: sequence(:function_block_lines),
196
- enddef: simple(:_)
197
- } ) do |context|
198
- output = "#{' ' * @@indent}def #{context[:function_name]}(#{context[:arguments].join(', ')})\n"
199
- self.up_indent
200
- output += context[:function_block_lines].map { |l| "#{' ' * @@indent}#{l}\n" }.join
201
- self.down_indent
202
- output += "#{' ' * @@indent}end # enddef"
203
- output
204
- end
205
-
206
- rule(function_call: {
207
- function_name: simple(:function_name),
208
- arguments: sequence(:arguments)
209
- } ) do
210
- "#{function_name}(#{arguments.join(', ')})"
211
- end
212
-
213
- rule(line: simple(:line)) { line == "\n" ? nil : line }
214
- rule(lyrics: sequence(:lines)) { lines.join("\n") + "\n" }
215
-
216
- def self.parameterize(string)
217
- string.to_s.downcase.gsub(/\s+/, '_')
218
- end
219
-
220
- def self.str_to_num(string)
221
- string.to_s.split(/\s+/).map { |e| e.length % 10 }.join
222
- end
223
- end
224
- end