Giter Club home page Giter Club logo

Comments (8)

jashkenas avatar jashkenas commented on July 23, 2024

Definitely using the issue tracker -- this is the right place.

For future reference, you can load "coffee-script" like any other Narwhal module. To check what a function compiles as from within the REPL, you can do this:

cs> coffee = require('coffee-script')
[object Object]
cs> coffee.compile("(square: x => x * x.)(4)")
square = function(x) {
  return x * x;
}(4);

In the REPL, things compile a little differently, without "var" keywords at the top level, or the function wrapper, in order to add every input line to global scope by default.

If you want to quickly check compilation, I'd use coffee-script -e "(square: x => x * x.)(4)"

Unfortunately, your problem is a little bit deeper than at first glance. Your parenthesis are getting ignored because they contain an assignment, which is considered to be a statement (because it might have to declare "var", which would be a syntax error in JS). You can't do (var square = function ... ). It would be great if we could fix this in CoffeeScript by pulling out the variable declaration outside the parenthesis, but the code generation doesn't support that at this point. It might also be better to throw a syntax error when parenthesis enclose a statement.

If you have ideas for how best to fix it, I'm all ears. Perhaps we can special case it, just for assignment, in the same manner as custom_return and custom_assign.

from coffeescript.

fitzgen avatar fitzgen commented on July 23, 2024

You're right this does get a little messy.

Off the top of my head, here is the best I can think up for special casing it:

js> coffee.compile("(square: x * x.)(4)")
var square;
(square = function (x) {
    return x * x;
})(4);

What do you think of that?

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Yeah, that would be ideal, but it's going to get complicated. Assignment can't be used as an expression in the general case unless the variable is already declared and it's wrapped in parenthesis. Take this case:

1 + 2 + (func: => 3.)()

Right now it compiles into a JS syntax error:

1 + 2 + var func = function() {
  return 3;
}();

Ideally, it would compile into this:

var func;
1 + 2 + (func = function() {
  return 3;
})();

In order to accomplish this, the inner assignment would have to push back the variable declaration through the previous ParenNode and OpNode, to tuck it in as the expression prior to the current one. Either backtracking or a multi-pass compiler is required for that (I think). Neither of which we have at the moment.

Maybe something with throw :declaration, and catching it at the Expressions level....

from coffeescript.

fitzgen avatar fitzgen commented on July 23, 2024

I would say just throw an exception then. This is pretty edge case, and its not like CoffeeScript has to match JS one-to-one for every little detail.

Maybe this can be revisited after more important things have been completed? I think there definitely should be some type of alert to the user about the current ambiguities if they try to compile something like this, though.

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Sure, you got it. I'll throw an exception if a parenthesis wraps a statement, instead of swallowing it silently. And leave this ticket open as an enhancement for future reference. If we could push back the declaration (ensuring it gets declared in proper scope), then all assignment could be used as an expression, which would be pretty cool.

from coffeescript.

fitzgen avatar fitzgen commented on July 23, 2024

Idea!

Instead of pushing back the "var" declaration, can we push it forward? I know that JS looks ahead for all vars declared and named functions (not "var foo = function ..." but "function foo () ....") and will actually read those first before executing any code. Thats why you can define functions inside other functions after the return! Very wierd.

Check out this code I just ran through firebug's console:

(function () {
    alert(1 + 2 + (foo = 3) + 4);
    alert(foo);
    alert(window.foo);
    var foo;
})();

Does this make it easier?

from coffeescript.

jashkenas avatar jashkenas commented on July 23, 2024

Thanks for pushing this issue. It made a real positive change to the language. I just pushed a commit that pulls out all variable declarations to the top of the current scope, allowing assignment to act as an expression. Pull from master and give it a try:

six: (one: 1) + (two: 2) + (three: 3)

Compiles into:

var one, six, three, two;
six = (one = 1) + (two = 2) + (three = 3);

Your original example:

(square: x => x * x.)(4)

Compiles into:

var square;
(square = function(x) {
  return x * x;
})(4);

As it turns out, we already had the Scope object handy, which knew exactly which variables were declared inside of it (as opposed to being declared within an external scope). This commit has the changes:

http://github.com/jashkenas/coffee-script/commit/9adf2e2d306bcd36eebd9e162adf6e30a374b9c8

Closing the ticket...

from coffeescript.

fitzgen avatar fitzgen commented on July 23, 2024

Don't thank me, I didn't do anything. Thank you!

from coffeescript.

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.