template-ruby 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +9 -0
  3. data/.gitignore +1 -0
  4. data/.prettierrc +3 -0
  5. data/CHANGELOG.md +15 -0
  6. data/Gemfile.lock +1 -1
  7. data/README.md +10 -0
  8. data/bin/template +39 -0
  9. data/docs/euler/1.template +14 -0
  10. data/docs/euler/2.template +16 -0
  11. data/docs/euler/3.template +16 -0
  12. data/docs/euler/4.template +11 -0
  13. data/docs/euler/5.template +14 -0
  14. data/docs/precedence.template +94 -0
  15. data/lib/code/error.rb +15 -0
  16. data/lib/code/node/base_10_decimal.rb +12 -7
  17. data/lib/code/node/base_10_integer.rb +13 -4
  18. data/lib/code/node/base_10_number.rb +3 -3
  19. data/lib/code/node/base_16_number.rb +2 -2
  20. data/lib/code/node/base_2_number.rb +2 -2
  21. data/lib/code/node/base_8_number.rb +2 -2
  22. data/lib/code/node/block.rb +17 -0
  23. data/lib/code/node/boolean.rb +13 -4
  24. data/lib/code/node/call.rb +40 -8
  25. data/lib/code/node/call_argument.rb +37 -0
  26. data/lib/code/node/chained_call.rb +38 -0
  27. data/lib/code/node/code.rb +4 -7
  28. data/lib/code/node/defined.rb +19 -0
  29. data/lib/code/node/dictionnary.rb +11 -7
  30. data/lib/code/node/dictionnary_key_value.rb +3 -3
  31. data/lib/code/node/equal.rb +36 -0
  32. data/lib/code/node/function.rb +17 -0
  33. data/lib/code/node/function_argument.rb +45 -0
  34. data/lib/code/node/group.rb +13 -0
  35. data/lib/code/node/if.rb +55 -0
  36. data/lib/code/node/if_modifier.rb +48 -0
  37. data/lib/code/node/keyword_call_argument.rb +30 -0
  38. data/lib/code/node/keyword_function_argument.rb +33 -0
  39. data/lib/code/node/list.rb +10 -4
  40. data/lib/code/node/name.rb +37 -4
  41. data/lib/code/node/negation.rb +33 -0
  42. data/lib/code/node/not_keyword.rb +13 -0
  43. data/lib/code/node/nothing.rb +2 -2
  44. data/lib/code/node/number.rb +3 -3
  45. data/lib/code/node/operation.rb +33 -0
  46. data/lib/code/node/or_keyword.rb +34 -0
  47. data/lib/code/node/power.rb +16 -0
  48. data/lib/code/node/range.rb +31 -0
  49. data/lib/code/node/regular_call_argument.rb +34 -0
  50. data/lib/code/node/regular_function_argument.rb +36 -0
  51. data/lib/code/node/rescue.rb +16 -0
  52. data/lib/code/node/statement.rb +53 -3
  53. data/lib/code/node/string.rb +2 -2
  54. data/lib/code/node/ternary.rb +26 -0
  55. data/lib/code/node/unary_minus.rb +22 -0
  56. data/lib/code/node/while.rb +42 -0
  57. data/lib/code/node.rb +10 -0
  58. data/lib/code/object/argument.rb +41 -0
  59. data/lib/code/object/boolean.rb +8 -9
  60. data/lib/code/object/decimal.rb +32 -9
  61. data/lib/code/object/dictionnary.rb +33 -10
  62. data/lib/code/object/function.rb +64 -0
  63. data/lib/code/object/integer.rb +94 -7
  64. data/lib/code/object/list.rb +190 -10
  65. data/lib/code/object/nothing.rb +10 -9
  66. data/lib/code/object/number.rb +6 -0
  67. data/lib/code/object/range.rb +158 -0
  68. data/lib/code/object/string.rb +37 -7
  69. data/lib/code/object.rb +129 -2
  70. data/lib/code/parser/addition.rb +29 -0
  71. data/lib/code/parser/and_operator.rb +28 -0
  72. data/lib/code/parser/bitwise_and.rb +28 -0
  73. data/lib/code/parser/bitwise_or.rb +29 -0
  74. data/lib/code/parser/call.rb +77 -3
  75. data/lib/code/parser/defined.rb +20 -0
  76. data/lib/code/parser/equal.rb +42 -0
  77. data/lib/code/parser/equality.rb +36 -0
  78. data/lib/code/parser/function.rb +57 -0
  79. data/lib/code/parser/greater_than.rb +33 -0
  80. data/lib/code/parser/group.rb +17 -0
  81. data/lib/code/parser/if.rb +33 -0
  82. data/lib/code/parser/if_modifier.rb +28 -0
  83. data/lib/code/parser/multiplication.rb +30 -0
  84. data/lib/code/parser/name.rb +44 -4
  85. data/lib/code/parser/negation.rb +19 -0
  86. data/lib/code/parser/not_keyword.rb +21 -0
  87. data/lib/code/parser/nothing.rb +2 -2
  88. data/lib/code/parser/or_keyword.rb +29 -0
  89. data/lib/code/parser/or_operator.rb +28 -0
  90. data/lib/code/parser/power.rb +25 -0
  91. data/lib/code/parser/range.rb +25 -0
  92. data/lib/code/parser/rescue.rb +23 -0
  93. data/lib/code/parser/shift.rb +31 -0
  94. data/lib/code/parser/statement.rb +1 -4
  95. data/lib/code/parser/string.rb +7 -1
  96. data/lib/code/parser/ternary.rb +25 -0
  97. data/lib/code/parser/unary_minus.rb +13 -0
  98. data/lib/code/parser/while.rb +25 -0
  99. data/lib/code.rb +5 -7
  100. data/lib/template/node/code_part.rb +2 -2
  101. data/lib/template/node/part.rb +2 -2
  102. data/lib/template/node/template.rb +4 -2
  103. data/lib/template/node/text_part.rb +1 -1
  104. data/lib/template-ruby.rb +4 -0
  105. data/lib/template.rb +9 -4
  106. data/spec/call_spec.rb +22 -0
  107. data/spec/code/error/type_error_spec.rb +65 -0
  108. data/spec/code/parser/boolean_spec.rb +1 -1
  109. data/spec/code/parser/call_spec.rb +40 -11
  110. data/spec/code/parser/dictionnary_spec.rb +11 -11
  111. data/spec/code/parser/function_spec.rb +32 -0
  112. data/spec/code/parser/list_spec.rb +5 -5
  113. data/spec/code/parser/nothing_spec.rb +1 -1
  114. data/spec/code/parser/number_spec.rb +35 -35
  115. data/spec/code/parser/string_spec.rb +3 -2
  116. data/spec/code_spec.rb +75 -3
  117. data/spec/function_spec.rb +26 -0
  118. data/spec/spec_helper.rb +2 -0
  119. data/spec/template/parser/template_spec.rb +1 -1
  120. data/spec/template_spec.rb +6 -6
  121. data/template-ruby.gemspec +6 -3
  122. metadata +76 -4
