Exemple #1
0
def cell(self, node, cur):
    """
Verbatim cells

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

Returns:
	int : End of cell

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "{1, 2}")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  '{1, 2}'
       0     Expression  expression.create    '{1, 2}'
       0     Cell        misc.cell            '{1, 2}'
       1     Expression  expression.create    '1'
       1     Int         misc.number          '1'
       4     Expression  expression.create    '2'
       4     Int         misc.number          '2'
    >>> 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| | Cell       cell         TYPE
    1 2| | | Int        int          int
    1 5| | | Int        int          int
    """

    if self.code[cur] != "{":
        self.syntaxerror(cur, "curly braces")

    end = findend.cell(self, cur)
    if self.disp:
        print "%4d     Cell       " % cur,
        print "%-20s" % "misc.cell",
        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)
    cell = mc.collection.Cell(node, cur=cur, code=self.code[cur : end + 1])

    for array in L:

        if array:
            start = array[0][0]
            end = array[-1][-1]
        else:
            start = cur

        for start, end in array:

            self.create_expression(cell, start, end)

    return findend.cell(self, cur)
Exemple #2
0
def variable(self, parent, cur):
    """
Variable not on the left side of an assignment

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

Kwargs:
    end (int, optional): End of variable

Returns:
	int : End of variable

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  'a'
       0     Expression  expression.create    'a'
       0     Var         variables.variable   'a'
    >>> builder.configure()
    >>> print mc.qtree(builder, core=True) #doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Statement  code_block   TYPE
    1 1| | Var        unknown      TYPE    a
    """

    k = cur
    if self.code[k] == "@":
        k += 1

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

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

    name = self.code[cur:k]
    last = k

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

    # Get value of cell
    if self.code[k] == "{":

        end = findend.cell(self, k)
        end = end + 1
        while self.code[end] in " \t":
            end += 1

        if self.code[end] == "(":

            end = findend.paren(self, end)
            node = mc.collection.Cget(parent,
                                      name,
                                      cur=cur,
                                      code=self.code[cur:end + 1])

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

            n_fields = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur + 1
                while self.code[k] in " \t":
                    k += 1
                n_fields += 1

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

            if self.code[k] != "(":
                self.syntaxerror(k, "argument parenthesis")

            cur = self.create_list(node, k)

            node["n_fields"] = n_fields
            node["n_args"] = len(node) - n_fields

        else:
            end = findend.cell(self, k)
            node = mc.collection.Cvar(parent,
                                      name,
                                      cur=cur,
                                      code=self.code[cur:end + 1])

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

            num = 0
            while self.code[k] == "{":

                cur = cell_arg(self, node, k)
                k = cur + 1
                while self.code[k] in " \t":
                    k += 1
                num += 1

    # Get value of array
    elif self.code[k] == "(":

        end = findend.paren(self, k)
        if self.code[end + 1] == "." and self.code[end + 2] in c.letters:

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

            if self.disp:
                print "%4d     Sget        " %\
                        cur,
                print "%-20s" % "variables.variable",
                print repr(self.code[cur:end])

            node = mc.collection.Sget(parent,
                                      name,
                                      value,
                                      cur=cur,
                                      code=self.code[cur:end],
                                      pointer=1)

            last = self.create_list(node, k)
            cur = end

        else:

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

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

            last = self.create_list(node, k)
            cur = last

    elif self.code[k] == "." and self.code[k + 1] not in "*/\\^'":

        k += 1

        # Fieldname of type "a.(..)"
        if self.code[k] == "(":

            end = findend.paren(self, k)

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

            k += 1

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

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

            cur = self.create_expression(node, k)

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

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

            value = self.code[k:j]
            last = j

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

            # Fieldname of type "a.b(...)"
            if self.code[j] == "(":

                end = findend.paren(self, j)
                if self.disp:
                    print "%4d     Fget       " % cur,
                    print "%-20s" % "variables.variable",
                    print repr(self.code[cur:end + 1])

                node = mc.collection.Fget(parent,
                                          name,
                                          cur=cur,
                                          value=value,
                                          code=self.code[cur:end + 1])

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

                cur = self.create_expression(node, j)

                node.create_declare()

            # Fieldname of type "a.b"
            else:

                if self.disp:
                    print "%4d     Fvar       " % cur,
                    print "%-20s" % "variables.variable",
                    print repr(self.code[cur:last])

                node = mc.collection.Fvar(parent,
                                          name,
                                          value=value,
                                          cur=cur,
                                          code=self.code[cur:last])

                cur = last - 1

                node.create_declare()

    # Simple variable
    else:

        if self.disp:
            print "%4d     Var        " % cur,
            print "%-20s" % "variables.variable",
            print repr(self.code[cur:last])

        node = mc.collection.Var(parent,
                                 name,
                                 cur=cur,
                                 code=self.code[cur:last])

        cur = last - 1

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

    return cur
