コード例 #1
0
def space_list(self, start):
    """
Iterate over a space delimited list

Args:
    self (Builder): Code constructor
    start (int): Current position in code

Returns:
    list : A list of 2-tuples that represents index start and end for each
    expression in list
    """

    if self.code[start] not in c.l_start:
        self.syntaxerror(start, "start of list")

    k = start + 1
    while self.code[k] in " \t":
        k += 1

    out = [[]]
    count = False
    dots = False

    while True:

        if self.code[k:k + 3] == "...":
            k = findend.dots(self, k)
            dots = True

        if self.code[k] in ";\n":
            if not dots:
                out.append([])
                dots = False

            count = False
            if self.code[k] == ";":  # and self.code[k+1] == "\n":
                while self.code[k + 1] in " \n":
                    k += 1

        if self.code[k] in c.e_start:

            if count:
                self.syntaxerror(k, "expression start")

            count = True

            end = findend.expression_space(self, k)
            out[-1].append((k, end))
            k = end

        elif self.code[k] in c.l_end:
            return out

        elif self.code[k] in " \t":
            count = False

        k += 1
コード例 #2
0
ファイル: iterate.py プロジェクト: KeeganRen/matlab2cpp
def space_list(self, start):
    """
Iterate over a space delimited list

Args:
    self (Builder): Code constructor
    start (int): Current position in code

Returns:
    list : A list of 2-tuples that represents index start and end for each
    expression in list
    """

    if  self.code[start] not in c.l_start:
        self.syntaxerror(start, "start of list")

    k = start+1
    while self.code[k] in " \t":
        k += 1

    out = [[]]
    count = False
    dots = False

    while True:

        if self.code[k:k+3] == "...":
            k = findend.dots(self, k)
            dots = True
            
        if self.code[k] in ";\n":
            if not dots:
                out.append([])
                dots = False
                
            count = False
            if self.code[k] == ";": # and self.code[k+1] == "\n":
                while self.code[k+1] in " \n":
                    k += 1
            
        if self.code[k] in c.e_start:

            if count:
                self.syntaxerror(k, "expression start")

            count = True

            end = findend.expression_space(self, k)
            out[-1].append((k, end))
            k = end

        elif self.code[k] in c.l_end:
            return out

        elif self.code[k] in " \t":
            count = False

        k += 1
コード例 #3
0
def comma_list(self, start):
    """
Iterate over a comma separated list

Args:
    self (Builder): Code constructor
    start (int): Current position in code

Returns:
    list : A list of 2-tuples that represents index start and end for each
    expression in list
    """

    if  self.code[start] not in c.l_start:
        self.syntaxerror(start, "list start")
    k = start+1

    while self.code[k] in " \t":
        k += 1

    if self.code[k] in "]}":
        return [[]]

    out = [[]]
    count = False

    while True:

        if self.code[k:k+3] == "...":
            k = findend.dots(self, k)

        elif self.code[k] in c.e_start:

            if count:
                self.syntaxerror(k, "comma list indicator")

            count = True
            end = findend.expression(self, k)
            out[-1].append((k, end))

            k = end

        elif self.code[k] == ",":

            if not count:
                self.syntaxerror(k, "comma list indicator")

            count = False

        elif self.code[k] == ";":

            if not count:
                self.syntaxerror(k, "comma list indicator")

            count = False
            out.append([])

        elif self.code[k] in c.l_end:
            return out

        k += 1
