Branch
Hash :
755b4ec4
Author :
Thomas de Grivel
Date :
2025-09-05T13:20:41
wip call.kc3
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* kc3
* Copyright from 2022 to 2025 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.
*/
#include <string.h>
#include "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_parse.h"
#include "call.h"
#include "pcall.h"
#include "sym.h"
#include "tag_type.h"
void pcall_clean (p_call *pcall)
{
assert(pcall);
call_delete(*pcall);
}
s_ident * pcall_ident (p_call *pcall, s_ident *dest)
{
if (! pcall || ! dest) {
err_puts("pcall_ident: invalid argument");
assert(! "pcall_ident: invalid argument");
return NULL;
}
*dest = (*pcall)->ident;
return dest;
}
p_call * pcall_init (p_call *pcall)
{
p_call tmp = NULL;
if (! (tmp = call_new()))
return NULL;
*pcall = tmp;
return pcall;
}
p_call * pcall_init_1 (p_call *pcall, const char *p)
{
s_buf buf;
uw len;
sw r;
len = strlen(p);
buf_init_const(&buf, len, p);
buf.wpos = len;
r = buf_parse_pcall(&buf, pcall);
if (r < 0 || (uw) r != len) {
err_write_1("call_init_1: invalid call: ");
err_write_1(p);
err_write_1(": ");
err_inspect_sw(r);
err_write_1(" != ");
err_inspect_uw(len);
err_write_1("\n");
return NULL;
}
return pcall;
}
p_call * pcall_init_call_cast (p_call *pcall, const s_sym *type)
{
p_call tmp;
if (! (tmp = alloc(sizeof(s_call))))
return NULL;
if (! call_init_call_cast(tmp, type)) {
free(tmp);
return NULL;
}
*pcall = tmp;
return pcall;
}
p_call * pcall_init_cast (p_call *pcall, p_sym *type, s_tag *tag)
{
assert(pcall);
assert(type);
assert(tag);
switch (tag->type) {
case TAG_PCALL:
return pcall_init_copy(pcall, &tag->data.pcall);
default:
break;
}
err_write_1("call_init_cast: cannot cast ");
err_write_1(tag_type_to_string(tag->type));
if (*type == &g_sym_Call)
err_puts(" to Call");
else {
err_write_1(" to ");
err_inspect_psym(type);
err_puts(" aka Call");
}
assert(! "call_init_cast: cannot cast to Call");
return NULL;
}
p_call * pcall_init_copy (p_call *pcall, p_call *src)
{
p_call tmp;
assert(pcall);
assert(src);
if (env_global()->pass_by_copy) {
if (! (tmp = call_new_copy(*src))) {
err_puts("pcall_init_copy: call_new_copy");
assert(! "pcall_init_copy: call_new_copy");
return NULL;
}
}
else {
if (! (tmp = call_new_ref(*src))) {
err_puts("pcall_init_copy: call_new_ref");
assert(! "pcall_init_copy: call_new_ref");
return NULL;
}
}
*pcall = tmp;
return pcall;
}
const s_sym ** pcall_sym (p_call *pcall, const s_sym **dest)
{
if (! pcall || ! *pcall || ! (*pcall)->ident.sym || ! dest) {
err_puts("pcall_sym: invalid argument");
assert(! "pcall_sym: invalid argument");
return NULL;
}
*dest = (*pcall)->ident.sym;
return dest;
}