code-ruby 0.5.6 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -3
  3. data/Gemfile.lock +11 -21
  4. data/README.md +2 -102
  5. data/bin/code +34 -16
  6. data/code-ruby.gemspec +5 -3
  7. data/lib/code/error.rb +16 -5
  8. data/lib/code/node/base_10.rb +4 -2
  9. data/lib/code/node/base_16.rb +3 -1
  10. data/lib/code/node/base_2.rb +3 -1
  11. data/lib/code/node/base_8.rb +3 -1
  12. data/lib/code/node/boolean.rb +4 -2
  13. data/lib/code/node/call.rb +35 -12
  14. data/lib/code/node/call_argument.rb +16 -5
  15. data/lib/code/node/code.rb +5 -4
  16. data/lib/code/node/decimal.rb +2 -0
  17. data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
  18. data/lib/code/node/function.rb +7 -4
  19. data/lib/code/node/function_parameter.rb +3 -1
  20. data/lib/code/node/if.rb +5 -3
  21. data/lib/code/node/left_operation.rb +63 -0
  22. data/lib/code/node/list.rb +2 -0
  23. data/lib/code/node/negation.rb +2 -0
  24. data/lib/code/node/not.rb +2 -0
  25. data/lib/code/node/nothing.rb +3 -1
  26. data/lib/code/node/number.rb +2 -0
  27. data/lib/code/node/right_operation.rb +70 -0
  28. data/lib/code/node/splat.rb +2 -0
  29. data/lib/code/node/square_bracket.rb +37 -0
  30. data/lib/code/node/statement.rb +13 -5
  31. data/lib/code/node/string.rb +3 -1
  32. data/lib/code/node/ternary.rb +5 -3
  33. data/lib/code/node/unary_minus.rb +2 -0
  34. data/lib/code/node/while.rb +6 -4
  35. data/lib/code/node.rb +11 -5
  36. data/lib/code/object/argument.rb +13 -7
  37. data/lib/code/object/boolean.rb +43 -5
  38. data/lib/code/object/class.rb +17 -0
  39. data/lib/code/object/context.rb +36 -0
  40. data/lib/code/object/decimal.rb +252 -100
  41. data/lib/code/object/dictionary.rb +641 -0
  42. data/lib/code/object/function.rb +54 -27
  43. data/lib/code/object/global.rb +65 -19
  44. data/lib/code/object/identifier_list.rb +47 -0
  45. data/lib/code/object/integer.rb +320 -137
  46. data/lib/code/object/list.rb +140 -138
  47. data/lib/code/object/nothing.rb +10 -4
  48. data/lib/code/object/number.rb +6 -1
  49. data/lib/code/object/range.rb +85 -88
  50. data/lib/code/object/ruby_function.rb +11 -6
  51. data/lib/code/object/string.rb +51 -48
  52. data/lib/code/object.rb +117 -139
  53. data/lib/code/parser/addition.rb +4 -2
  54. data/lib/code/parser/and_operator.rb +4 -2
  55. data/lib/code/parser/bitwise_and.rb +4 -2
  56. data/lib/code/parser/bitwise_or.rb +4 -2
  57. data/lib/code/parser/boolean.rb +3 -1
  58. data/lib/code/parser/call.rb +17 -11
  59. data/lib/code/parser/chained_call.rb +10 -22
  60. data/lib/code/parser/class.rb +9 -6
  61. data/lib/code/parser/code.rb +6 -4
  62. data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
  63. data/lib/code/parser/equal.rb +9 -36
  64. data/lib/code/parser/equality.rb +4 -2
  65. data/lib/code/parser/function.rb +24 -9
  66. data/lib/code/parser/greater.rb +6 -3
  67. data/lib/code/parser/group.rb +4 -2
  68. data/lib/code/parser/if.rb +6 -4
  69. data/lib/code/parser/if_modifier.rb +5 -25
  70. data/lib/code/parser/left_operation.rb +40 -0
  71. data/lib/code/parser/list.rb +6 -5
  72. data/lib/code/parser/multiplication.rb +4 -2
  73. data/lib/code/parser/name.rb +19 -4
  74. data/lib/code/parser/negation.rb +4 -2
  75. data/lib/code/parser/not_keyword.rb +5 -3
  76. data/lib/code/parser/nothing.rb +3 -10
  77. data/lib/code/parser/number.rb +4 -2
  78. data/lib/code/parser/or_keyword.rb +4 -2
  79. data/lib/code/parser/or_operator.rb +4 -2
  80. data/lib/code/parser/power.rb +4 -28
  81. data/lib/code/parser/range.rb +4 -2
  82. data/lib/code/parser/rescue.rb +6 -26
  83. data/lib/code/parser/right_operation.rb +40 -0
  84. data/lib/code/parser/shift.rb +4 -2
  85. data/lib/code/parser/splat.rb +5 -3
  86. data/lib/code/parser/square_bracket.rb +48 -0
  87. data/lib/code/parser/statement.rb +3 -1
  88. data/lib/code/parser/string.rb +12 -10
  89. data/lib/code/parser/ternary.rb +10 -11
  90. data/lib/code/parser/unary_minus.rb +5 -3
  91. data/lib/code/parser/while.rb +5 -3
  92. data/lib/code/parser/whitespace.rb +2 -0
  93. data/lib/code/parser.rb +10 -3
  94. data/lib/code/ruby.rb +4 -2
  95. data/lib/code/type/hash.rb +38 -0
  96. data/lib/code/type/maybe.rb +29 -0
  97. data/lib/code/type/or.rb +38 -0
  98. data/lib/code/type/repeat.rb +38 -0
  99. data/lib/code/type/sig.rb +130 -0
  100. data/lib/code/type.rb +25 -0
  101. data/lib/code/version.rb +3 -0
  102. data/lib/code-ruby.rb +1 -2
  103. data/lib/code.rb +15 -16
  104. data/spec/code/node/call_spec.rb +39 -0
  105. data/spec/code/object/boolean_spec.rb +18 -0
  106. data/spec/code/object/decimal_spec.rb +51 -0
  107. data/spec/code/object/dictionary_spec.rb +98 -0
  108. data/spec/code/object/function_spec.rb +42 -0
  109. data/spec/code/object/integer_spec.rb +43 -0
  110. data/spec/code/object/nothing_spec.rb +14 -0
  111. data/spec/code/object/range_spec.rb +23 -0
  112. data/spec/code/parser/boolean_spec.rb +5 -10
  113. data/spec/code/parser/chained_call.rb +4 -5
  114. data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
  115. data/spec/code/parser/function_spec.rb +4 -5
  116. data/spec/code/parser/group_spec.rb +5 -12
  117. data/spec/code/parser/if_modifier_spec.rb +18 -0
  118. data/spec/code/parser/list_spec.rb +4 -5
  119. data/spec/code/parser/number_spec.rb +4 -5
  120. data/spec/code/parser/string_spec.rb +4 -5
  121. data/spec/code/parser_spec.rb +22 -16
  122. data/spec/code/type_spec.rb +21 -0
  123. data/spec/code_spec.rb +171 -0
  124. data/spec/spec_helper.rb +1 -6
  125. metadata +63 -136
  126. data/.cherry.js +0 -21
  127. data/.editorconfig +0 -9
  128. data/.github/workflows/rspec.yml +0 -14
  129. data/.gitignore +0 -2
  130. data/.prettierrc +0 -3
  131. data/.tool-versions +0 -1
  132. data/CHANGELOG.md +0 -55
  133. data/LICENSE +0 -7
  134. data/TODO +0 -17
  135. data/bin/format +0 -3
  136. data/bin/publish +0 -19
  137. data/bin/template +0 -85
  138. data/bin/test +0 -17
  139. data/docs/class.code +0 -9
  140. data/docs/euler/1.template +0 -10
  141. data/docs/euler/2.template +0 -16
  142. data/docs/euler/3.template +0 -16
  143. data/docs/euler/4.template +0 -10
  144. data/docs/euler/5.template +0 -13
  145. data/docs/fibonnaci.template +0 -14
  146. data/docs/meetup.code +0 -12
  147. data/docs/precedence.template +0 -36
  148. data/docs/rain.code +0 -22
  149. data/docs/slack.code +0 -17
  150. data/docs/stripe.code +0 -7
  151. data/docs/twitter.code +0 -9
  152. data/language-ruby.gemspec +0 -17
  153. data/lib/code/node/chained_call.rb +0 -23
  154. data/lib/code/node/equal.rb +0 -34
  155. data/lib/code/node/if_modifier.rb +0 -47
  156. data/lib/code/node/operation.rb +0 -38
  157. data/lib/code/node/power.rb +0 -20
  158. data/lib/code/node/rescue.rb +0 -17
  159. data/lib/code/object/dictionnary.rb +0 -96
  160. data/lib/code/parser/equality_lower.rb +0 -9
  161. data/lib/code/parser/operation.rb +0 -35
  162. data/lib/language/atom.rb +0 -342
  163. data/lib/language/output.rb +0 -130
  164. data/lib/language/parser/absent/present.rb +0 -8
  165. data/lib/language/parser/absent.rb +0 -6
  166. data/lib/language/parser/end_of_input.rb +0 -6
  167. data/lib/language/parser/interuption.rb +0 -38
  168. data/lib/language/parser/not_end_of_input.rb +0 -6
  169. data/lib/language/parser/str/not_found.rb +0 -16
  170. data/lib/language/parser/str.rb +0 -6
  171. data/lib/language/parser.rb +0 -53
  172. data/lib/language-ruby.rb +0 -10
  173. data/lib/language.rb +0 -80
  174. data/lib/template/node/code_part.rb +0 -13
  175. data/lib/template/node/part.rb +0 -19
  176. data/lib/template/node/template.rb +0 -15
  177. data/lib/template/node/text_part.rb +0 -13
  178. data/lib/template/node.rb +0 -4
  179. data/lib/template/parser/template.rb +0 -39
  180. data/lib/template/parser.rb +0 -19
  181. data/lib/template/version.rb +0 -3
  182. data/lib/template-ruby.rb +0 -10
  183. data/lib/template.rb +0 -50
  184. data/spec/code/addition_spec.rb +0 -13
  185. data/spec/code/and_operator_spec.rb +0 -13
  186. data/spec/code/bitwise_and_spec.rb +0 -13
  187. data/spec/code/bitwise_or_spec.rb +0 -13
  188. data/spec/code/boolean_spec.rb +0 -13
  189. data/spec/code/call_spec.rb +0 -21
  190. data/spec/code/chained_call_spec.rb +0 -16
  191. data/spec/code/code_spec.rb +0 -29
  192. data/spec/code/dictionnary_spec.rb +0 -17
  193. data/spec/code/equal_spec.rb +0 -26
  194. data/spec/code/equality_spec.rb +0 -13
  195. data/spec/code/function_spec.rb +0 -18
  196. data/spec/code/greater_spec.rb +0 -18
  197. data/spec/code/group_spec.rb +0 -12
  198. data/spec/code/if_modifier_spec.rb +0 -20
  199. data/spec/code/if_spec.rb +0 -25
  200. data/spec/code/list_spec.rb +0 -19
  201. data/spec/code/multiplication_spec.rb +0 -18
  202. data/spec/code/negation_spec.rb +0 -20
  203. data/spec/code/not_keyword_spec.rb +0 -13
  204. data/spec/code/nothing_spec.rb +0 -17
  205. data/spec/code/number_spec.rb +0 -22
  206. data/spec/code/or_keyword_spec.rb +0 -17
  207. data/spec/code/or_operator_spec.rb +0 -16
  208. data/spec/code/parser/call_spec.rb +0 -26
  209. data/spec/code/power_spec.rb +0 -13
  210. data/spec/code/range_spec.rb +0 -16
  211. data/spec/code/rescue_spec.rb +0 -13
  212. data/spec/code/shift_spec.rb +0 -13
  213. data/spec/code/splat_spec.rb +0 -13
  214. data/spec/code/string_spec.rb +0 -27
  215. data/spec/code/ternary_spec.rb +0 -18
  216. data/spec/code/unary_minus_spec.rb +0 -13
  217. data/spec/code/while_spec.rb +0 -18
  218. data/template-ruby.gemspec +0 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ec67f37dc432338ef23418046fd1b4672268351bbce19bc737793df2fef16c8
