mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
if statement decorator, unary ops, ... keyword
This commit is contained in:
parent
708e0dd35f
commit
0babe3ad55
@ -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)
|
||||
|
@ -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" +
|
||||
|
@ -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}}
|
||||
|
@ -56,4 +56,34 @@ local argon = { num = 100, world = function(self)
|
||||
end }
|
||||
something.what()
|
||||
argon:world().something()
|
||||
argon:somethin("200").world(1, 2)
|
||||
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 } }
|
Loading…
Reference in New Issue
Block a user