code-ruby 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/CHANGELOG.md +5 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +28 -55
  6. data/TODO +17 -0
  7. data/bin/code +56 -31
  8. data/bin/format +3 -0
  9. data/bin/template +62 -20
  10. data/bin/test +17 -0
  11. data/code-ruby.gemspec +1 -6
  12. data/docs/class.code +9 -0
  13. data/docs/euler/1.template +1 -5
  14. data/docs/euler/5.template +0 -1
  15. data/docs/meetup.code +12 -0
  16. data/docs/precedence.template +6 -39
  17. data/docs/rain.code +22 -0
  18. data/docs/slack.code +17 -0
  19. data/docs/stripe.code +7 -0
  20. data/docs/twitter.code +9 -0
  21. data/language-ruby.gemspec +18 -0
  22. data/lib/code/error.rb +3 -0
  23. data/lib/code/node/base_10.rb +29 -0
  24. data/lib/code/node/base_16.rb +13 -0
  25. data/lib/code/node/base_2.rb +13 -0
  26. data/lib/code/node/base_8.rb +13 -0
  27. data/lib/code/node/boolean.rb +7 -7
  28. data/lib/code/node/call.rb +32 -37
  29. data/lib/code/node/call_argument.rb +11 -27
  30. data/lib/code/node/chained_call.rb +10 -25
  31. data/lib/code/node/code.rb +4 -6
  32. data/lib/code/node/decimal.rb +26 -0
  33. data/lib/code/node/dictionnary.rb +20 -9
  34. data/lib/code/node/equal.rb +18 -20
  35. data/lib/code/node/function.rb +10 -7
  36. data/lib/code/node/function_parameter.rb +31 -0
  37. data/lib/code/node/if.rb +36 -32
  38. data/lib/code/node/if_modifier.rb +35 -36
  39. data/lib/code/node/list.rb +6 -9
  40. data/lib/code/node/negation.rb +5 -23
  41. data/lib/code/node/not.rb +15 -0
  42. data/lib/code/node/nothing.rb +1 -1
  43. data/lib/code/node/number.rb +14 -12
  44. data/lib/code/node/operation.rb +21 -16
  45. data/lib/code/node/power.rb +10 -6
  46. data/lib/code/node/rescue.rb +4 -3
  47. data/lib/code/node/splat.rb +15 -0
  48. data/lib/code/node/statement.rb +47 -69
  49. data/lib/code/node/string.rb +41 -10
  50. data/lib/code/node/ternary.rb +7 -9
  51. data/lib/code/node/unary_minus.rb +5 -12
  52. data/lib/code/node/while.rb +17 -24
  53. data/lib/code/node.rb +7 -8
  54. data/lib/code/object/argument.rb +3 -12
  55. data/lib/code/object/decimal.rb +114 -27
  56. data/lib/code/object/dictionnary.rb +29 -12
  57. data/lib/code/object/function.rb +23 -24
  58. data/lib/code/object/global.rb +42 -0
  59. data/lib/code/object/integer.rb +142 -35
  60. data/lib/code/object/list.rb +74 -95
  61. data/lib/code/object/range.rb +38 -50
  62. data/lib/code/object/ruby_function.rb +31 -0
  63. data/lib/code/object/string.rb +36 -16
  64. data/lib/code/object.rb +114 -54
  65. data/lib/code/parser/addition.rb +12 -20
  66. data/lib/code/parser/and_operator.rb +9 -20
  67. data/lib/code/parser/bitwise_and.rb +9 -20
  68. data/lib/code/parser/bitwise_or.rb +12 -20
  69. data/lib/code/parser/boolean.rb +10 -7
  70. data/lib/code/parser/call.rb +92 -60
  71. data/lib/code/parser/chained_call.rb +47 -0
  72. data/lib/code/parser/class.rb +45 -0
  73. data/lib/code/parser/code.rb +17 -10
  74. data/lib/code/parser/dictionnary.rb +56 -30
  75. data/lib/code/parser/equal.rb +87 -35
  76. data/lib/code/parser/equality.rb +23 -24
  77. data/lib/code/parser/equality_lower.rb +9 -0
  78. data/lib/code/parser/function.rb +67 -40
  79. data/lib/code/parser/greater.rb +25 -0
  80. data/lib/code/parser/group.rb +13 -8
  81. data/lib/code/parser/if.rb +51 -21
  82. data/lib/code/parser/if_modifier.rb +43 -16
  83. data/lib/code/parser/list.rb +32 -19
  84. data/lib/code/parser/multiplication.rb +15 -20
  85. data/lib/code/parser/name.rb +96 -84
  86. data/lib/code/parser/negation.rb +20 -9
  87. data/lib/code/parser/not_keyword.rb +14 -12
  88. data/lib/code/parser/nothing.rb +13 -8
  89. data/lib/code/parser/number.rb +124 -68
  90. data/lib/code/parser/operation.rb +35 -0
  91. data/lib/code/parser/or_keyword.rb +12 -20
  92. data/lib/code/parser/or_operator.rb +9 -20
  93. data/lib/code/parser/power.rb +32 -14
  94. data/lib/code/parser/range.rb +9 -17
  95. data/lib/code/parser/rescue.rb +29 -13
  96. data/lib/code/parser/shift.rb +11 -21
  97. data/lib/code/parser/splat.rb +31 -0
  98. data/lib/code/parser/statement.rb +4 -3
  99. data/lib/code/parser/string.rb +53 -62
  100. data/lib/code/parser/ternary.rb +36 -15
  101. data/lib/code/parser/unary_minus.rb +23 -5
  102. data/lib/code/parser/while.rb +26 -15
  103. data/lib/code/parser/whitespace.rb +49 -0
  104. data/lib/code/parser.rb +15 -0
  105. data/lib/code/ruby.rb +162 -0
  106. data/lib/code-ruby.rb +2 -5
  107. data/lib/code.rb +24 -7
  108. data/lib/language/atom.rb +343 -0
  109. data/lib/language/output.rb +130 -0
  110. data/lib/language/parser/absent/present.rb +8 -0
  111. data/lib/language/parser/absent.rb +6 -0
  112. data/lib/language/parser/end_of_input.rb +6 -0
  113. data/lib/language/parser/interuption.rb +38 -0
  114. data/lib/language/parser/not_end_of_input.rb +6 -0
  115. data/lib/language/parser/str/not_found.rb +16 -0
  116. data/lib/language/parser/str.rb +6 -0
  117. data/lib/language/parser.rb +53 -0
  118. data/lib/language-ruby.rb +10 -0
  119. data/lib/language.rb +80 -0
  120. data/lib/template/node/code_part.rb +1 -1
  121. data/lib/template/node/part.rb +1 -1
  122. data/lib/template/node/template.rb +1 -1
  123. data/lib/template/node/text_part.rb +1 -1
  124. data/lib/template/node.rb +1 -1
  125. data/lib/template/parser/template.rb +26 -17
  126. data/lib/template/parser.rb +15 -0
  127. data/lib/template/version.rb +1 -1
  128. data/lib/template-ruby.rb +2 -5
  129. data/lib/template.rb +23 -13
  130. data/spec/code/addition_spec.rb +13 -0
  131. data/spec/code/and_operator_spec.rb +13 -0
  132. data/spec/code/bitwise_and_spec.rb +13 -0
  133. data/spec/code/bitwise_or_spec.rb +13 -0
  134. data/spec/code/boolean_spec.rb +13 -0
  135. data/spec/code/call_spec.rb +21 -0
  136. data/spec/code/chained_call_spec.rb +16 -0
  137. data/spec/code/dictionnary_spec.rb +17 -0
  138. data/spec/code/equal_spec.rb +26 -0
  139. data/spec/code/equality_spec.rb +13 -0
  140. data/spec/code/function_spec.rb +18 -0
  141. data/spec/code/greater_spec.rb +18 -0
  142. data/spec/code/group_spec.rb +12 -0
  143. data/spec/code/if_modifier_spec.rb +20 -0
  144. data/spec/code/if_spec.rb +25 -0
  145. data/spec/code/list_spec.rb +17 -0
  146. data/spec/code/multiplication_spec.rb +18 -0
  147. data/spec/code/negation_spec.rb +20 -0
  148. data/spec/code/not_keyword_spec.rb +13 -0
  149. data/spec/code/nothing_spec.rb +17 -0
  150. data/spec/code/number_spec.rb +22 -0
  151. data/spec/code/or_keyword_spec.rb +17 -0
  152. data/spec/code/or_operator_spec.rb +16 -0
  153. data/spec/code/parser/boolean_spec.rb +5 -7
  154. data/spec/code/parser/call_spec.rb +16 -56
  155. data/spec/code/parser/chained_call.rb +17 -0
  156. data/spec/code/parser/dictionnary_spec.rb +8 -9
  157. data/spec/code/parser/function_spec.rb +5 -21
  158. data/spec/code/parser/group_spec.rb +18 -0
  159. data/spec/code/parser/list_spec.rb +9 -20
  160. data/spec/code/parser/number_spec.rb +4 -109
  161. data/spec/code/parser/string_spec.rb +9 -17
  162. data/spec/code/parser_spec.rb +23 -0
  163. data/spec/code/power_spec.rb +13 -0
  164. data/spec/code/range_spec.rb +16 -0
  165. data/spec/code/rescue_spec.rb +13 -0
  166. data/spec/code/shift_spec.rb +13 -0
  167. data/spec/code/splat_spec.rb +13 -0
  168. data/spec/code/string_spec.rb +25 -0
  169. data/spec/code/ternary_spec.rb +18 -0
  170. data/spec/code/unary_minus_spec.rb +13 -0
  171. data/spec/code/while_spec.rb +18 -0
  172. data/spec/spec_helper.rb +4 -1
  173. data/template-ruby.gemspec +2 -7
  174. metadata +113 -99
  175. data/lib/code/node/base_10_decimal.rb +0 -32
  176. data/lib/code/node/base_10_integer.rb +0 -32
  177. data/lib/code/node/base_10_number.rb +0 -19
  178. data/lib/code/node/base_16_number.rb +0 -19
  179. data/lib/code/node/base_2_number.rb +0 -19
  180. data/lib/code/node/base_8_number.rb +0 -19
  181. data/lib/code/node/block.rb +0 -17
  182. data/lib/code/node/defined.rb +0 -19
  183. data/lib/code/node/dictionnary_key_value.rb +0 -23
  184. data/lib/code/node/function_argument.rb +0 -45
  185. data/lib/code/node/group.rb +0 -13
  186. data/lib/code/node/keyword_call_argument.rb +0 -30
  187. data/lib/code/node/keyword_function_argument.rb +0 -33
  188. data/lib/code/node/name.rb +0 -55
  189. data/lib/code/node/not_keyword.rb +0 -13
  190. data/lib/code/node/or_keyword.rb +0 -34
  191. data/lib/code/node/range.rb +0 -31
  192. data/lib/code/node/regular_call_argument.rb +0 -34
  193. data/lib/code/node/regular_function_argument.rb +0 -36
  194. data/lib/code/node/string_characters.rb +0 -13
  195. data/lib/code/node/string_component.rb +0 -23
  196. data/lib/code/node/string_interpolation.rb +0 -13
  197. data/lib/code/parser/defined.rb +0 -20
  198. data/lib/code/parser/greater_than.rb +0 -33
  199. data/spec/call_spec.rb +0 -22
  200. data/spec/code/error/type_error_spec.rb +0 -63
  201. data/spec/code/parser/name_spec.rb +0 -15
  202. data/spec/code/parser/nothing_spec.rb +0 -19
  203. data/spec/code_spec.rb +0 -120
  204. data/spec/function_spec.rb +0 -26
  205. data/spec/template/parser/template_spec.rb +0 -19
  206. data/spec/template_spec.rb +0 -33
