Commit f82f1d95 authored by Maxime Perrotin's avatar Maxime Perrotin

Add support for "x in {1,3,4}" expressions

parent b288f765
......@@ -2285,35 +2285,57 @@ def _append(expr, **kwargs):
@expression.register(ogAST.ExprIn)
def _expr_in(expr, **kwargs):
''' IN expressions: check if item is in a SEQUENCE OF '''
# Check if item is in a SEQUENCE OF
# Temporary variable needed to hold the test result
ada_string = 'tmp{}'.format(expr.tmpVar)
stmts = []
local_decl = ['{} : BOOLEAN := False;'.format(ada_string)]
left_stmts, left_str, left_local = expression(expr.left, readonly=1)
stmts, local_decl = [], []
ada_string = ""
left_stmts, left_str, left_local = expression(expr.left, readonly=1)
right_stmts, right_str, right_local = expression(expr.right, readonly=1)
stmts.extend(left_stmts)
stmts.extend(right_stmts)
local_decl.extend(left_local)
local_decl.extend(right_local)
stmts.append("in_loop_{}:".format(ada_string))
left_type = find_basic_type(expr.left.exprType)
if isinstance(expr.left, ogAST.PrimSubstring):
len_str = u"{}'Length".format(left_str)
else:
len_str = u"{}.Length".format(left_str)
left_str += u".Data"
if left_type.Min != left_type.Max:
stmts.append("for elem in 1..{} loop".format(len_str))
stmts.extend(left_stmts)
stmts.extend(right_stmts)
# it is possible to test against a raw sequence of: x in { 1,2,3 }
# in that case we create an array on the type of x, and we test
# presence using the form "for some Value of tmpXXX => x = Value"
if isinstance(expr.left, ogAST.PrimSequenceOf):
sort = type_name(expr.right.exprType)
size = expr.left.exprType.Max
local_decl.extend([u'tmp{} : constant array (1 .. {}) of {} := ({});'
.format(expr.tmpVar, size, sort, left_str)])
ada_string = u'for some var of tmp{} => var = {}'.format(expr.tmpVar,
right_str)
else:
stmts.append("for elem in {}'Range loop".format(left_str))
stmts.append("if {container}(elem) = {pattern} then".format
(container=left_str, pattern=right_str))
stmts.append("{} := True;".format(ada_string))
stmts.append("end if;")
stmts.append("exit in_loop_{tmp} when {tmp} = True;"
.format(tmp=ada_string))
stmts.append("end loop in_loop_{};".format(ada_string))
local_decl.extend([u'tmp{} : Boolean := False;'.format(expr.tmpVar)])
ada_string = u'tmp{}'.format(expr.tmpVar)
stmts.append(u"in_loop_{}:".format(ada_string))
left_type = find_basic_type(expr.left.exprType)
if isinstance(expr.left, ogAST.PrimSubstring):
len_str = u"{}'Length".format(left_str)
else:
len_str = u"{}.Length".format(left_str)
left_str += u".Data"
if left_type.Min != left_type.Max:
stmts.append(u"for elem in 1..{} loop".format(len_str))
else:
stmts.append(u"for elem in {}'Range loop".format(left_str))
stmts.append(u"if {container}(elem) = {pattern} then".format
(container=left_str, pattern=right_str))
stmts.append(u"{} := True;".format(ada_string))
stmts.append(u"end if;")
stmts.append(u"exit in_loop_{tmp} when {tmp} = True;"
.format(tmp=ada_string))
stmts.append(u"end loop in_loop_{};".format(ada_string))
return stmts, unicode(ada_string), local_decl
......
......@@ -14,14 +14,6 @@ process controlflow;
START;
/* CIF NEXTSTATE (290, 50), (70, 35) */
NEXTSTATE Wait;
/* CIF label (31, 1747), (78, 35) */
connection label1:
/* CIF PROCEDURECALL (0, 1797), (141, 35) */
call writeln('label1');
/* CIF join (53, 1847), (35, 35) */
join label2;
/* CIF End Label */
endconnection;
/* CIF label (289, 1904), (75, 35) */
connection label2:
/* CIF PROCEDURECALL (259, 1954), (136, 35) */
......@@ -34,11 +26,19 @@ process controlflow;
NEXTSTATE wait;
/* CIF End Label */
endconnection;
/* CIF label (31, 1747), (78, 35) */
connection label1:
/* CIF PROCEDURECALL (0, 1797), (141, 35) */
call writeln('label1');
/* CIF join (53, 1847), (35, 35) */
join label2;
/* CIF End Label */
endconnection;
/* CIF state (290, 50), (70, 35) */
state Wait;
/* CIF input (290, 105), (70, 35) */
input run;
/* CIF task (154, 155), (343, 35) */
/* CIF task (127, 155), (396, 35) */
task '------------------------------- Decision -------------------------------';
/* CIF task (290, 205), (70, 35) */
task n := 2;
......@@ -86,7 +86,7 @@ process controlflow;
/* CIF PROCEDURECALL (47, 841), (205, 35) */
call fail('decision closed range');
enddecision;
/* CIF task (151, 891), (348, 35) */
/* CIF task (126, 891), (398, 35) */
task '------------------------------- For loops -------------------------------';
/* CIF task (290, 941), (70, 35) */
task n := 0;
......@@ -122,9 +122,16 @@ endfor;
endfor;
/* CIF PROCEDURECALL (220, 1625), (210, 35) */
call assert(n = 34, 'for x in seq1' );
/* CIF task (163, 1675), (325, 35) */
/* CIF task (137, 1675), (376, 35) */
task '------------------------------- Join ------------------------------- ';
/* CIF join (308, 1725), (35, 35) */
/* CIF decision (285, 1730), (80, 50) */
decision n in {1,2};
/* CIF ANSWER (245, 1800), (70, 23) */
(true):
/* CIF ANSWER (335, 1800), (70, 23) */
(false):
enddecision;
/* CIF join (308, 1838), (35, 35) */
join label1;
endstate;
endprocess controlflow;
\ 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