Commit 994c2754 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Added short-circuit evaluation syntax

parent 7784ec59
......@@ -931,8 +931,11 @@ def _bitwise_operators(expr):
ada_string += u", Length => {left}.Length".format(left=left_str)
ada_string += u')'
else:
ada_string = u'({left} {op} {right})'.format(
left=left_str, op=expr.operand, right=right_str)
ada_string = u'({left} {op}{short} {right})'.format(
left=left_str,
op=expr.operand,
short=expr.shortcircuit,
right=right_str)
code.extend(left_stmts)
code.extend(right_stmts)
local_decl.extend(left_local)
......
......@@ -2,7 +2,7 @@
 
# Resource object code
#
# Created: Fri Jun 27 18:30:08 2014
# Created: Wed Jul 2 08:06:50 2014
# by: The Resource Compiler for PySide (Qt v4.8.6)
#
# WARNING! All changes made in this file will be lost!
......@@ -76,14 +76,19 @@ class ExprMinus(Expression):
class ExprOr(Expression):
operand = 'or'
# Indicate that short circuit form is used ("or else")
shortcircuit = ''
class ExprAnd(Expression):
operand = 'and'
# Indicate that short circuit form is used ("and then")
shortcircuit = ''
class ExprXor(Expression):
operand = 'xor'
shortcircuit = ''
class ExprEq(Expression):
......
......@@ -1282,6 +1282,22 @@ def expression(root, context):
root.getCharPositionInLine())
expr.exprType = UNKNOWN_TYPE
if root.type in (lexer.OR, lexer.AND):
# detect optional THEN in AND/OR expressions, indicating that the
# short-circuit version of the operator is needed, to prevent the
# evaluation of the right part if the left part does not evaluate
# to true.
for idx, val in enumerate(root.children):
if val.type == lexer.THEN:
expr.shortcircuit = ' then'
root.children.pop(idx)
break
elif val.type == lexer.ELSE:
expr.shortcircuit = ' else'
root.children.pop(idx)
break
if root.type in (lexer.PLUS,
lexer.ASTERISK,
lexer.DASH,
......
......@@ -796,8 +796,8 @@ field_selection
: (('!'|'.') field_name);
expression : operand0 ( IMPLIES^ operand0)* ;
operand0 : operand1 (( OR^ | XOR^ ) operand1)*;
operand1 : operand2 ( AND^ operand2)*;
operand0 : operand1 (( (OR^ ELSE?) | XOR^ ) operand1)*;
operand1 : operand2 ( AND^ THEN? operand2)*;
operand2 : operand3
(( EQ^ | NEQ^ | GT^ | GE^ | LT^ | LE^ | IN^ )
operand3)*;
......
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-06-27 18:30:11
# $ANTLR 3.1.3 Mar 17, 2009 19:23:44 sdl92.g 2014-07-02 08:11:12
import sys
from antlr3 import *
......
This diff is collapsed.
/* CIF PROCESS (148, 150), (150, 75) */
PROCESS myfunction;
/* CIF TEXT (-533, -82), (271, 323) */
-- Timers defined in the interface view
/* CIF TEXT (0, 17), (271, 323) */
-- Timers defined in the interface view
-- Use SET_TIMER (value, timer name)
-- and RESET_TIMER (timer name) in a
-- PROCEDURE CALL symbol to use them
......@@ -21,66 +21,72 @@ dcl someInt t_uInt8 := 2;
-- To test 'op_not'
dcl opnot T_boolean := not true;
/* CIF ENDTEXT */
/* CIF PROCEDURE (358, 391), (91, 35) */
PROCEDURE factorial;
/* CIF TEXT (29, 42), (215, 48) */
fpar in N MyInteger,
/* CIF ENDTEXT */
/* CIF PROCEDURE (891, 490), (91, 35) */
PROCEDURE factorial;
/* CIF TEXT (29, 42), (215, 48) */
fpar in N MyInteger,
in/out result MyInteger;
/* CIF ENDTEXT */
/* CIF START (298, 33), (70, 35) */
START;
/* CIF TASK (284, 83), (97, 35) */
TASK result := 1;
/* CIF TASK (223, 133), (220, 53) */
TASK for x in range(1, n+1):
/* CIF ENDTEXT */
/* CIF START (298, 33), (70, 35) */
START;
/* CIF TASK (284, 83), (97, 35) */
TASK result := 1;
/* CIF TASK (223, 133), (220, 53) */
TASK for x in range(1, n+1):
result := (result * x) mod 255
endfor;
/* CIF RETURN (315, 201), (35, 35) */
RETURN ;
ENDPROCEDURE;
/* CIF START (58, -99), (100, 50) */
START;
/* CIF NEXTSTATE (58, -34), (100, 50) */
NEXTSTATE Wait;
/* CIF STATE (58, -34), (100, 50) */
STATE Wait;
/* CIF INPUT (277, 36), (100, 50) */
INPUT mytimer;
/* CIF PROCEDURECALL (259, 101), (135, 50) */
CALL writeln
/* CIF RETURN (315, 201), (35, 35) */
RETURN ;
ENDPROCEDURE;
/* CIF START (591, 0), (100, 50) */
START;
/* CIF DECISION (516, 65), (250, 50) */
DECISION someint = 5 and then somereal=1.1
or else someint = 6;
/* CIF ANSWER (561, 135), (70, 23) */
(true):
/* CIF ANSWER (651, 135), (70, 23) */
(false):
ENDDECISION;
/* CIF NEXTSTATE (591, 183), (100, 50) */
NEXTSTATE Wait;
/* CIF STATE (591, 183), (100, 50) */
STATE Wait;
/* CIF INPUT (810, 253), (100, 50) */
INPUT mytimer;
/* CIF PROCEDURECALL (792, 318), (135, 50) */
CALL writeln
('timer expired');
/* CIF PROCEDURECALL (247, 166), (160, 35) */
CALL factorial(3, someint);
/* CIF NEXTSTATE (277, 216), (100, 50) */
NEXTSTATE Wait;
/* CIF INPUT (-83, 36), (198, 50) */
INPUT start_something (toto);
/* CIF OUTPUT (-119, 101), (270, 50) */
OUTPUT result_data((toto+1) mod 2147483647);
/* CIF PROCEDURECALL (-59, 166), (150, 50) */
CALL writeln
/* CIF PROCEDURECALL (780, 383), (160, 35) */
CALL factorial(3, someint);
/* CIF NEXTSTATE (810, 433), (100, 50) */
NEXTSTATE Wait;
/* CIF INPUT (450, 253), (198, 50) */
INPUT start_something (toto);
/* CIF OUTPUT (414, 318), (270, 50) */
OUTPUT result_data((toto+1) mod 2147483647);
/* CIF PROCEDURECALL (474, 383), (150, 50) */
CALL writeln
('Hello Toto', toto);
/* CIF PROCEDURECALL (-50, 231), (132, 50) */
CALL set_timer
/* CIF PROCEDURECALL (483, 448), (132, 50) */
CALL set_timer
(1000, myTimer);
/* CIF TASK (-142, 296), (316, 35) */
TASK someReal := power(someReal, someInt mod 1);
/* CIF PROCEDURECALL (-154, 346), (340, 35) */
CALL writeln('1.5 ** 2 == ', someReal, ' (should be 2.25 )');
/* CIF TASK (-84, 396), (201, 35) */
TASK someReal := float(someInt);
/* CIF TASK (-104, 446), (241, 35) */
TASK someInt := fix(someReal) mod 255;
/* CIF TASK (-60, 496), (152, 35) */
TASK opnot := not opnot;
/* CIF TASK (-62, 546), (157, 35) */
TASK someint := -someint;
/* CIF TASK (-66, 596), (164, 35) */
TASK someint := (-8) mod 5;
/* CIF NEXTSTATE (-35, 646), (102, 50) */
NEXTSTATE wait;
ENDSTATE;
/* CIF TASK (391, 513), (316, 35) */
TASK someReal := power(someReal, someInt mod 1);
/* CIF PROCEDURECALL (379, 563), (340, 35) */
CALL writeln('1.5 ** 2 == ', someReal, ' (should be 2.25 )');
/* CIF TASK (448, 613), (201, 35) */
TASK someReal := float(someInt);
/* CIF TASK (428, 663), (241, 35) */
TASK someInt := fix(someReal) mod 255;
/* CIF TASK (473, 713), (152, 35) */
TASK opnot := not opnot;
/* CIF TASK (470, 763), (157, 35) */
TASK someint := -someint;
/* CIF TASK (467, 813), (164, 35) */
TASK someint := (-8) mod 5;
/* CIF NEXTSTATE (498, 863), (102, 50) */
NEXTSTATE wait;
ENDSTATE;
ENDPROCESS myfunction;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment