www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extending syntax

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
I am writing a preprocessor for D that can add new syntaxes to D,
and have implemented noreturn statement that basically throws
an exception
int func(int x)
{
    if(x>0){}
    else if(x<0){}
    noreturn;
    //or noreturn("Some message");
}

and another iterating construct i call each, It is used like this:
int[] numbers;

(each numbers)++;

and translated to
for(int index=0;index<numbers.length; index++)
{
    numbers[index]++;
}

You can also specify an index:

(each[int i] numbers) = i*i;
===
for(int i=0;i<numbers.length; i++)
{
    numbers[i]=i*i;
}

Or use it in function calls
writef("\n",(each numbers));

But now my brain has stopped and i can't figure out how
this should work for a multidimensional case?
int[][] a; //initialized somwhere
sum += (each[int x,int y] a);
//or should it be each[int y,int x]

This should translate to what?
for(int y=0;y<a.length; y++)
{
    for(int x=0;x<a[y].length;x++)
    {
        sum+= a[x][y];
    }
}

Is this correct or did i get the indexes wrong? Should x and y
be the other way around? Comments?
Sep 30 2004
next sibling parent reply h3r3tic <foo bar.baz> writes:
Ivan Senji wrote:
 I am writing a preprocessor for D that can add new syntaxes to D

Cool, so I'm not the only one, I've done a preprocessor in Python to allow for some aspect oriented programming yet it's very limited ATM and kinda slowish (gonna rewrite in D and add functionality). I hope these 'modifications' can eventually find their way in D 2.0
 But now my brain has stopped and i can't figure out how
 this should work for a multidimensional case?
 int[][] a; //initialized somwhere
 sum += (each[int x,int y] a);
 //or should it be each[int y,int x]

Are you sure this should be done like this for more than 1 dimension ? I think it would be relatively simple to do sth like int[][] a; sum += (each(each a)); or something along these lines :] Tom
Sep 30 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"h3r3tic" <foo bar.baz> wrote in message
news:cjhqqh$1hbe$1 digitaldaemon.com...
 Ivan Senji wrote:
 I am writing a preprocessor for D that can add new syntaxes to D

Cool, so I'm not the only one, I've done a preprocessor in Python to allow for some aspect oriented programming yet it's very limited ATM and kinda slowish (gonna rewrite in D and add functionality).

Cool! Isn't it great how people all over the world can get simillar ideas!
 I hope these 'modifications' can eventually find their way in D 2.0

I'm not hoping for anything like this, just playing around, although it would be very nice.
 But now my brain has stopped and i can't figure out how
 this should work for a multidimensional case?
 int[][] a; //initialized somwhere
 sum += (each[int x,int y] a);
 //or should it be each[int y,int x]

Are you sure this should be done like this for more than 1 dimension ? I think it would be relatively simple to do sth like int[][] a; sum += (each(each a));

Well, it looks nice but it would break how things work :( I only expect one each in a statement, so it will generate a loop for only th first each and some strange looking code.
 or something along these lines :]


 Tom

Sep 30 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:cjitk8$s0$1 digitaldaemon.com...
 "h3r3tic" <foo bar.baz> wrote in message
 news:cjhqqh$1hbe$1 digitaldaemon.com...
 I hope these 'modifications' can eventually find their way in D 2.0

I'm not hoping for anything like this, just playing around, although it would be very nice.

I think your preprocessor idea is a great way for experimenting with new syntactical forms.
Oct 01 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cjk1er$pdu$1 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:cjitk8$s0$1 digitaldaemon.com...
 "h3r3tic" <foo bar.baz> wrote in message
 news:cjhqqh$1hbe$1 digitaldaemon.com...
 I hope these 'modifications' can eventually find their way in D 2.0

I'm not hoping for anything like this, just playing around, although it would be very nice.

I think your preprocessor idea is a great way for experimenting with new syntactical forms.

I thought so too. :) But I am not sure if preprocessor is the right word because it sound like C preprocessor, but it is actually parsing code and creating the syntax tree with which i am playing.
Oct 01 2004
parent reply "Walter" <newshound digitalmars.com> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:cjk4kg$se5$1 digitaldaemon.com...
 "Walter" <newshound digitalmars.com> wrote in message
 news:cjk1er$pdu$1 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:cjitk8$s0$1 digitaldaemon.com...
 "h3r3tic" <foo bar.baz> wrote in message
 news:cjhqqh$1hbe$1 digitaldaemon.com...
 I hope these 'modifications' can eventually find their way in D 2.0

