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.
- checksums.yaml +4 -4
- data/Gemfile +1 -3
- data/Gemfile.lock +11 -21
- data/README.md +2 -102
- data/bin/code +34 -16
- data/code-ruby.gemspec +5 -3
- data/lib/code/error.rb +16 -5
- data/lib/code/node/base_10.rb +4 -2
- data/lib/code/node/base_16.rb +3 -1
- data/lib/code/node/base_2.rb +3 -1
- data/lib/code/node/base_8.rb +3 -1
- data/lib/code/node/boolean.rb +4 -2
- data/lib/code/node/call.rb +35 -12
- data/lib/code/node/call_argument.rb +16 -5
- data/lib/code/node/code.rb +5 -4
- data/lib/code/node/decimal.rb +2 -0
- data/lib/code/node/{dictionnary.rb → dictionary.rb} +14 -5
- data/lib/code/node/function.rb +7 -4
- data/lib/code/node/function_parameter.rb +3 -1
- data/lib/code/node/if.rb +5 -3
- data/lib/code/node/left_operation.rb +63 -0
- data/lib/code/node/list.rb +2 -0
- data/lib/code/node/negation.rb +2 -0
- data/lib/code/node/not.rb +2 -0
- data/lib/code/node/nothing.rb +3 -1
- data/lib/code/node/number.rb +2 -0
- data/lib/code/node/right_operation.rb +70 -0
- data/lib/code/node/splat.rb +2 -0
- data/lib/code/node/square_bracket.rb +37 -0
- data/lib/code/node/statement.rb +13 -5
- data/lib/code/node/string.rb +3 -1
- data/lib/code/node/ternary.rb +5 -3
- data/lib/code/node/unary_minus.rb +2 -0
- data/lib/code/node/while.rb +6 -4
- data/lib/code/node.rb +11 -5
- data/lib/code/object/argument.rb +13 -7
- data/lib/code/object/boolean.rb +43 -5
- data/lib/code/object/class.rb +17 -0
- data/lib/code/object/context.rb +36 -0
- data/lib/code/object/decimal.rb +252 -100
- data/lib/code/object/dictionary.rb +641 -0
- data/lib/code/object/function.rb +54 -27
- data/lib/code/object/global.rb +65 -19
- data/lib/code/object/identifier_list.rb +47 -0
- data/lib/code/object/integer.rb +320 -137
- data/lib/code/object/list.rb +140 -138
- data/lib/code/object/nothing.rb +10 -4
- data/lib/code/object/number.rb +6 -1
- data/lib/code/object/range.rb +85 -88
- data/lib/code/object/ruby_function.rb +11 -6
- data/lib/code/object/string.rb +51 -48
- data/lib/code/object.rb +117 -139
- data/lib/code/parser/addition.rb +4 -2
- data/lib/code/parser/and_operator.rb +4 -2
- data/lib/code/parser/bitwise_and.rb +4 -2
- data/lib/code/parser/bitwise_or.rb +4 -2
- data/lib/code/parser/boolean.rb +3 -1
- data/lib/code/parser/call.rb +17 -11
- data/lib/code/parser/chained_call.rb +10 -22
- data/lib/code/parser/class.rb +9 -6
- data/lib/code/parser/code.rb +6 -4
- data/lib/code/parser/{dictionnary.rb → dictionary.rb} +16 -13
- data/lib/code/parser/equal.rb +9 -36
- data/lib/code/parser/equality.rb +4 -2
- data/lib/code/parser/function.rb +24 -9
- data/lib/code/parser/greater.rb +6 -3
- data/lib/code/parser/group.rb +4 -2
- data/lib/code/parser/if.rb +6 -4
- data/lib/code/parser/if_modifier.rb +5 -25
- data/lib/code/parser/left_operation.rb +40 -0
- data/lib/code/parser/list.rb +6 -5
- data/lib/code/parser/multiplication.rb +4 -2
- data/lib/code/parser/name.rb +19 -4
- data/lib/code/parser/negation.rb +4 -2
- data/lib/code/parser/not_keyword.rb +5 -3
- data/lib/code/parser/nothing.rb +3 -10
- data/lib/code/parser/number.rb +4 -2
- data/lib/code/parser/or_keyword.rb +4 -2
- data/lib/code/parser/or_operator.rb +4 -2
- data/lib/code/parser/power.rb +4 -28
- data/lib/code/parser/range.rb +4 -2
- data/lib/code/parser/rescue.rb +6 -26
- data/lib/code/parser/right_operation.rb +40 -0
- data/lib/code/parser/shift.rb +4 -2
- data/lib/code/parser/splat.rb +5 -3
- data/lib/code/parser/square_bracket.rb +48 -0
- data/lib/code/parser/statement.rb +3 -1
- data/lib/code/parser/string.rb +12 -10
- data/lib/code/parser/ternary.rb +10 -11
- data/lib/code/parser/unary_minus.rb +5 -3
- data/lib/code/parser/while.rb +5 -3
- data/lib/code/parser/whitespace.rb +2 -0
- data/lib/code/parser.rb +10 -3
- data/lib/code/ruby.rb +4 -2
- data/lib/code/type/hash.rb +38 -0
- data/lib/code/type/maybe.rb +29 -0
- data/lib/code/type/or.rb +38 -0
- data/lib/code/type/repeat.rb +38 -0
- data/lib/code/type/sig.rb +130 -0
- data/lib/code/type.rb +25 -0
- data/lib/code/version.rb +3 -0
- data/lib/code-ruby.rb +1 -2
- data/lib/code.rb +15 -16
- data/spec/code/node/call_spec.rb +39 -0
- data/spec/code/object/boolean_spec.rb +18 -0
- data/spec/code/object/decimal_spec.rb +51 -0
- data/spec/code/object/dictionary_spec.rb +98 -0
- data/spec/code/object/function_spec.rb +42 -0
- data/spec/code/object/integer_spec.rb +43 -0
- data/spec/code/object/nothing_spec.rb +14 -0
- data/spec/code/object/range_spec.rb +23 -0
- data/spec/code/parser/boolean_spec.rb +5 -10
- data/spec/code/parser/chained_call.rb +4 -5
- data/spec/code/parser/{dictionnary_spec.rb → dictionary_spec.rb} +5 -6
- data/spec/code/parser/function_spec.rb +4 -5
- data/spec/code/parser/group_spec.rb +5 -12
- data/spec/code/parser/if_modifier_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +4 -5
- data/spec/code/parser/number_spec.rb +4 -5
- data/spec/code/parser/string_spec.rb +4 -5
- data/spec/code/parser_spec.rb +22 -16
- data/spec/code/type_spec.rb +21 -0
- data/spec/code_spec.rb +171 -0
- data/spec/spec_helper.rb +1 -6
- metadata +63 -136
- data/.cherry.js +0 -21
- data/.editorconfig +0 -9
- data/.github/workflows/rspec.yml +0 -14
- data/.gitignore +0 -2
- data/.prettierrc +0 -3
- data/.tool-versions +0 -1
- data/CHANGELOG.md +0 -55
- data/LICENSE +0 -7
- data/TODO +0 -17
- data/bin/format +0 -3
- data/bin/publish +0 -19
- data/bin/template +0 -85
- data/bin/test +0 -17
- data/docs/class.code +0 -9
- data/docs/euler/1.template +0 -10
- data/docs/euler/2.template +0 -16
- data/docs/euler/3.template +0 -16
- data/docs/euler/4.template +0 -10
- data/docs/euler/5.template +0 -13
- data/docs/fibonnaci.template +0 -14
- data/docs/meetup.code +0 -12
- data/docs/precedence.template +0 -36
- data/docs/rain.code +0 -22
- data/docs/slack.code +0 -17
- data/docs/stripe.code +0 -7
- data/docs/twitter.code +0 -9
- data/language-ruby.gemspec +0 -17
- data/lib/code/node/chained_call.rb +0 -23
- data/lib/code/node/equal.rb +0 -34
- data/lib/code/node/if_modifier.rb +0 -47
- data/lib/code/node/operation.rb +0 -38
- data/lib/code/node/power.rb +0 -20
- data/lib/code/node/rescue.rb +0 -17
- data/lib/code/object/dictionnary.rb +0 -96
- data/lib/code/parser/equality_lower.rb +0 -9
- data/lib/code/parser/operation.rb +0 -35
- data/lib/language/atom.rb +0 -342
- data/lib/language/output.rb +0 -130
- data/lib/language/parser/absent/present.rb +0 -8
- data/lib/language/parser/absent.rb +0 -6
- data/lib/language/parser/end_of_input.rb +0 -6
- data/lib/language/parser/interuption.rb +0 -38
- data/lib/language/parser/not_end_of_input.rb +0 -6
- data/lib/language/parser/str/not_found.rb +0 -16
- data/lib/language/parser/str.rb +0 -6
- data/lib/language/parser.rb +0 -53
- data/lib/language-ruby.rb +0 -10
- data/lib/language.rb +0 -80
- data/lib/template/node/code_part.rb +0 -13
- data/lib/template/node/part.rb +0 -19
- data/lib/template/node/template.rb +0 -15
- data/lib/template/node/text_part.rb +0 -13
- data/lib/template/node.rb +0 -4
- data/lib/template/parser/template.rb +0 -39
- data/lib/template/parser.rb +0 -19
- data/lib/template/version.rb +0 -3
- data/lib/template-ruby.rb +0 -10
- data/lib/template.rb +0 -50
- data/spec/code/addition_spec.rb +0 -13
- data/spec/code/and_operator_spec.rb +0 -13
- data/spec/code/bitwise_and_spec.rb +0 -13
- data/spec/code/bitwise_or_spec.rb +0 -13
- data/spec/code/boolean_spec.rb +0 -13
- data/spec/code/call_spec.rb +0 -21
- data/spec/code/chained_call_spec.rb +0 -16
- data/spec/code/code_spec.rb +0 -29
- data/spec/code/dictionnary_spec.rb +0 -17
- data/spec/code/equal_spec.rb +0 -26
- data/spec/code/equality_spec.rb +0 -13
- data/spec/code/function_spec.rb +0 -18
- data/spec/code/greater_spec.rb +0 -18
- data/spec/code/group_spec.rb +0 -12
- data/spec/code/if_modifier_spec.rb +0 -20
- data/spec/code/if_spec.rb +0 -25
- data/spec/code/list_spec.rb +0 -19
- data/spec/code/multiplication_spec.rb +0 -18
- data/spec/code/negation_spec.rb +0 -20
- data/spec/code/not_keyword_spec.rb +0 -13
- data/spec/code/nothing_spec.rb +0 -17
- data/spec/code/number_spec.rb +0 -22
- data/spec/code/or_keyword_spec.rb +0 -17
- data/spec/code/or_operator_spec.rb +0 -16
- data/spec/code/parser/call_spec.rb +0 -26
- data/spec/code/power_spec.rb +0 -13
- data/spec/code/range_spec.rb +0 -16
- data/spec/code/rescue_spec.rb +0 -13
- data/spec/code/shift_spec.rb +0 -13
- data/spec/code/splat_spec.rb +0 -13
- data/spec/code/string_spec.rb +0 -27
- data/spec/code/ternary_spec.rb +0 -18
- data/spec/code/unary_minus_spec.rb +0 -13
- data/spec/code/while_spec.rb +0 -18
- data/template-ruby.gemspec +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d1e6240535fbaa53a8402bb09fca25f382435c77b631953da7650279c1779c4
|
4
|
+
data.tar.gz: 31ca274073a554129651ad5f6f398ec421bdf34ce20d0953f360b801bb4ba1f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a81a91547923b0bcdca0b971fc0a18a684c439a4bf4821fd4b981afa06d4dbcb7baaf00b4e0e04513a2bc4abbd965b538ab92a7f4cf5739a42d1ffd306c72108
|
7
|
+
data.tar.gz: b4135cc2b58e02bde82fe6fb41de533f5c37e0c8ca13bd2507e8c559fb2ebd71b796ab36059c5db0f0a51ca73091c75581136c38a5558e89e0f3ef8341f7a436
|
data/Gemfile
CHANGED
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.
|
12
|
-
|
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
|
-
|
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.
|
18
|
+
rspec-core (3.12.2)
|
26
19
|
rspec-support (~> 3.12.0)
|
27
|
-
rspec-expectations (3.12.
|
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.
|
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.
|
34
|
-
ruby-prof (1.
|
35
|
-
zeitwerk (2.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.
|
39
|
+
2.4.22
|
data/README.md
CHANGED
@@ -1,103 +1,3 @@
|
|
1
|
-
#
|
1
|
+
# code-ruby
|
2
2
|
|
3
|
-
|
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/
|
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:
|
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.
|
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.
|
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
|
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
|
-
|
75
|
-
|
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/
|
1
|
+
require_relative "lib/code/version"
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "code-ruby"
|
5
|
-
s.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/
|
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
|
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
|
17
|
+
class UndefinedVariable < Error
|
7
18
|
end
|
8
19
|
|
9
|
-
class
|
20
|
+
class ArgumentError < Error
|
10
21
|
end
|
11
22
|
|
12
|
-
class
|
23
|
+
class IncompatibleContext < Error
|
13
24
|
end
|
14
25
|
|
15
|
-
class
|
26
|
+
class KeyNotFound < Error
|
16
27
|
end
|
17
28
|
end
|
18
29
|
end
|
data/lib/code/node/base_10.rb
CHANGED
@@ -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:
|
21
|
+
::Code::Object::Integer.new(@whole.to_i, exponent:)
|
20
22
|
else
|
21
|
-
::Code::Object::Decimal.new(@whole, exponent:
|
23
|
+
::Code::Object::Decimal.new(@whole, exponent:)
|
22
24
|
end
|
23
25
|
else
|
24
26
|
::Code::Object::Integer.new(@whole.to_i)
|
data/lib/code/node/base_16.rb
CHANGED
data/lib/code/node/base_2.rb
CHANGED
data/lib/code/node/base_8.rb
CHANGED
data/lib/code/node/boolean.rb
CHANGED
@@ -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(**
|
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
|
19
|
+
raise NotImplementedError, @boolean
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
data/lib/code/node/call.rb
CHANGED
@@ -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|
|
11
|
+
.map { |parameter| FunctionParameter.new(parameter) }
|
10
12
|
|
11
|
-
@body =
|
13
|
+
@body = Code.new(parsed.delete(:body))
|
12
14
|
|
13
15
|
super(parsed)
|
14
16
|
end
|
15
17
|
|
16
|
-
def evaluate(**
|
17
|
-
|
18
|
-
|
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|
|
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 =
|
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 =
|
60
|
+
name = Object::String.new(@name)
|
61
|
+
|
62
|
+
args.fetch(:object).call(operator: name, arguments:, **args)
|
63
|
+
end
|
42
64
|
|
43
|
-
|
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
|
-
|
12
|
-
@value.evaluate(**args),
|
13
|
-
name: ::Code::Object::String.new(@name)
|
14
|
-
)
|
13
|
+
Object::Argument.new(@value.evaluate(**args), name:)
|
15
14
|
else
|
16
|
-
|
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
|
data/lib/code/node/code.rb
CHANGED
@@ -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|
|
7
|
+
@statements = parsed.map { |statement| Statement.new(statement) }
|
6
8
|
end
|
7
9
|
|
8
10
|
def evaluate(**args)
|
9
|
-
last =
|
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
|
data/lib/code/node/decimal.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Code
|
2
4
|
class Node
|
3
|
-
class
|
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
|
-
|
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::
|
32
|
+
parsed.map { |key_value| Node::Dictionary::KeyValue.new(key_value) }
|
24
33
|
end
|
25
34
|
|
26
35
|
def evaluate(**args)
|
27
|
-
::Code::Object::
|
36
|
+
::Code::Object::Dictionary.new(
|
28
37
|
@key_values.map { |key_value| key_value.evaluate(**args) }.to_h
|
29
38
|
)
|
30
39
|
end
|
data/lib/code/node/function.rb
CHANGED
@@ -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
|
-
|
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(**
|
18
|
+
def evaluate(**_args)
|
16
19
|
::Code::Object::Function.new(parameters: @parameters, body: @body)
|
17
20
|
end
|
18
21
|
end
|
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
|
-
|
17
|
-
|
18
|
-
|
18
|
+
return unless parsed.key?(:statement)
|
19
|
+
|
20
|
+
@statement = Node::Statement.new(parsed.delete(:statement))
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|