prakhar1989 / JSJS

A strongly typed language for the web!
http://jsjs-lang.org
MIT License
40 stars 5 forks source link

Need to distinguish between Lambdas as expressions and Lambdas within expressions #76

Closed gaurangsadekar closed 8 years ago

gaurangsadekar commented 8 years ago

This is correct JSJS code

val fn = /\() => {
    (/\(x) => print(x);)("lambda 1 - 1 arg");
    (/\(x, y) => print(("lambda 2 - 2 args: " ^ x ^ " " ^ y));)("foo", "bar");
    print("after 2 lambdas");
};

fn();

but it breaks in Javascript because it assumes that the 2nd lambda is an argument to the first lambda. Which is why is gives a Javascript error which is something like /\(x) is not a function. The fix to this is: During codegen, we need to enclose { } around both the generated /\s. But we can't have { } around generated /\s that are used within expressions, since that's a Javascript error. We can't do

let f = { (function (.... }

So, during codegen, we need to distinguish between 'lambdas as expressions' and 'lambdas in expressions'.

// lambdas as expressions
(/\(x) => print(x);)("lambda 1 - 1 arg");

// lambdas in expressions
val ten = (/\(x) => x + x;)(5);

and put braces only around the first case. If we do this, we don't need to add ;s after prints, as mentioned in #75 , since it'll fix both problems.