コード例 #4
0
ファイル: expression.py プロジェクト: emc2norway/m2cpp
def create(self, node, start, end=None, start_opr=None):
    """
Create expression in three steps:

    1) In order, split into sub-expressions for each dividing operator
    2) Address prefixes, postfixes, parenthesises, etc.
    3) Identify the remaining singleton

Args:
    self (Builder): Code constructor.
    node (Node): Reference to the parent node
    start (int): current possition in code
    end (int, optional): end of expression. Required for space-delimited expression.
    start_opr (str, optional): At which operator the recursive process is. (For internal use)

Returns:
	int : index to end of the expression

Examples::

    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a*b+c/d")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  'a*b+c/d'
       0     Expression  expression.create    'a*b+c/d'
       0     Expression  expression.create    'a*b'
       0     Expression  expression.create    'a'
       0     Var         variables.variable   'a'
       2     Expression  expression.create    'b'
       2     Var         variables.variable   'b'
       4     Expression  expression.create    'c/d'
       4     Expression  expression.create    'c'
       4     Var         variables.variable   'c'
       6     Expression  expression.create    'd'
       6     Var         variables.variable   'd'
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Statement  code_block   TYPE
    1 1| | Plus       expression   TYPE
    1 1| | | Mul        expression   TYPE
    1 1| | | | Var        unknown      TYPE    a
    1 3| | | | Var        unknown      TYPE    b
    1 5| | | Matrixdivisionexpression   TYPE
    1 5| | | | Var        unknown      TYPE    c
    1 7| | | | Var        unknown      TYPE    d
    """

    if self.code[start:start + 3] == "...":
        start = findend.dots(self, start)
        start += 1
        while self.code[start] in " \t":
            start += 1

    if self.code[start] == ":":

        if self.disp:

            print "%4d     Expression " % (start),
            print "%-20s" % "expression.create",
            print repr(self.code[start:start + 1])

            print "%4d     All        " % (start),
            print "%-20s" % "expression.create",
            print repr(self.code[start:start + 1])

        mc.collection.All(node, cur=start, code=self.code[start])
        return start

    if end is None:
        end = findend.expression(self, start)

    if self.disp:
        print "%4d     Expression " % (start),
        print "%-20s" % "expression.create",
        print repr(self.code[start:end + 1])

    if self.code[start] not in c.e_start:
        self.syntaxerror(start, "expression start")

    operators = [
        "||", "&&", "|", "&", "~=", "==", ">=", ">", "<=", "<", ":", "+", "-",
        ".*", "*", "./", "/", ".\\", "\\", ".^", "^"
    ]

    if not (start_opr is None):
        operators = operators[operators.index(start_opr) + 1:]

    for opr in operators:
        # Pre-screen
        if opr not in self.code[start:end + 1]:
            continue

        starts = [start]
        last = start
        ends = []

        k = start
        while True:

            if self.code[k] == "(":
                k = last = findend.paren(self, k)

            elif self.code[k] == "[":
                k = last = findend.matrix(self, k)

            elif self.code[k] == "{":
                k = last = findend.cell(self, k)

            elif self.code[k] == "'":
                if identify.string(self, k):
                    k = last = findend.string(self, k)
                else:
                    last = k

            elif opr == self.code[k:k + len(opr)]:

                if opr in "+-":
                    # no prefixes and no (scientific) numbers
                    if self.code[last] not in c.letters+c.digits+")]}" or\
                            self.code[k-1] in "dDeE" and self.code[k-2] in\
                            c.digits+"." and self.code[k+1] in c.digits:
                        k += 1
                        continue

                k += len(opr) - 1
                while self.code[k + 1] in " \t":
                    k += 1

                # no all-operator
                if opr == ":" and self.code[k + 1] in ",;\n)]}":
                    k += 1
                    continue

                starts.append(k + 1)
                ends.append(last)

            elif self.code[k] in c.letters + c.digits + "_":
                last = k

            k += 1
            if k >= end:
                ends.append(end)
                break

        if len(ends) > 1:

            node = retrieve_operator(self, opr)(node)
            node.cur = start
            node.code = self.code[starts[0]:ends[-1] + 1]

            for s, e in zip(starts, ends):
                create(self, node, s, e, opr)

            return end

    # All operators removed at this point!

    END = end

    # Prefixes
    while self.code[start] in "-~":

        if self.code[start] == "-":

            node = mc.collection.Neg(node,
                                     cur=start,
                                     code=self.code[start:end + 1])
            start += 1

        if self.code[start] == "~":

            node = mc.collection.Not(node,
                                     cur=start,
                                     code=self.code[start:end + 1])
            start += 1

        while self.code[start] in " \t":
            start += 1

    # Postfixes
    if self.code[end] == "'" and not self.code[start] == "'":
        if self.code[end - 1] == ".":
            node = mc.collection.Transpose(node,
                                           cur=start,
                                           code=self.code[start:end + 1])
            end -= 2
        else:
            node = mc.collection.Ctranspose(node,
                                            cur=start,
                                            code=self.code[start:end + 1])
            node.cur = start
            node.code = self.code[start:end + 1]
            end -= 1

        while self.code[end] in " \t":
            end -= 1

    # Parenthesis
    if self.code[start] == "(":
        if self.code[end] != ")":
            self.syntaxerror(end, "parenthesis end")

        node = mc.collection.Paren(node,
                                   cur=start,
                                   code=self.code[start:end + 1])

        start += 1
        while self.code[start] in " \t":
            start += 1

        end -= 1
        while self.code[end] in " \t":
            end -= 1

        return create(self, node, start, end)

    # Reserved keywords
    elif self.code[start:start +
                   3] == "end" and self.code[start + 3] in " +-:\t" + c.e_end:
        node = mc.collection.End(node,
                                 cur=start,
                                 code=self.code[start:start + 3])

    elif self.code[start:start + 6] == "return" and self.code[start +
                                                              6] in " ,;\n":
        node = mc.collection.Return(node,
                                    cur=start,
                                    code=self.code[start:start + 6])

    elif self.code[start:start + 5] == "break" and self.code[start +
                                                             5] in " ,;\n":
        node = mc.collection.Break(node,
                                   cur=start,
                                   code=self.code[start:start + 5])

    # Rest
    elif self.code[start] == "'":
        if self.code[end] != "'":
            self.syntaxerror(end, "string end")

        if "\n" in self.code[start:end]:
            self.syntaxerror(end, "non line-feed characters in string")

        mc.collection.String(node,
                             self.code[start + 1:end],
                             cur=start,
                             code=self.code[start:end + 1])

    elif self.code[start] in c.digits or\
            self.code[start] == "." and self.code[start+1] in c.digits:
        cur = self.create_number(node, start)

    elif self.code[start] == "[":
        cur = self.create_matrix(node, start)

    elif self.code[start] == "{":
        cur = self.create_cell(node, start)

    else:
        if self.code[start] not in c.letters + "@":
            self.syntaxerror(start, "variable name")

        cur = self.create_variable(node, start)

    return END
