keisan 0.8.10 → 0.8.11

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: 9f97e020e01a4789ca6f181ac1cd2556d4338297d0d59ed89e4c6e29cf90fe27
4
- data.tar.gz: 3b06e9483c5f7ac0f38b32ce925a27b06c0de2e8df8cbe5b45b5262c341bdf80
3
+ metadata.gz: 982db21218a4096daa15314fb7de88f1710e1a82be5b122c1b1c4a76323fb44a
4
+ data.tar.gz: 4df1eec9b4afd5da69db4a6d40d6900dc4bd9bd249dd1b67646766ba8468fd36
5
5
  SHA512:
6
- metadata.gz: d2814acec39cba71faa5c9e3ce924ff17361b826fd092c01a82a54097992be7d28238ee127ada33f5577496ce573cd3da9947c65a6a09956ece8f66a524dd58a
7
- data.tar.gz: a64666d0b022ca0352054b2a8c2c9d1e7cd0b01b4a569e52f28d86fd034103360a854d7eef60da0d0f8452c959b6859a051446762059457f6bf155b2ac6868f5
6
+ metadata.gz: bcc26f352e4f13139a9f0e2c0e1e97be8b3801ca6c2590349aad746fb1283776bf14d4f8ff4eb98049dfd624e7d1b71ce5be5770080f3187a4349c01a5fba4a6
7
+ data.tar.gz: 70e240222845d98e231ceb577939a37020b03ba1e3a5b9b88165a3c62ce4ffb6d5aa16cff0d4d2eed3078746b4595d3e104a2545b09f55f6d5b784dba1307bf1
data/README.md CHANGED
@@ -83,6 +83,18 @@ calculator.evaluate("3*x + 1")
83
83
  #=> 61
84
84
  ```
85
85
 
86
+ To perform multiple assignments, lists can be used
87
+
88
+ ```ruby
89
+ calculator = Keisan::Calculator.new
90
+ calculator.evaluate("x = [1, 2]")
91
+ calculator.evaluate("[x[1], y] = [11, 22]")
92
+ calculator.evaluate("x")
93
+ #=> [1, 11]
94
+ calculator.evaluate("y")
95
+ #=> 22
96
+ ```
97
+
86
98
 
87
99
  ##### Specifying functions
88
100
 
@@ -1,5 +1,6 @@
1
1
  require_relative "variable_assignment"
2
2
  require_relative "function_assignment"
3
+ require_relative "list_assignment"
3
4
  require_relative "cell_assignment"
4
5
 
5
6
  module Keisan
@@ -31,6 +32,8 @@ module Keisan
31
32
  evaluate_variable_assignment(context, lhs, rhs)
32
33
  elsif is_function_definition?
33
34
  evaluate_function_assignment(context, lhs, rhs)
35
+ elsif is_list_assignment?
36
+ evaluate_list_assignment(context, lhs, rhs)
34
37
  else
35
38
  # Try cell assignment
36
39
  evaluate_cell_assignment(context, lhs, rhs)
@@ -71,6 +74,10 @@ module Keisan
71
74
  children.first.is_a?(Function)
72
75
  end
73
76
 
77
+ def is_list_assignment?
78
+ children.first.is_a?(List)
79
+ end
80
+
74
81
  private
75
82
 
76
83
  def evaluate_variable_assignment(context, lhs, rhs)
@@ -82,6 +89,10 @@ module Keisan
82
89
  FunctionAssignment.new(context, lhs, rhs, local).evaluate
83
90
  end
84
91
 
92
+ def evaluate_list_assignment(context, lhs, rhs)
93
+ ListAssignment.new(self, context, lhs, rhs).evaluate
94
+ end
95
+
85
96
  def evaluate_cell_assignment(context, lhs, rhs)
86
97
  CellAssignment.new(self, context, lhs, rhs).evaluate
87
98
  end
@@ -0,0 +1,38 @@
1
+ module Keisan
2
+ module AST
3
+ class ListAssignment
4
+ attr_reader :assignment, :context, :lhs, :rhs
5
+
6
+ def initialize(assignment, context, lhs, rhs)
7
+ @assignment = assignment
8
+ @context = context
9
+ @lhs = lhs
10
+ @rhs = rhs
11
+ end
12
+
13
+ def evaluate
14
+ rhs = @rhs.evaluate(context)
15
+
16
+ if !rhs.is_a?(List)
17
+ raise Exceptions::InvalidExpression.new("To do multiple assignment, RHS must be a list")
18
+ end
19
+ if lhs.children.size != rhs.children.size
20
+ raise Exceptions::InvalidExpression.new("To do multiple assignment, RHS list must have same length as LHS list")
21
+ end
22
+
23
+ i = 0
24
+ while i < lhs.children.size
25
+ lhs_variable = lhs.children[i]
26
+ rhs_assignment = rhs.children[i]
27
+ individual_assignment = Assignment.new(
28
+ children = [lhs_variable, rhs_assignment],
29
+ local: assignment.local,
30
+ compound_operator: assignment.compound_operator
31
+ )
32
+ individual_assignment.evaluate(context)
33
+ i += 1
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,5 @@
1
+ require "set"
2
+
1
3
  module Keisan
2
4
  module Variables
3
5
  class Registry
@@ -5,7 +7,7 @@ module Keisan
5
7
 
6
8
  def initialize(variables: {}, shadowed: [], parent: nil, use_defaults: true, force: false)
7
9
  @hash = {}
8
- @shadowed = Set.new(shadowed.map(&:to_s))
10
+ @shadowed = ::Set.new(shadowed.map(&:to_s))
9
11
  @parent = parent
10
12
  @use_defaults = use_defaults
11
13
 
@@ -1,3 +1,3 @@
1
1
  module Keisan
2
- VERSION = "0.8.10"
2
+ VERSION = "0.8.11"
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.10
4
+ version: 0.8.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-19 00:00:00.000000000 Z
11
+ date: 2021-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cmath
@@ -151,6 +151,7 @@ files:
151
151
  - lib/keisan/ast/indexing.rb
152
152
  - lib/keisan/ast/line_builder.rb
153
153
  - lib/keisan/ast/list.rb
154
+ - lib/keisan/ast/list_assignment.rb
154
155
  - lib/keisan/ast/literal.rb
155
156
  - lib/keisan/ast/logical_and.rb
156
157
  - lib/keisan/ast/logical_equal.rb