Syntax Highlighting & Language Extensions

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

Syntax Highlighting & Language Extensions

Christopher Olah
Hey All,

I spent my weekend hacking on openSCAD and I thought people might be
interested. I added syntax highlighting and a couple language
extensions:

    * Dot/Cross Product (* and %)
    * length function for lengths of vectors and strings
    * accessing characters in a string via []

I'm also thinking of adding the following features:

    * includes using relative paths
    * echo not prepending with “ECHO: “
    * concatenate strings/vectors with a “++” operator
    * describe vectors in circular/spherical coordinates. Something
like magnitude$theta$phi?

I've also done a post on my blog
http://christopherolah.wordpress.com/2011/03/28/extending-openscad/.
My code is at https://github.com/colah/openscad .

Your thoughts are appreciated. Also, I'd like to merge my code: do
people prefer frequent small pull requests or less frequent big ones?

All the best,

Christopher Olah

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Giles Bathgate-2

I did syntax highlighting some time ago in openscad using qcodeedit http://www.gilesbathgate.com/2010/04/openscad/ the code was merged in some time ago but is turned off by a compile flag by default.
The other ideas are great though in fact I already had a go at doing a cross product operator in rapcad www.rapcad.org (sorry shameless plug) I would not use % though as that is commonly used for mod. I chose ** and .* also ++ could be a increment operator so string concat could be better using ~ (as in d programming language)

On 28 Mar 2011 20:20, "Christopher Olah" <christopherolah.co@gmail.com> wrote:
> Hey All,
>
> I spent my weekend hacking on openSCAD and I thought people might be
> interested. I added syntax highlighting and a couple language
> extensions:
>
> * Dot/Cross Product (* and %)
> * length function for lengths of vectors and strings
> * accessing characters in a string via []
>
> I'm also thinking of adding the following features:
>
> * includes using relative paths
> * echo not prepending with “ECHO: “
> * concatenate strings/vectors with a “++” operator
> * describe vectors in circular/spherical coordinates. Something
> like magnitude$theta$phi?
>
> I've also done a post on my blog
> http://christopherolah.wordpress.com/2011/03/28/extending-openscad/.
> My code is at https://github.com/colah/openscad .
>
> Your thoughts are appreciated. Also, I'd like to merge my code: do
> people prefer frequent small pull requests or less frequent big ones?
>
> All the best,
>
> Christopher Olah
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Christopher Olah
On Mon, Mar 28, 2011 at 7:08 PM, Giles Bathgate
<[hidden email]> wrote:
> I did syntax highlighting some time ago in openscad using qcodeedit
> http://www.gilesbathgate.com/2010/04/openscad/ the code was merged in some
> time ago but is turned off by a compile flag by default.

Ah. I was wondering what all the
#ifdef _QCODE_EDIT_
were about, especially since I couldn't find a QDocument in the QT
documentation... I figured it was some sort of legacy thing. :)

I'm not clear as to how it works, though. We don't seem to ship
qdocument.h, etc, in the repos, so does one have to pull QCodeEdit
into the build environment? Or is it getting downloaded somehow if one
enables it?

Is the reason that its disabled because its an additional dependency
or because of a aesthetic decision against syntax highlighting?

If the former, I'm going to change my code so that its only enabled if
one isn't using QCodeEdit. That way people can get syntax highlighting
either way.

> The other ideas are great though in fact I already had a go at doing a cross
> product operator in rapcad www.rapcad.org (sorry shameless plug)

Thanks! I've noticed that when multiple people start coming up with
the same idea, its usually a good one.

> I would not
> use % though as that is commonly used for mod. I chose ** and .*

I didn't pull the % symbol out of thin air, I've seen it used for the
cross product operator before in c++ libraries. It doesn't conflict
with modulus because modulus is not defined on vectors (it may have
been generalized to them at some point, but I've never heard of it and
certainly never heard of it getting used in programing). ** has its
own issues because it is used as an exponentiation operator in many
languages.