コード例 #5
0
ファイル: functions.py プロジェクト: KeeganRen/matlab2cpp
def function(self, parent, cur):
    """
Explicit functions

Args:
    self (Builder): Code constructor
    parent (Node): Parent node
    cur (int): Current position in code

Returns:
	int : Index to end of function

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "function f(); end")
    loading unnamed
         Program     functions.program
       0 Function        functions.function   'function f()'
      12 Codeblock   codeblock.codeblock 
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1  1Funcs      program      TYPE    unnamed
    1  1| Func       func_returns TYPE    f
    1  1| | Declares   func_returns TYPE
    1  1| | Returns    func_returns TYPE
    1 11| | Params     func_returns TYPE
    1 13| | Block      code_block   TYPE
    """

    if self.code[cur:cur+8] != "function":
        self.syntaxerror(cur, "function start")
    if self.code[cur+8] not in c.k_end+"[":
        self.syntaxerror(cur, "function name or return values")

    START = cur
    k = cur + 8

    while self.code[k] in " \t":
        k += 1

    if  self.code[k] not in c.letters+"[":
        self.syntaxerror(k, "function name or return values")
    start = k

    k = findend.expression(self, k)
    end = k

    k += 1
    while self.code[k] in " \t":
        k += 1

    # with return values
    if self.code[k] == "=":

        k += 1
        while self.code[k] in " \t.":
            if self.code[k:k+3] == "...":
                k = findend.dots(self, k)+1
            else:
                k += 1

        l = k
        if  self.code[l] not in c.letters:
            self.syntaxerror(l, "function name")

        while self.code[l+1] in c.letters+c.digits+"_":
            l += 1

        m = l+1

        while self.code[m] in " \t":
            m += 1

        if self.code[m] == "(":
            m = findend.paren(self, m)
        else:
            m = l

        if self.disp:
            print "%4d Function       " % cur,
            print "%-20s" % "functions.function",
            print repr(self.code[START:m+1])

        name = self.code[k:l+1]
        func = mc.collection.Func(parent, name, cur=cur)
        mc.collection.Declares(func, code="")
        returns = mc.collection.Returns(func, code=self.code[start:end+1])

        # multi-return
        if self.code[start] == "[":
            if identify.space_delimited(self, start):
                L = iterate.space_list(self, start)
            else:
                L = iterate.comma_list(self, start)
            end = START
            for array in L:
                for s,e in array:
                    end = s

                    if self.disp:
                        print "%4d   Return       " % cur,
                        print "%-20s" % "functions.function",
                        print repr(self.code[s:e+1])

                    if not any([a in c.letters+c.digits+"_@" \
                            for a in self.code[s:e+1]]):
                        self.syntaxerror(s, "return value")

                    mc.collection.Var(returns, self.code[s:e+1], cur=s,
                            code=self.code[s:e+1])

        # single return
        else:
            end = findend.expression(self, start)

            if self.disp:
                print "%4d   Return       " % cur,
                print repr(self.code[start:end+1])

            mc.collection.Var(returns, self.code[start:end+1], cur=start,
                    code=self.code[start:end+1])


        cur = l+1
        while self.code[cur] in " \t":
            cur += 1

    # No returns
    else:
        m = k
        if self.code[m] == "(":
            m = findend.paren(self, m)
        else:
            m = end


        if self.disp:
            print "%4d Function       " % cur,
            print "%-20s" % "functions.function",
            print repr(self.code[START:m+1])

        end = start+1
        while self.code[end] in c.letters+"_":
            end += 1

        name = self.code[start:end]
        func = mc.collection.Func(parent, name, cur=cur)

        mc.collection.Declares(func)
        returns = mc.collection.Returns(func)

        cur = end

    # Parameters
    params = mc.collection.Params(func, cur=cur)
    if self.code[cur] == "(":

        end = findend.paren(self, cur)
        params.code = self.code[cur+1:end]

        L = iterate.comma_list(self, cur)
        for array in L:
            for s,e in array:

                if self.disp:
                    print "%4d   Param        " % cur,
                    print "%-20s" % "functions.function",
                    print repr(self.code[s:e+1])

                var = mc.collection.Var(params, self.code[s:e+1], cur=s,
                        code=self.code[s:e+1])

        cur = end

    cur += 1

    cur = self.create_codeblock(func, cur)

    # Postfix
    for var in returns:
        var.create_declare()

    end = cur
    func.code = self.code[START:end+1]

    mc.collection.Header(func.program[4], func.name)

    return cur
