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

Fixed unary operators

parent 1bb90909
...@@ -722,11 +722,15 @@ def expression(expr): ...@@ -722,11 +722,15 @@ def expression(expr):
def _primary_variable(prim): def _primary_variable(prim):
''' Single variable reference ''' ''' Single variable reference '''
sep = u'l_' if find_var(prim.value[0]) else u'' sep = u'l_' if find_var(prim.value[0]) else u''
ada_string = u'{opnot}{sep}{name}'.format( # ada_string = u'{opnot}{sep}{name}'.format(
opnot='not ' if prim.op_not else '', # opnot='not ' if prim.op_not else '',
sep=sep, name=prim.value[0]) # sep=sep, name=prim.value[0])
if prim.op_minus: # if prim.op_minus:
ada_string = '(-{})'.format(ada_string) # ada_string = '(-{})'.format(ada_string)
string = u'{sep}{name}'.format(sep=sep, name=prim.value[0])
ada_string = unary(prim, string)
if prim.exprType.__name__ == 'for_range': if prim.exprType.__name__ == 'for_range':
# Ada iterator in FOR loops is an Integer - we must cast to 64 bits # Ada iterator in FOR loops is an Integer - we must cast to 64 bits
ada_string = u'Asn1Int({})'.format(ada_string) ada_string = u'Asn1Int({})'.format(ada_string)
...@@ -897,10 +901,11 @@ def _prim_path(primary_id): ...@@ -897,10 +901,11 @@ def _prim_path(primary_id):
ada_string += ', '.join(list_of_params) ada_string += ', '.join(list_of_params)
ada_string += ')' ada_string += ')'
sep = '.' sep = '.'
if primary_id.op_not: ada_string = unary(primary_id, ada_string)
ada_string = 'not {}'.format(ada_string) # if primary_id.op_not:
elif primary_id.op_minus: # ada_string = 'not {}'.format(ada_string)
ada_string = '(-{})'.format(ada_string) # elif primary_id.op_minus:
# ada_string = '(-{})'.format(ada_string)
return stmts, ada_string, local_decl return stmts, ada_string, local_decl
...@@ -928,6 +933,7 @@ def _basic_operators(expr): ...@@ -928,6 +933,7 @@ def _basic_operators(expr):
code.extend(right_stmts) code.extend(right_stmts)
local_decl.extend(left_local) local_decl.extend(left_local)
local_decl.extend(right_local) local_decl.extend(right_local)
ada_string = unary(expr, ada_string)
return code, ada_string, local_decl return code, ada_string, local_decl
...@@ -967,6 +973,7 @@ def _bitwise_operators(expr): ...@@ -967,6 +973,7 @@ def _bitwise_operators(expr):
code.extend(right_stmts) code.extend(right_stmts)
local_decl.extend(left_local) local_decl.extend(left_local)
local_decl.extend(right_local) local_decl.extend(right_local)
ada_string = unary(expr, ada_string)
return code, ada_string, local_decl return code, ada_string, local_decl
...@@ -1039,6 +1046,7 @@ def _append(expr): ...@@ -1039,6 +1046,7 @@ def _append(expr):
return stmts, ada_string, local_decl return stmts, ada_string, local_decl
@expression.register(ogAST.ExprIn) @expression.register(ogAST.ExprIn)
def _expr_in(expr): def _expr_in(expr):
''' IN expressions: check if item is in a SEQUENCE OF ''' ''' IN expressions: check if item is in a SEQUENCE OF '''
...@@ -1092,12 +1100,13 @@ def _choice_determinant(primary): ...@@ -1092,12 +1100,13 @@ def _choice_determinant(primary):
def _integer(primary): def _integer(primary):
''' Generate code for a raw numerical value ''' ''' Generate code for a raw numerical value '''
ada_string = primary.value[0] ada_string = primary.value[0]
if primary.op_minus and float(ada_string) >= 0: ada_string = unary(primary, ada_string)
ada_string = '(-{})'.format(ada_string) # if primary.op_minus and float(ada_string) >= 0:
elif float(ada_string) < 0: # ada_string = '(-{})'.format(ada_string)
ada_string = '({})'.format(ada_string) # elif float(ada_string) < 0:
if primary.op_not: # ada_string = '({})'.format(ada_string)
ada_string = 'not {}'.format(ada_string) # if primary.op_not:
# ada_string = 'not {}'.format(ada_string)
return [], ada_string, [] return [], ada_string, []
...@@ -1105,8 +1114,9 @@ def _integer(primary): ...@@ -1105,8 +1114,9 @@ def _integer(primary):
def _integer(primary): def _integer(primary):
''' Generate code for a raw boolean value ''' ''' Generate code for a raw boolean value '''
ada_string = primary.value[0] ada_string = primary.value[0]
if primary.op_not: ada_string = unary(primary, ada_string)
ada_string = 'not {}'.format(ada_string) # if primary.op_not:
# ada_string = 'not {}'.format(ada_string)
return [], ada_string, [] return [], ada_string, []
...@@ -1523,6 +1533,23 @@ def _inner_procedure(proc): ...@@ -1523,6 +1533,23 @@ def _inner_procedure(proc):
# A few helper functions needed by the Ada backend # A few helper functions needed by the Ada backend
def unary(expr, string):
''' Check for NOT or MINUS unary operators and add them to the string '''
op_minus = getattr(expr, 'op_minus', False)
op_not = getattr(expr, 'op_not', False)
try:
float_val = float(string)
except ValueError:
float_val = 1
if op_minus and float_val > 0:
string = u'(-{})'.format(string)
elif float_val <= 0:
string = u'({})'.format(string)
if op_not:
string = u'not {}'.format(string)
return string
def find_basic_type(a_type): def find_basic_type(a_type):
''' Return the ASN.1 basic type of a_type ''' ''' Return the ASN.1 basic type of a_type '''
basic_type = a_type basic_type = a_type
......
...@@ -343,7 +343,6 @@ def fix_special_operators(op_name, expr_list, context): ...@@ -343,7 +343,6 @@ def fix_special_operators(op_name, expr_list, context):
check_type_compatibility(param, each, context) check_type_compatibility(param, each, context)
if each is OCTETSTRING and isinstance(param, if each is OCTETSTRING and isinstance(param,
ogAST.PrimIfThenElse): ogAST.PrimIfThenElse):
print param.inputString, 'is OCTET STRING'
param.exprType = param.value['then'].exprType param.exprType = param.value['then'].exprType
else: else:
param.exprType = each param.exprType = each
......
...@@ -49,107 +49,117 @@ endfor; ...@@ -49,107 +49,117 @@ endfor;
ENDPROCEDURE; ENDPROCEDURE;
/* CIF START (591, 0), (100, 50) */ /* CIF START (591, 0), (100, 50) */
START; START;
/* CIF PROCEDURECALL (586, 65), (110, 35) */ /* CIF PROCEDURECALL (553, 65), (175, 35) */
CALL writeln(-(someint + 1))
/* CIF COMMENT (748, 65), (226, 38) */
COMMENT 'Test unary on expression result
should display -3';
/* CIF PROCEDURECALL (542, 115), (197, 35) */
CALL writeln(not(true or false))
/* CIF COMMENT (759, 115), (187, 38) */
COMMENT 'test unary on expression
should display FALSE';
/* CIF PROCEDURECALL (586, 165), (110, 35) */
CALL writeln(str) CALL writeln(str)
/* CIF COMMENT (716, 65), (231, 35) */ /* CIF COMMENT (716, 165), (231, 35) */
COMMENT 'Test writeln with an octet string'; COMMENT 'Test writeln with an octet string';
/* CIF PROCEDURECALL (586, 115), (110, 35) */ /* CIF PROCEDURECALL (586, 215), (110, 35) */
CALL writeln(str) CALL writeln(str)
/* CIF COMMENT (716, 115), (254, 35) */ /* CIF COMMENT (716, 215), (254, 35) */
COMMENT 'Write again to check local variables'; COMMENT 'Write again to check local variables';
/* CIF PROCEDURECALL (558, 165), (165, 35) */ /* CIF PROCEDURECALL (558, 265), (165, 35) */
CALL writeln(variable_str) CALL writeln(variable_str)
/* CIF COMMENT (743, 165), (275, 35) */ /* CIF COMMENT (743, 265), (275, 35) */
COMMENT 'Write a non-fixed length OCTET STRING'; COMMENT 'Write a non-fixed length OCTET STRING';
/* CIF PROCEDURECALL (541, 215), (199, 38) */ /* CIF PROCEDURECALL (541, 315), (199, 38) */
CALL writeln(variable_str // '!!!') CALL writeln(variable_str // '!!!')
/* CIF COMMENT (760, 216), (117, 35) */ /* CIF COMMENT (760, 316), (117, 35) */
COMMENT 'with APPEND'; COMMENT 'with APPEND';
/* CIF PROCEDURECALL (465, 268), (352, 35) */ /* CIF PROCEDURECALL (465, 368), (352, 35) */
CALL writeln(if someint>0 then variable_str else other fi) CALL writeln(if someint>0 then variable_str else other fi)
/* CIF COMMENT (837, 268), (275, 35) */ /* CIF COMMENT (837, 368), (275, 35) */
COMMENT 'Write a non-fixed lenght OCTET STRING'; COMMENT 'Write a non-fixed lenght OCTET STRING';
/* CIF PROCEDURECALL (552, 318), (177, 35) */ /* CIF PROCEDURECALL (552, 418), (177, 35) */
CALL writeln(1 + (-otherint)) CALL writeln(1 + (-otherint))
/* CIF COMMENT (749, 318), (266, 35) */ /* CIF COMMENT (749, 418), (266, 35) */
COMMENT 'Test the op_minus in a sub-expression'; COMMENT 'Test the op_minus in a sub-expression';
/* CIF PROCEDURECALL (430, 368), (421, 35) */ /* CIF PROCEDURECALL (430, 468), (421, 35) */
CALL writeln(-someint, if someint>0 then ' is ' else 'Foo' fi, not true) CALL writeln(-someint, if someint>0 then ' is ' else 'Foo' fi, not true)
/* CIF COMMENT (871, 368), (291, 35) */ /* CIF COMMENT (871, 468), (291, 35) */
COMMENT 'Various tests with strings, ternary, op_not'; COMMENT 'Various tests with strings, ternary, op_not';
/* CIF PROCEDURECALL (514, 418), (254, 35) */ /* CIF PROCEDURECALL (514, 518), (254, 35) */
CALL write(if someint>0 then 2 else 1 fi) CALL write(if someint>0 then 2 else 1 fi)
/* CIF COMMENT (788, 418), (220, 35) */ /* CIF COMMENT (788, 518), (220, 35) */
COMMENT 'test ternary with raw numbers'; COMMENT 'test ternary with raw numbers';
/* CIF PROCEDURECALL (496, 468), (289, 35) */ /* CIF PROCEDURECALL (496, 568), (289, 35) */
CALL write(if someint>0 then someint else 1 fi) CALL write(if someint>0 then someint else 1 fi)
/* CIF COMMENT (805, 468), (308, 35) */ /* CIF COMMENT (805, 568), (308, 35) */
COMMENT 'test ternary with mixed variable/raw number'; COMMENT 'test ternary with mixed variable/raw number';
/* CIF PROCEDURECALL (577, 518), (128, 35) */ /* CIF PROCEDURECALL (577, 618), (128, 35) */
CALL writeln(bar(1)) CALL writeln(bar(1))
/* CIF COMMENT (725, 518), (100, 35) */ /* CIF COMMENT (725, 618), (100, 35) */
COMMENT 'test index'; COMMENT 'test index';
/* CIF PROCEDURECALL (532, 568), (217, 35) */ /* CIF PROCEDURECALL (532, 668), (217, 35) */
CALL writeln(opnot, ' ', not opnot) CALL writeln(opnot, ' ', not opnot)
/* CIF COMMENT (769, 568), (191, 35) */ /* CIF COMMENT (769, 668), (191, 35) */
COMMENT 'test op_not with variable'; COMMENT 'test op_not with variable';
/* CIF TASK (480, 618), (321, 35) */ /* CIF TASK (480, 718), (321, 35) */
TASK someInt := if someint = 0 then someint else 0 fi; TASK someInt := if someint = 0 then someint else 0 fi;
/* CIF DECISION (504, 668), (273, 87) */ /* CIF DECISION (504, 768), (273, 87) */
DECISION someint /=0 and then (10 / someInt > 0) DECISION someint /=0 and then (10 / someInt > 0)
or else someint = 0 or else someint = 0
/* CIF COMMENT (793, 677), (179, 68) */ /* CIF COMMENT (793, 777), (179, 68) */
COMMENT 'Using "and else" is the COMMENT 'Using "and else" is the
short-circuit form. The short-circuit form. The
second part should not second part should not
be evaluated.'; be evaluated.';
/* CIF ANSWER (561, 775), (70, 23) */ /* CIF ANSWER (561, 875), (70, 23) */
(true): (true):
/* CIF TASK (541, 813), (110, 35) */ /* CIF TASK (541, 913), (110, 35) */
TASK someInt := 2; TASK someInt := 2;
/* CIF PROCEDURECALL (537, 863), (117, 38) */ /* CIF PROCEDURECALL (537, 963), (117, 38) */
CALL writeln('OK'); CALL writeln('OK');
/* CIF ANSWER (664, 775), (70, 23) */ /* CIF ANSWER (664, 875), (70, 23) */
(false): (false):
ENDDECISION; ENDDECISION;
/* CIF NEXTSTATE (608, 916), (65, 33) */ /* CIF NEXTSTATE (608, 1016), (65, 33) */
NEXTSTATE Wait; NEXTSTATE Wait;
/* CIF STATE (608, 916), (65, 33) */ /* CIF STATE (608, 1016), (65, 33) */
STATE Wait; STATE Wait;
/* CIF INPUT (865, 969), (89, 33) */ /* CIF INPUT (865, 1069), (89, 33) */
INPUT mytimer; INPUT mytimer;
/* CIF PROCEDURECALL (818, 1017), (182, 33) */ /* CIF PROCEDURECALL (818, 1117), (182, 33) */
CALL writeln('timer expired'); CALL writeln('timer expired');
/* CIF PROCEDURECALL (829, 1065), (160, 33) */ /* CIF PROCEDURECALL (829, 1165), (160, 33) */
CALL factorial(3, someint); CALL factorial(3, someint);
/* CIF NEXTSTATE (877, 1113), (65, 33) */ /* CIF NEXTSTATE (877, 1213), (65, 33) */
NEXTSTATE Wait; NEXTSTATE Wait;
/* CIF INPUT (421, 969), (181, 33) */ /* CIF INPUT (421, 1069), (181, 33) */
INPUT start_something (toto); INPUT start_something (toto);
/* CIF OUTPUT (376, 1017), (270, 33) */ /* CIF OUTPUT (376, 1117), (270, 33) */
OUTPUT result_data((toto+1) mod 2147483647); OUTPUT result_data((toto+1) mod 2147483647);
/* CIF PROCEDURECALL (436, 1065), (150, 48) */ /* CIF PROCEDURECALL (436, 1165), (150, 48) */
CALL writeln CALL writeln
('Hello Toto', toto); ('Hello Toto', toto);
/* CIF PROCEDURECALL (413, 1128), (196, 33) */ /* CIF PROCEDURECALL (413, 1228), (196, 33) */
CALL set_timer(1000, myTimer); CALL set_timer(1000, myTimer);
/* CIF TASK (346, 1176), (330, 35) */ /* CIF TASK (346, 1276), (330, 35) */
TASK largeReal := power(someReal, 2); TASK largeReal := power(someReal, 2);
/* CIF PROCEDURECALL (282, 1226), (458, 35) */ /* CIF PROCEDURECALL (282, 1326), (458, 35) */
CALL writeln(someReal, ' ** 2' , ' == ', largeReal, ' (should be 2.25 )'); CALL writeln(someReal, ' ** 2' , ' == ', largeReal, ' (should be 2.25 )');
/* CIF TASK (411, 1276), (201, 35) */ /* CIF TASK (411, 1376), (201, 35) */
TASK someReal := float(someInt); TASK someReal := float(someInt);
/* CIF TASK (391, 1326), (241, 35) */ /* CIF TASK (391, 1426), (241, 35) */
TASK someInt := fix(someReal) mod 255; TASK someInt := fix(someReal) mod 255;
/* CIF TASK (435, 1376), (152, 35) */ /* CIF TASK (435, 1476), (152, 35) */
TASK opnot := not opnot; TASK opnot := not opnot;
/* CIF TASK (430, 1426), (163, 35) */ /* CIF TASK (430, 1526), (163, 35) */
TASK 'someint := -someint' TASK 'someint := -someint'
/* CIF COMMENT (613, 1426), (196, 35) */ /* CIF COMMENT (613, 1526), (196, 35) */
COMMENT 'XXX should raise an error!'; COMMENT 'XXX should raise an error!';
/* CIF TASK (429, 1476), (164, 35) */ /* CIF TASK (429, 1576), (164, 35) */
TASK someint := (-8) mod 5; TASK someint := (-8) mod 5;
/* CIF NEXTSTATE (480, 1526), (63, 33) */ /* CIF NEXTSTATE (480, 1626), (63, 33) */
NEXTSTATE wait; NEXTSTATE wait;
ENDSTATE; ENDSTATE;
ENDPROCESS myfunction; 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