Commit a683085a authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Support optional fields in sequence

parent 98b58eb1
......@@ -1355,7 +1355,8 @@ def _sequence(seq):
ada_string = u"{}'(".format(type_name(seq.exprType))
sep = ''
type_children = find_basic_type(seq.exprType).Children
optional_fields = {field.lower(): False
optional_fields = {field.lower().replace('-', '_'): {'present': False,
'ref': (field, val)}
for field, val in type_children.viewitems()
if val.Optional == 'True'}
present_fields = []
......@@ -1373,17 +1374,31 @@ def _sequence(seq):
ada_string += u"{} {} => {}".format(sep, elem, value_str)
if elem.lower() in optional_fields:
# Set optional field presence
optional_fields[elem.lower()] = True
optional_fields[elem.lower()]['present'] = True
sep = u', '
stmts.extend(value_stmts)
local_decl.extend(local_var)
# Process optional fields
if optional_fields:
sep = ''
absent_fields = ((fd_name, fd_data['ref'])
for fd_name, fd_data in optional_fields.viewitems()
if not fd_data['present'])
for fd_name, fd_data in absent_fields:
fd_type = fd_data[1].type
if fd_type.kind == 'ReferenceType':
value = u'{}_Init'.format(type_name(fd_type))
elif fd_type.kind == 'BooleanType':
value = u'False'
elif fd_type in ('IntegerType', 'RealType'):
value = fd_type.Min
ada_string += u'{}{} => {}'.format(sep, fd_name, value)
sep = u', '
ada_string += u', Exist => ('
for fd_name, fd_present in optional_fields.viewitems():
sep = ''
for fd_name, fd_data in optional_fields.viewitems():
ada_string += u'{}{} => {}'.format(sep, fd_name,
'1' if fd_present else '0')
'1' if fd_data['present'] else '0')
sep = u', '
ada_string += u')'
ada_string += ')'
......
......@@ -688,32 +688,40 @@ def check_type_compatibility(primary, type_ref, context):
and basic_type.kind == 'SequenceType':
user_nb_elem = len(primary.value.keys())
type_nb_elem = len(basic_type.Children.keys())
if user_nb_elem != type_nb_elem:
raise TypeError('Wrong number of fields in SEQUENCE of type {}'
.format(type_name(type_ref)))
else:
for field, fd_data in basic_type.Children.viewitems():
ufield = field.replace('-', '_')
if ufield not in primary.value:
raise TypeError('Missing field {field} in SEQUENCE'
' of type {t1} '
.format(field=ufield,
t1=type_name(type_ref)))
optional_fields = [field.lower().replace('-', '_')
for field, val in basic_type.Children.viewitems()
if val.Optional == 'True']
user_fields = [field.lower() for field in primary.value.keys()]
# if user_nb_elem != type_nb_elem:
# raise TypeError('Wrong number of fields in SEQUENCE of type {}'
# .format(type_name(type_ref)))
# else:
for field, fd_data in basic_type.Children.viewitems():
ufield = field.replace('-', '_')
if ufield.lower() not in optional_fields \
and ufield.lower() not in user_fields:
raise TypeError('Missing mandatory field {field} in SEQUENCE'
' of type {t1} '
.format(field=ufield,
t1=type_name(type_ref)))
elif field.lower() not in user_fields:
# Optional field not set - OK
continue
else:
# If the user field is a raw value
if primary.value[ufield].is_raw:
check_type_compatibility(primary.value[ufield],
fd_data.type, context)
else:
# If the user field is a raw value
if primary.value[ufield].is_raw:
check_type_compatibility(primary.value[ufield],
fd_data.type, context)
else:
# Compare the types for semantic equivalence
try:
compare_types(
primary.value[ufield].exprType, fd_data.type)
except TypeError as err:
raise TypeError('Field ' + ufield +
' is not of the proper type, i.e. ' +
type_name(fd_data.type) +
' - ' + str(err))
# Compare the types for semantic equivalence
try:
compare_types(
primary.value[ufield].exprType, fd_data.type)
except TypeError as err:
raise TypeError('Field ' + ufield +
' is not of the proper type, i.e. ' +
type_name(fd_data.type) +
' - ' + str(err))
return
elif isinstance(primary, ogAST.PrimChoiceItem) \
and basic_type.kind.startswith('Choice'):
......
......@@ -16,7 +16,7 @@ SeqEnumFix ::= SEQUENCE (SIZE(2)) OF ENUMERATED { hello, world }
Enum ::= ENUMERATED {a, b, c, d, eE}
Choice ::= CHOICE {c1 BOOLEAN, cDe2 BOOLEAN}
SeqOpt ::= SEQUENCE { a BOOLEAN OPTIONAL }
SeqOpt ::= SEQUENCE { a BOOLEAN OPTIONAL , b BOOLEAN, c SEQUENCE { a BOOLEAN } OPTIONAL, d OCTET STRING (SIZE (0..10)) OPTIONAL}
SeqNonOpt ::= SEQUENCE { a BOOLEAN }
END
......
......@@ -2,7 +2,7 @@
PROCESS orchestrator
/* CIF COMMENT (405, 192), (71, 35) */
COMMENT 'Hello';
/* CIF TEXT (39, 94), (293, 293) */
/* CIF TEXT (39, 94), (389, 293) */
dcl seq tastE_Peek_id_list;
dcl fixed FixedString := 'Hello';
......@@ -21,97 +21,101 @@ dcl check tasTE_Peek_id;
dcl choice choice;
dcl opt SeqOpt := { a TRUE };
dcl opt SeqOpt := { a TRUE, b FALSE }; -- test optional fields
/* CIF ENDTEXT */
/* CIF PROCEDURE (727, 175), (106, 35) */
PROCEDURE emptyproc;
ENDPROCEDURE;
/* CIF START (438, 223), (80, 36) */
/* CIF START (502, 224), (80, 36) */
START;
/* CIF DECISION (428, 274), (99, 50) */
/* CIF TASK (474, 275), (136, 35) */
TASK opt := { b TRUE }
/* CIF COMMENT (630, 275), (155, 35) */
COMMENT 'Test optional fields';
/* CIF DECISION (492, 325), (99, 50) */
DECISION 'informal'
/* CIF COMMENT (547, 281), (179, 53) */
/* CIF COMMENT (611, 332), (179, 53) */
COMMENT 'Informal decision -
check that it is ignored
by the code generator';
/* CIF ANSWER (398, 344), (70, 33) */
/* CIF ANSWER (462, 395), (70, 33) */
('a'):
/* CIF ANSWER (488, 344), (70, 33) */
/* CIF ANSWER (552, 395), (70, 33) */
('2'):
ENDDECISION;
/* CIF TASK (366, 392), (223, 38) */
/* CIF TASK (430, 443), (223, 38) */
TASK seqboolean := {true, false},
seqboolean := not {true, false};
/* CIF TASK (396, 445), (164, 53) */
/* CIF TASK (460, 496), (164, 53) */
TASK for x in seqen:
call writeln(num(x));
endfor;
/* CIF TASK (396, 513), (164, 53) */
/* CIF TASK (460, 564), (164, 53) */
TASK for x in seqen2:
call writeln(num(x));
endfor;
/* CIF TASK (397, 581), (162, 53) */
/* CIF TASK (461, 632), (162, 53) */
TASK for x in seqboolean:
call writeln(x);
endfor
/* CIF COMMENT (579, 590), (168, 38) */
/* CIF COMMENT (643, 641), (168, 38) */
COMMENT 'FOR with a basic type';
/* CIF TASK (367, 649), (222, 35) */
/* CIF TASK (431, 700), (222, 35) */
TASK seqboolean := not seqboolean
/* CIF COMMENT (609, 649), (279, 35) */
/* CIF COMMENT (673, 700), (279, 35) */
COMMENT 'check NOT on a SEQUENCE of BOOLEAN';
/* CIF TASK (397, 699), (162, 53) */
/* CIF TASK (461, 750), (162, 53) */
TASK for x in seqboolean:
call writeln(x);
endfor;
/* CIF TASK (415, 767), (126, 38) */
/* CIF TASK (479, 818), (126, 38) */
TASK fixed := 'hello';
/* CIF TASK (403, 820), (149, 35) */
/* CIF TASK (467, 871), (149, 35) */
TASK variable := 'HELLO';
/* CIF PROCEDURECALL (390, 870), (176, 35) */
/* CIF PROCEDURECALL (454, 921), (176, 35) */
CALL writeln(variable // '!!!');
/* CIF PROCEDURECALL (342, 920), (272, 35) */
/* CIF PROCEDURECALL (406, 971), (272, 35) */
CALL writeln(variable // variable // variable);
/* CIF TASK (432, 970), (91, 35) */
/* CIF TASK (496, 1021), (91, 35) */
TASK seq := {1};
/* CIF TASK (401, 1020), (153, 35) */
/* CIF TASK (465, 1071), (153, 35) */
TASK seq := {1} // {2} // {3};
/* CIF TASK (392, 1070), (172, 35) */
/* CIF TASK (456, 1121), (172, 35) */
TASK seq := seq // {2} // {1};
/* CIF DECISION (443, 1120), (70, 50) */
/* CIF DECISION (507, 1171), (70, 50) */
DECISION any;
/* CIF ANSWER (398, 1190), (70, 23) */
/* CIF ANSWER (462, 1241), (70, 23) */
('a'):
/* CIF ANSWER (488, 1190), (70, 23) */
/* CIF ANSWER (552, 1241), (70, 23) */
('b'):
ENDDECISION;
/* CIF DECISION (433, 1228), (89, 50) */
/* CIF DECISION (497, 1279), (89, 50) */
DECISION myenum
/* CIF COMMENT (542, 1235), (183, 35) */
/* CIF COMMENT (606, 1286), (183, 35) */
COMMENT 'Check case insensitivity';
/* CIF ANSWER (250, 1298), (70, 23) */
/* CIF ANSWER (314, 1349), (70, 23) */
(a):
/* CIF ANSWER (330, 1298), (70, 23) */
/* CIF ANSWER (394, 1349), (70, 23) */
(B):
/* CIF ANSWER (410, 1298), (70, 23) */
/* CIF ANSWER (474, 1349), (70, 23) */
(c):
/* CIF ANSWER (488, 1298), (70, 23) */
/* CIF ANSWER (552, 1349), (70, 23) */
(d):
/* CIF ANSWER (576, 1298), (70, 23) */
/* CIF ANSWER (640, 1349), (70, 23) */
(Ee):
ENDDECISION;
/* CIF DECISION (409, 1336), (138, 50) */
/* CIF DECISION (473, 1387), (138, 50) */
DECISION present(choice)
/* CIF COMMENT (566, 1343), (183, 35) */
/* CIF COMMENT (630, 1394), (183, 35) */
COMMENT 'Check case insensitivity';
/* CIF ANSWER (361, 1406), (70, 23) */
/* CIF ANSWER (425, 1457), (70, 23) */
(cde2):
/* CIF ANSWER (532, 1406), (70, 23) */
/* CIF ANSWER (596, 1457), (70, 23) */
ELSE:
ENDDECISION;
/* CIF NEXTSTATE (419, 1444), (116, 33) */
/* CIF NEXTSTATE (483, 1495), (116, 33) */
NEXTSTATE Wait_for_GUI;
/* CIF STATE (424, 163), (116, 33) */
/* CIF STATE (438, 163), (116, 33) */
STATE Wait_for_GUI;
ENDSTATE;
ENDPROCESS orchestrator;
\ 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