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

Refactor Ada backend (minor)

parent a4379d9f
...@@ -1276,8 +1276,7 @@ def _boolean(primary): ...@@ -1276,8 +1276,7 @@ def _boolean(primary):
@expression.register(ogAST.PrimEmptyString) @expression.register(ogAST.PrimEmptyString)
def _empty_string(primary): def _empty_string(primary):
''' Generate code for an empty SEQUENCE OF: {} ''' ''' Generate code for an empty SEQUENCE OF: {} '''
ada_string = u'asn1Scc{typeRef}_Init'.format( ada_string = u'{}_Init'.format(type_name(primary.exprType))
typeRef=primary.exprType.ReferencedTypeName.replace('-', '_'))
return [], unicode(ada_string), [] return [], unicode(ada_string), []
...@@ -1310,16 +1309,11 @@ def _mantissa_base_exp(primary): ...@@ -1310,16 +1309,11 @@ def _mantissa_base_exp(primary):
@expression.register(ogAST.PrimConditional) @expression.register(ogAST.PrimConditional)
def _conditional(cond): def _conditional(cond):
''' Return string and statements for conditional expressions ''' ''' Return string and statements for conditional expressions '''
resType = cond.exprType
stmts = [] stmts = []
if resType.kind in ('IntegerType', 'Integer32Type'): tmp_type = type_name(cond.exprType)
tmp_type = 'Asn1Int'
elif resType.kind == 'RealType': if tmp_type == 'String':
tmp_type = 'Asn1Real'
elif resType.kind == 'BooleanType':
tmp_type = 'Boolean'
elif resType.kind == 'StringType':
then_str = cond.value['then'].value.replace("'", '"') then_str = cond.value['then'].value.replace("'", '"')
else_str = cond.value['else'].value.replace("'", '"') else_str = cond.value['else'].value.replace("'", '"')
lens = [len(then_str), len(else_str)] lens = [len(then_str), len(else_str)]
...@@ -1329,16 +1323,13 @@ def _conditional(cond): ...@@ -1329,16 +1323,13 @@ def _conditional(cond):
then_str = then_str[0:-1] + ' ' * (lens[1] - lens[0]) + '"' then_str = then_str[0:-1] + ' ' * (lens[1] - lens[0]) + '"'
elif lens[1] < lens[0]: elif lens[1] < lens[0]:
else_str = else_str[0:-1] + ' ' * (lens[0] - lens[1]) + '"' else_str = else_str[0:-1] + ' ' * (lens[0] - lens[1]) + '"'
else:
tmp_type = 'asn1Scc' + resType.ReferencedTypeName.replace('-', '_')
local_decl = ['tmp{idx} : {tmpType};'.format( local_decl = ['tmp{idx} : {tmpType};'.format(idx=cond.value['tmpVar'],
idx=cond.value['tmpVar'], tmpType=tmp_type)]
tmpType=tmp_type)]
if_stmts, if_str, if_local = expression(cond.value['if']) if_stmts, if_str, if_local = expression(cond.value['if'])
stmts.extend(if_stmts) stmts.extend(if_stmts)
local_decl.extend(if_local) local_decl.extend(if_local)
if resType.kind != 'StringType': if not tmp_type.startswith('String'):
then_stmts, then_str, then_local = expression(cond.value['then']) then_stmts, then_str, then_local = expression(cond.value['then'])
else_stmts, else_str, else_local = expression(cond.value['else']) else_stmts, else_str, else_local = expression(cond.value['else'])
stmts.extend(then_stmts) stmts.extend(then_stmts)
...@@ -1435,17 +1426,14 @@ def _decision(dec): ...@@ -1435,17 +1426,14 @@ def _decision(dec):
code.append('-- "DECISION ANY" statement was ignored') code.append('-- "DECISION ANY" statement was ignored')
return code, local_decl return code, local_decl
question_type = dec.question.exprType question_type = dec.question.exprType
# XXX check if we should get the type like this actual_type = type_name(question_type)
actual_type = getattr(
question_type, 'ReferencedTypeName', None) or question_type.kind
actual_type = actual_type.replace('-', '_')
basic = find_basic_type(question_type).kind in ('IntegerType', basic = find_basic_type(question_type).kind in ('IntegerType',
'Integer32Type', 'BooleanType', 'Integer32Type', 'BooleanType',
'RealType', 'EnumeratedType', 'ChoiceEnumeratedType') 'RealType', 'EnumeratedType', 'ChoiceEnumeratedType')
# for ASN.1 types, declare a local variable # for ASN.1 types, declare a local variable
# to hold the evaluation of the question # to hold the evaluation of the question
if not basic: if not basic:
local_decl.append('tmp{idx} : aliased asn1Scc{actType};'.format( local_decl.append('tmp{idx} : aliased {actType};'.format(
idx=dec.tmpVar, actType=actual_type)) idx=dec.tmpVar, actType=actual_type))
q_stmts, q_str, q_decl = expression(dec.question) q_stmts, q_str, q_decl = expression(dec.question)
# Add code-to-model traceability # Add code-to-model traceability
...@@ -1470,7 +1458,7 @@ def _decision(dec): ...@@ -1470,7 +1458,7 @@ def _decision(dec):
ogAST.PrimStringLiteral)): ogAST.PrimStringLiteral)):
ans_str = array_content(a.constant, ans_str, ans_str = array_content(a.constant, ans_str,
find_basic_type(question_type)) find_basic_type(question_type))
exp = u'asn1Scc{actType}_Equal(tmp{idx}, {ans})'.format( exp = u'{actType}_Equal(tmp{idx}, {ans})'.format(
actType=actual_type, idx=dec.tmpVar, ans=ans_str) actType=actual_type, idx=dec.tmpVar, ans=ans_str)
if a.openRangeOp == ogAST.ExprNeq: if a.openRangeOp == ogAST.ExprNeq:
exp = u'not {}'.format(exp) exp = u'not {}'.format(exp)
...@@ -1789,7 +1777,9 @@ def type_name(a_type): ...@@ -1789,7 +1777,9 @@ def type_name(a_type):
elif a_type.kind.startswith('Integer'): elif a_type.kind.startswith('Integer'):
return 'Asn1Int' return 'Asn1Int'
elif a_type.kind == 'RealType': elif a_type.kind == 'RealType':
return 'Long_Float' return 'Asn1Real' # 'Long_Float'
elif a_type.kind == 'StringType':
return 'String'
else: else:
raise NotImplmentedError('Type name for {}'.format(a_type.kind)) raise NotImplmentedError('Type name for {}'.format(a_type.kind))
......
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