diff --git a/.ikc3_history b/.ikc3_history
index dd1066c..f4faa5c 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,4 +1,3 @@
-1 + 100000000000000000000000000000000000000000000000000000000
type(1 + 100000000000000000000000000000000000000000000000000000000)
type(1 + 1)
type(255)
@@ -97,3 +96,4 @@ Time.from_str("2024-10-31 23:00:00")
Time.to_str(Time.from_str("2024-10-31 23:00:00"))
List.map([1, 2, 3], fn (x) { x * 2 })
Facts.collect_with(Facts.env_db(), [[op = ?, :is_a, :operator]], fn (fact) { op })
+1 + 1
diff --git a/lib/kc3/0.1/facts.kc3 b/lib/kc3/0.1/facts.kc3
index f25c2b7..6a7eb1c 100644
--- a/lib/kc3/0.1/facts.kc3
+++ b/lib/kc3/0.1/facts.kc3
@@ -21,7 +21,7 @@ defmodule Facts do
Fn, Result)
def collect_with_tags = cfn Tag "kc3_facts_collect_with_tags" (Facts,
- Tag, Tag, Tag, Fn, Result)
+ Tag, Tag, Tag, Callable, Result)
def database = cfn Ptr "facts_database" (Result)
@@ -30,12 +30,12 @@ defmodule Facts do
# env_db() -> facts
def env_db = cfn Ptr "kc3_env_db" (Result)
- def first_with = cfn Tag "kc3_facts_first_with" (Facts, List, Fn, Result)
+ def first_with = cfn Tag "kc3_facts_first_with" (Facts, List, Callable, Result)
# first_with_tags(facts, subject, predicate, object,
# fn (fact) {result}) -> result
def first_with_tags = cfn Tag "kc3_facts_first_with_tags" (Facts, Tag,
- Tag, Tag, Fn, Result)
+ Tag, Tag, Callable, Result)
def open = cfn Sw "facts_open_file" (Facts, Str)
@@ -46,7 +46,7 @@ defmodule Facts do
def replace_tags = cfn Bool "kc3_facts_replace_tags" (Facts, Tag, Tag, Tag, Result)
# with(facts, [[?, ?, ?]], fn (fact) {result}) -> result
- def with = cfn Tag "kc3_facts_with" (Facts, List, Fn, Result)
+ def with = cfn Tag "kc3_facts_with" (Facts, List, Callable, Result)
# with_macro(facts, [[?, ?, ?]], do result end) -> result
def with_macro = cfn_macro Tag "kc3_facts_with_macro" (Tag, Tag, Tag, Result)
@@ -54,10 +54,10 @@ defmodule Facts do
# with_tags(facts, subject, predicate, object,
# fn (fact) {result}) -> result
def with_tags = cfn Tag "kc3_facts_with_tags" (Facts, Tag, Tag, Tag,
- Fn, Result)
+ Callable, Result)
# with_tuple(facts, tuple, fn (fact) {result}) -> result
- def with_tuple = cfn Tag "kc3_facts_with_tuple" (Facts, Tuple, Fn,
+ def with_tuple = cfn Tag "kc3_facts_with_tuple" (Facts, Tuple, Callable,
Result)
end
diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index 38045c1..682c4dc 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -34,7 +34,7 @@ defmodule List do
([first], sep, acc) { str(reverse([first | acc])) }
}
- def map = cfn List "list_map" (List, Fn, Result)
+ def map = cfn List "list_map" (List, Callable, Result)
def reverse = fn {
(x) { reverse(x, []) }
@@ -44,6 +44,6 @@ defmodule List do
def sort = cfn List "list_sort" (List, Result)
- def sort_by = cfn List "list_sort_by" (List, Fn, Result)
+ def sort_by = cfn List "list_sort_by" (List, Callable, Result)
end
diff --git a/libkc3/buf_parse.c b/libkc3/buf_parse.c
index 468d77f..479f424 100644
--- a/libkc3/buf_parse.c
+++ b/libkc3/buf_parse.c
@@ -1114,7 +1114,7 @@ sw buf_parse_call_paren (s_buf *buf, s_call *dest)
return r;
}
-sw buf_parse_callable (s_buf *buf, s_callable *dest)
+sw buf_parse_callable (s_buf *buf, p_callable *dest)
{
sw r;
s_callable tmp = {0};
@@ -1123,12 +1123,14 @@ sw buf_parse_callable (s_buf *buf, s_callable *dest)
if ((r = buf_parse_cfn(buf, &tmp.data.cfn)) > 0) {
tmp.type = CALLABLE_CFN;
tmp.reference_count = 1;
- *dest = tmp;
+ *dest = alloc(sizeof(s_callable));
+ **dest = tmp;
}
else if ((r = buf_parse_fn(buf, &tmp.data.fn)) > 0) {
tmp.type = CALLABLE_FN;
tmp.reference_count = 1;
- *dest = tmp;
+ *dest = alloc(sizeof(s_callable));
+ **dest = tmp;
}
return r;
}
@@ -3857,7 +3859,7 @@ sw buf_parse_tag_callable (s_buf *buf, s_tag *dest)
sw r;
assert(buf);
assert(dest);
- if ((r = buf_parse_callable(buf, dest->data.callable)) > 0)
+ if ((r = buf_parse_callable(buf, &dest->data.callable)) > 0)
dest->type = TAG_CALLABLE;
return r;
}
diff --git a/libkc3/buf_parse.h b/libkc3/buf_parse.h
index ee5b8db..190f80a 100644
--- a/libkc3/buf_parse.h
+++ b/libkc3/buf_parse.h
@@ -55,7 +55,7 @@ sw buf_parse_call_op (s_buf *buf, s_call *dest);
sw buf_parse_call_op_rec (s_buf *buf, s_call *dest, sw min_precedence);
sw buf_parse_call_op_unary (s_buf *buf, s_call *dest);
sw buf_parse_call_paren (s_buf *buf, s_call *dest);
-sw buf_parse_callable (s_buf *buf, s_callable *dest);
+sw buf_parse_callable (s_buf *buf, p_callable *dest);
sw buf_parse_cast (s_buf *buf, s_call *dest);
sw buf_parse_cfn (s_buf *buf, s_cfn *dest);
sw buf_parse_character (s_buf *buf, character *dest);
diff --git a/libkc3/env.c b/libkc3/env.c
index c8ddacd..3c91990 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -600,7 +600,7 @@ bool env_eval_call (s_env *env, s_call *call, s_tag *dest)
err_write_1("\n");
result = false;
}
- result = env_eval_callable(env, c.callable, dest);
+ result = env_eval_call_callable(env, &c, dest);
call_clean(&c);
return result;
}
@@ -648,6 +648,28 @@ bool env_eval_call_callable (s_env *env, const s_call *call,
return false;
}
+bool env_eval_call_callable_args (s_env *env,
+ s_callable *callable,
+ s_list *arguments,
+ s_tag *dest)
+{
+ switch (callable->type) {
+ case CALLABLE_CFN:
+ return env_eval_call_cfn_args(env, &callable->data.cfn,
+ arguments, dest);
+ case CALLABLE_FN:
+ return env_eval_call_fn_args(env, &callable->data.fn,
+ arguments, dest);
+ case CALLABLE_VOID:
+ err_puts("env_eval_call_callable_args: CALLABLE_VOID");
+ assert(! "env_eval_call_callable_args: CALLABLE_VOID");
+ return false;
+ }
+ err_puts("env_eval_call_callable_args: unknown callable type");
+ assert(! "env_eval_call_callable_args: unknown callable type");
+ return false;
+}
+
bool env_eval_call_cfn_args (s_env *env, s_cfn *cfn, s_list *arguments,
s_tag *dest)
{
@@ -2197,7 +2219,7 @@ s_tag * env_facts_collect_with_tags (s_env *env, s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback,
+ s_callable *callback,
s_tag *dest)
{
s_list *arguments;
@@ -2226,9 +2248,9 @@ s_tag * env_facts_collect_with_tags (s_env *env, s_facts *facts,
if (! fact_w_init_fact(fact_w, fact))
goto clean;
*l = list_new(NULL);
- if (! env_eval_call_fn_args(env, callback, arguments, &(*l)->tag)) {
+ if (! env_eval_call_callable_args(env, callback, arguments,
+ &(*l)->tag))
goto clean;
- }
l = &(*l)->next.data.list;
}
ok:
@@ -2247,7 +2269,7 @@ s_tag * env_facts_collect_with_tags (s_env *env, s_facts *facts,
s_tag * env_facts_first_with (s_env *env, s_facts *facts,
s_list **spec,
- s_fn *callback, s_tag *dest)
+ s_callable *callback, s_tag *dest)
{
s_list *arguments;
s_facts_with_cursor cursor = {0};
@@ -2274,7 +2296,7 @@ s_tag * env_facts_first_with (s_env *env, s_facts *facts,
goto ok;
if (! fact_w_init_fact(fact_w, fact))
goto clean;
- if (! env_eval_call_fn_args(env, callback, arguments, &tmp)) {
+ if (! env_eval_call_callable_args(env, callback, arguments, &tmp)) {
goto clean;
}
ok:
@@ -2295,7 +2317,7 @@ s_tag * env_facts_first_with (s_env *env, s_facts *facts,
s_tag * env_facts_first_with_tags (s_env *env, s_facts *facts,
s_tag *subject, s_tag *predicate,
- s_tag *object, s_fn *callback,
+ s_tag *object, s_callable *callback,
s_tag *dest)
{
s_list *arguments;
@@ -2325,7 +2347,7 @@ s_tag * env_facts_first_with_tags (s_env *env, s_facts *facts,
goto ok;
if (! fact_w_init_fact(fact_w, fact))
goto clean;
- if (! env_eval_call_fn_args(env, callback, arguments, &tmp)) {
+ if (! env_eval_call_callable_args(env, callback, arguments, &tmp)) {
goto clean;
}
ok:
@@ -2340,7 +2362,7 @@ s_tag * env_facts_first_with_tags (s_env *env, s_facts *facts,
}
s_tag * env_facts_with (s_env *env, s_facts *facts, s_list **spec,
- s_fn *callback, s_tag *dest)
+ s_callable *callback, s_tag *dest)
{
s_list *arguments;
s_facts_with_cursor cursor = {0};
@@ -2361,7 +2383,7 @@ s_tag * env_facts_with (s_env *env, s_facts *facts, s_list **spec,
break;
tag_clean(&tmp);
fact_w_init_fact(fact_w, fact);
- if (! env_eval_call_fn_args(env, callback, arguments, &tmp)) {
+ if (! env_eval_call_callable_args(env, callback, arguments, &tmp)) {
fact_w_clean(fact_w);
goto clean;
}
@@ -2429,7 +2451,7 @@ s_tag * env_facts_with_macro (s_env *env, s_tag *facts_tag, s_tag *spec_tag,
s_tag * env_facts_with_tags (s_env *env, s_facts *facts, s_tag *subject,
s_tag *predicate, s_tag *object,
- s_fn *callback, s_tag *dest)
+ s_callable *callback, s_tag *dest)
{
s_list *arguments;
s_facts_cursor cursor = {0};
@@ -2453,7 +2475,7 @@ s_tag * env_facts_with_tags (s_env *env, s_facts *facts, s_tag *subject,
fact_w_clean(fact_w);
if (! fact_w_init_fact(fact_w, fact))
goto clean;
- if (! env_eval_call_fn_args(env, callback, arguments, &tmp)) {
+ if (! env_eval_call_callable_args(env, callback, arguments, &tmp)) {
fact_w_clean(fact_w);
goto clean;
}
diff --git a/libkc3/env.h b/libkc3/env.h
index 9137218..8da02da 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -67,25 +67,27 @@ s_tag * env_facts_collect_with_tags (s_env *env, s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback,
+ s_callable *callback,
s_tag *dest);
s_tag * env_facts_first_with (s_env *env, s_facts *facts,
- s_list **spec, s_fn *callback,
+ s_list **spec,
+ s_callable *callback,
s_tag *dest);
s_tag * env_facts_first_with_tags (s_env *env, s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback, s_tag *dest);
+ s_callable *callback,
+ s_tag *dest);
s_tag * env_facts_with (s_env *env, s_facts *facts,
- s_list **spec, s_fn *callback,
+ s_list **spec, s_callable *callback,
s_tag *dest);
s_tag * env_facts_with_macro (s_env *env, s_tag *facts_tag,
s_tag *spec_tag, s_tag *block_tag,
s_tag *dest);
s_tag * env_facts_with_tags (s_env *env, s_facts *facts,
s_tag *subject, s_tag *predicate,
- s_tag *object, s_fn *callback,
+ s_tag *object, s_callable *callback,
s_tag *dest);
s_tag * env_ident_get (s_env *env, const s_ident *ident,
s_tag *dest);
@@ -155,8 +157,10 @@ bool env_eval_call (s_env *env, s_call *call,
s_tag *dest);
bool env_eval_call_arguments (s_env *env, s_list *args,
s_list **dest);
+bool env_eval_call_callable (s_env *env, const s_call *call,
+ s_tag *dest);
bool env_eval_call_callable_args (s_env *env,
- const s_callable *callable,
+ s_callable *callable,
s_list *arguments,
s_tag *dest);
bool env_eval_call_cfn_args (s_env *env, s_cfn *cfn, s_list *arguments,
diff --git a/libkc3/hash.c b/libkc3/hash.c
index afae8ab..d69bade 100644
--- a/libkc3/hash.c
+++ b/libkc3/hash.c
@@ -26,7 +26,6 @@
{ \
const char t[] = #type; \
assert(hash); \
- assert(x); \
return hash_update(hash, t, sizeof(t)) && \
hash_update(hash, &x, sizeof(x)); \
}
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index 87446ee..7d6c4f4 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -258,25 +258,26 @@ s_tag * kc3_facts_collect_with_tags (s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback,
+ p_callable *callback,
s_tag *dest)
{
return env_facts_collect_with_tags(&g_kc3_env, facts, subject,
- predicate, object, callback, dest);
+ predicate, object, *callback,
+ dest);
}
s_tag * kc3_facts_first_with (s_facts *facts, s_list **spec,
- s_fn *callback, s_tag *dest)
+ p_callable *callback, s_tag *dest)
{
- return env_facts_first_with(&g_kc3_env, facts, spec, callback, dest);
+ return env_facts_first_with(&g_kc3_env, facts, spec, *callback, dest);
}
s_tag * kc3_facts_first_with_tags (s_facts *facts, s_tag *subject,
s_tag *predicate, s_tag *object,
- s_fn *callback, s_tag *dest)
+ p_callable *callback, s_tag *dest)
{
return env_facts_first_with_tags(&g_kc3_env, facts, subject,
- predicate, object, callback, dest);
+ predicate, object, *callback, dest);
}
uw * kc3_facts_next_id (uw *dest)
@@ -312,9 +313,9 @@ bool * kc3_facts_replace_tags (s_facts *facts,
}
s_tag * kc3_facts_with (s_facts *facts, s_list **spec,
- s_fn *callback, s_tag *dest)
+ p_callable *callback, s_tag *dest)
{
- return env_facts_with(&g_kc3_env, facts, spec, callback, dest);
+ return env_facts_with(&g_kc3_env, facts, spec, *callback, dest);
}
s_tag * kc3_facts_with_macro (s_tag *facts_tag, s_tag *spec_tag,
@@ -325,14 +326,14 @@ s_tag * kc3_facts_with_macro (s_tag *facts_tag, s_tag *spec_tag,
s_tag * kc3_facts_with_tags (s_facts *facts, s_tag *subject,
s_tag *predicate, s_tag *object,
- s_fn *callback, s_tag *dest)
+ p_callable *callback, s_tag *dest)
{
return env_facts_with_tags(&g_kc3_env, facts, subject, predicate,
- object, callback, dest);
+ object, *callback, dest);
}
s_tag * kc3_facts_with_tuple (s_facts *facts, s_tuple *tuple,
- s_fn *callback, s_tag *dest)
+ p_callable *callback, s_tag *dest)
{
assert(facts);
assert(tuple);
@@ -344,8 +345,8 @@ s_tag * kc3_facts_with_tuple (s_facts *facts, s_tuple *tuple,
return NULL;
}
return env_facts_with_tags(&g_kc3_env, facts, tuple->tag,
- tuple->tag + 1, tuple->tag + 2, callback,
- dest);
+ tuple->tag + 1, tuple->tag + 2,
+ *callback, dest);
}
s_tag * kc3_quote_cfn (const s_sym **sym, s_tag *dest)
diff --git a/libkc3/kc3_main.h b/libkc3/kc3_main.h
index e453cd5..9117df1 100644
--- a/libkc3/kc3_main.h
+++ b/libkc3/kc3_main.h
@@ -41,7 +41,8 @@ s_tag * kc3_facts_first_with_tags (s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback, s_tag *dest);
+ p_callable *callback,
+ s_tag *dest);
uw * kc3_facts_next_id (uw *dest);
s_str * kc3_getenv (const s_str *name, s_str *dest);
void kc3_license (void);
@@ -80,13 +81,14 @@ s_tag * kc3_facts_collect_with_tags (s_facts *facts,
s_tag *subject,
s_tag *predicate,
s_tag *object,
- s_fn *callback,
+ p_callable *callback,
s_tag *dest);
s_tag * kc3_facts_first_with (s_facts *facts, s_list **spec,
- s_fn *callback, s_tag *dest);
+ p_callable *callback, s_tag *dest);
s_tag * kc3_facts_first_with_tags (s_facts *facts, s_tag *subject,
s_tag *predicate, s_tag *object,
- s_fn *callback, s_tag *dest);
+ p_callable *callback,
+ s_tag *dest);
bool * kc3_facts_remove_tags (s_facts *facts,
s_tag *subject,
s_tag *predicate,
@@ -98,12 +100,12 @@ bool * kc3_facts_replace_tags (s_facts *facts,
s_tag *object,
bool *dest);
s_tag * kc3_facts_with (s_facts *facts, s_list **spec,
- s_fn *callback, s_tag *dest);
+ p_callable *callback, s_tag *dest);
s_tag * kc3_facts_with_macro (s_tag *facts_tag, s_tag *spec_tag,
s_tag *block_tag, s_tag *dest);
s_tag * kc3_facts_with_tags (s_facts *facts, s_tag *subject,
s_tag *predicate, s_tag *object,
- s_fn *callback, s_tag *dest);
+ p_callable *callback, s_tag *dest);
s_tag * kc3_identity (s_tag *tag, s_tag *dest);
s_tag * kc3_integer_reduce (s_tag *tag, s_tag *dest);
s_tag * kc3_let (s_tag *vars, s_tag *tag, s_tag *dest);
diff --git a/libkc3/list.c b/libkc3/list.c
index 2efae1f..869bd24 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -60,7 +60,7 @@ void list_f_clean (s_list **list)
l = list_delete(l);
}
-s_list ** list_filter (s_list **list, s_callable *function,
+s_list ** list_filter (s_list **list, p_callable *function,
s_list **dest)
{
s_list *arg;
@@ -76,7 +76,7 @@ s_list ** list_filter (s_list **list, s_callable *function,
if (! tag_copy(&arg->tag, &l->tag))
goto ko;
*tail = list_new(NULL);
- if (! eval_callable_call(function, arg, &(*tail)->tag))
+ if (! eval_callable_call(*function, arg, &(*tail)->tag))
goto ko;
if ((*tail)->tag.type == TAG_VOID)
*tail = list_delete(*tail);
@@ -233,7 +233,7 @@ sw list_length (const s_list *list)
return length;
}
-s_list ** list_map (s_list **list, s_callable *function,
+s_list ** list_map (s_list **list, p_callable *function,
s_list **dest)
{
s_list *arg;
@@ -249,7 +249,7 @@ s_list ** list_map (s_list **list, s_callable *function,
if (! tag_copy(&arg->tag, &l->tag))
goto ko;
*tail = list_new(NULL);
- if (! eval_callable_call(function, arg, &(*tail)->tag))
+ if (! eval_callable_call(*function, arg, &(*tail)->tag))
goto ko;
tail = &(*tail)->next.data.list;
l = list_next(l);
@@ -395,7 +395,7 @@ s_list ** list_sort (s_list **list, s_list **dest)
return dest;
}
-s_list ** list_sort_by (s_list **list, s_callable *compare,
+s_list ** list_sort_by (s_list **list, p_callable *compare,
s_list **dest)
{
s_list *arg1;
@@ -424,7 +424,7 @@ s_list ** list_sort_by (s_list **list, s_callable *compare,
goto ko;
if (! tag_init_copy(&arg2->tag, &l->tag))
goto ko;
- if (! eval_callable_call(compare, arg1, &tag))
+ if (! eval_callable_call(*compare, arg1, &tag))
goto ko;
tag_void(&arg1->tag);
tag_void(&arg2->tag);
diff --git a/libkc3/list.h b/libkc3/list.h
index 47e0c5d..1893803 100644
--- a/libkc3/list.h
+++ b/libkc3/list.h
@@ -61,11 +61,11 @@ s_list ** list_tail (s_list **list);
s_tuple * list_to_tuple_reverse (const s_list *list, s_tuple *dest);
/* Operators */
-s_list ** list_filter (s_list **list, s_callable *function,
+s_list ** list_filter (s_list **list, p_callable *function,
s_list **dest);
-s_list ** list_map (s_list **list, s_callable *function, s_list **dest);
+s_list ** list_map (s_list **list, p_callable *function, s_list **dest);
s_list ** list_remove_void (s_list **list);
-s_list ** list_sort_by (s_list **list, s_callable *compare,
+s_list ** list_sort_by (s_list **list, p_callable *compare,
s_list **dest);
s_array * list_to_array (s_list *list, const s_sym *type,
s_array *dest);