Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rubyish heredocs, literal and interpolating
  • Loading branch information
dwarring committed Nov 2, 2013
1 parent 149a377 commit 11384fe
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 28 deletions.
3 changes: 2 additions & 1 deletion examples/rubyish/README.md
Expand Up @@ -27,8 +27,9 @@ Implemented:
- basic arrays and hashes
- for loops on arrays: `for val in [10,20,30] do puts val end`
- for loops on hash keys: `h = {'a'=>10, 'b'=>20}; for k in h do puts h{k} end`
- lambda blocks: `def make_counter(n,incr) ; n-=incr; lambda { n += incr }; end`
- lambda blocks/closures: `def make_counter(n,incr) ; n-=incr; lambda { n += incr }; end`
- lightweight eRuby like templating, see [template.rbi](examples-rubyish/template.rbi)
- heredocs literal `<<EOF ... EOF` and interpolating `<<"END" ... END`

Notes:

Expand Down
68 changes: 53 additions & 15 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -42,7 +42,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
proto token template-nibble {*}
token template-nibble:sym<interp> { <interp> }
token template-nibble:sym<stray-tag> { [<.tmpl-unesc>|<.tmpl-hdr>] <.panic("Stray tag, e.g. '%>' or '<?rbi?>'")> }
token template-nibble:sym<plain-text> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }
token template-nibble:sym<literal> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }

token tmpl-hdr {'<?rbi?>' \h* \n? <?{$*IN_TEMPLATE := 1}>}
token tmpl-esc {\h* '<%'
Expand Down Expand Up @@ -82,9 +82,7 @@ grammar Rubyish::Grammar is HLL::Grammar {

rule signature {
:my $*IN_PARENS := 1;
<param>* % <comma>
[ ',' '*' <slurpy=.param> ]?
','?
[ <param> | '*' <slurpy=.param> <!before ','>]* % ','
}

token param { <ident> }
Expand Down Expand Up @@ -156,6 +154,20 @@ grammar Rubyish::Grammar is HLL::Grammar {
]
}

token value:sym<heredoc> {'<<'<heredoc>}

proto token heredoc {*}
token heredoc:sym<literal> {[$<marker>=<.ident> | \' $<marker>=<- [\' \n]>+? \' ]\n
$<text>=.*?
\n$<marker>$$
}

token chars {\n? [<!before ['#{']> \N]+ | \n }
token heredoc:sym<interp> {\" $<marker>=<- [\" \n]>+? \"\n
[<text=.interp> | <text=.chars> ]*?
\n$<marker>$$
}

token paren-list {
:my $*IN_PARENS := 1;
<EXPR> *%% <comma>
Expand Down Expand Up @@ -389,7 +401,7 @@ class Rubyish::Actions is HLL::Actions {
make $<interp>.ast
}

method template-nibble:sym<plain-text>($/) {
method template-nibble:sym<literal>($/) {
make QAST::SVal.new( :value(~$/) );
}

Expand Down Expand Up @@ -580,15 +592,19 @@ class Rubyish::Actions is HLL::Actions {

method signature($/) {
my @params;
for $<param> {
@params.push(QAST::Var.new(
:name(~$_), :scope('lexical'), :decl('param')
));
}

@params.push(QAST::Var.new(
:name(~$<slurpy>), :scope('lexical'), :decl('param'), :slurpy<1>
)) if $<slurpy>;
if $<param> {
@params.push(QAST::Var.new(
:name(~$_), :scope('lexical'), :decl('param')
))
for $<param>;
}

if $<slurpy> {
@params.push(QAST::Var.new(
:name(~$<slurpy>[0]), :scope('lexical'), :decl('param'), :slurpy<1>
));
}

make @params;
}
Expand Down Expand Up @@ -622,6 +638,7 @@ class Rubyish::Actions is HLL::Actions {

make $class_stmts;
}

method classbody($/) {
$*CUR_BLOCK.push($<stmtlist>.ast);
$*CUR_BLOCK.blocktype('immediate');
Expand Down Expand Up @@ -654,6 +671,27 @@ class Rubyish::Actions is HLL::Actions {
make $<quote_EXPR>.ast;
}

method value:sym<heredoc>($/) {
make $<heredoc>.ast
}

method heredoc:sym<literal>($/) {
make QAST::SVal.new( :value( ~$<text> ) );
}

method chars ($/) { make QAST::SVal.new( :value(~$/) ) }

method heredoc:sym<interp>($/) {
my $list := QAST::Op.new( :op<list> );

$list.push($_.ast)
for $<text>;

make QAST::Op.new( :op<join>,
QAST::SVal.new( :value('') ),
$list );
}

method value:sym<integer>($/) {
make QAST::IVal.new( :value(+$/.Str) )
}
Expand Down Expand Up @@ -792,8 +830,8 @@ class Rubyish::Actions is HLL::Actions {
$block.push($_);
$block.symbol($_.name, :declared(1));
}
}
$block.push($<stmtlist>.ast);
}
$block.push($<stmtlist>.ast);

make QAST::Op.new(:op<takeclosure>, $block );
}
Expand Down
26 changes: 24 additions & 2 deletions examples/rubyish/t/line-spanning.t
@@ -1,4 +1,4 @@
puts "1..6"
puts "1..11"

a=[10,
20]
Expand All @@ -23,7 +23,7 @@ h = {"a" => 10,
, 20
, "c" => 30
}
puts "#{h<c>? 'ok' : 'nok'} 3 - hash spanning lines"
puts "#{h<c> == 30? 'ok' : 'nok'} 3 - hash spanning lines"
def tricky(k,
n, desc)
Expand All @@ -42,3 +42,25 @@ tricky(
puts \
"ok 6 - \\ line continuation"
heredoc = <<EOF
ok 7 - heredoc line 1
ok 8 - heredoc line 2
EOF
puts heredoc
heredoc2 = <<'EOF'
ok 9 - heredoc2
EOF
puts heredoc2
tst = 10
theredoc = <<"THE_END"
ok #{tst} - heredoc interpolating line 1
ok #{tst += 1} - heredoc interpolating line 2
THE_END
puts theredoc
25 changes: 15 additions & 10 deletions examples/rubyish/t/scoping.t
@@ -1,4 +1,4 @@
puts "1..11"
puts "1..12"

$a = "ok 1"
a = "ok 2"
Expand All @@ -13,28 +13,32 @@ end
some_sub()

class MyClass
def set_class(v); @@c = v; end
def get_class; @@c; end
@@final_test = 12
a = 'wtf'

def set_class(v); @@c = v; b = 'wtf'; end
def get_class; @@c; end
def set_inst(v); @i = v; end
def get_inst; @i; end
def class_const; @@final_test ; end

def set_inst(v); @i = v; end
def get_inst; @i; end
end

class OtherClass
def set_class(v); @@c = v; end
def get_class; @@c; end
def get_class; @@c; end
end

obj1 = MyClass.new;
obj2 = MyClass.new;
obj3 = OtherClass.new;

puts "#{$a} - $a (global)"
puts "#{a} - a (local)"
puts "#{b} - b (local)"
puts "#{$c} - $c (global)"
puts "#{$d} - $d (global)"

obj1 = MyClass.new;
obj2 = MyClass.new;
obj3 = OtherClass.new;

@e=6
puts "ok #{@e} - @gobal"

Expand All @@ -50,3 +54,4 @@ obj2.set_inst(11)

puts "ok #{obj1.get_inst} - instance access"
puts "ok #{obj2.get_inst} - instance access"
puts "ok #{obj2.class_const} - class constant"

0 comments on commit 11384fe

Please sign in to comment.