Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added rubyish fractal tree example
  • Loading branch information
dwarring committed Oct 15, 2013
1 parent 150e52a commit 8ca5f43
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
31 changes: 31 additions & 0 deletions examples/rubyish/rubyish-examples/fractal-tree.rbi
@@ -0,0 +1,31 @@
<?rbi?>
<%# Fractal tree example %>
<%# % nqp rubyish.nqp rubyish-examples/fractal-tree.rbi > fractal.svg %>
<?xml version='1.0' encoding='utf-8' standalone='no'?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
<svg width='100%' height='100%' version='1.1'
xmlns='http://www.w3.org/2000/svg'>

<%#----------
@scale = 6/10
@PI = 3.1415926535
def tree(x, y, len, angle)
if len >= 1.0 then
x2 = x + len * nqp::cos_n(angle)
y2 = y + len * nqp::sin_n(angle)
puts "<line x1='#{x}' y1='#{y}' x2='#{x2}' y2='#{y2}' style='stroke:rgb(0,0,0);stroke-width:1'/>";
tree(x2, y2, len*@scale, angle + @PI/5);
tree(x2, y2, len*@scale, angle - @PI/5);
end
end

width = 1000
height = 1000
length = 400

tree(width/2, height, length, 3*@PI/2)
#-----------%>

</svg>
6 changes: 3 additions & 3 deletions examples/rubyish/rubyish-examples/green-bottles.rbi 100644 → 100755
@@ -1,15 +1,15 @@
# eRubyish templating - see http://en.wikipedia.org/wiki/ERuby
# There are only three directives:
# <%rbi> - Header; remainder of the source file is the template body
# <?rbi?> - Header; remainder of the source file is the template body
# <% ... %> - rubyish statements
# #{ ... } - inserted content

# Any arbritrary code can appear before the template. Output to stdout
# is appended to the template, both before and within the template body

puts '<?xml>'
puts '<?xml version='1.0' encoding='utf-8'?>'

<%rbi>
<?rbi?>
<html>
<body>
<h1>Green Bottles...</h1>
Expand Down
4 changes: 2 additions & 2 deletions examples/rubyish/rubyish.nqp
Expand Up @@ -41,7 +41,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
token template-content:sym<interp> { <interp> }
token template-content:sym<plain-text> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }

token tmpl-hdr {'<%rbi>' \h* \n? <?{$*IN_TEMPLATE := 1}>}
token tmpl-hdr {'<?rbi?>' \h* \n? <?{$*IN_TEMPLATE := 1}>}
token tmpl-esc {\h* '<%'
[<?{$*IN_TEMPLATE}> || <.panic('Template directive precedes "<%rbi>"')>]
}
Expand Down Expand Up @@ -110,7 +110,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
}

token term:sym<nqp-op> {
'nqp::'<ident> ['(' ~ ')' <call-args=.paren-args>? | <call-args>? ]
'nqp:'':'?<ident> ['(' ~ ')' <call-args=.paren-args>? | <call-args>? ]
}

token term:sym<quote-words> {
Expand Down
24 changes: 8 additions & 16 deletions examples/rubyish/t/template.t
Expand Up @@ -3,28 +3,20 @@
puts '1..10'
puts "ok 1 - statements before template"

<%rbi>
<?rbi?>
ok 2 - initial template text
<%n = 2 %>
<%while n < 4 %>
<% n += 1%>
ok #{n} - while loop test
<%while (n+=1) < 5 %>ok #{n} - while loop test
<%end%>
ok 5 - text between statements
<%if n == 4 %>
ok 6 - if block (true)
<%else%>
nok 6 - if block (true)
<%if n == 5 %>ok 6 - if block (true)
<%else %>nok 6 - if block (true)
<%end%>
<%if n == 1234 %>
nok 7 - if block (false)
<%elsif n == 20 %>
nok 7 - if block (false)
<%else%>
ok 7 - if block (false)
<%if n == 1234 %>nok 7 - if block (false)
<%elsif n == 20 %>nok 7 - if block (false)
<%else %>ok 7 - if block (false)
<% end %>
<%for test in [8, 9] do %>
ok #{test} - for loop test
<%for test in [8, 9] do %>ok #{test} - for loop test
<%end%>
<%#puts "nok 10 - commented out directive" %>
ok 10 - final template text

0 comments on commit 8ca5f43

Please sign in to comment.