Exemple #1
0
def intf_ENTCHTAG(E):
    """ From level three either an entity ID value or a list of entity ID values
    off the stack. From level two, a TXT object of an existing tag is
    taken off the stack. Finally from level one, the TXT object of the new
    replacement tag is taken.
    Any specified entities which are tagged with the specified tags will have
    those tags removed and replaced with the new one.
        <entID> ''roof'' ''wall'' chtag ->  
        [<entID> <entID> <entID>] ''roof'' ''wall'' chtag -> 
    """
    if ( not inc.entid_or_LST_of_entids(E.The,3) or
         not inc.TXT(E,2) or not inc.TXT(E,1) ):
        print("Input Error: chtag")
        print(intf_ENTCHTAG.__doc__)
        return # Without doing much of anything.
    refreshview= False # No need unless view attributes (@) have been affected.
    newtag= E.The.StackPop().val
    oldtag= E.The.StackPop().val
    myeids= E.The.StackPop().val
    if type(myeids)==type(list()):
        #myeids= map(lambda x:x.val, myeids) # Should now be a list of VALs.
        myeids= [x.val for x in  myeids] # Should now be a list of VALs.
    else:
        myeids= [ myeids ] # Also a (1 item) list of ints.
    for myeid in myeids:
        if myeid in MMEL.El: # Check if eid exists.
            if MMEL.El[myeid].has_tag(oldtag):
                print("Untagging entity #%d with tag ''%s''" % (myeid,oldtag))
                MMEL.El[myeid].del_tag(oldtag)
                MMEL.El[myeid].add_tag(newtag)
                if '@' in oldtag or '@' in newtag: refreshview= True
        else:
            print("Warning: No entity #%d. Skipping." % myeid)
    if refreshview: OUT.default(MMEL,E) # AUTODUMP 
Exemple #2
0
def intf_REPL(E):
    """Takes a string or list object from v3 and a position from v2
    and a replacement string or list object from v1 and replaces the
    portion of v3 starting at v2 with v1. If v2 is less than 1 or
    greater than the `len` of v3, the items are simply concatenated,
    v1+v3 and v3+v1 respectively.
       [5 10 16 21 25] 3 [15 20] -> [5 10 15 20 25]
       ''abcdefghi'' 4 ''XYZ'' repl -> ''abcXYZghi''
    """
    if ( ( not inc.VAL(E,2) or not inc.listlike(E,1) or not inc.listlike(E,3) )
        or ( inc.TXT(E,1) != inc.TXT(E,3) ) ): # If types don't match.
            print("Input Error: repl")
            print(intf_REPL.__doc__)
            return # Without doing much of anything.
    ob1= E.The.StackPop() # Replacement text or list
    n= int(E.The.StackPop().val) # Position.
    ob3ob= E.The.StackPop() # Original list or text.
    outistxt= (ob3ob.val == "TXT")
    oblen= len(ob3ob.val)
    if oblen < n:
        ob3= ob3ob.val+ob1.val
    elif n < 1:
        ob3= ob1.val+ob3ob.val
    else:
        n-= 1
        if outistxt:
            ob3= list(ob3ob.val) # Python strings are immutable.
        else:
            ob3= ob3ob.val
        start= ob3ob.val[0:n]
        plusmid= start + ob1.val
        ob3= plusmid
        if len(plusmid) < oblen:
            ob3+= ob3ob.val[len(plusmid):]
    if outistxt:
        outob= objectifier.StackOB_TXT( ''.join(plusmid) )
    else:
        outob= objectifier.StackOB_LST( ob3 )
    E.The.StackPush(outob)
Exemple #3
0
def intf_POS(E):
    """Takes a LST or TXT item from v2. If v2 is a LST then take an
    item from v1 and return the position v1 is found in v2. If v2 is a
    TXT itme, then v1 must be a TXT item and the value returned is the
    position where the substring v2 is found in v1.
        ''abcdefghijklmnopqrstuvwxyz'' ''stu'' pos -> 19
    """
    # Not sure searching for a list inside a list works, but otherwise fine.
    if ( inc.TXT(E,2) and not inc.TXT(E,1) ) or not inc.listlike(E,2):
        print("Input Error: pos")
        print(intf_POS.__doc__)
        return # Without doing much of anything.
    i= E.The.StackPop() # Item to find.
    ob= E.The.StackPop() # List or text.
    if ob.whatami == "LST":
        vallist= map(lambda x:x.val, ob.val)
        if i.val in vallist:
            p= vallist.index(i.val) + 1
        else:
            p= 0
    elif ob.whatami == "TXT":
        p= ob.val.find(i.val) + 1
    out= objectifier.StackOB_VAL(p)
    E.The.StackPush(out)
