loa

Virtual machine for the Logic of Assumptions
git clone git://juanmeleiro.mat.br/loa
Log | Files | Refs

commit 2b705fa259384af58c6450f412fe47efd5fe3431
parent 9d7c856c2d0f4190cf577369e93da9f5c9444f18
Author: Juan F. Meleiro <juan@juanmeleiro.mat.br>
Date:   Fri, 26 Apr 2024 18:42:04 -0300

Implement function for displaying a gardener's state

Diffstat:
Acoding/.gitignore | 2++
Acoding/default.test.do | 7+++++++
Mcoding/gardener.c | 28+++++++++++++++++++---------
Mcoding/gardener.h | 3+++
Mcoding/gardener.test.c | 20++++++++++----------
Mcoding/metaschema.c | 2+-
Mcoding/model.c | 2+-
Mcoding/model.test.c | 7+++++++
Mcoding/schema.c | 22+++++++++++++++++++++-
Mcoding/schema.h | 7++++++-
Mcoding/schema.test.c | 32+++++++++++++++++++++++++++++---
11 files changed, 106 insertions(+), 26 deletions(-)

diff --git a/coding/.gitignore b/coding/.gitignore @@ -0,0 +1 @@ +*.test +\ No newline at end of file diff --git a/coding/default.test.do b/coding/default.test.do @@ -0,0 +1,7 @@ +#!/bin/sh + +headers=*.h +src=$(ls *.c | grep -v .test.c) +redo-ifchange $src $headers +cc -g -fmax-errors=1 $src $1.c -o $3 >&2 +./$3 >&2 || exit 1 diff --git a/coding/gardener.c b/coding/gardener.c @@ -1,4 +1,5 @@ #include <assert.h> +#include <stdio.h> #include "gardener.h" @@ -109,16 +110,25 @@ done(gardener* g) return res; } -/* void -sup(gardener* g, symbol key, symbol constructor) +show(FILE *f, gardener *g) { - if (at_ground(g)) { - tree *sup = new_node(constructor); - set(sup, key, get_cur_tree(g)); - set_cur_tree(g, sup); - } else { - set_error(g); + schema *s = get_cur_schema(g); + tree *cur = get_cur_tree(g); + symbol head = get_head(get_cur_tree(g)); + bool err = get_error(g); + + fprintf(f, "schema %s\n", repr(get_name(s))); + fprintf(f, "head %s\n", repr(head)); + size_t num = get_amount_of_required_keys(s, head); + symbol *keys = get_required_keys(s, head); + for (size_t i = 0; i < num; i++) { + if (!get_subtree(cur, keys[i])) + if (takes_leaf(s, head, keys[i])) + fprintf(f, "goal symbol %s\n", repr(keys[i])); + else + fprintf(f, "goal %s %s\n", repr(get_name(get_subschema(s, head, keys[i]))), repr(keys[i])); } + if (err) fprintf(f, "ERROR\n"); } -*/ + diff --git a/coding/gardener.h b/coding/gardener.h @@ -1,3 +1,4 @@ +#include <stdio.h> #include <stdbool.h> #include "schema.h" @@ -11,3 +12,5 @@ void sub(gardener*, symbol key, symbol constructor); bool done(gardener*); void sup(gardener*, symbol key, symbol constructor); void start(gardener*, symbol constructor); + +void show(FILE *f, gardener*); diff --git a/coding/gardener.test.c b/coding/gardener.test.c @@ -12,7 +12,7 @@ test_empty_constructor_success(void) bool res = true; symbol c = intern("c"); - schema *s = new_schema(); + schema *s = new_schema(intern("test")); add_constructor(s, c); gardener *g = new_gardener(s); @@ -30,7 +30,7 @@ test_empty_constructor_empty_tree_fail(void) bool res = true; symbol c = intern("c"); - schema *s = new_schema(); + schema *s = new_schema(intern("test")); add_constructor(s, c); gardener *g = new_gardener(s); @@ -44,11 +44,11 @@ test_double_deep_schema_fail(void) { bool res = true; - schema *s = new_schema(); + schema *s = new_schema(intern("test0")); symbol c = intern("c"); add_constructor(s, c); - schema *t = new_schema(); + schema *t = new_schema(intern("test1")); symbol d = intern("d"); add_constructor(t, d); @@ -73,10 +73,10 @@ test_double_deep_schema_success(void) symbol d = intern("d"); symbol k = intern("k"); - schema *s = new_schema(); + schema *s = new_schema(intern("test0")); add_constructor(s, c); - schema *t = new_schema(); + schema *t = new_schema(intern("test1")); add_constructor(t, d); assign_subschema(s, c, k, t); @@ -100,10 +100,10 @@ test_triple_deep_schema_fail(void) symbol d = intern("d"); symbol k = intern("k"); - schema *s = new_schema(); + schema *s = new_schema(intern("test0")); add_constructor(s, c); - schema *t = new_schema(); + schema *t = new_schema(intern("test1")); add_constructor(t, d); assign_subschema(s, c, k, t); @@ -124,11 +124,11 @@ test_triple_deep_schema_success(void) { bool res = true; - schema *s = new_schema(); + schema *s = new_schema(intern("test0")); symbol c = intern("c"); add_constructor(s, c); - schema *t = new_schema(); + schema *t = new_schema(intern("test1")); symbol d = intern("d"); add_constructor(t, d); diff --git a/coding/metaschema.c b/coding/metaschema.c @@ -11,7 +11,7 @@ metacompile(metaschema *m, size_t len) /* 1st: Create all schemas */ for (size_t i = 0; i < len; i++) { - schema *s = new_schema(); + schema *s = new_schema(intern(m[i].name)); assoc_set(a, intern(m[i].name), s); } diff --git a/coding/model.c b/coding/model.c @@ -9,7 +9,7 @@ schema* \ NAME ## _schema() { \ if (_ ## NAME ## _schema != NULL) return _ ## NAME ## _schema; \ - schema *it = new_schema(); \ + schema *it = new_schema(intern(#NAME)); \ _ ## NAME ## _schema = it; \ #define end_schema(NAME) \ diff --git a/coding/model.test.c b/coding/model.test.c @@ -98,6 +98,13 @@ test_use(void) start(g, I("attitude")); assert(!get_error(g)); + fill(g, I("name"), I("wft")); + sub(g, I("vars"), I("var")); + fill(g, I("name"), I("a")); + sub(g, I("rest"), I("nil")); + done(g); + // done(g); + show(stdout, g); } int diff --git a/coding/schema.c b/coding/schema.c @@ -8,6 +8,7 @@ struct schema { assoc *constructors; bool (*checker)(tree*); + symbol name; }; struct schema _LEAF = { NULL }; @@ -22,11 +23,12 @@ typedef struct constructor constructor; /* CONSTRUCTORS */ schema* -new_schema(void) +new_schema(symbol name) { schema *s = malloc(sizeof(schema)); s->constructors = new_assoc(); s->checker = NULL; + s->name = name; return s; } @@ -41,6 +43,12 @@ new_constructor() /* GETTERS AND SETTERS */ +symbol +get_name(schema*s) +{ + return s->name; +} + bool is_constructor(schema* s, symbol c) { @@ -86,6 +94,18 @@ bool (*get_check_function(schema* s))(tree*) return s->checker; } +size_t +get_amount_of_required_keys(schema *s, symbol c) +{ + return get_amount_of_keys(get_constructor(s, c)->subschemas); +} + +symbol* +get_required_keys(schema *s, symbol c) +{ + return get_keys(get_constructor(s, c)->subschemas); +} + /* REST OF INTERFACE */ void diff --git a/coding/schema.h b/coding/schema.h @@ -1,4 +1,5 @@ #include <stdbool.h> +#include <stdlib.h> #include "symbol.h" #include "tree.h" @@ -6,7 +7,7 @@ typedef struct schema schema; /* BUILDING */ -schema* new_schema(void); +schema* new_schema(symbol); void add_constructor(schema*, symbol); void assign_subschema(schema*, symbol, symbol, schema*); void mark_as_leaf(schema*, symbol, symbol); @@ -18,6 +19,10 @@ bool is_constructor(schema*, symbol); bool has_key(schema*, symbol, symbol); bool takes_leaf(schema*, symbol, symbol); bool check(schema*, tree*); +symbol get_name(schema*); + +size_t get_amount_of_required_keys(schema*, symbol); +symbol *get_required_keys(schema*, symbol); extern struct schema *LEAF; diff --git a/coding/schema.test.c b/coding/schema.test.c @@ -7,10 +7,17 @@ #include "assoc.h" bool +test_name(void) +{ + schema *s = new_schema(intern("test")); + return (get_name(s) == intern("test")); +} + +bool test_set_and_get_subschema(void) { - schema *s = new_schema(); - schema *sub = new_schema(); + schema *s = new_schema(intern("test")); + schema *sub = new_schema(intern("test")); symbol c = intern("c"); symbol a = intern("a"); @@ -23,7 +30,7 @@ test_set_and_get_subschema(void) bool test_set_and_query_leaf(void) { - schema *s = new_schema(); + schema *s = new_schema(intern("test")); symbol c = intern("c"); symbol a = intern("a"); @@ -40,6 +47,23 @@ test_leaf_exists(void) } bool +test_listing_all_keys(void) +{ + schema *s = new_schema(intern("test")); + add_constructor(s, intern("c")); + mark_as_leaf(s, intern("c"), intern("l")); + assign_subschema(s, intern("c"), intern("s"), s); + + bool good = true; + size_t num = get_amount_of_required_keys(s, intern("c")); + symbol *keys = get_required_keys(s, intern("c")); + for (size_t i = 0; i < num; i++) + good &= (has_key(s, intern("c"), keys[i])); + + return good; +} + +bool test_check(void) { } @@ -47,8 +71,10 @@ test_check(void) int main() { + assert(test_name()); assert(test_set_and_get_subschema()); assert(test_set_and_query_leaf()); assert(test_leaf_exists()); + assert(test_listing_all_keys()); return 0; }