Exemple #3
0
def assign(self, node, cur, end=None):
    """
Variable left side of an assignment

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

Kwargs:
    end (int, optional): End of variable

Returns:
	int : End of variable

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a = 4")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Assign      assign.single        'a = 4'
       0     Var         variables.assign     'a'
       4     Expression  expression.create    '4'
       4     Int         misc.number          '4'
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Assign     int          int 
    1 1| | Var        unknown      (int)   a
    1 5| | Int        int          int
    """

    if self.code[cur] not in c.letters:
        self.syntaxerror(cur, "assign variable name")

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

    name = self.code[cur:k]
    last = k

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

    # Get value of cell
    if self.code[k] == "{":

        end = findend.cell(self, k)
        end = end + 1
        while self.code[end] in " \t":
            end += 1

        if self.code[end] == "(":

            end = findend.paren(self, end)
            node = mc.collection.Cset(node,
                                      name,
                                      cur=cur,
                                      code=self.code[cur:end + 1])

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

            n_fields = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur + 1
                while self.code[k] in " \t":
                    k += 1
                n_fields += 1

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

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

            cur = self.create_list(node, k)

            node["n_fields"] = n_fields
            node["n_args"] = len(node) - n_fields

        else:
            end = findend.cell(self, k)
            node = mc.collection.Cvar(node,
                                      name,
                                      cur=cur,
                                      code=self.code[cur:end + 1])

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

            num = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur + 1
                while self.code[k] in " \t":
                    k += 1
                num += 1

    # Set value of array
    elif self.code[k] == "(":

        end = findend.paren(self, k)
        if self.code[end + 1] == "." and self.code[end + 2] in c.letters:

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

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

            node = mc.collection.Sset(node,
                                      name,
                                      value,
                                      cur=cur,
                                      code=self.code[cur:end],
                                      pointer=1)

            last = self.create_list(node, k)
            cur = end - 1

        else:

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

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

            last = self.create_list(node, k)
            cur = last

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

        k += 1

        # Fieldname of type "a.() = ..."
        if self.code[k] == "(":

            end = findend.paren(self, k)

            k += 1

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

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

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

            cur = self.create_expression(node, cur)

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

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

            value = self.code[k:j]
            last = j - 1

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

            # Fieldname of type "a.b(...) = ..."
            if self.code[j] == "(":

                end = findend.paren(self, j)
                if self.disp:
                    print "%4d     Fset       " % cur,
                    print "%-20s" % "variables.assign",
                    print repr(self.code[cur:end + 1])

                node = mc.collection.Fset(node,
                                          name,
                                          value=value,
                                          cur=cur,
                                          code=self.code[cur:end + 1])

                cur = self.create_list(node, j)

            # Fieldname of type "a.b = ..."
            else:

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

                node = mc.collection.Fvar(node,
                                          name,
                                          value=value,
                                          cur=cur,
                                          code=self.code[cur:last + 1])

                cur = last

    # Simple variable assignment
    else:
        if self.disp:
            print "%4d     Var        " % cur,
            print "%-20s" % "variables.assign",
            print repr(self.code[cur:last])

        node = mc.collection.Var(node, name, cur=cur, code=self.code[cur:last])

        cur = last - 1

    node.create_declare()
    return cur
