Skip to content

Commit

Permalink
libdeng2|Script: Implemented missing AND and OR operators
Browse files Browse the repository at this point in the history
Oops.
  • Loading branch information
skyjake committed Dec 8, 2012
1 parent 28749ba commit 61afbeb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion doomsday/libdeng2/include/de/scriptsys/operator.h
Expand Up @@ -58,7 +58,9 @@ namespace de
DICTIONARY,
INDEX,
SLICE,
PARENTHESIS
PARENTHESIS,
AND,
OR
};

String operatorToText(Operator op);
Expand Down
4 changes: 4 additions & 0 deletions doomsday/libdeng2/src/scriptsys/operator.cpp
Expand Up @@ -28,6 +28,10 @@ String de::operatorToText(Operator op)
{
case NOT:
return "NOT";
case AND:
return "AND";
case OR:
return "OR";
case IN:
return "IN";
case EQUAL:
Expand Down
11 changes: 10 additions & 1 deletion doomsday/libdeng2/src/scriptsys/operatorexpression.cpp
Expand Up @@ -172,6 +172,14 @@ Value *OperatorExpression::evaluate(Evaluator &evaluator) const
result = newBooleanValue(rightValue->isFalse());
break;

case AND:
result = newBooleanValue(leftValue->isTrue() && rightValue->isTrue());
break;

case OR:
result = newBooleanValue(leftValue->isTrue() || rightValue->isTrue());
break;

case EQUAL:
result = newBooleanValue(!leftValue->compare(*rightValue));
break;
Expand Down Expand Up @@ -238,7 +246,8 @@ Value *OperatorExpression::evaluate(Evaluator &evaluator) const
if(!recValue)
{
throw ScopeError("OperatorExpression::evaluate",
"Left side of " + operatorToText(_op) + " must evaluate to a record");
"Left side of " + operatorToText(_op) + " must evaluate to a record [" +
DENG2_TYPE_NAME(*leftValue) + "]");
}

// Now that we know what the scope is, push the rest of the expression
Expand Down
8 changes: 8 additions & 0 deletions doomsday/libdeng2/src/scriptsys/parser.cpp
Expand Up @@ -556,6 +556,9 @@ Expression *Parser::parseConditionalCompound(Compound &compound, CompoundFlags c
auto_ptr<Expression> condition;
if(flags.testFlag(HasCondition))
{
LOG_AS("parseConditionalCompound");
LOG_DEV_TRACE("colon at %i", colon);

TokenRange conditionRange = range.between(1, colon);
if(conditionRange.empty())
{
Expand Down Expand Up @@ -614,6 +617,9 @@ ArrayExpression *Parser::parseList(TokenRange const &range, QChar const *separat
Expression *Parser::parseExpression(TokenRange const &fullRange, Expression::Flags const &flags)
{
TokenRange range = fullRange;

LOG_AS("parseExpression");
LOG_DEV_TRACE("", range.asText());

if(!range.size())
{
Expand Down Expand Up @@ -953,6 +959,8 @@ Operator Parser::findLowestOperator(TokenRange const &range, TokenRange &leftSid
{ "*=", MULTIPLY_ASSIGN, 0, RIGHT_TO_LEFT },
{ "/=", DIVIDE_ASSIGN, 0, RIGHT_TO_LEFT },
{ "%=", MODULO_ASSIGN, 0, RIGHT_TO_LEFT },
{ "or", OR, 1, LEFT_TO_RIGHT },
{ "and", AND, 2, LEFT_TO_RIGHT },
{ "not", NOT, 3, RIGHT_TO_LEFT },
{ "in", IN, 4, LEFT_TO_RIGHT },
{ "==", EQUAL, 5, LEFT_TO_RIGHT },
Expand Down
8 changes: 8 additions & 0 deletions doomsday/tests/script/kitchen_sink.de
Expand Up @@ -139,6 +139,14 @@ print not True
print not False
print not 'Is this true?'

sections.subsection('Operators: and/or.')
print True and True
print True and False
print True or True
print True or False
print False or False
print "'and' has higher preference than 'or':", True and False or True

sections.subsection('Operators: comparisons.')
print 'Numbers:', 1 > 3, 1 < 3, 2 >= 2, 4 >= 2, 2 <= 2, 4 <= 2
print 'Text:', "hello" > "world", "hello" < "world", 'hello' == 'hello'
Expand Down

0 comments on commit 61afbeb

Please sign in to comment.