I'm not hoping for anything like this, just playing around, although it would be very nice.

I think your preprocessor idea is a great way for experimenting with new syntactical forms.

I thought so too. :) But I am not sure if preprocessor is the right word because it sound like C preprocessor, but it is actually parsing code and creating the syntax tree with which i am playing.

Technically, that would be called a 'translator'. RATFOR and Cfront are particularly famous examples of the genre.
Oct 02 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:cjmhr2$2ju9$1 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:cjk4kg$se5$1 digitaldaemon.com...
 "Walter" <newshound digitalmars.com> wrote in message
 news:cjk1er$pdu$1 digitaldaemon.com...
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:cjitk8$s0$1 digitaldaemon.com...
 "h3r3tic" <foo bar.baz> wrote in message
 news:cjhqqh$1hbe$1 digitaldaemon.com...
 I hope these 'modifications' can eventually find their way in D





 I'm not hoping for anything like this, just playing around, although
 it would be very nice.

I think your preprocessor idea is a great way for experimenting with



 syntactical forms.

I thought so too. :) But I am not sure if preprocessor is the right word because it sound like C preprocessor, but it is actually parsing code


 creating the syntax tree with which i am playing.

Technically, that would be called a 'translator'. RATFOR and Cfront are particularly famous examples of the genre.

Thanks, translator is the word i have been looking for. I have to add that D is a great language to translate to. Thanks for that! BTW: I am not that great at understanding licenses, and i was wondering if the license would permit translating D's lexer to D and changing it to fit my needs? (The lexer i use is really idiotic)
Oct 02 2004
next sibling parent "Walter" <newshound digitalmars.com> writes:
"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:cjmnps$2nsm$1 digitaldaemon.com...
 BTW: I am not that great at understanding licenses, and i was wondering
 if the license would permit translating D's lexer to D and changing it to
 fit my needs? (The lexer i use is really idiotic)

Yes.
Oct 03 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Ivan Senji wrote:
 BTW: I am not that great at understanding licenses, and i was wondering
 if the license would permit translating D's lexer to D and changing it to
 fit my needs? (The lexer i use is really idiotic)

With respect to that... awhile ago, I wrote a little Python script to convert a certain subset of C++ (that which is used by DMD) to D. It handles most everything I've tested it on. (#ifdef, pointers, arrays, etc) To use it, just pass the name of the .c/.h pair as an argument, eg: python c2d.py expression It will parse expression.c and expression.h, and produce expression.d. Maybe it will save you some time. :) <http://andy.tadan.us/d/c2d.py> -- andy
Oct 03 2004
next sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Andy Friesen" <andy ikagames.com> wrote in message
news:cjpb7g$170u$1 digitaldaemon.com...
 Ivan Senji wrote:
 BTW: I am not that great at understanding licenses, and i was wondering
 if the license would permit translating D's lexer to D and changing it


 fit my needs? (The lexer i use is really idiotic)

With respect to that... awhile ago, I wrote a little Python script to convert a certain subset of C++ (that which is used by DMD) to D. It handles most everything I've tested it on. (#ifdef, pointers, arrays, etc) To use it, just pass the name of the .c/.h pair as an argument, eg: python c2d.py expression It will parse expression.c and expression.h, and produce expression.d. Maybe it will save you some time. :) <http://andy.tadan.us/d/c2d.py>

Thanks. I am sory that i don't have more time so i can explore python because i have never used it before, so when i do get some free time i think it will be easier for me to convert Walter's lexer from C++ to D.
   -- andy

Oct 04 2004
prev sibling parent reply J Thomas <jtd514 ameritech.net> writes:
hey is there any chance i can get this c2d.py from someone?

http://andy.tadan.us/d/c2d.py

the site longer exists

Andy Friesen wrote:
 Ivan Senji wrote:
 
 BTW: I am not that great at understanding licenses, and i was wondering
 if the license would permit translating D's lexer to D and changing it to
 fit my needs? (The lexer i use is really idiotic)

