A mental model of OpenSCAD under the covers

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

A mental model of OpenSCAD under the covers

sgraves
One of my missions is to educate people about OpenSCAD.  I am an EE and I like to have a model in my head about how things work.  And I believe models are helpful for everyone.  I went into embedded systems and over to the other side and do a lot of software development.  I have models in my head of what various procedural and OO languages do under the covers.  OpenSCAD does not fit those models and I have developed a model which helps me visualize what is happening.  I am also using it to teach others.

I want to run this model by the group and get feedback on how accurate it is and how to improve it.

The manual mentions a two step compile.  I am visualizing something like a 2 1/2 step compile.  In my model, OpenSCAD first goes through and generates a CSG tree.  During this step it is following the various conditional branches (ifs, for loops, etc.) and creating various parts of the tree.  The 1/2 compile is that in each of these "scopes" it evaluates the user controlled values first and then does the generation of the tree for that branch. (This is the source of the rule of last value applies to all in a given scope).

The second step is taking the generated tree and producing the tessellated mesh or preview rendering.  That generated tree has all the user controlled values determined.  I believe the menu choice "Design - Display CSG Tree..." shows the intermediate tree after the 1st compile pass.

The 2nd pass is really the core of the OpenSCAD language and is what makes it different from any other language (unless you call CSG a language).  That pass is clearly not procedural and understanding that is a big help in visualizing what one's goal is in writing an OpenSCAD script.

The script we write is taking various tree segments and directing the 1st (and 1/2) pass on how to put those segments together.  These instructions are sort of procedural (if, for loops, etc. are procedural), but the procedure is to create the tree to be realized in the 2nd pass.

Steve
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

kintel
Administrator
Hi Steve,

I sometimes think of what you call the CSG Tree as a DOM - similar to the HTML DOM; It’s a hierarchical data structure describing one explicit design. This is what you can export as a .csg file - which happens to be valid OpenSCAD source code.
This DOM can be rendered in various ways (Preview using OpenCSG, Render using CGAL, Throwntogether using pure OpenGL), so it can be looked at as any other data model in an MVC framework.

The first step doesn’t really consist of half steps. We simply create an AST from the source code and walk that tree evaluating it, resolving any user-defined data in-line. The “feature” that the last variable survives is an unfortunate hack which allows us to override variables on the cmd-line (but due to widespread use we’ve chosen to be backwards compatible so far).

This diagram could be better, but might help visualize what’s going on:
https://github.com/openscad/openscad/blob/master/doc/OpenSCAD-compile.pdf

Hope that helped,

 -Marius


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

sgraves
Hi Marius,

Thank you.  I had looked at that pdf, but didn't quite get it.  Now I understand better.  So one could safely say that there is a 3 step process.  Parsing the script into AST, evaluating the AST into CSG and rendering the CSG in one of three ways.  One could say that a script is a set of instructions to build a data model representing the desired 3D model.

I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that explaining what happens under the covers has a lot of impact on getting noobies up to speed.  I can emphasize that variables are to define things in the model.  And that they have their own syntax.  That the only conditional that operates with variables is the tertiary operator.  The other conditionals (if, fors, etc) only operate on the data model.  I seems to help greatly to get them thinking about two different syntaxes.  The syntax for variables and math and the syntax for controlling the creation of the tree (which I will now sell as a data model).

I am feeling like the light is going on behind more of the eyes I see.  I am now going deeper into using OpenSCAD in my introductory classes and seeing more people grasping the concepts.

I hope my students recognize the multiple reasons why the following does not work. (From another question on the forum).

    if (form>1) {form=1};

Steve

BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of List Comprehensions is obvious.  But I am finding a little thing like the LET is greatly improving my code.  Thank you for all your work.
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

kintel
Administrator
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <[hidden email]> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

doug.moen
As Marius says, OpenSCAD has what I call a two level syntax.
  • The expression syntax is used for computing values. Eg, 2+2, sin(x)
  • The 'statement' syntax is used for computing shapes. Eg, cube(10), if, for, {...} etc

OpenSCAD is not a procedural language. This is important for the mental model. It *looks* like a procedural language, since the statement syntax is stolen from C, but it isn't, and this is a source of confusion.

In a procedural language, you have mutable variables, and mutable data structures. When a statement is executed, it doesn't return a value, instead it modifies state (eg, incrementing a variable or appending a value to a list).

