3

I have PEG grammar problem with lambda expressions, they work if I use syntax:

x:{y:{x+y}}(20)(30)

which is equivalent of

(function(x) { return function(y) { return x+y; }; })(20)(30);

but this don't work

f:{f(10)}(x:{x*x})

which is equivalent of:

(function(f) { return f(10); })(function(x) { return x*x; })

Is it possible to make that second function work with PEG.js?

L_7337
  • 2,650
  • 28
  • 42
jcubic
  • 61,973
  • 54
  • 229
  • 402

1 Answers1

2

After some trial and error on the online grammar parser, I found that this works:

f:{f}(x:{x*x})(10)
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • It's because it create from left to right first create identity function then pass a function to that function and then execute that function. Is it possible to make it work from right to left like normal evaluation where it first execute arguments? – jcubic Sep 03 '13 at 19:28
  • That seems to be a limitation of the grammar you're using, but I don't know the answer. Maybe if you modify the grammar or create your own. I never used PEGjs before, so I can't help you much further... – bfavaretto Sep 03 '13 at 19:45