From 7eba3f7dbcef5ecf05d6d05e1c2fbd323d7898df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Sat, 9 Feb 2019 03:01:53 +0100 Subject: Add some more scheme bindings. --- scheme.scm.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 12 deletions(-) (limited to 'scheme.scm.c') diff --git a/scheme.scm.c b/scheme.scm.c index 2c06dd3d..d148a435 100644 --- a/scheme.scm.c +++ b/scheme.scm.c @@ -20,7 +20,7 @@ SCM_DEFINE (make_calendar, "make-calendar", 1, 0, 0, vcomponent* cal = (vcomponent*) scm_gc_malloc ( sizeof(*cal), "calendar"); - INIT(vcomponent, cal); + INIT(vcomponent, cal, "ROOT"); char* p = scm_to_utf8_stringn(path, NULL); read_vcalendar(cal, p); @@ -31,8 +31,20 @@ SCM_DEFINE (make_calendar, "make-calendar", 1, 0, 0, } -static SCM scm_from_strbuf(strbuf* s) { - return scm_from_utf8_stringn (s->mem, s->len - 1); +SCM scm_from_strbuf(strbuf* s) { + return scm_from_utf8_stringn (s->mem, s->len - 2); +} + +SCM scm_from_llist(LLIST(strbuf)* lst) { + + SCM llist = SCM_EOL; + + for ( LINK(strbuf)* n = FIRST(lst); + n->after != NULL; + n = n->after) { + llist = scm_cons(scm_from_strbuf(n->value), llist); + } + return llist; } SCM_DEFINE (calendar_get_attr, "calendar-get-attr", 3, 0, 0, @@ -49,17 +61,39 @@ SCM_DEFINE (calendar_get_attr, "calendar-get-attr", 3, 0, 0, if (c == NULL) return SCM_BOOL_F; - SCM llist = SCM_EOL; + return scm_from_llist(&c->vals); +} - // TODO actuall iterators - // TODO this reverses the list - for ( LINK(strbuf)* n = FIRST(&c->vals); - n->after != NULL; - n = n->after) { - llist = scm_cons(scm_from_strbuf(n->value), llist); +SCM scm_from_trie_node(TRIE_NODE(content_line)* node) { + + TRIE_NODE(content_line)* n = node->child; + SCM childs = SCM_EOL; + while (n != NULL) { + childs = scm_cons(scm_from_trie_node(n), childs); + n = n->next; } + SCM lst; + if (node->value != NULL) { + lst = scm_cons(scm_from_char(node->c), + scm_from_llist(&node->value->vals)); + } else { + lst = scm_list_1(scm_from_char(node->c)); + } + return scm_cons (lst, childs); +} - return llist; +SCM scm_from_trie(TRIE(content_line)* trie) { + return scm_from_trie_node (trie->root); +} + +SCM scm_from_vector(VECT(vcomponent)* vect) { + SCM l = SCM_EOL; + for (size_t i = 0; i < vect->length; i++) { + l = scm_cons( + scm_make_foreign_object_1 (calendar_type, GET(VECT(vcomponent))(vect, i)), + l); + } + return scm_reverse(l); } SCM_DEFINE (calendar_size, "calendar-size", 1, 0, 0, @@ -71,11 +105,37 @@ SCM_DEFINE (calendar_size, "calendar-size", 1, 0, 0, return scm_from_size_t (cal->components.length); } +SCM_DEFINE (calendar_properties, "primitive-get-properties", 1, 0, 0, + (SCM calendar), + "Returns the TRIE from a calendar object") +{ + scm_assert_foreign_object_type (calendar_type, calendar); + vcomponent* cal = scm_foreign_object_ref (calendar, 0); + return scm_from_trie(&cal->clines); +} + +SCM_DEFINE(calendar_components, "get-components", 1, 0, 0, + (SCM component), + "") +{ + scm_assert_foreign_object_type (calendar_type, component); + vcomponent* cal = scm_foreign_object_ref (component, 0); + return scm_from_vector(&cal->components); +} + +SCM_DEFINE(component_type, "component-type", 1, 0, 0, + (SCM component), + "Returns type of vcomponent#") +{ + scm_assert_foreign_object_type (calendar_type, component); + vcomponent* comp = scm_foreign_object_ref (component, 0); + return scm_from_utf8_symbol(comp->type); +} + void init_calendar () { init_calendar_type(); #ifndef SCM_MAGIC_SNARFER #include "scheme.x" #endif - } -- cgit v1.2.3