Commit 352e2a4e authored by dbarbera's avatar dbarbera
Browse files

Refactor conditional expression node

parent a988169c
......@@ -1183,17 +1183,17 @@ def _mantissa_base_exp(primary):
return [], u'', []
@expression.register(ogAST.PrimIfThenElse)
def _if_then_else(ifThenElse):
''' Return string and statements for ternary operator '''
resType = ifThenElse.exprType
@expression.register(ogAST.PrimConditional)
def _conditional(cond):
''' Return string and statements for conditional expressions '''
resType = cond.exprType
stmts = []
if resType.kind.startswith('Integer'):
tmp_type = 'Asn1Int'
elif resType.kind == 'StandardStringType':
print ifThenElse.value['then'].value
then_str = ifThenElse.value['then'].value.replace("'", '"')
else_str = ifThenElse.value['else'].value.replace("'", '"')
elif resType.kind == 'StringType':
print cond.value['then'].value
then_str = cond.value['then'].value.replace("'", '"')
else_str = cond.value['else'].value.replace("'", '"')
lens = [len(then_str), len(else_str)]
tmp_type = 'String(1 .. {})'.format(max(lens) - 2)
# Ada require fixed-length strings, adjust with spaces
......@@ -1204,28 +1204,28 @@ def _if_then_else(ifThenElse):
else:
tmp_type = 'asn1Scc' + resType.ReferencedTypeName.replace('-', '_')
local_decl = ['tmp{idx} : {tmpType};'.format(
idx=ifThenElse.value['tmpVar'],
idx=cond.value['tmpVar'],
tmpType=tmp_type)]
if_stmts, if_str, if_local = expression(ifThenElse.value['if'])
if_stmts, if_str, if_local = expression(cond.value['if'])
stmts.extend(if_stmts)
local_decl.extend(if_local)
if resType.kind != 'StandardStringType':
then_stmts, then_str, then_local = expression(ifThenElse.value['then'])
else_stmts, else_str, else_local = expression(ifThenElse.value['else'])
if resType.kind != 'StringType':
then_stmts, then_str, then_local = expression(cond.value['then'])
else_stmts, else_str, else_local = expression(cond.value['else'])
stmts.extend(then_stmts)
stmts.extend(else_stmts)
local_decl.extend(then_local)
local_decl.extend(else_local)
stmts.append(u'if {if_str} then'.format(if_str=if_str))
stmts.append(u'tmp{idx} := {then_str};'.format(
idx=ifThenElse.value['tmpVar'],
idx=cond.value['tmpVar'],
then_str=then_str))
stmts.append('else')
stmts.append(u'tmp{idx} := {else_str};'.format(
idx=ifThenElse.value['tmpVar'],
idx=cond.value['tmpVar'],
else_str=else_str))
stmts.append('end if;')
ada_string = u'tmp{idx}'.format(idx=ifThenElse.value['tmpVar'])
ada_string = u'tmp{idx}'.format(idx=cond.value['tmpVar'])
return stmts, unicode(ada_string), local_decl
......
......@@ -387,9 +387,9 @@ def _rename_path(ast, from_name, to_name):
ast.value[0] = to_name
@rename_everything.register(ogAST.PrimIfThenElse)
@rename_everything.register(ogAST.PrimConditional)
def _rename_ifhthenelse(ast, from_name, to_name):
''' Rename expressions in If-Then-Else-Fi construct '''
''' Rename expressions in Conditional expression construct '''
for expr in ('if', 'then', 'else'):
rename_everything(ast.value[expr], from_name, to_name)
......
......@@ -248,8 +248,8 @@ def _mantissa_base_exp(primary):
pass
@expression.register(ogAST.PrimIfThenElse)
def _if_then_else(ifThenElse):
@expression.register(ogAST.PrimConditional)
def _conditional(cond):
''' Return string and statements for ternary operator '''
pass
......
......@@ -232,13 +232,14 @@ class PrimOctetStringLiteral(Primary):
pass
class PrimIfThenElse(Primary):
class PrimConditional(Primary):
''' value is a dictionnary:
{ 'if': Expression, 'then': Expression,
'else': Expression, 'tmpVar': integer}
tmpVar can be used if the backend needs a temporary variable
to process the ifThenElse
'''
is_raw = False
pass
......
......@@ -66,7 +66,6 @@ EXPR_NODE = {
lexer.NOT: ogAST.ExprNot,
lexer.NEG: ogAST.ExprNeg,
lexer.PRIMARY: ogAST.Primary,
lexer.IFTHENELSE: ogAST.PrimIfThenElse,
}
# Insert current path in the search list for importing modules
......@@ -545,7 +544,7 @@ def check_type_compatibility(primary, typeRef, context):
'" not in this enumeration: ' +
str(actual_type.EnumValues.keys()))
raise TypeError(err)
elif isinstance(primary, ogAST.PrimIfThenElse) \
elif isinstance(primary, ogAST.PrimConditional) \
or isinstance(primary, ogAST.PrimVariable):
try:
compare_types(primary.exprType, typeRef)
......@@ -1004,7 +1003,7 @@ def expression(root, context):
return neg_expression(root, context)
elif root.type == lexer.PAREN:
return expression(root.children[0], context)
elif root.type == lexer.IFTHENELSE:
elif root.type == lexer.CONDITIONAL:
return conditional_expression(root, context)
elif root.type == lexer.PRIMARY:
return primary(root.children[0], context)
......@@ -1228,7 +1227,7 @@ def conditional_expression(root, context):
''' Conditional expression analysis '''
errors, warnings = [], []
expr = ogAST.PrimIfThenElse(
expr = ogAST.PrimConditional(
get_input_string(root),
root.getLine(),
root.getCharPositionInLine()
......
......@@ -33,6 +33,7 @@ tokens {
CLOSED_RANGE;
COMMENT;
COMPOSITE_STATE;
CONDITIONAL;
CONNECT;
CONNECTION;
CONSTANT;
......@@ -836,7 +837,7 @@ postfix_expression
primary_expression
: primary -> ^(PRIMARY primary)
| '(' expression ')' -> ^(PAREN expression)
| conditional_ground_expression
| conditional_expression
;
......@@ -966,31 +967,19 @@ operator_application
active_expression_list
: active_expression (',' expression_list)?;/* |
ground_expression ',' active_expression_list;*/ // Will not work (recursion)
/*
conditional_expression
: IF boolean_active_expression THEN consequence_expression ELSE alternative_expression FI |
IF boolean_expression THEN active_consequence_expression ELSE alternative_expression FI |
IF boolean_expression THEN consequence_expression ELSE active_alternative_expression FI;
*/
// Simpler version, the rest will is checked by semantic analysis
conditional_expression
: IF expression THEN expression ELSE expression FI;
//synonym : ID; // synonym_id | external_synonym;
external_synonym
: external_synonym_id;
conditional_ground_expression
conditional_expression
: IF ifexpr=expression
THEN thenexpr=expression
ELSE elseexpr=expression FI
-> ^(IFTHENELSE $ifexpr $thenexpr $elseexpr);
-> ^(CONDITIONAL $ifexpr $thenexpr $elseexpr);
expression_list
......
This diff is collapsed.
This diff is collapsed.
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