language-ruby 0.5.6 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -4
  3. data/language-ruby.gemspec +3 -3
  4. data/lib/language/atom/any.rb +8 -0
  5. data/lib/language/atom/repeat.rb +15 -0
  6. data/lib/language/atom.rb +25 -42
  7. data/lib/language/output.rb +13 -11
  8. data/lib/language/parser/absent/present.rb +2 -0
  9. data/lib/language/parser/absent.rb +2 -0
  10. data/lib/language/parser/end_of_input.rb +2 -0
  11. data/lib/language/parser/interuption.rb +2 -0
  12. data/lib/language/parser/not_end_of_input.rb +2 -0
  13. data/lib/language/parser/str/not_found.rb +2 -0
  14. data/lib/language/parser/str.rb +2 -0
  15. data/lib/language/parser.rb +10 -10
  16. data/lib/language/version.rb +3 -0
  17. data/lib/language-ruby.rb +0 -5
  18. data/lib/language.rb +9 -7
  19. data/spec/spec_helper.rb +1 -6
  20. metadata +7 -221
  21. data/.cherry.js +0 -21
  22. data/.editorconfig +0 -9
  23. data/.github/workflows/rspec.yml +0 -14
  24. data/.gitignore +0 -2
  25. data/.prettierrc +0 -3
  26. data/.rspec +0 -1
  27. data/.tool-versions +0 -1
  28. data/CHANGELOG.md +0 -55
  29. data/Gemfile.lock +0 -49
  30. data/LICENSE +0 -7
  31. data/README.md +0 -103
  32. data/TODO +0 -17
  33. data/bin/code +0 -76
  34. data/bin/format +0 -3
  35. data/bin/publish +0 -19
  36. data/bin/template +0 -85
  37. data/bin/test +0 -17
  38. data/code-ruby.gemspec +0 -17
  39. data/docs/class.code +0 -9
  40. data/docs/euler/1.template +0 -10
  41. data/docs/euler/2.template +0 -16
  42. data/docs/euler/3.template +0 -16
  43. data/docs/euler/4.template +0 -10
  44. data/docs/euler/5.template +0 -13
  45. data/docs/fibonnaci.template +0 -14
  46. data/docs/meetup.code +0 -12
  47. data/docs/precedence.template +0 -36
  48. data/docs/rain.code +0 -22
  49. data/docs/slack.code +0 -17
  50. data/docs/stripe.code +0 -7
  51. data/docs/twitter.code +0 -9
  52. data/lib/code/error.rb +0 -18
  53. data/lib/code/node/base_10.rb +0 -29
  54. data/lib/code/node/base_16.rb +0 -13
  55. data/lib/code/node/base_2.rb +0 -13
  56. data/lib/code/node/base_8.rb +0 -13
  57. data/lib/code/node/boolean.rb +0 -22
  58. data/lib/code/node/call.rb +0 -47
  59. data/lib/code/node/call_argument.rb +0 -21
  60. data/lib/code/node/chained_call.rb +0 -23
  61. data/lib/code/node/code.rb +0 -20
  62. data/lib/code/node/decimal.rb +0 -26
  63. data/lib/code/node/dictionnary.rb +0 -33
  64. data/lib/code/node/equal.rb +0 -34
  65. data/lib/code/node/function.rb +0 -20
  66. data/lib/code/node/function_parameter.rb +0 -31
  67. data/lib/code/node/if.rb +0 -59
  68. data/lib/code/node/if_modifier.rb +0 -47
  69. data/lib/code/node/list.rb +0 -16
  70. data/lib/code/node/negation.rb +0 -15
  71. data/lib/code/node/not.rb +0 -15
  72. data/lib/code/node/nothing.rb +0 -12
  73. data/lib/code/node/number.rb +0 -25
  74. data/lib/code/node/operation.rb +0 -38
  75. data/lib/code/node/power.rb +0 -20
  76. data/lib/code/node/rescue.rb +0 -17
  77. data/lib/code/node/splat.rb +0 -15
  78. data/lib/code/node/statement.rb +0 -59
  79. data/lib/code/node/string.rb +0 -53
  80. data/lib/code/node/ternary.rb +0 -24
  81. data/lib/code/node/unary_minus.rb +0 -15
  82. data/lib/code/node/while.rb +0 -35
  83. data/lib/code/node.rb +0 -13
  84. data/lib/code/object/argument.rb +0 -32
  85. data/lib/code/object/boolean.rb +0 -27
  86. data/lib/code/object/decimal.rb +0 -162
  87. data/lib/code/object/dictionnary.rb +0 -96
  88. data/lib/code/object/function.rb +0 -64
  89. data/lib/code/object/global.rb +0 -42
  90. data/lib/code/object/integer.rb +0 -221
  91. data/lib/code/object/list.rb +0 -207
  92. data/lib/code/object/nothing.rb +0 -23
  93. data/lib/code/object/number.rb +0 -6
  94. data/lib/code/object/range.rb +0 -146
  95. data/lib/code/object/ruby_function.rb +0 -31
  96. data/lib/code/object/string.rb +0 -102
  97. data/lib/code/object.rb +0 -197
  98. data/lib/code/parser/addition.rb +0 -21
  99. data/lib/code/parser/and_operator.rb +0 -17
  100. data/lib/code/parser/bitwise_and.rb +0 -17
  101. data/lib/code/parser/bitwise_or.rb +0 -21
  102. data/lib/code/parser/boolean.rb +0 -17
  103. data/lib/code/parser/call.rb +0 -122
  104. data/lib/code/parser/chained_call.rb +0 -47
  105. data/lib/code/parser/class.rb +0 -45
  106. data/lib/code/parser/code.rb +0 -25
  107. data/lib/code/parser/dictionnary.rb +0 -67
  108. data/lib/code/parser/equal.rb +0 -94
  109. data/lib/code/parser/equality.rb +0 -35
  110. data/lib/code/parser/equality_lower.rb +0 -9
  111. data/lib/code/parser/function.rb +0 -85
  112. data/lib/code/parser/greater.rb +0 -25
  113. data/lib/code/parser/group.rb +0 -22
  114. data/lib/code/parser/if.rb +0 -63
  115. data/lib/code/parser/if_modifier.rb +0 -55
  116. data/lib/code/parser/list.rb +0 -42
  117. data/lib/code/parser/multiplication.rb +0 -25
  118. data/lib/code/parser/name.rb +0 -107
  119. data/lib/code/parser/negation.rb +0 -30
  120. data/lib/code/parser/not_keyword.rb +0 -23
  121. data/lib/code/parser/nothing.rb +0 -22
  122. data/lib/code/parser/number.rb +0 -154
  123. data/lib/code/parser/operation.rb +0 -35
  124. data/lib/code/parser/or_keyword.rb +0 -21
  125. data/lib/code/parser/or_operator.rb +0 -17
  126. data/lib/code/parser/power.rb +0 -43
  127. data/lib/code/parser/range.rb +0 -17
  128. data/lib/code/parser/rescue.rb +0 -39
  129. data/lib/code/parser/shift.rb +0 -21
  130. data/lib/code/parser/splat.rb +0 -31
  131. data/lib/code/parser/statement.rb +0 -9
  132. data/lib/code/parser/string.rb +0 -78
  133. data/lib/code/parser/ternary.rb +0 -46
  134. data/lib/code/parser/unary_minus.rb +0 -31
  135. data/lib/code/parser/while.rb +0 -36
  136. data/lib/code/parser/whitespace.rb +0 -49
  137. data/lib/code/parser.rb +0 -19
  138. data/lib/code/ruby.rb +0 -174
  139. data/lib/code-ruby.rb +0 -10
  140. data/lib/code.rb +0 -47
  141. data/lib/template/node/code_part.rb +0 -13
  142. data/lib/template/node/part.rb +0 -19
  143. data/lib/template/node/template.rb +0 -15
  144. data/lib/template/node/text_part.rb +0 -13
  145. data/lib/template/node.rb +0 -4
  146. data/lib/template/parser/template.rb +0 -39
  147. data/lib/template/parser.rb +0 -19
  148. data/lib/template/version.rb +0 -3
  149. data/lib/template-ruby.rb +0 -10
  150. data/lib/template.rb +0 -50
  151. data/spec/code/addition_spec.rb +0 -13
  152. data/spec/code/and_operator_spec.rb +0 -13
  153. data/spec/code/bitwise_and_spec.rb +0 -13
  154. data/spec/code/bitwise_or_spec.rb +0 -13
  155. data/spec/code/boolean_spec.rb +0 -13
  156. data/spec/code/call_spec.rb +0 -21
  157. data/spec/code/chained_call_spec.rb +0 -16
  158. data/spec/code/code_spec.rb +0 -29
  159. data/spec/code/dictionnary_spec.rb +0 -17
  160. data/spec/code/equal_spec.rb +0 -26
  161. data/spec/code/equality_spec.rb +0 -13
  162. data/spec/code/function_spec.rb +0 -18
  163. data/spec/code/greater_spec.rb +0 -18
  164. data/spec/code/group_spec.rb +0 -12
  165. data/spec/code/if_modifier_spec.rb +0 -20
  166. data/spec/code/if_spec.rb +0 -25
  167. data/spec/code/list_spec.rb +0 -19
  168. data/spec/code/multiplication_spec.rb +0 -18
  169. data/spec/code/negation_spec.rb +0 -20
  170. data/spec/code/not_keyword_spec.rb +0 -13
  171. data/spec/code/nothing_spec.rb +0 -17
  172. data/spec/code/number_spec.rb +0 -22
  173. data/spec/code/or_keyword_spec.rb +0 -17
  174. data/spec/code/or_operator_spec.rb +0 -16
  175. data/spec/code/parser/boolean_spec.rb +0 -16
  176. data/spec/code/parser/call_spec.rb +0 -26
  177. data/spec/code/parser/chained_call.rb +0 -17
  178. data/spec/code/parser/dictionnary_spec.rb +0 -18
  179. data/spec/code/parser/function_spec.rb +0 -16
  180. data/spec/code/parser/group_spec.rb +0 -18
  181. data/spec/code/parser/list_spec.rb +0 -18
  182. data/spec/code/parser/number_spec.rb +0 -12
  183. data/spec/code/parser/string_spec.rb +0 -21
  184. data/spec/code/parser_spec.rb +0 -23
  185. data/spec/code/power_spec.rb +0 -13
  186. data/spec/code/range_spec.rb +0 -16
  187. data/spec/code/rescue_spec.rb +0 -13
  188. data/spec/code/shift_spec.rb +0 -13
  189. data/spec/code/splat_spec.rb +0 -13
  190. data/spec/code/string_spec.rb +0 -27
  191. data/spec/code/ternary_spec.rb +0 -18
  192. data/spec/code/unary_minus_spec.rb +0 -13
  193. data/spec/code/while_spec.rb +0 -18
  194. data/template-ruby.gemspec +0 -19
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
@@ -1,17 +0,0 @@
1
- #!/bin/bash
2
-
3
- set -e
4
-
5
- rspec
6
-
7
- for f in docs/**/*.template
8
- do
9
- echo $f
10
- bin/template -i $f -p > /dev/null
11
- done
12
-
13
- for f in docs/*.code
14
- do
15
- echo $f
16
- bin/code -i $f -p > /dev/null
17
- done
data/code-ruby.gemspec DELETED
@@ -1,17 +0,0 @@
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 DELETED
@@ -1,9 +0,0 @@
1
- class ApiClient
2
- get = (url) => {
3
- fetch(url)
4
- }
5
- end
6
-
7
- class Twitter < ApiClient
8
- self.base_url = "https://api.twitter.com"
9
- end
@@ -1,10 +0,0 @@
1
- {
2
- sum = 0
3
-
4
- 1000.times do |i|
5
- if i % 3 == 0 or i % 5 == 0
6
- sum += i
7
- end
8
- end
9
-
10
- sum
@@ -1,16 +0,0 @@
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
@@ -1,16 +0,0 @@
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
@@ -1,10 +0,0 @@
1
- {
2
-
3
- min = 1
4
- max = 999
5
-
6
- (min..max).map do |i|
7
- ((min..max).to_list.reverse.detect do |j|
8
- (i * j).to_string.reverse == (i * j).to_string
9
- end || 0) * i
10
- end.max
@@ -1,13 +0,0 @@
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
@@ -1,14 +0,0 @@
1
- {
2
- fibonnaci = (n) => {
3
- if n < 2
4
- n
5
- else
6
- fibonnaci(n - 1) + fibonnaci(n - 2)
7
- end
8
- }
9
-
10
- (0..20).each do |i|
11
- puts("fibonnaci({i}) = {fibonnaci(i)}")
12
- end
13
-
14
- nothing
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
@@ -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
@@ -1,7 +0,0 @@
1
- event = Stripe::Webhook.event
2
-
3
- if event.type == "payment_intent.succeeded"
4
- payment_intent = event.data.object
5
- amount = Money.format(payment_intent.amount, payment_intent.currency)
6
- Sms.send("You got paid {amount}")
7
- end
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/lib/code/error.rb DELETED
@@ -1,18 +0,0 @@
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
-
15
- class IncompatibleContext < ::Code::Error
16
- end
17
- end
18
- end
@@ -1,29 +0,0 @@
1
- class Code
2
- class Node
3
- class Base10 < Node
4
- def initialize(parsed)
5
- @whole = parsed.delete(:whole)
6
-
7
- if parsed.key?(:exponent)
8
- @exponent = Node::Statement.new(parsed.delete(:exponent))
9
- end
10
-
11
- super(parsed)
12
- end
13
-
14
- def evaluate(**args)
15
- if @exponent
16
- exponent = @exponent.evaluate(**args)
17
-
18
- if exponent.is_a?(::Code::Object::Integer)
19
- ::Code::Object::Integer.new(@whole.to_i, exponent: exponent)
20
- else
21
- ::Code::Object::Decimal.new(@whole, exponent: exponent)
22
- end
23
- else
24
- ::Code::Object::Integer.new(@whole.to_i)
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class Base16 < Node
4
- def initialize(parsed)
5
- @base_16 = parsed
6
- end
7
-
8
- def evaluate(**args)
9
- ::Code::Object::Integer.new(@base_16.to_i(16))
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class Base2 < Node
4
- def initialize(parsed)
5
- @base_2 = parsed
6
- end
7
-
8
- def evaluate(**args)
9
- ::Code::Object::Integer.new(@base_2.to_i(2))
10
- end
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class Base8 < Node
4
- def initialize(parsed)
5
- @base_8 = parsed
6
- end
7
-
8
- def evaluate(**args)
9
- ::Code::Object::Integer.new(@base_8.to_i(8))
10
- end
11
- end
12
- end
13
- end
@@ -1,22 +0,0 @@
1
- class Code
2
- class Node
3
- class Boolean < Node
4
- TRUE_KEYWORD = "true"
5
- FALSE_KEYWORD = "false"
6
-
7
- def initialize(parsed)
8
- @boolean = parsed
9
- end
10
-
11
- def evaluate(**args)
12
- if @boolean == TRUE_KEYWORD
13
- ::Code::Object::Boolean.new(true)
14
- elsif @boolean == FALSE_KEYWORD
15
- ::Code::Object::Boolean.new(false)
16
- else
17
- raise NotImplementedError.new(@boolean)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,47 +0,0 @@
1
- class Code
2
- class Node
3
- class Call < Node
4
- class Block < Node
5
- def initialize(parsed)
6
- @parameters =
7
- parsed
8
- .delete(:parameters) { [] }
9
- .map { |parameter| Node::FunctionParameter.new(parameter) }
10
-
11
- @body = Node::Code.new(parsed.delete(:body))
12
-
13
- super(parsed)
14
- end
15
-
16
- def evaluate(**args)
17
- ::Code::Object::Argument.new(
18
- ::Code::Object::Function.new(parameters: @parameters, body: @body)
19
- )
20
- end
21
- end
22
-
23
- def initialize(parsed)
24
- @name = parsed.delete(:name)
25
- @arguments =
26
- parsed
27
- .delete(:arguments) { [] }
28
- .map { |argument| Node::CallArgument.new(argument) }
29
-
30
- if parsed.key?(:block)
31
- @block = Node::Call::Block.new(parsed.delete(:block))
32
- end
33
-
34
- super(parsed)
35
- end
36
-
37
- def evaluate(**args)
38
- arguments = @arguments.map { |argument| argument.evaluate(**args) }
39
- arguments << @block.evaluate(**args) if @block
40
-
41
- name = ::Code::Object::String.new(@name)
42
-
43
- args.fetch(:object).call(operator: name, arguments: arguments, **args)
44
- end
45
- end
46
- end
47
- end
@@ -1,21 +0,0 @@
1
- class Code
2
- class Node
3
- class CallArgument < Node
4
- def initialize(parsed)
5
- @value = Node::Code.new(parsed.delete(:value))
6
- @name = parsed.delete(:name)
7
- end
8
-
9
- def evaluate(**args)
10
- if @name
11
- ::Code::Object::Argument.new(
12
- @value.evaluate(**args),
13
- name: ::Code::Object::String.new(@name)
14
- )
15
- else
16
- ::Code::Object::Argument.new(@value.evaluate(**args))
17
- end
18
- end
19
- end
20
- end
21
- 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
@@ -1,20 +0,0 @@
1
- class Code
2
- class Node
3
- class Code < Node
4
- def initialize(parsed)
5
- @statements = parsed.map { |statement| Node::Statement.new(statement) }
6
- end
7
-
8
- def evaluate(**args)
9
- last = ::Code::Object::Nothing.new
10
-
11
- @statements.each do |statement|
12
- last =
13
- statement.evaluate(**args.merge(object: ::Code::Object::Global.new))
14
- end
15
-
16
- last
17
- end
18
- end
19
- end
20
- end
@@ -1,26 +0,0 @@
1
- class Code
2
- class Node
3
- class Decimal < Node
4
- def initialize(parsed)
5
- @decimal = parsed.delete(:decimal)
6
-
7
- if parsed.key?(:exponent)
8
- @exponent = Node::Statement.new(parsed.delete(:exponent))
9
- end
10
-
11
- super(parsed)
12
- end
13
-
14
- def evaluate(**args)
15
- if @exponent
16
- ::Code::Object::Decimal.new(
17
- @decimal,
18
- exponent: @exponent.evaluate(**args)
19
- )
20
- else
21
- ::Code::Object::Decimal.new(@decimal)
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,33 +0,0 @@
1
- class Code
2
- class Node
3
- class Dictionnary < Node
4
- class KeyValue < Node
5
- def initialize(parsed)
6
- if parsed.key?(:statement)
7
- @key = Node::Statement.new(parsed.delete(:statement))
8
- elsif parsed.key?(:name)
9
- @key = Node::String.new([{ text: parsed.delete(:name) }])
10
- end
11
-
12
- @value = Node::Code.new(parsed.delete(:value))
13
- end
14
-
15
- def evaluate(**args)
16
- [@key.evaluate(**args), @value.evaluate(**args)]
17
- end
18
- end
19
-
20
- def initialize(parsed)
21
- parsed = [] if parsed == ""
22
- @key_values =
23
- parsed.map { |key_value| Node::Dictionnary::KeyValue.new(key_value) }
24
- end
25
-
26
- def evaluate(**args)
27
- ::Code::Object::Dictionnary.new(
28
- @key_values.map { |key_value| key_value.evaluate(**args) }.to_h
29
- )
30
- end
31
- end
32
- end
33
- end
@@ -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,20 +0,0 @@
1
- class Code
2
- class Node
3
- class Function < Node
4
- def initialize(parsed)
5
- @parameters =
6
- parsed
7
- .delete(:parameters) { [] }
8
- .map { |parameter| Node::FunctionParameter.new(parameter) }
9
-
10
- @body = Node::Code.new(parsed.delete(:body))
11
-
12
- super(parsed)
13
- end
14
-
15
- def evaluate(**args)
16
- ::Code::Object::Function.new(parameters: @parameters, body: @body)
17
- end
18
- end
19
- end
20
- end