Clean up the +=, -=, *= and /= operators.

Refs #5846
This commit is contained in:
Gunnar Beutner 2014-03-24 09:06:16 +01:00
parent 9adc63737a
commit 1aca29afd2
3 changed files with 32 additions and 32 deletions

View File

@ -223,65 +223,67 @@ In this example a has the value 7 after both instructions are executed.
#### <a id="operator-additive-assignment"></a> Operator += #### <a id="operator-additive-assignment"></a> Operator +=
Modifies a dictionary or array by adding new elements to it. The += operator is a shortcut. The following expression:
Example:
{ {
a = [ "hello" ], a = [ "hello" ],
a += [ "world" ] a += [ "world" ]
} }
In this example a contains both `"hello"` and `"world"`. This currently is equivalent to:
only works for dictionaries and arrays.
<!-- {
a = [ "hello" ],
a = a + [ "world" ]
}
#### <a id="operator-substractive-assignment"></a> Operator -= #### <a id="operator-substractive-assignment"></a> Operator -=
Removes elements from a dictionary. The -= operator is a shortcut. The following expression:
Example:
{ {
a = { "hello", "world" }, a = 10,
a -= [ "world" ] a -= 5
} }
In this example a contains `"hello"`. Trying to remove an item that does is equivalent to:
not exist is not an error. Not implemented yet.
{
a = 10,
a = a - 5
}
#### <a id="operator-multiply-assignment"></a> Operator \*= #### <a id="operator-multiply-assignment"></a> Operator \*=
Multiplies an existing dictionary element with the specified number. If The *= operator is a shortcut. The following expression:
the dictionary element does not already exist 0 is used as its value.
Example:
{ {
a = 60, a = 60,
a *= 5 a *= 5
} }
In this example a is 300. This only works for numbers. Not implemented is equivalent to:
yet.
{
a = 60,
a = a * 5
}
#### <a id="operator-dividing-assignment"></a> Operator /= #### <a id="operator-dividing-assignment"></a> Operator /=
Divides an existing dictionary element by the specified number. If the The /= operator is a shortcut. The following expression:
dictionary element does not already exist 0 is used as its value.
Example:
{ {
a = 300, a = 300,
a /= 5 a /= 5
} }
In this example a is 60. This only works for numbers. Not implemented is equivalent to:
yet.
--> {
a = 300,
a = a / 5
}
### <a id="indexer"></a> Indexer ### <a id="indexer"></a> Indexer

View File

@ -397,5 +397,9 @@ Value AExpression::OpSetDivide(const AExpression *expr, const Dictionary::Ptr& l
Value AExpression::OpIndexer(const AExpression *expr, const Dictionary::Ptr& locals) Value AExpression::OpIndexer(const AExpression *expr, const Dictionary::Ptr& locals)
{ {
Dictionary::Ptr dict = locals->Get(expr->m_Operand1); Dictionary::Ptr dict = locals->Get(expr->m_Operand1);
if (!dict)
BOOST_THROW_EXCEPTION(ConfigError("Script variable '" + expr->m_Operand1 + "' not set in this scope."));
return dict->Get(expr->m_Operand2); return dict->Get(expr->m_Operand2);
} }

View File

@ -547,9 +547,6 @@ lterm_items_inner: /* empty */
lterm: identifier lbinary_op rterm lterm: identifier lbinary_op rterm
{ {
AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$3); AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$3);
if ($2 == &AExpression::OpSetPlus || $2 == &AExpression::OpSetMinus || $2 == &AExpression::OpSetMultiply || $2 == &AExpression::OpSetDivide)
aexpr->MakeInline();
$$ = new Value(make_shared<AExpression>($2, $1, aexpr, DebugInfoRange(@1, @3))); $$ = new Value(make_shared<AExpression>($2, $1, aexpr, DebugInfoRange(@1, @3)));
free($1); free($1);
delete $3; delete $3;
@ -564,9 +561,6 @@ lterm: identifier lbinary_op rterm
subexprl->Add(subexpr); subexprl->Add(subexpr);
AExpression::Ptr expr = make_shared<AExpression>(&AExpression::OpDict, subexprl, DebugInfoRange(@1, @6)); AExpression::Ptr expr = make_shared<AExpression>(&AExpression::OpDict, subexprl, DebugInfoRange(@1, @6));
if ($5 == &AExpression::OpSetPlus || $5 == &AExpression::OpSetMinus || $5 == &AExpression::OpSetMultiply || $5 == &AExpression::OpSetDivide)
expr->MakeInline();
$$ = new Value(make_shared<AExpression>(&AExpression::OpSetPlus, $1, expr, DebugInfoRange(@1, @6))); $$ = new Value(make_shared<AExpression>(&AExpression::OpSetPlus, $1, expr, DebugInfoRange(@1, @6)));
free($1); free($1);
} }