code-ruby 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +9 -0
  3. data/.github/workflows/rspec.yml +14 -0
  4. data/.gitignore +2 -0
  5. data/.prettierrc +3 -0
  6. data/.rspec +1 -0
  7. data/CHANGELOG.md +31 -0
  8. data/Gemfile +3 -0
  9. data/Gemfile.lock +70 -0
  10. data/LICENSE +7 -0
  11. data/README.md +103 -0
  12. data/TODO.md +1 -0
  13. data/bin/template +39 -0
  14. data/code-ruby.gemspec +22 -0
  15. data/docs/euler/1.template +14 -0
  16. data/docs/euler/2.template +16 -0
  17. data/docs/euler/3.template +16 -0
  18. data/docs/euler/4.template +11 -0
  19. data/docs/euler/5.template +14 -0
  20. data/docs/precedence.template +94 -0
  21. data/lib/code/error.rb +15 -0
  22. data/lib/code/node/base_10_decimal.rb +32 -0
  23. data/lib/code/node/base_10_integer.rb +32 -0
  24. data/lib/code/node/base_10_number.rb +19 -0
  25. data/lib/code/node/base_16_number.rb +19 -0
  26. data/lib/code/node/base_2_number.rb +19 -0
  27. data/lib/code/node/base_8_number.rb +19 -0
  28. data/lib/code/node/block.rb +17 -0
  29. data/lib/code/node/boolean.rb +22 -0
  30. data/lib/code/node/call.rb +52 -0
  31. data/lib/code/node/call_argument.rb +37 -0
  32. data/lib/code/node/chained_call.rb +38 -0
  33. data/lib/code/node/code.rb +16 -0
  34. data/lib/code/node/defined.rb +19 -0
  35. data/lib/code/node/dictionnary.rb +22 -0
  36. data/lib/code/node/dictionnary_key_value.rb +23 -0
  37. data/lib/code/node/equal.rb +36 -0
  38. data/lib/code/node/function.rb +17 -0
  39. data/lib/code/node/function_argument.rb +45 -0
  40. data/lib/code/node/group.rb +13 -0
  41. data/lib/code/node/if.rb +55 -0
  42. data/lib/code/node/if_modifier.rb +48 -0
  43. data/lib/code/node/keyword_call_argument.rb +30 -0
  44. data/lib/code/node/keyword_function_argument.rb +33 -0
  45. data/lib/code/node/list.rb +19 -0
  46. data/lib/code/node/name.rb +50 -0
  47. data/lib/code/node/negation.rb +33 -0
  48. data/lib/code/node/not_keyword.rb +13 -0
  49. data/lib/code/node/nothing.rb +12 -0
  50. data/lib/code/node/number.rb +23 -0
  51. data/lib/code/node/operation.rb +33 -0
  52. data/lib/code/node/or_keyword.rb +34 -0
  53. data/lib/code/node/power.rb +16 -0
  54. data/lib/code/node/range.rb +31 -0
  55. data/lib/code/node/regular_call_argument.rb +34 -0
  56. data/lib/code/node/regular_function_argument.rb +36 -0
  57. data/lib/code/node/rescue.rb +16 -0
  58. data/lib/code/node/statement.rb +81 -0
  59. data/lib/code/node/string.rb +17 -0
  60. data/lib/code/node/ternary.rb +26 -0
  61. data/lib/code/node/unary_minus.rb +22 -0
  62. data/lib/code/node/while.rb +42 -0
  63. data/lib/code/node.rb +14 -0
  64. data/lib/code/object/argument.rb +41 -0
  65. data/lib/code/object/boolean.rb +27 -0
  66. data/lib/code/object/decimal.rb +54 -0
  67. data/lib/code/object/dictionnary.rb +55 -0
  68. data/lib/code/object/function.rb +64 -0
  69. data/lib/code/object/integer.rb +116 -0
  70. data/lib/code/object/list.rb +217 -0
  71. data/lib/code/object/nothing.rb +23 -0
  72. data/lib/code/object/number.rb +6 -0
  73. data/lib/code/object/range.rb +158 -0
  74. data/lib/code/object/string.rb +68 -0
  75. data/lib/code/object.rb +130 -0
  76. data/lib/code/parser/addition.rb +29 -0
  77. data/lib/code/parser/and_operator.rb +28 -0
  78. data/lib/code/parser/bitwise_and.rb +28 -0
  79. data/lib/code/parser/bitwise_or.rb +29 -0
  80. data/lib/code/parser/boolean.rb +14 -0
  81. data/lib/code/parser/call.rb +90 -0
  82. data/lib/code/parser/code.rb +19 -0
  83. data/lib/code/parser/defined.rb +20 -0
  84. data/lib/code/parser/dictionnary.rb +41 -0
  85. data/lib/code/parser/equal.rb +42 -0
  86. data/lib/code/parser/equality.rb +36 -0
  87. data/lib/code/parser/function.rb +57 -0
  88. data/lib/code/parser/greater_than.rb +33 -0
  89. data/lib/code/parser/group.rb +17 -0
  90. data/lib/code/parser/if.rb +33 -0
  91. data/lib/code/parser/if_modifier.rb +28 -0
  92. data/lib/code/parser/list.rb +29 -0
  93. data/lib/code/parser/multiplication.rb +30 -0
  94. data/lib/code/parser/name.rb +89 -0
  95. data/lib/code/parser/negation.rb +19 -0
  96. data/lib/code/parser/not_keyword.rb +21 -0
  97. data/lib/code/parser/nothing.rb +17 -0
  98. data/lib/code/parser/number.rb +98 -0
  99. data/lib/code/parser/or_keyword.rb +29 -0
  100. data/lib/code/parser/or_operator.rb +28 -0
  101. data/lib/code/parser/power.rb +25 -0
  102. data/lib/code/parser/range.rb +25 -0
  103. data/lib/code/parser/rescue.rb +23 -0
  104. data/lib/code/parser/shift.rb +31 -0
  105. data/lib/code/parser/statement.rb +8 -0
  106. data/lib/code/parser/string.rb +72 -0
  107. data/lib/code/parser/ternary.rb +25 -0
  108. data/lib/code/parser/unary_minus.rb +13 -0
  109. data/lib/code/parser/while.rb +25 -0
  110. data/lib/code/parser.rb +4 -0
  111. data/lib/code-ruby.rb +11 -0
  112. data/lib/code.rb +29 -0
  113. data/lib/template/node/code_part.rb +13 -0
  114. data/lib/template/node/part.rb +19 -0
  115. data/lib/template/node/template.rb +15 -0
  116. data/lib/template/node/text_part.rb +13 -0
  117. data/lib/template/node.rb +4 -0
  118. data/lib/template/parser/template.rb +30 -0
  119. data/lib/template/parser.rb +4 -0
  120. data/lib/template/version.rb +3 -0
  121. data/lib/template-ruby.rb +11 -0
  122. data/lib/template.rb +34 -0
  123. data/spec/call_spec.rb +22 -0
  124. data/spec/code/error/type_error_spec.rb +65 -0
  125. data/spec/code/parser/boolean_spec.rb +18 -0
  126. data/spec/code/parser/call_spec.rb +66 -0
  127. data/spec/code/parser/dictionnary_spec.rb +46 -0
  128. data/spec/code/parser/function_spec.rb +32 -0
  129. data/spec/code/parser/list_spec.rb +29 -0
  130. data/spec/code/parser/name_spec.rb +15 -0
  131. data/spec/code/parser/nothing_spec.rb +19 -0
  132. data/spec/code/parser/number_spec.rb +117 -0
  133. data/spec/code/parser/string_spec.rb +30 -0
  134. data/spec/code_spec.rb +108 -0
  135. data/spec/function_spec.rb +26 -0
  136. data/spec/spec_helper.rb +3 -0
  137. data/spec/template/parser/template_spec.rb +19 -0
  138. data/spec/template_spec.rb +27 -0
  139. data/template-ruby.gemspec +24 -0
  140. metadata +266 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 82ade8726d95b0c6a5305197d56eeba0470ba5ae02df49ae96ec5fb1d16a46bf
