ascode 0.5.2 → 0.5.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: 2bb261a46eb4fafcf1993ea9424ecd61e1b0563bb7442b19f0bc36dbc3aeac00
4
- data.tar.gz: 57ce7e23f1fcb9846dc1e081a67b0904e7b5f45adf9ad4e31775ec46e133c273
3
+ metadata.gz: d5f4408a0db6f51c967a99742111084fa89aac0c41237313ccbfed61e9d64fe6
4
+ data.tar.gz: b1ff0ca8a235111caaa549b768f0d4444480e3d15910c8585e6029b71f44f0bf
5
5
  SHA512:
6
- metadata.gz: 60f714e57e55ef148f53ceb7491157bf939271cf39748ec6e0025ccfce42c6247b0c7233a125c0779075faf921a6e26e492679d09f4db28c679cd3ca8e944249
7
- data.tar.gz: 5266a0121fe8e7c11db83a78dc9d35565b02736e9ad58081b8e741a49a6a5207885eb541016170d0b452909fc39f51dea5ad886e6b81246742a8c00a5a905c94
6
+ metadata.gz: fae634110d25627412c71d7d30f6c87cea3f37764db75149f937387f82b03250306d6822cb9482f8e99ce8415fd0fc2eab80366fca99a00e1b945f83d1a94b31
7
+ data.tar.gz: af4ffcf2b5779e69583d54e935dbfcc7ffa0aad67d76d2b3694e00b743566aee3ede7c0f748a7908867e85ee93d6cc4996bd87ed10cde69a593a402a27106bbe
@@ -0,0 +1,14 @@
1
+ module Ascode
2
+ module Interpreter
3
+ class Condition
4
+ def self.begin(env, true_ast, false_ast)
5
+ condition = env.pop
6
+ ast = condition.nil? || !condition || condition.zero? ? false_ast : true_ast
7
+
8
+ (Main.new ast, env).run
9
+ end
10
+
11
+ def self.end(_); end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "io"
2
+
3
+ module Ascode
4
+ module Interpreter
5
+ class Environment
6
+ attr_accessor :ast, :stack
7
+
8
+ def initialize(ast = "")
9
+ @ast = ast
10
+ @stack = []
11
+ end
12
+
13
+ def push(what)
14
+ @stack.push what
15
+ end
16
+
17
+ def pop(ask = true)
18
+ value = @stack.pop
19
+ if value
20
+ value
21
+ elsif ask
22
+ IO.input itself
23
+ @stack.pop
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "../converter"
2
+
3
+ module Ascode
4
+ module Interpreter
5
+ class IO
6
+ def self.input(env)
7
+ env.push Converter.convert gets
8
+ end
9
+
10
+ def self.output(env)
11
+ puts env.pop(false)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ require_relative "environment"
2
+ require_relative "condition"
3
+ require_relative "math"
4
+ require_relative "io"
5
+
6
+ module Ascode
7
+ module Interpreter
8
+ class Main
9
+ def initialize(ast, env = Environment.new)
10
+ @env = env
11
+ @env.ast = ast
12
+
13
+ @map = {
14
+ "io" => IO,
15
+ "math" => Math,
16
+ "condition" => Condition
17
+ }
18
+ end
19
+
20
+ def run
21
+ @env.ast.each do |action|
22
+ name = action[:action]
23
+
24
+ if name == "push"
25
+ @env.push(action[:what])
26
+ elsif name == "condition"
27
+ Condition.begin @env, action[:true_block], action[:false_block]
28
+ else
29
+ call action[:type], name
30
+ end
31
+ end
32
+ end
33
+
34
+ def call(type, name)
35
+ if type == "env"
36
+ @env.send name
37
+ else
38
+ @map[type].send name, @env
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,46 @@
1
+ module Ascode
2
+ module Interpreter
3
+ class Math
4
+ def self.plus(env)
5
+ a = env.pop
6
+ b = env.pop
7
+ env.push a + b
8
+ end
9
+
10
+ def self.minus(env)
11
+ a = env.pop
12
+ b = env.pop
13
+ env.push a - b
14
+ end
15
+
16
+ def self.divide(env)
17
+ a = env.pop
18
+ b = env.pop
19
+ env.push a / b
20
+ end
21
+
22
+ def self.multiply(env)
23
+ a = env.pop
24
+ b = env.pop
25
+ env.push a * b
26
+ end
27
+
28
+ def self.even(env)
29
+ env.push env.pop.even? ? 1 : 0
30
+ end
31
+
32
+ def self.invert(env)
33
+ a = env.pop
34
+ if a.zero?
35
+ env.push 1
36
+ else
37
+ env.push 0
38
+ end
39
+ end
40
+
41
+ def self.change_sign(env)
42
+ env.push(-1 * env.pop)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -20,9 +20,10 @@ module Ascode
20
20
  value[:short] == char
