buzz-lang/Buzz

Wrong lamba parameter parsing

ilpincy opened this issue · 2 comments

The following code listings generate different behavior. (A) is correct, (B) is wrong.

# Code (A), correct
f = function(a,b,c) {
  log(a,";",b,";",c)
}
f(1,2,3)
# Code (B), wrong
if(1) {
  f = function(a,b,c) {
    log(a,";",b,";",c)
  }
}
f(1,2,3)
# Assembly (A): correct, notice the lload statements
52:	pushnil	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
53:	pushs 1	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
58:	gload	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
59:	lload 1	|3,9,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
64:	pushs 2	|3,10,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
69:	lload 2	|3,15,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
74:	pushs 2	|3,16,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
79:	lload 3	|3,21,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
84:	pushi 5	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
89:	callc	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
90:	pop	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
# Assembly (B): correct, notice the lload statements
62:	pushnil	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
63:	pushs 1	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
68:	gload	|3,7,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
69:	lload 0	|3,9,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
74:	pushs 2	|3,10,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
79:	lload 1	|3,15,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
84:	pushs 2	|3,16,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
89:	lload 2	|3,21,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
94:	pushi 5	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
99:	callc	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz
100:	pop	|4,2,/Users/cpinciroli/Fabrica/Buzz/buzz/src/testing/testfunction.bzz

The problem is related to parsing, in particular when a lambda is created inside a block (in this case the if), but the if is not itself into a function().

I don't have a fix yet, need to check how to solve it.

I'm getting output 1;2;3 with code A, and [nil];1;2 with code B.
I've just started exploring Buzz. Could you let me know which file contains the function implementation? I'm eager to contribute to your swarm robotics work.

It's not for the faint of heart, but you should look into buzzparser.c, where the assembly is generated. The function parse_if() and parse_lambda() might be a good point to start, there is likely something in there that is not properly set for this situation.