tinycc-devel
[Top][All Lists]
Advanced

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

Re: [Tinycc-devel] Dollar Sign in Identifiers Extension


From: Christian Jullien
Subject: Re: [Tinycc-devel] Dollar Sign in Identifiers Extension
Date: Mon, 13 Apr 2015 06:57:00 +0200

Good luck for nested function implementers that (as is Lisp :o) will want to
make this code working!

typedef int (*fp) (uint32_t i);

int
callback(fp fn, uint32_t i)
        fn(i);{
}

uint32_t
isEven(uint32_t i) {
        uint32_t odd(uint32_t i) {
                if (i == 0) return 0;
                if (i == 1) return 1;
                retrun even(i - 1);
        }
        uint32_t even(uint32_t i) {
                if (i == 0) return 1;
                if (i == 1) return 0;
                retrun odd(i - 1);
        }
        callback(even, i);
}

C is not prepared to support nested function unless you rewrite whole
calling convention mechanism that will break all known libraries.
Don't waste your time and energy, it will not work.

M2c

Christian

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of Jared
Maddox
Sent: dimanche 12 avril 2015 21:37
To: address@hidden
Subject: Re: [Tinycc-devel] Dollar Sign in Identifiers Extension

> Date: Sat, 11 Apr 2015 17:31:30 +0300
> From: Sergey Korshunoff <address@hidden>
> To: address@hidden
> Subject: Re: [Tinycc-devel] Dollar Sign in Identifiers Extension
> Message-ID:
>         
> <address@hidden>
> Content-Type: text/plain; charset=UTF-8
>
> Hi, Daniel Holden.
>> I'm mainly interested in this change because I'm working on a new 
>> version of my library Cello: http://libcello.org/ which uses `$` and 
>> several variations of as macros.
>
> >From libCello docs: Lambda ... GNU99 C has a great feature that lets
> you define functions inside functions
>
> tcc currently don't have a nested functions implemented. This is a 
> nice feature. But it is not implemented :-(
>

As has been pointed out, within the context of C this is actually a
mis-feature. However, if you REALLY like it, here's how to do it
correctly:

1) Allow static functions to be defined inside of other functions:
their namespacing should work like a static variable declared inside a
function.

2) Implement a feature to return a pointer to the current "stack reference",
whatever that is for the current architecture (on x86, presumably BP). You
may want to add a special pointer type or something.

3) Implement a feature to allow a function to refer to another function
invocation's arguments & local vars if it's both defined inside the relevant
scope, and given a pointer to the invocation's "stack reference".



If this sounds like a lot of work, well, yeah. It's absolutely the right way
to have "proper" nested functions in C, though: the GCC style is appropriate
for higher-level languages like LISP (which I assume was the point of
inspiration), but not for lower-level languages like C. Step 1 could also be
pretty nice all by itself, if the functions so declared were allowed to
access all static variables that could be accessed by the enclosing function
at that point of definition.

_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel




reply via email to

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