|
|
|
Your skill will accomplish what the force of many cannot |
|
| PerlMonks |
Yet Another Perl-to-Lambda-Calculus Translatorby withering (Monk) |
| on Mar 19, 2014 at 03:19 UTC ( [id://1078890]=CUFP: print w/replies, xml ) | Need Help?? |
|
There is an old post about Perl (maybe perl?) and (untyped) lambda calculus http://perl.plover.com/lambda/ . However, even from the perspective of a small compiler, we may reach almost the same simplicity. The code below is written in yapp (Parse::Yapp) grammar and should be compiled with yapp utilities:
Constants are compiled to functions with trivial return values. Variables, abstraction terms and application terms are handled with some trick, which is similar to lambda lifting (http://en.wikipedia.org/wiki/Lambda_lifting). All bound variables are saved in a hash context and passed to any term in the current scope. Thus, a bound variable is accessed by getting the value of the hash entry with its variable name as the key. Abstraction and application terms are quoted in a function to separate the passing of context and arguments. Here is a test case, with lexical analysis utilities:
The test is a factorial function, implemented by using a typical call-by-value Y combinator (for references on Y combinator, see http://en.wikipedia.org/wiki/Fixed-point_combinator#Strict_fixed_point_combinator).
Back to
Cool Uses for Perl
|
|
||||||||||||||||||||||||||||||||||||