Skip to content
Permalink
Browse files

extend list of supported R code

  • Loading branch information...
sdpython committed Jun 22, 2017
1 parent 6bb6335 commit a0bd25bb3f1bfc5a687f13b59627f02446527958
@@ -0,0 +1,13 @@
if a == 0:

b = 1


elif a == 1:

b = 2


else:

b = 3
@@ -0,0 +1,12 @@
if (a == 0)
{
b = 1;
}
else if (a == 1)
{
b = 2;
}
else
{
b = 3;
}
@@ -1,7 +1,8 @@
from python2r_helper import ImplicitColumn
from python2r_helper import bquote
from python2r_helper import list_or_dict


def some():
t = bquote(list(a=~ (age == ImplicitColumn . whichAge)))
t = bquote(list_or_dict(a=~ (age == ImplicitColumn . whichAge)))
r = bquote(~ (age == ImplicitColumn . whichAge))
@@ -1,3 +1,4 @@
from python2r_helper import list_or_dict
from python2r_helper import within

# some comments
@@ -9,8 +10,8 @@ def test_machine_chouette_something():
def func(infile, csch, predmod):
impnode = zoo_test(infile="$infile", dot="$dotdot", csch=csch)
obj = waouh(
impnode, nono, trnode, inputs=list(
infile=infile), outputs=list(
impnode, nono, trnode, inputs=list_or_dict(
infile=infile), outputs=list_or_dict(
predmod=predmod))

# one
@@ -1,5 +1,5 @@
"""
@brief test log(time=5s)
@brief test log(time=7s)
You should indicate a time in seconds. The program ``run_unittests.py``
will sort all test files by increasing time and run them.
@@ -216,10 +216,17 @@ forexpr

ifexpr
: 'if' '(' expr ')' NL* expr
(elseif '(' expr ')' NL* expr NL* )*
;

ifelseexpr
: 'if' '(' expr ')' NL* expr NL* 'else' NL* expr
: 'if' '(' expr ')' NL* expr NL*
(elseif '(' expr ')' NL* expr NL* )*
'else' NL* expr
;

elseif
: 'else' 'if'
;

returnexpr
@@ -258,6 +258,14 @@ def enterIfelseexpr(self, ctx: RParser.IfelseexprContext):
def exitIfelseexpr(self, ctx: RParser.IfelseexprContext):
pass

# Enter a parse tree produced by RParser#elseif.
def enterElseif(self, ctx: RParser.ElseifContext):
pass

# Exit a parse tree produced by RParser#elseif.
def exitElseif(self, ctx: RParser.ElseifContext):
pass

# Enter a parse tree produced by RParser#returnexpr.
def enterReturnexpr(self, ctx: RParser.ReturnexprContext):
pass

Large diffs are not rendered by default.

@@ -21,11 +21,11 @@ def __init__(self, node, message, sofar, sostack):
text = node.symbol.text
except AttributeError:
text = "ERROR"
mes = "Unable to convert\n'{0}'\n{1}\n{2}\nPARENT\n'{3}'\n{4}\nSOFAR\n{5}\nSOSTACK\n{5}".format(
mes = "Unable to convert\n'{0}'\n{1}\n{2}\nPARENT\n'{3}'\n{4}\n---SOFAR---\n{5}\n---SOSTACK---\n{5}\n---END---".format(
text, type(node), node, ("" if isinstance(
node, str) else node.parentCtx),
(str if isinstance(node, str) else type(node.parentCtx)), sofar, sostack)
mes += "\n------------\n" + str(message)
mes += "\n------------\nMESSAGE=\n" + str(message)
Exception.__init__(self, mes)


@@ -185,7 +185,14 @@ def add_code(self, node):
self.in_formula = True
elif name_parent == "Functioncall":
self.stack.append(("Functioncall", ""))
self.stack.append((name, node))
if text == "list":
self.imports.add("from python2r_helper import list_or_dict")
self.stack.append((name, "list_or_dict"))
elif text == "is":
self.imports.add("from python2r_helper import is_")
self.stack.append((name, "is_"))
else:
self.stack.append((name, node))
return self.add_code_final()
elif name in ("Affectop", "Comparison"):
self.stack.append((name, node))
@@ -243,26 +250,37 @@ def add_code(self, node):
if len(self.elements) > 0 and self.elements[-1] != '\n':
self.elements.append("\n")
return self.add_code_final()
if text == "break":
self.stack.append((name, node))
return self.add_code_final()
if text == "{":
self.empty_stack()
if len(self.block) == 0:
raise R2PyConversionError(node, name, "".join(
self.elements), "\n".join(str(_) for _ in self.stack))
self.block[-1] = True
return self.add_code_final()
# We are in an expression.
self.stack.append((name, "("))
return self.add_code_final()
else:
self.block[-1] = True
return self.add_code_final()
if text == "}":
# We are in an expression.
self.empty_stack()
if len(self.block) == 0:
raise R2PyConversionError(node, name, "".join(
self.elements), "\n".join(str(_) for _ in self.stack))
self.block.pop()
self.indent -= 1
self.fLOG(
"[TreeStringListener.add_code] indent -= 1", "--", self.indent)
return self.add_code_final()
self.stack.append((name, ")"))
return self.add_code_final()
else:
self.block.pop()
self.indent -= 1
self.fLOG(
"[TreeStringListener.add_code] indent -= 1", "--", self.indent)
return self.add_code_final()
elif name == "Form":
self.stack.append((None, node))
return self.add_code_final()
if text == "...":
self.stack.append((None, "*args"))
return self.add_code_final()
else:
self.stack.append((None, node))
return self.add_code_final()
elif name == "Formlist":
self.stack.append((None, node))
return self.add_code_final()
@@ -283,6 +301,9 @@ def add_code(self, node):
if text == ':':
self.stack.append((name, node))
return self.add_code_final()
if text == '...':
self.stack.append((name, "*args"))
return self.add_code_final()
elif name == "Sublist":
if text == ",":
self.stack.append((name, node))
@@ -307,6 +328,23 @@ def add_code(self, node):
if len(self.elements) > 0 and self.elements[-1] != '\n':
self.elements.append("\n")
return self.add_code_final()
elif name == "Elseif":
if text == "if":
if id(node.parentCtx.parentCtx) not in self.indent_level:
raise R2PyConversionError(node, name, "".join(
self.elements), "\n".join(str(_) for _ in self.stack))
else:
if self.indent_level[id(node.parentCtx.parentCtx)] != self.indent:
raise R2PyConversionError(node, name, "".join(
self.elements), "\n".join(str(_) for _ in self.stack))
self.stack.append((name, "elif"))
return self.add_code_final()
elif text == "else":
# We do nothing. If follows.
return self.add_code_final()
else:
# There should be nothing else.
pass
elif name == "Ifelseexpr" or name == "Ifexpr":
if self.search_parents(node, "Sublist") or self.search_parents(node, "Affectation", 2):
if text == "if":
@@ -355,6 +393,12 @@ def add_code(self, node):
elif text == "in":
self.stack.append((name, node))
return self.add_code_final()
elif name == "Whileexpr":
if text == "while":
self.stack.append((name, node))
return self.add_code_final()
elif text in ('(', ')'):
return self.add_code_final()
elif name == "Rangeop":
if text == ":":
self.memo.append("range")
@@ -502,6 +546,9 @@ def empty_stack(self):
elif name == "Forexpr":
is_for = True
break
elif name == "Whileexpr":
is_for = True
break
elif name == "Identifier":
text = node if isinstance(node, str) else node.symbol.text
if text == "asNamespace":
@@ -513,7 +560,8 @@ def empty_stack(self):

if is_function_def:
self.elements.append("\n")
function_name = self.stack[0][1].symbol.text
function_name = self.stack[0][1] if isinstance(
self.stack[0][1], str) else self.stack[0][1].symbol.text
self.fLOG(
"[TreeStringListener.empty_stack] add function '{0}'".format(function_name))
self.elements.append(" " * self.indent)
@@ -698,7 +746,7 @@ def to_python(self, name, node):
return ","
else:
return text
elif name == "Ifexpr" or name == "Ifelseexpr":
elif name in ("Ifexpr", "Ifelseexpr"):
text = node.symbol.text
if text in ("if", "else") and (self.search_parents(node, "Sublist") or
self.search_parents(node, "Affectation", 2)):

0 comments on commit a0bd25b

Please sign in to comment.
You can’t perform that action at this time.