@@ -1,45 +0,0 @@
1
- class Code
2
- class Node
3
- class FunctionArgument < Node
4
- def initialize(argument)
5
- if argument.key?(:regular)
6
- @argument =
7
- ::Code::Node::RegularFunctionArgument.new(argument.fetch(:regular))
8
- elsif argument.key?(:keyword)
9
- @argument =
10
- ::Code::Node::KeywordFunctionArgument.new(argument.fetch(:keyword))
11
- else
12
- raise NotImplementedError.new(argument.inspect)
13
- end
14
- end
15
-
16
- def evaluate(**args)
17
- @argument.evaluate(**args)
18
- end
19
-
20
- def splat?
21
- @argument.splat?
22
- end
23
-
24
- def keyword_splat?
25
- @argument.keyword_splat?
26
- end
27
-
28
- def name
29
- @argument.name
30
- end
31
-
32
- def block?
33
- @argument.block?
34
- end
35
-
36
- def regular?
37
- @argument.is_a?(::Code::Node::RegularFunctionArgument)
38
- end
39
-
40
- def keyword?
41
- @argument.is_a?(::Code::Node::KeywordFunctionArgument)
42
- end
43
- end
44
- end
45
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class Group < Node
4
- def initialize(group)
5
- @code = ::Code::Node::Code.new(group)
6
- end
7
-
8
- def evaluate(**args)
9
- @code.evaluate(**args)
10
- end
11
- end
12
- end
13
- end
@@ -1,30 +0,0 @@
1
- class Code
2
- class Node
3
- class KeywordCallArgument < Node
4
- def initialize(argument)
5
- @name = argument.fetch(:name)
6
- @value = ::Code::Node::Code.new(argument.fetch(:value))
7
- end
8
-
9
- def evaluate(**args)
10
- @value.evaluate(**args)
11
- end
12
-
13
- def name
14
- ::Code::Object::String.new(@name.to_s)
15
- end
16
-
17
- def block?
18
- false
19
- end
20
-
21
- def splat?
22
- false
23
- end
24
-
25
- def keyword_splat?
26
- false
27
- end
28
- end
29
- end
30
- end
@@ -1,33 +0,0 @@
1
- class Code
2
- class Node
3
- class KeywordFunctionArgument < Node
4
- def initialize(argument)
5
- @name = argument.fetch(:name)
6
-
7
- if argument.key?(:default)
8
- @default = ::Code::Node::Code.new(argument.fetch(:default))
9
- end
10
- end
11
-
12
- def evaluate(**args)
13
- @default ? @default.evaluate(**args) : ::Code::Object::Nothing.new
14
- end
15
-
16
- def name
17
- ::Code::Object::String.new(@name.to_s)
18
- end
19
-
20
- def splat?
21
- false
22
- end
23
-
24
- def keyword_splat?
25
- false
26
- end
27
-
28
- def block?
29
- false
30
- end
31
- end
32
- end
33
- end
@@ -1,55 +0,0 @@
1
- class Code
2
- class Node
3
- class Name < Node
4
- def initialize(name)
5
- @name = name
6
- end
7
-
8
- def evaluate(**args)
9
- context = args.fetch(:context)
10
- arguments = args.fetch(:arguments, [])
11
- object = args.fetch(:object, nil)
12
- io = args.fetch(:io)
13
-
14
- if object
15
- object.call(
16
- context: context,
17
- operator: name,
18
- arguments: arguments,
19
- io: io,
20
- )
21
- elsif context.key?(name)
22
- object = context[name]
23
-
24
- if object.is_a?(::Code::Object::Function)
25
- object.call(
26
- context: context,
27
- operator: nil,
28
- arguments: arguments,
29
- io: io,
30
- )
31
- else
32
- object
33
- end
34
- elsif name == "puts"
35
- arguments.each { |argument| io.puts argument.value }
36
- ::Code::Object::Nothing.new
37
- elsif name == "print"
38
- arguments.each { |argument| io.print argument.value }
39
- ::Code::Object::Nothing.new
40
- elsif name == "context"
41
- return ::Code::Object::Nothing.new if arguments.size != 1
42
- context[arguments.first&.value] || ::Code::Object::Nothing.new
43
- else
44
- raise ::Code::Error::Undefined.new("#{name} undefined")
45
- end
46
- end
47
-
48
- private
49
-
50
- def name
51
- ::Code::Object::String.new(@name.to_s)
52
- end
53
- end
54
- end
55
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class NotKeyword < Node
4
- def initialize(not_keyword)
5
- @statement = ::Code::Node::Statement.new(not_keyword)
6
- end
7
-
8
- def evaluate(**args)
9
- ::Code::Object::Boolean.new(!@statement.evaluate(**args).truthy?)
10
- end
11
- end
12
- end
13
- end
@@ -1,34 +0,0 @@
1
- class Code
2
- class Node
3
- class OrKeyword < Node
4
- OR_KEYWORD = "or"
5
- AND_KEYWORD = "and"
6
-
7
- def initialize(or_keyword)
8
- @first = ::Code::Node::Statement.new(or_keyword.fetch(:first))
9
- @rest = or_keyword.fetch(:rest)
10
- @rest.map! do |operation|
11
- ::Code::Node::Operation::Operation.new(operation)
12
- end
13
- end
14
-
15
- def evaluate(**args)
16
- object = @first.evaluate(**args)
17
-
18
- @rest.each do |operation|
19
- if operation.operator == OR_KEYWORD
20
- return object if object.truthy?
21
- elsif operation.operator == AND_KEYWORD
22
- return object unless object.truthy?
23
- else
24
- raise NotImplementedError.new(operation.operator.inspect)
25
- end
26
-
27
- object = operation.statement.evaluate(**args)
28
- end
29
-
30
- object
31
- end
32
- end
33
- end
34
- end
@@ -1,31 +0,0 @@
1
- class Code
2
- class Node
3
- class Range < Node
4
- INCLUSIVE_RANGE = ".."
5
- EXCLUSIVE_RANGE = "..."
6
-
7
- def initialize(range)
8
- @left = ::Code::Node::Statement.new(range.fetch(:left))
9
- @operator = range.fetch(:operator)
10
- @right = ::Code::Node::Statement.new(range.fetch(:right))
11
- end
12
-
13
- def evaluate(**args)
14
- left = @left.evaluate(**args)
15
- right = @right.evaluate(**args)
16
-
17
- if operator == INCLUSIVE_RANGE
18
- ::Code::Object::Range.new(left, right, exclude_end: false)
19
- elsif operator == EXCLUSIVE_RANGE
20
- ::Code::Object::Range.new(left, right, exclude_end: true)
21
- else
22
- raise NotImplementedError.new(operator)
23
- end
24
- end
25
-
26
- private
27
-
28
- attr_reader :operator
29
- end
30
- end
31
- end
@@ -1,34 +0,0 @@
1
- class Code
2
- class Node
3
- class RegularCallArgument < Node
4
- def initialize(argument)
5
- @splat = argument.key?(:splat)
6
- @keyword_splat = argument.key?(:keyword_splat)
7
- @block = argument.key?(:block)
8
- @value = ::Code::Node::Code.new(argument.fetch(:value))
9
- end
10
-
11
- def evaluate(**args)
12
- object = @value.evaluate(**args)
13
-
14
- block? ? simple_call(object, :to_function, **args) : object
15
- end
16
-
17
- def block?
18
- @block
19
- end
20
-
21
- def splat?
22
- @splat
23
- end
24
-
25
- def keyword_splat?
26
- @keyword_splat
27
- end
28
-
29
- def name
30
- nil
31
- end
32
- end
33
- end
34
- end
@@ -1,36 +0,0 @@
1
- class Code
2
- class Node
3
- class RegularFunctionArgument < Node
4
- def initialize(argument)
5
- @block = argument.key?(:block)
6
- @splat = argument.key?(:splat)
7
- @keyword_splat = argument.key?(:keyword_splat)
8
- @name = argument.fetch(:name)
9
-
10
- if argument.key?(:default)
11
- @default = ::Code::Node::Code.new(argument.fetch(:default))
12
- end
13
- end
14
-
15
- def evaluate(**args)
16
- @default ? @default.evaluate(**args) : ::Code::Object::Nothing.new
17
- end
18
-
19
- def name
20
- ::Code::Object::String.new(@name.to_s)
21
- end
22
-
23
- def splat?
24
- @splat
25
- end
26
-
27
- def keyword_splat?
28
- @keyword_splat
29
- end
30
-
31
- def block?
32
- @block
33
- end
34
- end
35
- end
36
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class StringCharacters < Node
4
- def initialize(characters)
5
- @characters = characters
6
- end
7
-
8
- def evaluate(**args)
9
- ::Code::Object::String.new(@characters.to_s)
10
- end
11
- end
12
- end
13
- end
@@ -1,23 +0,0 @@
1
- class Code
2
- class Node
3
- class StringComponent < Node
4
- def initialize(component)
5
- if component.key?(:characters)
6
- @component = ::Code::Node::StringCharacters.new(
7
- component.fetch(:characters)
8
- )
9
- elsif component.key?(:interpolation)
10
- @component = ::Code::Node::StringInterpolation.new(
11
- component.fetch(:interpolation)
12
- )
13
- else
14
- raise NotImplementedError.new(component.inspect)
15
- end
16
- end
17
-
18
- def evaluate(**args)
19
- @component.evaluate(**args)
20
- end
21
- end
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- class Code
2
- class Node
3
- class StringInterpolation < Node
4
- def initialize(interpolation)
5
- @interpolation = ::Code::Node::Code.new(interpolation)
6
- end
7
-
8
- def evaluate(**args)
9
- @interpolation.evaluate(**args)
10
- end
11
- end
12
- end
13
- end
@@ -1,20 +0,0 @@
1
- class Code
2
- class Parser
3
- class Defined < Parslet::Parser
4
- rule(:range) { ::Code::Parser::Range.new }
5
- rule(:name) { ::Code::Parser::Name.new }
6
-
7
- rule(:defined_keyword) { str("defined?") }
8
- rule(:opening_parenthesis) { str("(") }
9
- rule(:closing_parenthesis) { str(")") }
10
-
11
- rule(:defined) do
12
- (
13
- defined_keyword >> opening_parenthesis >> name >> closing_parenthesis
14
- ).as(:defined) | range
15
- end
16
-
17
- root(:defined)
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- class Code
2
- class Parser
3
- class GreaterThan < Parslet::Parser
4
- rule(:bitwise_or) { ::Code::Parser::BitwiseOr.new }
5
-
6
- rule(:right_caret) { str(">") }
7
- rule(:left_caret) { str("<") }
8
- rule(:equal) { str("=") }
9
-
10
- rule(:operator) do
11
- (right_caret >> equal) | (left_caret >> equal) | right_caret |
12
- left_caret
13
- end
14
-
15
- rule(:space) { str(" ") }
16
- rule(:newline) { str("\n") }
17
- rule(:whitespace) { (space | newline).repeat(1) }
18
- rule(:whitespace?) { whitespace.maybe }
19
-
20
- rule(:greater_than) do
21
- (
22
- bitwise_or.as(:first) >>
23
- (
24
- whitespace? >> operator.as(:operator) >> whitespace? >>
25
- bitwise_or.as(:statement)
26
- ).repeat(1).as(:rest)
27
- ).as(:greater_than) | bitwise_or
28
- end
29
-
30
- root(:greater_than)
31
- end
32
- end
33
- end
data/spec/call_spec.rb DELETED
@@ -1,22 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Code do
4
- subject { described_class.evaluate(input).to_s }
5
-
6
- [
7
- %w[(1..2).any?(&:even?) true],
8
- %w[(1..1).any?(&:even?) false],
9
- %w[(3..3).any?(&:even?) false],
10
- %w[(2..5).any?(&:even?) true],
11
- ["(2..5).any? { |n| n.even? }", "true"],
12
- ["(2..5).select { |i| i.even? }.any? { |n| n.even? }", "true"],
13
- ].each do |(input, expected)|
14
- context input.inspect do
15
- let(:input) { input }
16
-
17
- it "succeeds" do
18
- expect(subject).to eq(expected)
19
- end
20
- end
21
- end
22
- end
@@ -1,63 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe ::Code::Error::TypeError do
4
- describe "Integer" do
5
- context "creating an integer with a non-number exponent" do
6
- it "raises" do
7
- expect { ::Code::Object::Integer.new(1, exponent: "2") }.to raise_error(
8
- described_class,
9
- )
10
- end
11
- end
12
-
13
- [
14
- '1 / ""',
15
- '1 ** ""',
16
- '1 % ""',
17
- '1 - ""',
18
- '1 << ""',
19
- '1 >> ""',
20
- '1 & ""',
21
- '1 | ""',
22
- '1 ^ ""',
23
- '1 > ""',
24
- '1 >= ""',
25
- '1 < ""',
26
- '1 <= ""',
27
- ].each do |input|
28
- context input.inspect do
29
- it "raises" do
30
- expect { ::Code.evaluate(input) }.to raise_error(described_class)
31
- end
32
- end
33
- end
34
- end
35
-
36
- describe "Decimal" do
37
- context "creating an integer with a non-number exponent" do
38
- it "raises" do
39
- expect { ::Code::Object::Decimal.new(1, exponent: "2") }.to raise_error(
40
- described_class,
41
- )
42
- end
43
- end
44
-
45
- [
46
- '1.0 / ""',
47
- '1.0 ** ""',
48
- '1.0 * ""',
49
- '1.0 % ""',
50
- '1.0 - ""',
51
- '1.0 > ""',
52
- '1.0 >= ""',
53
- '1.0 < ""',
54
- '1.0 <= ""',
55
- ].each do |input|
56
- context input.inspect do
57
- it "raises" do
58
- expect { ::Code.evaluate(input) }.to raise_error(described_class)
59
- end
60
- end
61
- end
62
- end
63
- end
@@ -1,15 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Code::Parser::Name do
4
- subject { described_class.new.parse(input) }
5
-
6
- %w[user dorian User RSpec describe?].each do |(input, expected)|
7
- context input.inspect do
8
- let(:input) { input }
9
-
10
- it "succeeds" do
11
- expect(subject).to eq({ name: input })
12
- end
13
- end
14
- end
15
- end
@@ -1,19 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Code::Parser::Nothing do
4
- subject { described_class.new.parse(input) }
5
-
6
- [
7
- ["nothing", { nothing: "nothing" }],
8
- ["null", { nothing: "null" }],
9
- ["nil", { nothing: "nil" }],
10
- ].each do |(input, expected)|
11
- context input.inspect do
12
- let(:input) { input }
13
-
14
- it "succeeds" do
15
- expect(subject).to eq(expected)
16
- end
17
- end
18
- end
19
- end
data/spec/code_spec.rb DELETED
@@ -1,120 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Code do
4
- subject { described_class.evaluate(input).to_s }
5
-
6
- [
7
- ["nothing", ""],
8
- ["null", ""],
9
- ["nil", ""],
10
- %w[true true],
11
- %w[false false],
12
- %w[1 1],
13
- %w[1.2 1.2],
14
- %w[0b10 2],
15
- %w[0o10 8],
16
- %w[0x10 16],
17
- %w[1e2 100],
18
- %w[1.2e2.2 190.1871830953336182242521644],
19
- %w[1e1e1 10000000000],
20
- %w['hello' hello],
21
- %w["hello" hello],
22
- ["[true, 1, nothing]", "[true, 1, nothing]"],
23
- ['{a: 1, "b": 2}', '{"a" => 1, "b" => 2}'],
24
- %w[!true false],
25
- %w[!!true true],
26
- %w[!!nothing false],
27
- %w[!!1 true],
28
- %w[+1 1],
29
- %w[++++1 1],
30
- ["++++nothing", ""],
31
- %w[+{} {}],
32
- ["2 ** 2", "4"],
33
- ["2 ** 2 ** 3", "256"],
34
- %w[-2 -2],
35
- %w[--2 2],
36
- ["2 * 3", "6"],
37
- ["1 / 2", "0.5"],
38
- ["1 / 2 / 2", "0.25"],
39
- ["12 % 10", "2"],
40
- ["8 / -2 ** 3", "-1.0"],
41
- ["1 + 2 * 3 + 4", "11"],
42
- ["1e1.1 * 2", "25.1785082358833442084790822"],
43
- ["1 / 3 * 3", "0.999999999999999999999999999999999999"],
44
- ['3 * "hello"', "hellohellohello"],
45
- ['"Go" + "od"', "Good"],
46
- ["1 << 2", "4"],
47
- ["4 >> 2", "1"],
48
- ["2 & 1", "0"],
49
- ["2 | 1", "3"],
50
- ["5 ^ 6", "3"],
51
- ["5 > 6", "false"],
52
- ["5 > 5", "false"],
53
- ["5 > 4", "true"],
54
- ["2 > 1 == 3 > 2", "true"],
55
- ["true && false", "false"],
56
- ["true || false", "true"],
57
- %w[1..3 1..3],
58
- ['1 > 3 ? "Impossible" : "Sounds about right"', "Sounds about right"],
59
- ['1 < 3 ? "OK"', "OK"],
60
- ['1 < "" rescue "oops"', "oops"],
61
- ['"fine" rescue "oops"', "fine"],
62
- ["a = 1", "1"],
63
- ["a = 1 a * 2", "2"],
64
- ["a = 1 a += 1 a", "2"],
65
- ["a = 1 a -= 1 a", "0"],
66
- %w[defined?(a) false],
67
- ["a = 1 defined?(a)", "true"],
68
- ["not true", "false"],
69
- ["not false", "true"],
70
- ["not not 1", "true"],
71
- ["1 or 2", "1"],
72
- ["true or false", "true"],
73
- ["1 and 2", "2"],
74
- ["false and 2", "false"],
75
- ["true and false", "false"],
76
- ["true and true", "true"],
77
- ["1 if false", ""],
78
- ["1 if true", "1"],
79
- ["1 if true if true", "1"],
80
- ["1 unless false", "1"],
81
- ["1 unless true", ""],
82
- ["a = 0 a += 1 while a < 10 a", "10"],
83
- ["a = 0 a += 1 until a > 10 a", "11"],
84
- ["if true 1 end", "1"],
85
- ["if false 1 end", ""],
86
- ["if false 1 else 2 end", "2"],
87
- ["unless false 1 end", "1"],
88
- ["unless true 1 end", ""],
89
- ["if false 1 else if true 2 else 3 end", "2"],
90
- ["if false 1 else if false 2 else 3 end", "3"],
91
- ["unless false 1 else if false 2 else 3 end", "1"],
92
- ["if false 1 else unless false 2 else 3 end", "2"],
93
- ["a = 0\n while a < 10 a += 1 end a", "10"],
94
- ["a = 0\n until a > 10 a += 1 end a", "11"],
95
- ["until true end", ""],
96
- ["until true\nend", ""],
97
- %w[("Good".."Bad").first Good],
98
- ['"Dorian " * 2', "Dorian Dorian "],
99
- ["while false end == nothing", "true"],
100
- ['"1 + 1 = {1 + 1}"', "1 + 1 = 2"],
101
- ["'1 + 1 = {1 + 1}'", "1 + 1 = 2"],
102
- ["{}.to_string + [].to_string", "{}[]"],
103
- ["'a' + 1", "a1"],
104
- ["'a' + 1.0", "a1.0"],
105
- ["1 + 'a'", "1a"],
106
- ["1.0 + 'a'", "1.0a"],
107
- ["1 << 1", "2"],
108
- ["1.0 << 1", "2"],
109
- ["1 << 1.0", "2"],
110
- ["1.0 << 1.0", "2"],
111
- ].each do |(input, expected)|
112
- context input.inspect do
113
- let(:input) { input }
114
-
115
- it "succeeds" do
116
- expect(subject).to eq(expected)
117
- end
118
- end
119
- end
120
- end