Exemple #4
0
def intf_MUL(E):
    """Item multiplication operator where the values are the focus. Compare to
    `:*` where lists are the focus. Valid inputs:
      SYM SYM   + -> (resolve any SYMs and proceed again) 
      VAL VAL * -> simple multiplication
      TXT VAL * -> replication of text into concatenated text, commutative input
      LST_of_VALs VAL * ->  list of each list item multiplied by VAL, commutative input
      LST_of_VALs LST_of_VALs * -> pad smaller with 1 and multiply each ith pos
    If two lists are multiplied, the shorter one is padded with ones to match the other.
    In the case of two lists and both have list names, the longest list's are used. 
    Examples:
      2 2 * -> 4
      2 [1 2 3] * -> [2 4 6]
      31.831 |diameter sto |diameter |pi * -> 100.000035756
      [pi pi pi] pi inv * -> [1 1 1]
      [1 2 3] 2 * -> [2 4 6]
      [1 2 3] [1 2] * -> [1 4 3]  Pad the shorter list with ones.
      [2 2] [4 4 4::x y z] * -> [8 8 4]<x y z>
      [1 2] [1 2 3] * -> [1 4 3]
      4 ''ha'' * -> ''hahahaha''
      ''backwards'' -2 * -> ''sdrawkcabsdrawkcab''
    """
    if not ((inc.TXT(E, 1) and inc.VAL(E, 2)) or
            (inc.VAL(E, 1) and inc.TXT(E, 2)) or
            ((inc.VAL(E, 1) or inc.LST_of_VALs(E, 1)) and
             (inc.VAL(E, 2) or inc.LST_of_VALs(E, 2)))):
        print("Input Error: mul")
        print(intf_MUL.__doc__)
        return  # Without doing much of anything.
    v2 = E.resolve_symbol(E.The.StackPop())
    v1 = E.resolve_symbol(E.The.StackPop())
    if v1.whatami == 'VAL' and v2.whatami == 'VAL':
        E.The.StackPush(objectifier.StackOB_VAL(v1.val * v2.val))
    elif v1.whatami == "TXT":
        if v2.val < 0:
            v1.val, v2.val = v1.val[::-1], v2.val * -1  # Do this for `neg`.
        E.The.StackPush(objectifier.StackOB_TXT(v1.val * int(v2.val)))
    elif v2.whatami == "TXT":
        if v1.val < 0:
            v2.val, v1.val = v2.val[::-1], v1.val * -1  # It's silly, I know.
        E.The.StackPush(objectifier.StackOB_TXT(v2.val * int(v1.val)))
    elif ((v1.whatami == 'LST' and v2.whatami == 'VAL')
          or (v1.whatami == 'VAL' and v2.whatami == 'LST')):  # Mixed LST&VAL
        if v1.whatami == 'VAL': v1, v2 = v2, v1  # Ensure LST 1st then VAL 2nd
        outlist = list()
        for i in v1.val:
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            outlist.append(objectifier.StackOB_VAL(i.val * v2.val))
        outlistob = objectifier.StackOB_LST(outlist)
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    elif v1.whatami == 'LST' and v2.whatami == 'LST':
        lv1, lv2 = len(v1.val), len(v2.val)
        if lv1 < lv2:
            v1, v2 = v2, v1  # Longest LST first.
            lv1, lv2 = lv2, lv1
        outlist = list()
        for n, i in enumerate(v1.val):
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            if n < lv2:
                if v2.val[n].whatami == 'SYM':
                    froml2 = E.resolve_symbol(i)
                else:
                    froml2 = v2.val[n]
                outlist.append(objectifier.StackOB_VAL(i.val * froml2.val))
            else:
                outlist.append(objectifier.StackOB_VAL(i.val))
        outlistob = objectifier.StackOB_LST(outlist)
        if not v1.names and v2.names:  # If both have names go with v1's.
            v1, v2 = v2, v1
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    else:
        print(
            "Error: What the hell have you done!? This should never have happend. See `intf_MUL`."
        )
    E.Undo.StackPush([objectifier.StackOB_SYM('drop'), v2, v1])