> also ++
> could be a increment operator so string concat could be better using ~ (as
> in d programming language)

Again, increment isn't really defined outside of numbers, so there's
no conflict... ~ could also work, though.

I actually like the idea of using ~ for spherical coordinates.

Thanks for your response!

Christopher

> On 28 Mar 2011 20:20, "Christopher Olah" <[hidden email]>
> wrote:
>> Hey All,
>>
>> I spent my weekend hacking on openSCAD and I thought people might be
>> interested. I added syntax highlighting and a couple language
>> extensions:
>>
>> * Dot/Cross Product (* and %)
>> * length function for lengths of vectors and strings
>> * accessing characters in a string via []
>>
>> I'm also thinking of adding the following features:
>>
>> * includes using relative paths
>> * echo not prepending with “ECHO: “
>> * concatenate strings/vectors with a “++” operator
>> * describe vectors in circular/spherical coordinates. Something
>> like magnitude$theta$phi?
>>
>> I've also done a post on my blog
>> http://christopherolah.wordpress.com/2011/03/28/extending-openscad/.
>> My code is at https://github.com/colah/openscad .
>>
>> Your thoughts are appreciated. Also, I'd like to merge my code: do
>> people prefer frequent small pull requests or less frequent big ones?
>>
>> All the best,
>>
>> Christopher Olah
>> _______________________________________________
>> OpenSCAD mailing list
>> [hidden email]
>> http://rocklinux.net/mailman/listinfo/openscad
>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Elmo Mäntynen
Great stuff, notes below:

On 03/29/2011 04:54 AM, Christopher Olah wrote:

> On Mon, Mar 28, 2011 at 7:08 PM, Giles Bathgate
> <[hidden email]>  wrote:
>> I did syntax highlighting some time ago in openscad using qcodeedit
>> http://www.gilesbathgate.com/2010/04/openscad/ the code was merged in some
>> time ago but is turned off by a compile flag by default.
>
> Ah. I was wondering what all the
> #ifdef _QCODE_EDIT_
> were about, especially since I couldn't find a QDocument in the QT
> documentation... I figured it was some sort of legacy thing. :)
>
> I'm not clear as to how it works, though. We don't seem to ship
> qdocument.h, etc, in the repos, so does one have to pull QCodeEdit
> into the build environment? Or is it getting downloaded somehow if one
> enables it?
>
> Is the reason that its disabled because its an additional dependency
> or because of a aesthetic decision against syntax highlighting?
>
> If the former, I'm going to change my code so that its only enabled if
> one isn't using QCodeEdit. That way people can get syntax highlighting
> either way.
>
>> The other ideas are great though in fact I already had a go at doing a cross
>> product operator in rapcad www.rapcad.org (sorry shameless plug)
>
> Thanks! I've noticed that when multiple people start coming up with
> the same idea, its usually a good one.
>
>> I would not
>> use % though as that is commonly used for mod. I chose ** and .*
>
> I didn't pull the % symbol out of thin air, I've seen it used for the
> cross product operator before in c++ libraries. It doesn't conflict
> with modulus because modulus is not defined on vectors (it may have
> been generalized to them at some point, but I've never heard of it and
> certainly never heard of it getting used in programing). ** has its
> own issues because it is used as an exponentiation operator in many
> languages.

>
>> also ++
>> could be a increment operator so string concat could be better using ~ (as
>> in d programming language)

Would overloading be too difficult? Like * for dot product, it's
unambiguous between two vectors, and + between strings (like in python).
Is ¤ in ASCII? It's a useless char on my keyboard, could we give it the
meaning of cross product? :)


> Again, increment isn't really defined outside of numbers, so there's
> no conflict... ~ could also work, though.
>
> I actually like the idea of using ~ for spherical coordinates.

I would like that too.

