Skip to content

Commit 88b6dd8

Browse files
Merge pull request #192 from cyrillemidingoyi/master
update
2 parents 4f8eb24 + 96972ae commit 88b6dd8

30 files changed

+457
-96
lines changed

src/pycropml/cyml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pycropml.transpiler.antlr_py.bioma.run import run_bioma
1515
from pycropml.transpiler.antlr_py.openalea.run import run_openalea
1616
from pycropml.transpiler.antlr_py.fortran.run import run_fortran
17+
from pycropml.transpiler.antlr_py.python.run import run_python
1718
from pycropml import render_cyml, nameconvention
1819
from pycropml.pparse import model_parser
1920
from pycropml.writeTest import WriteTest
@@ -32,7 +33,7 @@
3233
NAMES = {'r':'r','cs':'csharp','cpp':'cpp', 'py':'python', 'f90':'fortran', 'java':'java', 'simplace':'simplace', 'sirius':'sirius', "openalea":"openalea","apsim":"apsim", "record":"record", "dssat":"dssat","bioma":"bioma", "stics":"stics", "sirius2":"sirius2"}
3334
ext = {'r':'r','cs':'cs','cpp':'cpp', 'py':'py', 'f90':'f90', 'java':'java', 'simplace':'java', 'sirius':'cs','bioma':'cs', "openalea":"py", "apsim":"cs", "record":"cpp", "dssat":"f90", "stics":"f90", "sirius2":'cs'}
3435

35-
cymltx_languages = ['dssat', "simplace", "bioma", "openalea", "f90", "stics"]
36+
cymltx_languages = ['dssat', "simplace", "bioma", "openalea", "f90", "stics", "py"]
3637
langs = ["cs", "cpp", "java", "f90", "r", "py"]
3738

3839
domain_class = ["cs", "java", 'sirius','cpp', "bioma", "sirius2"]

src/pycropml/transpiler/antlr_py/bioma/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ def run_bioma(component, output):
425425
env = {m.name:m.pseudo_type for j in rr.declarations for m in j.decl}
426426
zz = CheckingInOut( {},isAlgo = True)
427427
r_ch = zz.process(vv)
428+
print(zz.inputs, zz.outputs)
428429
z.modelunit(description, var_, all_var_pa,var, list(set(zz.inputs)), list(set(zz.outputs)))
429430
print(z.model.name)
430431
#print(z.model.outputs)

src/pycropml/transpiler/antlr_py/createXml.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def run_compo(self):
128128

129129
composition = ns.Composition()
130130
for m in md.model:
131-
print("uuu", m)
132131
composition.append(ns.Model(name=m, id=self.pkgname+'.'+m, filename='unit.'+m+'.xml'))
133132

134133
links = ns.Links()

src/pycropml/transpiler/antlr_py/csharp/csharp_preprocessing.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ def inst_dclass(meth):
4949

5050
class CheckingInOut(Middleware):
5151

52+
"""_summary_
53+
This code defines a middleware class called "CheckingInOut"
54+
that checks the inputs and outputs of a given code block.
55+
It does this by keeping track of the current scope and environment,
56+
and adding any new variables to the environment.
57+
It also keeps track of the inputs and outputs of the code block
58+
by checking if a variable is already in the current scope or not.
59+
The middleware class has methods for different types of statements
60+
and expressions, such as assignment, if statements, for loops, and function calls.
61+
The purpose of this middleware is to ensure that the inputs and outputs
62+
of a code block are well-defined and can be used by other parts of a program.
63+
"""
64+
5265
def __init__(self, env_init=None, isAlgo=False):
5366
if env_init is None:
5467
self.env_init = {}
@@ -80,8 +93,9 @@ def action_assignment(self, tree):
8093
if tree.target.type == "local":
8194
t_name = tree.target.name
8295
type_ = tree.target.pseudo_type
83-
if t_name not in self.current_scope and not self.isAlgo:
96+
if t_name not in self.current_scope and not self.isAlgo: # self.env[-1]
8497
self.inputs.append(t_name)
98+
8599
self.env[-1][t_name] = type_
86100
self.outputs.append(t_name)
87101

@@ -123,33 +137,48 @@ def action_declaration(self, tree):
123137
for d in tree.decl:
124138
if "value" in dir(d):
125139
self.transform_default(d.value)
140+
self.env[-1][d.name] = d.type
141+
self.current_scope = self.current()
126142
return tree
127143

128144

129145
def action_local(self, tree):
130-
if tree.name not in self.current_scope:
146+
147+
if tree.name not in self.current_scope:# and tree.name not in self.outputs:
131148
self.inputs.append(tree.name)
149+
self.env[-1][tree.name] = tree.type
150+
self.current_scope = self.current()
132151
return tree
133152