21
21
  end
22
22
 
23
- raise "Unexpected function: '#{char}'" unless function
23
+ return unless function
24
24
 
25
25
  {
26
+ type: function[:type],
26
27
  action: function[:long]
27
28
  }
28
29
  end
@@ -33,7 +34,8 @@ module Ascode
33
34
 
34
35
  @csv_data.push(
35
36
  short: clear_quotes(src[0]),
36
- long: clear_quotes(src[1])
37
+ type: clear_quotes(src[1]),
38
+ long: clear_quotes(src[2])
37
39
  )
38
40
  end
39
41
  end
@@ -1,15 +1,15 @@
1
- Character|Name|`pop`s|`push`es|Description
2
- ---|---|---|---|---
3
- `I`|`input`||`a`|`stdin` -> `a`
4
- `O`|`output`|`a`||`a` -> `stdout`
5
- `P`|`pop`|`a`||
6
- `[`|`condition_begin`|`a`|| If `a` = `0`, `nil` or `false`, then part after `:` is executed
7
- `:`|`condition_else`|||
8
- `]`|`condition_end`|||
9
- `+`|`plus`|`a`, `b`|`c`|`c` = `a` + `b`
10
- `-`|`minus`|`a`, `b`|`c`|`c` = `a` - `b`
11
- `/`|`divide`|`a`, `b`|`c`|`c` = `a` / `b`
12
- `*`|`multiply`|`a`, `b`|`c`|`c` = `a` * `b`
13
- `e`|`even`|`a`|`b`|`b` = `1` if `a` is even, `0` otherwise
14
- `!`|`invert`|`a`|`b`|`b` = `1` if `a` = `0`, otherwise `0`
15
- `s`|`change_sign`|`a`|`b`|`b` = `-1` * `a`
1
+ Character|Type|Name|`pop`s|`push`es|Description
2
+ ---|---|---|---|---|---
3
+ `I`|`io`|`input`||`a`|`stdin` -> `a`
4
+ `O`|`io`|`output`|`a`||`a` -> `stdout`
5
+ `P`|`env`|`pop`|`a`||
6
+ `[`|`condition`|`begin`|`a`|| If `a` = `0`, `nil` or `false`, then part after `:` is executed
7
+ `:`|`condition`|`else`|||
8
+ `]`|`condition`|`end`|||
9
+ `+`|`math`|`plus`|`a`, `b`|`c`|`c` = `a` + `b`
10
+ `-`|`math`|`minus`|`a`, `b`|`c`|`c` = `a` - `b`
11
+ `/`|`math`|`divide`|`a`, `b`|`c`|`c` = `a` / `b`
12
+ `*`|`math`|`multiply`|`a`, `b`|`c`|`c` = `a` * `b`
13
+ `e`|`math`|`even`|`a`|`b`|`b` = `1` if `a` is even, `0` otherwise
14
+ `!`|`math`|`invert`|`a`|`b`|`b` = `1` if `a` = `0`, otherwise `0`
15
+ `s`|`math`|`change_sign`|`a`|`b`|`b` = `-1` * `a`
@@ -26,7 +26,7 @@ module Ascode
26
26
  character char, index
27
27
  end
28
28
 
