File: lang.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (74 lines) | stat: -rwxr-xr-x 1,434 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# The aim of these tests is to detect pitfall for optimized VM.

# Test for or/and
#
# You may think instruction fusion(OP_EQ and OP_JMPIF) for avoiding
# generate intermediate boolean value.
# But and/or is pitfall for this fusioning.
#
# For example, the following mruby code:
#
#   if i > 0 and i < 10
#
# compiles to the following byte code:
#
#    1 000 LOADI_0      R1      (0)             ; R1:i
#    2 002 MOVE         R2      R1              ; R1:i
#    2 005 LOADI_0      R3      (0)
#    2 007 GT     R2      R3
#    2 009 JMPNOT       R2      021
#    2 013 MOVE         R2      R1              ; R1:i
#    2 016 LOADI8       R3      10
#    2 019 LT     R2      R3
#    2 021 JMPNOT       R2      (The address of end of then part)
#
# When the instruction fusion the OP_GT and OP_JMPNOT you fell into the pitfalls.
# The deleted intermediate boolean value is used in OP_JMPNOT (address 008).

assert('and', '11.2.3') do
  a = 1
  if a > 0 and a < 10
    b = 1
  else
    b = 0
  end
  assert_equal 1, b

  if a < 0 and a < 10
    b = 1
  else
    b = 0
  end
  assert_equal 0, b

  if a < 0 and a > 10
    b = 1
  else
    b = 0
  end
  assert_equal 0, b
end

assert('or','11.2.4') do
  a = 1
  if a > 0 or a < 10
    b = 1
  else
    b = 0
  end
  assert_equal 1, b

  if a < 0 or a < 10
    b = 1
  else
    b = 0
  end
  assert_equal 1, b

  if a < 0 or a > 10
    b = 1
  else
    b = 0
  end
  assert_equal 0, b
end