Azinix

rlpNode.c

Go to the documentation of this file.
00001 /** \file rlpNode.c 
00002 
00003   \brief Routines for building lisp-like lists
00004 
00005 */
00006 
00007 #include "rlp.h"
00008 
00009 /**AutomaticStart*************************************************************/
00010 
00011 /**AutomaticEnd***************************************************************/
00012 
00013 
00014 /** \brief Allocate a new node */
00015 
00016 Rlp_Formula_t *
00017 new_node (Rlp_OperatorEnum_t type,
00018       Rlp_Formula_t * lchild, Rlp_Formula_t * rchild)
00019 {
00020   Rlp_Formula_t *tnode;
00021 
00022   tnode = (Rlp_Formula_t *) malloc (sizeof (Rlp_Formula_t));
00023   tnode->type = type;
00024   tnode->lchild = lchild;
00025   tnode->rchild = rchild;
00026 
00027   return tnode;
00028 }
00029 
00030 
00031 /**
00032   * \brief List is always a list node with right child another
00033   * list node or nil.
00034   */
00035 
00036 Rlp_Formula_t *
00037 make2eltlist (Rlp_Formula_t * a, Rlp_Formula_t * b)
00038 {
00039   Rlp_Formula_t *tnode;
00040 
00041   tnode = (Rlp_Formula_t *) malloc (sizeof (Rlp_Formula_t));
00042   tnode->type = Rlp_List_c;     // need to ensure that Rlp_List_c is 0
00043   tnode->lchild = a;
00044 
00045   tnode->rchild = (Rlp_Formula_t *) malloc (sizeof (Rlp_Formula_t));
00046   tnode->rchild->type = Rlp_List_c;
00047   tnode->rchild->rchild = NIL (Rlp_Formula_t);
00048   tnode->rchild->lchild = b;
00049 
00050   return tnode;
00051 }
00052 
00053 
00054 /** \brief LISP car function */
00055 
00056 Rlp_Formula_t *
00057 car (Rlp_Formula_t * a)
00058 {
00059   return a->lchild;
00060 }
00061 
00062 
00063 /** \brief LISP cdr function */
00064 
00065 Rlp_Formula_t *
00066 cdr (Rlp_Formula_t * a)
00067 {
00068   return a->rchild;
00069 }
00070 
00071 
00072 /** \brief print node */
00073 
00074 int
00075 node_print (Rlp_Formula_t * a)
00076 {
00077   if (a == NIL (Rlp_Formula_t))
00078     {
00079       printf ("-NIL-\n");
00080       return 0;
00081     }
00082   printf ("Type: %d, Left: %p, Right %p\n", a->type, a->lchild, a->rchild);
00083   return 0;
00084 }