With respect to that... awhile ago, I wrote a little Python script to convert a certain subset of C++ (that which is used by DMD) to D. It handles most everything I've tested it on. (#ifdef, pointers, arrays, etc) To use it, just pass the name of the .c/.h pair as an argument, eg: python c2d.py expression It will parse expression.c and expression.h, and produce expression.d. Maybe it will save you some time. :) <http://andy.tadan.us/d/c2d.py> -- andy

Jul 29 2005
parent J C Calvarese <technocrat7 gmail.com> writes:
In article <dcdr7u$1vcq$1 digitaldaemon.com>, J Thomas says...
hey is there any chance i can get this c2d.py from someone?

http://andy.tadan.us/d/c2d.py

the site longer exists

Well, Andy's URL is now http://aegisknight.org/~andy/d/, but I'm not sure if that particular file is there since I'm having trouble reaching his website right now. I don't know if the server is overloaded or if the server is just ticked off at me. If the file isn't at the new website, I should have it burned onto a CD at home. If I remember, I'll try to find it this weekend. jcc7
Jul 29 2005
prev sibling next sibling parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
Ivan Senji wrote:
 I am writing a preprocessor for D that can add new syntaxes to D,
 and have implemented noreturn statement that basically throws
 an exception
 int func(int x)
 {
     if(x>0){}
     else if(x<0){}
     noreturn;
     //or noreturn("Some message");
 }
 
 and another iterating construct i call each, It is used like this:
 int[] numbers;
 
 (each numbers)++;
 
 and translated to
 for(int index=0;index<numbers.length; index++)
 {
     numbers[index]++;
 }
 
 You can also specify an index:
 
 (each[int i] numbers) = i*i;
 ===
 for(int i=0;i<numbers.length; i++)
 {
     numbers[i]=i*i;
 }
 
 Or use it in function calls
 writef("\n",(each numbers));
 
 But now my brain has stopped and i can't figure out how
 this should work for a multidimensional case?
 int[][] a; //initialized somwhere
 sum += (each[int x,int y] a);
 //or should it be each[int y,int x]
 
 This should translate to what?
 for(int y=0;y<a.length; y++)
 {
     for(int x=0;x<a[y].length;x++)
     {
         sum+= a[x][y];
     }
 }
 
 Is this correct or did i get the indexes wrong? Should x and y
 be the other way around? Comments?
 

Work on Aspect Oriented development. This would be a nice addition to D. Regards, Sjoerd
Sep 30 2004
prev sibling next sibling parent reply Burton Radons <burton-radons smocky.com> writes:
Ivan Senji wrote:

[snip]
 You can also specify an index:
 
 (each[int i] numbers) = i*i;
 ===
 for(int i=0;i<numbers.length; i++)
 {
     numbers[i]=i*i;
 }

If this is literally what your preprocessor produces, then it should produce this instead: foreach (size_t i, inout typeof (*(numbers)) __item; numbers) { __item = i * i; } Your code requires a bounds check every iteration, and might require evaluating numbers every iteration.
 Or use it in function calls
 writef("\n",(each numbers));
 
 But now my brain has stopped and i can't figure out how
 this should work for a multidimensional case?
 int[][] a; //initialized somwhere
 sum += (each[int x,int y] a);
 //or should it be each[int y,int x]

I think it would be less confusing if it were "each[int x][int y]" because comma-separated indices have special meaning in operator overloading. Declarations are read from left to right; expressions are read from right to left. So: char [x] [y] value; value [y] [x] = 0;
 This should translate to what?
 for(int y=0;y<a.length; y++)
 {
     for(int x=0;x<a[y].length;x++)
     {
         sum+= a[x][y];
     }
 }

Similarly this should be: foreach (size_t y, inout typeof (*a) __item0; a) { foreach (size_t x, inout typeof (*__item0) __item1; __item0) { sum += __item1; } }
Sep 30 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Burton Radons" <burton-radons smocky.com> wrote in message
news:cjhvlu$1v4f$1 digitaldaemon.com...
 Ivan Senji wrote:

 [snip]
 You can also specify an index:

 (each[int i] numbers) = i*i;
 ===
 for(int i=0;i<numbers.length; i++)
 {
     numbers[i]=i*i;
 }

If this is literally what your preprocessor produces, then it should produce this instead:

     foreach (size_t i, inout typeof (*(numbers)) __item; numbers)

