ascode 0.2.2 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c30a8867f452240b259b57822fb28a74100231bfc828f59f2aedf513d1f29db
4
- data.tar.gz: 220987caf6add5de1c9e698b1f9b6c3fbf8c36b16de0ca56bd43562759726180
3
+ metadata.gz: be5ffda76c2c8c88b0a89678a05730f7497fc8752208b3a41dc2523d81e62e46
4
+ data.tar.gz: a510b069ecb83cf610dd696c3d3b55abf761062f4296329b1ef012a67cfa21af
5
5
  SHA512:
6
- metadata.gz: 01c54a8516ecdf874871129971f409f1d44644d869f30c72b96f2a77408762ad7cf1f88036d1a79e9c9dd78c0771712d00ce8620c0d04d62e8a5301239d7a18d
7
- data.tar.gz: 570d22a8acf8466cfbaec23cd87fe31b4bf2bf1f8eab58b39f5a65bb6060889430570b16b1e916544f140273f920bd72cc05f93aa441ff016c3fc8b5a66bfdf5
6
+ metadata.gz: f18a849a4413d3182427a88f98da6736712443915a199e320ac857b79e2bb436807b977920a21cad5d527673382d8f79dc3f99df789955017a25d38e59b87214
7
+ data.tar.gz: 6a10e1ef202258ebb4414e922e4a97e69d286b8524c7c46cc34000650ecb78d4a7e611079aa09ae6c7224e38fdb8d34fd0ba0b67b786a53314adf1a1b78e7e72
data/README.md CHANGED
@@ -27,9 +27,9 @@ At this moment, the only way to play with `ascode` is `irb`.
27
27
  $ irb
28
28
  irb(main):001:0> require 'ascode'
29
29
  => true
30
- irb(main):002:0> Ascode.run('5')
31
- 123
32
- -615
30
+ irb(main):002:0> Ascode.run '5*ê[Even~Odd]'
31
+ 10
32
+ Even
33
33
  ```
34
34
 
35
35
  ### Features
@@ -3,6 +3,9 @@ Character|Name|`pop`s|`push`es|Description
3
3
  `>`|`output`|`a`||`a` -> `stdout`
4
4
  `<`|`input`||`a`|`stdin` -> `a`
5
5
  `^`|`pop`|`a`||
6
+ `[`|`condition_begin`|`a`|| If `a` = `0`, `nil` or `false`, jump to `~`, or to `]` if `~` is not present
7
+ `~`|`condition_else`|||
8
+ `]`|`condition_end`|||
6
9
  `+`|`plus`|`a`, `b`|`c`|`c` = `a` + `b`
7
10
  `-`|`minus`|`a`, `b`|`c`|`c` = `a` - `b`
8
11
  `/`|`divide`|`a`, `b`|`c`|`c` = `a` / `b`
@@ -4,6 +4,8 @@ module Ascode
4
4
  class Interpreter
5
5
  def initialize(ast)
6
6
  @ast = ast
7
+ @conditions = []
8
+ @cond_positions = []
7
9
 
8
10
  @stack = []
9
11
  end
@@ -11,13 +13,12 @@ module Ascode
11
13
  def run
12
14
  @ast.each do |action|
13
15
  name = action[:action]
14
- what = action[:what]
15
16
 
16
- if name == 'push'
17
- push what
18
- else
19
- send(name)
17
+ if !@conditions.empty? && !%w[condition_else condition_end].include?(name)
18
+ next if @conditions[0] != @cond_positions[0]
20
19
  end
20
+
21
+ name == 'push' ? push(action[:what]) : send(name)
21
22
  end
22
23
  end
23
24
 
@@ -25,11 +26,11 @@ module Ascode
25
26
  @stack.push what
26
27
  end
27
28
 
28
- def pop
29
+ def pop(ask = true)
29
30
  value = @stack.pop
30
31
  if value
31
32
  value
32
- else
33
+ elsif ask
33
34
  input
34
35
  @stack.pop
35
36
  end
@@ -40,7 +41,21 @@ module Ascode
40
41
  end
41
42
 
42
43
  def output
43
- puts pop
44
+ puts pop(false)
45
+ end
46
+
47
+ def condition_begin
48
+ condition = pop
49
+ @conditions.push(!(condition.nil? || !condition || condition.zero?))
50
+ @cond_positions.push true
51
+ end
52
+
53
+ def condition_else
54
+ @cond_positions[0] = false
55
+ end
56
+
57
+ def condition_end
58
+ @conditions.pop
44
59
  end
45
60
 
46
61
  def plus
data/lib/ascode.rb CHANGED
@@ -10,4 +10,3 @@ module Ascode
10
10
  interpreter.run
11
11
  end
12
12
  end
13
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ascode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Varaksa