In OpenSCAD, we don't have mutable variables or mutable data structures. If you use the word "procedural" to describe OpenSCAD, you are setting up the expectation that we do have these features, and we don't.

In OpenSCAD, "statement" syntax is really just another kind of expression syntax, for computing shapes. Statements are really shape expressions. The "if" statement is the analog of the "? :" operator in the expression syntax--it returns a shape instead of returning a value. The "for" statement is the analog of the "for" expression in a list comprehension--it is essentially a group comprehension, where "group" is the analog of "list" in the shape world. A group is an ordered sequence of shapes. The {...} statement has tricky semantics, but in the common case it is just a group literal, analogous to [...] in the expression syntax.

On 21 October 2015 at 23:11, Marius Kintel <[hidden email]> wrote:
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <[hidden email]> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

Tim Hawkins

I tend to think of openscad as being like "javascript with a 3d DOM"

given all tbe available tools for js, it would be possibly a good future direction to switch to javascript as a potential carrier language for the csg tree builder.


On Sat, Oct 24, 2015, 01:02 doug moen <[hidden email]> wrote:
As Marius says, OpenSCAD has what I call a two level syntax.
  • The expression syntax is used for computing values. Eg, 2+2, sin(x)
  • The 'statement' syntax is used for computing shapes. Eg, cube(10), if, for, {...} etc

OpenSCAD is not a procedural language. This is important for the mental model. It *looks* like a procedural language, since the statement syntax is stolen from C, but it isn't, and this is a source of confusion.

In a procedural language, you have mutable variables, and mutable data structures. When a statement is executed, it doesn't return a value, instead it modifies state (eg, incrementing a variable or appending a value to a list).

In OpenSCAD, we don't have mutable variables or mutable data structures. If you use the word "procedural" to describe OpenSCAD, you are setting up the expectation that we do have these features, and we don't.

In OpenSCAD, "statement" syntax is really just another kind of expression syntax, for computing shapes. Statements are really shape expressions. The "if" statement is the analog of the "? :" operator in the expression syntax--it returns a shape instead of returning a value. The "for" statement is the analog of the "for" expression in a list comprehension--it is essentially a group comprehension, where "group" is the analog of "list" in the shape world. A group is an ordered sequence of shapes. The {...} statement has tricky semantics, but in the common case it is just a group literal, analogous to [...] in the expression syntax.

On 21 October 2015 at 23:11, Marius Kintel <[hidden email]> wrote:
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <[hidden email]> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

doug.moen
"Switching" to JavaScript would really mean recreating OpenSCAD from scratch in JavaScript. Which has already been done. See openjscad.org

On Friday, 23 October 2015, Tim Hawkins <[hidden email]> wrote:

I tend to think of openscad as being like "javascript with a 3d DOM"

given all tbe available tools for js, it would be possibly a good future direction to switch to javascript as a potential carrier language for the csg tree builder.


On Sat, Oct 24, 2015, 01:02 doug moen <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;doug@moens.org&#39;);" target="_blank">doug@...> wrote:
As Marius says, OpenSCAD has what I call a two level syntax.
  • The expression syntax is used for computing values. Eg, 2+2, sin(x)
  • The 'statement' syntax is used for computing shapes. Eg, cube(10), if, for, {...} etc

OpenSCAD is not a procedural language. This is important for the mental model. It *looks* like a procedural language, since the statement syntax is stolen from C, but it isn't, and this is a source of confusion.

In a procedural language, you have mutable variables, and mutable data structures. When a statement is executed, it doesn't return a value, instead it modifies state (eg, incrementing a variable or appending a value to a list).

In OpenSCAD, we don't have mutable variables or mutable data structures. If you use the word "procedural" to describe OpenSCAD, you are setting up the expectation that we do have these features, and we don't.

In OpenSCAD, "statement" syntax is really just another kind of expression syntax, for computing shapes. Statements are really shape expressions. The "if" statement is the analog of the "? :" operator in the expression syntax--it returns a shape instead of returning a value. The "for" statement is the analog of the "for" expression in a list comprehension--it is essentially a group comprehension, where "group" is the analog of "list" in the shape world. A group is an ordered sequence of shapes. The {...} statement has tricky semantics, but in the common case it is just a group literal, analogous to [...] in the expression syntax.

