tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Extension to C language: sorry ignore prev "#TCC part


From: Jared Maddox
Subject: Re: [Tinycc-devel] Extension to C language: sorry ignore prev "#TCC part2#2"
Date: Mon, 4 Jun 2012 18:37:44 -0500

> Date: Sun, 03 Jun 2012 20:32:37 +0200
> From: Stefano Zaglio <address@hidden>
> To: address@hidden
> Subject: Re: [Tinycc-devel] Extension to C language: sorry ignore prev
>       "#TCC part2#2"
> Message-ID: <address@hidden>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Hi,
> I was a programmer asm/c/c++. Then I was Microsoftnized.
>
>  From a long time I have an idea that I'm developing too slowly.
>
> I'm drawing a comparison between C and C++ that would not consider C++
> an evolution of C.
>

Okay, but pointless. C++ is not inherently better than C (in fact, at
the moment it's a complete mess), but it IS an evolution of C. C is
not the only language in it's immediate ancestry, but it's the major
one, to the extent that some modern C programs are also modern C++
programs.


> What follows is part of a longer processing which includes different
> points of view including genetic, physical, metaphysical and
> psychological. But I do not want to bore.
>
> and I'm asking to who well know TCC code, if this could be possible:
>
>
> *int* fact(*int* n) { *return *( n==0 ? 1 : fact(n-1) ); }
>
>          printf( "factorial of 3 is " #fact(3) );
>

I object to the syntax, because the definition doesn't resemble C's
preprocessor language at all. Something like this would be better:

#function FACT( n )
# return ( (int)( n == 0 ? 1 : FACT( n - 1 ) ) )
#endfunc

Except, of course, this runs afoul of one of the design decisions of
the C preprocessor: no recursion. The preprocessor intentionally
avoids anything that can produce infinite loops, so that they simply
can't happen. It's fully intentional.

As for your example, if n is negative then it'll result in an infinite
loop, giving a very good example of why recursion is forbidden in the
C preprocessor. In addition, your specific example always returns 1 if
given a valid input, so it might as well be this:

#define FACT( n ) ( (int)1 )

Thereby saving a LOT of compilation time.

For a better example of how to implement recursion in the C
preprocessor, go look at the two proposals that I mentioned before.


> while the C++ version ... :
>
>       *template*  <int N>
>       *struct*  Factorial
>       {
>       *enum*  { value = N * Factorial<N-1>::value };
>       };
>
>       *template*  <>
>       *struct*  Factorial<1>
>       {
>       *enum*  { value = 1 };
>       };
>
>       // example use
>       *int*  main()
>       {
>       *const*  *int*  fact3 = Factorial<3>::value;
>           std::cout<<  fact3<<  endl;
>       *return*  0;
>       }
>
> is more elegant, not dangerous, unbreakable, yes, I know.
>

Yes, you know, C++ doesn't need it in the first place because of
templates themselves.


> I mean, use the ability of TCC to generate code from
>      in-code:
>
>
> *void* config_from_outsource(*char* *param) {
>          // ...
>          printf("red,green,blue");
>          }
>
>      ...
> *enum* colors {
>          #config_from_outsource("base_colors")
>          }
>

Here's an alternative:

#include <stdio.h>

int i;
long l;
char *c;

int a()
{
        if( scanf( "\nProvide integer: %d", &i) <= 0 )
        {
                return( 0 );
        }
        if( scanf( "\nProvide long: %ld", &l ) <= 0 )
        {
                return( 1 );
        }
        if( scanf( "\nProvide string: %s\n", &c ) <= 0 )
        {
                return( 2 );
        }
        
        return( 3 );
}

int res = a();

void b()
{
        switch()
        {
                case 3:
                        printf( "Compile-time arguments: %d, %ld, %s", i, l, c 
);
                        break;
                case 2:
                        printf( "Compile-time arguments: %d, %ld", i, l );
                        break;
                case 1:
                        printf( "Compile-time arguments: %d", i );
                        break;
                default:
        }
}

int main( int argn, char *argc[] )
{
        b();
        
        return( 0 );
}

This works similarly to one of the C++ template hacks, allowing code
to run at compile-time to produce data values (in this case including
memory allocations) which are later availible during run-time. And it
doesn't require adding yet ANOTHER weird behavior to the * operator
(seriously, you had to pick the one that already gets used for too
many things?).


>      ...
> *void* gen_config() {FILE f;...fopen("config.h","w+");...}
>      ...
>      #gen_config()
>      #include "config.h"
>      ...
>      #import("http://...";);
>      ...
>      #import("compressed_header.gz");
>      ...
>      #sql_parser(
>          "select * from tbl where tid=#tid and address@hidden",
>          "@tid tinyint, @id int",
>          tid, id
>          )
>      ...
>      #expand_your_imagination()
>

Import really doesn't make any sense for C. Require might (it doesn't
include a file, it just forces that file to be included NO LATER than
at that location) be useful, but import honestly is a name better
suited to a language that supports objects, such as LPC (very neat
language, but not C, so not directly applicable). And what is #import(
"http://..."; ) supposed to do, anyways?

Having hooks to add preprocessor commands would of course be nice, but
it might already be in the code. Have you looked?


> And I like that Google was financing all of this instead of spending
> money trying toreplace JavaScript with Dart, Go & C.,

Huh?


> because #TCC just
> needs a good editor for children, chicken and monkeys like me, with
> libtcc, of course, to get or create plugins.
>

Feel free to write an editor, I'd suggest SDL for the gui system.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]