Exemple #4
0
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
Exemple #5
0
def cell(self, node, cur):
    """
Verbatim cells

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

Returns:
	int : End of cell

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "{1, 2}")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  '{1, 2}'
       0     Expression  expression.create    '{1, 2}'
       0     Cell        misc.cell            '{1, 2}'
       1     Expression  expression.create    '1'
       1     Int         misc.number          '1'
       4     Expression  expression.create    '2'
       4     Int         misc.number          '2'
    >>> 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| | Cell       cell         TYPE
    1 2| | | Int        int          int
    1 5| | | Int        int          int
    """

    if self.code[cur] != "{":
        self.syntaxerror(cur, "curly braces")

    end = findend.cell(self, cur)
    if self.disp:
        print "%4d     Cell       " % cur,
        print "%-20s" % "misc.cell",
        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)
    cell = mc.collection.Cell(node, cur=cur, code=self.code[cur:end + 1])

    for array in L:

        if array:
            start = array[0][0]
            end = array[-1][-1]
        else:
            start = cur

        for start, end in array:

            self.create_expression(cell, start, end)

    return findend.cell(self, cur)
Exemple #6
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] == "'":
            # 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
Exemple #7
0
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
Exemple #8
0
def assign(self, node, cur, end=None):
    """
Variable left side of an assignment

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

Kwargs:
    end (int, optional): End of variable

Returns:
	int : End of variable

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a = 4")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Assign      assign.single        'a = 4'
       0     Var         variables.assign     'a'
       4     Expression  expression.create    '4'
       4     Int         misc.number          '4'
    >>> builder.configure(suggest=False)
    >>> print mc.qtree(builder, core=True) # doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Assign     int          int 
    1 1| | Var        unknown      (int)   a
    1 5| | Int        int          int
    """

    if  self.code[cur] not in c.letters:
        self.syntaxerror(cur, "assign variable name")

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

    name = self.code[cur:k]
    last = k

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

    # Get value of cell
    if self.code[k] == "{":

        end = findend.cell(self, k)
        end = end+1
        while self.code[end] in " \t":
            end += 1

        if self.code[end] == "(":

            end = findend.paren(self, end)
            node = mc.collection.Cset(node, name, cur=cur,
                    code=self.code[cur:end+1])

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

            n_fields = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur+1
                while self.code[k] in " \t":
                    k += 1
                n_fields += 1

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

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

            cur = self.create_list(node, k)

            node["n_fields"] = n_fields
            node["n_args"] = len(node) - n_fields

        else:
            end = findend.cell(self, k)
            node = mc.collection.Cvar(node, name, cur=cur,
                    code=self.code[cur:end+1])

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

            num = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur+1
                while self.code[k] in " \t":
                    k += 1
                num += 1


    # Set value of array
    elif self.code[k] == "(":

        end = findend.paren(self, k)
        if self.code[end+1] == "." and self.code[end+2] in c.letters:

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

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

            node = mc.collection.Sset(node, name, value, cur=cur,
                    code=self.code[cur:end], pointer=1)

            last = self.create_list(node, k)
            cur = end-1

        else:

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

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

            last = self.create_list(node, k)
            cur = last

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

        k += 1

        # Fieldname of type "a.() = ..."
        if self.code[k] == "(":

            end = findend.paren(self, k)

            k += 1

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

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


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

            cur = self.create_expression(node, cur)


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

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

            value = self.code[k:j]
            last = j-1

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

            # Fieldname of type "a.b(...) = ..."
            if self.code[j] == "(":

                end = findend.paren(self, j)
                if self.disp:
                    print "%4d     Fset       " % cur,
                    print "%-20s" % "variables.assign",
                    print repr(self.code[cur:end+1])

                node = mc.collection.Fset(node, name, value=value, cur=cur,
                        code=self.code[cur:end+1])

                cur = self.create_list(node, j)

            # Fieldname of type "a.b = ..."
            else:

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

                node = mc.collection.Fvar(node, name, value=value, cur=cur,
                        code=self.code[cur:last+1])

                cur = last

    # Simple variable assignment
    else:
        if self.disp:
            print "%4d     Var        " % cur,
            print "%-20s" % "variables.assign",
            print repr(self.code[cur:last])


        node = mc.collection.Var(node, name, cur=cur,
                code=self.code[cur:last])

        cur = last-1

    node.create_declare()
    return cur
