def value_environ(ins): """ ENVIRON$: get environment string. """ util.require_read(ins, ('$',)) expr = parse_bracket(ins) if expr[0] == '$': return vartypes.pack_string(shell.get_env(vartypes.unpack_string(expr))) else: expr = vartypes.pass_int_unpack(expr) util.range_check(1, 255, expr) return vartypes.pack_string(shell.get_env_entry(expr))
def value_environ(ins): """ ENVIRON$: get environment string. """ util.require_read(ins, ('$', )) expr = parse_bracket(ins) if expr[0] == '$': return vartypes.pack_string(shell.get_env( vartypes.unpack_string(expr))) else: expr = vartypes.pass_int_unpack(expr) util.range_check(1, 255, expr) return vartypes.pack_string(shell.get_env_entry(expr))
def string_assign_into(name, indices, offset, num, value): """ Write a packed value into a string variable or array. """ # WARNING - need to decrement basic offset by 1 to get python offset if value[0] != '$': # type mismatch raise error.RunError(13) s = vartypes.unpack_string(value) v = get_var_or_array_string_pointer(name, indices) if v == None: # illegal function call raise error.RunError(5) string_assign_unpacked_into(v, offset, num, s)
def string_assign_into(name, indices, offset, num, value): """ Write a packed value into a string variable or array. """ # WARNING - need to decrement basic offset by 1 to get python offset if value[0] != '$': # type mismatch raise error.RunError(error.TYPE_MISMATCH) s = vartypes.unpack_string(value) v = get_var_or_array_string_pointer(name, indices) if v is None: # illegal function call raise error.RunError(error.IFC) string_assign_unpacked_into(v, offset, num, s)
def value_string(ins): """ STRING$: repeat characters. """ util.require_read(ins, ('(',)) n, j = parse_expr_list(ins, 2) n = vartypes.pass_int_unpack(n) util.range_check(0, 255, n) if j[0] == '$': j = vartypes.unpack_string(j) util.range_check(1, 255, len(j)) j = j[0] else: j = vartypes.pass_int_unpack(j) util.range_check(0, 255, j) util.require_read(ins, (')',)) return vartypes.pack_string(bytearray(chr(j)*n))
def value_string(ins): """ STRING$: repeat characters. """ util.require_read(ins, ('(', )) n, j = parse_expr_list(ins, 2) n = vartypes.pass_int_unpack(n) util.range_check(0, 255, n) if j[0] == '$': j = vartypes.unpack_string(j) util.range_check(1, 255, len(j)) j = j[0] else: j = vartypes.pass_int_unpack(j) util.range_check(0, 255, j) util.require_read(ins, (')', )) return vartypes.pack_string(bytearray(chr(j) * n))
def assign_field_var_or_array(name, indices, value, justify_right=False): """ Write a packed value into a field-assigned string. """ if value[0] != '$': # type mismatch raise error.RunError(13) s = vartypes.unpack_string(value) v = get_var_or_array_string_pointer(name, indices) if v == None: # LSET has no effect if variable does not exist return # trim and pad to size length = ord(v[0:1]) s = s[:length] if justify_right: s = ' '*(length-len(s)) + s else: s += ' '*(length-len(s)) # copy new value into existing buffer string_assign_unpacked_into(v, 0, length, s)
def assign_field_var_or_array(name, indices, value, justify_right=False): """ Write a packed value into a field-assigned string. """ if value[0] != '$': # type mismatch raise error.RunError(error.TYPE_MISMATCH) s = vartypes.unpack_string(value) v = get_var_or_array_string_pointer(name, indices) if v is None: # LSET has no effect if variable does not exist return # trim and pad to size length = ord(v[0:1]) s = s[:length] if justify_right: s = ' ' * (length - len(s)) + s else: s += ' ' * (length - len(s)) # copy new value into existing buffer string_assign_unpacked_into(v, 0, length, s)
def debug_step(linum): """ Execute traces and watches on a program step. """ if not debug_mode: return outstr = '' if debug_tron: outstr += ('['+('%i' % linum) +']') for (expr, outs) in watch_list: outstr += (' ' + expr +' = ') outs.seek(2) try: val = expressions.parse_expression(outs) st = vartypes.unpack_string(representation.value_to_str_keep(val, screen=False)) if val[0] == '$': outstr += ('"'+st+'"') else: outstr += (st) except Exception as e: debug_handle_exc(e) if outstr: logging.debug(outstr)
def debug_step(linum): """ Execute traces and watches on a program step. """ if not debug_mode: return outstr = '' if debug_tron: outstr += ('[' + ('%i' % linum) + ']') for (expr, outs) in watch_list: outstr += (' ' + expr + ' = ') outs.seek(2) try: val = expressions.parse_expression(outs) st = vartypes.unpack_string( representation.value_to_str_keep(val, screen=False)) if val[0] == '$': outstr += ('"' + st + '"') else: outstr += (st) except Exception as e: debug_handle_exc(e) if outstr: logging.debug(outstr)
def clear_variables(preserve_common=False, preserve_all=False, preserve_deftype=False): """ Reset and clear variables, arrays, common definitions and functions. """ if not preserve_deftype: # deftype is not preserved on CHAIN with ALL, but is preserved with MERGE state.basic_state.deftype = ['!']*26 if not preserve_all: if preserve_common: # preserve COMMON variables (CHAIN does this) common, common_arrays, common_strings = {}, {}, {} for varname in state.basic_state.common_names: try: common[varname] = state.basic_state.variables[varname] except KeyError: pass for varname in state.basic_state.common_array_names: try: common_arrays[varname] = state.basic_state.arrays[varname] except KeyError: pass else: # clear option base state.basic_state.array_base = None common = {} common_arrays = {} # at least I think these should be cleared by CLEAR? state.basic_state.common_names = [] state.basic_state.common_array_names = [] # restore only common variables # this is a re-assignment which is not FOR-safe; but clear_variables is only called in CLEAR which also clears the FOR stack state.basic_state.variables = {} state.basic_state.arrays = {} state.basic_state.var_memory = {} state.basic_state.array_memory = {} state.basic_state.var_current = memory.var_start() # arrays are always kept after all vars state.basic_state.array_current = 0 # functions are cleared except when CHAIN ... ALL is specified state.basic_state.functions = {} # reset string space new_strings = StringSpace() # preserve common variables # use set_var and dim_array to rebuild memory model for v in common: if v[-1] == '$': state.basic_state.variables[v] = ( new_strings.store(bytearray(vartypes.unpack_string( get_string_copy_packed(common[v]) )))) else: set_var(v, (v[-1], common[v])) for a in common_arrays: dim_array(a, common_arrays[a][0]) if a[-1] == '$': s = bytearray() for i in range(0, len(common_arrays[a][1]), byte_size['$']): s += (new_strings.store(bytearray(vartypes.unpack_string( get_string_copy_packed(common_arrays[a][1][i+1:i+byte_size['$']]) )))) state.basic_state.arrays[a][1] = s else: state.basic_state.arrays[a] = common_arrays[a] state.basic_state.strings = new_strings
def clear_variables(preserve_common=False, preserve_all=False, preserve_deftype=False): """ Reset and clear variables, arrays, common definitions and functions. """ if not preserve_deftype: # deftype is not preserved on CHAIN with ALL, but is preserved with MERGE state.basic_state.deftype = ['!'] * 26 if not preserve_all: if preserve_common: # preserve COMMON variables (CHAIN does this) common, common_arrays, common_strings = {}, {}, {} for varname in state.basic_state.common_names: try: common[varname] = state.basic_state.variables[varname] except KeyError: pass for varname in state.basic_state.common_array_names: try: common_arrays[varname] = state.basic_state.arrays[varname] except KeyError: pass else: # clear option base state.basic_state.array_base = None common = {} common_arrays = {} # at least I think these should be cleared by CLEAR? state.basic_state.common_names = [] state.basic_state.common_array_names = [] # restore only common variables # this is a re-assignment which is not FOR-safe; but clear_variables is only called in CLEAR which also clears the FOR stack state.basic_state.variables = {} state.basic_state.arrays = {} state.basic_state.var_memory = {} state.basic_state.array_memory = {} state.basic_state.var_current = memory.var_start() # arrays are always kept after all vars state.basic_state.array_current = 0 # functions are cleared except when CHAIN ... ALL is specified state.basic_state.functions = {} # reset string space new_strings = StringSpace() # preserve common variables # use set_var and dim_array to rebuild memory model for v in common: if v[-1] == '$': state.basic_state.variables[v] = (new_strings.store( bytearray( vartypes.unpack_string( get_string_copy_packed(common[v]))))) else: set_var(v, (v[-1], common[v])) for a in common_arrays: dim_array(a, common_arrays[a][0]) if a[-1] == '$': s = bytearray() for i in range(0, len(common_arrays[a][1]), byte_size['$']): s += (new_strings.store( bytearray( vartypes.unpack_string( get_string_copy_packed( common_arrays[a][1][i + 1:i + byte_size['$']]))))) state.basic_state.arrays[a][1] = s else: state.basic_state.arrays[a] = common_arrays[a] state.basic_state.strings = new_strings