Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A use for `countexecstack`. (L-system)

41 views
Skip to first unread message

luser- -droog

unread,
Mar 17, 2013, 1:23:42 AM3/17/13
to
Finally followed up this idea from a year ago. Using countexecstack to check the recursion depth for an L-system interpreter with no memory issues! I posted this already to codegolf.stackexchange.com, but it's probably more valuable here.

This program makes an enormous Dragon curve.

%!

[ %The Dragon Curve
/X{X + Y F} % productions
/Y{F X - Y} %
/I{F X} % initial production
/A 90 % angle
/R 1 % radius
/b 70 % recursion bound
>>begin

/T[ %The Turtle
/F{R 0 rlineto
%currentpoint stroke moveto flushpage % uncomment to preview
}
/+{A rotate}
/-{A neg rotate}
>>def

[ %The Engine
/a countexecstack % base execstack size
/z{countexecstack a b add lt } % execstack size within bounds
/t{T exch 2 copy known{get exec}{pop pop}ifelse} % try turtle command
/s{
{ % try
load % lookup
} stopped { % failed to lookup
t % try turtle
}{ % succeeded in lookup
dup type/arraytype eq{ % if it's an array (ie. a procedure)
{ % iterate through it
z{ % within bounds
s % recursively expand (or descend)
}{ % exceeded bounds
t % try turtle
}ifelse
}forall
}{ % not an array
exec % simply execute it
}ifelse
}ifelse
}
>>begin
300 400 moveto
/I s stroke
showpage

luser- -droog

unread,
Mar 26, 2013, 10:44:46 PM3/26/13
to
It's probably simpler just to track the recursion level with an integer on the stack.

Yet another L-system Sierpinski Triangle.

%!
[
48(0-1+0+1-0) % 0
49(11) % 1
43(+) % +
45(-) % -
/s{ % string recursion-level
dup 0 eq{ % level=0
exch{ % iterate through string
[
48{1 0 rlineto} % 0
49 1 index % 1
43{240 rotate} % +
45{120 rotate} % -
>>exch get exec % interpret turtle command
}forall
}{ % level>0
exch{ % iterate through string
load exch % lookup charcode
1 sub s % recurse with level-1
}forall
}ifelse
1 add % return recursion-level+1
}
>>begin
9 9 moveto(0-1-1)9 s stroke
0 new messages