4
- data.tar.gz: 1e92c9e186f0463109589f843862c01633af3cbcc6d12a6f916b7b40ddd641ed
3
+ metadata.gz: 1d1e6240535fbaa53a8402bb09fca25f382435c77b631953da7650279c1779c4
4
+ data.tar.gz: 31ca274073a554129651ad5f6f398ec421bdf34ce20d0953f360b801bb4ba1f4
5
5
  SHA512:
6
- metadata.gz: 218135cc2e96e8d85609c3efe047d7aa716a3a87380dd60608bb4d2d107b44c86852362dcadeff7b18e35800dbbe172761d457d3bf039c2dae36fb8b3fa1358d
7
- data.tar.gz: c44d215dcb56eb2f2357139e16c3cca8fe4e4ecff825dc62afec8625695c6f2f0634c428605e57461f73debd58c6d72a6a4f2fcb0a680cf76593df4edbd9d5a0
6
+ metadata.gz: a81a91547923b0bcdca0b971fc0a18a684c439a4bf4821fd4b981afa06d4dbcb7baaf00b4e0e04513a2bc4abbd965b538ab92a7f4cf5739a42d1ffd306c72108
7
+ data.tar.gz: b4135cc2b58e02bde82fe6fb41de533f5c37e0c8ca13bd2507e8c559fb2ebd71b796ab36059c5db0f0a51ca73091c75581136c38a5558e89e0f3ef8341f7a436
data/Gemfile CHANGED
@@ -1,8 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gemspec name: "template-ruby"
4
- gemspec name: "code-ruby"
3
+ gemspec
5
4
 
