language-ruby 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +9 -0
- data/.github/workflows/rspec.yml +14 -0
- data/.gitignore +2 -0
- data/.prettierrc +3 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +55 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +48 -0
- data/LICENSE +7 -0
- data/README.md +103 -0
- data/TODO +17 -0
- data/bin/code +76 -0
- data/bin/format +3 -0
- data/bin/template +85 -0
- data/bin/test +17 -0
- data/code-ruby.gemspec +17 -0
- data/docs/class.code +9 -0
- data/docs/euler/1.template +10 -0
- data/docs/euler/2.template +16 -0
- data/docs/euler/3.template +16 -0
- data/docs/euler/4.template +10 -0
- data/docs/euler/5.template +13 -0
- data/docs/fibonnaci.template +14 -0
- data/docs/meetup.code +12 -0
- data/docs/precedence.template +36 -0
- data/docs/rain.code +22 -0
- data/docs/slack.code +17 -0
- data/docs/stripe.code +7 -0
- data/docs/twitter.code +9 -0
- data/language-ruby.gemspec +18 -0
- data/lib/code/error.rb +18 -0
- data/lib/code/node/base_10.rb +29 -0
- data/lib/code/node/base_16.rb +13 -0
- data/lib/code/node/base_2.rb +13 -0
- data/lib/code/node/base_8.rb +13 -0
- data/lib/code/node/boolean.rb +22 -0
- data/lib/code/node/call.rb +47 -0
- data/lib/code/node/call_argument.rb +21 -0
- data/lib/code/node/chained_call.rb +23 -0
- data/lib/code/node/code.rb +20 -0
- data/lib/code/node/decimal.rb +26 -0
- data/lib/code/node/dictionnary.rb +33 -0
- data/lib/code/node/equal.rb +34 -0
- data/lib/code/node/function.rb +20 -0
- data/lib/code/node/function_parameter.rb +31 -0
- data/lib/code/node/if.rb +59 -0
- data/lib/code/node/if_modifier.rb +47 -0
- data/lib/code/node/list.rb +16 -0
- data/lib/code/node/negation.rb +15 -0
- data/lib/code/node/not.rb +15 -0
- data/lib/code/node/nothing.rb +12 -0
- data/lib/code/node/number.rb +25 -0
- data/lib/code/node/operation.rb +38 -0
- data/lib/code/node/power.rb +20 -0
- data/lib/code/node/rescue.rb +17 -0
- data/lib/code/node/splat.rb +15 -0
- data/lib/code/node/statement.rb +59 -0
- data/lib/code/node/string.rb +53 -0
- data/lib/code/node/ternary.rb +24 -0
- data/lib/code/node/unary_minus.rb +15 -0
- data/lib/code/node/while.rb +35 -0
- data/lib/code/node.rb +13 -0
- data/lib/code/object/argument.rb +32 -0
- data/lib/code/object/boolean.rb +27 -0
- data/lib/code/object/decimal.rb +162 -0
- data/lib/code/object/dictionnary.rb +96 -0
- data/lib/code/object/function.rb +64 -0
- data/lib/code/object/global.rb +42 -0
- data/lib/code/object/integer.rb +221 -0
- data/lib/code/object/list.rb +200 -0
- data/lib/code/object/nothing.rb +23 -0
- data/lib/code/object/number.rb +6 -0
- data/lib/code/object/range.rb +146 -0
- data/lib/code/object/ruby_function.rb +31 -0
- data/lib/code/object/string.rb +88 -0
- data/lib/code/object.rb +197 -0
- data/lib/code/parser/addition.rb +21 -0
- data/lib/code/parser/and_operator.rb +17 -0
- data/lib/code/parser/bitwise_and.rb +17 -0
- data/lib/code/parser/bitwise_or.rb +21 -0
- data/lib/code/parser/boolean.rb +17 -0
- data/lib/code/parser/call.rb +122 -0
- data/lib/code/parser/chained_call.rb +47 -0
- data/lib/code/parser/class.rb +45 -0
- data/lib/code/parser/code.rb +25 -0
- data/lib/code/parser/dictionnary.rb +67 -0
- data/lib/code/parser/equal.rb +94 -0
- data/lib/code/parser/equality.rb +35 -0
- data/lib/code/parser/equality_lower.rb +9 -0
- data/lib/code/parser/function.rb +85 -0
- data/lib/code/parser/greater.rb +25 -0
- data/lib/code/parser/group.rb +22 -0
- data/lib/code/parser/if.rb +63 -0
- data/lib/code/parser/if_modifier.rb +55 -0
- data/lib/code/parser/list.rb +42 -0
- data/lib/code/parser/multiplication.rb +25 -0
- data/lib/code/parser/name.rb +101 -0
- data/lib/code/parser/negation.rb +30 -0
- data/lib/code/parser/not_keyword.rb +23 -0
- data/lib/code/parser/nothing.rb +22 -0
- data/lib/code/parser/number.rb +154 -0
- data/lib/code/parser/operation.rb +35 -0
- data/lib/code/parser/or_keyword.rb +21 -0
- data/lib/code/parser/or_operator.rb +17 -0
- data/lib/code/parser/power.rb +43 -0
- data/lib/code/parser/range.rb +17 -0
- data/lib/code/parser/rescue.rb +39 -0
- data/lib/code/parser/shift.rb +21 -0
- data/lib/code/parser/splat.rb +31 -0
- data/lib/code/parser/statement.rb +9 -0
- data/lib/code/parser/string.rb +78 -0
- data/lib/code/parser/ternary.rb +46 -0
- data/lib/code/parser/unary_minus.rb +31 -0
- data/lib/code/parser/while.rb +36 -0
- data/lib/code/parser/whitespace.rb +49 -0
- data/lib/code/parser.rb +19 -0
- data/lib/code/ruby.rb +162 -0
- data/lib/code-ruby.rb +10 -0
- data/lib/code.rb +47 -0
- data/lib/language/atom.rb +343 -0
- data/lib/language/output.rb +130 -0
- data/lib/language/parser/absent/present.rb +8 -0
- data/lib/language/parser/absent.rb +6 -0
- data/lib/language/parser/end_of_input.rb +6 -0
- data/lib/language/parser/interuption.rb +38 -0
- data/lib/language/parser/not_end_of_input.rb +6 -0
- data/lib/language/parser/str/not_found.rb +16 -0
- data/lib/language/parser/str.rb +6 -0
- data/lib/language/parser.rb +53 -0
- data/lib/language-ruby.rb +10 -0
- data/lib/language.rb +80 -0
- data/lib/template/node/code_part.rb +13 -0
- data/lib/template/node/part.rb +19 -0
- data/lib/template/node/template.rb +15 -0
- data/lib/template/node/text_part.rb +13 -0
- data/lib/template/node.rb +4 -0
- data/lib/template/parser/template.rb +39 -0
- data/lib/template/parser.rb +19 -0
- data/lib/template/version.rb +3 -0
- data/lib/template-ruby.rb +10 -0
- data/lib/template.rb +50 -0
- data/spec/code/addition_spec.rb +13 -0
- data/spec/code/and_operator_spec.rb +13 -0
- data/spec/code/bitwise_and_spec.rb +13 -0
- data/spec/code/bitwise_or_spec.rb +13 -0
- data/spec/code/boolean_spec.rb +13 -0
- data/spec/code/call_spec.rb +21 -0
- data/spec/code/chained_call_spec.rb +16 -0
- data/spec/code/dictionnary_spec.rb +17 -0
- data/spec/code/equal_spec.rb +26 -0
- data/spec/code/equality_spec.rb +13 -0
- data/spec/code/function_spec.rb +18 -0
- data/spec/code/greater_spec.rb +18 -0
- data/spec/code/group_spec.rb +12 -0
- data/spec/code/if_modifier_spec.rb +20 -0
- data/spec/code/if_spec.rb +25 -0
- data/spec/code/list_spec.rb +17 -0
- data/spec/code/multiplication_spec.rb +18 -0
- data/spec/code/negation_spec.rb +20 -0
- data/spec/code/not_keyword_spec.rb +13 -0
- data/spec/code/nothing_spec.rb +17 -0
- data/spec/code/number_spec.rb +22 -0
- data/spec/code/or_keyword_spec.rb +17 -0
- data/spec/code/or_operator_spec.rb +16 -0
- data/spec/code/parser/boolean_spec.rb +16 -0
- data/spec/code/parser/call_spec.rb +26 -0
- data/spec/code/parser/chained_call.rb +17 -0
- data/spec/code/parser/dictionnary_spec.rb +18 -0
- data/spec/code/parser/function_spec.rb +16 -0
- data/spec/code/parser/group_spec.rb +18 -0
- data/spec/code/parser/list_spec.rb +18 -0
- data/spec/code/parser/number_spec.rb +12 -0
- data/spec/code/parser/string_spec.rb +21 -0
- data/spec/code/parser_spec.rb +23 -0
- data/spec/code/power_spec.rb +13 -0
- data/spec/code/range_spec.rb +16 -0
- data/spec/code/rescue_spec.rb +13 -0
- data/spec/code/shift_spec.rb +13 -0
- data/spec/code/splat_spec.rb +13 -0
- data/spec/code/string_spec.rb +25 -0
- data/spec/code/ternary_spec.rb +18 -0
- data/spec/code/unary_minus_spec.rb +13 -0
- data/spec/code/while_spec.rb +18 -0
- data/spec/spec_helper.rb +6 -0
- data/template-ruby.gemspec +19 -0
- metadata +284 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 182d0456a2c73bdf225a70c643eb2c96feaf24dd0ffb11505d06f92536f39613
|
4
|
+
data.tar.gz: 9b0a7f5d97a9a076b711e7bf8872722d46a2906f98e988f9b661a150a5dcec00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f362c37edd49bbe9e34b5e62d8911d859a0bef58c68adc891db682a74f7b53de6153dccfb1acc163450e376eef0b66a07f11d398cc21e966486954d5d1fb8b2a
|
7
|
+
data.tar.gz: 36e9c2247fa179763e091499d2c4315e45bb302308e7460ac4b9fca3513bfad0ae7bfe4fbf95ea2071295b3d892874bc5b26248a2f19762c3a3d6e66a0f48812
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.prettierrc
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,55 @@
|
|
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.4.0 / 2022-10-21
|
9
|
+
|
10
|
+
- Rewrite of how functions are called internally (no more `public_send`)
|
11
|
+
- Call ruby functions from template/code
|
12
|
+
|
13
|
+
## 0.3.1 / 2022-10-13
|
14
|
+
|
15
|
+
- Fix Zeitwerk auto-loading issue
|
16
|
+
|
17
|
+
## 0.3.0 / 2022-10-10
|
18
|
+
|
19
|
+
- `bin/template` accepts `--timeout` (or `-t`) parameter
|
20
|
+
- Adds `bin/code` with same options as `bin/template`
|
21
|
+
- Prevent loose syntax like `{ a: }`, `[1,,,]` and `()`
|
22
|
+
- Change precedence of defined? (to allow `defined?(name) ? name : nothing`)
|
23
|
+
- Updates parsers to allow `while false end == nothing`
|
24
|
+
- String interpolations like `"1 + 1 = {1 + 1}"`
|
25
|
+
- `context(:name)` to get a function without calling it for instance
|
26
|
+
- `.to_string` on all objects
|
27
|
+
- `1 + "a"` and `"a" + 1.0` for instance now convert to strings
|
28
|
+
- `Dictionnary#each` e.g. `{ a: 1 }.each { |k, v| print(k) }`
|
29
|
+
- Fix context duplication issue that was preventing implementation of recursive
|
30
|
+
functions like Fibonacci
|
31
|
+
|
32
|
+
## 0.2.4 / 2022-08-02
|
33
|
+
|
34
|
+
- Add method `String#*`, e.g. `{"Dorian " \* 2}" -> "Dorian Dorian "
|
35
|
+
- Add executable to gem, e.g. `template --help`
|
36
|
+
|
37
|
+
## 0.2.3 / 2022-08-31
|
38
|
+
|
39
|
+
- Add default timeout for code and template parsing and evaluation
|
40
|
+
|
41
|
+
## 0.2.2 / 2022-08-31
|
42
|
+
|
43
|
+
- Fix parsing error when the template is empty, e.g. ""
|
44
|
+
|
45
|
+
## 0.2.1 / 2022-08-31
|
46
|
+
|
47
|
+
- Fix parsing error on empty code like `Hello {`
|
48
|
+
|
49
|
+
## 0.2.0 / 2022-08-30
|
50
|
+
|
51
|
+
- Programming language capable of solving the first 5 Project Euler problems
|
52
|
+
|
53
|
+
## 0.1.0 / 2022-07-28
|
54
|
+
|
55
|
+
- Initial version with interpolation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
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
|
+
PATH
|
9
|
+
remote: .
|
10
|
+
specs:
|
11
|
+
code-ruby (0.4.0)
|
12
|
+
zeitwerk (~> 2)
|
13
|
+
template-ruby (0.4.0)
|
14
|
+
zeitwerk (~> 2)
|
15
|
+
|
16
|
+
GEM
|
17
|
+
remote: https://rubygems.org/
|
18
|
+
specs:
|
19
|
+
diff-lcs (1.5.0)
|
20
|
+
prettier_print (1.1.0)
|
21
|
+
rspec (3.12.0)
|
22
|
+
rspec-core (~> 3.12.0)
|
23
|
+
rspec-expectations (~> 3.12.0)
|
24
|
+
rspec-mocks (~> 3.12.0)
|
25
|
+
rspec-core (3.12.0)
|
26
|
+
rspec-support (~> 3.12.0)
|
27
|
+
rspec-expectations (3.12.0)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.12.0)
|
30
|
+
rspec-mocks (3.12.0)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.12.0)
|
33
|
+
rspec-support (3.12.0)
|
34
|
+
ruby-prof (1.4.3)
|
35
|
+
zeitwerk (2.6.6)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
arm64-darwin-21
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
code-ruby!
|
42
|
+
rspec
|
43
|
+
ruby-prof
|
44
|
+
syntax_tree!
|
45
|
+
template-ruby!
|
46
|
+
|
47
|
+
BUNDLED WITH
|
48
|
+
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
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
- Class
|
2
|
+
- Function parameters regular splat *args
|
3
|
+
- Function parameters keyword splat **kargs
|
4
|
+
- Function parameters block &block
|
5
|
+
- Block parameters regular splat *args
|
6
|
+
- Block parameters keyword splat **kargs
|
7
|
+
- Block parameters block &block
|
8
|
+
- Dictionnary regular splat { *args }
|
9
|
+
- Dictionnary keyword splat { **kargs }
|
10
|
+
- Dictionnary block { &block }
|
11
|
+
- Dictionnary expand name { user } and { user: }
|
12
|
+
- Rescue with exception name, rescue Code::Undefined
|
13
|
+
- Rescue with exception catched, rescue => e
|
14
|
+
- begin end
|
15
|
+
- begin rescue end
|
16
|
+
- begin rescue ensure end
|
17
|
+
- begin rescue else end
|
data/bin/code
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require_relative "../lib/template-ruby"
|
5
|
+
|
6
|
+
options = { timeout: 0, profile: false }
|
7
|
+
|
8
|
+
OptionParser
|
9
|
+
.new do |opts|
|
10
|
+
opts.banner = "Usage: bin/code [options]"
|
11
|
+
|
12
|
+
opts.on(
|
13
|
+
"-i INPUT",
|
14
|
+
"--input=INPUT",
|
15
|
+
"Input in the code language (String or File)"
|
16
|
+
) do |input|
|
17
|
+
input = File.read(input) if File.exists?(input)
|
18
|
+
|
19
|
+
options[:input] = input
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on(
|
23
|
+
"-c CONTEXT",
|
24
|
+
"--context=CONTEXT",
|
25
|
+
"Context in the code language (String or File)"
|
26
|
+
) do |context|
|
27
|
+
context = File.read(context) if File.exists?(context)
|
28
|
+
|
29
|
+
options[:context] = context
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on("-p", "--parse", "Get parser results for input") do |parse|
|
33
|
+
options[:parse] = parse
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on(
|
37
|
+
"-t TIMEOUT",
|
38
|
+
"--timeout=TIMEOUT",
|
39
|
+
"Set timeout in seconds"
|
40
|
+
) { |timeout| options[:timeout] = timeout.to_f }
|
41
|
+
|
42
|
+
opts.on(
|
43
|
+
"--profile",
|
44
|
+
"Profile Ruby code"
|
45
|
+
) do |timeout|
|
46
|
+
require "ruby-prof"
|
47
|
+
options[:profile] = true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
.parse!
|
51
|
+
|
52
|
+
input = options.fetch(:input, "")
|
53
|
+
context = options.fetch(:context, "")
|
54
|
+
|
55
|
+
if options[:profile]
|
56
|
+
RubyProf.start
|
57
|
+
end
|
58
|
+
|
59
|
+
if options[:parse]
|
60
|
+
pp ::Code::Parser.parse(input).to_raw
|
61
|
+
else
|
62
|
+
print(
|
63
|
+
Code.evaluate(
|
64
|
+
input,
|
65
|
+
context,
|
66
|
+
io: $stdout,
|
67
|
+
timeout: options[:timeout]
|
68
|
+
).to_s
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
if options[:profile]
|
73
|
+
result = RubyProf.stop
|
74
|
+
printer = RubyProf::FlatPrinter.new(result)
|
75
|
+
printer.print($stdout)
|
76
|
+
end
|
data/bin/format
ADDED
data/bin/template
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require_relative "../lib/template-ruby"
|
5
|
+
|
6
|
+
options = { timeout: 0, profile: false, profiler: "text" }
|
7
|
+
|
8
|
+
OptionParser
|
9
|
+
.new do |opts|
|
10
|
+
opts.banner = "Usage: template [options]"
|
11
|
+
|
12
|
+
opts.on(
|
13
|
+
"-i INPUT",
|
14
|
+
"--input=INPUT",
|
15
|
+
"Input in the template language (String or File)"
|
16
|
+
) do |input|
|
17
|
+
input = File.read(input) if File.exists?(input)
|
18
|
+
|
19
|
+
options[:input] = input
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on(
|
23
|
+
"-c CONTEXT",
|
24
|
+
"--context=CONTEXT",
|
25
|
+
"Context in the code language (String or File)"
|
26
|
+
) do |context|
|
27
|
+
context = File.read(context) if File.exists?(context)
|
28
|
+
|
29
|
+
options[:context] = context
|
30
|
+
end
|
31
|
+
|
32
|
+
opts.on("-p", "--parse", "Get parser results for input") do |parse|
|
33
|
+
options[:parse] = parse
|
34
|
+
end
|
35
|
+
|
36
|
+
opts.on(
|
37
|
+
"-t TIMEOUT",
|
38
|
+
"--timeout=TIMEOUT",
|
39
|
+
"Set timeout in seconds"
|
40
|
+
) { |timeout| options[:timeout] = timeout.to_f }
|
41
|
+
|
42
|
+
opts.on(
|
43
|
+
"--profile",
|
44
|
+
"Profile Ruby code"
|
45
|
+
) do |timeout|
|
46
|
+
require "ruby-prof"
|
47
|
+
options[:profile] = true
|
48
|
+
end
|
49
|
+
|
50
|
+
opts.on(
|
51
|
+
"--profiler TYPE",
|
52
|
+
"Profiler output type (text (default) or html)"
|
53
|
+
) do |profiler|
|
54
|
+
require "ruby-prof"
|
55
|
+
options[:profile] = true
|
56
|
+
options[:profiler] = profiler
|
57
|
+
end
|
58
|
+
end
|
59
|
+
.parse!
|
60
|
+
|
61
|
+
input = options.fetch(:input, "")
|
62
|
+
context = options.fetch(:context, "")
|
63
|
+
|
64
|
+
if options[:profile]
|
65
|
+
RubyProf.start
|
66
|
+
end
|
67
|
+
|
68
|
+
if options[:parse]
|
69
|
+
pp ::Template::Parser.parse(input).to_raw
|
70
|
+
else
|
71
|
+
Template.render(input, context, io: $stdout, timeout: options[:timeout])
|
72
|
+
end
|
73
|
+
|
74
|
+
if options[:profile]
|
75
|
+
result = RubyProf.stop
|
76
|
+
if options[:profiler] == "text"
|
77
|
+
printer = RubyProf::FlatPrinter.new(result)
|
78
|
+
printer.print($stdout)
|
79
|
+
elsif options[:profiler] == "html"
|
80
|
+
printer = RubyProf::GraphHtmlPrinter.new(result)
|
81
|
+
printer.print($stdout)
|
82
|
+
else
|
83
|
+
abort "#{options[:profiler]} not recognized"
|
84
|
+
end
|
85
|
+
end
|
data/bin/test
ADDED
data/code-ruby.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
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 "zeitwerk", "~> 2"
|
17
|
+
end
|
data/docs/class.code
ADDED
data/docs/meetup.code
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
url = fetch(:url) { "https://www.meetup.com/parisrb" }
|
2
|
+
response = HTTP.get(url)
|
3
|
+
times = response.css(".groupHome-eventsList-upcomingEvents time")
|
4
|
+
times = times.map(&:datetime).map { |time| Time.from_epoch(time.to_integer) }
|
5
|
+
|
6
|
+
times.each do |time|
|
7
|
+
if (1.day.from_now...(1.day.from_now + 1.hour)).include?(time)
|
8
|
+
Sms.send("Event in one day {url}")
|
9
|
+
elsif (2.hours.from_now...(1.hour.from_now)).include(time)
|
10
|
+
Sms.send("Event in two hours {url}")
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
- Code
|
2
|
+
- Statement
|
3
|
+
- Equality
|
4
|
+
- Splat
|
5
|
+
- Class
|
6
|
+
- While
|
7
|
+
- If
|
8
|
+
- IfModifier
|
9
|
+
- OrKeyword
|
10
|
+
- NotKeyword
|
11
|
+
- Equal
|
12
|
+
- Rescue
|
13
|
+
- Ternary
|
14
|
+
- Range
|
15
|
+
- OrOperator
|
16
|
+
- AndOperator
|
17
|
+
- EqualityLower
|
18
|
+
- Greater
|
19
|
+
- BitwiseOr
|
20
|
+
- BitwiseAnd
|
21
|
+
- Shift
|
22
|
+
- Addition
|
23
|
+
- Multiplication
|
24
|
+
- UnaryMinus
|
25
|
+
- Power
|
26
|
+
- Negation
|
27
|
+
- Function
|
28
|
+
- ChainedCall
|
29
|
+
- Dictionnary
|
30
|
+
- List
|
31
|
+
- String
|
32
|
+
- Number
|
33
|
+
- Boolean
|
34
|
+
- Nothing
|
35
|
+
- Group
|
36
|
+
- Call
|
data/docs/rain.code
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
paris = { latitude: 48.8566, longitude: 2.4022 }
|
2
|
+
latitude = fetch(:latitude) { paris.latitude }
|
3
|
+
longitude = fetch(:longitude) { paris.longitude }
|
4
|
+
duration = fetch(:duration) { 1.hour }
|
5
|
+
hour = duration.from_now.hour
|
6
|
+
|
7
|
+
response =
|
8
|
+
HTTP.get(
|
9
|
+
"https://api.open-meteo.com/v1/forecast",
|
10
|
+
parameters: {
|
11
|
+
latitude: latitude,
|
12
|
+
longitude: longitude,
|
13
|
+
hourly: :precipitation
|
14
|
+
}
|
15
|
+
)
|
16
|
+
|
17
|
+
precipitation = response.hourly.precipitation[hour]
|
18
|
+
precipitation = (precipitation * 100).to_integer
|
19
|
+
|
20
|
+
if precipitation > 0
|
21
|
+
Sms.send("There will be {precipitation}% of precipitation in the next hour")
|
22
|
+
end
|
data/docs/slack.code
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
Slack.send("Who is leading the syncs?", channel: "#team-template")
|
2
|
+
|
3
|
+
Twitter.send("What do you want to do this week?")
|
4
|
+
|
5
|
+
Discord.send("Who will be the game master this week?")
|
6
|
+
|
7
|
+
Email.send(
|
8
|
+
subject: "What did you do last week?",
|
9
|
+
from: "dorian@thoughtbot.com",
|
10
|
+
reply_to: "rob@thoughtbot.com"
|
11
|
+
)
|
12
|
+
|
13
|
+
Email.send(
|
14
|
+
subject: "What do you want to do this week?",
|
15
|
+
from: "dorian@thoughtbot.com",
|
16
|
+
reply_to: "rob@thoughtbot.com"
|
17
|
+
)
|