keisan 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: de85515fc642eede46d24bdb2347a7f8bbe32c4d317fd305404c08367e742ecf
4
- data.tar.gz: 0ce75e5555ca880e690e1cae4b76820594899706aa5608944fa5c9eedd0be9c8
3
+ metadata.gz: a2a155be79ae75b3c67a097791c2af2bf67196cae16ef5372049b4eb54884117
4
+ data.tar.gz: b5d38c6111d1803a14c0229b0f54dc8733d581409254068ac9970af1321d9fca
5
5
  SHA512:
6
- metadata.gz: cd6b7cee90edee2d0ba51eb6b4d0a223305eeaf75a85ec6c2c6dbd9c3310ecd035c00d4a86d16768303f28d61996291e6e4d3ac7e5323a3a4df0c22f6343ff58
7
- data.tar.gz: '05873d8324f622a818f5cba2b9ef260e61f3500154db3ab0ecb9570bf308a686f4b32eddcbfb664afab76845fe71da7c82f47dd511f845f33498045011024a8a'
6
+ metadata.gz: 550d595b64739c49e6fdcd12b149fa0f5706d3032f11c61f6f9b9bd3ac6ff7465398a5b04f58868dca803c9cef1b9fb14f9df029bc03c550d8616760751f3886
7
+ data.tar.gz: d2c25f61cdd3400c198cb6962cd31c0023ef5aa0081d3a0ea2699b7e7098620d8d39dc8af26bee5fe10197f84fb91218763fdc6ba830d2c97b30122a1b30fd3d
data/README.md CHANGED
@@ -178,6 +178,22 @@ calculator.evaluate("x = 11; {let x = 12}; x")
178
178
  #=> 11
179
179
  ```
180
180
 
181
+ ##### Comments
182
+
183
+ When working with multi-line blocks of code, sometimes comments are useful to include.
184
+ Comments are parts of a string from the `#` character to the end of a line (indicated by a newline character `"\n"`).
185
+
186
+ ```
187
+ calculator = Keisan::Calculator.new
188
+ calculator.evaluate("""
189
+ # This is a comment
190
+ x = 'foo'
191
+ x += '#bar' # Notice that `#` inside strings is not part of the comment
192
+ x # Should print 'foo#bar'
193
+ """)
194
+ #=> "foo#bar"
195
+ ```
196
+
181
197
  ##### Lists
182
198
 
183
199
  Just like in Ruby, lists can be defined using square brackets, and indexed using square brackets
@@ -9,24 +9,26 @@ module Keisan
9
9
  end
10
10
 
11
11
  class StringPortion < Portion
12
- attr_reader :string
12
+ attr_reader :string, :escaped_string
13
13
 
14
14
  def initialize(expression, start_index)
15
15
  super(start_index)
16
16
 
17
17
  @string = expression[start_index]
18
+ @escaped_string = expression[start_index]
18
19
  @end_index = start_index + 1
19
20
 
20
21
  while @end_index < expression.size
21
22
  if expression[@end_index] == quote_type
22
23
  @string << quote_type
24
+ @escaped_string << quote_type
23
25
  @end_index += 1
24
26
  # Successfully parsed the string
25
27
  return
26
28
  end
27
29
 
28
- n, c = process_next_character(expression, @end_index)
29
- @string << c
30
+ n, c = get_potentially_escaped_next_character(expression, @end_index)
31
+ @escaped_string << c
30
32
  @end_index += n
31
33
  end
32
34
 
@@ -44,9 +46,13 @@ module Keisan
44
46
  private
45
47
 
46
48
  # Returns number of processed input characters, and the output character
47
- def process_next_character(expression, index)
48
- # escape character
49
- if expression[index] == "\\"
49
+ # If a sequence like '\"' is encountered, the first backslash escapes the
50
+ # second double-quote, and the two characters will act as a one double-quote
51
+ # character.
52
+ def get_potentially_escaped_next_character(expression, index)
53
+ @string << expression[index]
54
+ if expression[index] == "\\" && index + 1 < expression.size
55
+ @string << expression[index + 1]
50
56
  return [2, escaped_character(expression[index + 1])]
51
57
  else
52
58
  return [1, expression[index]]
@@ -33,7 +33,7 @@ module Keisan
33
33
  @tokens = portions.inject([]) do |tokens, portion|
34
34
  case portion
35
35
  when StringAndGroupParser::StringPortion
36
- tokens << Tokens::String.new(portion.to_s)
36
+ tokens << Tokens::String.new(portion.escaped_string)
37
37
  when StringAndGroupParser::GroupPortion
38
38
  tokens << Tokens::Group.new(portion.to_s)
39
39
  when StringAndGroupParser::OtherPortion
@@ -1,3 +1,3 @@
1
1
  module Keisan
2
- VERSION = "0.8.4"
2
+ VERSION = "0.8.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keisan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.4
4
+ version: 0.8.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-16 00:00:00.000000000 Z
11
+ date: 2020-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cmath