>
> Thanks for your response!
>
> Christopher
>
>> On 28 Mar 2011 20:20, "Christopher Olah"<[hidden email]>
>> wrote:
>>> Hey All,
>>>
>>> I spent my weekend hacking on openSCAD and I thought people might be
>>> interested. I added syntax highlighting and a couple language
>>> extensions:
>>>
>>> * Dot/Cross Product (* and %)
>>> * length function for lengths of vectors and strings
>>> * accessing characters in a string via []
>>>
>>> I'm also thinking of adding the following features:
>>>
>>> * includes using relative paths
>>> * echo not prepending with “ECHO: “
>>> * concatenate strings/vectors with a “++” operator
>>> * describe vectors in circular/spherical coordinates. Something
>>> like magnitude$theta$phi?
>>>
>>> I've also done a post on my blog
>>> http://christopherolah.wordpress.com/2011/03/28/extending-openscad/.
>>> My code is at https://github.com/colah/openscad .
>>>
>>> Your thoughts are appreciated. Also, I'd like to merge my code: do
>>> people prefer frequent small pull requests or less frequent big ones?
>>>
>>> All the best,
>>>
>>> Christopher Olah
>>> _______________________________________________
>>> OpenSCAD mailing list
>>> [hidden email]
>>> http://rocklinux.net/mailman/listinfo/openscad
>>
>> _______________________________________________
>> OpenSCAD mailing list
>> [hidden email]
>> http://rocklinux.net/mailman/listinfo/openscad
>>
>>
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Giles Bathgate-2
In reply to this post by Christopher Olah
On 29 March 2011 02:54, Christopher Olah <[hidden email]> wrote:
> If the former, I'm going to change my code so that its only enabled if
> one isn't using QCodeEdit. That way people can get syntax highlighting
> either way.

I would remove the qcodeedit stuff if you have done a native
implementation (QSyntaxHilighter?) When I implemented it I hadn't
realised that qcodeedit was effectively a dead project, and that there
was something better now natively in QT. I used qcodedit because it
was mentioned in the OpenSCAD TODO.txt. It also adds and extra
external dependency like you say, It does do some nice things like
line numbering and code collapsing, but I didn't utilise those
features

Have a quick read of this thread in case I missed anything:

http://rocklinux.net/pipermail/openscad/2010-April/000312.html

Regards

Giles

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Clifford Wolf
In reply to this post by Elmo Mäntynen
Hi,

On Tue, Mar 29, 2011 at 09:33:43AM +0300, Elmo wrote:

> >> I would not
> >> use % though as that is commonly used for mod. I chose ** and .*
> >
> > I didn't pull the % symbol out of thin air, I've seen it used for the
> > cross product operator before in c++ libraries. It doesn't conflict
> > with modulus because modulus is not defined on vectors (it may have
> > been generalized to them at some point, but I've never heard of it and
> > certainly never heard of it getting used in programing). ** has its
> > own issues because it is used as an exponentiation operator in many
> > languages.
> >
> >> also ++
> >> could be a increment operator so string concat could be better using ~ (as
> >> in d programming language)
>
> Would overloading be too difficult? Like * for dot product, it's
> unambiguous between two vectors, and + between strings (like in python).
> Is ? in ASCII? It's a useless char on my keyboard, could we give it the
> meaning of cross product? :)

I like '*' for the dot product and '%' for the cross product, as the '%'
looks a bit like an 'X'.

It should be easy to add this functionality to 'src/value.cc'. Be sure to
check that the arguments are vectors of numbers (just by checking the type
of the first vector element should be ok) so we could later on implement
matrix (vectors of vectors of number) operations without breaking backward
compatibility.

yours,
 - clifford

--
Walking on water and developing software from a specification are easy if
both are frozen.

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Giles Bathgate-2
Ah yes that reminds me, what I was actually trying to implement in RapCAD
was the "outer product" so you could do fancy things like:

  [10,10,20]**[[cos(t),sin(t),0],[-sin(t),cos(t),0],[0,0,1]];

