Skip to content

Commit 8ca5f43

Browse files
committed
added rubyish fractal tree example
1 parent 150e52a commit 8ca5f43

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?rbi?>
2+
<%# Fractal tree example %>
3+
<%# % nqp rubyish.nqp rubyish-examples/fractal-tree.rbi > fractal.svg %>
4+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
5+
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'
6+
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
7+
<svg width='100%' height='100%' version='1.1'
8+
xmlns='http://www.w3.org/2000/svg'>
9+
10+
<%#----------
11+
@scale = 6/10
12+
@PI = 3.1415926535
13+
14+
def tree(x, y, len, angle)
15+
if len >= 1.0 then
16+
x2 = x + len * nqp::cos_n(angle)
17+
y2 = y + len * nqp::sin_n(angle)
18+
puts "<line x1='#{x}' y1='#{y}' x2='#{x2}' y2='#{y2}' style='stroke:rgb(0,0,0);stroke-width:1'/>";
19+
tree(x2, y2, len*@scale, angle + @PI/5);
20+
tree(x2, y2, len*@scale, angle - @PI/5);
21+
end
22+
end
23+
24+
width = 1000
25+
height = 1000
26+
length = 400
27+
28+
tree(width/2, height, length, 3*@PI/2)
29+
#-----------%>
30+
31+
</svg>

examples/rubyish/rubyish-examples/green-bottles.rbi

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# eRubyish templating - see http://en.wikipedia.org/wiki/ERuby
22
# There are only three directives:
3-
# <%rbi> - Header; remainder of the source file is the template body
3+
# <?rbi?> - Header; remainder of the source file is the template body
44
# <% ... %> - rubyish statements
55
# #{ ... } - inserted content
66

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

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

12-
<%rbi>
12+
<?rbi?>
1313
<html>
1414
<body>
1515
<h1>Green Bottles...</h1>

examples/rubyish/rubyish.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ grammar Rubyish::Grammar is HLL::Grammar {
4141
token template-content:sym<interp> { <interp> }
4242
token template-content:sym<plain-text> { [<!before [<tmpl-esc>|'#{'|$]> .]+ }
4343

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

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

116116
token term:sym<quote-words> {

examples/rubyish/t/template.t

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,20 @@
33
puts '1..10'
44
puts "ok 1 - statements before template"
55

6-
<%rbi>
6+
<?rbi?>
77
ok 2 - initial template text
88
<%n = 2 %>
9-
<%while n < 4 %>
10-
<% n += 1%>
11-
ok #{n} - while loop test
9+
<%while (n+=1) < 5 %>ok #{n} - while loop test
1210
<%end%>
1311
ok 5 - text between statements
14-
<%if n == 4 %>
15-
ok 6 - if block (true)
16-
<%else%>
17-
nok 6 - if block (true)
12+
<%if n == 5 %>ok 6 - if block (true)
13+
<%else %>nok 6 - if block (true)
1814
<%end%>
19-
<%if n == 1234 %>
20-
nok 7 - if block (false)
21-
<%elsif n == 20 %>
22-
nok 7 - if block (false)
23-
<%else%>
24-
ok 7 - if block (false)
15+
<%if n == 1234 %>nok 7 - if block (false)
16+
<%elsif n == 20 %>nok 7 - if block (false)
17+
<%else %>ok 7 - if block (false)
2518
<% end %>
26-
<%for test in [8, 9] do %>
27-
ok #{test} - for loop test
19+
<%for test in [8, 9] do %>ok #{test} - for loop test
2820
<%end%>
2921
<%#puts "nok 10 - commented out directive" %>
3022
ok 10 - final template text

0 commit comments

Comments
 (0)