#!/usr/bin/python
# fn_vector.py $Revision: 1.2 $ $Date: 2005/10/12 15:48:52 $
# This library contains functions which interact with and use the
# vector parts of the memory model.

import vector

model_vectors= vector.VectorList()

import objectifier
build_GG_command= objectifier.build_GG_command

# =============== Built In Functions ===========================
internal_syms= dict() # Establish the list of built-in functions.


def intf_ADDVEC(E):
    """Add a vector to the model's vector list."""
    listxyz= E.The.StackPop().val
    x, y, z= listxyz
    anewvector= vector.Vector(x.val,y.val,z.val)
    model_vectors.append(anewvector)
internal_syms['addvec']= intf_ADDVEC # Register function.
internal_syms['vector']= build_GG_command('addvec')
    
def intf_LASTVEC(E):
    """Return a list of the x,y,z of the last vector."""
    P= model_vectors[-1]
    if P:
        newvectorlistob= list()
        newvectorlistob.append(objectifier.StackOB_VAL(P.x))
        newvectorlistob.append(objectifier.StackOB_VAL(P.y))
        newvectorlistob.append(objectifier.StackOB_VAL(P.z))
        newvectorlistob= objectifier.StackOB_LST(newvectorlistob)
        newvectorlistob.names= ['x', 'y', 'z']
        E.The.StackPush(newvectorlistob)
internal_syms['lastvec']= intf_LASTVEC # Register function.

def intf_GETVEC(E):
    """Takes a number off the stack and returns that position's vector
    in the vector list. If the number is negative, then counts from the
    end. Return a list of the x,y,z of the last vector."""
    location= E.The.StackPop().val
    P= model_vectors[int(location)]
    if P:
        newvectorlistob= list()
        newvectorlistob.append(objectifier.StackOB_VAL(P.x))
        newvectorlistob.append(objectifier.StackOB_VAL(P.y))
        newvectorlistob.append(objectifier.StackOB_VAL(P.z))
        newvectorlistob= objectifier.StackOB_LST(newvectorlistob)
        newvectorlistob.names= ['x', 'y', 'z']
        E.The.StackPush(newvectorlistob)
internal_syms['getvec']= intf_GETVEC # Register function.

def intf_ALLVEC(E):
    """Display a list of all vectors."""
    for p in model_vectors:
        print str(p.p_id) +' - '+ str(p)
internal_syms['allvec']= intf_ALLVEC # Register function.
    