4
+ data.tar.gz: 004cc8befedb42093761f65464c24d937e8437b14adba8d8bcd0998831be972e
5
+ SHA512:
6
+ metadata.gz: 817a6ae6b29e967cca049d5b31c39348a8dfb9c4e84c78bae33053389f3d8e2768689e07b290b6e3ed66e3929871d95c5efd0df190f2b54623b00be283847603
7
+ data.tar.gz: 4535e7a6cfad6639b837c75fc54ad369b6b7666007cc360300aa3ef520f4da5e8b4ce402e6e65bf74460a3fd8eecc8ff25db7c94097c9cfadb38e53874658a19
data/.editorconfig ADDED
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ insert_final_newline = true
7
+ indent_style = space
8
+ indent_size = 2
9
+ trim_trailing_whitespace = true
@@ -0,0 +1,14 @@
1
+ name: RSpec
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ name: RSpec
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+ - uses: actions/setup-ruby@v1
13
+ - run: bundle install
14
+ - run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ examples.txt
data/.prettierrc ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "rubyPlugins": "plugin/trailing_comma"
3
+ }
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,31 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 0.2.4 / 2022-08-02
9
+
10
+ - Add method `String#*`, e.g. `{"Dorian " \* 2}" -> "Dorian Dorian "
11
+ - Add executable to gem, e.g. `template --help`
12
+
13
+ ## 0.2.3 / 2022-08-31
14
+
15
+ - Add default timeout for code and template parsing and evaluation
16
+
17
+ ## 0.2.2 / 2022-08-31
18
+
19
+ - Fix parsing error when the template is empty, e.g. ""
20
+
21
+ ## 0.2.1 / 2022-08-31
22
+
23
+ - Fix parsing error on empty code like `Hello {`
24
+
25
+ ## 0.2.0 / 2022-08-30
26
+
27
+ - Programming language capable of solving the first 5 Project Euler problems
28
+
29
+ ## 0.1.0 / 2022-07-28
30
+
31
+ - Initial version with interpolation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ template-ruby (0.2.3)
5
+ activesupport (~> 7)
6
+ parslet (~> 2)
7
+ zeitwerk (~> 2.6)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.3.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ concurrent-ruby (1.1.10)
18
+ diff-lcs (1.5.0)
19
+ haml (5.2.2)
20
+ temple (>= 0.8.0)
21
+ tilt
22
+ i18n (1.12.0)
23
+ concurrent-ruby (~> 1.0)
24
+ minitest (5.16.3)
25
+ parslet (2.0.0)
26
+ prettier (3.2.0)
27
+ syntax_tree (>= 2.7.1)
28
+ syntax_tree-haml (>= 1.1.0)
29
+ syntax_tree-rbs (>= 0.2.0)
30
+ prettier_print (0.1.0)
31
+ rbs (2.6.0)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ syntax_tree (3.5.0)
46
+ prettier_print
47
+ syntax_tree-haml (1.3.1)
48
+ haml (>= 5.2)
49
+ prettier_print
50
+ syntax_tree (>= 2.0.1)
51
+ syntax_tree-rbs (0.5.0)
52
+ prettier_print
53
+ rbs
54
+ syntax_tree (>= 2.0.1)
55
+ temple (0.8.2)
56
+ tilt (2.0.11)
57
+ tzinfo (2.0.5)
58
+ concurrent-ruby (~> 1.0)
59
+ zeitwerk (2.6.0)
60
+
61
+ PLATFORMS
62
+ arm64-darwin-21
63
+
64
+ DEPENDENCIES
65
+ prettier (~> 3)
66
+ rspec (~> 3)
67
+ template-ruby!
68
+
69
+ BUNDLED WITH
70
+ 2.3.18
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Dorian Marié <dorian@dorianmarie.fr>
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # Template
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.
data/TODO.md ADDED
@@ -0,0 +1 @@
1
+ - Code gem
data/bin/template ADDED
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require_relative "../lib/template-ruby"
5
+
6
+ options = {}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: template [options]"
10
+
11
+ opts.on("-i INPUT", "--input=INPUT", "Input in the template language (String or File)") do |input|
12
+ if File.exists?(input)
13
+ input = File.read(input)
14
+ end
15
+
16
+ options[:input] = input
17
+ end
18
+
19
+ opts.on("-c CONTEXT", "--context=CONTEXT", "Context in the code language (String or File)") do |context|
20
+ if File.exists?(context)
21
+ context = File.read(context)
22
+ end
23
+
24
+ options[:context] = context
25
+ end
26
+
27
+ opts.on("-p", "--parse", "Get parser results for input") do |parse|
28
+ options[:parse] = parse
29
+ end
30
+ end.parse!
31
+
32
+ input = options.fetch(:input, "")
33
+ context = options.fetch(:context, "")
34
+
35
+ if options[:parse]
36
+ pp ::Template::Parser::Template.new.parse(input)
37
+ else
38
+ Template.render(input, context, io: $stdout)
39
+ end
data/code-ruby.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ require_relative "lib/template/version"
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "code-ruby"
5
+ s.version = ::Template::Version
6
+ s.summary = "A programming language"
7
+ s.description = 'A programming language, like Code.evaluate("1 + 1") # => 2'
8
+ s.authors = ["Dorian Marié"]
9
+ s.email = "dorian@dorianmarie.fr"
10
+ s.files = `git ls-files`.split($/)
11
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
12
+ s.require_paths = ["lib"]
13
+ s.homepage = "https://github.com/dorianmariefr/template-ruby"
14
+ s.license = "MIT"
15
+
16
+ s.add_dependency "activesupport", "~> 7"
17
+ s.add_dependency "parslet", "~> 2"
18
+ s.add_dependency "zeitwerk", "~> 2.6"
19
+
20
+ s.add_development_dependency "prettier", "~> 3"
21
+ s.add_development_dependency "rspec", "~> 3"
22
+ end
@@ -0,0 +1,14 @@
1
+ {
2
+ sum = 0
3
+
4
+ i = 1
5
+
6
+ while i < 1000
7
+ if i % 3 == 0 or i % 5 == 0
8
+ sum += i
9
+ end
10
+
11
+ i += 1
12
+ end
13
+
14
+ sum
@@ -0,0 +1,16 @@
1
+ {
2
+ a = 1
3
+ b = 2
4
+ sum = 2
5
+
6
+ while b < 4000000
7
+ c = a + b
8
+ a = b
9
+ b = c
10
+
11
+ if b % 2 == 0
12
+ sum += b
13
+ end
14
+ end
15
+
16
+ sum
@@ -0,0 +1,16 @@
1
+ {
2
+ n = 600851475143
3
+ max = 0
4
+
5
+ i = 2
6
+
7
+ until n == 1
8
+ while n % i == 0
9
+ max = i
10
+ n = n / i
11
+ end
12
+
13
+ i += 1
14
+ end
15
+
16
+ max
@@ -0,0 +1,11 @@
1
+ {
2
+
3
+ min = 1
4
+ max = 999
5
+
6
+
7
+ (min..max).map do |i|
8
+ ((min..max).to_list.reverse.detect do |j|
9
+ (i * j).to_string.reverse == (i * j).to_string
10
+ end || 0) * i
11
+ end.max
@@ -0,0 +1,14 @@
1
+ {
2
+ min = 1
3
+ max = 20
4
+
5
+ step = [2, 3, 5, 7, 11, 13, 17].reduce do |acc, el|
6
+ acc * el
7
+ end
8
+
9
+ (step..(step * step)).step(step).detect do |i|
10
+ (min..max).all? do |j|
11
+ i % j == 0
12
+ end
13
+ end
14
+ }
@@ -0,0 +1,94 @@
1
+ - Code
2
+ - {a = "Go" a += "od" a}
3
+ - Statement
4
+ - {a = "Good"}
5
+ - {
6
+ a = 0
7
+ while a < 10
8
+ a += 1
9
+ "Good"
10
+ end
11
+ }
12
+ - While
13
+ - {
14
+ a = 0
15
+ until a > 10
16
+ a += 1
17
+ "Good"
18
+ end
19
+ }
20
+ - {
21
+ a = 0
22
+ while
23
+ if a > 10
24
+ false
25
+ else
26
+ true
27
+ end
28
+ a += 1
29
+ "Good"
30
+ end
31
+ }
32
+ - If
33
+ - {if true "Good" end}
34
+ - {if true if false "Bad" else "Good" end}
35
+ - IfModifier
36
+ - {"Good" if true}
37
+ - OrKeyword
38
+ - {false or "Good"}
39
+ - NotKeyword
40
+ - {not false and "Good"}
41
+ - Defined
42
+ - {a = 1 "Good" if defined?(a)}
43
+ - Equal
44
+ - {a = "Good"}
45
+ - Rescue
46
+ - {0 > "String" rescue "Good"}
47
+ - Ternary
48
+ - {nothing ? "Bad" : "Good"}
49
+ - Range
50
+ - {("Good".."Bad").first}
51
+ - OrOperator
52
+ - {false || "Good"}
53
+ - AndOperator
54
+ - {"Bad" && "Good"}
55
+ - Equality
56
+ - {1 == 1 ? "Good"}
57
+ - GreaterThan
58
+ - {2 > 1 ? "Good"}
59
+ - BitwiseOr
60
+ - {2 | 1 == 3 ? "Good"}
61
+ - BitwiseAnd
62
+ - {2 & 1 == 0 ? "Good"}
63
+ - Shift
64
+ - {1 << 1 == 2 ? "Good"}
65
+ - Addition
66
+ - {1 + 1 == 2 ? "Good"}
67
+ - Multiplication
68
+ - {1 * 2 == 2 ? "Good"}
69
+ - UnaryMinus
70
+ - {-1 == 1 - 2 ? "Good"}
71
+ - Power
72
+ - {2 ** 2 == 4 ? "Good"}
73
+ - Negation
74
+ - {!false ? "Good"}
75
+ - Function
76
+ - {good = () => { "Good" } good}
77
+ - Call
78
+ - {["Good"].first}
79
+ - Dictionnary
80
+ - {{a: "Good"}.a}
81
+ - List
82
+ - {["Good", "Bad"].first}
83
+ - String
84
+ - {"Good"}
85
+ - Number
86
+ - {1 == 1 ? "Good"}
87
+ - Boolean
88
+ - {true ? "Good"}
89
+ - Nothing
90
+ - {nothing ? "Bad" : "Good"}
91
+ - Group
92
+ - {(a = 0 a += 1 a) == 1 ? "Good"}
93
+ - Name
94
+ - {a = "Good" a}
data/lib/code/error.rb ADDED
@@ -0,0 +1,15 @@
1
+ class Code
2
+ class Error < StandardError
3
+ class TypeError < ::Code::Error
4
+ end
5
+
6
+ class Undefined < ::Code::Error
7
+ end
8
+
9
+ class UndefinedVariable < ::Code::Error
10
+ end
11
+
12
+ class ArgumentError < ::Code::Error
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ class Code
2
+ class Node
3
+ class Base10Decimal < Node
4
+ def initialize(number)
5
+ @sign = number[:sign]
6
+ @whole = number.fetch(:whole)
7
+ @decimal = number.fetch(:decimal)
8
+
9
+ if number.key?(:exponent)
10
+ @exponent = ::Code::Node::Base10Number.new(number[:exponent])
11
+ end
12
+ end
13
+
14
+ def evaluate(**args)
15
+ if @exponent
16
+ exponent = @exponent.evaluate(**args)
17
+
18
+ ::Code::Object::Decimal.new(
19
+ "#{sign}#{whole}.#{decimal}",
20
+ exponent: exponent,
21
+ )
22
+ else
23
+ ::Code::Object::Decimal.new("#{sign}#{whole}.#{decimal}")
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :sign, :whole, :decimal
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,32 @@
1
+ class Code
2
+ class Node
3
+ class Base10Integer < Node
4
+ def initialize(number)
5
+ @sign = number[:sign]
6
+ @whole = number.fetch(:whole)
7
+
8
+ if number.key?(:exponent)
9
+ @exponent = ::Code::Node::Base10Number.new(number[:exponent])
10
+ end
11
+ end
12
+
13
+ def evaluate(**args)
14
+ if @exponent
15
+ exponent = @exponent.evaluate(**args)
16
+
17
+ if exponent.is_a?(::Code::Object::Decimal)
18
+ ::Code::Object::Decimal.new("#{sign}#{whole}", exponent: exponent)
19
+ else
20
+ ::Code::Object::Integer.new("#{sign}#{whole}", exponent: exponent)
21
+ end
22
+ else
23
+ ::Code::Object::Integer.new("#{sign}#{whole}")
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :sign, :whole
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Node
3
+ class Base10Number < Node
4
+ def initialize(number)
5
+ if number.key?(:integer)
6
+ @number = ::Code::Node::Base10Integer.new(number[:integer])
7
+ elsif number.key?(:decimal)
8
+ @number = ::Code::Node::Base10Decimal.new(number[:decimal])
9
+ else
10
+ raise NotImplementedErorr.new(number.inspect)
11
+ end
12
+ end
13
+
14
+ def evaluate(**args)
15
+ @number.evaluate(**args)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Node
3
+ class Base16Number < Node
4
+ def initialize(number)
5
+ @number = number
6
+ end
7
+
8
+ def evaluate(**args)
9
+ ::Code::Object::Integer.new(number)
10
+ end
11
+
12
+ private
13
+
14
+ def number
15
+ @number.to_s.to_i(16)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Node
3
+ class Base2Number < Node
4
+ def initialize(number)
5
+ @number = number
6
+ end
7
+
8
+ def evaluate(**args)
9
+ ::Code::Object::Integer.new(number)
10
+ end
11
+
12
+ private
13
+
14
+ def number
15
+ @number.to_s.to_i(2)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class Code
2
+ class Node
3
+ class Base8Number < Node
4
+ def initialize(number)
5
+ @number = number
6
+ end
7
+
8
+ def evaluate(**args)
9
+ ::Code::Object::Integer.new(number)
10
+ end
11
+
12
+ private
13
+
14
+ def number
15
+ @number.to_s.to_i(8)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ class Code
2
+ class Node
3
+ class Block < Node
4
+ def initialize(block)
5
+ @body = ::Code::Node::Code.new(block.fetch(:body))
6
+ @arguments = block.fetch(:arguments, [])
7
+ @arguments.map! do |argument|
8
+ ::Code::Node::FunctionArgument.new(argument)
9
+ end
10
+ end
11
+
12
+ def evaluate(**args)
13
+ ::Code::Object::Function.new(arguments: @arguments, body: @body)
14
+ end
15
+ end
16
+ end
17
+ end