Skip to content

Commit db37f2d

Browse files
committed
added rubyish unless blocks
1 parent 309fb1c commit db37f2d

File tree

12 files changed

+30
-117
lines changed

12 files changed

+30
-117
lines changed

examples/rubyish/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ Implemented:
1616
- interpolating strings: "number #{37+5}" %Q{Hello #{planet}!}
1717
- quoted words: `%w[aa bb cc]`
1818
- scoping, including $globals, @class_instance and @@package variables
19-
- conditional blocks: `if ... then ... elsif ... else ... endif`
20-
- begin .. end blocks
19+
- conditional blocks: `if ... then ... elsif ... else ... endif`, `unless..end`
2120
- nqp opcode calls: `nqp::sleep(5)`
2221
- a few built-ins: `abort`, `print`, `puts`, `sleep`
2322
- a couple of methods: `.call` and `.nil?`

examples/rubyish/examples-rubyish/fractal-tree.rbi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
<svg width='100%' height='100%' version='1.1'
1111
xmlns='http://www.w3.org/2000/svg'>
1212
<%#----------
13+
14+
def cos(a); nqp::cos_n(a); end
15+
def sin(a); nqp::sin_n(a); end
16+
1317
@scale = 0.66
1418
@PI = 3.1415926535
1519
@eps = 2.0
1620
1721
def tree(x, y, len, angle)
1822
if len >= @eps then
19-
x2 = x + len * nqp::cos_n(angle)
20-
y2 = y + len * nqp::sin_n(angle)
23+
x2 = x + len * cos(angle)
24+
y2 = y + len * sin(angle)
2125
sw = len > 20? len / 10 : 2;
2226
g = len < @eps*2? 210: 20;
2327
puts " <line x1='#{x}' y1='#{y}' x2='#{x2}' y2='#{y2}' style='stroke:rgb(130,#{g},80);stroke-width:#{sw}'/>" \

examples/rubyish/rubyish-examples/closure.rbi

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/rubyish/rubyish-examples/fractal-tree.rbi

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/rubyish/rubyish-examples/pi.rbi

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/rubyish/rubyish-examples/template.rbi

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/rubyish/rubyish.nqp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
293293
<EXPR> [ <separator> [:s 'then']? | 'then' | <?before <tmpl-unesc>>] <stmtlist>
294294
}
295295

296-
token stmt:sym<if> {
297-
if ~ 'end' [ :s <xblock> [<else=.elsif>|<else>]? ]
296+
token stmt:sym<cond> {
297+
$<op>=[if|unless] ~ 'end' [ :s <xblock> [<else=.elsif>|<else>]? ]
298298
}
299299

300300
token elsif {
@@ -715,11 +715,12 @@ class Rubyish::Actions is HLL::Actions {
715715
}
716716

717717
method xblock($/) {
718-
make QAST::Op.new( $<EXPR>.ast, $<stmtlist>.ast, :op('if'), :node($/) );
718+
make QAST::Op.new( $<EXPR>.ast, $<stmtlist>.ast, :node($/) );
719719
}
720720

721-
method stmt:sym<if>($/) {
721+
method stmt:sym<cond>($/) {
722722
my $ast := $<xblock>.ast;
723+
$ast.op( ~$<op> );
723724
$ast.push( $<else>.ast )
724725
if $<else>;
725726

@@ -728,6 +729,7 @@ class Rubyish::Actions is HLL::Actions {
728729

729730
method elsif($/) {
730731
my $ast := $<xblock>.ast;
732+
$ast.op( 'if' );
731733
$ast.push( $<else>.ast )
732734
if $<else>;
733735

examples/rubyish/t/hashs.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
puts "1..8"
2-
h = {'a' =>10,'b' => 18+2 , 'c' , 30}
2+
h = {'a' =>10,'b' => 18+2 , 'c' => 30}
33
h{'d'} = 100
44
puts "#{h{'a'} == 10? 'ok' : 'nok'} 1 - h{'a'}"
55
puts "#{h<b> == 20? 'ok' : 'nok'} 2 - h<b>"

examples/rubyish/t/if-then-else.t

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
puts "1..18"
1+
puts "1..17"
22

33
if 1+1==3 then
44
puts "nok 1"
@@ -69,5 +69,11 @@ puts "ok #{iffy 20} - if [elsif] elsif else"
6969
puts "ok #{iffy 30} - if elsif [elsif] else"
7070
puts "ok #{iffy 42} - if elsif elsif [else]"
7171

72-
puts "#{true? 'ok' : 'nok'} 17 - true"
73-
puts "#{false ? 'nok' : 'ok'} 18 - false"
72+
x=10
73+
unless x > 2
74+
puts "nok 17 - unless"
75+
elsif x == 20
76+
puts "nok 17 - unless (elsif)"
77+
else
78+
puts "ok 17 - unless"
79+
end

examples/rubyish/t/infix.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ puts "#{(false && 2)? 'nok' : 'ok'} 12 - infix &&"
2929
xx = nil
3030
yy = 42
3131
puts "#{(false || 2) == 2? 'ok' : 'nok'} 13 - infix ||"
32-
puts "#{xx.nil? == true? 'ok' : 'nok'} 14 - .nil? when true"
33-
puts "#{yy.nil? == false? 'ok' : 'nok'} 15 - .nil? when false"
32+
puts "#{(xx.nil? == true)? 'ok' : 'nok'} 14 - .nil? when true"
33+
puts "#{(yy.nil? == false)? 'ok' : 'nok'} 15 - .nil? when false"
3434

3535
puts "ok #{2 ** 4} - exponentiation **"
3636
puts "ok #{37 % 20} - modulus %"

0 commit comments

Comments
 (0)