134153
def action_if_statement(self, tree):
135154
self.env.append({})
136155
self.transform(tree.test)
137156
self.transform(tree.block)
157+
m1 = self.env[-1]
158+
print("pppppp", m1)
138159
self.env.pop()
139160
self.current_scope = self.current()
140161

141162
if tree.otherwise:
142163
self.env.append({})
143164
self.transform(tree.otherwise)
165+
m2 = self.env[-1]
166+
print("nnnnnnnn", m2)
144167
self.env.pop()
168+
common_keys = m1.keys( ) & m2.keys()
169+
common_dict = {k:m1[k] for k in common_keys}
170+
self.env[-1].update(common_dict)
145171
self.current_scope = self.current()
172+
print("iiiii", self.inputs)
173+
print("ifff", self.current_scope)
146174
return tree
147175

148-
def action_elseif_statement(self, tree):
149-
return self.workflow(tree)
176+
#def action_elseif_statement(self, tree):
177+
#print("oooooooooooooooo")
178+
#return self.workflow(tree)
150179

151-
def action_else_statement(self, tree):
152-
return self.workflow(tree)
180+
#def action_else_statement(self, tree):
181+
#return self.workflow(tree)
153182

154183
def action_for_statement(self, tree):
155184
return self.workflow(tree)

src/pycropml/transpiler/antlr_py/extract_metadata_from_comment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def attval(pat_name, string):
102102
lines = att[0][0].split('\n')[1:-1]
103103
dic = {}
104104
for line in lines:
105+
if not line: continue
105106
attribute = re.search(pattern_attr_val, line).group("attribute")
106107
value = re.search(pattern_attr_val, line, re.ASCII).group("value").replace('\r', "")
107108
dic[attribute] = str(value)

src/pycropml/transpiler/antlr_py/fortran/f90_cyml.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,26 @@ def visit_ExprStatNode(self, node):
304304
"expr": self.visit(node.expr)
305305
}
306306

307+
def visit_sliceindex(self, node):
308+
rec = self.visit(node.receiver)
309+
print(rec)
310+
return {'type': 'sliceindex',
311+
'receiver': self.visit(node.receiver),
312+
'message': 'sliceindex',
313+
'args': self.visit(node.args),
314+
'pseudo_type': rec["pseudo_type"]}
315+
307316
def visit_index(self, node):
308317
z = self.visit(node.sequence)
309318
index = self.visit(node.index)
319+
if not isinstance(index, list): index = index
320+
elif isinstance(index, list) and len(index)==1: index = index[0]
321+
else: print("#########TODO") #TODO
322+
#print("bababa", index)
310323
return {'type': 'index',
311324
'sequence': z,
312325
'index': {"type":"binary_op", "op":"-", "left":index, "right":{"type":"int", "value":"1","pseudo_type":"int"}},
313-
'pseudo_type': z["pseudo_type"]}
326+
'pseudo_type': z["pseudo_type"][-1]}
314327

315328
def visit_while_statement(self, node):
316329

@@ -350,7 +363,6 @@ def visit_unary_op(self, node):
350363

351364
def visit_whereconstruct(self, node):
352365
test = self.visit(node.test)
353-
print(test, "yuuuuuuuuuuuu")
354366
body = self.visit(node.body)
355367
m = transform_to_syntax_tree(body)
356368
r = TransformLocal()

src/pycropml/transpiler/antlr_py/fortran/fortranExtraction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def __init__(self):
2828

2929
def getProcess(self, tree):
3030
self.getTypeNode(tree, "function_definition")
31-
print([m.name for m in self.getTree])
3231
res = []
3332
for n in self.getTree:
3433
if n.comments:
@@ -94,6 +93,7 @@ def modelunit(self, file, tree):
9493
def externFunction(self, algo):
9594
self.getTypeNode(algo, "call_stmt")
9695
custom_call = self.getTree
96+
9797
methNames = set({c.name for c in custom_call}) if custom_call else set()
9898
return methNames
9999

src/pycropml/transpiler/antlr_py/fortran/fortranTransformer.py

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ class ActionStmt(AliasNode):
383383

384384
class AcImpliedDo(AliasNode):
385385
_fields_spec = ["acImpliedDo","impliedDoVariable" ,"expression"]
386+
387+
class SFExprList(AliasNode):
388+
_fields_spec = ["expression", "DOUBLECOLON", "COLON"]
389+
390+
class SubscriptTripletTail(AliasNode):
391+
_fields_spec = ["expression", "DOUBLECOLON", "COLON"]
386392

387393

388394

@@ -631,6 +637,10 @@ def visit_ActionStmt(self, node):
631637
return ActionStmt.from_spec(node)
632638
def visit_AcImpliedDo(self, node):
633639
return AcImpliedDo.from_spec(node)
640+
def visit_SFExprList(self, node):
641+
return SFExprList.from_spec(node)
642+
def visit_SubscriptTripletTail(self, node):
643+
return SubscriptTripletTail.from_spec(node)
634644