Exemple #5
0
def intf_ADD(E):
    """Item addition operator where the values are the focus. Compare 
    to `:+` where lists are the focus. All SYMs are resolved. Valid inputs:
        TXT TXT + -> concatenate text
        VAL VAL + -> simple addition, commutative
        LST_of_VALs VAL + -> add VAL to all items in list (of values), commutative
        LST_of_VALs LST_of_VALs + -> pad smaller with zero and add each ith pos, commutative
    If two lists are added, the shorter one is padded with zeros to match the other.
    In the case of two lists and both have list names, the longest list's are used. 
    Examples:
        2 2 + -> 4
        |pi pi + 2deg -> 360
        [pi 10::a b] |Q sto -180 2rad Q.a + -> 0
        [4 8] 3 + -> [7 11]
        [4 8] [2 3] + -> [6 11]
        [4 8] [2 3] + -> [6 11]
        [4 8 3] [2 3] + -> [6 11 3]
        [4 8] [1 2 3] + -> [5 10 3] 
        [0 4] [1 2 3] + -> [1 6 3] 
        [1 2 3::a b c] [2 2 2::x y z] + -> [3 4 5]<a b c>
        ''xed'' ''.ch'' + -> ''xed.ch''
    """
    if not ((inc.TXT(E, 1) and inc.TXT(E, 2)) or
            ((inc.VAL(E, 1) or inc.LST_of_VALs(E, 1)) and
             (inc.VAL(E, 2) or inc.LST_of_VALs(E, 2)))):
        print("Input Error: add")
        print(inc.LST_of_VALs(E, 1))
        print(inc.LST_of_VALs(E, 2))
        print(" Value 1:" + str(E.The.StackPop()))
        print(" Value 2:" + str(E.The.StackPop()))
        print(intf_ADD.__doc__)
        return  # Without doing much of anything.
    v2 = E.resolve_symbol(E.The.StackPop())
    v1 = E.resolve_symbol(E.The.StackPop())
    if v1.whatami == 'TXT':  # v2's been checked.
        E.The.StackPush(objectifier.StackOB_TXT(v1.val + v2.val))
    elif v1.whatami == 'VAL' and v2.whatami == 'VAL':
        E.The.StackPush(objectifier.StackOB_VAL(v1.val + v2.val))
    elif ((v1.whatami == 'LST' and v2.whatami == 'VAL')
          or (v1.whatami == 'VAL' and v2.whatami == 'LST')):  # Mixed LST&VAL
        if v1.whatami == 'VAL': v1, v2 = v2, v1  # Ensure LST 1st then VAL 2nd
        outlist = list()
        for i in v1.val:
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            outlist.append(objectifier.StackOB_VAL(i.val + v2.val))
        outlistob = objectifier.StackOB_LST(outlist)
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    elif v1.whatami == 'LST' and v2.whatami == 'LST':
        lv1, lv2 = len(v1.val), len(v2.val)
        if lv1 < lv2:
            v1, v2 = v2, v1  # Longest LST first.
            lv1, lv2 = lv2, lv1
        outlist = list()
        for n, i in enumerate(v1.val):
            if i.whatami == 'SYM':
                i = E.resolve_symbol(i)
            if n < lv2:
                if v2.val[n].whatami == 'SYM':
                    froml2 = E.resolve_symbol(i)
                else:
                    froml2 = v2.val[n]
                outlist.append(objectifier.StackOB_VAL(i.val + froml2.val))
            else:
                outlist.append(objectifier.StackOB_VAL(i.val))
        outlistob = objectifier.StackOB_LST(outlist)
        if not v1.names and v2.names:  # If both have names go with v1's.
            v1, v2 = v2, v1
        outlistob.names = v1.names[:]
        E.The.StackPush(outlistob)
    else:
        print(
            "Error: What the hell have you done!? This should never have happend. See `intf_ADD`."
        )
    E.Undo.StackPush([objectifier.StackOB_SYM('drop'), v2, v1])