ByronMayne / LoxSharp

The C# implementation of the Lox Tutorials written by @munificentbob found at http://www.craftinginterpreters.com/
MIT License
8 stars 1 forks source link

Bug in Lox interpreter - Resolver.cs #1

Open SeanCross opened 6 years ago

SeanCross commented 6 years ago

Your stack is reversed compared to the java one, so you are iterating though the wrong way, and getting the wrong index in your resolver.

Should be private void ResolveLocal(Expr _variable, Token name) { //for (int i = m_Scopes.Count - 1; i >= 0; i--) for (int i = 0; i < m_Scopes.Count; i++) { Scope scope = m_Scopes[i]; if (scope.ContainsKey(name.lexeme)) { m_Iterpreter.Resolve(_variable, i); return; } } }

You can test with this example code:

fun fibonacci(n) {
  if (n <= 1) return n;
  return fibonacci(n - 2) + fibonacci(n - 1);
}

for (var i = 0; i < 10; i = i + 1) {
  print "fibonacci " + i + ": " + fibonacci(i);
}
ByronMayne commented 6 years ago

Thanks for pointing that out! I am not going to lie I knew there was a bug as I was seeing some weird results. I was going to try to find the bug and fix it when I got some spare time, However you solved it so I will update the code later tonight.

Cheers, Byron

SeanCross commented 6 years ago

No worries. I was looking at your implementation to try and find the bug in mine, but you had the same issue. After that I realised that any weird behaviour so far has been the difference between java and c# things that look the same but behave differently (string.substring comes to mind). A few moments of fiddling and we are both fixed.

Regards

Sean

From: Byron Mayne [mailto:notifications@github.com] Sent: Thursday, 21 December 2017 2:25 PM To: ByronMayne/LoxSharp LoxSharp@noreply.github.com Cc: Sean Cross Sean.Cross@catalystrisk.co.nz; Author author@noreply.github.com Subject: Re: [ByronMayne/LoxSharp] Bug in Lox interpreter - Resolver.cs (#1)

Thanks for pointing that out! I am not going to lie I knew there was a bug as I was seeing some weird results. I was going to try to find the bug and fix it when I got some spare time, However you solved it so I will update the code later tonight.

Cheers, Byron

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/ByronMayne/LoxSharp/issues/1#issuecomment-353231188, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ADbpRsbBeLWNlWpK93jsDaVAllyvxqI-ks5tCbNbgaJpZM4RJOr9.