data/spec/code_spec.rb CHANGED
@@ -19,10 +19,82 @@ RSpec.describe Code do
19
19
  %w[1e1e1 10000000000],
20
20
  %w['hello' hello],
21
21
  %w["hello" hello],
22
- ["user", ""],
23
- ["user.first_name", ""],
24
22
  ["[true, 1, nothing]", "[true, 1, nothing]"],
25
- ['{a: 1, "b": 2}', '{"a" => 1, "b" => 2}']
23
+ ['{a: 1, "b": 2}', '{"a" => 1, "b" => 2}'],
24
+ %w[!true false],
25
+ %w[!!true true],
26
+ %w[!!nothing false],
27
+ %w[!!1 true],
28
+ %w[+1 1],
29
+ %w[++++1 1],
30
+ ["++++nothing", ""],
31
+ %w[+{} {}],
32
+ ["2 ** 2", "4"],
33
+ ["2 ** 2 ** 3", "256"],
34
+ %w[-2 -2],
35
+ %w[--2 2],
36
+ ["2 * 3", "6"],
37
+ ["1 / 2", "0.5"],
38
+ ["1 / 2 / 2", "0.25"],
39
+ ["12 % 10", "2"],
40
+ ["8 / -2 ** 3", "-1.0"],
41
+ ["1 + 2 * 3 + 4", "11"],
42
+ ["1e1.1 * 2", "25.1785082358833442084790822"],
43
+ ["1 / 3 * 3", "0.999999999999999999999999999999999999"],
44
+ ['3 * "hello"', "hellohellohello"],
45
+ ['"Go" + "od"', "Good"],
46
+ ["1 << 2", "4"],
47
+ ["4 >> 2", "1"],
48
+ ["2 & 1", "0"],
49
+ ["2 | 1", "3"],
50
+ ["5 ^ 6", "3"],
51
+ ["5 > 6", "false"],
52
+ ["5 > 5", "false"],
53
+ ["5 > 4", "true"],
54
+ ["2 > 1 == 3 > 2", "true"],
55
+ ["true && false", "false"],
56
+ ["true || false", "true"],
57
+ %w[1..3 1..3],
58
+ ['1 > 3 ? "Impossible" : "Sounds about right"', "Sounds about right"],
59
+ ['1 < 3 ? "OK"', "OK"],
60
+ ['1 < "" rescue "oops"', "oops"],
61
+ ['"fine" rescue "oops"', "fine"],
62
+ ["a = 1", "1"],
63
+ ["a = 1 a * 2", "2"],
64
+ ["a = 1 a += 1 a", "2"],
65
+ ["a = 1 a -= 1 a", "0"],
66
+ %w[defined?(a) false],
67
+ ["a = 1 defined?(a)", "true"],
68
+ ["not true", "false"],
69
+ ["not false", "true"],
70
+ ["not not 1", "true"],
71
+ ["1 or 2", "1"],
72
+ ["true or false", "true"],
73
+ ["1 and 2", "2"],
74
+ ["false and 2", "false"],
75
+ ["true and false", "false"],
76
+ ["true and true", "true"],
77
+ ["1 if false", ""],
78
+ ["1 if true", "1"],
79
+ ["1 if true if true", "1"],
80
+ ["1 unless false", "1"],
81
+ ["1 unless true", ""],
82
+ ["a = 0 a += 1 while a < 10 a", "10"],
83
+ ["a = 0 a += 1 until a > 10 a", "11"],
84
+ ["if true 1 end", "1"],
85
+ ["if false 1 end", ""],
86
+ ["if false 1 else 2 end", "2"],
87
+ ["unless false 1 end", "1"],
88
+ ["unless true 1 end", ""],
89
+ ["if false 1 else if true 2 else 3 end", "2"],
90
+ ["if false 1 else if false 2 else 3 end", "3"],
91
+ ["unless false 1 else if false 2 else 3 end", "1"],
92
+ ["if false 1 else unless false 2 else 3 end", "2"],
93
+ ["a = 0\n while a < 10 a += 1 end a", "10"],
94
+ ["a = 0\n until a > 10 a += 1 end a", "11"],
95
+ ["until true end", ""],
96
+ ["until true\nend", ""],
97
+ %w[("Good".."Bad").first Good],
26
98
  ].each do |(input, expected)|
