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 op, arg in zip(self.operators, self.arg[1:]): if (op == "+"): ret.append(' {} {!s}'.format( "|plus|", to_python(arg, context, params=params))) else: ret.append(' {} {!s}'.format( op, "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))
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 + ")"
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 + ")"
def to_python(self, context, params=None, indent=0): arg_str = to_python(self.arg, context) r = "core.vba_library.run_function(\"_Chr\", vm_context, [" + arg_str + "])" return r
def to_python(self, context, params=None, indent=0): return "ord(" + to_python(self.arg, context) + "[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
def to_python(self, context, params=None, indent=0): r = "- (" + "coerce_to_num(" + to_python(self.arg, context) + "))" return r
def to_python(self, context, params=None, indent=0): r = "~ (coerce_to_int(" + to_python(self.arg, context) + "))" return r
def to_python(self, context, params=None, indent=0): # Get the global variables read in the function body. tmp_context = Context(context=context, _locals=context.locals, copy_globals=True) 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: val = to_python(global_var_info[global_var], context) global_var_init_str += indent_str + safe_str_convert( global_var) + " = " + safe_str_convert(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 = safe_str_convert(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 " + safe_str_convert( 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 + safe_str_convert(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: r += indent_str + " " * 4 + "global " + safe_str_convert( 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 " + safe_str_convert( self.name) + "\n" # Done. return r
def to_python(self, context, params=None, indent=0): return to_python(self.block, context, indent=indent, statements=True)
def to_python(self, context, params=None, indent=0): return to_python(self.loose_lines, context, indent=indent, statements=True)