From 0babe3ad55170beeb279861121f1e4f58dd5b523 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 May 2011 00:21:25 -0700 Subject: [PATCH] if statement decorator, unary ops, ... keyword --- moonscript/compile.lua | 15 +++++++++++++++ moonscript/parse.lua | 22 +++++++++++++++++++--- tests/inputs/syntax.moon | 25 +++++++++++++++++++++++++ tests/outputs/syntax.lua | 32 +++++++++++++++++++++++++++++++- 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 13fb86f..fc66fea 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -388,6 +388,21 @@ local compiler_index = { return node end, + minus = function(self, node) + local _, value = unpack(node) + return "-"..self:value(value) + end, + + length = function(self, node) + local _, value = unpack(node) + return "#"..self:value(value) + end, + + ["not"] = function(self, node) + local _, value = unpack(node) + return "not "..self:value(value) + end, + self = function(self, node) local _, val = unpack(node) return "self."..self:value(val) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 0071147..4628aca 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -168,7 +168,7 @@ local build_grammar = wrap(function() return false end - local Name = sym"@" * Name / mark"self" + Name + local Name = sym"@" * Name / mark"self" + Name + "..." -- make sure name is not a keyword local Name = Cmt(Name, function(str, pos, name) @@ -180,6 +180,16 @@ local build_grammar = wrap(function() return C(symx(delim)) * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string" end + -- wrap if statement if there is a conditional decorator + local function wrap_if(stm, cond) + if cond then + local pass, fail = unpack(cond) + if fail then fail = {"else", {fail}} end + return {"if", cond[2], {stm}, fail} + end + return stm + end + local function check_lua_string(str, pos, right, left) return #left == #right end @@ -189,7 +199,9 @@ local build_grammar = wrap(function() File = Block + Ct"", Block = Ct(Line * (Break^1 * Line)^0), Line = Cmt(Indent, check_indent) * Statement + _Space * Comment, - Statement = Import + If + While + Exp * Space, + + Statement = (Import + If + While + Exp * Space) * + (key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if")^-1 / wrap_if, Body = Break * InBlock + Ct(Statement), @@ -225,7 +237,11 @@ local build_grammar = wrap(function() Factor = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp", Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp", - Value = TableLit + + Value = + sym"-" * Exp / mark"minus" + + sym"#" * Exp / mark"length" + + sym"not" * Exp / mark"not" + + TableLit + Comprehension + ColonChain + Ct(KeyValueList) / mark"table" + diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index e535389..6f123be 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -101,8 +101,33 @@ argon:world().something() argon:somethin"200".world(1,2) +x = -434 + +x = -hello world one two + +hi = -"herfef" + +x = -[x for x in x] + +print "hello" if cool + +print "nutjob" + +if hello then 343 + +print "what" if cool else whack + +arg = {...} + +x = (...) -> + dump {...} +x = not true + +y = not(5+5) +y = #"hello" +x = #{#{},#{1},#{1,2}} diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index 5d7cd76..cffd1b9 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -56,4 +56,34 @@ local argon = { num = 100, world = function(self) end } something.what() argon:world().something() -argon:somethin("200").world(1, 2) \ No newline at end of file +argon:somethin("200").world(1, 2) +x = -434 +x = -hello(world(one(two))) +local hi = -"herfef" +x = -(function() + local tmp = {} + for x in x do + table.insert(tmp, x) + end + return tmp +end)() +if cool then + print("hello") +else + cool +end +print("nutjob") +if hello then + 343 +end +if cool then + print("what") +else + cool +end +local arg = { ... } +x = function(...) dump({ ... }) end +x = not true +local y = not (5 + 5) +y = #"hello" +x = #{ #{ }, #{ 1 }, #{ 1, 2 } } \ No newline at end of file