Exemple #9
0
def variable(self, parent, cur):
    """
Variable not on the left side of an assignment

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

Kwargs:
    end (int, optional): End of variable

Returns:
	int : End of variable

Example:
    >>> builder = mc.Builder(True)
    >>> builder.load("unnamed", "a")
    loading unnamed
         Program     functions.program
       0 Main        functions.main
       0 Codeblock   codeblock.codeblock 
       0   Statement     codeblock.codeblock  'a'
       0     Expression  expression.create    'a'
       0     Var         variables.variable   'a'
    >>> builder.configure()
    >>> print mc.qtree(builder, core=True) #doctest: +NORMALIZE_WHITESPACE
    1 1Block      code_block   TYPE
    1 1| Statement  code_block   TYPE
    1 1| | Var        unknown      TYPE    a
    """

    k = cur
    if self.code[k] == "@":
        k += 1

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

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

    name = self.code[cur:k]
    last = k

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

    # Get value of cell
    if self.code[k] == "{":

        end = findend.cell(self, k)
        end = end+1
        while self.code[end] in " \t":
            end += 1

        if self.code[end] == "(":

            end = findend.paren(self, end)
            node = mc.collection.Cget(parent, name, cur=cur,
                    code=self.code[cur:end+1])

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

            n_fields = 0
            while self.code[k] == "{":

                cur = self.iterate_cell(node, k)
                k = cur+1
                while self.code[k] in " \t":
                    k += 1
                n_fields += 1

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

            if  self.code[k] != "(":
                self.syntaxerror(k, "argument parenthesis")

            cur = self.create_list(node, k)

            node["n_fields"] = n_fields
            node["n_args"] = len(node) - n_fields

        else:
            end = findend.cell(self, k)
            node = mc.collection.Cvar(parent, name, cur=cur,
                    code=self.code[cur:end+1])

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

            num = 0
            while self.code[k] == "{":

                cur = cell_arg(self, node, k)
                k = cur+1
                while self.code[k] in " \t":
                    k += 1
                num += 1


    # Get value of array
    elif self.code[k] == "(":

        end = findend.paren(self, k)
        if self.code[end+1] == "." and self.code[end+2] in c.letters:

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

            if self.disp:
                print "%4d     Sget        " %\
                        cur,
                print "%-20s" % "variables.variable",
                print repr(self.code[cur:end])

            node = mc.collection.Sget(parent, name, value, cur=cur,
                    code=self.code[cur:end], pointer=1)

            last = self.create_list(node, k)
            cur = end

        else:

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

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

            last = self.create_list(node, k)
            cur = last



    elif self.code[k] == "." and self.code[k+1] not in "*/\\^'":

        k += 1

        # Fieldname of type "a.(..)"
        if self.code[k] == "(":

            end = findend.paren(self, k)

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

            k += 1

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

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

            cur = self.create_expression(node, k)


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

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

            value = self.code[k:j]
            last = j

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

            # Fieldname of type "a.b(...)"
            if self.code[j] == "(":

                end = findend.paren(self, j)
                if self.disp:
                    print "%4d     Fget       " % cur,
                    print "%-20s" % "variables.variable",
                    print repr(self.code[cur:end+1])


                node = mc.collection.Fget(parent, name, cur=cur,
                        value=value, code=self.code[cur:end+1])

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

                cur = self.create_expression(node, j)

                node.create_declare()

            # Fieldname of type "a.b"
            else:

                if self.disp:
                    print "%4d     Fvar       " % cur,
                    print "%-20s" % "variables.variable",
                    print repr(self.code[cur:last])

                node = mc.collection.Fvar(parent, name, value=value,
                        cur=cur, code=self.code[cur:last])

                cur = last-1

                node.create_declare()


    # Simple variable
    else:

        if self.disp:
            print "%4d     Var        " % cur,
            print "%-20s" % "variables.variable",
            print repr(self.code[cur:last])

        node = mc.collection.Var(parent, name, cur=cur,
                code=self.code[cur:last])

        cur = last-1

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

    return cur
Exemple #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