Obviously that's a bit of a pain to write, you would define a function
so it would be:

  function rotate_x(t) = [[cos(t),sin(t),0],[-sin(t),cos(t),0],[0,0,1]];

  [10,10,20]**rotate_x(t);

Like Clifford suggests though It could just as easily be an overloaded
% symbol for that type of thing as well, so that:

  [10,10,20]%[2,3,4];

Means cross product and when the right operand is a matrix (vector of
vector of number) it means outer product, although personally I would
find that a bit confusing.

Either way, its good to see that more operators are being considered
it would be good to see the existing set extended. Some time ago I
wrote a patch to do component wise operations e.g.

   [10,10,10].*[2,3,4];    = [20,30,40];

This can be useful if you had a vector that described a cube and you
wanted to scale the vector rather than the cube itself:

  a=[10,10,10];
  cube(a);
  cube(a.*[2,3,4]);

Another thing that could be handy is addition between vectors and scalars e.g.

  [10,20,30]*2; //yields [20,40,60]

I expected:

  [10,20,30]+2; //to yield [11,21,31]


Regards

Giles


On 29 March 2011 09:57, Clifford Wolf <[hidden email]> wrote:

> Hi,
>
> On Tue, Mar 29, 2011 at 09:33:43AM +0300, Elmo wrote:
>> >> I would not
>> >> use % though as that is commonly used for mod. I chose ** and .*
>> >
>> > I didn't pull the % symbol out of thin air, I've seen it used for the
>> > cross product operator before in c++ libraries. It doesn't conflict
>> > with modulus because modulus is not defined on vectors (it may have
>> > been generalized to them at some point, but I've never heard of it and
>> > certainly never heard of it getting used in programing). ** has its
>> > own issues because it is used as an exponentiation operator in many
>> > languages.
>> >
>> >> also ++
>> >> could be a increment operator so string concat could be better using ~ (as
>> >> in d programming language)
>>
>> Would overloading be too difficult? Like * for dot product, it's
>> unambiguous between two vectors, and + between strings (like in python).
>> Is ? in ASCII? It's a useless char on my keyboard, could we give it the
>> meaning of cross product? :)
>
> I like '*' for the dot product and '%' for the cross product, as the '%'
> looks a bit like an 'X'.
>
> It should be easy to add this functionality to 'src/value.cc'. Be sure to
> check that the arguments are vectors of numbers (just by checking the type
> of the first vector element should be ok) so we could later on implement
> matrix (vectors of vectors of number) operations without breaking backward
> compatibility.
>
> yours,
>  - clifford
>
> --
> Walking on water and developing software from a specification are easy if
> both are frozen.
> _______________________________________________
> OpenSCAD mailing list
> [hidden email]
> http://rocklinux.net/mailman/listinfo/openscad
>

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Christopher Olah
In reply to this post by Clifford Wolf
> Would overloading be too difficult? Like * for dot product, it's
> unambiguous between two vectors, and + between strings (like in python).
> Is ¤ in ASCII? It's a useless char on my keyboard, could we give it the
> meaning of cross product? :)

Nope, it's trivial. The reason I didn't suggest + for concatenation is
that I'd like to also concatenate vectors with the same symbol, and +
is taken for vectors.

>> I actually like the idea of using ~ for spherical coordinates.
> I would like that too.

Yay!

> I like '*' for the dot product and '%' for the cross product, as the '%'
> looks a bit like an 'X'.
>
> It should be easy to add this functionality to 'src/value.cc'. Be sure to
> check that the arguments are vectors of numbers (just by checking the type
> of the first vector element should be ok) so we could later on implement
> matrix (vectors of vectors of number) operations without breaking backward
> compatibility.

That's already done! :) We were just discussing if I'd done it the
right way, I think.

(I check that they are number vectors of length 2 or 3.)