コード例 #6
0
ファイル: assign.py プロジェクト: yanweifu/matlab2cpp
def multi(self, parent, cur, eq_loc):
    """
Assignment with multiple return

Args:
    self (Builder): Code constructor.
    parent (Node): Parent node
    cur (int): Current position in code
    eq_loc (int): position of the assignment marker ('='-sign)

Returns:
	int: Index to end of assignment

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "[a,b] = c")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Assigns     assign.multi         '[a,b] = c'
       1     Var         variables.assign     'a'
       3     Var         variables.assign     'b'
       8     Expression  expression.create    'c'
       8     Var         variables.variable   'c'
    >>> builder.configure()
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
     1  1Block      code_block   TYPE
     1  1| Assigns    unknown      TYPE    c
     1  2| | Var        unknown      TYPE    a
     1  4| | Var        unknown      TYPE    b
     1  9| | Var        unknown      TYPE    c
    """

    if self.code[cur] != "[":
        self.syntaxerror(cur, "multi-assign start")
    if self.code[eq_loc] != "=":
        self.syntaxerror(cur, "assignment sign (=)")

    j = eq_loc + 1
    while self.code[j] in " \t.":
        if self.code[j] == ".":
            j = findend.dots(self, j) + 1
        else:
            j += 1
    end = findend.expression(self, j)

    if self.disp:
        print "%4d   Assigns    " %\
                cur,
        print "%-20s" % "assign.multi",
        print repr(self.code[cur:end + 1])

    if identify.space_delimited(self, cur):
        l = iterate.space_list(self, cur)
    else:
        l = iterate.comma_list(self, cur)

    if len(l[0]) == 1:
        return self.create_assign(parent, l[0][0][0], eq_loc)

    assigns = mc.collection.Assigns(parent,
                                    cur=cur,
                                    code=self.code[cur:end + 1])

    for vector in l:
        for start, stop in vector:
            self.create_assign_variable(assigns, start, end=stop)

    cur = eq_loc + 1
    while self.code[cur] in " \t":
        cur += 1

    cur_ = self.create_expression(assigns, cur)

    assigns.name = assigns[-1].name

    return cur_