6
5
  gem "rspec"
7
- gem "syntax_tree", github: "dorianmariefr/syntax_tree"
8
6
  gem "ruby-prof"
data/Gemfile.lock CHANGED
@@ -1,49 +1,39 @@
1
- GIT
2
- remote: https://github.com/dorianmariefr/syntax_tree.git
3
- revision: f7c9dc36a3d60c90a8d2411f123913b37319dc11
4
- specs:
5
- syntax_tree (5.0.1)
6
- prettier_print (>= 1.1.0)
7
-
8
1
  PATH
9
2
  remote: .
10
3
  specs:
11
- code-ruby (0.5.6)
12
- zeitwerk (~> 2)
13
- template-ruby (0.5.6)
4
+ code-ruby (0.6.0)
5
+ language-ruby (~> 0)
14
6
  zeitwerk (~> 2)
15
7
 
16
8
  GEM
17
9
  remote: https://rubygems.org/
18
10
  specs:
19
11
  diff-lcs (1.5.0)
20
- prettier_print (1.1.0)
12
+ language-ruby (0.6.0)
13
+ zeitwerk (~> 2)
21
14
  rspec (3.12.0)
22
15
  rspec-core (~> 3.12.0)
23
16
  rspec-expectations (~> 3.12.0)
24
17
  rspec-mocks (~> 3.12.0)
