def set_var(name, value): """ Assign a value to a variable. """ name = vartypes.complete_name(name) type_char = name[-1] # check if garbage needs collecting before allocating mem size = (max(3, len(name)) + 1 + byte_size[type_char]) if type_char == '$': unpacked = vartypes.pass_string_unpack(value) size += len(unpacked) if fre() <= size: # TODO: GARBTEST difference is because string literal is currently stored in string space, whereas GW stores it in code space. collect_garbage() if fre() <= size: raise error.RunError(7) # assign variables if type_char == '$': # every assignment to string leads to new pointer being allocated # TODO: string literals in programs have the var ptr point to program space. state.basic_state.variables[name] = state.basic_state.strings.store(bytearray(unpacked[:])) else: # make a copy of the value in case we want to use POKE on it - we would change both values otherwise # NOTE: this is an in-place copy - crucial for FOR! try: state.basic_state.variables[name][:] = vartypes.pass_type_keep(name[-1], value)[1][:] except KeyError: state.basic_state.variables[name] = vartypes.pass_type_keep(name[-1], value)[1][:] # update memory model # first two bytes: chars of name or 0 if name is one byte long if name not in state.basic_state.var_memory: name_ptr = state.basic_state.var_current var_ptr = name_ptr + max(3, len(name)) + 1 # byte_size first_letter second_letter_or_nul remaining_length_or_nul state.basic_state.var_current += max(3, len(name)) + 1 + byte_size[name[-1]] state.basic_state.var_memory[name] = (name_ptr, var_ptr)
def set_array(name, index, value): """ Assign a value to an array element. """ [dimensions, lst] = check_dim_array(name, index) bigindex = index_array(index, dimensions) # make a copy of the value, we don't want them to be linked value = (vartypes.pass_type_keep(name[-1], value)[1])[:] # for strings, store the string in string space and store the key in the array if name[-1] == '$': value = state.basic_state.strings.store(bytearray(value)) bytesize = var_size_bytes(name) lst[bigindex*bytesize:(bigindex+1)*bytesize] = value # inc version state.basic_state.arrays[name][2] += 1
def set_array(name, index, value): """ Assign a value to an array element. """ [dimensions, lst] = check_dim_array(name, index) bigindex = index_array(index, dimensions) # make a copy of the value, we don't want them to be linked value = (vartypes.pass_type_keep(name[-1], value)[1])[:] # for strings, store the string in string space and store the key in the array if name[-1] == '$': value = state.basic_state.strings.store(bytearray(value)) bytesize = var_size_bytes(name) lst[bigindex * bytesize:(bigindex + 1) * bytesize] = value # inc version state.basic_state.arrays[name][2] += 1
def set_var(name, value): """ Assign a value to a variable. """ name = vartypes.complete_name(name) type_char = name[-1] # check if garbage needs collecting before allocating mem size = (max(3, len(name)) + 1 + byte_size[type_char]) if type_char == '$': unpacked = vartypes.pass_string_unpack(value) size += len(unpacked) if fre() <= size: # TODO: GARBTEST difference is because string literal is currently stored in string space, whereas GW stores it in code space. collect_garbage() if fre() <= size: raise error.RunError(error.OUT_OF_MEMORY) # assign variables if type_char == '$': # every assignment to string leads to new pointer being allocated # TODO: string literals in programs have the var ptr point to program space. state.basic_state.variables[name] = state.basic_state.strings.store( bytearray(unpacked[:])) else: # make a copy of the value in case we want to use POKE on it - we would change both values otherwise # NOTE: this is an in-place copy - crucial for FOR! try: state.basic_state.variables[name][:] = vartypes.pass_type_keep( name[-1], value)[1][:] except KeyError: state.basic_state.variables[name] = vartypes.pass_type_keep( name[-1], value)[1][:] # update memory model # first two bytes: chars of name or 0 if name is one byte long if name not in state.basic_state.var_memory: name_ptr = state.basic_state.var_current var_ptr = name_ptr + max( 3, len(name) ) + 1 # byte_size first_letter second_letter_or_nul remaining_length_or_nul state.basic_state.var_current += max( 3, len(name)) + 1 + byte_size[name[-1]] state.basic_state.var_memory[name] = (name_ptr, var_ptr)