コード例 #7
0
ファイル: identify.py プロジェクト: uxvworks/matlab2cpp
def space_delimited(self, start):
    """
Check if list is space-delimited

Args:
    self (Builder): Code constructor
    start (int): Current position in code

Returns:
	bool: True if list consists of whitespace delimiters
    """

    if self.code[start] not in c.l_start:
        self.syntaxerror(start, "list start")

    k = start + 1
    while self.code[k] in " \t":
        k += 1

    if self.code[k] in c.l_end:
        return False

    if self.code[k] not in c.e_start:
        self.syntaxerror(k, "expression start")

    if self.code[k] == "'":
        k = findend.string(self, k) + 1

        while self.code[k] in " \t":
            k += 1

    while True:

        if self.code[k] == "(":
            k = findend.paren(self, k)

        elif self.code[k] == "[":
            k = findend.matrix(self, k)

        elif self.code[k] == "{":
            k = findend.cell(self, k)

        elif self.code[k] == "'":
            # if self.code[k-1] in c.s_start:
            return True

        elif self.code[k : k + 3] == "...":
            k = findend.dots(self, k)

        elif self.code[k] in " \t":
            if space_delimiter(self, k):
                return True
            while self.code[k + 1] in " \t":
                k += 1

        elif self.code[k] in c.e_end:
            if self.code[k] == ",":
                return False
            elif self.code[k] in c.l_end:
                return False
            elif self.code[k] != ";":
                return True

            while self.code[k + 1] in " \t":
                k += 1

        elif self.code[k + 1] in c.letters + c.digits + "_@":
            while self.code[k + 1] in c.letters + c.digits + "_@":
                k += 1
        k += 1
