Skip to content

Commit 5fbce43

Browse files
committed
Added some examples from my blog post, added more normal "newarray" instruction like "anewarray" form.
1 parent b05d68c commit 5fbce43

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

examples/hello_world.bs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
main do
2+
ldc "Hello, world!"
3+
aprintln
4+
returnvoid
5+
end

examples/hello_world_macro.bs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.lang.System
2+
import java.io.PrintStream
3+
4+
macro :aprintln do
5+
getstatic System, :out, PrintStream
6+
swap
7+
invokevirtual PrintStream, println, [Object]
8+
end
9+
10+
macro :aprint do
11+
getstatic System, :out, PrintStream
12+
swap
13+
invokevirtual PrintStream, print, [Object]
14+
end
15+
16+
main do
17+
ldc "Hello, "
18+
aprint
19+
aload 0
20+
aaload 0
21+
aprintln
22+
returnvoid
23+
end

examples/using_ruby.bs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
main do
2+
5.times do
3+
ldc "Wow!"
4+
aprintln
5+
end
6+
returnvoid
7+
end

lib/bitescript/bytecode.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,28 @@ def #{const_down}(type)
191191

192192
when "NEWARRAY"
193193
# newaray has its own peculiarities
194-
{
195-
:boolean => 'Opcodes::T_BOOLEAN',
196-
:byte => 'Opcodes::T_BYTE',
197-
:short => 'Opcodes::T_SHORT',
198-
:char => 'Opcodes::T_CHAR',
199-
:int => 'Opcodes::T_INT',
200-
:long => 'Opcodes::T_LONG',
201-
:float => 'Opcodes::T_FLOAT',
202-
:double => 'Opcodes::T_DOUBLE'
203-
}.each do |type, op_type|
194+
NEWARRAY_TYPES = {
195+
:boolean => Opcodes::T_BOOLEAN,
196+
:byte => Opcodes::T_BYTE,
197+
:short => Opcodes::T_SHORT,
198+
:char => Opcodes::T_CHAR,
199+
:int => Opcodes::T_INT,
200+
:long => Opcodes::T_LONG,
201+
:float => Opcodes::T_FLOAT,
202+
:double => Opcodes::T_DOUBLE
203+
}
204+
NEWARRAY_TYPES.each do |type, op_type|
204205
line = __LINE__; eval "
205206
def new#{type}array()
206207
method_visitor.visit_int_insn(Opcodes::#{const_name}, #{op_type})
207208
#{OpcodeStackDeltas[const_name]}
208209
end
209210
", b, __FILE__, line
210211
end
212+
def newarray(type)
213+
t_type = NEWARRAY_TYPES[path(type).intern]
214+
method_visitor.visit_int_insn(Opcodes::NEWARRAY, t_type)
215+
end
211216
OpcodeInstructions[const_name] = const_down
212217

213218
when "GETFIELD", "PUTFIELD", "GETSTATIC", "PUTSTATIC"

test/test_bytecode.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ def test_newarray_insns
285285
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_LONG], @dummy.single {newlongarray})
286286
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_FLOAT], @dummy.single {newfloatarray})
287287
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_DOUBLE], @dummy.single {newdoublearray})
288+
289+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_BOOLEAN], @dummy.single {newarray Java::boolean})
290+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_BYTE], @dummy.single {newarray Java::byte})
291+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_SHORT], @dummy.single {newarray Java::short})
292+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_CHAR], @dummy.single {newarray Java::char})
293+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_INT], @dummy.single {newarray Java::int})
294+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_LONG], @dummy.single {newarray Java::long})
295+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_FLOAT], @dummy.single {newarray Java::float})
296+
assert_equal([:visit_int_insn, Opcodes::NEWARRAY, Opcodes::T_DOUBLE], @dummy.single {newarray Java::double})
288297
end
289298

290299
def test_field_insns

0 commit comments

Comments
 (0)