[OpenSCAD] Extending OpenSCAD from assembler to C or Perlish language and adding standard library

Doug Moen doug at moens.org
Tue Oct 1 09:30:27 EDT 2019

On Tue, Oct 1, 2019, at 3:00 AM, nop head wrote:
> Doesn't the current C like for(..;..;) syntax effectively allow a while loop and mutable variables in the loop?

If we add mutable variables, an assignment statement, and a while loop to OpenSCAD, then we enable a rich set of coding patterns:
* conditional assignment statements
* assignments inside a `for` or `while` statement which modify an accumulator variable
And these patterns can be nested and sequenced.

The C-like for (;;) syntax is only available inside list comprehensions, where it is used for generating a sequence of list elements. I do not think that list comprehensions are in any way a replacement for general imperative programming features.

Here is a Curv function that I used for generating a shape based on voronoi diagrams. I think it would be quite painful to translate this into OpenSCAD using tail recursion and list comprehensions. I ported this code from another language. A major benefit of imperative programming in Curv is that I could port the code trivially, doing a line-for-line transliteration, without wholesale restructuring of the code to use entirely different coding patterns.

// input: 2D coordinate, output: distance to cell border
voronoi x =
 n = floor(x - 1.5);
 f = x - n;
 md = 8;
 in do
 // first pass: distance to cell centre
 local mr = [0,0];
 for (j in 0 .. 3)
 for (i in 0 .. 3) (
 local g = [i,j];
 local o = hash2x2(n + g);
 local r = g + o - f;
 local d = dot(r,r);
 if (d < md) (
 md := d;
 mr := r;
 // second pass: distance to border
 md := 8.0;
 for (j in 0 .. 3)
 for (i in 0 .. 3) (
 local g = [i,j];
 local o = hash2x2( n + g );
 local r = g + o - f;
 if (dot(mr-r,mr-r)>EPSILON) // skip the same cell
 md := smooth_min(md, dot(0.5*(mr+r), normalize(r-mr)),.2);
 in md;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20191001/4d01bfa5/attachment.html>

More information about the Discuss mailing list