Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
OpenGEODE
Commits
352e2a4e
Commit
352e2a4e
authored
Jul 29, 2014
by
dbarbera
Browse files
Refactor conditional expression node
parent
a988169c
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
AdaGenerator.py
View file @
352e2a4e
...
...
@@ -1183,17 +1183,17 @@ def _mantissa_base_exp(primary):
return
[],
u
''
,
[]
@
expression
.
register
(
ogAST
.
Prim
IfThenElse
)
def
_
if_then_else
(
ifThenElse
):
''' Return string and statements for
ternary operator
'''
resType
=
ifThenElse
.
exprType
@
expression
.
register
(
ogAST
.
Prim
Conditional
)
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
==
'
Standard
StringType'
:
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
!=
'
Standard
StringType'
:
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
...
...
Helper.py
View file @
352e2a4e
...
...
@@ -387,9 +387,9 @@ def _rename_path(ast, from_name, to_name):
ast
.
value
[
0
]
=
to_name
@
rename_everything
.
register
(
ogAST
.
Prim
IfThenElse
)
@
rename_everything
.
register
(
ogAST
.
Prim
Conditional
)
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
)
...
...
LlvmGenerator.py
View file @
352e2a4e
...
...
@@ -248,8 +248,8 @@ def _mantissa_base_exp(primary):
pass
@
expression
.
register
(
ogAST
.
Prim
IfThenElse
)
def
_
if_then_else
(
ifThenElse
):
@
expression
.
register
(
ogAST
.
Prim
Conditional
)
def
_
conditional
(
cond
):
''' Return string and statements for ternary operator '''
pass
...
...
ogAST.py
View file @
352e2a4e
...
...
@@ -232,13 +232,14 @@ class PrimOctetStringLiteral(Primary):
pass
class
Prim
IfThenElse
(
Primary
):
class
Prim
Conditional
(
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
...
...
ogParser.py
View file @
352e2a4e
...
...
@@ -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
.
Prim
IfThenElse
)
\
elif
isinstance
(
primary
,
ogAST
.
Prim
Conditional
)
\
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
.
Prim
IfThenElse
(
expr
=
ogAST
.
Prim
Conditional
(
get_input_string
(
root
),
root
.
getLine
(),
root
.
getCharPositionInLine
()
...
...
sdl92.g
View file @
352e2a4e
...
...
@@ -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
...
...
sdl92Lexer.py
View file @
352e2a4e
This diff is collapsed.
Click to expand it.
sdl92Parser.py
View file @
352e2a4e
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment