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
data/LICENSE
DELETED
@@ -1,7 +0,0 @@
|
|
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/TODO
DELETED
@@ -1,17 +0,0 @@
|
|
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/format
DELETED
data/bin/publish
DELETED
data/bin/template
DELETED
@@ -1,85 +0,0 @@
|
|
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
DELETED
data/docs/class.code
DELETED
data/docs/euler/1.template
DELETED
data/docs/euler/2.template
DELETED
data/docs/euler/3.template
DELETED
data/docs/euler/4.template
DELETED
data/docs/euler/5.template
DELETED
data/docs/fibonnaci.template
DELETED
data/docs/meetup.code
DELETED
@@ -1,12 +0,0 @@
|
|
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
|
data/docs/precedence.template
DELETED
@@ -1,36 +0,0 @@
|
|
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
DELETED
@@ -1,22 +0,0 @@
|
|
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
DELETED
@@ -1,17 +0,0 @@
|
|
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
|
-
)
|
data/docs/stripe.code
DELETED
data/docs/twitter.code
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
twitter_username = fetch(:twitter_username) { "dorianmariefr" }
|
2
|
-
|
3
|
-
Twitter
|
4
|
-
.search("to:{twitter_username}", type: :recent)
|
5
|
-
.each do |tweet|
|
6
|
-
next if Storage.exists?(tweet.id)
|
7
|
-
Storage.create!(tweet.id)
|
8
|
-
Sms.send("New mention: {tweet.user.screen_name}: {tweet.text}")
|
9
|
-
end
|
data/language-ruby.gemspec
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
require_relative "lib/template/version"
|
2
|
-
|
3
|
-
Gem::Specification.new do |s|
|
4
|
-
s.name = "language-ruby"
|
5
|
-
s.version = ::Template::Version
|
6
|
-
s.summary = "A Parsing Expression Grammar (PEG) for making parsers"
|
7
|
-
s.description = s.summary
|
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
|
@@ -1,23 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class ChainedCall < Node
|
4
|
-
def initialize(parsed)
|
5
|
-
@first = Node::Statement.new(parsed.delete(:first))
|
6
|
-
@others =
|
7
|
-
parsed
|
8
|
-
.delete(:others)
|
9
|
-
.map { |operator| Node::Statement.new(operator) }
|
10
|
-
|
11
|
-
super(parsed)
|
12
|
-
end
|
13
|
-
|
14
|
-
def evaluate(**args)
|
15
|
-
first = @first.evaluate(**args)
|
16
|
-
|
17
|
-
@others.reduce(first) do |acc, element|
|
18
|
-
element.evaluate(**args.merge(object: acc))
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/lib/code/node/equal.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class Equal < Node
|
4
|
-
def initialize(parsed)
|
5
|
-
@operator = parsed.delete(:operator)
|
6
|
-
@left = parsed.delete(:left)
|
7
|
-
@right = Node::Statement.new(parsed.delete(:right))
|
8
|
-
super(parsed)
|
9
|
-
end
|
10
|
-
|
11
|
-
def evaluate(**args)
|
12
|
-
right = @right.evaluate(**args)
|
13
|
-
left = ::Code::Object::String.new(@left)
|
14
|
-
context = args.fetch(:context)
|
15
|
-
|
16
|
-
if @operator == ""
|
17
|
-
context[left] = right
|
18
|
-
else
|
19
|
-
if context[left].nil?
|
20
|
-
raise ::Code::Error::Undefined.new("#{left} is not defined")
|
21
|
-
end
|
22
|
-
|
23
|
-
context[left] = context.fetch(left).call(
|
24
|
-
operator: @operator,
|
25
|
-
arguments: [::Code::Object::Argument.new(right)],
|
26
|
-
**args
|
27
|
-
)
|
28
|
-
end
|
29
|
-
|
30
|
-
args[:context][left]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class IfModifier < Node
|
4
|
-
IF_KEYWORD = "if"
|
5
|
-
UNLESS_KEYWORD = "unless"
|
6
|
-
WHILE_KEYWORD = "while"
|
7
|
-
UNTIL_KEYWORD = "until"
|
8
|
-
|
9
|
-
def initialize(parsed)
|
10
|
-
@operator = parsed.delete(:operator)
|
11
|
-
@left = Node::Statement.new(parsed.delete(:left))
|
12
|
-
@right = Node::Statement.new(parsed.delete(:right))
|
13
|
-
super(parsed)
|
14
|
-
end
|
15
|
-
|
16
|
-
def evaluate(**args)
|
17
|
-
if @operator == IF_KEYWORD
|
18
|
-
if @right.evaluate(**args).truthy?
|
19
|
-
@left.evaluate(**args)
|
20
|
-
else
|
21
|
-
::Code::Object::Nothing.new
|
22
|
-
end
|
23
|
-
elsif @operator == UNLESS_KEYWORD
|
24
|
-
if @right.evaluate(**args).falsy?
|
25
|
-
@left.evaluate(**args)
|
26
|
-
else
|
27
|
-
::Code::Object::Nothing.new
|
28
|
-
end
|
29
|
-
elsif @operator == WHILE_KEYWORD
|
30
|
-
last = ::Code::Object::Nothing.new
|
31
|
-
|
32
|
-
last = @left.evaluate(**args) while @right.evaluate(**args).truthy?
|
33
|
-
|
34
|
-
last
|
35
|
-
elsif @operator == UNTIL_KEYWORD
|
36
|
-
last = ::Code::Object::Nothing.new
|
37
|
-
|
38
|
-
last = @left.evaluate(**args) while @right.evaluate(**args).falsy?
|
39
|
-
|
40
|
-
last
|
41
|
-
else
|
42
|
-
raise NotImplementedError.new(@operator)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
data/lib/code/node/operation.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class Operation < Node
|
4
|
-
class Operator < Node
|
5
|
-
attr_reader :operator, :statement
|
6
|
-
|
7
|
-
def initialize(parsed)
|
8
|
-
@operator = parsed.delete(:operator)
|
9
|
-
@statement = Node::Statement.new(parsed.delete(:statement))
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(parsed)
|
14
|
-
@first = Node::Statement.new(parsed.delete(:first))
|
15
|
-
@others =
|
16
|
-
parsed
|
17
|
-
.delete(:others)
|
18
|
-
.map { |operator| Node::Operation::Operator.new(operator) }
|
19
|
-
|
20
|
-
super(parsed)
|
21
|
-
end
|
22
|
-
|
23
|
-
def evaluate(**args)
|
24
|
-
first = @first.evaluate(**args)
|
25
|
-
|
26
|
-
@others.reduce(first) do |left, right|
|
27
|
-
statement = right.statement.evaluate(**args)
|
28
|
-
|
29
|
-
left.call(
|
30
|
-
operator: right.operator,
|
31
|
-
arguments: [::Code::Object::Argument.new(statement)],
|
32
|
-
**args
|
33
|
-
)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
data/lib/code/node/power.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class Power < Node
|
4
|
-
def initialize(parsed)
|
5
|
-
@operator = parsed.delete(:operator)
|
6
|
-
@left = Node::Statement.new(parsed.delete(:left))
|
7
|
-
@right = Node::Statement.new(parsed.delete(:right))
|
8
|
-
super(parsed)
|
9
|
-
end
|
10
|
-
|
11
|
-
def evaluate(**args)
|
12
|
-
@left.evaluate(**args).call(
|
13
|
-
operator: @operator,
|
14
|
-
arguments: [::Code::Object::Argument.new(@right.evaluate(**args))],
|
15
|
-
**args
|
16
|
-
)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/code/node/rescue.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Node
|
3
|
-
class Rescue < Node
|
4
|
-
def initialize(parsed)
|
5
|
-
@left = Node::Statement.new(parsed.delete(:left))
|
6
|
-
@right = Node::Statement.new(parsed.delete(:right))
|
7
|
-
super(parsed)
|
8
|
-
end
|
9
|
-
|
10
|
-
def evaluate(**args)
|
11
|
-
@left.evaluate(**args)
|
12
|
-
rescue ::Code::Error
|
13
|
-
@right.evaluate(**args)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
class Code
|
2
|
-
class Object
|
3
|
-
class Dictionnary < ::Code::Object
|
4
|
-
attr_reader :raw
|
5
|
-
|
6
|
-
def initialize(raw = {})
|
7
|
-
@raw = raw
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(**args)
|
11
|
-
operator = args.fetch(:operator, nil)
|
12
|
-
arguments = args.fetch(:arguments, [])
|
13
|
-
globals = multi_fetch(args, *::Code::GLOBALS)
|
14
|
-
value = arguments.first&.value
|
15
|
-
|
16
|
-
if operator == "values"
|
17
|
-
sig(arguments)
|
18
|
-
values
|
19
|
-
elsif operator == "keys"
|
20
|
-
sig(arguments)
|
21
|
-
keys
|
22
|
-
elsif operator == "each"
|
23
|
-
sig(arguments) { ::Code::Object::Function }
|
24
|
-
each(value, **globals)
|
25
|
-
elsif key?(operator)
|
26
|
-
result = fetch(operator)
|
27
|
-
|
28
|
-
if result.is_a?(::Code::Object::Function)
|
29
|
-
result.call(**args.merge(operator: nil))
|
30
|
-
else
|
31
|
-
sig(arguments)
|
32
|
-
result
|
33
|
-
end
|
34
|
-
else
|
35
|
-
super
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def merge(other)
|
40
|
-
::Code::Object::Dictionnary.new(raw.merge(other.raw))
|
41
|
-
end
|
42
|
-
|
43
|
-
def fetch(key)
|
44
|
-
raw.fetch(key)
|
45
|
-
end
|
46
|
-
|
47
|
-
def [](key)
|
48
|
-
raw[key]
|
49
|
-
end
|
50
|
-
|
51
|
-
def []=(key, value)
|
52
|
-
raw[key] = value
|
53
|
-
end
|
54
|
-
|
55
|
-
def key?(key)
|
56
|
-
raw.key?(key)
|
57
|
-
end
|
58
|
-
|
59
|
-
def deep_dup
|
60
|
-
::Code::Object::Dictionnary.new(raw.deep_dup)
|
61
|
-
end
|
62
|
-
|
63
|
-
def to_s
|
64
|
-
"{#{raw.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")}}"
|
65
|
-
end
|
66
|
-
|
67
|
-
def inspect
|
68
|
-
to_s
|
69
|
-
end
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
def keys
|
74
|
-
::Code::Object::List.new(raw.keys)
|
75
|
-
end
|
76
|
-
|
77
|
-
def values
|
78
|
-
::Code::Object::List.new(raw.values)
|
79
|
-
end
|
80
|
-
|
81
|
-
def each(argument, **globals)
|
82
|
-
raw.each do |key, value|
|
83
|
-
argument.call(
|
84
|
-
arguments: [
|
85
|
-
::Code::Object::Argument.new(key),
|
86
|
-
::Code::Object::Argument.new(value)
|
87
|
-
],
|
88
|
-
**globals
|
89
|
-
)
|
90
|
-
end
|
91
|
-
|
92
|
-
self
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|