635645
class AstTransformer():
636646
def __init__(self, tree, code :str = None ,comments: str=None, env=None):
@@ -642,7 +652,6 @@ def __init__(self, tree, code :str = None ,comments: str=None, env=None):
642652
self.type_env = Env(dict(list(TYPED_API.items())), None)
643653
self.type_env['arrays']={}
644654
self.code : str = code
645-
print(self.type_env['arrays'])
646655
if self.code:
647656
self.codelines = self.code.split("\n")
648657

@@ -1144,21 +1153,23 @@ def visit_typedeclarationstmt(self, node, typeSpec, entityDeclList, DOUBLECOLON,
11441153
if attrSpecSeq:
11451154
self.attr = {}
11461155
attr = self.visit(attrSpecSeq)
1147-
if attr and "DIMENSION" in attr and "ALLOCATABLE" in attr and attr["DIMENSION"] == [":"]:
1156+
1157+
#if attr and "DIMENSION" in attr and "ALLOCATABLE" in attr and attr["DIMENSION"] == [":"]:
1158+
if attr and ("allocatable" in attr or "ALLOCATABLE" in attr):
11481159
typ = "list"
11491160
pseudo_type = ["list", elemtype_]
11501161
else :
11511162
typ = elemtype_
11521163
pseudo_type = elemtype_
11531164
for j in names:
11541165
zj = {"type":typ, "name": str(j["name"]), "pseudo_type":pseudo_type}
1155-
if "dim" in j:
1166+
if "dim" in j and typ!="list":
11561167
zj["type"] = "array"
11571168
zj["dim"] = j["dim"]
11581169
zj["elts"] = j["elts"]
11591170
zj["pseudo_type"] = ["array", elemtype_]
11601171
self.type_env['arrays'].update({zj["name"]: zj["elts"] })
1161-
elif attr and "DIMENSION" in attr and "ALLOCATABLE" not in attr:
1172+
elif attr and "DIMENSION" in attr and ("ALLOCATABLE" not in attr and "allocatable" not in attr):
11621173
zj["type"] = "array"
11631174
zj["dim"] = len(attr["DIMENSION"])
11641175
zj["elts"] = attr["DIMENSION"]
@@ -1172,8 +1183,8 @@ def visit_typedeclarationstmt(self, node, typeSpec, entityDeclList, DOUBLECOLON,
11721183
return res
11731184

11741185
def visit_attrspecseq(self, node, attrSpecSeq,attrSpec,comments, location):
1175-
if "DIMENSION" in dir(attrSpec) : self.attr.update({"DIMENSION": self.visit(attrSpec.arraySpec)})
1176-
elif "INTENT" in dir(attrSpec): self.attr.update({"INTENT": self.visit(attrSpec.intentSpec)})
1186+
if ("DIMENSION" in dir(attrSpec) or "dimmension" in dir(attrSpec) ) : self.attr.update({"DIMENSION": self.visit(attrSpec.arraySpec)})
1187+
elif ("INTENT" in dir(attrSpec) or "intent" in dir(attrSpec)): self.attr.update({"INTENT": self.visit(attrSpec.intentSpec)})
11771188
else : self.attr.update({self.visit(attrSpec):None})
11781189

11791190
if attrSpecSeq:
@@ -1241,17 +1252,17 @@ def visit_assignmentstmt(self, node, label, NAME, sFExprListRef, substringRange,
12411252
"value" : self.visit(expression),
12421253
"pseudo_type":"VOID",
12431254
"comments":comments}
1244-
if isinstance(res["target"]["pseudo_type"], list) and res["target"]["pseudo_type"][0]=="array" and res["value"]["type"] in ["int", "float", "str", "bool"]:
1245-
type_ = res["value"]["type"]
1246-
res["value"] = {'type': 'array',
1255+
if isinstance(res["target"]["pseudo_type"], list) and (res["target"]["pseudo_type"][0]=="array" or res["target"]["pseudo_type"][0]=="list") and res["value"]["pseudo_type"] in ["int", "float", "str", "bool"]:
1256+
type_ = res["value"]["pseudo_type"]
1257+
res["value"] = {'type': res["target"]["pseudo_type"][0],
12471258
'elements': {'type': 'binary_op',
12481259
'op': '*',
12491260
'left': {'type': 'list',
12501261
'pseudo_type': ['list', type_],
12511262
'elements': [res["value"]]},
12521263
'right': self.type_env['arrays'][res["target"]["name"]],
12531264
'pseudo_type': ['list', type_]},
1254-
'pseudo_type': ['array', type_]}
1265+
'pseudo_type': type_}
12551266
if self.recursive and str(NAME)==self.out:
12561267
return {"type":"implicit_return",
12571268
"value": res["value"],
@@ -1279,6 +1290,7 @@ def visit_sfexprlistref(self, node,sFExprList, commaSectionSubscript,comments, l
12791290
return self.visit(sFExprList)
12801291
else:
12811292
return self.translate_list(commaSectionSubscript)
1293+
12821294

12831295
def visit_expression(self, node, level5Expr, expression, definedBinaryOp, comments,location):
12841296
if definedBinaryOp:
@@ -1946,12 +1958,34 @@ def visit_sectionsubscriptlist(self, node,sectionSubscript, comments,location ):
19461958

19471959
def visit_sectionsubscript(self, node, expression,subscriptTripletTail,comments,location):
19481960
if expression:
1961+
res = self.visit(expression)
1962+
#print("uuuuuuuuu", res)
19491963
if subscriptTripletTail:
1950-
pass
1964+
r = self.visit(subscriptTripletTail)
1965+
#print("uuuuuuuuu2", r)
1966+
if "args" in r and r["colon"]==1:
1967+
res2 = {"sliceindex":[res, r["args"][0]]}
1968+
return res2
1969+
else:
1970+
print(f" visit_sectionsubscript not implemented at {location}")
19511971
else:
1952-
return self.visit(expression)
1972+
return res
19531973
else:
19541974
return self.visit(subscriptTripletTail)
1975+
1976+
def visit_subscripttriplettail(self, node, expression, COLON, DOUBLECOLON, comments, location):
1977+
if expression:
1978+
res = self.visit(expression)
1979+
if COLON:
1980+
if len(res)==1:
1981+
return {"args":res, "colon":1}
1982+
else:
1983+
return {"args":res, "colon":2}
1984+
elif DOUBLECOLON:
1985+
return {"args":res, "colon":3}
1986+
else:
1987+
return {"colon":3}
1988+
19551989

19561990

19571991
def visit_functionarglist(self, node, functionArg, functionArgList,sectionSubscriptList,comments, location):
@@ -2001,16 +2035,25 @@ def translate_func(self, name, args, location):
20012035
return z
20022036
id_type = self.type_env[name]
20032037
if id_type and isinstance(id_type, list):
2038+
args = args[0][0]
2039+
if "sliceindex" in args:
2040+
res = {'type': 'sliceindex',
2041+
'receiver': {'type': 'local',
2042+
'name': name,
2043+
'pseudo_type': id_type},
2044+
'message': 'sliceindex',
2045+
'args': [args["sliceindex"][0],args["sliceindex"][1]],
2046+
'pseudo_type': id_type}
2047+
return res
20042048
return {'type': 'index',
20052049
'sequence': {'type': 'local',
20062050
'name': name,
20072051
'pseudo_type': id_type},
2008-
'index': args[0][0],
2052+
'index': args,
20092053
'pseudo_type': id_type[-1] }
20102054

20112055
else:
20122056
fname = name
2013-
print(name, "jjhhhhhhhhh")
20142057
c = self.type_env.top['functions']
20152058
if fname in c:
20162059
pseudo_type = c[fname][-1]
@@ -2377,6 +2420,19 @@ def visit_arrayconstructor(self, node, OBRACKETSLASH, CBRACKETSLASH, acValueList
23772420
def visit_acimplieddo(self, node, acImpliedDo,impliedDoVariable ,expression, comments, location):
23782421
print("akklll", location)
23792422
return
2423+
2424+
def visit_sfexprlist(self, node, expression, COLON, DOUBLECOLON, comments, location):
2425+
res = self.visit(expression)
2426+
if not COLON and not DOUBLECOLON:
2427+
return res
2428+
elif COLON and expression:
2429+
print("uiiiiiiii", "TODO") #TODO
2430+
2431+
2432+
2433+
2434+
2435+
23802436

23812437

23822438

src/pycropml/transpiler/antlr_py/fortran/fortran_preprocessing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def action_call_stmt(self, tree):
106106
inputs = [args[n] for n in subr.inputs_pos]
107107
if otherparams: inputs = otherparams + inputs
108108
outputs = [args[n] for n in subr.outputs_pos]
109-
tree =Node(type="assignment", target = Node(type="tuple", elements=outputs), value = Node(type="custom_call", function=name, args=inputs), comments = comments)
109+
if len(outputs)==1: tree =Node(type="assignment", target = Node(type="local", name=outputs[0].name, pseudo_type=outputs[0].pseudo_type), value = Node(type="custom_call", function=name, args=inputs), comments = comments)
110+
else: tree =Node(type="assignment", target = Node(type="tuple", elements=outputs), value = Node(type="custom_call", function=name, args=inputs), comments = comments)
110111
return self.transform_default(tree)
111112

112113

0 commit comments

Comments
 (0)