:) I had a feeling i should use foreach but this will have to be (i think) foreach (indexTypeGivenByUser i, inout typeof (numbers[i]) __item; numbers)
     {
         __item = i * i;
     }

So i could even (maybe) iterate asociative arrays this way.
 Your code requires a bounds check every iteration, and might require
 evaluating numbers every iteration.

100% right and a good point, thanks!
 Or use it in function calls
 writef("\n",(each numbers));

 But now my brain has stopped and i can't figure out how
 this should work for a multidimensional case?
 int[][] a; //initialized somwhere
 sum += (each[int x,int y] a);
 //or should it be each[int y,int x]

I think it would be less confusing if it were "each[int x][int y]" because comma-separated indices have special meaning in operator overloading.

I know but i think this is what i tried first and got an ambiguous grammar, i'll try it again.
 Declarations are read from left to right; expressions are read from
 right to left.  So:

     char [x] [y] value;

     value [y] [x] = 0;

 This should translate to what?
 for(int y=0;y<a.length; y++)
 {
     for(int x=0;x<a[y].length;x++)
     {
         sum+= a[x][y];
     }
 }

Similarly this should be: foreach (size_t y, inout typeof (*a) __item0; a) { foreach (size_t x, inout typeof (*__item0) __item1; __item0) { sum += __item1; } }

Thanks! Again, this doesnt look bad at all. I will only have to replace *a with a[y] and *__item0 with __itemo0[x]!
Sep 30 2004
prev sibling next sibling parent reply Ant <duitoolkit yahoo.ca> writes:
On Thu, 30 Sep 2004 22:07:50 +0200, Ivan Senji wrote:

 I am writing a preprocessor for D that can add new syntaxes to D,

so can we do ctor() instead of this()? and the postfix var.cast(TYPE) ? ;) Ant
Oct 10 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Ant" <duitoolkit yahoo.ca> wrote in message
news:pan.2004.10.10.16.18.26.147697 yahoo.ca...
 On Thu, 30 Sep 2004 22:07:50 +0200, Ivan Senji wrote:

 I am writing a preprocessor for D that can add new syntaxes to D,

so can we do ctor() instead of this()? and the postfix var.cast(TYPE) ?

Well i like this so the first answer is no ;)- I'm going to do the postfix cast these days. And add it to dsource, it will be part of lr-lalr project. So anyone can add things.
 ;)

 Ant

Oct 10 2004
prev sibling parent reply X <X_member pathlink.com> writes:
In article <cjhp3l$1coi$1 digitaldaemon.com>, Ivan Senji says...
I am writing a preprocessor for D that can add new syntaxes to D,
and have implemented noreturn statement that basically throws
an exception
int func(int x)
{
    if(x>0){}
    else if(x<0){}
    noreturn;
    //or noreturn("Some message");
}

and another iterating construct i call each, It is used like this:
int[] numbers;

(each numbers)++;

and translated to
for(int index=0;index<numbers.length; index++)
{
    numbers[index]++;
}

You can also specify an index:

(each[int i] numbers) = i*i;
===
for(int i=0;i<numbers.length; i++)
{
    numbers[i]=i*i;
}

Or use it in function calls
writef("\n",(each numbers));

But now my brain has stopped and i can't figure out how
this should work for a multidimensional case?
int[][] a; //initialized somwhere
sum += (each[int x,int y] a);
//or should it be each[int y,int x]

This should translate to what?
for(int y=0;y<a.length; y++)
{
    for(int x=0;x<a[y].length;x++)
    {
        sum+= a[x][y];
    }
}

Is this correct or did i get the indexes wrong? Should x and y
be the other way around? Comments?

This is very cool! A substitute for array arith ops till 2.0 maybe :) ? Lots of other cool uses Im sure. Where can we get it ? X
Oct 11 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"X" <X_member pathlink.com> wrote in message
news:ckebi9$1msk$1 digitaldaemon.com...
 This is very cool!  A substitute for array arith ops till 2.0 maybe :) ?

 of other cool uses Im sure.

Thanks. Each looks like a good substitute for array ops.
 Where can we get it ?

It will be added to lr-lalr parser project on dsource in a couple of days but it is still mostly a toy because i don't have much time to conect it to a real lexer (like Walter's).
 X

Oct 11 2004