29
- @ast.push(action: "output") if @implicit_output
29
+ @ast.push(type: "io", action: "output") if @implicit_output
30
30
 
31
31
  @ast
32
32
  end
@@ -44,7 +44,7 @@ module Ascode
44
44
  end
45
45
 
46
46
  def literal(index, num)
47
- add = num ? 0 : 1
47
+ add = num ? -1 : 1
48
48
 
49
49
  literal = Literal.new @code, index
50
50
  @ast.push literal.parse
@@ -59,6 +59,7 @@ module Ascode
59
59
 
60
60
  def function(char)
61
61
  function = @function.parse char
62
+ return unless function
62
63
  @implicit_output = false if function[:action] == "output"
63
64
  @ast.push function
64
65
  end
data/lib/ascode.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative "ascode/interpreter"
1
+ require_relative "ascode/interpreter/main"
2
2
  require_relative "ascode/parser/main"
3
3
 
4
4
  module Ascode
@@ -6,7 +6,7 @@ module Ascode
6
6
  parser = Parser::Main.new code
7
7
  ast = parser.parse
8
8
 
9
- interpreter = Interpreter.new ast
9
+ interpreter = Interpreter::Main.new ast
10
10
  interpreter.run
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ascode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Varaksa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-17 00:00:00.000000000 Z
11
+ date: 2018-01-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Esoteric golfing language. In active development, so behavior may be
14
14
  changed without prior notice.
@@ -21,7 +21,11 @@ files:
21
21
  - README.md
22
22
  - lib/ascode.rb
23
23
  - lib/ascode/converter.rb
24
- - lib/ascode/interpreter.rb
24
+ - lib/ascode/interpreter/condition.rb
25
+ - lib/ascode/interpreter/environment.rb
26
+ - lib/ascode/interpreter/io.rb
27
+ - lib/ascode/interpreter/main.rb
28
+ - lib/ascode/interpreter/math.rb
25
29
  - lib/ascode/parser/condition_block.rb
26
30
  - lib/ascode/parser/function.rb
27
31
  - lib/ascode/parser/functions.md
@@ -1,99 +0,0 @@
1
- require_relative "converter"
2
-
3
- module Ascode
4
- class Interpreter
5
- def initialize(ast, stack = [])
6
- @ast = ast
7
-
8
- @stack = stack
9
- end
10
-
11
- def run
12
- @ast.each do |action|
13
- name = action[:action]
14
-
15
- if name == "push"
16
- push(action[:what])
17
- elsif name == "condition"
18
- condition_begin action[:true_block], action[:false_block]
19
- else
20
- send(name)
21
- end
22
- end
23
- end
24
-
25
- def push(what)
26
- @stack.push what
27
- end
28
-
29
- def pop(ask = true)
30
- value = @stack.pop
31
- if value
32
- value
33
- elsif ask
34
- input
35
- @stack.pop
36
- end
37
- end
38
-
39
- def input
40
- push Converter.convert gets
41
- end
42
-
43
- def output
44
- puts pop(false)
45
- end
46
-
47
- def condition_begin(true_ast, false_ast)
48
- condition = pop
49
- ast = condition.nil? || !condition || condition.zero? ? false_ast : true_ast
50
-
51
- (Interpreter.new ast, @stack).run
52
- end
53
-
54
- def condition_else; end
55
-
56
- def condition_end; end
57
-
58
- def plus
59
- a = pop
60
- b = pop
61
- push a + b
62
- end
63
-
64
- def minus
65
- a = pop
66
- b = pop
67
- push a - b
68
- end
69
-
70
- def divide
71
- a = pop
72
- b = pop
73
- push a / b
74
- end
75
-
76
- def multiply
77
- a = pop
78
- b = pop
79
- push a * b
80
- end
81
-
82
- def even
83
- push pop.even? ? 1 : 0
84
- end
85
-
86
- def invert
87
- a = pop
88
- if a.zero?
89
- push 1
90
- else
91
- push 0
92
- end
93
- end
94
-
95
- def change_sign
96
- push(-1 * pop)
97
- end
98
- end
99
- end