jaina 0.2.0 → 0.3.0

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: 021a99a70e1f530c83857667a7c0defdd785abc635931167fe2a9bd45bf1393f
4
- data.tar.gz: fe1956e513688b643365ed85a55a02226a18b5adc75ee202130f7b1dab8404ab
3
+ metadata.gz: 850df9cc5e4a4962b435569da878367493a801ac0f842760157083e8f5fba578
4
+ data.tar.gz: 79cb4a3a71d81205b4d6647a04de688b084b6108373d9cc274a11ca390fffadd
5
5
  SHA512:
6
- metadata.gz: 0d147dbfe65aef16511b703b52100b00f28b81695c1e18906b166180c49629cd6b8cc07ccf362022a04fc7897f0e9625e3a10969d52ad2a7f3606755941c8a6e
7
- data.tar.gz: 39ca3e5fadb5341a29f363f4ec3426395edb214c4041620ccea19282d49602edeef417b641659dc38d17e01a81b9baf696258e8f86b3455f20f4cf9979791610
6
+ metadata.gz: 5253d6c057ef220178518f8793cbb7e40e091556a8efec5ee48d443c582408e2122b27cc098747f1b0d620d470604b041e1c8cf66ddd8eb7ffa4486fbd63f08a
7
+ data.tar.gz: 54d7867f5ee7ec104e2a46d8311f69c6cf3cab25b5d635a64458ee18ce1f94778cd3794425474346e371d576f4a6183f62a4363fb181ebe27507c9b36c44f231
@@ -1,6 +1,9 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.3.0] - 2019-06-21
5
+ - Support for initial context: `Jaina.evaluate(program, **initial_context)`
6
+
4
7
  ## [0.2.0] - 2019-06-21
5
8
  ### Added
6
9
  - `#evaluate(context)` implementation for all core operators (`AND`, `OR`, `NOT`);
data/README.md CHANGED
@@ -28,6 +28,7 @@ require 'jaina'
28
28
  - [Parse your code (build AST)](#parse-your-code-build-ast)
29
29
  - [Evaluate your code](#evaluate-your-code)
30
30
  - [List registered operands and operators](#list-and-fetch-registered-operands-and-operators)
31
+ - [Full example](#full-example)
31
32
 
32
33
  ---
33
34
 
@@ -117,6 +118,9 @@ ast.evaluate
117
118
 
118
119
  # --- or ---
119
120
  Jaina.evaluate('A AND B AND (C OR D) OR A AND (C OR E)')
121
+
122
+ # --- you can set initial context of your program ---
123
+ Jaina.evaluate('A AND B', login: 'admin', logged_in: true)
120
124
  ```
121
125
 
122
126
  ---
@@ -144,6 +148,53 @@ Jaina.fetch_expression("KEK")
144
148
 
145
149
  ---
146
150
 
151
+ ### Full example
152
+
153
+ ```ruby
154
+ # step 1: register new operand
155
+ class AddNumber < Jaina::TerminalExpr
156
+ token 'ADD'
157
+
158
+ def evaluate(context)
159
+ context.set(:current_value, context.get(:current_value) + 10)
160
+ end
161
+ end
162
+
163
+ # step 2: register another new operand
164
+ class CheckNumber < Jaina::TerminalExpr
165
+ token 'CHECK'
166
+
167
+ def evaluate(context)
168
+ context.get(:current_value) < 0
169
+ end
170
+ end
171
+
172
+ class InitState < Jaina::TerminalExpr
173
+ token 'INIT'
174
+
175
+ def evaluate(context)
176
+ context.set(:current_value, 0)
177
+ end
178
+ end
179
+
180
+ # step 3: register new oeprands
181
+ Jaina.register_expression(AddNumber)
182
+ Jaina.register_expression(CheckNumber)
183
+ Jaina.register_expression(InitState)
184
+
185
+ # step 4: run your program
186
+
187
+ # NOTE: with initial context
188
+ Jaina.evaluate('CHECK AND ADD', current_value: -1) # => false
189
+ Jaina.evaluate('CHECK AND ADD', current_value: 2) # => 12
190
+
191
+ # NOTE: without initial context
192
+ Jaina.evaluate('INIT AND ADD') # => 10
193
+ Jaina.evaluate('INIT AND (CHECK OR ADD)') # => 12
194
+ ```
195
+
196
+ ---
197
+
147
198
  ## Contributing
148
199
 
149
200
  - Fork it ( https://github.com/0exp/jaina/fork )
@@ -24,12 +24,13 @@ module Jaina
24
24
  end
25
25
 
26
26
  # @param program [String]
27
+ # @param initial_context [Hash<Symbol,Any>]
27
28
  # @return [Any]
28
29
  #
29
30
  # @api public
30
31
  # @since 0.1.0
31
- def evaluate(program)
32
- parse(program).evaluate
32
+ def evaluate(program, **initial_context)
33
+ parse(program).evaluate(**initial_context)
33
34
  end
34
35
 
35
36
  # @param expression_klass [Class{Jaina::Parser::Expression::Operator::Abstract}]
@@ -49,11 +49,12 @@ class Jaina::Parser::AST
49
49
  @ast_tree = ast_tree
50
50
  end
51
51
 
52
+ # @param initial_context [Hash<Symbol,Any>]
52
53
  # @return [Any]
53
54
  #
54
55
  # @api private
55
56
  # @since 0.1.0
56
- def evaluate
57
- Jaina::Parser::AST::Evaluator.evaluate(self)
57
+ def evaluate(**initial_context)
58
+ Jaina::Parser::AST::Evaluator.evaluate(self, **initial_context)
58
59
  end
59
60
  end
@@ -8,12 +8,13 @@ class Jaina::Parser::AST::Context
8
8
  # @since 0.1.0
9
9
  UndefinedContextKeyError = Class.new(Error)
10
10
 
11
+ # @param initial_context [Hash<Symbol,Any>]
11
12
  # @return [void]
12
13
  #
13
14
  # @api private
14
15
  # @since 0.1.0
15
- def initialize
16
- @data = {}
16
+ def initialize(**initial_context)
17
+ @data = {}.merge!(initial_context)
17
18
  @access_lock = Mutex.new
18
19
  end
19
20
 
@@ -4,17 +4,18 @@
4
4
  # @since 0.1.0
5
5
  module Jaina::Parser::AST::Evaluator
6
6
  class << self
7
+ # @param initial_context [Hash<Symbol,Any>]
7
8
  # @param ast [Jaina::Parser::AST]
8
9
  # @return [Any]
9
10
  #
10
11
  # @api private
11
12
  # @since 0.1.0
12
- def evaluate(ast)
13
+ def evaluate(ast, **initial_context)
13
14
  # NOTE: build shared context for a program
14
- context = Jaina::Parser::AST::Context.new
15
+ context = Jaina::Parser::AST::Context.new(**initial_context)
15
16
 
16
17
  # NOTE: evaluate the root expression of AST
17
- # NOTE: root is an atity of type [Jaina::Parser::Expression::Operator::Abstract]
18
+ # NOTE: root is an entity of type [Jaina::Parser::Expression::Operator::Abstract]
18
19
  ast.root.evaluate(context)
19
20
  end
20
21
  end
@@ -21,8 +21,9 @@ class Jaina::Parser::AST::Tree
21
21
  # @since 0.1.0
22
22
  attr_reader :expression
23
23
 
24
- # @param initial_program [String]
25
- # @param expression [Jaina::Parser::Expression::Operator::Abstract]
24
+ # @option initial_program [String]
25
+ # @option ast_oriented_program [String]
26
+ # @option expression [Jaina::Parser::Expression::Operator::Abstract]
26
27
  # @return [void]
27
28
  #
28
29
  # @api private
@@ -36,6 +37,6 @@ class Jaina::Parser::AST::Tree
36
37
  # @return [Jaina::Parser::Expression::operator::Abstract]
37
38
  #
38
39
  # @api private
39
- # @since 0.1.0
40
+ # @since 0.3.0
40
41
  alias_method :root, :expression
41
42
  end
@@ -23,7 +23,7 @@ module Jaina::Parser::Expression::Operator
23
23
  # @api private
24
24
  # @since 0.2.0
25
25
  def left_expression
26
- expressions.first
26
+ expressions[0]
27
27
  end
28
28
 
29
29
  # @return [Jaina::Parser::Expression::Operator::Abstract]
@@ -31,7 +31,7 @@ module Jaina::Parser::Expression::Operator
31
31
  # @api private
32
32
  # @since 0.2.0
33
33
  def right_expression
34
- expressions.second
34
+ expressions[1]
35
35
  end
36
36
  end
37
37
  end
@@ -5,5 +5,5 @@ module Jaina
5
5
  #
6
6
  # @api public
7
7
  # @since 0.0.0
8
- VERSION = '0.2.0'
8
+ VERSION = '0.3.0'
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Ibragimov