Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc fixes to translations. #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/matlab2cpp/node/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,11 @@ def auxillary(node, type, convert):

# Return value
aux_var = matlab2cpp.collection.Var(assign, var)
aux_var.create_declare()
aux_var.declare.type = type
aux_var.declare.backend = type
aux_var.type = type
aux_var.backend = type
aux_var.create_declare()

if convert:
rhs = matlab2cpp.collection.Get(assign, "_conv_to")
Expand All @@ -194,7 +196,10 @@ def auxillary(node, type, convert):
rhs = assign

swap_var = matlab2cpp.collection.Var(rhs, var)
swap_var.type = type
swap_var.backend = type
swap_var.declare.type = type
swap_var.declare.backend = type

# Place Assign correctly in Block
i = block.children.index(line)
Expand Down Expand Up @@ -353,7 +358,6 @@ def create_declare(node):
var.type="struct"

return matlab2cpp.collection.Var(struct, name=value)
parent = struct

else:
parent = node.func[0]
Expand Down
2 changes: 1 addition & 1 deletion src/matlab2cpp/node/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def auxiliary(self, type=None, convert=False):
1 4| | | | Int int int
1 1| Statement code_block TYPE
1 1| | Plus expression irowvec
1 1| | | Var unknown irowvec _aux_irowvec_1
1 1| | | Var irowvec irowvec _aux_irowvec_1
1 7| | | Int int int
"""
return backend.auxillary(self, type, convert)
Expand Down
8 changes: 4 additions & 4 deletions src/matlab2cpp/rules/_reserved.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,14 +996,14 @@ def Get_sum(node):
>>> print(matlab2cpp.qscript("a=[-1,2;3,4]; b = sum(a, 1)", suggest=True))
sword _a [] = {-1, 2, 3, 4} ;
a = arma::strans(imat(_a, 2, 2, false)) ;
b = arma::sum(arma:vectorize(a), 0) ;
b = arma::sum(arma::vectorise(a), 0) ;
>>> print(matlab2cpp.qscript("a=[1., 2.; 3., 4.]; b = sum(a(:))", suggest=True))
double _a [] = {1., 2., 3., 4.} ;
a = arma::strans(mat(_a, 2, 2, false)) ;
b = double(arma::as_scalar(arma::sum(arma:vectorize(a(span(0, a.n_rows-1)))))) ;
b = double(arma::as_scalar(arma::sum(arma::vectorise(a)))) ;
>>> print(matlab2cpp.qscript("a=rand(9, 9, 9); b = sum(a(:))", suggest=True))
a = arma::randu<cube>(9, 9, 9) ;
b = double(arma::as_scalar(arma::sum(arma:vectorize(a(span(0, a.n_rows-1)))))) ;
b = double(arma::as_scalar(arma::sum(arma::vectorise(a)))) ;
"""
arg = node[0]
# unknown input
Expand All @@ -1012,7 +1012,7 @@ def Get_sum(node):
return "arma::sum(", ", ", ")"

if arg.dim > 2:
arg = "arma:vectorize(%(0)s)"
arg = "arma::vectorise(%(0)s)"
else:
arg = "%(0)s"

Expand Down
10 changes: 4 additions & 6 deletions src/matlab2cpp/rules/armadillo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def configure_arg(node, index):
>>> print(matlab2cpp.qscript('x=[1,2]; x(:)'))
sword _x [] = {1, 2} ;
x = irowvec(_x, 2, false) ;
x(span(0, x.n_rows-1)) ;
x ;
>>> print(matlab2cpp.qscript('x=[1,2]; x(1)'))
sword _x [] = {1, 2} ;
x = irowvec(_x, 2, false) ;
Expand All @@ -42,7 +42,6 @@ def configure_arg(node, index):
x = irowvec(_x, 2, false) ;
x(arma::trans(x)-1) ;
"""

out = "%(" + str(index) + ")s"

# the full range ':'
Expand Down Expand Up @@ -71,7 +70,7 @@ def configure_arg(node, index):

# undefined type
elif node.type == "TYPE":
return out, -1
return out , -1

# float point scalar
elif node.mem > 1 and node.dim == 0:
Expand Down Expand Up @@ -103,10 +102,9 @@ def configure_arg(node, index):

else:
dim = 1

if len(node) > 0 and node[0].cls == "Paren":
if (len(node) > 0) and (node[0].cls == "Paren"):
pass
elif node.cls not in ["Colon", "Paren"]:
elif node.cls not in ("Colon", "Paren"):
out = out + "-1"

return out, dim
Expand Down
27 changes: 18 additions & 9 deletions src/matlab2cpp/rules/mat.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ def Set(node):

a(n) = b
"""

# wrong number of argumets
if len(node) not in (1,2):
if len(node) not in (1, 2):

if not len(node):
node.error("Zero arguments in a matrix set")
Expand Down Expand Up @@ -195,17 +194,27 @@ def Set(node):

# uvec + scalar
elif dim0 > 0 and dim1 == 0:
index = node[0].str.index('(')
return "%(name)s(" + "m2cpp::span<uvec>" + node[0].str[index:] + \
", m2cpp::span<uvec>(" + arg1 + ", " + arg1 + "))"

if "(" in node[0].str:
index = node[0].str.index('(')
lhs = node[0].str[index:]
return (
"%(name)s(m2cpp::span<uvec>" + lhs +
", m2cpp::span<uvec>(" + arg1 + ", " + arg1 + "))"
)
return "%(name)s(" + arg0 + ", " + arg1 + ")"
#return "%(name)s.row(" + arg0 + ").cols(" + arg1 + ")"

# uvec + uvec
if dim0 > 0 and dim1 > 0:
a0 = node[0].str.replace("arma::span", "m2cpp::span<uvec>")
a1 = node[1].str.replace("arma::span", "m2cpp::span<uvec>")

return "%(name)s(" + a0 + ", " + a1 + ")"
arg0 = node[0].str.replace("arma::span", "m2cpp::span<uvec>")
arg1 = node[1].str.replace("arma::span", "m2cpp::span<uvec>")
if arg0.startswith("_aux_"):
arg0 = arg0 + "-1"
if arg1.startswith("_aux_"):
arg1 = arg1 + "-1"

return "%(name)s(" + arg0 + ", " + arg1 + ")"

return "%(name)s(" + arg0 + ", " + arg1 + ")"

10 changes: 8 additions & 2 deletions src/matlab2cpp/tree/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,14 @@ def variable(self, parent, cur):
print("%-20s" % "variables.variable", end="")
print(repr(self.code[cur:end+1]))

node = collection.Get(parent, name, cur=cur,
code=self.code[cur:end+1])
# 'A(:)' is equivalent to 'A':
if self.code[end-2:end+1] == "(:)":
node = collection.Var(
parent, name, cur=cur, code=self.code[cur:end+1])

else:
node = collection.Get(
parent, name, cur=cur, code=self.code[cur:end+1])

last = self.create_list(node, k)
cur = last
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test simple assignment."""
import pytest

from matlab2cpp import qcpp, qhpp
from matlab2cpp import qcpp, qhpp, qtree


@pytest.fixture(params=[
Expand Down