Ejemplo n.º 1
0
    def to_python(self, context, params=None, indent=0):

        # We are generating Python code for some string or numeric
        # expression. Therefore any boolean operators we find in the
        # expression are actually bitwise operators.
        # Track that in the context.
        set_flag = False
        if (not context.in_bitwise_expression):
            context.in_bitwise_expression = True
            set_flag = True

        if (self.operators[0] == "+"):
            ret = [to_python(self.arg[0], context, params=params)]
        else:
            ret = [
                "coerce_to_num(" +
                to_python(self.arg[0], context, params=params) + ")"
            ]
        for operator, arg in zip(self.operators, self.arg[1:]):
            if (operator == "+"):
                ret.append(' {} {!s}'.format(
                    "|plus|", to_python(arg, context, params=params)))
            else:
                ret.append(' {} {!s}'.format(
                    operator, "coerce_to_num(" +
                    to_python(arg, context, params=params) + ")"))

        # Out of the string/numeric expression. Might have actual boolean
        # expressions now.
        if set_flag:
            context.in_bitwise_expression = False

        return '({})'.format(''.join(ret))
Ejemplo n.º 2
0
    def to_python(self, context, params=None, indent=0):
        
        # Get the global variables read in the function body.
        tmp_context = Context(context=context)
        global_var_info, _ = _get_var_vals(self, tmp_context, global_only=True)
        
        # Set up the initial values for the global variables.
        global_var_init_str = ""
        indent_str = " " * indent
        for global_var in global_var_info.keys():
            val = global_var_info[global_var]
            global_var_init_str += indent_str + str(global_var) + " = " + str(val) + "\n"
        
        # Make a copy of the context so we can mark variables as function
        # arguments.
        tmp_context = Context(context=context)
        for param in self.params:
            tmp_context.set(param.name, "__FUNC_ARG__")

        # Save the name of the current function so we can handle exit function calls.
        tmp_context.curr_func_name = str(self.name)

        # Global variable initialization goes first.
        r = global_var_init_str
        
        # Define the function prototype.
        func_args = "("
        first = True
        for param in self.params:
            if (not first):
                func_args += ", "
            first = False
            func_args += utils.fix_python_overlap(to_python(param, tmp_context))
        func_args += ")"
        r += indent_str + "def " + str(self.name) + func_args + ":\n"

        # Init return value.
        r += indent_str + " " * 4 + "import core.vba_library\n"
        r += indent_str + " " * 4 + "global vm_context\n\n"
        r += indent_str + " " * 4 + "# Function return value.\n"
        r += indent_str + " " * 4 + str(self.name) + " = 0\n\n"

        # Global variables used in the function.
        r += indent_str + " " * 4 + "# Referenced global variables.\n"
        for global_var in global_var_info.keys():
            r += indent_str + " " * 4 + "global " + str(global_var) + "\n"
        r += "\n"
            
        # Function body.
        r += to_python(self.statements, tmp_context, indent=indent+4, statements=True)

        # Check for IOCs.
        r += "\n" + _check_for_iocs(self, tmp_context, indent=indent+4)
        
        # Return the function return val.
        r += "\n" + indent_str + " " * 4 + "return " + str(self.name) + "\n"

        # Done.
        return r
Ejemplo n.º 3
0
 def to_python(self, context, params=None, indent=0):
     if (self.operators[0] == "+"):
         ret = [to_python(self.arg[0], context, params=params)]
     else:
         ret = [
             "coerce_to_num(" +
             to_python(self.arg[0], context, params=params) + ")"
         ]
     for operator, arg in zip(self.operators, self.arg[1:]):
         if (operator == "+"):
             ret.append(' {} {!s}'.format(
                 operator, to_python(arg, context, params=params)))
         else:
             ret.append(' {} {!s}'.format(
                 operator, "coerce_to_num(" +
                 to_python(arg, context, params=params) + ")"))
     return '({})'.format(''.join(ret))
Ejemplo n.º 4
0
 def to_python(self, context, params=None, indent=0):
     r = ""
     first = True
     for arg in self.arg:
         if (not first):
             r += " % "
         first = False
         r += "coerce_to_num(" + to_python(arg, context,
                                           params=params) + ")"
     return "(" + r + ")"
Ejemplo n.º 5
0
 def to_python(self, context, params=None, indent=0):
     r = ""
     first = True
     for arg in self.arg:
         if (not first):
             r += " + "
         first = False
         # Could be a str or an int, so hope for the best.
         r += to_python(arg, context, params=params)
     return "(" + r + ")"
Ejemplo n.º 6
0
 def to_python(self, context, params=None, indent=0):
     return to_python(self.block, context, indent=indent, statements=True)
Ejemplo n.º 7
0
 def to_python(self, context, params=None, indent=0):
     return to_python(self.loose_lines,
                      context,
                      indent=indent,
                      statements=True)
Ejemplo n.º 8
0
 def to_python(self, context, params=None, indent=0):
     r = reduce(
         lambda x, y: "pow(coerce_to_num(" + to_python(x, context) +
         "), coerce_to_num(" + to_python(y, context) + "))", self.arg)
     return r
Ejemplo n.º 9
0
 def to_python(self, context, params=None, indent=0):
     r = "- (" + "coerce_to_num(" + to_python(self.arg, context) + "))"
     return r