コード例 #8
0
ファイル: expression.py プロジェクト: bambang/matlab2cpp
def create(self, node, start, end=None, start_opr=None):
    """
Create expression in three steps:

    1) In order, split into sub-expressions for each dividing operator
    2) Address prefixes, postfixes, parenthesises, etc.
    3) Identify the remaining singleton

Args:
    self (Builder): Code constructor.
    node (Node): Reference to the parent node
    start (int): current possition in code
    end (int, optional): end of expression. Required for space-delimited expression.
    start_opr (str, optional): At which operator the recursive process is. (For internal use)

Returns:
	int : index to end of the expression

Examples::

    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a*b+c/d")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  'a*b+c/d'
       0     Expression  expression.create    'a*b+c/d'
       0     Expression  expression.create    'a*b'
       0     Expression  expression.create    'a'
       0     Var         variables.variable   'a'
       2     Expression  expression.create    'b'
       2     Var         variables.variable   'b'
       4     Expression  expression.create    'c/d'
       4     Expression  expression.create    'c'
       4     Var         variables.variable   'c'
       6     Expression  expression.create    'd'
       6     Var         variables.variable   'd'
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Statement  code_block   TYPE
    1 1| | Plus       expression   TYPE
    1 1| | | Mul        expression   TYPE
    1 1| | | | Var        unknown      TYPE    a
    1 3| | | | Var        unknown      TYPE    b
    1 5| | | Matrixdivisionexpression   TYPE
    1 5| | | | Var        unknown      TYPE    c
    1 7| | | | Var        unknown      TYPE    d
    """

    if self.code[start:start+3] == "...":
        start = findend.dots(self, start)
        start += 1
        while self.code[start] in " \t":
            start += 1

    if self.code[start] == ":":

        if self.disp:

            print "%4d     Expression " % (start),
            print "%-20s" % "expression.create",
            print repr(self.code[start:start+1])

            print "%4d     All        " % (start),
            print "%-20s" % "expression.create",
            print repr(self.code[start:start+1])

        mc.collection.All(node, cur=start, code=self.code[start])
        return start

    if end is None:
        end = findend.expression(self, start)

    if self.disp:
        print "%4d     Expression " % (start),
        print "%-20s" % "expression.create",
        print repr(self.code[start:end+1])

    if  self.code[start] not in c.e_start:
        self.syntaxerror(start, "expression start")


    operators = [
        "||", "&&", "|", "&",
        "~=", "==", ">=", ">", "<=", "<",
        ":", "+", "-",
        ".*", "*", "./", "/", ".\\", "\\",
        ".^", "^"]

    if not (start_opr is None):
        operators = operators[operators.index(start_opr)+1:]

    for opr in operators:
        # Pre-screen
        if opr not in self.code[start:end+1]:
            continue

        starts = [start]
        last = start
        ends = []

        k = start
        while True:

            if self.code[k] == "(":
                k = last = findend.paren(self, k)

            elif self.code[k] == "[":
                k = last = findend.matrix(self, k)

            elif self.code[k] == "{":
                k = last = findend.cell(self, k)

            elif self.code[k] == "'":
                if identify.string(self, k):
                    k = last = findend.string(self, k)
                else:
                    last = k

            elif opr == self.code[k:k+len(opr)]:

                if opr in "+-":
                    # no prefixes and no (scientific) numbers
                    if self.code[last] not in c.letters+c.digits+")]}" or\
                            self.code[k-1] in "dDeE" and self.code[k-2] in\
                            c.digits+"." and self.code[k+1] in c.digits:
                        k += 1
                        continue


                k += len(opr)-1
                while self.code[k+1] in " \t":
                    k += 1

                # no all-operator
                if opr == ":" and self.code[k+1] in ",;\n)]}":
                    k += 1
                    continue

                starts.append(k+1)
                ends.append(last)

            elif self.code[k] in c.letters+c.digits+"_":
                last = k

            k += 1
            if k >= end:
                ends.append(end)
                break

        if len(ends)>1:

            node = retrieve_operator(self, opr)(node)
            node.cur = start
            node.code = self.code[starts[0]:ends[-1]+1]

            for s,e in zip(starts, ends):
                create(self, node, s, e, opr)

            return end


    # All operators removed at this point!

    END = end

    # Prefixes
    while self.code[start] in "-~":

        if self.code[start] == "-":

            node = mc.collection.Neg(node, cur=start, code=self.code[start:end+1])
            start += 1

        if self.code[start] == "~":

            node = mc.collection.Not(node, cur=start, code=self.code[start:end+1])
            start += 1

        while self.code[start] in " \t":
            start += 1

    # Postfixes
    if self.code[end] == "'" and not self.code[start] == "'":
        if self.code[end-1] == ".":
            node = mc.collection.Transpose(node, cur=start,
                    code=self.code[start:end+1])
            end -= 2
        else:
            node = mc.collection.Ctranspose(node, cur=start,
                    code=self.code[start:end+1])
            node.cur = start
            node.code = self.code[start:end+1]
            end -= 1

        while self.code[end] in " \t":
            end -= 1

    # Parenthesis
    if self.code[start] == "(":
        if self.code[end] != ")":
            self.syntaxerror(end, "parenthesis end")

        node = mc.collection.Paren(node, cur=start, code=self.code[start:end+1])

        start += 1
        while self.code[start] in " \t":
            start += 1

        end -= 1
        while self.code[end] in " \t":
            end -= 1

        return create(self, node, start, end)

    # Reserved keywords
    elif self.code[start:start+3] == "end": # and self.code[start+3] in " \t" + c.e_end:
        node = mc.collection.End(node, cur=start, code=self.code[start:start+3])

    elif self.code[start:start+6] == "return" and self.code[start+6] in " ,;\n":
        node = mc.collection.Return(node, cur=start, code=self.code[start:start+6])

    elif self.code[start:start+5] == "break" and self.code[start+5] in " ,;\n":
        node = mc.collection.Break(node, cur=start, code=self.code[start:start+5])


    # Rest
    elif self.code[start] == "'":
        if self.code[end] != "'":
            self.syntaxerror(end, "string end")

        if "\n" in self.code[start:end]:
            self.syntaxerror(end, "non line-feed characters in string")

        mc.collection.String(node, self.code[start+1:end], cur=start,
                code=self.code[start:end+1])

    elif self.code[start] in c.digits or\
            self.code[start] == "." and self.code[start+1] in c.digits:
        cur = self.create_number(node, start)

    elif self.code[start] == "[":
        cur = self.create_matrix(node, start)

    elif self.code[start] == "{":
        cur = self.create_cell(node, start)

    else:
        if self.code[start] not in c.letters+"@":
            self.syntaxerror(start, "variable name")

        cur = self.create_variable(node, start)

    return END
