JohnEarnest / ok

An open-source interpreter for the K5 programming language.
MIT License
585 stars 73 forks source link

infinite loop with tacit function scan #70

Closed tangentstorm closed 4 years ago

tangentstorm commented 7 years ago
  {x<10}{x+1}\0
1 2 3 4 5 6 7 8 9 10

but:

  (10>)(1+)\0
{no output... goes into infinite loop}

Shouldn't these do the same thing?

  {10>x}(1+)\0
1 2 3 4 5 6 7 8 9 10

But (10>){1+x}\0 loops forever.

JohnEarnest commented 7 years ago

Appears to be a parser bug. Handling all the forms of this composition properly is kinda weird. Consider this:

a:5>;b:1+;a b\0

in k6 it evaluates to 0 1 2 3 4 5

matiasmorant commented 5 years ago

A {10>x}{x+1}\0 works B (10>){x+1}\0 doesn't work

The root node of the expression should be the \ adverb in both cases. In A that holds. In B the root node is the > verb with 10 as left and {x+1}\0 as right, which is an infinite loop trying to find a fixed point.

The reason is that after parsing (10>) with parseNoun, the condition node.t == 8 && !node.r is matched in parseEx, while not in the other case.

JohnEarnest commented 4 years ago

@matiasmorant's diagnosis is accurate; I've tweaked the parser to correctly fold the AST for all combinations of (nominal verb|lambda)(nominal verb|lambda)adverb.

JohnEarnest commented 4 years ago

(one of these days I really need to rewrite this damn parser from scratch. yeesh.)