> I would remove the qcodeedit stuff if you have done a native
> implementation (QSyntaxHilighter?) When I implemented it I hadn't
> realised that qcodeedit was effectively a dead project, and that there
> was something better now natively in QT.

This is rather disappointing, since QCodeEdit is a great idea.

> I used qcodedit because it
> was mentioned in the OpenSCAD TODO.txt. It also adds and extra
> external dependency like you say, It does do some nice things like
> line numbering and code collapsing, but I didn't utilise those
> features

They'd be nice things to have, though.

As it is, I don't think your support for QCodeEdit causes any harm.
Maybe someday QCodeEdit will become better? If someone else thinks it
should be removed, they can do so, but I'm just going to comment it a
bit better.

> Have a quick read of this thread in case I missed anything:
>
> http://rocklinux.net/pipermail/openscad/2010-April/000312.html

Will do.

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Christopher Olah
In reply to this post by Giles Bathgate-2
On Tue, Mar 29, 2011 at 7:39 AM, Giles Bathgate
<[hidden email]> wrote:

> Ah yes that reminds me, what I was actually trying to implement in RapCAD
> was the "outer product" so you could do fancy things like:
>
>  [10,10,20]**[[cos(t),sin(t),0],[-sin(t),cos(t),0],[0,0,1]];
>
> Obviously that's a bit of a pain to write, you would define a function
> so it would be:
>
>  function rotate_x(t) = [[cos(t),sin(t),0],[-sin(t),cos(t),0],[0,0,1]];
>
>  [10,10,20]**rotate_x(t);

Or if we introduce ~, it is 1~t. Spherical coordinates are such a
useful thing...

> Like Clifford suggests though It could just as easily be an overloaded
> % symbol for that type of thing as well, so that:
>
>  [10,10,20]%[2,3,4];
>
> Means cross product and when the right operand is a matrix (vector of
> vector of number) it means outer product, although personally I would
> find that a bit confusing.

Yeah. Outer product seems a bit unnecessary. We could just have a
transpose operator.

> Either way, its good to see that more operators are being considered
> it would be good to see the existing set extended. Some time ago I
> wrote a patch to do component wise operations e.g.
>
>   [10,10,10].*[2,3,4];    = [20,30,40];
>
> This can be useful if you had a vector that described a cube and you
> wanted to scale the vector rather than the cube itself:
>
>  a=[10,10,10];
>  cube(a);
>  cube(a.*[2,3,4]);
>
> Another thing that could be handy is addition between vectors and scalars e.g.
>
>  [10,20,30]*2; //yields [20,40,60]
>
> I expected:
>
>  [10,20,30]+2; //to yield [11,21,31]

So this brings to mind another question: is there a reason we've
separated nodes and values the way they are?

It might be interesting to define operators between them. eg.
[1,2,3]*cube([1,1,1]) is the same as scale([1,2,3]) cube([1,1,1]) is
the same as cube([1,2,3]). Or [1,2,3]+cube([1,1,1]) is
translate([1,2,3]) cube([1,1,1])... Or would that just be crazy?

On a different note, what is the process for merging upstream?

Thanks,

Christopher

Reply | Threaded
Open this post in threaded view
|

Re: Syntax Highlighting & Language Extensions

Giles Bathgate-2
On 3 April 2011 22:19, Christopher Olah <[hidden email]> wrote:
> Or if we introduce ~, it is 1~t. Spherical coordinates are such a
> useful thing...

Well that wouldn't allow you to specify which axis to rotate around.

I implemented a "rotation operator" some time ago the syntax is:

[1,2,3]@[4,5,6];

That would rotate the vector [1,2,3] about the origin, first 4 deg
around the x axis, then 5 deg around the y axis, then 6 deg around the
z axis.

see:

http://gitorious.org/openscad/openscad/commit/daecb8020b0d5996d9a30cac88f431bd560ecb66

Regards

Giles