27
99
  context input.inspect do
28
100
  let(:input) { input }
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Code do
4
+ subject { described_class.evaluate(input).to_s }
5
+
6
+ [
7
+ ['hello = () => { "Hello" } hello', "Hello"],
8
+ ["add = (a, b) => { a + b } add(1, 2)", "3"],
9
+ ["add = (a:, b:) => { a + b } add(a: 1, b: 2)", "3"],
10
+ ["add = (a = 1, b = 2) => { a + b } add", "3"],
11
+ ["add = (a: 1, b: 2) => { a + b } add", "3"],
12
+ ["add = (*args) => { args.first + args.last } add(1, 2)", "3"],
13
+ [
14
+ "add = (**kargs) => { kargs.values.first + kargs.values.last } add(a: 1, b: 2)",
15
+ "3",
16
+ ],
17
+ ].each do |(input, expected)|
18
+ context input.inspect do
19
+ let(:input) { input }
20
+
21
+ it "succeeds" do
22
+ expect(subject).to eq(expected)
23
+ end
24
+ end
25
+ end
26
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,3 @@
1
1
  require_relative "../lib/template-ruby"
2
+
3
+ RSpec.configure { |c| c.example_status_persistence_file_path = "examples.txt" }
@@ -6,7 +6,7 @@ RSpec.describe Template::Parser::Template do
6
6
  [
7
7
  ["hello", [{ text: "hello" }]],
8
8
  ["hello {name}", [{ text: "hello " }, { code: [{ name: "name" }] }]],
9
- ["{name", [{ code: [{ name: "name" }] }]]
9
+ ["{name", [{ code: [{ name: "name" }] }]],
10
10
  ].each do |(input, expected)|
11
11
  context input.inspect do
12
12
  let(:input) { input }
@@ -10,13 +10,13 @@ RSpec.describe Template do
10
10
  [
11
11
  "Hello {user.first_name}",
12
12
  '{ user: { first_name: "Dorian" } }',
13
- "Hello Dorian"
13
+ "Hello Dorian",
14
14
  ],
15
- [
16
- "Hello {user.first_name}",
17
- "",
18
- "Hello "
19
- ]
15
+ [<<~TEMPLATE, "", ""]
16
+ {
17
+ until true
18
+ end
19
+ TEMPLATE
20
20
  ].each do |(input, input_context, expected)|
21
21
  context "#{input.inspect} #{input_context.inspect}" do
22
22
  let(:input) { input }
@@ -1,8 +1,11 @@
1
+ require_relative "lib/template-ruby"
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = "template-ruby"
3
- s.version = "0.1.0"
5
+ s.version = ::Template::VERSION
4
6
  s.summary = "A templating programming language"
5
- s.description = 'Like "Hello {name}" with {name: "Dorian"} gives "Hello Dorian"'
7
+ s.description =
8
+ 'Like "Hello {name}" with {name: "Dorian"} gives "Hello Dorian"'
6
9
  s.authors = ["Dorian Marié"]
7
10
  s.email = "dorian@dorianmarie.fr"
8
11
  s.files = `git ls-files`.split($/)
@@ -13,7 +16,7 @@ Gem::Specification.new do |s|
13
16
 
14
17
  s.add_dependency "activesupport", "~> 7"
15
18
  s.add_dependency "parslet", "~> 2"
16
- s.add_dependency "zeitwerk", "~> 2"
19
+ s.add_dependency "zeitwerk", "~> 2.6"
17
20
 
18
21
  s.add_development_dependency "prettier", "~> 3"
19
22
  s.add_development_dependency "rspec", "~> 3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: template-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-28 00:00:00.000000000 Z
11
+ date: 2022-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2'
47
+ version: '2.6'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '2'
54
+ version: '2.6'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: prettier
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -86,11 +86,23 @@ executables: []
86
86
  extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
+ - ".editorconfig"
89
90
  - ".gitignore"
91
+ - ".prettierrc"
90
92
  - ".rspec"
93
+ - CHANGELOG.md
91
94
  - Gemfile
92
95
  - Gemfile.lock
96
+ - README.md
97
+ - bin/template
98
+ - docs/euler/1.template
99
+ - docs/euler/2.template
100
+ - docs/euler/3.template
101
+ - docs/euler/4.template
102
+ - docs/euler/5.template
103
+ - docs/precedence.template
93
104
  - lib/code.rb
105
+ - lib/code/error.rb
94
106
  - lib/code/node.rb
95
107
  - lib/code/node/base_10_decimal.rb
96
108
  - lib/code/node/base_10_integer.rb
@@ -98,36 +110,88 @@ files:
98
110
  - lib/code/node/base_16_number.rb
99
111
  - lib/code/node/base_2_number.rb
100
112
  - lib/code/node/base_8_number.rb
113
+ - lib/code/node/block.rb
101
114
  - lib/code/node/boolean.rb
102
115
  - lib/code/node/call.rb
116
+ - lib/code/node/call_argument.rb
117
+ - lib/code/node/chained_call.rb
103
118
  - lib/code/node/code.rb
119
+ - lib/code/node/defined.rb
104
120
  - lib/code/node/dictionnary.rb
105
121
  - lib/code/node/dictionnary_key_value.rb
122
+ - lib/code/node/equal.rb
123
+ - lib/code/node/function.rb
124
+ - lib/code/node/function_argument.rb
125
+ - lib/code/node/group.rb
126
+ - lib/code/node/if.rb
127
+ - lib/code/node/if_modifier.rb
128
+ - lib/code/node/keyword_call_argument.rb
129
+ - lib/code/node/keyword_function_argument.rb
106
130
  - lib/code/node/list.rb
107
131
  - lib/code/node/name.rb
132
+ - lib/code/node/negation.rb
133
+ - lib/code/node/not_keyword.rb
108
134
  - lib/code/node/nothing.rb
109
135
  - lib/code/node/number.rb
136
+ - lib/code/node/operation.rb
137
+ - lib/code/node/or_keyword.rb
138
+ - lib/code/node/power.rb
139
+ - lib/code/node/range.rb
140
+ - lib/code/node/regular_call_argument.rb
141
+ - lib/code/node/regular_function_argument.rb
142
+ - lib/code/node/rescue.rb
110
143
  - lib/code/node/statement.rb
111
144
  - lib/code/node/string.rb
145
+ - lib/code/node/ternary.rb
146
+ - lib/code/node/unary_minus.rb
147
+ - lib/code/node/while.rb
112
148
  - lib/code/object.rb
149
+ - lib/code/object/argument.rb
113
150
  - lib/code/object/boolean.rb
114
151
  - lib/code/object/decimal.rb
115
152
  - lib/code/object/dictionnary.rb
153
+ - lib/code/object/function.rb
116
154
  - lib/code/object/integer.rb
117
155
  - lib/code/object/list.rb
118
156
  - lib/code/object/nothing.rb
157
+ - lib/code/object/number.rb
158
+ - lib/code/object/range.rb
119
159
  - lib/code/object/string.rb
120
160
  - lib/code/parser.rb
161
+ - lib/code/parser/addition.rb
162
+ - lib/code/parser/and_operator.rb
163
+ - lib/code/parser/bitwise_and.rb
164
+ - lib/code/parser/bitwise_or.rb
121
165
  - lib/code/parser/boolean.rb
122
166
  - lib/code/parser/call.rb
123
167
  - lib/code/parser/code.rb
168
+ - lib/code/parser/defined.rb
124
169
  - lib/code/parser/dictionnary.rb
170
+ - lib/code/parser/equal.rb
171
+ - lib/code/parser/equality.rb
172
+ - lib/code/parser/function.rb
173
+ - lib/code/parser/greater_than.rb
174
+ - lib/code/parser/group.rb
175
+ - lib/code/parser/if.rb
176
+ - lib/code/parser/if_modifier.rb
125
177
  - lib/code/parser/list.rb
178
+ - lib/code/parser/multiplication.rb
126
179
  - lib/code/parser/name.rb
180
+ - lib/code/parser/negation.rb
181
+ - lib/code/parser/not_keyword.rb
127
182
  - lib/code/parser/nothing.rb
128
183
  - lib/code/parser/number.rb
184
+ - lib/code/parser/or_keyword.rb
185
+ - lib/code/parser/or_operator.rb
186
+ - lib/code/parser/power.rb
187
+ - lib/code/parser/range.rb
188
+ - lib/code/parser/rescue.rb
189
+ - lib/code/parser/shift.rb
129
190
  - lib/code/parser/statement.rb
130
191
  - lib/code/parser/string.rb
192
+ - lib/code/parser/ternary.rb
193
+ - lib/code/parser/unary_minus.rb
194
+ - lib/code/parser/while.rb
131
195
  - lib/template-ruby.rb
132
196
  - lib/template.rb
133
197
  - lib/template/node.rb
@@ -137,15 +201,19 @@ files:
137
201
  - lib/template/node/text_part.rb
138
202
  - lib/template/parser.rb
139
203
  - lib/template/parser/template.rb
204
+ - spec/call_spec.rb
205
+ - spec/code/error/type_error_spec.rb
140
206
  - spec/code/parser/boolean_spec.rb
141
207
  - spec/code/parser/call_spec.rb
142
208
  - spec/code/parser/dictionnary_spec.rb
209
+ - spec/code/parser/function_spec.rb
143
210
  - spec/code/parser/list_spec.rb
144
211
  - spec/code/parser/name_spec.rb
145
212
  - spec/code/parser/nothing_spec.rb
146
213
  - spec/code/parser/number_spec.rb
147
214
  - spec/code/parser/string_spec.rb
148
215
  - spec/code_spec.rb
216
+ - spec/function_spec.rb
149
217
  - spec/spec_helper.rb
150
218
  - spec/template/parser/template_spec.rb
151
219
  - spec/template_spec.rb
@@ -174,15 +242,19 @@ signing_key:
174
242
  specification_version: 4
175
243
  summary: A templating programming language
176
244
  test_files:
245
+ - spec/call_spec.rb
246
+ - spec/code/error/type_error_spec.rb
177
247
  - spec/code/parser/boolean_spec.rb
178
248
  - spec/code/parser/call_spec.rb
179
249
  - spec/code/parser/dictionnary_spec.rb
250
+ - spec/code/parser/function_spec.rb
180
251
  - spec/code/parser/list_spec.rb
181
252
  - spec/code/parser/name_spec.rb
182
253
  - spec/code/parser/nothing_spec.rb
183
254
  - spec/code/parser/number_spec.rb
184
255
  - spec/code/parser/string_spec.rb
185
256
  - spec/code_spec.rb
257
+ - spec/function_spec.rb
186
258
  - spec/spec_helper.rb
187
259
  - spec/template/parser/template_spec.rb
188
260
  - spec/template_spec.rb