Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
OpenGEODE
Commits
43573e2e
Commit
43573e2e
authored
Jan 17, 2015
by
Maxime Perrotin
Browse files
Elaborate the string template example for Ada
parent
ca2c57c1
Changes
4
Hide whitespace changes
Inline
Side-by-side
opengeode/StgBackend.py
View file @
43573e2e
...
...
@@ -60,6 +60,7 @@ def _process(process, simu=False, stgfile='ada_source.st', **kwargs):
global
TYPES
global
STG
global
SHARED_LIB
process_name
=
process
.
processName
if
not
stg
:
LOG
.
error
(
'Library missing. As root: pip install stringtemplate3'
)
...
...
@@ -72,11 +73,9 @@ def _process(process, simu=False, stgfile='ada_source.st', **kwargs):
return
# Get the template corresponding to a SDL PROCESS
template
=
STG
.
getInstanceOf
(
"process"
)
template
[
'name'
]
=
process
.
processName
print
str
(
template
)
process_template
=
STG
.
getInstanceOf
(
"process"
)
process_template
[
'name'
]
=
process_name
process_name
=
process
.
processName
TYPES
=
process
.
dataview
del
OUT_SIGNALS
[:]
del
PROCEDURES
[:]
...
...
@@ -96,9 +95,13 @@ def _process(process, simu=False, stgfile='ada_source.st', **kwargs):
VARIABLES
.
update
(
process
.
variables
)
# Initialize array of strings containing all local declarations
process_decl
=
[]
# Generate the code to declare process-level variables
process_level_decl
=
[]
for
var_name
,
(
var_type
,
def_value
)
in
process
.
variables
.
viewitems
():
dcl_template
=
STG
.
getInstanceOf
(
"dcl"
)
if
def_value
:
# Expression must be a ground expression, i.e. must not
# require temporary variable to store computed result
...
...
@@ -107,72 +110,39 @@ def _process(process, simu=False, stgfile='ada_source.st', **kwargs):
if
varbty
.
kind
in
(
'SequenceOfType'
,
'OctetStringType'
):
dstr
=
array_content
(
def_value
,
dstr
,
varbty
)
assert
not
dst
and
not
dlocal
,
'DCL: Expecting a ground expression'
process_level_decl
.
append
(
u
'l_{n} : aliased {sort}{default};'
.
format
(
n
=
var_name
,
sort
=
type_name
(
var_type
),
default
=
u
' := '
+
dstr
if
def_value
else
u
''
))
# Add the process states list to the process-level variables
statelist
=
', '
.
join
(
name
for
name
in
process
.
mapping
.
iterkeys
()
if
not
name
.
endswith
(
u
'START'
))
or
'No_State'
if
statelist
:
states_decl
=
u
'type States is ({});'
.
format
(
statelist
)
process_level_decl
.
append
(
states_decl
)
process_level_decl
.
append
(
'state : states;'
)
dcl_template
[
'var'
]
=
var_name
dcl_template
[
'sort'
]
=
type_name
(
var_type
)
dcl_template
[
'def_expr'
]
=
dstr
if
def_value
else
''
process_decl
.
append
(
str
(
dcl_template
))
# Set the DCL declarations variable in the process template
process_template
[
'decl'
]
=
process_decl
# Set the list of SDL states
process_template
[
'states'
]
=
(
name
for
name
in
process
.
mapping
.
iterkeys
()
if
not
name
.
endswith
(
u
'START'
))
# Set constants corresponding to substate start transitions
constants
=
[]
for
name
,
val
in
process
.
mapping
.
viewitems
():
if
name
.
endswith
(
u
'START'
)
and
name
!=
u
'START'
:
process_level_decl
.
append
(
u
'{name} : constant := {val};'
.
format
(
name
=
name
,
val
=
str
(
val
)))
const_template
=
STG
.
getInstanceOf
(
"constant"
)
const_template
[
'var'
]
=
name
const_template
[
'val'
]
=
str
(
val
)
constants
.
append
(
str
(
const_template
))
# Add the declaration of the runTransition procedure
process_level_decl
.
append
(
'procedure runTransition(Id: Integer);'
)
# Generate the code of the start transition:
start_transition
=
[
'begin'
,
'runTransition(0);'
]
process_template
[
'constants'
]
=
constants
# Generate the TASTE template
try
:
asn1_modules
=
'
\n
'
.
join
([
'with {dv};
\n
use {dv};'
.
format
(
dv
=
dv
.
replace
(
'-'
,
'_'
))
for
dv
in
process
.
asn1Modules
])
asn1_modules
+=
'
\n
with adaasn1rtl;
\n
use adaasn1rtl;'
process_template
[
'asn1_mod'
]
=
(
dv
.
replace
(
'-'
,
'_'
)
for
dv
in
process
.
asn1Modules
)
except
TypeError
:
asn1_modules
=
'-- No ASN.1 data types are used in this model'
taste_template
=
[
'''
\
-- This file was generated automatically: DO NOT MODIFY IT !
with System.IO;
use System.IO;
with Ada.Unchecked_Conversion;
with Ada.Numerics.Generic_Elementary_Functions;
{dataview}
with Interfaces;
use Interfaces;
{C}
package body {process_name} is'''
.
format
(
process_name
=
process_name
,
dataview
=
asn1_modules
,
C
=
'with Interfaces.C.Strings;
\n
'
'use Interfaces.C.Strings;'
if
simu
else
''
)]
# Generate the source file (.ads) header
ads_template
=
[
'''
\
-- This file was generated automatically: DO NOT MODIFY IT !
{dataview}
{C}
package {process_name} is'''
.
format
(
process_name
=
process_name
,
dataview
=
asn1_modules
,
C
=
'with Interfaces.C.Strings;
\n
'
'use Interfaces.C.Strings;'
if
simu
else
''
)]
pass
# No ASN.1 module
print
str
(
process_template
)
return
dll_api
=
[]
if
simu
:
...
...
opengeode/ada_body.st
View file @
43573e2e
group
adb
;
process
(
name
,
decl
,
code
)
:
:=
<<
/*
top
-level:
template
for
the
code
of
a
process
*
name:
process
name
*
decl:
list
of
local
declarations
*
constants:
list
of
start
named
start
transitions
(
when
using
substates
)
*
states:
list
of
states
*
asn1_mod
:
list
of
ASN
.
1
modules
*
simu
,
dll
:
flags
set
by
the
user
for
generation
of
optional
code
*/
process
(
name
,
decl
,
constants
,
states
,
asn1_mod
,
simu
,
dll
)
:
:=
<<
--
This
file
was
generated
automatically:
DO
NOT
MODIFY
!
package
<
name
>
is
<
if
(
asn1_mod
)
>
--
ASN
.
1
Modules
used
by
this
process,
and
generated
by
ASN1SCC
<
asn
1
_
mod:
{
each
|
with
<
each
>
;
use
<
each
>
;
};
separator=
"\n"
>
--
ASN
.
1
Runtime,
containing
basic
types
with
AdaASN1rtl
;
use
AdaASN1rtl
;
<
endif
>
--
Include
Ada
generic
libraries
used
by
the
generated
code
with
System
.
IO
;
use
System
.
IO
;
with
Ada
.
Unchecked_Conversion
;
with
Ada
.
Numeric
.
Generic_Elementary_Functions
;
with
Interfaces
;
use
Interfaces
;
<
if
(
simu
)
>
--
Access
to
C
compatible
types
when
interacting
with
another
language
with
Interfaces
.
C
.
Strings
;
use
Interfaces
.
C
.
Strings
;
<
endif
>
package
body
<
name
>
is
--
Local
variables
declared
in
textboxes
<
decl
;
separator=
"\n"
>
--
List
of
SDL
states,
and
variable
holding
the
current
state
type
States
is
(
<
states
;
separator=
", "
>
);
state:
States
;
<
if
(
constants
)
>
--
Constants
holding
substate
identifier
for
the
start
transition
<
constants
;
separator=
"\n"
>
<
endif
>
--
Declaration
of
the
procedure
executing
transitions
procedure
RunTransition
(
Id:
Integer
);
--
Process
initialization:
execute
the
START
transition
)
begin
RunTransition
(
0
);
end
<
name
>
;
>>
/*
just
a
test
*/
vardef
(
type
,
name
)
:
:=
"<type> <name>;"
/*
Variable
declaration
(
DCL
var
sort
[
:=
def_expr
];
*/
dcl
(
var
,
sort
,
def_expr
)
:
:=
<<
l_
<
var
>
:
aliased
<
sort
><
if
(
def_expr
)
>
:=
<
def_expr
><
endif
>
;
>>
/*
Constant
declaration
*/
constant
(
var
,
val
)
:
:=
"<var> : constant := <val>;"
opengeode/ada_source.st
View file @
43573e2e
group
ads
;
process
(
name
,
decl
,
co
de
)
:
:=
<<
process
(
name
,
decl
,
co
nstants
,
states
,
asn1_mod
,
simu
,
dll
)
:
:=
<<
--
This
file
was
generated
automatically:
DO
NOT
MODIFY
!
<
if
(
asn1_mod
)
>
--
ASN
.
1
Modules
used
by
this
process,
and
generated
by
ASN1SCC
<
asn
1
_
mod:
{
each
|
with
<
each
>
;
use
<
each
>
;
};
separator=
"\n"
>
<
endif
>
<
if
(
simu
)
>
--
Access
to
C
compatible
types
when
interacting
with
another
language
with
Interfaces
.
C
.
Strings
;
use
Interfaces
.
C
.
Strings
;
<
endif
>
package
<
name
>
is
end
<
name
>
;
>>
/*
just
a
test
*/
vardef
(
type
,
name
)
:
:=
"<type> <name>;
"
/*
Variable
declaration
(
DCL
var
sort
[
:=
def_expr
];
*/
dcl
(
var
,
sort
,
def_expr
)
:
:=
"
"
/*
Constant
declaration
*/
constant
(
var
,
val
)
:
:=
""
opengeode/opengeode.py
View file @
43573e2e
...
...
@@ -1962,8 +1962,7 @@ def parse_args():
help
=
'Generate getters/setters to access internal state'
)
parser
.
add_argument
(
'--dll'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Generate callback hooks when compiling as shared object'
)
parser
.
add_argument
(
'--stg'
,
type
=
str
,
default
=
'ada_source.st'
,
metavar
=
'file'
,
parser
.
add_argument
(
'--stg'
,
type
=
str
,
metavar
=
'file'
,
help
=
'Generate code using a custom String Template file'
)
parser
.
add_argument
(
'--check'
,
action
=
'store_true'
,
dest
=
'check'
,
help
=
'Check a .pr file for syntax and semantics'
)
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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