On 21 October 2015 at 23:11, Marius Kintel <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;marius@kintel.net&#39;);" target="_blank">marius@...> wrote:
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;sgraves@gte.net&#39;);" target="_blank">sgraves@...> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> <a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;Discuss@lists.openscad.org&#39;);" target="_blank">Discuss@...
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;Discuss@lists.openscad.org&#39;);" target="_blank">Discuss@...
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;Discuss@lists.openscad.org&#39;);" target="_blank">Discuss@...
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

Tim Hawkins

Different thing, openjscad is an interpreter for the current openscad language written in js,  i was suggesting that it may be worth exploring using javascript itself as the base language instead of tbe current openscad language. It would require a language level binding to the csg model.


On Sat, Oct 24, 2015, 06:24 doug moen <[hidden email]> wrote:
"Switching" to JavaScript would really mean recreating OpenSCAD from scratch in JavaScript. Which has already been done. See openjscad.org

On Friday, 23 October 2015, Tim Hawkins <[hidden email]> wrote:

I tend to think of openscad as being like "javascript with a 3d DOM"

given all tbe available tools for js, it would be possibly a good future direction to switch to javascript as a potential carrier language for the csg tree builder.


On Sat, Oct 24, 2015, 01:02 doug moen <[hidden email]> wrote:
As Marius says, OpenSCAD has what I call a two level syntax.
  • The expression syntax is used for computing values. Eg, 2+2, sin(x)
  • The 'statement' syntax is used for computing shapes. Eg, cube(10), if, for, {...} etc

OpenSCAD is not a procedural language. This is important for the mental model. It *looks* like a procedural language, since the statement syntax is stolen from C, but it isn't, and this is a source of confusion.

In a procedural language, you have mutable variables, and mutable data structures. When a statement is executed, it doesn't return a value, instead it modifies state (eg, incrementing a variable or appending a value to a list).

In OpenSCAD, we don't have mutable variables or mutable data structures. If you use the word "procedural" to describe OpenSCAD, you are setting up the expectation that we do have these features, and we don't.

In OpenSCAD, "statement" syntax is really just another kind of expression syntax, for computing shapes. Statements are really shape expressions. The "if" statement is the analog of the "? :" operator in the expression syntax--it returns a shape instead of returning a value. The "for" statement is the analog of the "for" expression in a list comprehension--it is essentially a group comprehension, where "group" is the analog of "list" in the shape world. A group is an ordered sequence of shapes. The {...} statement has tricky semantics, but in the common case it is just a group literal, analogous to [...] in the expression syntax.

On 21 October 2015 at 23:11, Marius Kintel <[hidden email]> wrote:
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <[hidden email]> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

doug.moen
But that's exactly what openjscad is. Go to http://openjscad.org/ and check out the example program in the upper right corner. That is javascript, not openscad.

But in addition to providing a javascript binding for the openscad operations, you are right, it is also able to read openscad source files.

On 23 October 2015 at 18:30, Tim Hawkins <[hidden email]> wrote:

Different thing, openjscad is an interpreter for the current openscad language written in js,  i was suggesting that it may be worth exploring using javascript itself as the base language instead of tbe current openscad language. It would require a language level binding to the csg model.


On Sat, Oct 24, 2015, 06:24 doug moen <[hidden email]> wrote:
"Switching" to JavaScript would really mean recreating OpenSCAD from scratch in JavaScript. Which has already been done. See openjscad.org

On Friday, 23 October 2015, Tim Hawkins <[hidden email]> wrote:

I tend to think of openscad as being like "javascript with a 3d DOM"

given all tbe available tools for js, it would be possibly a good future direction to switch to javascript as a potential carrier language for the csg tree builder.


On Sat, Oct 24, 2015, 01:02 doug moen <[hidden email]> wrote:
As Marius says, OpenSCAD has what I call a two level syntax.
  • The expression syntax is used for computing values. Eg, 2+2, sin(x)
  • The 'statement' syntax is used for computing shapes. Eg, cube(10), if, for, {...} etc

OpenSCAD is not a procedural language. This is important for the mental model. It *looks* like a procedural language, since the statement syntax is stolen from C, but it isn't, and this is a source of confusion.

In a procedural language, you have mutable variables, and mutable data structures. When a statement is executed, it doesn't return a value, instead it modifies state (eg, incrementing a variable or appending a value to a list).

