keisan 0.8.4 → 0.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/keisan/string_and_group_parser.rb +12 -6
- data/lib/keisan/tokenizer.rb +1 -1
- data/lib/keisan/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2a155be79ae75b3c67a097791c2af2bf67196cae16ef5372049b4eb54884117
|
4
|
+
data.tar.gz: b5d38c6111d1803a14c0229b0f54dc8733d581409254068ac9970af1321d9fca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
29
|
-
@
|
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
|
-
|
48
|
-
|
49
|
-
|
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]]
|
data/lib/keisan/tokenizer.rb
CHANGED
@@ -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.
|
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
|
data/lib/keisan/version.rb
CHANGED
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
|
+
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-
|
11
|
+
date: 2020-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cmath
|