Commit bf495611 authored by Maxime Perrotin's avatar Maxime Perrotin

Add support for the "val" operator

as per ISS request
parent e728ee23
......@@ -134,6 +134,11 @@ The background pattern was downloaded from www.subtlepatterns.com
Changelog
=========
2.0.20 (02/2019)
- Added "val" operator to convert a number to an enumerant
useage: someVal := val (0, MyEnumeratedType)
with someVal of type MyEnumeratedType. Will return the first enumerant
2.0.19 (01/2019)
- for CHOICE types variables can be declared with enumerated type
corresponding to the choice distriminant. CHOICE type is suffixed by
......
......@@ -1604,6 +1604,16 @@ def _prim_call(prim, **kwargs):
.format(sort=sort_name,
var_type=var_typename,
var_str=var_str)
elif ident == 'val':
variable, target_type = params
var_typename = type_name (variable.exprType)
var_stmts, var_str, var_decl = expression (variable, readonly=1)
stmts.extend(var_stmts)
local_decl.extend(var_decl)
sort_name = 'asn1Scc' + target_type.value[0].replace('-', '_')
ada_string += "{sort}'Val ({var_str})"\
.format(sort=sort_name,
var_str=var_str)
elif ident == 'num':
# User wants to get an enumerated corresponding integer value
exp = params[0]
......
......@@ -141,6 +141,10 @@ SPECIAL_OPERATORS = {
{'type': ENUMERATED, 'direction': 'in'},
{'type': ANY_TYPE, 'direction': 'in'}
],
'val' : [ # to convert a number to an enumerated type value
{'type': UNSIGNED, 'direction': 'in'}, # eg. 1
{'type': ANY_TYPE, 'direction': 'in'} # eg. MyChoice
],
}
# Container to keep a list of types mapped from ANTLR Tokens
......@@ -591,6 +595,27 @@ def check_call(name, params, context):
return type('Exist', (object,), {
'kind': 'BooleanType'
})
elif name == 'val':
# val converts a positive number to a enumeration literal
# the first parameter is the number and the second is the type name
# of the enumeration. It is equivalent in Ada to EnumType'Val(number)
if len(params) != 2:
raise TypeError(name + " takes 2 parameters: number, type")
variable, target_type = params
variable_sort = find_basic_type(variable.exprType)
if variable_sort.kind != 'IntegerType':
raise TypeError(name + ': First parameter is not an number')
sort_name = target_type.value[0] # raw string of the type to cast
for sort in types().keys():
if sort.lower().replace('-', '_') == \
sort_name.lower().replace('-', '_'):
break
else:
raise TypeError(name + ': type ' + sort_name + 'not found')
# we could check if the range of the number is compatible with the
# number of values...
return_type = types()[sort].type
return return_type
elif name in ('to_selector', 'to_enum'):
if len(params) != 2:
......
......@@ -141,7 +141,7 @@ except ImportError:
__all__ = ['opengeode', 'SDL_Scene', 'SDL_View', 'parse']
__version__ = '2.0.19'
__version__ = '2.0.20'
if hasattr(sys, 'frozen'):
# Detect if we are running on Windows (py2exe-generated)
......
......@@ -6,4 +6,7 @@ BEGIN
}
MyEnum ::= ENUMERATED { foo, bar }
MyInt ::= INTEGER (0 .. 1)
END
......@@ -21,59 +21,71 @@ system toto;
dcl val MyChoice, det MyEnum;
dcl choice_selectors MyChoice_selection := foo;
dcl pos MyInt;
/* CIF ENDTEXT */
/* CIF START (240, 89), (70, 35) */
START;
/* CIF task (160, 144), (229, 35) */
task choice_selectors := present(val);
/* CIF decision (222, 194), (106, 50) */
/* CIF decision (222, 199), (106, 50) */
decision present (val);
/* CIF ANSWER (195, 264), (70, 24) */
/* CIF ANSWER (195, 269), (70, 24) */
(foo):
/* CIF ANSWER (285, 264), (70, 24) */
/* CIF ANSWER (285, 269), (70, 24) */
(bar):
enddecision;
/* CIF decision (222, 303), (106, 50) */
/* CIF decision (222, 308), (106, 50) */
decision present (val);
/* CIF ANSWER (166, 373), (129, 24) */
/* CIF ANSWER (166, 378), (129, 24) */
(choice_selectors):
/* CIF ANSWER (315, 373), (70, 24) */
/* CIF ANSWER (315, 378), (70, 24) */
else:
enddecision;
/* CIF decision (210, 412), (129, 50) */
/* CIF decision (210, 417), (129, 50) */
decision choice_selectors;
/* CIF ANSWER (223, 482), (103, 24) */
/* CIF ANSWER (223, 487), (103, 24) */
(present(val)):
/* CIF ANSWER (346, 482), (70, 24) */
/* CIF ANSWER (346, 487), (70, 24) */
else:
enddecision;
/* CIF task (233, 521), (83, 35) */
/* CIF task (233, 526), (83, 35) */
task det := foo;
/* CIF task (189, 576), (171, 40) */
/* CIF task (215, 581), (119, 35) */
task pos := num (det);
/* CIF PROCEDURECALL (137, 636), (276, 35) */
call writeln ('num(det) = ', pos, ' (should be 0)');
/* CIF task (189, 686), (171, 40) */
task choice_selectors := bar;
/* CIF task (113, 636), (324, 40) */
/* CIF task (113, 746), (324, 40) */
task choice_selectors := to_selector (det, MyChoice);
/* CIF PROCEDURECALL (120, 696), (309, 35) */
/* CIF PROCEDURECALL (120, 806), (309, 35) */
call writeln (choice_selectors, ' (should be FOO)');
/* CIF task (233, 751), (83, 35) */
/* CIF task (233, 861), (83, 35) */
task det := bar;
/* CIF task (124, 801), (302, 40) */
/* CIF task (124, 911), (302, 40) */
task det := to_enum (choice_selectors, MyEnum);
/* CIF PROCEDURECALL (120, 861), (309, 35) */
/* CIF PROCEDURECALL (120, 971), (309, 35) */
call writeln (det, ' (should be FOO)');
/* CIF task (233, 916), (83, 35) */
/* CIF task (233, 1026), (83, 35) */
task det := bar;
/* CIF task (113, 971), (324, 40) */
/* CIF task (113, 1081), (324, 40) */
task choice_selectors := to_selector (det, MyChoice);
/* CIF PROCEDURECALL (120, 1031), (309, 35) */
/* CIF PROCEDURECALL (120, 1141), (309, 35) */
call writeln (choice_selectors, ' (should be BAR)');
/* CIF task (233, 1086), (83, 35) */
/* CIF task (233, 1196), (83, 35) */
task det := foo;
/* CIF task (124, 1141), (302, 40) */
/* CIF task (124, 1251), (302, 40) */
task det := to_enum (choice_selectors, MyEnum);
/* CIF PROCEDURECALL (120, 1201), (309, 35) */
/* CIF PROCEDURECALL (120, 1311), (309, 35) */
call writeln (det, ' (should be BAR)');
/* CIF NEXTSTATE (243, 1251), (64, 35) */
/* CIF task (193, 1366), (164, 35) */
task det := val (0, MyEnum)
/* CIF comment (377, 1366), (133, 35) */
comment 'convert 0 to foo';
/* CIF PROCEDURECALL (120, 1421), (309, 35) */
call writeln (det, ' (should be FOO)');
/* CIF NEXTSTATE (243, 1471), (64, 35) */
NEXTSTATE hop;
/* CIF state (528, 360), (70, 35) */
state hop;
......
#!/bin/bash -e
rm -rf testsc_simu
mkdir -p testsc_simu
cp /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/testsc.pr dataview.asn testsc_simu
cp /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/testsc.pr /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/dataview.asn testsc_simu
cd testsc_simu
opengeode testsc.pr --shared
cat dataview.asn >> dataview-uniq.asn
mono $(which asn1.exe) -Ada -typePrefix asn1Scc -equal dataview.asn
mono $(which asn1.exe) -c -typePrefix asn1Scc -equal dataview.asn
cat /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/dataview.asn >> dataview-uniq.asn
mono $(which asn1.exe) -Ada -typePrefix asn1Scc -equal /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/dataview.asn
mono $(which asn1.exe) -c -typePrefix asn1Scc -equal /home/taste/opengeode-github/tests/regression/test-eds1/testsc_simu/dataview.asn
gnatmake -fPIC -gnat2012 -c *.adb
gnatbind -n -Llibtestsc testsc
gnatmake -fPIC -c -gnat2012 b~testsc.adb
......
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