Code Monkey home page Code Monkey logo

Comments (9)

gahag avatar gahag commented on August 15, 2024 4

Yes, implementing elseif is on my high priority list right now. It shouldn't take to much work to implement, but I have to find some time to do it.

from hush.

telemachus avatar telemachus commented on August 15, 2024 1

but I have to find some time to do it.

Fair enough. Thanks for the quick reply!

from hush.

deaddog avatar deaddog commented on August 15, 2024 1

I have two working solutions to this. At this point neither of them make any changes to the ast. They simply wrap nested if expressions as single statement blocks in the otherwise field of the outer if expression. The difference between the two solutions is a classic: One uses elseif while the other uses else if (as two separate tokens).
Is there a preference for which direction to go?

The issue with the elseif approach is that the ast doesn't directly represent the distinction, so the token is not represented in --ast or --program , which could potentially be confusing. On the other hand the implementation has a much smaller footprint then an extension to the ast would.

The issue with if is that I'm relying on source positioning (else and following if being on the same line) to identify the else if branch. I do that specifically to distinguish between the following two examples:

if <cond> then
  ...
else if <cond> then
  ...
end
if <cond> then
  ...
else
  if <cond> then
    ...
  end
end

I'm personally a fan of else if over a single keyword, and I think the same-line compromise is reasonable. It's also more clearly in-line with the ast - since I've left it as-is.

from hush.

gahag avatar gahag commented on August 15, 2024 1

The PR looks good! But I still feel like we should stick to not being whitespace sensitive here, so I'd insist on the elseif approach. Do you feel comfortable adjusting the PR to do so? Once again, thanks for your contribution.

from hush.

deaddog avatar deaddog commented on August 15, 2024 1

There you go: else if in #30 and elseif in #31 for comparison - feel free to reject either one.

from hush.

gahag avatar gahag commented on August 15, 2024

I vouch for the elseif approach, as Hush currently is not whitespace sensitive, and I'd like to keept it that way. If you're really inclined to else if, we could consider an implementation that works with the token sequence instead of line number. I.e., if the else token is immediately followed by an if token, then it's an else if construction. Thanks for taking you time into this issue!

from hush.

deaddog avatar deaddog commented on August 15, 2024

I generally agree on non-whitespace sensitivity, though I find this to be a reasonable approach. I am more inclined to else if but beyond that I also just want the feature implemented ;)

we could consider an implementation that works with the token sequence instead of line number. I.e., if the else token is immediately followed by an if token

The token sequence would always be else followed by if as whitespace isn't represented - nor should it be. I guess my question is what you mean by "immediately" in this context. I interpret it as "a single space-character between the two". But that would still mean that it's whitespace sensitive, so I'm not sure that's what you meant. I simply took the slightly more laxed approach that the number of spaces shouldn't matter.

When an if token follows an else token in the token sequence, the challenge is to detect if the parser should consume it with the else token as an else if sequence, or as a nested if expression with it's own final end token.

if <cond> then
  ...
else if <cond> then
  ...
end
if <cond> then
  ...
else
  if <cond> then
    ...
  end
end

from hush.

deaddog avatar deaddog commented on August 15, 2024

I've created a pull request #30. It's probably simpler to actually look at some code ;)

from hush.

gahag avatar gahag commented on August 15, 2024

Closed by #31

from hush.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.