www.digitalmars.com         C & C++   DMDScript  

D - C and D code in one file

reply J Anderson <REMOVEanderson badmama.com.au> writes:
Is there a way (without using pre-processors) to have both C/C++ and D 
versions of code in one file.  I was just thinking that parhaps some 
slight moderations to comments (or a new comment) could aid in this.  
Having both C++ and D code in one file could reduce redundancy if 
something needs to be written for both.

ie something like (this does not currently work)

/*+/
class x(T) //D code
{  }
/+*/

/*/+*/
template <T> class x //C++ code
{  }
/*+/*/

There's probably a better way of doing this (which I hope will be 
suggested), but I think you get the idea.

-- 
-Anderson: http://badmama.com.au/~anderson/
Jan 27 2004
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
While it was 27/1/04 4:24 pm throughout the UK, J Anderson sprinkled 
little black dots on a white screen, and they fell thus:

 Is there a way (without using pre-processors) to have both C/C++ and D 
 versions of code in one file.  I was just thinking that parhaps some 
 slight moderations to comments (or a new comment) could aid in this.  
 Having both C++ and D code in one file could reduce redundancy if 
 something needs to be written for both.

int dummy = 0 /+ 1; int main() { printf("This is C!\n"); return 0; } /* +/; int main() { printf("This is D!" \n); return 0; } */ This, of course, relies on the simplism described in thread "[Style Guide] Nested Comments". Not to mention that you'd generally need to either change the extension from time to time or use Unix file-linking. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jan 27 2004
next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Stewart Gordon wrote:

 While it was 27/1/04 4:24 pm throughout the UK, J Anderson sprinkled 
 little black dots on a white screen, and they fell thus:

 Is there a way (without using pre-processors) to have both C/C++ and 
 D versions of code in one file.  I was just thinking that parhaps 
 some slight moderations to comments (or a new comment) could aid in 
 this.  Having both C++ and D code in one file could reduce redundancy 
 if something needs to be written for both.

<snip> int dummy = 0 /+ 1; int main() { printf("This is C!\n"); return 0; } /* +/; int main() { printf("This is D!" \n); return 0; } */ This, of course, relies on the simplism described in thread "[Style Guide] Nested Comments". Not to mention that you'd generally need to either change the extension from time to time or use Unix file-linking. Stewart.

-- -Anderson: http://badmama.com.au/~anderson/
Jan 28 2004
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
While it was 27/1/04 5:17 pm throughout the UK, Stewart Gordon sprinkled 
little black dots on a white screen, and they fell thus:

<snip>
 int main() {
     printf("This is D!" \n);
     return 0;
 }
 */

Oops, this should be // */ at the end. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Jan 29 2004
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I wonder if we could add some syntax somewhat like the asm syntax:

	language(C) { ...C source... };

At first pass, I would assume that the C source would be passed without 
modification into the system's C compiler; the D compiler would then 
automatically link the C compiler's .o file with the D compiler's .o file.

But even cooler, what if the C source file automagically got prototypes 
for all of the extern(C) declarations in the D source?  Imagine this D 
source file:

	module foo;
	extern(C) int foo(byte *bar, byte* baz) { ... };
	extern(C) int C_function();
	int func();	/* since this is not extern(C),
			 * the language(C) won't see it */
	language(C) {
		extern int C_function()
		{
			...
			foo(... , ...);
			...
		}
	}

The D compiler passes this source into the C compiler:

	int foo(char *bar, char* baz) { ... };
	int C_function();
	extern int C_function()
	{
		...
		foo(... , ...);
		...
	}

(Note that the D compiler is smart enough to turn the 'byte' arguments 
into the C equivalent 'char'.).

This idea gets especially exciting when you invision that later 
compilers might support not just extern(C)/language(C), but 
extern(Java)/language(Java), extern(perl)/language(perl), and maybe more.

Thoughts?

J Anderson wrote:
 Is there a way (without using pre-processors) to have both C/C++ and D 
 versions of code in one file.  I was just thinking that parhaps some 
 slight moderations to comments (or a new comment) could aid in this.  
 Having both C++ and D code in one file could reduce redundancy if 
 something needs to be written for both.
 
 ie something like (this does not currently work)
 
 /*+/
 class x(T) //D code
 {  }
 /+*/
 
 /*/+*/
 template <T> class x //C++ code
 {  }
 /*+/*/
 
 There's probably a better way of doing this (which I hope will be 
 suggested), but I think you get the idea.
 

Jan 27 2004
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Russ Lewis wrote:

 I wonder if we could add some syntax somewhat like the asm syntax:

     language(C) { ...C source... };

 At first pass, I would assume that the C source would be passed 
 without modification into the system's C compiler; the D compiler 
 would then automatically link the C compiler's .o file with the D 
 compiler's .o file.

 But even cooler, what if the C source file automagically got 
 prototypes for all of the extern(C) declarations in the D source?  
 Imagine this D source file:

     module foo;
     extern(C) int foo(byte *bar, byte* baz) { ... };
     extern(C) int C_function();
     int func();    /* since this is not extern(C),
              * the language(C) won't see it */
     language(C) {
         extern int C_function()
         {
             ...
             foo(... , ...);
             ...
         }
     }

 The D compiler passes this source into the C compiler:

     int foo(char *bar, char* baz) { ... };
     int C_function();
     extern int C_function()
     {
         ...
         foo(... , ...);
         ...
     }

 (Note that the D compiler is smart enough to turn the 'byte' arguments 
 into the C equivalent 'char'.).

 This idea gets especially exciting when you invision that later 
 compilers might support not just extern(C)/language(C), but 
 extern(Java)/language(Java), extern(perl)/language(perl), and maybe more.

 Thoughts?

 J Anderson wrote:

 Is there a way (without using pre-processors) to have both C/C++ and 
 D versions of code in one file.  I was just thinking that parhaps 
 some slight moderations to comments (or a new comment) could aid in 
 this.  Having both C++ and D code in one file could reduce redundancy 
 if something needs to be written for both.

 ie something like (this does not currently work)

 /*+/
 class x(T) //D code
 {  }
 /+*/

 /*/+*/
 template <T> class x //C++ code
 {  }
 /*+/*/

 There's probably a better way of doing this (which I hope will be 
 suggested), but I think you get the idea.


couple of years back before I even knew about D, but I guess it's not new. I discussed it with some friends and they seemed to hate the idea. Although I think I'd be really cool to switch from language to language in code, for whatever language is suited best for the particular problem. Of course there is C# which allows you to link languages, but it's not quite the same. But this is something I strongly doubt, would never happen in the D language itself. It would need to be in a meta language. -- -Anderson: http://badmama.com.au/~anderson/
Jan 27 2004
parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
 But this is something I strongly doubt, would never happen in the D
 language itself.  It would need to be in a meta language.

One language to rule them all, One language to find them, One language to bring them all and in the darkness bind them. sorry, I couldn't resist. -Ben
Jan 27 2004
parent "C" <dont respond.com> writes:
Nice ;).

C
"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:bv6fif$7a$1 digitaldaemon.com...
 But this is something I strongly doubt, would never happen in the D
 language itself.  It would need to be in a meta language.

One language to rule them all, One language to find them, One language to bring them all and in the darkness bind them. sorry, I couldn't resist. -Ben

Jan 27 2004