jaina 0.2.0 → 0.3.0
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/CHANGELOG.md +3 -0
- data/README.md +51 -0
- data/lib/jaina.rb +3 -2
- data/lib/jaina/parser/ast.rb +3 -2
- data/lib/jaina/parser/ast/context.rb +3 -2
- data/lib/jaina/parser/ast/evaluator.rb +4 -3
- data/lib/jaina/parser/ast/tree.rb +4 -3
- data/lib/jaina/parser/expression/operator/or.rb +2 -2
- data/lib/jaina/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 850df9cc5e4a4962b435569da878367493a801ac0f842760157083e8f5fba578
|
4
|
+
data.tar.gz: 79cb4a3a71d81205b4d6647a04de688b084b6108373d9cc274a11ca390fffadd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5253d6c057ef220178518f8793cbb7e40e091556a8efec5ee48d443c582408e2122b27cc098747f1b0d620d470604b041e1c8cf66ddd8eb7ffa4486fbd63f08a
|
7
|
+
data.tar.gz: 54d7867f5ee7ec104e2a46d8311f69c6cf3cab25b5d635a64458ee18ce1f94778cd3794425474346e371d576f4a6183f62a4363fb181ebe27507c9b36c44f231
|
data/CHANGELOG.md
CHANGED
@@ -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 )
|
data/lib/jaina.rb
CHANGED
@@ -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}]
|
data/lib/jaina/parser/ast.rb
CHANGED
@@ -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
|
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
|
-
# @
|
25
|
-
# @
|
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.
|
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
|
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
|
34
|
+
expressions[1]
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
data/lib/jaina/version.rb
CHANGED