ckknight / gorillascript

GorillaScript is a compile-to-JavaScript language designed to empower the user while attempting to prevent some common errors.
MIT License
300 stars 34 forks source link

Multiline if statements? #142

Closed kkirby closed 10 years ago

kkirby commented 10 years ago

Is it possible to have multiline if statements?

if thisreallylongexpression
    and thisOtherReallyLongExpression

Thanks

McSaks commented 10 years ago
macro \\
 syntax b as Body
  ASTE $b

if thisreallylongexpression and \\
   thisOtherReallyLongExpression
     Do something

works for this simple two-line expression. Not an elegant solution, though. For multiline one it becomes

if one and \\
 two and    \\
  three and  \\
   four       \\
     Do something

with successive indentings.

I’m not a pro in writing macros and processing AST nodes; it may be better solution.

kkirby commented 10 years ago

Ha, that's pretty brilliant. Thanks!

McSaks commented 10 years ago

A bit better:

macro multiline!
 syntax op as Logic, b as Body
  let mutable vals = null
  if b.func.inspect() == 'Symbol.block'
    vals := b.args[0].args[0]
    for line, ind in b.args
      if ind
        vals := @call op, vals, line.args[0]
  else
    vals := b.args[0]
  vals

if multiline! (and)
    _ thisreallylongexpression
    _ thisOtherReallyLongExpression
    _ that.Other + Really (complex Expression or not very) and multiline! (or)
      // An ignored underscore may be replaced by any identifier
      // but not a macro, hence ‘OR’, not ‘or’:
      OR true
      OR false
  Do something

It’s somewhat tricky but seems to work.

It can be used not only with logical expressions:

let sum = multiline! (+)
  _ first.summand
  _ second summand
  _ this.is the-third

I had to use function-call syntax with ignored callee (‘_ something’ is syntactically nothing more than function call) due to GorillaScript’s annihilation of evidently-non-side-effect expressions. So,

bad-multiline! (and)
  one two
  three.four
  five()

would immediately reduce to

bad-multiline! (and)
  one two
  five()
kkirby commented 7 years ago

Coming back to this after awhile, but @McSaks you can also do:

if (and)(
    a == b,
    c == d
)
    console.log true
McSaks commented 7 years ago

@kkirby, Well, it was facepalmly obvious… in retrospective. Heh, Haven’t heard of gorilla for years, nearly forgot. It didn’t run now without a patch.

kkirby commented 7 years ago

@McSaks Yeah, check out my fork at https://github.com/kkirby/gorillascript it has a few patches keeping GorillaScript working with the newer javascript engines.

vendethiel commented 7 years ago

The only issue with that (and)(...) style is that is breaks shortcircuiting :/.