25
- rspec-core (3.12.0)
18
+ rspec-core (3.12.2)
26
19
  rspec-support (~> 3.12.0)
27
- rspec-expectations (3.12.0)
20
+ rspec-expectations (3.12.3)
28
21
  diff-lcs (>= 1.2.0, < 2.0)
29
22
  rspec-support (~> 3.12.0)
30
- rspec-mocks (3.12.0)
23
+ rspec-mocks (3.12.6)
31
24
  diff-lcs (>= 1.2.0, < 2.0)
32
25
  rspec-support (~> 3.12.0)
33
- rspec-support (3.12.0)
34
- ruby-prof (1.4.3)
35
- zeitwerk (2.6.6)
26
+ rspec-support (3.12.1)
27
+ ruby-prof (1.6.3)
28
+ zeitwerk (2.6.12)
36
29
 
37
30
  PLATFORMS
38
- arm64-darwin-21
39
31
  arm64-darwin-22
40
32
 
41
33
  DEPENDENCIES
42
34
  code-ruby!
43
35
  rspec
44
36
  ruby-prof
45
- syntax_tree!
46
- template-ruby!
47
37
 
48
38
  BUNDLED WITH
49
- 2.3.18
39
+ 2.4.22
data/README.md CHANGED
@@ -1,103 +1,3 @@
1
- # Template
1
+ # code-ruby
2
2
 
3
- [![RSpec](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml/badge.svg)](https://github.com/dorianmariefr/template-ruby/actions/workflows/rspec.yml)
4
-
5
- See [templatelang.com](https://templatelang.com) for the full documentation and
6
- live code editing.
7
-
8
- ## The programming language
9
-
10
- Hi, I'm [Dorian Marié](https://dorianmarie.fr), I created Template to let users of my websites provide templates to customize their experience.
11
-
12
- Template is meant to be:
13
-
14
- - **Simple**: `Hello` and `Hello {name}`
15
- - **Safe**: Can be provided user input
16
- - **Powerful**: Functions, object-oriented, built-in methods
17
-
18
- Template is currently written in Ruby and embeddable as a Ruby gem.
19
-
20
- ## Install
21
-
22
- ### As a command line tool:
23
-
24
- ```bash
25
- $ gem install template-ruby
26
- $ template --input "Hello {name}" --context '{ name: "Dorian" }'
27
- Hello Dorian
28
- $ template --input "1 + 2 = {1 + 2}"
29
- 1 + 2 = 3
30
- ```
31
-
32
- ### As a Ruby gem:
33
-
34
- In a `Gemfile`:
35
-
36
- ```ruby
37
- gem "template-ruby"
38
- ```
39
-
40
- Then `$ bundle install`
41
-
42
- Then you can use Template like:
43
-
44
- ```ruby
45
- Template.render("Hello {name}", '{ name: "Dorian" }')
46
- # => "Hello Dorian"
47
- Template.render("1 + 2 = {1 + 2}")
48
- # => "1 + 2 = 3"
49
- Template.render(input, context, io: StringIO.new, timeout: 10)
50
- ```
51
-
52
- The context is a sub-language called Code, you can use it like:
53
-
54
- ```ruby
55
- Code.evaluate("1 + 2") # => 3
56
- ```
57
-
58
- ## Future work
59
-
60
- - Extend standard library
61
- - Global methods from Ruby, e.g. `{markdown "**bold**"}`
62
- - Object methods from Ruby, e.g. `{"**bold**".markdown}`
63
- - Classes, e.g. `{class User end}`
64
- - Write JavaScript version
65
- - Write Crystal version
66
-
67
- ## Contributing
68
-
69
- Feel free to open [issues](https://github.com/dorianmariefr/template-ruby/issues),
70
- and [pull requests](https://github.com/dorianmariefr/template-ruby/pulls).
71
-
72
- To develop locally:
73
-
74
- ```text
75
- $ git clone https://github.com/dorianmariefr/template-ruby
76
- $ cd template-ruby
77
- $ bundle
78
- $ rspec
79
- $ bin/template -i docs/...
80
- ```
81
-
82
- ## Credits
83
-
84
- Thanks to [thoughtbot](https://thoughtbot.com) who let me work on this programming
85
- language as a Friday project.
86
-
87
- Thanks to [Kaspar Schiess](https://github.com/kschiess) who made
88
- [Parslet](https://kschiess.github.io/parslet/), the gem that helped me write the parser.
89
-
90
- Inspiration from [Ruby](https://www.ruby-lang.org/en/) and
91
- [Liquid](https://shopify.github.io/liquid/).
92
-
93
- ## License
94
-
95
- MIT
96
-
97
- Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
98
-
99
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
100
-
101
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
102
-
103
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3
+ A programming language, like `Code.evaluate("1 + 1") # => 2`
data/bin/code CHANGED
@@ -1,20 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "optparse"
4
- require_relative "../lib/template-ruby"
4
+ require_relative "../lib/code-ruby"
5
5
 
6
- options = { timeout: 0, profile: false }
6
+ options = { timeout: 0, profile: false, profiler: "text" }
7
7
 
8
8
  OptionParser
9
9
  .new do |opts|
10
- opts.banner = "Usage: bin/code [options]"
10
+ opts.banner = "Usage: template [options]"
11
+
12
+ opts.on(
13
+ "-v",
14
+ "--version",
15
+ "Version of template"
16
+ ) do |input|
17
+ puts Code::Version
18
+ exit
19
+ end
11
20
 
12
21
  opts.on(
13
22
  "-i INPUT",
14
23
  "--input=INPUT",
15
24
  "Input in the code language (String or File)"
16
25
  ) do |input|
17
- input = File.read(input) if File.exists?(input)
26
+ input = File.read(input) if File.exist?(input)
18
27
 
19
28
  options[:input] = input
20
29
  end
@@ -24,7 +33,7 @@ OptionParser
24
33
  "--context=CONTEXT",
25
34
  "Context in the code language (String or File)"
26
35
  ) do |context|
27
- context = File.read(context) if File.exists?(context)
36
+ context = File.read(context) if File.exist?(context)
28
37
 
29
38
  options[:context] = context
30
39
  end
@@ -46,6 +55,15 @@ OptionParser
46
55
  require "ruby-prof"
47
56
  options[:profile] = true
48
57
  end
58
+
59
+ opts.on(
60
+ "--profiler TYPE",
61
+ "Profiler output type (text (default) or html)"
62
+ ) do |profiler|
63
+ require "ruby-prof"
64
+ options[:profile] = true
65
+ options[:profiler] = profiler
66
+ end
49
67
  end
50
68
  .parse!
51
69
 
@@ -57,20 +75,20 @@ if options[:profile]
57
75
  end
58
76
 
59
77
  if options[:parse]
60
- pp ::Code::Parser.parse(input).to_raw
78
+ pp Code::Parser.parse(input).to_raw
61
79
  else
62
- print(
63
- Code.evaluate(
64
- input,
65
- context,
66
- io: $stdout,
67
- timeout: options[:timeout]
68
- ).to_s
69
- )
80
+ print Code.evaluate(input, context, io: $stdout, timeout: options[:timeout])
70
81
  end
71
82
 
72
83
  if options[:profile]
73
84
  result = RubyProf.stop
74
- printer = RubyProf::FlatPrinter.new(result)
75
- printer.print($stdout)
85
+ if options[:profiler] == "text"
86
+ printer = RubyProf::FlatPrinter.new(result)
87
+ printer.print($stdout)
88
+ elsif options[:profiler] == "html"
89
+ printer = RubyProf::GraphHtmlPrinter.new(result)
90
+ printer.print($stdout)
91
+ else
92
+ abort "#{options[:profiler]} not recognized"
93
+ end
76
94
  end
data/code-ruby.gemspec CHANGED
@@ -1,8 +1,8 @@
1
- require_relative "lib/template/version"
1
+ require_relative "lib/code/version"
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "code-ruby"
5
- s.version = ::Template::Version
5
+ s.version = ::Code::Version
6
6
  s.summary = "A programming language"
7
7
  s.description = 'A programming language, like Code.evaluate("1 + 1") # => 2'
8
8
  s.authors = ["Dorian Marié"]
@@ -10,8 +10,10 @@ Gem::Specification.new do |s|
10
10
  s.files = `git ls-files`.split($/)
11
11
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
12
12
  s.require_paths = ["lib"]
13
- s.homepage = "https://github.com/dorianmariefr/template-ruby"
13
+ s.homepage = "https://github.com/dorianmariefr/code-ruby"
14
14
  s.license = "MIT"
15
+ s.executables = "code"
15
16
 
16
17
  s.add_dependency "zeitwerk", "~> 2"
18
+ s.add_dependency "language-ruby", "~> 0"
17
19
  end
data/lib/code/error.rb CHANGED
@@ -1,18 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Error < StandardError
3
- class TypeError < ::Code::Error
5
+ class ArityError < Error
6
+ end
7
+
8
+ class TypeError < Error
9
+ end
10
+
11
+ class TypeError < Error
12
+ end
13
+
14
+ class Undefined < Error
4
15
  end
5
16
 
6
- class Undefined < ::Code::Error
17
+ class UndefinedVariable < Error
7
18
  end
8
19
 
9
- class UndefinedVariable < ::Code::Error
20
+ class ArgumentError < Error
10
21
  end
11
22
 
12
- class ArgumentError < ::Code::Error
23
+ class IncompatibleContext < Error
13
24
  end
14
25
 
15
- class IncompatibleContext < ::Code::Error
26
+ class KeyNotFound < Error
16
27
  end
17
28
  end
18
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Base10 < Node
@@ -16,9 +18,9 @@ class Code
16
18
  exponent = @exponent.evaluate(**args)
17
19
 
18
20
  if exponent.is_a?(::Code::Object::Integer)
19
- ::Code::Object::Integer.new(@whole.to_i, exponent: exponent)
21
+ ::Code::Object::Integer.new(@whole.to_i, exponent:)
20
22
  else
21
- ::Code::Object::Decimal.new(@whole, exponent: exponent)
23
+ ::Code::Object::Decimal.new(@whole, exponent:)
22
24
  end
23
25
  else
24
26
  ::Code::Object::Integer.new(@whole.to_i)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Base16 < Node
@@ -5,7 +7,7 @@ class Code
5
7
  @base_16 = parsed
6
8
  end
7
9
 
8
- def evaluate(**args)
10
+ def evaluate(**_args)
9
11
  ::Code::Object::Integer.new(@base_16.to_i(16))
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Base2 < Node
@@ -5,7 +7,7 @@ class Code
5
7
  @base_2 = parsed
6
8
  end
7
9
 
8
- def evaluate(**args)
10
+ def evaluate(**_args)
9
11
  ::Code::Object::Integer.new(@base_2.to_i(2))
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Base8 < Node
@@ -5,7 +7,7 @@ class Code
5
7
  @base_8 = parsed
6
8
  end
7
9
 
8
- def evaluate(**args)
10
+ def evaluate(**_args)
9
11
  ::Code::Object::Integer.new(@base_8.to_i(8))
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Boolean < Node
@@ -8,13 +10,13 @@ class Code
8
10
  @boolean = parsed
9
11
  end
10
12
 
11
- def evaluate(**args)
13
+ def evaluate(**_args)
12
14
  if @boolean == TRUE_KEYWORD
13
15
  ::Code::Object::Boolean.new(true)
14
16
  elsif @boolean == FALSE_KEYWORD
15
17
  ::Code::Object::Boolean.new(false)
16
18
  else
17
- raise NotImplementedError.new(@boolean)
19
+ raise NotImplementedError, @boolean
18
20
  end
19
21
  end
20
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Call < Node
@@ -6,16 +8,16 @@ class Code
6
8
  @parameters =
7
9
  parsed
8
10
  .delete(:parameters) { [] }
9
- .map { |parameter| Node::FunctionParameter.new(parameter) }
11
+ .map { |parameter| FunctionParameter.new(parameter) }
10
12
 
11
- @body = Node::Code.new(parsed.delete(:body))
13
+ @body = Code.new(parsed.delete(:body))
12
14
 
13
15
  super(parsed)
14
16
  end
15
17
 
16
- def evaluate(**args)
17
- ::Code::Object::Argument.new(
18
- ::Code::Object::Function.new(parameters: @parameters, body: @body)
18
+ def evaluate(**_args)
19
+ Object::Argument.new(
20
+ Object::Function.new(parameters: @parameters, body: @body)
19
21
  )
20
22
  end
21
23
  end
@@ -25,22 +27,43 @@ class Code
25
27
  @arguments =
26
28
  parsed
27
29
  .delete(:arguments) { [] }
28
- .map { |argument| Node::CallArgument.new(argument) }
30
+ .map { |argument| CallArgument.new(argument) }
29
31
 
30
- if parsed.key?(:block)
31
- @block = Node::Call::Block.new(parsed.delete(:block))
32
- end
32
+ @block = Call::Block.new(parsed.delete(:block)) if parsed.key?(:block)
33
33
 
34
34
  super(parsed)
35
35
  end
36
36
 
37
37
  def evaluate(**args)
38
- arguments = @arguments.map { |argument| argument.evaluate(**args) }
38
+ arguments = []
39
+
40
+ @arguments.each do |argument|
41
+ if argument.keyword?
42
+ if arguments.last&.value.is_a?(Object::Dictionary)
43
+ arguments.last.value[argument.name] = argument.evaluate(
44
+ **args
45
+ ).value
46
+ else
47
+ arguments << Object::Argument.new(
48
+ Object::Dictionary.new(
49
+ { argument.name => argument.evaluate(**args).value }
50
+ )
51
+ )
52
+ end
53
+ else
54
+ arguments << argument.evaluate(**args)
55
+ end
56
+ end
57
+
39
58
  arguments << @block.evaluate(**args) if @block
40
59
 
41
- name = ::Code::Object::String.new(@name)
60
+ name = Object::String.new(@name)
61
+
62
+ args.fetch(:object).call(operator: name, arguments:, **args)
63
+ end
42
64
 
43
- args.fetch(:object).call(operator: name, arguments: arguments, **args)
65
+ def resolve(**_args)
66
+ Object::String.new(@name)
44
67
  end
45
68
  end
46
69
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class CallArgument < Node
@@ -8,14 +10,23 @@ class Code
8
10
 
9
11
  def evaluate(**args)
10
12
  if @name
11
- ::Code::Object::Argument.new(
12
- @value.evaluate(**args),
13
- name: ::Code::Object::String.new(@name)
14
- )
13
+ Object::Argument.new(@value.evaluate(**args), name:)
15
14
  else
16
- ::Code::Object::Argument.new(@value.evaluate(**args))
15
+ Object::Argument.new(@value.evaluate(**args))
17
16
  end
18
17
  end
18
+
19
+ def keyword?
20
+ !!@name
21
+ end
22
+
23
+ def regular?
24
+ !keyword?
25
+ end
26
+
27
+ def name
28
+ Object::String.new(@name)
29
+ end
19
30
  end
20
31
  end
21
32
  end
@@ -1,16 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Code < Node
4
6
  def initialize(parsed)
5
- @statements = parsed.map { |statement| Node::Statement.new(statement) }
7
+ @statements = parsed.map { |statement| Statement.new(statement) }
6
8
  end
7
9
 
8
10
  def evaluate(**args)
9
- last = ::Code::Object::Nothing.new
11
+ last = Object::Nothing.new
10
12
 
11
13
  @statements.each do |statement|
12
- last =
13
- statement.evaluate(**args.merge(object: ::Code::Object::Global.new))
14
+ last = statement.evaluate(**args.merge(object: Object::Global.new))
14
15
  end
15
16
 
16
17
  last
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Decimal < Node
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
- class Dictionnary < Node
5
+ class Dictionary < Node
4
6
  class KeyValue < Node
5
7
  def initialize(parsed)
6
8
  if parsed.key?(:statement)
@@ -9,22 +11,29 @@ class Code
9
11
  @key = Node::String.new([{ text: parsed.delete(:name) }])
10
12
  end
11
13
 
12
- @value = Node::Code.new(parsed.delete(:value))
14
+ @value = Node::Code.new(parsed.delete(:value)) if parsed[:value]
13
15
  end
14
16
 
15
17
  def evaluate(**args)
16
- [@key.evaluate(**args), @value.evaluate(**args)]
18
+ key = @key.evaluate(**args)
19
+
20
+ if @value
21
+ value = @value.evaluate(**args)
22
+ [key, value]
23
+ else
24
+ [key, key]
25
+ end
17
26
  end
18
27
  end
19
28
 
20
29
  def initialize(parsed)
21
30
  parsed = [] if parsed == ""
22
31
  @key_values =
23
- parsed.map { |key_value| Node::Dictionnary::KeyValue.new(key_value) }
32
+ parsed.map { |key_value| Node::Dictionary::KeyValue.new(key_value) }
24
33
  end
25
34
 
26
35
  def evaluate(**args)
27
- ::Code::Object::Dictionnary.new(
36
+ ::Code::Object::Dictionary.new(
28
37
  @key_values.map { |key_value| key_value.evaluate(**args) }.to_h
29
38
  )
30
39
  end
@@ -1,18 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class Function < Node
4
6
  def initialize(parsed)
7
+ @parameters = parsed.delete(:parameters) { [] }
8
+ @parameters = [] if @parameters.empty?
9
+
5
10
  @parameters =
6
- parsed
7
- .delete(:parameters) { [] }
8
- .map { |parameter| Node::FunctionParameter.new(parameter) }
11
+ @parameters.map { |parameter| Node::FunctionParameter.new(parameter) }
9
12
 
10
13
  @body = Node::Code.new(parsed.delete(:body))
11
14
 
12
15
  super(parsed)
13
16
  end
14
17
 
15
- def evaluate(**args)
18
+ def evaluate(**_args)
16
19
  ::Code::Object::Function.new(parameters: @parameters, body: @body)
17
20
  end
18
21
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class FunctionParameter < Node
4
6
  def initialize(parsed)
5
7
  @name = parsed.delete(:name)
6
- @keyword = !!parsed.delete(:keyword)
8
+ @keyword = !parsed.delete(:keyword).nil?
7
9
  super(parsed)
8
10
  end
9
11
 
data/lib/code/node/if.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Code
2
4
  class Node
3
5
  class If < Node
@@ -13,9 +15,9 @@ class Code
13
15
  @operator = parsed.delete(:operator)
14
16
  @body = Node::Code.new(parsed.delete(:body))
15
17
 
16
- if parsed.key?(:statement)
17
- @statement = Node::Statement.new(parsed.delete(:statement))
18
- end
18
+ return unless parsed.key?(:statement)
19
+
20
+ @statement = Node::Statement.new(parsed.delete(:statement))
19
21
  end
20
22
  end
21
23