def Params(node): """Parameters in function definition with zero or multiple returns Adds type prefix. Contains: Var* Examples: >>> ftypes = {"f": {"a":"int", "b":"double"}} >>> print mc.qscript("function f(a,b)", ftypes=ftypes) void f(int a, double b) { // Empty block } """ # Create list of parameters out = "" # if -ref, -reference flag option if node.project.builder.reference: out += ", ".join([ "const " + type_string(child) + "& " + child.name if child.dim > 0 else type_string(child) + " " + child.name for child in node ]) return out else: out = ", ".join( [type_string(child) + " " + child.name for child in node]) return out
def Params(node): """Parameters in functions with one return Adds type prefix. Contains: Var* Examples: >>> code = "function y=f(a,b,c,d,e); y=1" >>> builder = mc.Builder() >>> builder.load("unamed", code) >>> builder[0].ftypes = {"f":{"a": "int", "b":"double", "c":"cx_mat", ... "d":"func_lambda", "e":"struct", "y":"int"}} >>> print mc.qscript(builder) int f(int a, double b, cx_mat c, std::function d, _E e) { int y ; y = 1 ; return y ; } """ out = "" for child in node: out += ", " + type_string(child) + " " + str(child) return out[2:]
def Func(node): """Function declaration Contains: Declares Returns Params Block Property: name (of function) Examples: >>> print mc.qscript("function y=f(); y=1") int f() { int y ; y = 1 ; return y ; } """ # type information is in params and declare, not return retname = node[1][0].name if retname in node[0].names: retval = node[0][node[0].names.index(retname)] if retname in node[2].names: retval = node[2][node[1].names.index(retname)] rettype = type_string(retval) # function ends with a return statement if node[-1][-1] and node[-1][-1][-1].cls == "Return": return rettype + """ %(name)s(%(2)s) { %(0)s %(3)s }""" return rettype + """ %(name)s(%(2)s)
def Params(node): """Parameter structure in lambda functions Contains: Var* Examples: >>> print mc.qscript("f = @(x,y,z) x+y+z; f(1,2.,'3')") f = [] (int x, double y, std::string z) {x+y+z ; } ; f(1, 2., "3") ; """ return ", ".join(["%s %s" % (type_string(n), n.name) for n in node])
def Params(node): """Parameters in functions with one return Adds type prefix. Contains: Var* Examples: >>> code = "function y=f(a,b,c,d,e); y=1" >>> builder = mc.Builder() >>> builder.load("unamed", code) >>> builder[0].ftypes = {"f":{"a": "int", "b":"double", "c":"cx_mat", ... "d":"func_lambda", "e":"struct", "y":"int"}} >>> print mc.qscript(builder) int f(int a, double b, cx_mat c, std::function d, _E e) { int y ; y = 1 ; return y ; } """ out = "" # if -ref, -reference flag option if node.project.builder.reference: out += ", ".join([ "const " + type_string(child) + "& " + child.name if child.dim > 0 else type_string(child) + " " + child.name for child in node ]) else: out = ", ".join( [type_string(child) + " " + child.name for child in node]) return out
def Returns(node): """Return value in function definition with zero or multiple returns Adds type prefix and '&' (since they are to be placed in parameters) Contains: Return* Examples: >>> print mc.qscript("function [a,b]=f(); a=1, b=2.") void f(int& a, double& b) { a = 1 ; b = 2. ; } """ out = "" for child in node[:]: out += ", " + type_string(child) + "& " + str(child) return out[2:]
def Params(node): """Parameters in function definition with zero or multiple returns Adds type prefix. Contains: Var* Examples: >>> ftypes = {"f": {"a":"int", "b":"double"}} >>> print mc.qscript("function f(a,b)", ftypes=ftypes) void f(int a, double b) { // Empty block } """ # Create list of parameters out = "" for child in node[:]: out += ", " + type_string(child) + " " + str(child) return out[2:]
def Declares(node): """Declarations in the beginning of function Contains: Var* Examples: >>> print mc.qscript("function d=f(); a=1; b.c='2'; d.e(1)=[4,5]") _D f() { _B b ; _D d ; int a ; a = 1 ; b.c = "2" ; sword _d [] = {4, 5} ; d.e[0] = irowvec(_d, 2, false) ; return d ; } """ if not node: return "" returns = node.parent[1] declares = {} # {"int" : ["a", "b"]} -> int a, b ; structs = {} # {"_A" : "a"} -> _A a; # fill declares and structs for child in node[:]: type = type_string(child) if type not in declares: declares[type] = [] declares[type].append(child) if child.type == "structs": structs[child.name] = child # create output out = "" keys = declares.keys() keys.sort() for key in keys: val = declares[key] val.sort(cmp=lambda x, y: cmp(x.name, y.name)) # datatype out += "\n" + key + " " # all variables with that type for v in val: out += str(v) if v.name in structs: structs_ = node.program[3] struct = structs_[structs_.names.index(v.name)] size = struct[struct.names.index("_size")].value out += "[%s]" % size out += ", " out = out[:-2] + " ;" return out[1:]
def Declares(node): """Declarations in the beginning of function Contains: Var* Examples: >>> print mc.qscript("function d=f(); a=1; b.c='2'; d.e(1)=[4,5]") _D f() { _B b ; _D d ; int a ; a = 1 ; b.c = "2" ; sword _d [] = {4, 5} ; d.e[0] = irowvec(_d, 2, false) ; return d ; } """ if not node: return "" returns = node.parent[1] declares = {} # {"int" : ["a", "b"]} -> int a, b ; structs = {} # {"_A" : "a"} -> _A a; # fill declares and structs for child in node[:]: type = type_string(child) if type not in declares: declares[type] = [] declares[type].append(child) if child.type == "structs": structs[child.name] = child # create output out = "" keys = declares.keys() keys.sort() for key in keys: val = declares[key] val.sort(cmp=lambda x,y: cmp(x.name, y.name)) # datatype out += "\n" + key + " " # all variables with that type for v in val: out += str(v) if v.name in structs: structs_ = node.program[3] struct = structs_[structs_.names.index(v.name)] size = struct[struct.names.index("_size")].value out += "[%s]" % size out += ", " out = out[:-2] + " ;" return out[1:]
def Header(node): func = node.program[1][node.program[1].names.index(node.name)] if func.backend == "func_return": # if -ref, -reference flag option if node.project.builder.reference: code = func[1][0].type + " " + func.name + "(" +\ ", ".join(["const " + type_string(p) + "& " + p.name if p.dim > 0 else type_string(p) + " " + p.name for p in func[2]]) + ") ;" else: code = func[1][0].type + " " + func.name + "(" +\ ", ".join([type_string(p) + " " + p.name for p in func[2]]) + ") ;" elif func.backend == "func_returns" and not func[1]: # if -ref, -reference flag option if node.project.builder.reference: code = "void " + func.name + "(" +\ ", ".join(["const " + type_string(p) + "& " + p.name if p.dim > 0 else type_string(p) + " " + p.name for p in func[2]]) + ") ;" else: code = "void " + func.name + "(" +\ ", ".join([type_string(p) + " " + p.name for p in func[2]]) + ") ;" elif func.backend == "func_returns" and func[1]: # if -ref, -reference flag option if node.project.builder.reference: code = "void " + func.name + "(" +\ ", ".join(["const " + type_string(p) + "& " + p.name if p.dim > 0 else type_string(p) + " " + p.name for p in func[2]]) + ", " +\ ", ".join([type_string(p) + "& " + p.name for p in func[1]]) + ") ;" else: code = "void " + func.name + "(" +\ ", ".join([type_string(p) + " " + p.name for p in func[2]]) + ", " +\ ", ".join([type_string(p) + "& " + p.name for p in func[1]]) + ") ;" return code