In OpenSCAD, we don't have mutable variables or mutable data structures. If you use the word "procedural" to describe OpenSCAD, you are setting up the expectation that we do have these features, and we don't.

In OpenSCAD, "statement" syntax is really just another kind of expression syntax, for computing shapes. Statements are really shape expressions. The "if" statement is the analog of the "? :" operator in the expression syntax--it returns a shape instead of returning a value. The "for" statement is the analog of the "for" expression in a list comprehension--it is essentially a group comprehension, where "group" is the analog of "list" in the shape world. A group is an ordered sequence of shapes. The {...} statement has tricky semantics, but in the common case it is just a group literal, analogous to [...] in the expression syntax.

On 21 October 2015 at 23:11, Marius Kintel <[hidden email]> wrote:
Hi again Steve,

We can look at this another way as well; why some syntax only works on values and other syntax work on the "data model”.
Doug (who might be reading and offer clarification) once came up with what he calls smth. like  “two-level language syntax”.
The idea is that OpenSCAD source code is really two syntaxes in one:
1) one syntax for describing the DOM (the CSG tree). This builds a data structure in-line by having all modules instantiate themselves (modules are things like union, sphere, if, for, hull etc.).
2) one syntax for describing simple values (not really a language but an expression engine including user-defined expressions)

.csg files contain only syntax 2), but .scad files can have both.
These two syntaxes are different (but similar) ..and in your original email you’re right in that all values need to be evaluated before instantiating each module (as modules take values as parameter).
Now, this is all how OpenSCAD, as a minimal language, was designed, which is also why we don’t support things like function and module references.

If you look at the working document for OpenSCAD2 (https://github.com/doug-moen/openscad2), this is described in at lot more detail :)

 -Marius

> On Oct 21, 2015, at 22:41 PM, sgraves <[hidden email]> wrote:
>
> Hi Marius,
>
> Thank you.  I had looked at that pdf, but didn't quite get it.  Now I
> understand better.  So one could safely say that there is a 3 step process.
> Parsing the script into AST, evaluating the AST into CSG and rendering the
> CSG in one of three ways.  One could say that a script is a set of
> instructions to build a data model representing the desired 3D model.
>
> I teach classes in OpenSCAD at the Tampa Hackerspace.  I am finding that
> explaining what happens under the covers has a lot of impact on getting
> noobies up to speed.  I can emphasize that variables are to define things in
> the model.  And that they have their own syntax.  That the only conditional
> that operates with variables is the tertiary operator.  The other
> conditionals (if, fors, etc) only operate on the data model.  I seems to
> help greatly to get them thinking about two different syntaxes.  The syntax
> for variables and math and the syntax for controlling the creation of the
> tree (which I will now sell as a data model).
>
> I am feeling like the light is going on behind more of the eyes I see.  I am
> now going deeper into using OpenSCAD in my introductory classes and seeing
> more people grasping the concepts.
>
> I hope my students recognize the multiple reasons why the following does not
> work. (From another question on the forum).
>
>    if (form>1) {form=1};
>
> Steve
>
> BTW, I am a big fan of OpenSCAD.  The last release was huge.  The value of
> List Comprehensions is obvious.  But I am finding a little thing like the
> LET is greatly improving my code.  Thank you for all your work.
>
>
>
>
> --
> View this message in context: http://forum.openscad.org/A-mental-model-of-OpenSCAD-under-the-covers-tp14180p14182.html
> Sent from the OpenSCAD mailing list archive at Nabble.com.
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org



_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: A mental model of OpenSCAD under the covers

Neon22
In reply to this post by sgraves
My 2 cents:

To me an OpenSCAD file is a description that when evaluated results in a CSG tree.
So while it has similar constructs to a programming language, like variable assignments and if statements, it evaluates once and the result is an object.

A programming language - if doing this task (like the javascript version) - creates a CSG tree as a side effect of the program running. So its statements and loops etc operate "normally".

We could draw another analogy. VHDL and Verilog are programming languages for hardware. They don't evaluate the same way as programming languages either. In fact they are quite similar to OpenSCAD but they have their own special aspects like clock cycles.

This isn't a discussion about whether OpenSCAD should be done one way or the other. But I would note that there are specific advantages and disadvantages to each approach. Similary for H/W description languages like VHDL which is why they exist at all.