Re: Add nested-function support in a language the based on a stack-machine

Kaz Kylheku <157-073-9834@kylheku.com>
Wed, 14 Mar 2018 14:49:01 +0000 (UTC)

          From comp.compilers

Related articles
| List of all articles for this month |
From: Kaz Kylheku <157-073-9834@kylheku.com>
Newsgroups: comp.compilers
Date: Wed, 14 Mar 2018 14:49:01 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: <6effed5e-6c90-f5f4-0c80-a03c61fd2127@gkc.org.uk> 18-03-042 18-03-049 <CANOtCLVc1Rxp4SRSwD+dLXRUSpxY4igGstfcuxxLj-ydekGsjQ@mail.gmail.com> 18-03-055 18-03-060 18-03-064
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="29301"; mail-complaints-to="abuse@iecc.com"
Keywords: syntax, design
Posted-Date: 14 Mar 2018 17:36:48 EDT

On 2018-03-14, Kartik Agaram <ak@akkartik.com> wrote:
> On Tue, Mar 13, 2018 at 5:07 PM, Kaz Kylheku <157-073-9834@kylheku.com> wrote:
>> Anonymous functions without environments are basically just
>> function pointers.
>>
>> If those are first class functions, then C has first class functions.
>
> C doesn't have anonymous functions (ignoring a combination of GNU
> extensions added sometime after 1988). But you mentioned anonymous
> functions, so you must be aware of this. So I'm not sure what you're
> saying.


That's just a syntactic restriction on where functions are
placed in the program text, due to the lack of a function literal
syntax.


If functions don't require an environment, you can easily add anonymous
function literals with a textual preprocessor like GNU m4, by
implementing a completely trivial "no-environment lambda lifting".


Syntax like:


      some_fun(LAMBDA{int (int a, int b){ return a + b; }})


is simply replaced by


      some_fun(f_0013);


accompanied by the insertion of


      static int f_0013(int arg){ return arg + 1; })


into the global scope. This is easy enough that it's usually not a major
inconvenience to simply do it by hand. If the C preprocessor were
slightly more powerful, it could handle the above.


Once we obtain a function as a function pointer by evaluating its
name, then it has been anonymized. We can pass it around the program,
store it in variables and structures and so on. It has the same power
as a lambda-with-no-env.


--
TXR Programming Lanuage: http://nongnu.org/txr
Music DIY Mailing List: http://www.kylheku.com/diy
ADA MP-1 Mailing List: http://www.kylheku.com/mp1


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.