Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rubyish - implement compound-statement/multi-line interpolation
  • Loading branch information
dwarring committed Oct 28, 2013
1 parent 724a9b1 commit 1161844
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -56,8 +56,8 @@ grammar Rubyish::Grammar is HLL::Grammar {
[ <stmt=.stmtish>? ] *%% [<.separator>|<stmt=.template-chunk>]
}

rule stmtish {
<stmt> [ <modifier> <EXPR>]?
token stmtish {
<stmt> [:s<hs> <modifier> <EXPR>]?
}

token modifier {if|unless|while|until}
Expand Down Expand Up @@ -164,7 +164,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
token value:sym<false> { <sym> }

# Interpolation
token interp { '#{' ~ '}' [:s<hs> [ <stmt> ]
token interp { '#{' ~ '}' [:s<hs> [ <stmtlist> ]
|| <panic('string interpolation error')>]
}
token quote_escape:sym<#{ }> { <interp> }
Expand Down Expand Up @@ -678,7 +678,7 @@ class Rubyish::Actions is HLL::Actions {
make QAST::IVal.new( :value<0> );
}

method interp($/) { make $<stmt>.ast }
method interp($/) { make $<stmtlist>.ast }
method quote_escape:sym<#{ }>($/) { make $<interp>.ast }
method circumfix:sym<( )>($/) { make $<EXPR>.ast }

Expand Down
11 changes: 6 additions & 5 deletions examples/rubyish/t/00hello-worldish.t
@@ -1,21 +1,22 @@
puts "1..4"
puts "1..5"

def capitalize(s)
nqp::uc(nqp::substr(s, 0, 1)) ~ nqp::lc(nqp::substr(s, 1));
end

class HelloWorld
puts "ok 1 - class immediate code"
def initialize(name)
puts "ok 1 - initialize called"
puts "ok 2 - initialize called"
@name = capitalize name
end
def sayHi
puts "ok 4 - Hello #{@name}!"
puts "ok 5 - Hello #{@name}!"
end
end

hello = HelloWorld.new("worldish")
puts "#{nqp::can(hello, 'sayHi')? 'ok' : 'nok'} 2 - can say Hi"
puts "#{nqp::can(hello, 'sayBye')? 'nok' : 'ok'} 3 - can't say Bye"
puts "#{nqp::can(hello, 'sayHi')? 'ok' : 'nok'} 3 - can say Hi"
puts "#{nqp::can(hello, 'sayBye')? 'nok' : 'ok'} 4 - can't say Bye"
hello.sayHi

11 changes: 9 additions & 2 deletions examples/rubyish/t/interpolation.t
@@ -1,11 +1,18 @@
puts "1..8"
puts "1..10"
a = 2
puts "ok #{1} - num"
puts "ok #{a} - variable"
puts "ok #{a+1} - expression"
b = 'ok'
puts "#{b} #{3*(a - 1) + 1} - expression"
puts %q<ok 5> ' - %q<...>'
tst=5
puts %q<ok> " #{tst}" ' - adjacent' ' strings'
puts %Q{ok #{a*=3} - expression - side affect}
puts %Q{ok #{(a+=2)-1} - expression - side affect}
puts "#{if a==8 then 'ok' else 'nok' end} #{a} - nested statement"
puts "ok 9 # todo nested starters / stoppers"
##puts "#{'#{tst}' == '#{' 'tst}'? 'ok' : 'nok'} 9 - nested starters / stoppers"
puts "ok #{
x = 4; x+= 1
x+5
} - multiline interpolation"

0 comments on commit 1161844

Please sign in to comment.