1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

GDScript: Show error on unary operators without argument

This commit is contained in:
George Marques
2021-09-15 11:08:59 -03:00
parent 5a612d61b9
commit d49046938a
11 changed files with 34 additions and 0 deletions

View File

@@ -2123,22 +2123,34 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_unary_operator(ExpressionN
operation->operation = UnaryOpNode::OP_NEGATIVE; operation->operation = UnaryOpNode::OP_NEGATIVE;
operation->variant_op = Variant::OP_NEGATE; operation->variant_op = Variant::OP_NEGATE;
operation->operand = parse_precedence(PREC_SIGN, false); operation->operand = parse_precedence(PREC_SIGN, false);
if (operation->operand == nullptr) {
push_error(R"(Expected expression after "-" operator.)");
}
break; break;
case GDScriptTokenizer::Token::PLUS: case GDScriptTokenizer::Token::PLUS:
operation->operation = UnaryOpNode::OP_POSITIVE; operation->operation = UnaryOpNode::OP_POSITIVE;
operation->variant_op = Variant::OP_POSITIVE; operation->variant_op = Variant::OP_POSITIVE;
operation->operand = parse_precedence(PREC_SIGN, false); operation->operand = parse_precedence(PREC_SIGN, false);
if (operation->operand == nullptr) {
push_error(R"(Expected expression after "+" operator.)");
}
break; break;
case GDScriptTokenizer::Token::TILDE: case GDScriptTokenizer::Token::TILDE:
operation->operation = UnaryOpNode::OP_COMPLEMENT; operation->operation = UnaryOpNode::OP_COMPLEMENT;
operation->variant_op = Variant::OP_BIT_NEGATE; operation->variant_op = Variant::OP_BIT_NEGATE;
operation->operand = parse_precedence(PREC_BIT_NOT, false); operation->operand = parse_precedence(PREC_BIT_NOT, false);
if (operation->operand == nullptr) {
push_error(R"(Expected expression after "~" operator.)");
}
break; break;
case GDScriptTokenizer::Token::NOT: case GDScriptTokenizer::Token::NOT:
case GDScriptTokenizer::Token::BANG: case GDScriptTokenizer::Token::BANG:
operation->operation = UnaryOpNode::OP_LOGIC_NOT; operation->operation = UnaryOpNode::OP_LOGIC_NOT;
operation->variant_op = Variant::OP_NOT; operation->variant_op = Variant::OP_NOT;
operation->operand = parse_precedence(PREC_LOGIC_NOT, false); operation->operand = parse_precedence(PREC_LOGIC_NOT, false);
if (operation->operand == nullptr) {
push_error(vformat(R"(Expected expression after "%s" operator.)", op_type == GDScriptTokenizer::Token::NOT ? "not" : "!"));
}
break; break;
default: default:
return nullptr; // Unreachable. return nullptr; // Unreachable.

View File

@@ -0,0 +1,2 @@
func test():
print(~)

View File

@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected expression after "~" operator.

View File

@@ -0,0 +1,2 @@
func test():
print(not)

View File

@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected expression after "not" operator.

View File

@@ -0,0 +1,2 @@
func test():
print(!)

View File

@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected expression after "!" operator.

View File

@@ -0,0 +1,3 @@
func test():
var a = 0
print(a--)

View File

@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected expression after "-" operator.

View File

@@ -0,0 +1,3 @@
func test():
var a = 0
print(a++)

View File

@@ -0,0 +1,2 @@
GDTEST_PARSER_ERROR
Expected expression after "+" operator.