コード例 #9
0
def function(self, parent, cur):
    """
Explicit functions

Args:
    self (Builder): Code constructor
    parent (Node): Parent node
    cur (int): Current position in code

Returns:
	int : Index to end of function

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "function f(); end")
    loading unnamed
         Program     functions.program
       0 Function        functions.function   'function f()'
      12 Codeblock   codeblock.codeblock 
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1  1Funcs      program      TYPE    unnamed
    1  1| Func       func_returns TYPE    f
    1  1| | Declares   func_returns TYPE
    1  1| | Returns    func_returns TYPE
    1 11| | Params     func_returns TYPE
    1 13| | Block      code_block   TYPE
    """

    if self.code[cur:cur + 8] != "function":
        self.syntaxerror(cur, "function start")
    if self.code[cur + 8] not in c.k_end + "[":
        self.syntaxerror(cur, "function name or return values")

    START = cur
    k = cur + 8

    while self.code[k] in " \t":
        k += 1

    if self.code[k] not in c.letters + "[":
        self.syntaxerror(k, "function name or return values")
    start = k

    k = findend.expression(self, k)
    end = k

    k += 1
    while self.code[k] in " \t":
        k += 1

    # with return values
    if self.code[k] == "=":

        k += 1
        while self.code[k] in " \t.":
            if self.code[k:k + 3] == "...":
                k = findend.dots(self, k) + 1
            else:
                k += 1

        l = k
        if self.code[l] not in c.letters:
            self.syntaxerror(l, "function name")

        while self.code[l + 1] in c.letters + c.digits + "_":
            l += 1

        m = l + 1

        while self.code[m] in " \t":
            m += 1

        if self.code[m] == "(":
            m = findend.paren(self, m)
        else:
            m = l

        if self.disp:
            print "%4d Function       " % cur,
            print "%-20s" % "functions.function",
            print repr(self.code[START:m + 1])

        name = self.code[k:l + 1]
        func = mc.collection.Func(parent, name, cur=cur)
        mc.collection.Declares(func, code="")
        returns = mc.collection.Returns(func, code=self.code[start:end + 1])

        # multi-return
        if self.code[start] == "[":
            if identify.space_delimited(self, start):
                L = iterate.space_list(self, start)
            else:
                L = iterate.comma_list(self, start)
            end = START
            for array in L:
                for s, e in array:
                    end = s

                    if self.disp:
                        print "%4d   Return       " % cur,
                        print "%-20s" % "functions.function",
                        print repr(self.code[s:e + 1])

                    if not any([a in c.letters+c.digits+"_@" \
                            for a in self.code[s:e+1]]):
                        self.syntaxerror(s, "return value")

                    mc.collection.Var(returns,
                                      self.code[s:e + 1],
                                      cur=s,
                                      code=self.code[s:e + 1])

        # single return
        else:
            end = findend.expression(self, start)

            if self.disp:
                print "%4d   Return       " % cur,
                print repr(self.code[start:end + 1])

            mc.collection.Var(returns,
                              self.code[start:end + 1],
                              cur=start,
                              code=self.code[start:end + 1])

        cur = l + 1
        while self.code[cur] in " \t":
            cur += 1

    # No returns
    else:
        m = k
        if self.code[m] == "(":
            m = findend.paren(self, m)
        else:
            m = end

        if self.disp:
            print "%4d Function       " % cur,
            print "%-20s" % "functions.function",
            print repr(self.code[START:m + 1])

        end = start + 1
        while self.code[end] in c.letters + "_":
            end += 1

        name = self.code[start:end]
        func = mc.collection.Func(parent, name, cur=cur)

        mc.collection.Declares(func)
        returns = mc.collection.Returns(func)

        cur = end

    # Parameters
    params = mc.collection.Params(func, cur=cur)
    if self.code[cur] == "(":

        end = findend.paren(self, cur)
        params.code = self.code[cur + 1:end]

        L = iterate.comma_list(self, cur)
        for array in L:
            for s, e in array:

                if self.disp:
                    print "%4d   Param        " % cur,
                    print "%-20s" % "functions.function",
                    print repr(self.code[s:e + 1])

                var = mc.collection.Var(params,
                                        self.code[s:e + 1],
                                        cur=s,
                                        code=self.code[s:e + 1])

        cur = end

    cur += 1

    cur = self.create_codeblock(func, cur)

    # Postfix
    for var in returns:
        var.create_declare()

    end = cur
    func.code = self.code[START:end + 1]

    mc.collection.Header(func.program[4], func.name)

    return cur
