diff --git a/README.md b/README.md index ae21f05428ded072a53cecedb6234730eadf385c..78812fd13c5588a3adb94b82837b30d7e50c1388 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/opengeode/AdaGenerator.py b/opengeode/AdaGenerator.py index 83bf0071ab2ed2edd010ed6dab047fbd3ba246ab..479cfc6e376f509c7d146d3a80c51b6a0887b181 100644 --- a/opengeode/AdaGenerator.py +++ b/opengeode/AdaGenerator.py @@ -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] diff --git a/opengeode/ogParser.py b/opengeode/ogParser.py index 71c0ad1e5f4b9e0ac1148f68477f9a25a39796bf..94f4399c9cf539a44eedaf3d9c480ceb03531c17 100644 --- a/opengeode/ogParser.py +++ b/opengeode/ogParser.py @@ -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: diff --git a/opengeode/opengeode.py b/opengeode/opengeode.py index af9b8506c644f9d049d6e86a3af3da5d58aeb3bd..7fac7c4131350c0954c6d0ba1b130d848e1fca7a 100755 --- a/opengeode/opengeode.py +++ b/opengeode/opengeode.py @@ -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) diff --git a/tests/regression/test-choice-determinant/dataview.asn b/tests/regression/test-choice-determinant/dataview.asn index 585be9eda21f5715ffdea7d36369d6bdbcac8dc5..22f519d8ac256b080cdf740763e1c63830d79c73 100644 --- a/tests/regression/test-choice-determinant/dataview.asn +++ b/tests/regression/test-choice-determinant/dataview.asn @@ -6,4 +6,7 @@ BEGIN } MyEnum ::= ENUMERATED { foo, bar } + + MyInt ::= INTEGER (0 .. 1) + END diff --git a/tests/regression/test-choice-determinant/toto.pr b/tests/regression/test-choice-determinant/toto.pr index 797d268652621100e79198e74b81a2a598dcd436..add22f940e46f4dc7fb1b5ce287a96ea8db6be31 100644 --- a/tests/regression/test-choice-determinant/toto.pr +++ b/tests/regression/test-choice-determinant/toto.pr @@ -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; diff --git a/tests/regression/test-demo2callers/check b/tests/regression/test-demo2callers/check index a0d602a97543947e1ea4b2580fc6a2894c888512..b6b67759d175ea15a47a52dcc03b35198ae1e109 100755 Binary files a/tests/regression/test-demo2callers/check and b/tests/regression/test-demo2callers/check differ diff --git a/tests/regression/test-eds1/testsc_simu/testsc_simu.sh b/tests/regression/test-eds1/testsc_simu/testsc_simu.sh index e6a56e052a3ea3d54d0ca38198b4a0efb47e8a2c..26cf554429e9adf48137210963373b4fe196faad 100755 --- a/tests/regression/test-eds1/testsc_simu/testsc_simu.sh +++ b/tests/regression/test-eds1/testsc_simu/testsc_simu.sh @@ -1,12 +1,12 @@ #!/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 diff --git a/tests/regression/test-uart/check b/tests/regression/test-uart/check index 13ec62d8906b92d4420408cfd7ba5103d75ac823..03ca347f0c264e18f7bd52fbcee0e5854c4644ad 100755 Binary files a/tests/regression/test-uart/check and b/tests/regression/test-uart/check differ