The example shows how to create and inspect symbols.
Output
$ ./symbol 0
the hash of 42 is 281474976710698
the hash of x is 562949963481760
the hash of x(42,x) is 1407374893613792
42 is equal to 42
42 is not equal to x
42 is less than x
Code
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
typedef struct {
char *string;
size_t string_n;
} string_buffer_t;
void free_string_buffer(string_buffer_t *buf) {
if (buf->string) {
free(buf->string);
buf->string = NULL;
buf->string_n = 0;
}
}
bool ret = true;
char *string;
size_t n;
if (buf->string_n < n) {
if (!(string = (char*)realloc(buf->string, sizeof(*buf->string) * n))) {
goto error;
}
buf->string = string;
buf->string_n = n;
}
printf("%s", buf->string);
goto out;
error:
ret = false;
out:
return ret;
}
int main() {
char const *error_message;
int ret = 0;
string_buffer_t buf = { NULL, 0 };
size_t size;
for (size_t i = 0; i < sizeof(symbols) / sizeof(*symbols); ++i) {
printf("the hash of ");
if (!print_symbol(symbols[i], &buf)) { goto error; }
}
assert(size == 2);
for (size_t i = 0; i < size; ++i) {
if (!print_symbol(symbols[0], &buf)) { goto error; }
if (!print_symbol(args[i], &buf)) { goto error; }
printf("\n");
}
if (!print_symbol(symbols[0], &buf)) { goto error; }
if (!print_symbol(symbols[1], &buf)) { goto error; }
printf("\n");
goto out;
error:
printf("%s\n", error_message);
out:
free_string_buffer(&buf);
return ret;
}
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, char *string, size_t size)
Get the string representation of a symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_create_id(char const *name, bool positive, clingo_symbol_t *symbol)
Construct a symbol representing an id.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_create_function(char const *name, clingo_symbol_t const *arguments, size_t arguments_size, bool positive, clingo_symbol_t *symbol)
Construct a symbol representing a function or tuple.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string_size(clingo_symbol_t symbol, size_t *size)
Get the size of the string representation of a symbol (including the terminating 0).
CLINGO_VISIBILITY_DEFAULT void clingo_set_error(clingo_error_t code, char const *message)
Set a custom error code and message in the active thread.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.