Hash :
37078995
Author :
Thomas de Grivel
Date :
2024-08-23T23:07:59
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
/**
* @file to_lisp.h
* @brief Convert Tag to Lisp notation
*
* Convert any Tag to Lisp notation, that is List for code.
*/
#include "assert.h"
#include "list.h"
#include "tag.h"
#include "to_lisp.h"
s_tag * to_lisp (const s_tag *tag, s_tag *dest)
{
assert(tag);
assert(dest);
switch (tag->type) {
case TAG_CALL:
return to_lisp_call(&tag->data.call, dest);
case TAG_LIST:
return to_lisp_list(tag->data.list, dest);
case TAG_TUPLE:
return to_lisp_tuple(&tag->data.tuple, dest);
default:
return tag_init_copy(dest, tag);
}
}
s_tag * to_lisp_call (const s_call *call, s_tag *dest)
{
s_tag arguments;
s_list *list;
if (! to_lisp_list(call->arguments, &arguments))
return NULL;
if (arguments.type != TAG_LIST) {
err_puts("to_lisp_call: arguments.type != TAG_LIST");
assert(! "to_lisp_call: arguments.type != TAG_LIST");
return NULL;
}
if (! (list = list_new_ident(&call->ident, arguments.data.list))) {
tag_clean(&arguments);
return NULL;
}
return tag_init_list(dest, list);
}
s_tag * to_lisp_list (const s_list *list, s_tag *dest)
{
const s_list *list_i;
s_list **tail;
s_list *tmp;
tmp = NULL;
tail = &tmp;
list_i = list;
while (list_i) {
*tail = list_new(NULL);
if (! to_lisp(&list_i->tag, &(*tail)->tag))
goto ko;
tail = &(*tail)->next.data.list;
list_i = list_next(list_i);
}
return tag_init_list(dest, tmp);
ko:
list_delete_all(tmp);
return NULL;
}
s_tag * to_lisp_tuple (const s_tuple *tuple, s_tag *dest)
{
uw i;
s_tag tmp = {0};
if (! tag_init_tuple(&tmp, tuple->count))
return NULL;
i = 0;
while (i < tuple->count) {
if (! to_lisp(tuple->tag + i, tmp.data.tuple.tag + i))
goto ko;
i++;
}
*dest = tmp;
return dest;
ko:
tag_clean(&tmp);
return NULL;
}