Hash :
72907a3f
Author :
Thomas de Grivel
Date :
2025-03-04T16:09:13
wip operators and struct/pstruct pass by ref
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
/* 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 <stdlib.h>
#include "alloc.h"
#include "assert.h"
#include "callable.h"
#include "op.h"
#include "sym.h"
void op_clean (s_op *op)
{
assert(op);
p_callable_clean(&op->callable);
}
void op_delete (s_op *op)
{
assert(op);
if (op->ref_count <= 0) {
err_puts("op_delete: invalid reference count");
assert(! "op_delete: invalid reference count");
return;
}
if (! --op->ref_count) {
op_clean(op);
free(op);
}
}
s_op * op_init (s_op *op)
{
s_op tmp = {0};
tmp.ref_count = 1;
*op = tmp;
return op;
}
s_op * op_init_cast (s_op *op, const s_sym * const *type, s_tag *src)
{
s_op *tmp;
assert(op);
assert(type);
assert(*type);
assert(src);
if (!type || *type != &g_sym_KC3_Op)
return NULL;
switch (src->type) {
case TAG_PTR:
tmp = src->data.ptr.p;
return op_init_copy(op, tmp);
default:
break;
}
err_puts("op_init_cast: cannot cast");
assert(! "op_init_cast: cannot cast");
return NULL;
}
s_op * op_init_copy (s_op *op, const s_op *src)
{
s_op tmp = {0};
tmp.sym = src->sym;
tmp.arity = src->arity;
tmp.special = src->special;
tmp.precedence = src->precedence;
tmp.associativity = src->associativity;
tmp.callable = callable_new_ref(src->callable);
tmp.ref_count = 1;
*op = tmp;
return op;
}
s_op * op_new (void)
{
s_op *op;
op = alloc(sizeof(s_op));
if (! op)
return NULL;
if (! op_init(op)) {
free(op);
return NULL;
}
return op;
}
s_op * op_new_copy (const s_op *src)
{
s_op *op;
op = alloc(sizeof(s_op));
if (! op)
return NULL;
if (! op_init_copy(op, src)) {
free(op);
return NULL;
}
return op;
}
s_op * op_new_ref (s_op *op)
{
assert(op);
if (op->ref_count <= 0) {
err_puts("op_new_ref: invalid reference count");
assert(! "op_new_ref: invalid reference count");
return NULL;
}
op->ref_count++;
return op;
}
s_op * op_set_callable (s_op *op, s_callable *callable)
{
op->callable = callable_new_ref(callable);
callable_set_special(op->callable, op->special);
return op;
}
s_op * op_set_special (s_op *op, bool special)
{
op->special = special;
callable_set_special(op->callable, special);
return op;
}