(self, fnname, ins)
Define a function.
Define a function.
Define a function .
def define(self, fnname, ins):
        """Define a function."""
        ins.skip_blank()
        pointer_loc = self._memory.code_start + ins.tell()
        # read parameters
        fnvars = []
        if ins.skip_blank_read_if((b'(',)):
            while True:
                name = ins.read_name()
                # must not be empty
                error.throw_if(not name, error.STX)
                # do not append sigil here yet, leave until evaluation time
                fnvars.append(name)
                if ins.skip_blank() in tk.END_STATEMENT + (b')',):
                    break
                ins.require_read((b',',))
            ins.require_read((b')',))
        # read code
        if not ins.skip_blank_read_if((tk.O_EQ,)):
            self._fn_dict[fnname] = None
            return
        self._fn_dict[fnname] = UserFunction(fnname, ins, fnvars, self._memory, self._expression_parser)
        ins.skip_to(tk.END_STATEMENT)
        # update memory model
        # allocate function pointer
        pointer = struct.pack('<H', pointer_loc) + bytearray(values.size_bytes(fnname)-2)
        # function name is represented with first char shifted by 128
        memory_name = int2byte(128+ord(fnname[0:1])) + fnname[1:]
        self._memory.scalars.set(memory_name, self._values.from_bytes(pointer))
        for name in fnvars:
            # allocate, but don't set, variables
            # this is essential for them to be preserved during evaluate()
            name = self._memory.complete_name(name)
            self._memory.scalars.set(name)
def define ( self , fnname , ins ) : ins . skip_blank ( ) pointer_loc = self . _memory . code_start + ins . tell ( ) # read parameters fnvars = [ ] if ins . skip_blank_read_if ( ( b'(' , ) ) : while True : name = ins . read_name ( ) # must not be empty error . throw_if ( not name , error . STX ) # do not append sigil here yet, leave until evaluation time fnvars . append ( name ) if ins . skip_blank ( ) in tk . END_STATEMENT + ( b')' , ) : break ins . require_read ( ( b',' , ) ) ins . require_read ( ( b')' , ) ) # read code if not ins . skip_blank_read_if ( ( tk . O_EQ , ) ) : self . _fn_dict [ fnname ] = None return self . _fn_dict [ fnname ] = UserFunction ( fnname , ins , fnvars , self . _memory , self . _expression_parser ) ins . skip_to ( tk . END_STATEMENT ) # update memory model # allocate function pointer pointer = struct . pack ( '<H' , pointer_loc ) + bytearray ( values . size_bytes ( fnname ) - 2 ) # function name is represented with first char shifted by 128 memory_name = int2byte ( 128 + ord ( fnname [ 0 : 1 ] ) ) + fnname [ 1 : ] self . _memory . scalars . set ( memory_name , self . _values . from_bytes ( pointer ) ) for name in fnvars : # allocate, but don't set, variables # this is essential for them to be preserved during evaluate() name = self . _memory . complete_name ( name ) self . _memory . scalars . set ( name )