def slice_ast_to_dict(ast_seq): sl_vars = {} if isinstance(ast_seq, (list, tuple)): for pattern in slice_patterns: found,data = match(pattern,ast_seq) if found: sl_vars = {'begin':'_beg', 'end':'_end', 'step':'_stp', 'single_index':'_index'} for key in data.keys(): data[key] = ast_to_string(data[key]) sl_vars.update(data) break; return sl_vars
def slice_ast_to_dict(ast_seq): sl_vars = {} if isinstance(ast_seq, (list, tuple)): for pattern in slice_patterns: found, data = match(pattern, ast_seq) if found: sl_vars = { 'begin': '_beg', 'end': '_end', 'step': '_stp', 'single_index': '_index' } for key in data.keys(): data[key] = ast_to_string(data[key]) sl_vars.update(data) break return sl_vars
def ast_to_blitz_expr(ast_seq): """ Convert an ast_sequence to a blitz expression. """ # Don't overwrite orignal sequence in call to transform slices. ast_seq = copy.deepcopy(ast_seq) slice_handler.transform_slices(ast_seq) # Build the actual program statement from ast_seq expr = ast_tools.ast_to_string(ast_seq) # Now find and replace specific symbols to convert this to # a blitz++ compatible statement. # I'm doing this with string replacement here. It could # also be done on the actual ast tree (and probably should from # a purest standpoint...). # this one isn't necessary but it helps code readability # and compactness. It requires that # Range _all = blitz::Range::all(); # be included in the generated code. # These could all alternatively be done to the ast in # build_slice_atom() expr = expr.replace('slice(_beg,_end)', '_all' ) expr = expr.replace('slice', 'blitz::Range' ) expr = expr.replace('[','(') expr = expr.replace(']', ')' ) expr = expr.replace('_stp', '1' ) # Instead of blitz::fromStart and blitz::toEnd. This requires # the following in the generated code. # Range _beg = blitz::fromStart; # Range _end = blitz::toEnd; #expr = expr.replace('_beg', 'blitz::fromStart' ) #expr = expr.replace('_end', 'blitz::toEnd' ) return expr + ';\n'
def ast_to_blitz_expr(ast_seq): """ Convert an ast_sequence to a blitz expression. """ # Don't overwrite orignal sequence in call to transform slices. ast_seq = copy.deepcopy(ast_seq) slice_handler.transform_slices(ast_seq) # Build the actual program statement from ast_seq expr = ast_tools.ast_to_string(ast_seq) # Now find and replace specific symbols to convert this to # a blitz++ compatible statement. # I'm doing this with string replacement here. It could # also be done on the actual ast tree (and probably should from # a purest standpoint...). # this one isn't necessary but it helps code readability # and compactness. It requires that # Range _all = blitz::Range::all(); # be included in the generated code. # These could all alternatively be done to the ast in # build_slice_atom() expr = expr.replace('slice(_beg,_end)', '_all') expr = expr.replace('slice', 'blitz::Range') expr = expr.replace('[', '(') expr = expr.replace(']', ')') expr = expr.replace('_stp', '1') # Instead of blitz::fromStart and blitz::toEnd. This requires # the following in the generated code. # Range _beg = blitz::fromStart; # Range _end = blitz::toEnd; #expr = expr.replace('_beg', 'blitz::fromStart' ) #expr = expr.replace('_end', 'blitz::toEnd' ) return expr + ';\n'