From b0904c170cae82d2b0947f058e7192bce07f0593 Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 13 Aug 2015 18:02:30 +0900 Subject: [PATCH] added Lua5.3's new operators --- moonscript/compile/value.lua | 3 +++ moonscript/compile/value.moon | 3 +++ moonscript/parse.lua | 6 +++--- moonscript/parse.moon | 5 +++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 8f8e8f2..c71a45b 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -276,6 +276,9 @@ return { number = function(self, node) return node[2] end, + bitnot = function(self, node) + return self:line("~", self:value(node[2])) + end, length = function(self, node) return self:line("#", self:value(node[2])) end, diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 515fed8..d1c9fae 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -166,6 +166,9 @@ string_chars = { number: (node) => node[2] + bitnot: (node) => + @line "~", @value node[2] + length: (node) => @line "#", @value node[2] diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 1ec4f11..920668b 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -145,12 +145,12 @@ local build_grammar = wrap_env(debug_grammar, function(root) CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"), - CharOperators = Space * C(S("+-*/%^><")), - WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op(".."), + CharOperators = Space * C(S("+-*/%^><|&")), + WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op("..") + op("<<") + op(">>") + op("//"), BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"), - SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, + SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + sym("~") * Exp / mark("bitnot") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, ChainValue = StringChain + ((Chain + DotChain + Callable) * Ct(InvokeArgs ^ -1)) / flatten_func, Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue), SliceValue = SimpleValue + ChainValue, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0dd07a0..ce4b221 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -179,8 +179,8 @@ build_grammar = wrap_env debug_grammar, (root) -> Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign" Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update" - CharOperators: Space * C(S"+-*/%^><") - WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + CharOperators: Space * C(S"+-*/%^><|&") + WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<<" + op">>" + op"//" BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0 Assignable: Cmt(DotChain + Chain, check_assignable) + Name + SelfName @@ -195,6 +195,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Cmt(Do, check_do) + sym"-" * -SomeSpace * Exp / mark"minus" + sym"#" * Exp / mark"length" + + sym"~" * Exp / mark"bitnot" + key"not" * Exp / mark"not" + TblComprehension + TableLit +