Hash :
37ebe824
Author :
Thomas de Grivel
Date :
2023-08-29T14:21:55
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/* c3
* Copyright 2022,2023 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 <assert.h>
#include <string.h>
#include "buf.h"
#include "buf_inspect.h"
#include "buf_parse.h"
#include "call.h"
#include "cfn.h"
#include "facts_with.h"
#include "facts_with_cursor.h"
#include "fn.h"
#include "ident.h"
#include "list.h"
#include "tag.h"
void call_clean (s_call *call)
{
assert(call);
list_delete_all(call->arguments);
if (call->cfn)
cfn_delete(call->cfn);
if (call->fn)
fn_delete(call->fn);
}
s_call * call_copy (const s_call *src, s_call *dest)
{
assert(src);
assert(dest);
ident_copy(&src->ident, &dest->ident);
list_copy(src->arguments, &dest->arguments);
dest->cfn = src->cfn;
dest->fn = src->fn;
return dest;
}
bool call_get (s_call *call, s_facts *facts)
{
s_facts_with_cursor cursor;
s_tag tag_cfn;
s_tag tag_fn;
s_tag tag_ident;
s_tag tag_is_a;
s_tag tag_macro;
s_tag tag_module_name;
s_tag tag_special_operator;
s_tag tag_sym;
s_tag tag_symbol;
s_tag tag_var;
tag_init_1( &tag_cfn, ":cfn");
tag_init_1( &tag_fn, ":fn");
tag_init_ident(&tag_ident, &call->ident);
tag_init_1( &tag_is_a, ":is_a");
tag_init_1( &tag_macro, ":macro");
tag_init_sym( &tag_module_name, call->ident.module_name);
tag_init_1( &tag_special_operator, ":special_operator");
tag_init_sym( &tag_sym, call->ident.sym);
tag_init_1( &tag_symbol, ":symbol");
tag_init_var( &tag_var);
facts_with_cursor_clean(&cursor);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_module_name,
&tag_symbol, &tag_ident, /* module exports symbol */
NULL, NULL });
if (! facts_with_cursor_next(&cursor)) {
warnx("symbol %s not found in module %s",
call->ident.sym->str.ptr.ps8,
call->ident.module_name->str.ptr.ps8);
facts_with_cursor_clean(&cursor);
return false;
}
facts_with_cursor_clean(&cursor);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_fn, &tag_var,
NULL, NULL });
if (facts_with_cursor_next(&cursor)) {
if (tag_var.type != TAG_FN)
errx(1, "%s.%s is not a function",
call->ident.module_name->str.ptr.ps8,
call->ident.sym->str.ptr.ps8);
call->fn = fn_new_copy(&tag_var.data.fn);
}
facts_with_cursor_clean(&cursor);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_cfn, &tag_var,
NULL, NULL });
if (facts_with_cursor_next(&cursor)) {
if (tag_var.type != TAG_CFN)
errx(1, "%s.%s is not a C function",
call->ident.module_name->str.ptr.ps8,
call->ident.sym->str.ptr.ps8);
call->cfn = cfn_new_copy(&tag_var.data.cfn);
}
facts_with_cursor_clean(&cursor);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_is_a, &tag_macro, NULL, NULL });
if (facts_with_cursor_next(&cursor)) {
if (call->fn)
call->fn->macro = true;
if (call->cfn)
call->cfn->macro = true;
}
facts_with_cursor_clean(&cursor);
facts_with(facts, &cursor, (t_facts_spec) {
&tag_ident, &tag_is_a, &tag_special_operator, NULL, NULL});
if (facts_with_cursor_next(&cursor)) {
if (call->fn)
call->fn->special_operator = true;
if (call->cfn)
call->cfn->special_operator = true;
}
facts_with_cursor_clean(&cursor);
return true;
}
s_call * call_init (s_call *call)
{
assert(call);
bzero(call, sizeof(s_call));
return call;
}
s_call * call_init_1 (s_call *call, const s8 *p)
{
s_buf buf;
buf_init_1(&buf, p);
if (buf_parse_call(&buf, call) != (sw) strlen(p))
errx(1, "invalid call: %s", p);
buf_clean(&buf);
return call;
}
s_call * call_init_op (s_call *call)
{
assert(call);
bzero(call, sizeof(s_call));
call->arguments = list_new(NULL, list_new(NULL, NULL));
return call;
}
s_call * call_init_op_unary (s_call *call)
{
assert(call);
bzero(call, sizeof(s_call));
call->arguments = list_new(NULL, NULL);
return call;
}
s_str * call_inspect (const s_call *call, s_str *dest)
{
sw size;
s_buf tmp;
size = buf_inspect_call_size(call);
if (size < 0) {
assert(! "error");
return NULL;
}
buf_init_alloc(&tmp, size);
buf_inspect_call(&tmp, call);
assert(tmp.wpos == tmp.size);
return buf_to_str(&tmp, dest);
}