コード例 #10
0
def space_delimited(self, start):
    """
Check if list is space-delimited

Args:
    self (Builder): Code constructor
    start (int): Current position in code

Returns:
	bool: True if list consists of whitespace delimiters
    """

    if self.code[start] not in c.l_start:
        self.syntaxerror(start, "list start")

    k = start + 1
    while self.code[k] in " \t":
        k += 1

    if self.code[k] in c.l_end:
        return False

    if self.code[k] not in c.e_start:
        self.syntaxerror(k, "expression start")

    if self.code[k] == "'":
        k = findend.string(self, k) + 1

        while self.code[k] in " \t":
            k += 1

    while True:

        if self.code[k] == "(":
            k = findend.paren(self, k)

        elif self.code[k] == "[":
            k = findend.matrix(self, k)

        elif self.code[k] == "{":
            k = findend.cell(self, k)

        elif self.code[k] == "'" and string(self, k):
            #k = findend.string(self, k)
            #if self.code[k-1] in c.s_start:
            return True

        elif self.code[k:k + 3] == "...":
            k = findend.dots(self, k)

        elif self.code[k] in " \t":
            if space_delimiter(self, k):
                return True
            while self.code[k + 1] in " \t":
                k += 1

        elif self.code[k] in c.e_end:
            if self.code[k] == ",":
                return False
            elif self.code[k] in c.l_end:
                return False
            elif self.code[k] != ";":
                return True

            while self.code[k + 1] in " \t":
                k += 1

        elif self.code[k + 1] in c.letters + c.digits + "_@":
            while self.code[k + 1] in c.letters + c.digits + "_@":
                k += 1
        k += 1
コード例 #11
0
ファイル: assign.py プロジェクト: uxvworks/matlab2cpp
def multi(self, parent, cur, eq_loc):
    """
Assignment with multiple return

Args:
    self (Builder): Code constructor.
    parent (Node): Parent node
    cur (int): Current position in code
    eq_loc (int): position of the assignment marker ('='-sign)

Returns:
	int: Index to end of assignment

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "[a,b] = c")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Assigns     assign.multi         '[a,b] = c'
       1     Var         variables.assign     'a'
       3     Var         variables.assign     'b'
       8     Expression  expression.create    'c'
       8     Var         variables.variable   'c'
    >>> builder.configure()
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
     1  1Block      code_block   TYPE
     1  1| Assigns    unknown      TYPE    c
     1  2| | Var        unknown      TYPE    a
     1  4| | Var        unknown      TYPE    b
     1  9| | Var        unknown      TYPE    c
    """

    if self.code[cur] != "[":
        self.syntaxerror(cur, "multi-assign start")
    if self.code[eq_loc] != "=":
        self.syntaxerror(cur, "assignment sign (=)")

    j = eq_loc + 1
    while self.code[j] in " \t.":
        if self.code[j] == ".":
            j = findend.dots(self, j) + 1
        else:
            j += 1
    end = findend.expression(self, j)

    if self.disp:
        print "%4d   Assigns    " % cur,
        print "%-20s" % "assign.multi",
        print repr(self.code[cur : end + 1])

    if identify.space_delimited(self, cur):
        l = iterate.space_list(self, cur)
    else:
        l = iterate.comma_list(self, cur)

    if len(l[0]) == 1:
        return self.create_assign(parent, l[0][0][0], eq_loc)

    assigns = mc.collection.Assigns(parent, cur=cur, code=self.code[cur : end + 1])

    for vector in l:
        for start, stop in vector:
            self.create_assign_variable(assigns, start, end=stop)

    cur = eq_loc + 1
    while self.code[cur] in " \t":
        cur += 1

    cur_ = self.create_expression(assigns, cur)

    assigns.name = assigns[-1].name

    return cur_