yruby 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7ea42800360e25a4e07044414522e6db405ac3a49c638de709682a37e179e4a
4
- data.tar.gz: '06669269c7f3bdddd63984276ef5e0efd88cbcd704e47881e79936c2496ab937'
3
+ metadata.gz: acc6d7a62450c4b7c552c9b1e7d12d796fcf4bde1bad72a920c1b30a782c158e
4
+ data.tar.gz: 4ddf85286c286227289aa947a4ca6af499305e3b1855beb318998de69c676114
5
5
  SHA512:
6
- metadata.gz: 3f301b9319a04cf8fb011d170b35c791e980a4b613e8a67a32d1469c50aef0b78d1f18570ef1af98d03cb584bfb9ddd424bfc7bb25cc101fddaf7e89a744b016
7
- data.tar.gz: 4b97d1b57a82adc478e482b27e18c9ba4dd4d6bb80f8105efd0a8bb717075f7f16cf4917b7298c5aeb78f2a69c833478b8f901abfa830c76b7e888aebb1e8d59
6
+ metadata.gz: 7c4a5abaee620ef6456d66c08123ef66810fe0873cf39200d44b988f6fca23d70a6e4d2d263d45690155c98369544a12802d789fe1bd921a502c5b467ba7f8bc
7
+ data.tar.gz: 4d98deed29752910093463e7778f028c66704e64b468ce29c369c6bb66c486032cebe47c858b39081239c0492892e70f0c5969f7ae1fe3119057c4c806cdf7a2
data/README.md CHANGED
@@ -133,6 +133,7 @@ puts iseq.disasm
133
133
  | Integer literals | `42`, `0`, `-1` |
134
134
  | Boolean / nil literals | `true`, `false`, `nil` |
135
135
  | Arithmetic operators | `a + b`, `a - b`, `a * b`, `a / b` |
136
+ | Comparison operators | `a == b`, `a != b`, `a < b`, `a <= b`, `a > b`, `a >= b` |
136
137
  | Local variable read/write | `a = 1; a` |
137
138
  | `if` / `else` / `elsif` | `if cond; ...; elsif ...; else; ...; end` |
138
139
  | `if` as expression | `x = if true; 1; else; 2; end` |
@@ -156,6 +157,12 @@ YRuby implements the following YARV-like instructions:
156
157
  | `opt_minus` | — | Pop two values and push their difference |
157
158
  | `opt_mult` | — | Pop two values and push their product |
158
159
  | `opt_div` | — | Pop two values and push their quotient |
160
+ | `opt_eq` | — | Pop two values and push equality result |
161
+ | `opt_neq` | — | Pop two values and push inequality result |
162
+ | `opt_lt` | — | Pop two values and push less-than result |
163
+ | `opt_le` | — | Pop two values and push less-than-or-equal result |
164
+ | `opt_gt` | — | Pop two values and push greater-than result |
165
+ | `opt_ge` | — | Pop two values and push greater-than-or-equal result |
159
166
  | `branchunless` | `offset` | Jump if the popped value is falsy |
160
167
  | `jump` | `offset` | Unconditional jump |
161
168
  | `definemethod` | `mid, iseq` | Register a method on the current object |
data/lib/yruby/compile.rb CHANGED
@@ -72,25 +72,30 @@ class YRuby
72
72
  def compile_call_node(iseq, node)
73
73
  if node.receiver.nil?
74
74
  iseq.emit(YRuby::Insns::Putself)
75
- argc = 0
76
- if node.arguments
77
- compile_node(iseq, node.arguments)
78
- argc = node.arguments.arguments.size
79
- end
80
- cd = CallData.new(mid: node.name, argc:)
81
- iseq.emit(YRuby::Insns::OptSendWithoutBlock, cd)
82
75
  else
83
76
  compile_node(iseq, node.receiver)
77
+ end
78
+
79
+ argc = 0
80
+ if node.arguments
84
81
  compile_node(iseq, node.arguments)
82
+ argc = node.arguments.arguments.size
83
+ end
85
84
 
86
- case node.name
87
- when :+; iseq.emit(YRuby::Insns::OptPlus)
88
- when :-; iseq.emit(YRuby::Insns::OptMinus)
89
- when :*; iseq.emit(YRuby::Insns::OptMult)
90
- when :/; iseq.emit(YRuby::Insns::OptDiv)
91
- else
92
- raise "Unknown operator: #{node.name}"
93
- end
85
+ case node.name
86
+ when :+; iseq.emit(YRuby::Insns::OptPlus)
87
+ when :-; iseq.emit(YRuby::Insns::OptMinus)
88
+ when :*; iseq.emit(YRuby::Insns::OptMult)
89
+ when :/; iseq.emit(YRuby::Insns::OptDiv)
90
+ when :==; iseq.emit(YRuby::Insns::OptEq)
91
+ when :!=; iseq.emit(YRuby::Insns::OptNeq)
92
+ when :<; iseq.emit(YRuby::Insns::OptLt)
93
+ when :<=; iseq.emit(YRuby::Insns::OptLe)
94
+ when :>; iseq.emit(YRuby::Insns::OptGt)
95
+ when :>=; iseq.emit(YRuby::Insns::OptGe)
96
+ else
97
+ cd = CallData.new(mid: node.name, argc:)
98
+ iseq.emit(YRuby::Insns::OptSendWithoutBlock, cd)
94
99
  end
