Open SeanCross opened 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
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.
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: