diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index be33b6c..553fe88 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -57,6 +57,13 @@ value_compile = { local _, delim, inner, delim_end = unpack(node) return delim .. inner .. (delim_end or delim) end, + with = function(self, node) + do + local _with_0 = self:block("(function()", "end)()") + _with_0:stm(node, returner) + return _with_0 + end + end, ["if"] = function(self, node) do local _with_0 = self:block("(function()", "end)()") diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 64e03a6..cfd77d8 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -38,6 +38,10 @@ value_compile = _, delim, inner, delim_end = unpack node delim..inner..(delim_end or delim) + with: (node) => + with @block "(function()", "end)()" + \stm node, returner + if: (node) => with @block "(function()", "end)()" \stm node, returner diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 6c2251d..754868f 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -244,7 +244,7 @@ local build_grammar = wrap(function() Block = Ct(Line * (Break^1 * Line)^0), Line = Cmt(Indent, check_indent) * Statement + _Space * Comment, - Statement = (Import + While + With + For + Foreach + Statement = (Import + While + With + For + Foreach + Return + ClassDecl + Export + BreakLoop + Ct(ExpList) / flatten_or_mark"explist" * Space) * ( -- statement decorators key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" + @@ -264,6 +264,8 @@ local build_grammar = wrap(function() BreakLoop = Ct(key"break"/trim), + Return = key"return" * (ExpListLow/ mark"explist") / mark"return", + With = key"with" * Exp * key"do"^-1 * Body / mark"with", If = key"if" * Exp * key"then"^-1 * Body * @@ -300,6 +302,7 @@ local build_grammar = wrap(function() SimpleValue = If + + With + sym"-" * -SomeSpace * Exp / mark"minus" + sym"#" * Exp / mark"length" + sym"not" * Exp / mark"not" + diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index 4f10acc..5aca4b3 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -37,6 +37,21 @@ yeah = ((1+5)*3)/2 + i % 100 whoa = (1+2) * (3+4) * (4+5) +-> + if something + return 1,2,4 + + print "hello" + +-> + if hello + "heloo", "world" + else + no, way + + +-> 1,2,34 + return 5 + () -> 4 + 2 return 5 + (() -> 4) + 2 diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index b9197b4..de57436 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -24,12 +24,28 @@ local yeah = (1 + 5) * 3 yeah = ((1 + 5) * 3) / 2 yeah = ((1 + 5) * 3) / 2 + i % 100 local whoa = (1 + 2) * (3 + 4) * (4 + 5) -return(5 + function() +_ = function() + if something then + return 1, 2, 4 + end + return print("hello") +end +_ = function() + if hello then + return "heloo", "world" + else + return no, way + end +end +_ = function() + return 1, 2, 34 +end +return 5 + function() return 4 + 2 -end) -return(5 + (function() +end +return 5 + (function() return 4 -end) + 2) +end) + 2 print(5 + function() _ = 34 return good(nads) diff --git a/todo b/todo index e538491..8e7a02e 100644 --- a/todo +++ b/todo @@ -3,13 +3,14 @@ - use `with` as an arugment to function (normal expression) - varargs that get put in a nested generated function aren't valid anymore: +- cascading expressions in function +- assigning a for loop + - class expressions, x = class extends Hello do new: => print "hello" * issues with whitespace on the end of lines -* default arguments * multiline comments -* table slices -* add a proper return statement +* table slices (almost) * add continue keyword (ouch) * vim syntax file