Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested for-loops contain an error #37

Closed
skx opened this issue Sep 30, 2018 · 1 comment
Closed

Nested for-loops contain an error #37

skx opened this issue Sep 30, 2018 · 1 comment

Comments

@skx
Copy link
Owner

skx commented Sep 30, 2018

This code works:

function array.sort() {
   let pass = 0;

   for( ! self.sorted?() ) {
      let i = 1;
      let l = len(self);
      for( i < l ) {
         if ( self[i] < self[i-1] ) {
           self = self.swap( i-1, i);
         }
         i++;
      }
      pass +=1;
   }
   return self;
 }

However remove the use of pass and it panics. Seems to be the return-value of the for looks is causing isseus - suspect it doesn't return a value and it should.

skx added a commit that referenced this issue Sep 30, 2018
This works with arrays of the same time, but not mixed types.
For example:

    // Now sort some ints
    let a = [ 32, 2, 33, 1, -1 ];
    puts( "Original Array: " , string(a), "\n" );
    let a = a.sort();
    puts( "Sorted Array: " , string(a), "\n" );

    // Now sort some strings
    let a = [ "Steve", "Kemp", "Kirsi" ];
    puts( "Original Array: " , string(a), "\n" );
    let a = a.sort();
    puts( "Sorted Array: " , string(a), "\n" );

This closes #36, but has revealed a new bug in the process, #37.
@skx
Copy link
Owner Author

skx commented Sep 30, 2018

Simple reproducer - I was correct nested for loops are a problem:

  function foo() {
    // outer-loop
    let i = 0;
    for( i < 10 ) {
       i++;
  
       // inner-loop
       let j = 0;
       for( j < 10 ) {
          j++;
       }
    }
  }
  foo();

Sample usage:

      frodo ~/go/src/github.com/skx/monkey $ implant ; go build . ; ./monkey crash.in 
      panic: runtime error: invalid memory address or nil pointer dereference
      [signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x4defcb]

      goroutine 1 [running]:
      github.com/skx/monkey/evaluator.evalForLoopExpression(0xc00004e140, 0xc000044fa0, 0x8, 0x8)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:593 +0x15b
      github.com/skx/monkey/evaluator.Eval(0x5648a0, 0xc00004e140, 0xc000044fa0, 0x5648a0, 0xc00004e140)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:67 +0x1a1f
      github.com/skx/monkey/evaluator.Eval(0x564820, 0xc00005c540, 0xc000044fa0, 0x564820, 0xc00005c540)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:35 +0xea0
      github.com/skx/monkey/evaluator.evalBlockStatement(0xc00004e0c0, 0xc000044fa0, 0x40ad03, 0x203000)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:161 +0x96
      github.com/skx/monkey/evaluator.Eval(0x564720, 0xc00004e0c0, 0xc000044fa0, 0x0, 0xc000044fa0)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:63 +0x108c
      github.com/skx/monkey/evaluator.applyFunction(0x565760, 0xc0000f7a00, 0x0, 0x0, 0x0, 0x0, 0x0)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:832 +0x1db
      github.com/skx/monkey/evaluator.Eval(0x5647a0, 0xc000086190, 0xc0000444e0, 0x5647a0, 0xc000086190)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:119 +0x24b
      github.com/skx/monkey/evaluator.Eval(0x564820, 0xc00005c840, 0xc0000444e0, 0x564820, 0xc00005c840)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:35 +0xea0
      github.com/skx/monkey/evaluator.evalProgram(0xc00000a0e0, 0xc0000444e0, 0x565820, 0x65a800)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:619 +0x9b
      github.com/skx/monkey/evaluator.Eval(0x564be0, 0xc00000a0e0, 0xc0000444e0, 0x565820, 0x65a800)
/home/skx/go/src/github.com/skx/monkey/evaluator/evaluator.go:33 +0x501
      main.Execute(0xc00008c000, 0xa6, 0xa6)
/home/skx/go/src/github.com/skx/monkey/monkey.go:102 +0x4a4
      main.main()
/home/skx/go/src/github.com/skx/monkey/monkey.go:121 +0xad

Fun bug :)

I've seen it before though, this is exactly the same root-cause as :

(i.e. "for" should return a value. Doesn't matter what it is. Even nil would do.)

@skx skx closed this as completed in 8c55d29 Sep 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant