/*
 *   Pure Data Packet system implementation. : code for handling different packet types
 *   Copyright (c) by Tom Schouten <tom@zwizwa.be>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


/* this file contains type meta data handling routines */
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <pf/list.h>
#include <pf/symbol.h>
#include <pf/packet.h>
#include <pf/stream.h>
#include <pf/type.h>
#include <pf/mem.h>
#include <pf/debug.h>


// debug
#define D if (0)


/* atom -> type symbol */
pf_symbol_t *pf_atom_typesymbol(pf_atom_t *a){
    pf_symbol_t *ts;
    PF_ASSERT(a);
    switch(a->t){
    case a_float:  ts = PF_SYM_FLOAT;   break;
    case a_int:    ts = PF_SYM_INT;     break;
    case a_symbol: ts = PF_SYM_SYMBOL;  break;
    case a_list:   ts = PF_SYM_LIST;    break;

    case a_forth_xt: ts = pf_symbol("xt"); break;

    case a_packet: 
    {
	ts = PF_SYM_INVALID;
	pf_header_t *header = pf_packet_header(a->w.w_packet);
	if (header && header->desc) ts = header->desc;
	break;
    }

    case a_atom_pointer:  ts = pf_symbol("variable"); break;
       

    case a_undef:  ts = pf_symbol("undef");   break;
    default:       ts = pf_symbol("unknown"); break;
    }
    return ts;
}

pf_atom_t *pf_atom_from_typesymbol(pf_symbol_t *t){
    pf_atom_t *a = pf_atom_new();
    a->t = a_undef;
    a->w.w_int = 0;

    if (t == PF_SYM_FLOAT) a->t = a_float;
    else if (t == PF_SYM_INT) a->t = a_int;
    else { a->t = a_packet; a->w.w_packet = pf_factory_newpacket(t);}

    return a;
}

/* convert a type to a list:
   returned list is owned by caller
   type lists can contain all atoms parsable by the list parser

   note that type lists can only contain integers and symbols, so
   the generic parser is not used.

*/ 

static pf_atom_t *atom_from_cstring(char *c){
    pf_atom_t *a = pf_atom_new();
    char *next;
    int l = strtol(c, &next, 0);
    if (0 != *next){
	a->t = a_symbol;
	a->w.w_symbol = pf_symbol(c);
    }
    else {
	a->t = a_int;
	a->w.w_int = l;
    }
    return a;
}



// parse a typesymbol to a list
pf_list_t *pf_list_from_typesymbol(pf_symbol_t *type)
{

    char *c = type->s_name;
    char *lastname = c;
    int n = 0;
    char tmp[strlen(type->s_name)+1];
    pf_list_t *l = pf_list_new();

    while(*c){
	if (*c == '/'){
	    strncpy(tmp, lastname, n);
	    tmp[n] = 0;
	    if (tmp[0] != 0){ // don't add empty symbols
		pf_list_queue_atom(l, atom_from_cstring(tmp));
	    }
	    c++;
	    lastname = c;
	    n = 0;
	}
	else{
	    c++;
	    n++;
	}
    }
    if (lastname[0] != 0){ //don't add empty symbols
	pf_list_queue_atom(l, atom_from_cstring(lastname));
    }

    return l;
}



/* convert a type list to a type symbol */
pf_symbol_t *pf_typesymbol_from_list(pf_list_t *typelist)
{

#define MAX_STRING 100 // FIXME

    char tmp[MAX_STRING+1];
    char *c = tmp;
    pf_symbol_t *type = pf_symbol("invalid");
    pf_atom_t *a = typelist->first;
    tmp[MAX_STRING] = 0; // terminate

    while (a){
	switch(a->t){
	case a_symbol: snprintf(c, MAX_STRING + tmp - c, "/%s", a->w.w_symbol->s_name); break;
	case a_int:    snprintf(c, MAX_STRING + tmp - c, "/%d", a->w.w_int); break;
	default:
	    /* invalid argument */
	    goto exit;
	}
	while (*c){
	    PF_ASSERT((c-tmp) < MAX_STRING);
	    c++; // wind
	}
	a = a->next;
    }
    type = pf_symbol(tmp + 1);
  exit:
    return type;

}


/* setup method */
void pf_type_setup(void)
{

}
