diff options
Diffstat (limited to 'old-lib/c')
| -rw-r--r-- | old-lib/c/Makefile | 19 | ||||
| -rw-r--r-- | old-lib/c/gfcc-term.c | 203 | ||||
| -rw-r--r-- | old-lib/c/gfcc-term.h | 65 | ||||
| -rw-r--r-- | old-lib/c/gfcc-tree.c | 61 | ||||
| -rw-r--r-- | old-lib/c/gfcc-tree.h | 49 |
5 files changed, 397 insertions, 0 deletions
diff --git a/old-lib/c/Makefile b/old-lib/c/Makefile new file mode 100644 index 000000000..72ac7ea79 --- /dev/null +++ b/old-lib/c/Makefile @@ -0,0 +1,19 @@ +CC = gcc +CFLAGS += -O2 -W -Wall + +.PHONY: all clean + +all: libgfcc.a + +libgfcc.a: gfcc-tree.o gfcc-term.o + ar r $@ $^ + +gfcc-tree.o: gfcc-tree.c gfcc-tree.h + $(CC) $(CFLAGS) -c -o $@ $< + +gfcc-term.o: gfcc-term.c gfcc-term.h + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + -rm -f libgfcc.a + -rm -f *.o diff --git a/old-lib/c/gfcc-term.c b/old-lib/c/gfcc-term.c new file mode 100644 index 000000000..b427479e6 --- /dev/null +++ b/old-lib/c/gfcc-term.c @@ -0,0 +1,203 @@ +#include "gfcc-term.h" + +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +static void *buffer = NULL; +static size_t current; + +extern void term_alloc_pool(size_t size) { + if (buffer == NULL) + buffer = malloc(size); + current = 0; +} + +extern void term_free_pool() { + if (buffer != NULL) + free(buffer); + buffer = NULL; +} + +extern void *term_alloc(size_t size) { + void *off = buffer + current; + current += size; + return off; +} + +static inline Term *create_term(TermType type, int n) { + Term *t = (Term*)term_alloc(sizeof(Term) + n * sizeof(Term *)); + t->type = type; + t->value.size = n; /* FIXME: hack! */ + return t; +} + +extern Term *term_array(int n, ...) { + Term *t = create_term(TERM_ARRAY, n); + va_list ap; + int i; + + va_start(ap, n); + for (i = 0; i < n; i++) { + term_set_child(t, i, va_arg(ap, Term *)); + } + va_end(ap); + + return t; +} + +extern Term *term_seq(int n, ...) { + Term *t = create_term(TERM_SEQUENCE, n); + va_list ap; + int i; + + va_start(ap, n); + for (i = 0; i < n; i++) { + term_set_child(t, i, va_arg(ap, Term *)); + } + va_end(ap); + + return t; +} + +extern Term *term_variants(int n, ...) { + Term *t = create_term(TERM_VARIANTS, n); + va_list ap; + int i; + + va_start(ap, n); + for (i = 0; i < n; i++) { + term_set_child(t, i, va_arg(ap, Term *)); + } + va_end(ap); + + return t; +} + +extern Term *term_glue(int n, ...) { + Term *t = create_term(TERM_GLUE, n); + va_list ap; + int i; + + va_start(ap, n); + for (i = 0; i < n; i++) { + term_set_child(t, i, va_arg(ap, Term *)); + } + va_end(ap); + + return t; +} + +extern Term *term_rp(Term *t1, Term *t2) { + Term *t = create_term(TERM_RECORD_PARAM, 2); + term_set_child(t, 0, t1); + term_set_child(t, 1, t2); + return t; +} + +extern Term *term_suffix(const char *pref, Term *suf) { + Term *t = create_term(TERM_SUFFIX_TABLE, 2); + term_set_child(t,0,term_str(pref)); + term_set_child(t,1,suf); + return t; +} + +extern Term *term_str(const char *s) { + Term *t = create_term(TERM_STRING, 0); + t->value.string_value = s; + return t; +} + +extern Term *term_int(int i) { + Term *t = create_term(TERM_INTEGER,0); + t->value.integer_value = i; + return t; +} + +extern Term *term_meta() { + return create_term(TERM_META, 0); +} + + + +extern Term *term_sel_int(Term *t, int i) { + switch (t->type) { + case TERM_ARRAY: + return term_get_child(t,i); + case TERM_SUFFIX_TABLE: + return term_glue(2, + term_get_child(t,0), + term_sel_int(term_get_child(t,1),i)); + case TERM_META: + return t; + default: + fprintf(stderr,"Error: term_sel_int %d %d\n", t->type, i); + exit(1); + return NULL; + } +} + +extern Term *term_sel(Term *t1, Term *t2) { + switch (t2->type) { + case TERM_INTEGER: + return term_sel_int(t1, t2->value.integer_value); + case TERM_RECORD_PARAM: + return term_sel(t1,term_get_child(t2,0)); + case TERM_META: + return term_sel_int(t1,0); + default: + fprintf(stderr,"Error: term_sel %d %d\n", t1->type, t2->type); + exit(1); + return 0; + } +} + + + +static void term_print_sep(FILE *stream, Term *t, const char *sep) { + int n = t->value.size; + int i; + + for (i = 0; i < n; i++) { + term_print(stream, term_get_child(t,i)); + if (i < n-1) { + fputs(sep, stream); + } + } +} + +extern void term_print(FILE *stream, Term *t) { + switch (t->type) { + case TERM_ARRAY: + term_print(stream, term_get_child(t,0)); + break; + case TERM_SEQUENCE: + term_print_sep(stream, t, " "); + break; + case TERM_VARIANTS: + term_print_sep(stream, t, "/"); + break; + case TERM_GLUE: + term_print_sep(stream, t, ""); + break; + case TERM_RECORD_PARAM: + term_print(stream, term_get_child(t,0)); + break; + case TERM_SUFFIX_TABLE: + term_print(stream, term_get_child(t,0)); + term_print(stream, term_get_child(t,1)); + break; + case TERM_META: + fputs("?", stream); + break; + case TERM_STRING: + fputs(t->value.string_value, stream); + break; + case TERM_INTEGER: + fprintf(stream, "%d", t->value.integer_value); + break; + default: + fprintf(stderr,"Error: term_print %d\n", t->type); + exit(1); + } +} diff --git a/old-lib/c/gfcc-term.h b/old-lib/c/gfcc-term.h new file mode 100644 index 000000000..d1307259d --- /dev/null +++ b/old-lib/c/gfcc-term.h @@ -0,0 +1,65 @@ +#ifndef GFCC_TERM_H +#define GFCC_TERM_H + +#include <stdio.h> + +typedef enum { + /* size = variable */ + TERM_ARRAY, + TERM_SEQUENCE, + TERM_VARIANTS, + TERM_GLUE, + /* size = 2 */ + TERM_RECORD_PARAM, + TERM_SUFFIX_TABLE, + /* size = 0 */ + TERM_META, + TERM_STRING, + TERM_INTEGER +} TermType; + +struct Term_ { + TermType type; + union { + const char *string_value; + int integer_value; + int size; + } value; + struct Term_ *args[0]; +}; + +typedef struct Term_ Term; + + + +static inline Term *term_get_child(Term *t, int n) { + return t->args[n]; +} + +static inline void term_set_child(Term *t, int n, Term *c) { + t->args[n] = c; +} + +extern void term_alloc_pool(size_t size); +extern void term_free_pool(); +extern void *term_alloc(size_t size); + + +extern Term *term_array(int n, ...); +extern Term *term_seq(int n, ...); +extern Term *term_variants(int n, ...); +extern Term *term_glue(int n, ...); + +extern Term *term_rp(Term *t1, Term *t2); +extern Term *term_suffix(const char *pref, Term *suf); +extern Term *term_str(const char *s); +extern Term *term_int(int i); +extern Term *term_meta(); + +extern Term *term_sel_int(Term *t, int i); +extern Term *term_sel(Term *t1, Term *t2); + + +extern void term_print(FILE *stream, Term *t); + +#endif diff --git a/old-lib/c/gfcc-tree.c b/old-lib/c/gfcc-tree.c new file mode 100644 index 000000000..6cd8759be --- /dev/null +++ b/old-lib/c/gfcc-tree.c @@ -0,0 +1,61 @@ +#include "gfcc-tree.h" + +#include <stdlib.h> + + +extern int arity(Tree *t) { + switch (t->type) { + case ATOM_STRING: + case ATOM_INTEGER: + case ATOM_DOUBLE: + case ATOM_META: + return 0; + default: + return t->value.size; + } +} + +static Tree *create_tree(atom_type c, int n) { + Tree *t = (Tree *)malloc(sizeof(Tree) + n * sizeof(Tree *)); + t->type = c; + return t; +} + +extern Tree *tree_string(const char *s) { + Tree *t = create_tree(ATOM_STRING, 0); + t->value.string_value = s; + return t; +} + +extern Tree *tree_integer(int i) { + Tree *t = create_tree(ATOM_INTEGER, 0); + t->value.integer_value = i; + return t; +} + +extern Tree *tree_double(double d) { + Tree *t = create_tree(ATOM_DOUBLE, 0); + t->value.double_value = d; + return t; +} + +extern Tree *tree_meta() { + return create_tree(ATOM_META, 0); +} + +extern Tree *tree_fun(atom_type f, int n) { + Tree *t = create_tree(f, n); + t->value.size = n; + return t; +} + + +extern void tree_free(Tree *t) { + int n = arity(t); + int i; + + for (i = 0; i < n; i++) { + tree_free(tree_get_child(t,i)); + } + free(t); +} diff --git a/old-lib/c/gfcc-tree.h b/old-lib/c/gfcc-tree.h new file mode 100644 index 000000000..cc8f0fcab --- /dev/null +++ b/old-lib/c/gfcc-tree.h @@ -0,0 +1,49 @@ +#ifndef GFCC_TREE_H +#define GFCC_TREE_H + +typedef enum { + ATOM_STRING, + ATOM_INTEGER, + ATOM_DOUBLE, + ATOM_META, + ATOM_FIRST_FUN +} atom_type; + +struct Tree_{ + atom_type type; + union { + const char *string_value; + int integer_value; + double double_value; + int size; + } value; + struct Tree_ *args[0]; +}; + +typedef struct Tree_ Tree; + +static inline Tree *tree_get_child(Tree *t, int n) { + return t->args[n]; +} + +static inline void tree_set_child(Tree *t, int n, Tree *a) { + t->args[n] = a; +} + +extern int arity(Tree *t); + + +extern Tree *tree_string(const char *s); + +extern Tree *tree_integer(int i); + +extern Tree *tree_double(double d); + +extern Tree *tree_meta(); + +extern Tree *tree_fun(atom_type f, int n); + + +extern void tree_free(Tree *t); + +#endif |
