def qscript(code, suggest=True, ftypes={}, **kws): """ Perform a full translation (like :py:func:`~matlab2cpp.qcpp` and :py:func:`~matlab2cpp.qhpp`), but only focus on the object of interest. If for example code is provided, then only the code part of the translation will be include, without any wrappers. It will be as close to a one-to-one translation as you can get. If a node tree is provided, current node position will be source of translation. Args: code (str, Builder, Node): Representation of the node tree. suggest (bool): Use suggestion engine where appropriate. **kws: Additional arguments passed to :py:obj:`~matlab2cpp.Builder`. Returns: str: A code translation in C++. Example: >>> print mc.qscript("a = 4") a = 4 ; """ if isinstance(code, str): tree_ = build(code, suggest=suggest, retall=True, **kws)[0] else: tree_ = code if isinstance(tree_, tree.builder.Builder): tree_ = tree_[0] if ftypes: tree_.ftypes = ftypes tree_.translate() out = "" if tree_.cls == "Program": if tree_[1] and tree_[1][0].name == "main": out = tree_[1][0][-1].str else: out = tree_[1].str else: out = tree_.str out = out.replace("__percent__", "%") out = strip(out) out = number_fix(out) out = add_indenting(out) return out
def qcpp(code, suggest=True, **kws): """ Quick code translation of matlab script to C++ executable. For Matlab modules, code that only consists of functions, will be placed in the :py:func:`~matlab2cpp.qhpp`. In most cases, the two functions must be used together to create valid runnable code. Args: code (str, Node, Builder): A string or tree representation of Matlab code. suggest (bool): If true, use the suggest engine to guess data types. **kws: Additional arguments passed to :py:obj:`~matlab2cpp.Builder`. Returns: str: Best estimate of script. If code is a module, return an empty string. Example:: >>> code = "a = 4; b = 5.; c = 'abc'" >>> print mc.qcpp(code, suggest=False) #include <armadillo> using namespace arma ; <BLANKLINE> int main(int argc, char** argv) { TYPE a, b, c ; a = 4 ; b = 5. ; c = "abc" ; return 0 ; } >>> print mc.qcpp(code, suggest=True) #include <armadillo> using namespace arma ; <BLANKLINE> int main(int argc, char** argv) { double b ; int a ; std::string c ; a = 4 ; b = 5. ; c = "abc" ; return 0 ; } >>> build = mc.build(code, retall=True) >>> print mc.qcpp(build) == mc.qcpp(code) True See also: :py:func:`~matlab2cpp.qscript`, :py:func:`~matlab2cpp.qhpp`, :py:obj:`~matlab2cpp.Builder` """ if isinstance(code, str): tree_ = build(code, suggest=suggest, retall=True, **kws)[0] else: tree_ = code if isinstance(tree_, tree.builder.Builder): tree_ = tree_[0] tree_ = tree_.program if not tree_.str: tree_.translate() includes, funcs, inlines, structs, headers, log = tree_.program out = "" if funcs and funcs[0].name == "main": if includes.str: out += includes.str + "\n\n" if len(headers) > 1: out += headers.str + "\n\n" if structs.str: out += structs.str + "\n\n" if funcs.str: out += funcs.str + "\n\n" out = out.replace("__percent__", "%") out = out[:-2] out = strip(out) out = number_fix(out) out = add_indenting(out) return out
def qhpp(code, suggest=False): """ Quick module translation of Matlab module to C++ library. If the code is a script, executable part of the code will be placed in :py:func:`~matlab2cpp.qcpp`. Args: code (str, Node, Builder): A string or tree representation of Matlab code. suggest (bool): If true, use the suggest engine to guess data types. **kws: Additional arguments passed to :py:obj:`~matlab2cpp.Builder`. Returns: str: C++ code of module. Example:: >>> code = "function y=f(x); y=x+1; end; function g(); f(4)" >>> print mc.qhpp(code) #ifndef F_M_HPP #define F_M_HPP <BLANKLINE> #include <armadillo> using namespace arma ; <BLANKLINE> TYPE f(TYPE x) ; void g() ; <BLANKLINE> TYPE f(TYPE x) { TYPE y ; y = x+1 ; return y ; } <BLANKLINE> void g() { f(4) ; } #endif >>> print mc.qhpp(code, suggest=True) #ifndef F_M_HPP #define F_M_HPP <BLANKLINE> #include <armadillo> using namespace arma ; <BLANKLINE> int f(int x) ; void g() ; <BLANKLINE> int f(int x) { int y ; y = x+1 ; return y ; } <BLANKLINE> void g() { f(4) ; } #endif See also: :py:func:`~matlab2cpp.qcpp`, :py:class:`~matlab2cpp.Builder` """ if isinstance(code, str): tree_ = build(code, suggest=suggest, retall=True)[0] else: tree_ = code if isinstance(tree_, tree.builder.Builder): tree_ = tree_[0] tree_ = tree_.program if not tree_.str: tree_.translate() includes, funcs, inlines, structs, headers, log = tree_ out = "" if funcs and funcs[0].name == "main": return out if funcs and funcs[0].name != "main": name = funcs[0].name + "_M_HPP" name = name.upper() name.replace(".", "_") out = "#ifndef " + name + "\n#define " + name + "\n\n" if includes.str: out += includes.str + "\n\n" if len(headers) > 1: out += headers.str + "\n\n" if structs.str: out += structs.str + "\n\n" if funcs.str: out += funcs.str + "\n\n" out = out[:-2] if funcs and funcs[0].name != "main": out += "\n#endif" out = out.replace("__percent__", "%") out = strip(out) out = number_fix(out) out = add_indenting(out) return out
def qhpp(code, suggest=False): """ Quick module translation of Matlab module to C++ library. If the code is a script, executable part of the code will be placed in :py:func:`~matlab2cpp.qcpp`. Args: code (str, Node, Builder): A string or tree representation of Matlab code. suggest (bool): If true, use the suggest engine to guess data types. **kws: Additional arguments passed to :py:obj:`~matlab2cpp.Builder`. Returns: str: C++ code of module. Example:: >>> code = "function y=f(x); y=x+1; end; function g(); f(4)" >>> print mc.qhpp(code) #include <armadillo> using namespace arma ; <BLANKLINE> TYPE f(TYPE x) ; void g() ; <BLANKLINE> TYPE f(TYPE x) { TYPE y ; y = x+1 ; return y ; } <BLANKLINE> void g() { f(4) ; } >>> print mc.qhpp(code, suggest=True) #include <armadillo> using namespace arma ; <BLANKLINE> int f(int x) ; void g() ; <BLANKLINE> int f(int x) { int y ; y = x+1 ; return y ; } <BLANKLINE> void g() { f(4) ; } See also: :py:func:`~matlab2cpp.qcpp`, :py:class:`~matlab2cpp.Builder` """ if isinstance(code, str): tree_ = build(code, suggest=suggest, retall=True)[0] else: tree_ = code if isinstance(tree_, tree.builder.Builder): tree_ = tree_[0] tree_ = tree_.program if not tree_.str: tree_.translate() includes, funcs, inlines, structs, headers, log = tree_ out = "" if funcs and funcs[0].name == "main": return out if includes.str: out += includes.str + "\n\n" if len(headers) > 1: out += headers.str + "\n\n" if structs.str: out += structs.str + "\n\n" if funcs.str: out += funcs.str + "\n\n" out = out[:-2] out = out.replace("__percent__", "%") out = strip(out) out = number_fix(out) out = add_indenting(out) return out