95
100
  end
96
101
 
@@ -47,7 +47,10 @@ class YRuby
47
47
  end
48
48
 
49
49
  # Control Frame
50
- def push_frame(iseq:, type: FRAME_TYPE_TOP, self_value: nil, sp:)
50
+ def push_frame(iseq:, type: FRAME_TYPE_TOP, self_value: nil, sp:, local_size:)
51
+ local_start = sp + iseq.local_table_size - local_size
52
+ local_size.times { |i| stack[local_start + i] = nil }
53
+
51
54
  sp = sp + iseq.local_table_size
52
55
  ep = sp - 1
53
56
 
@@ -86,13 +89,9 @@ class YRuby
86
89
  iseq: method_iseq,
87
90
  type: FRAME_TYPE_METHOD,
88
91
  self_value: recv,
89
- sp: argv_index
92
+ sp: argv_index,
93
+ local_size: method_iseq.local_table_size - method_iseq.argc
90
94
  )
91
-
92
- local_only_size = method_iseq.local_table_size - method_iseq.argc
93
- local_only_size.times do |i|
94
- env_write(-i, nil)
95
- end
96
95
  end
97
96
 
98
97
  def sendish(cd)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptEq < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv == arg)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptGe < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv >= arg)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptGt < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv > arg)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptLe < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv <= arg)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptLt < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv < arg)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class YRuby
4
+ module Insns
5
+ class OptNeq < Base
6
+ def self.call(vm)
7
+ recv = vm.topn(2)
8
+ arg = vm.topn(1)
9
+ vm.pop
10
+ vm.pop
11
+ vm.push(recv != arg)
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/yruby/insns.rb CHANGED
@@ -8,6 +8,12 @@ require_relative 'insns/opt_plus'
8
8
  require_relative 'insns/opt_minus'
9
9
  require_relative 'insns/opt_mult'
10
10
  require_relative 'insns/opt_div'
11
+ require_relative 'insns/opt_eq'
12
+ require_relative 'insns/opt_lt'
13
+ require_relative 'insns/opt_le'
14
+ require_relative 'insns/opt_gt'
15
+ require_relative 'insns/opt_ge'
16
+ require_relative 'insns/opt_neq'
11
17
  require_relative 'insns/getlocal'
12
18
  require_relative 'insns/setlocal'
13
19
  require_relative 'insns/dup'
data/lib/yruby/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class YRuby
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/yruby.rb CHANGED
@@ -35,7 +35,7 @@ class YRuby
35
35
  end
36
36
 
37
37
  def exec_core(iseq)
38
- push_frame(iseq:, type: FRAME_TYPE_TOP, self_value: @top_self, sp: 0)
38
+ push_frame(iseq:, type: FRAME_TYPE_TOP, self_value: @top_self, sp: 0, local_size: iseq.local_table_size)
39
39
 
40
40
  catch(:finish) do
41
41
  loop do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuhi-Sato
@@ -72,8 +72,14 @@ files:
72
72
  - lib/yruby/insns/jump.rb
73
73
  - lib/yruby/insns/leave.rb
74
74
  - lib/yruby/insns/opt_div.rb
75
+ - lib/yruby/insns/opt_eq.rb
76
+ - lib/yruby/insns/opt_ge.rb
77
+ - lib/yruby/insns/opt_gt.rb
78
+ - lib/yruby/insns/opt_le.rb
79
+ - lib/yruby/insns/opt_lt.rb
75
80
  - lib/yruby/insns/opt_minus.rb
76
81
  - lib/yruby/insns/opt_mult.rb
82
+ - lib/yruby/insns/opt_neq.rb
77
83
  - lib/yruby/insns/opt_plus.rb
78
84
  - lib/yruby/insns/opt_send_without_block.rb
79
85
  - lib/yruby/insns/putnil.rb
@@ -103,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
109
  - !ruby/object:Gem::Version
104
110
  version: '0'
105
111
  requirements: []
106
- rubygems_version: 3.6.7
112
+ rubygems_version: 3.6.9
107
113
  specification_version: 4
108
114
  summary: Yet Another Ruby VM - a YARV-based Ruby virtual machine implementation
109
115
  test_files: []