diff options
-rw-r--r-- | interpreter/Makefile | 4 | ||||
-rw-r--r-- | interpreter/code.c | 36 | ||||
-rw-r--r-- | interpreter/code.h | 4 | ||||
-rw-r--r-- | interpreter/eval.c | 74 | ||||
-rw-r--r-- | interpreter/eval.h | 2 | ||||
-rw-r--r-- | interpreter/fuspel.c | 16 | ||||
-rw-r--r-- | interpreter/fuspel.h | 2 | ||||
-rw-r--r-- | interpreter/graphs.c | 16 | ||||
-rw-r--r-- | interpreter/graphs.h | 20 | ||||
-rw-r--r-- | interpreter/lex.c | 16 | ||||
-rw-r--r-- | interpreter/lex.h | 2 | ||||
-rw-r--r-- | interpreter/log.c | 9 | ||||
-rw-r--r-- | interpreter/log.h | 14 | ||||
-rw-r--r-- | interpreter/mem.c | 8 | ||||
-rw-r--r-- | interpreter/mem.h | 6 | ||||
-rw-r--r-- | interpreter/parse.c | 77 | ||||
-rw-r--r-- | interpreter/parse.h | 2 | ||||
-rw-r--r-- | interpreter/print.c | 33 | ||||
-rw-r--r-- | interpreter/print.h | 14 | ||||
-rw-r--r-- | interpreter/syntax.c | 34 | ||||
-rw-r--r-- | interpreter/syntax.h | 110 |
21 files changed, 236 insertions, 263 deletions
diff --git a/interpreter/Makefile b/interpreter/Makefile index 400db3d..a1fba34 100644 --- a/interpreter/Makefile +++ b/interpreter/Makefile @@ -1,7 +1,7 @@ override CFLAGS+=-Werror -Wall -Wextra -Wno-missing-field-initializers -O3 -D_FUSPEL_CLI -DEPS=lex.h syntax.h print.h parse.h log.h eval.h mem.h code.h graphs.h -OBJ=fuspel.o lex.o syntax.o print.o parse.o log.o eval.o mem.o code.o graphs.o +DEPS=fuspel.h lex.h syntax.h print.h parse.h eval.h mem.h code.h graphs.h +OBJ=fuspel.o lex.o syntax.o print.o parse.o eval.o mem.o code.o graphs.o EXE=fuspel .PHONY: all clean diff --git a/interpreter/code.c b/interpreter/code.c index ddd8402..b20b2ba 100644 --- a/interpreter/code.c +++ b/interpreter/code.c @@ -7,7 +7,7 @@ #include "mem.h" #include "print.h" -void fill_node_int(struct node** node, int i) { +void fill_node_int(struct node **node, int i) { unsigned int used_count = (*node)->used_count; free_node(*node, used_count, 0); (*node)->kind = NODE_INT; @@ -16,11 +16,11 @@ void fill_node_int(struct node** node, int i) { use_node(*node, used_count); } -void fill_node_bool(struct node** node, int i) { +void fill_node_bool(struct node **node, int i) { fill_node_int(node, i ? 1 : 0); } -void fill_node_name(struct node** node, char* s) { +void fill_node_name(struct node **node, char *s) { unsigned int used_count = (*node)->used_count; free_node(*node, used_count, 0); (*node)->kind = NODE_NAME; @@ -29,11 +29,11 @@ void fill_node_name(struct node** node, char* s) { use_node(*node, used_count); } -void code_time(struct node** result) { +void code_time(struct node **result) { fill_node_int(result, (int) time(NULL)); } -void code_trace(struct node** result, struct node *p, struct node *r) { +void code_trace(struct node **result, struct node *p, struct node *r) { print_node(p); printf("\n"); use_node(r, (*result)->used_count); @@ -41,63 +41,63 @@ void code_trace(struct node** result, struct node *p, struct node *r) { *result = r; } -void code_add(struct node** result, struct node* a, struct node* b) { +void code_add(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "add on non-ints"); else fill_node_int(result, *((int*) b->var1) + *((int*) a->var1)); } -void code_mul(struct node** result, struct node* a, struct node* b) { +void code_mul(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "mul on non-ints"); else fill_node_int(result, *((int*) a->var1) * *((int*) b->var1)); } -void code_sub(struct node** result, struct node* a, struct node* b) { +void code_sub(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "sub on non-ints"); else fill_node_int(result, *((int*) b->var1) - *((int*) a->var1)); } -void code_eq(struct node** result, struct node* a, struct node* b) { +void code_eq(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "eq on non-ints"); else fill_node_bool(result, *((int*) a->var1) == *((int*) b->var1)); } -void code_gt(struct node** result, struct node* a, struct node* b) { +void code_gt(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "gt on non-ints"); else fill_node_bool(result, *((int*) a->var1) > *((int*) b->var1)); } -void code_ge(struct node** result, struct node* a, struct node* b) { +void code_ge(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "ge on non-ints"); else fill_node_bool(result, *((int*) a->var1) >= *((int*) b->var1)); } -void code_lt(struct node** result, struct node* a, struct node* b) { +void code_lt(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "lt on non-ints"); else fill_node_bool(result, *((int*) a->var1) < *((int*) b->var1)); } -void code_le(struct node** result, struct node* a, struct node* b) { +void code_le(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "le on non-ints"); else fill_node_bool(result, *((int*) a->var1) <= *((int*) b->var1)); } -void code_ne(struct node** result, struct node* a, struct node* b) { +void code_ne(struct node **result, struct node *a, struct node *b) { if (a->kind != NODE_INT || b->kind != NODE_INT) fill_node_name(result, "ne on non-ints"); else @@ -105,8 +105,8 @@ void code_ne(struct node** result, struct node* a, struct node* b) { } struct code_mapping { - char* name; - void* f; + char *name; + void *f; unsigned char arity; }; @@ -125,7 +125,7 @@ static struct code_mapping code_table[] = { { NULL } }; -unsigned char code_find(char* name, void** function) { +unsigned char code_find(char *name, void **function) { struct code_mapping *entry = code_table; while (entry) { if (!strcmp(name, entry->name)) { @@ -140,7 +140,7 @@ unsigned char code_find(char* name, void** function) { } #ifdef _FUSPEL_DEBUG -char *code_find_name(void* f) { +char *code_find_name(void *f) { struct code_mapping *entry = code_table; while (entry) { if (f == entry->f) diff --git a/interpreter/code.h b/interpreter/code.h index 8b7d060..0703fc5 100644 --- a/interpreter/code.h +++ b/interpreter/code.h @@ -8,10 +8,10 @@ typedef void (Code_0) (struct node**); typedef void (Code_1) (struct node**, struct node*); typedef void (Code_2) (struct node**, struct node*, struct node*); -unsigned char code_find(char* name, void** function); +unsigned char code_find(char *name, void **function); #ifdef _FUSPEL_DEBUG -char *code_find_name(void* f); +char *code_find_name(void *f); #endif #endif diff --git a/interpreter/eval.c b/interpreter/eval.c index a9d2edc..097dd21 100644 --- a/interpreter/eval.c +++ b/interpreter/eval.c @@ -11,27 +11,27 @@ #include "print.h" #endif -typedef struct { - char* name; - struct node* node; -} replacement; +struct replacement { + char *name; + struct node *node; +}; -typedef struct replacements { - replacement replacement; - struct replacements* rest; -} replacements; +struct replacements { + struct replacement replacement; + struct replacements *rest; +}; -void eval(fuspel* rules, struct node** node, bool to_rnf); +void eval(struct fuspel *rules, struct node **node, bool to_rnf); -void push_repl(replacements* repls, char* name, struct node* node) { +void push_repl(struct replacements *repls, char *name, struct node *node) { while (repls->rest) repls = repls->rest; - repls->rest = my_calloc(1, sizeof(replacements)); + repls->rest = my_calloc(1, sizeof(struct replacements)); repls->rest->replacement.name = name; repls->rest->replacement.node = node; repls->rest->rest = NULL; } -void free_repls(replacements* repls) { +void free_repls(struct replacements *repls) { if (repls) { if (repls->rest) { free_repls(repls->rest); @@ -43,7 +43,7 @@ void free_repls(replacements* repls) { } } -void replace_all(fuspel *rules, replacements* repls, struct node** node) { +void replace_all(struct fuspel *rules, struct replacements *repls, struct node **node) { unsigned int org_used_count; if (!node || !*node) @@ -89,9 +89,9 @@ void replace_all(fuspel *rules, replacements* repls, struct node** node) { } } -bool match_expr(fuspel* rules, expression* expr, struct node** node, - replacements* repls) { - replacements* _repls; +bool match_expr(struct fuspel *rules, struct expression *expr, struct node **node, + struct replacements *repls) { + struct replacements *_repls; for (_repls = repls; _repls->rest; _repls = _repls->rest); remove_redirects(*node); @@ -126,9 +126,9 @@ bool match_expr(fuspel* rules, expression* expr, struct node** node, } } -int match_rule(fuspel* rules, rewrite_rule* rule, struct node** node, - replacements* repls) { - struct node*** node_args; +int match_rule(struct fuspel *rules, struct rewrite_rule *rule, struct node **node, + struct replacements *repls) { + struct node ***node_args; unsigned char i; switch ((*node)->kind) { @@ -140,8 +140,8 @@ int match_rule(fuspel* rules, rewrite_rule* rule, struct node** node, node_args = flatten_app_args(node, true); i = 0; if (!strcmp((*node_args[0])->var1, rule->name)) { - struct node** node = node_args[++i]; - arg_list* args = rule->args; + struct node **node = node_args[++i]; + struct arg_list *args = rule->args; unsigned char args_len = len_arg_list(args); while (!empty_args_list(args)) { @@ -176,15 +176,15 @@ int match_rule(fuspel* rules, rewrite_rule* rule, struct node** node, } } -bool is_code_app(struct node* node) { +bool is_code_app(struct node *node) { for (; node->kind == NODE_APP; node = node->var1); return node->kind == NODE_CODE; } -void eval_code_app(fuspel* rules, struct node** node) { +void eval_code_app(struct fuspel *rules, struct node **node) { struct node *root, ***args; - Code_1* f1; - Code_2* f2; + Code_1 *f1; + Code_2 *f2; unsigned char i; for (root = *node; root->kind == NODE_APP; root = root->var1); @@ -209,21 +209,21 @@ void eval_code_app(fuspel* rules, struct node** node) { } #ifdef _FUSPEL_DEBUG -struct node** root_node; +struct node **root_node; #endif -void eval(fuspel* rules, struct node** node, bool to_rnf) { - fuspel* _rules; +void eval(struct fuspel *rules, struct node **node, bool to_rnf) { + struct fuspel *_rules; bool rerun; #ifdef _FUSPEL_DEBUG bool print_graph; #endif - replacements* repls; + struct replacements *repls; if (!node || !*node) return; - repls = my_calloc(1, sizeof(replacements)); + repls = my_calloc(1, sizeof(struct replacements)); #ifdef _FUSPEL_DEBUG if (!root_node) { @@ -258,9 +258,9 @@ void eval(fuspel* rules, struct node** node, bool to_rnf) { if (add_args >= 0) { unsigned char j; unsigned int org_used_count; - replacements* _repls; - struct node** _node = node; - struct node* new_node = my_calloc(1, sizeof(struct node)); + struct replacements *_repls; + struct node **_node = node; + struct node *new_node = my_calloc(1, sizeof(struct node)); for (j = 0; j < add_args; j++) _node = (struct node**) &(*_node)->var1; @@ -323,7 +323,7 @@ void eval(fuspel* rules, struct node** node, bool to_rnf) { case NODE_CODE: if (*((unsigned char*) (*node)->var2) == 0) { - Code_0* code_fun = (Code_0*) (*node)->var1; + Code_0 *code_fun = (Code_0*) (*node)->var1; code_fun(node); use_node(*node, 1); rerun = 1; @@ -346,9 +346,9 @@ void eval(fuspel* rules, struct node** node, bool to_rnf) { my_free(repls); } -expression* eval_main(fuspel* rules) { - struct node* main_node = my_calloc(1, sizeof(struct node)); - expression* expr = my_calloc(1, sizeof(expression)); +struct expression *eval_main(struct fuspel *rules) { + struct node *main_node = my_calloc(1, sizeof(struct node)); + struct expression *expr = my_calloc(1, sizeof(struct expression)); main_node->kind = EXPR_NAME; main_node->used_count = 1; diff --git a/interpreter/eval.h b/interpreter/eval.h index 1383456..3561965 100644 --- a/interpreter/eval.h +++ b/interpreter/eval.h @@ -3,6 +3,6 @@ #include "syntax.h" -expression* eval_main(fuspel*); +struct expression *eval_main(struct fuspel*); #endif diff --git a/interpreter/fuspel.c b/interpreter/fuspel.c index 1cbdd4c..b9ccd12 100644 --- a/interpreter/fuspel.c +++ b/interpreter/fuspel.c @@ -14,11 +14,11 @@ #define LINE_LENGTH 139 -fuspel* import(fuspel* already_parsed, char* fname) { - token_list* tokens = NULL; - fuspel* pgm; - FILE* f; - char* fname_; +struct fuspel *import(struct fuspel *already_parsed, char *fname) { + struct token_list *tokens = NULL; + struct fuspel *pgm; + FILE *f; + char *fname_; fname_ = my_calloc(1, strlen(fname) + 6); strcpy(fname_, fname); @@ -67,7 +67,7 @@ static struct argp_option options[] = { { 0 } }; struct environment { - fuspel* program; + struct fuspel *program; bool printProgram; }; @@ -88,8 +88,8 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 }; -int main(int argc, char* argv[]) { - expression* result; +int main(int argc, char *argv[]) { + struct expression *result; struct environment env; env.printProgram = false; diff --git a/interpreter/fuspel.h b/interpreter/fuspel.h index cc7a3a7..0de4c8c 100644 --- a/interpreter/fuspel.h +++ b/interpreter/fuspel.h @@ -1,6 +1,6 @@ #ifndef _H_FUSPEL #define _H_FUSPEL -fuspel* import(fuspel* already_parsed, char* name); +struct fuspel *import(struct fuspel *already_parsed, char *name); #endif diff --git a/interpreter/graphs.c b/interpreter/graphs.c index 48284ec..91f9a19 100644 --- a/interpreter/graphs.c +++ b/interpreter/graphs.c @@ -4,7 +4,7 @@ #include "mem.h" -void use_node(struct node* node, unsigned int count) { +void use_node(struct node *node, unsigned int count) { if (!node) return; @@ -27,7 +27,7 @@ void use_node(struct node* node, unsigned int count) { } } -void free_node(struct node* node, unsigned int count, bool free_first) { +void free_node(struct node *node, unsigned int count, bool free_first) { if (!node) return; @@ -63,11 +63,11 @@ void free_node(struct node* node, unsigned int count, bool free_first) { } } -struct node*** flatten_app_args(struct node** from, bool remove_redirs) { +struct node ***flatten_app_args(struct node **from, bool remove_redirs) { struct node ***result; unsigned int i; unsigned char len = 0; - struct node* _from = *from; + struct node *_from = *from; if (remove_redirs) remove_redirects(_from); @@ -104,7 +104,7 @@ void remove_redirects(struct node *node) { } } -void cpy_expression_to_node(struct node* dst, expression* src) { +void cpy_expression_to_node(struct node *dst, struct expression *src) { if (!dst || !src) return; @@ -143,7 +143,7 @@ void cpy_expression_to_node(struct node* dst, expression* src) { dst->used_count = 1; } -void cpy_node_to_expression(expression* dst, struct node* src) { +void cpy_node_to_expression(struct expression *dst, struct node *src) { if (!dst || !src) return; @@ -172,11 +172,11 @@ void cpy_node_to_expression(expression* dst, struct node* src) { case NODE_APP: dst->var1 = dst->var2 = NULL; if (src->var1) { - dst->var1 = my_calloc(1, sizeof(expression)); + dst->var1 = my_calloc(1, sizeof(struct expression)); cpy_node_to_expression(dst->var1, src->var1); } if (src->var2) { - dst->var2 = my_calloc(1, sizeof(expression)); + dst->var2 = my_calloc(1, sizeof(struct expression)); cpy_node_to_expression(dst->var2, src->var2); } break; diff --git a/interpreter/graphs.h b/interpreter/graphs.h index 204993c..6c82bcc 100644 --- a/interpreter/graphs.h +++ b/interpreter/graphs.h @@ -4,7 +4,7 @@ #include <stdbool.h> #include "syntax.h" -typedef enum { +enum node_kind{ NODE_INT, /* See expr_kind in syntax.h */ NODE_NAME, NODE_CODE, @@ -13,23 +13,23 @@ typedef enum { NODE_APP, NODE_REDIRECT /* Redirect to another node */ -} node_kind; +}; struct node { - node_kind kind; - void* var1; - void* var2; + enum node_kind kind; + void *var1; + void *var2; unsigned int used_count; }; -void use_node(struct node* node, unsigned int count); -void free_node(struct node* node, unsigned int count, bool free_first); +void use_node(struct node *node, unsigned int count); +void free_node(struct node *node, unsigned int count, bool free_first); -struct node*** flatten_app_args(struct node** from, bool remove_redirs); +struct node ***flatten_app_args(struct node **from, bool remove_redirs); void remove_redirects(struct node *node); -void cpy_expression_to_node(struct node* dst, expression* src); -void cpy_node_to_expression(expression* dst, struct node* src); +void cpy_expression_to_node(struct node *dst, struct expression *src); +void cpy_node_to_expression(struct expression *dst, struct node *src); #endif diff --git a/interpreter/lex.c b/interpreter/lex.c index ecf681f..5032bfb 100644 --- a/interpreter/lex.c +++ b/interpreter/lex.c @@ -14,7 +14,7 @@ inline bool is_int_char(char input) { } /* The number of bytes that should be read to read an integer */ -unsigned char lex_int_length(char* input) { +unsigned char lex_int_length(char *input) { unsigned char n = 0; while (is_int_char(*input++)) n++; return n; @@ -27,14 +27,14 @@ inline bool is_name_char(char input) { } /* The number of bytes that should be read to read a name */ -unsigned char lex_name_length(char* input) { +unsigned char lex_name_length(char *input) { unsigned char n = 0; while (is_name_char(*input++)) n++; return n; } -token_list* lex(token_list* list, char* input) { - token_list* first_list; +struct token_list *lex(struct token_list *list, char *input) { + struct token_list *first_list; bool create_new_token; while (*input && is_space_char(*input)) input++; @@ -45,10 +45,10 @@ token_list* lex(token_list* list, char* input) { if (list) { first_list = list; while (list->rest) list = list->rest; - list->rest = my_calloc(1, sizeof(token_list)); + list->rest = my_calloc(1, sizeof(struct token_list)); list = list->rest; } else { - first_list = list = my_calloc(1, sizeof(token_list)); + first_list = list = my_calloc(1, sizeof(struct token_list)); } create_new_token = 1; @@ -84,7 +84,7 @@ token_list* lex(token_list* list, char* input) { break; } if (is_int_char(*input)) { - char* s; + char *s; unsigned char len = lex_int_length(input); s = my_calloc(1, len + 1); list->elem.kind = TOKEN_INT; @@ -111,7 +111,7 @@ token_list* lex(token_list* list, char* input) { do input++; while (*input && is_space_char(*input)); if (*input && create_new_token) { - list->rest = my_calloc(1, sizeof(token_list)); + list->rest = my_calloc(1, sizeof(struct token_list)); list = list->rest; } } diff --git a/interpreter/lex.h b/interpreter/lex.h index 268d0e4..e6f525b 100644 --- a/interpreter/lex.h +++ b/interpreter/lex.h @@ -3,6 +3,6 @@ #include "syntax.h" -token_list* lex(token_list*, char*); +struct token_list *lex(struct token_list*, char*); #endif diff --git a/interpreter/log.c b/interpreter/log.c deleted file mode 100644 index c369b4b..0000000 --- a/interpreter/log.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "log.h" - -#include <stdio.h> - -#if(LOG_LEVEL < LOG_DEBUG) -void log_debug(char* msg) { - fprintf(stdout, "%s\n", msg); -} -#endif diff --git a/interpreter/log.h b/interpreter/log.h deleted file mode 100644 index 7d4e406..0000000 --- a/interpreter/log.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef _H_LOG -#define _H_LOG - -#define LOG_DEBUG 1 - -#define LOG_LEVEL 10 - -#if(LOG_LEVEL >= LOG_DEBUG) -#define log_debug(x) -#else -void log_debug(char*); -#endif - -#endif diff --git a/interpreter/mem.c b/interpreter/mem.c index db061c4..a9a2947 100644 --- a/interpreter/mem.c +++ b/interpreter/mem.c @@ -2,8 +2,8 @@ #include <stdio.h> -void* my_calloc(size_t num, size_t size) { - void* ptr = calloc(num, size); +void *my_calloc(size_t num, size_t size) { + void *ptr = calloc(num, size); if (!ptr) { perror(NULL); exit(EXIT_FAILURE); @@ -11,7 +11,7 @@ void* my_calloc(size_t num, size_t size) { return ptr; } -void* my_realloc(void* ptr, size_t size) { +void *my_realloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (!ptr) { perror(NULL); @@ -20,6 +20,6 @@ void* my_realloc(void* ptr, size_t size) { return ptr; } -void my_free(void* ptr) { +void my_free(void *ptr) { free(ptr); } diff --git a/interpreter/mem.h b/interpreter/mem.h index 5757311..62169e9 100644 --- a/interpreter/mem.h +++ b/interpreter/mem.h @@ -3,8 +3,8 @@ #include <stdlib.h> -void* my_calloc(size_t num, size_t size); -void* my_realloc(void* ptr, size_t size); -void my_free(void* ptr); +void *my_calloc(size_t num, size_t size); +void *my_realloc(void *ptr, size_t size); +void my_free(void *ptr); #endif diff --git a/interpreter/parse.c b/interpreter/parse.c index bdfd968..b949cb1 100644 --- a/interpreter/parse.c +++ b/interpreter/parse.c @@ -3,12 +3,11 @@ #include <string.h> #include "code.h" -#include "log.h" #include "mem.h" -extern fuspel* import(fuspel* already_parsed, char* name); +extern struct fuspel *import(struct fuspel *already_parsed, char *name); -token_list* parse_name(char** name, token_list* list) { +struct token_list *parse_name(char **name, struct token_list *list) { if (list->elem.kind != TOKEN_NAME) return NULL; @@ -19,8 +18,8 @@ token_list* parse_name(char** name, token_list* list) { return list->rest; } -token_list* parse_simple_expression(expression* expr, token_list* list) { - expression* _expr; +struct token_list *parse_simple_expression(struct expression *expr, struct token_list *list) { + struct expression *_expr; switch (list->elem.kind) { case TOKEN_INT: @@ -36,7 +35,7 @@ token_list* parse_simple_expression(expression* expr, token_list* list) { case TOKEN_CODE: if (list->rest && list->rest->elem.kind == TOKEN_NAME) { - char* name; + char *name; expr->kind = EXPR_CODE; list = parse_name(&name, list->rest); expr->var2 = my_calloc(1, sizeof(unsigned char)); @@ -57,12 +56,12 @@ token_list* parse_simple_expression(expression* expr, token_list* list) { return list->rest; break; case TOKEN_COMMA: - _expr = my_calloc(1, sizeof(expression)); + _expr = my_calloc(1, sizeof(struct expression)); cpy_expression(_expr, expr); free_expression(expr); expr->kind = EXPR_TUPLE; expr->var1 = _expr; - expr->var2 = my_calloc(1, sizeof(expression)); + expr->var2 = my_calloc(1, sizeof(struct expression)); list = parse_simple_expression(expr->var2, list->rest); if (!list || list->elem.kind != TOKEN_CLOSE_P) { free_expression(_expr); @@ -86,8 +85,8 @@ token_list* parse_simple_expression(expression* expr, token_list* list) { if (list->elem.kind == TOKEN_CLOSE_SQ) return list->rest; - expr->var1 = my_calloc(1, sizeof(expression)); - expr->var2 = my_calloc(1, sizeof(expression)); + expr->var1 = my_calloc(1, sizeof(struct expression)); + expr->var2 = my_calloc(1, sizeof(struct expression)); list = parse_simple_expression(expr->var1, list); @@ -115,11 +114,11 @@ token_list* parse_simple_expression(expression* expr, token_list* list) { } } -token_list* parse_arg_list(arg_list** args, token_list* list) { +struct token_list *parse_arg_list(struct arg_list **args, struct token_list *list) { if (list->elem.kind == TOKEN_EQUALS) return list; - *args = my_calloc(1, sizeof(arg_list)); + *args = my_calloc(1, sizeof(struct arg_list)); list = parse_simple_expression(&(*args)->elem, list); if (!list) @@ -130,23 +129,23 @@ token_list* parse_arg_list(arg_list** args, token_list* list) { return list; } -token_list* parse_expression(expression*, token_list*); +struct token_list *parse_expression(struct expression*, struct token_list*); -token_list* parse_expression_no_app(expression* expr, token_list* list) { +struct token_list *parse_expression_no_app(struct expression *expr, struct token_list *list) { if (list->elem.kind == TOKEN_OPEN_P) { - token_list* _list = parse_expression(expr, list->rest); + struct token_list *_list = parse_expression(expr, list->rest); if (_list) { if (_list->elem.kind == TOKEN_CLOSE_P) { return _list->rest; } else if (_list->elem.kind == TOKEN_COMMA) { - expression* _expr = my_calloc(1, sizeof(expression)); + struct expression *_expr = my_calloc(1, sizeof(struct expression)); cpy_expression(_expr, expr); free_expression(expr); expr->kind = EXPR_TUPLE; expr->var1 = _expr; - expr->var2 = my_calloc(1, sizeof(expression)); + expr->var2 = my_calloc(1, sizeof(struct expression)); _list = parse_expression(expr->var2, _list->rest); if (_list && _list->elem.kind == TOKEN_CLOSE_P) { @@ -156,7 +155,7 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { free_expression(expr); } } else if (list->elem.kind == TOKEN_OPEN_SQ) { - token_list* _list; + struct token_list *_list; expr->kind = EXPR_LIST; @@ -164,8 +163,8 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { return list->rest->rest; } - expr->var1 = my_calloc(1, sizeof(expression)); - expr->var2 = my_calloc(1, sizeof(expression)); + expr->var1 = my_calloc(1, sizeof(struct expression)); + expr->var2 = my_calloc(1, sizeof(struct expression)); _list = parse_expression(expr->var1, list->rest); if (!_list || @@ -174,17 +173,17 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { _list->elem.kind != TOKEN_CLOSE_SQ)) { free_expression(expr); } else if (_list->elem.kind == TOKEN_CLOSE_SQ) { - ((expression *) expr->var2)->kind = EXPR_LIST; - ((expression *) expr->var2)->var1 = NULL; - ((expression *) expr->var2)->var2 = NULL; + ((struct expression *) expr->var2)->kind = EXPR_LIST; + ((struct expression *) expr->var2)->var1 = NULL; + ((struct expression *) expr->var2)->var2 = NULL; return _list->rest; } else if (_list->elem.kind == TOKEN_COMMA) { - expression* _expr = expr; + struct expression *_expr = expr; while (_list && _list->elem.kind == TOKEN_COMMA) { _expr = _expr->var2; _expr->kind = EXPR_LIST; - _expr->var1 = my_calloc(1, sizeof(expression)); - _expr->var2 = my_calloc(1, sizeof(expression)); + _expr->var1 = my_calloc(1, sizeof(struct expression)); + _expr->var2 = my_calloc(1, sizeof(struct expression)); _list = parse_expression(_expr->var1, _list->rest); } if (!_list || @@ -192,16 +191,16 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { _list->elem.kind != TOKEN_COLON)) { free_expression(expr); } else if (_list->elem.kind == TOKEN_COLON) { - _list = parse_expression(((expression*) expr->var2)->var2, _list->rest); + _list = parse_expression(((struct expression*) expr->var2)->var2, _list->rest); if (!_list || _list->elem.kind != TOKEN_CLOSE_SQ) { free_expression(expr); } else { return _list->rest; } } else if (_list->elem.kind == TOKEN_CLOSE_SQ) { - ((expression*) _expr->var2)->kind = EXPR_LIST; - ((expression*) _expr->var2)->var1 = NULL; - ((expression*) _expr->var2)->var2 = NULL; + ((struct expression*) _expr->var2)->kind = EXPR_LIST; + ((struct expression*) _expr->var2)->var1 = NULL; + ((struct expression*) _expr->var2)->var2 = NULL; return _list->rest; } } else if (_list->elem.kind == TOKEN_COLON) { @@ -219,7 +218,7 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { return list; } -token_list* parse_expression(expression* expr, token_list* list) { +struct token_list *parse_expression(struct expression *expr, struct token_list *list) { list = parse_expression_no_app(expr, list); if (!list) return NULL; @@ -230,14 +229,14 @@ token_list* parse_expression(expression* expr, token_list* list) { list->elem.kind != TOKEN_COLON && list->elem.kind != TOKEN_COMMA) { - expression* _expr = my_calloc(1, sizeof(expression)); + struct expression *_expr = my_calloc(1, sizeof(struct expression)); cpy_expression(_expr, expr); free_expression(expr); expr->kind = EXPR_APP; expr->var1 = _expr; - expr->var2 = my_calloc(1, sizeof(expression)); + expr->var2 = my_calloc(1, sizeof(struct expression)); list = parse_expression_no_app(expr->var2, list); if (!list) { @@ -249,20 +248,18 @@ token_list* parse_expression(expression* expr, token_list* list) { return list; } -token_list* parse_rule(rewrite_rule* rule, token_list* list) { +struct token_list *parse_rule(struct rewrite_rule *rule, struct token_list *list) { list = parse_name(&rule->name, list); if (!list) return NULL; list = parse_arg_list(&rule->args, list); if (!list) { - log_debug("parse_rule: error in arg_list"); my_free(rule->name); return NULL; } if (list->elem.kind != TOKEN_EQUALS) { - log_debug("parse_rule: no ="); my_free(rule->name); free_arg_list(rule->args); return NULL; @@ -273,9 +270,9 @@ token_list* parse_rule(rewrite_rule* rule, token_list* list) { return list; } -fuspel* parse(token_list* list) { - fuspel* rules = NULL; - fuspel* return_rules; +struct fuspel *parse(struct token_list *list) { + struct fuspel *rules = NULL; + struct fuspel *return_rules; while (list && list->elem.kind == TOKEN_SEMICOLON) list = list->rest; @@ -296,7 +293,7 @@ fuspel* parse(token_list* list) { return_rules = rules; while (rules->rest) rules = rules->rest; } else { - return_rules = rules = my_calloc(1, sizeof(fuspel)); + return_rules = rules = my_calloc(1, sizeof(struct fuspel)); list = parse_rule(&rules->rule, list); if (!list) diff --git a/interpreter/parse.h b/interpreter/parse.h index 39f74e0..d265a01 100644 --- a/interpreter/parse.h +++ b/interpreter/parse.h @@ -3,6 +3,6 @@ #include "syntax.h" -fuspel* parse(token_list*); +struct fuspel *parse(struct token_list*); #endif diff --git a/interpreter/print.c b/interpreter/print.c index bc1f6dc..9126b41 100644 --- a/interpreter/print.c +++ b/interpreter/print.c @@ -7,10 +7,9 @@ #include "code.h" #endif -#include "log.h" #include "mem.h" -void print_token(token* tk) { +void print_token(struct token *tk) { char c = NULL; switch (tk->kind) { case TOKEN_SEMICOLON: c = ';'; break; @@ -34,7 +33,7 @@ void print_token(token* tk) { printf("%c", c); } -void print_token_list(token_list* list) { +void print_token_list(struct token_list *list) { print_token(&list->elem); if (list->rest) { printf(list->elem.kind == TOKEN_SEMICOLON ? "\n" : " "); @@ -42,14 +41,14 @@ void print_token_list(token_list* list) { } } -void print_in_list(expression* expr) { +void print_in_list(struct expression *expr) { if (!expr->var1) return; print_expression(expr->var1); - if (((expression*) expr->var2)->kind == EXPR_LIST) { - if (((expression*) expr->var2)->var1) { + if (((struct expression*) expr->var2)->kind == EXPR_LIST) { + if (((struct expression*) expr->var2)->var1) { printf(","); print_in_list(expr->var2); } @@ -59,7 +58,7 @@ void print_in_list(expression* expr) { } } -void print_expression(expression* expr) { +void print_expression(struct expression *expr) { if (!expr) return; @@ -87,22 +86,22 @@ void print_expression(expression* expr) { printf(")"); break; case EXPR_APP: - if (((expression*) expr->var1)->kind == EXPR_APP) + if (((struct expression*) expr->var1)->kind == EXPR_APP) printf("("); print_expression(expr->var1); - if (((expression*) expr->var1)->kind == EXPR_APP) + if (((struct expression*) expr->var1)->kind == EXPR_APP) printf(")"); printf(" "); - if (((expression*) expr->var2)->kind == EXPR_APP) + if (((struct expression*) expr->var2)->kind == EXPR_APP) printf("("); print_expression(expr->var2); - if (((expression*) expr->var2)->kind == EXPR_APP) + if (((struct expression*) expr->var2)->kind == EXPR_APP) printf(")"); break; } } -void print_arg_list(arg_list* args) { +void print_arg_list(struct arg_list *args) { if (!args) return; @@ -112,7 +111,7 @@ void print_arg_list(arg_list* args) { print_arg_list(args->rest); } -void print_rewrite_rule(rewrite_rule* rule) { +void print_rewrite_rule(struct rewrite_rule *rule) { printf("%s ", rule->name); print_arg_list(rule->args); printf("= "); @@ -120,7 +119,7 @@ void print_rewrite_rule(rewrite_rule* rule) { printf(";"); } -void print_fuspel(fuspel* rules) { +void print_fuspel(struct fuspel *rules) { print_rewrite_rule(&rules->rule); if (rules->rest) { printf("\n"); @@ -128,8 +127,8 @@ void print_fuspel(fuspel* rules) { } } -void print_node(struct node* node) { - expression* e = my_calloc(1, sizeof(expression)); +void print_node(struct node *node) { + struct expression *e = my_calloc(1, sizeof(struct expression)); cpy_node_to_expression(e, node); print_expression(e); free_expression(e); @@ -182,7 +181,7 @@ void *get_app_root(struct node *node) { return node; } -void print_node_to_file(struct node* node, FILE* f, struct visited_nodes *visited) { +void print_node_to_file(struct node *node, FILE *f, struct visited_nodes *visited) { bool close = 0; bool do_free_visited = 0; diff --git a/interpreter/print.h b/interpreter/print.h index cc191b9..43a9d19 100644 --- a/interpreter/print.h +++ b/interpreter/print.h @@ -6,19 +6,19 @@ #include <stdio.h> -void print_token(token*); -void print_token_list(token_list*); +void print_token(struct token*); +void print_token_list(struct token_list*); -void print_expression(expression*); -void print_rewrite_rule(rewrite_rule*); -void print_fuspel(fuspel*); +void print_expression(struct expression*); +void print_rewrite_rule(struct rewrite_rule*); +void print_fuspel(struct fuspel*); void print_node(struct node*); #ifdef _FUSPEL_DEBUG struct visited_nodes { - struct node* node; - struct visited_nodes* next; + struct node *node; + struct visited_nodes *next; }; void print_node_to_file(struct node*, FILE*, struct visited_nodes*); diff --git a/interpreter/syntax.c b/interpreter/syntax.c index 81d9493..c79d482 100644 --- a/interpreter/syntax.c +++ b/interpreter/syntax.c @@ -4,23 +4,23 @@ #include "mem.h" -void free_token(token* tk) { +void free_token(struct token *tk) { if (tk->var) my_free(tk->var); } -void free_token_list(token_list* list) { +void free_token_list(struct token_list *list) { free_token(&list->elem); if (list->rest) free_token_list(list->rest); my_free(list->rest); } -bool empty_args_list(arg_list* list) { +bool empty_args_list(struct arg_list *list) { return !list; } -unsigned char len_arg_list(arg_list* list) { +unsigned char len_arg_list(struct arg_list *list) { unsigned char i = 0; while (list) { i++; @@ -29,7 +29,7 @@ unsigned char len_arg_list(arg_list* list) { return i; } -void cpy_expression(expression* dst, expression* src) { +void cpy_expression(struct expression *dst, struct expression *src) { free_expression(dst); dst->kind = src->kind; switch (dst->kind) { @@ -51,15 +51,15 @@ void cpy_expression(expression* dst, expression* src) { break; case EXPR_TUPLE: case EXPR_APP: - dst->var1 = my_calloc(1, sizeof(expression)); - dst->var2 = my_calloc(1, sizeof(expression)); + dst->var1 = my_calloc(1, sizeof(struct expression)); + dst->var2 = my_calloc(1, sizeof(struct expression)); cpy_expression(dst->var1, src->var1); cpy_expression(dst->var2, src->var2); break; } } -bool eq_expression(expression* a, expression* b) { +bool eq_expression(struct expression *a, struct expression *b) { if (a->kind != b->kind) return 0; @@ -83,7 +83,7 @@ bool eq_expression(expression* a, expression* b) { return 0; } -void concat_fuspel(fuspel* start, fuspel* end) { +void concat_fuspel(struct fuspel *start, struct fuspel *end) { while (start) { if (!start->rest) { start->rest = end; @@ -93,18 +93,18 @@ void concat_fuspel(fuspel* start, fuspel* end) { } } -fuspel* push_fuspel(fuspel* rules) { - fuspel* new_rules = my_calloc(1, sizeof(fuspel)); +struct fuspel *push_fuspel(struct fuspel *rules) { + struct fuspel *new_rules = my_calloc(1, sizeof(struct fuspel)); new_rules->rest = rules; return new_rules; } -fuspel* pop_fuspel(fuspel* rules) { +struct fuspel *pop_fuspel(struct fuspel *rules) { free_rewrite_rule(&rules->rule); return rules->rest; } -fuspel* popn_fuspel(fuspel* rules, unsigned char n) { +struct fuspel *popn_fuspel(struct fuspel *rules, unsigned char n) { while (n > 0) { rules = pop_fuspel(rules); n--; @@ -112,7 +112,7 @@ fuspel* popn_fuspel(fuspel* rules, unsigned char n) { return rules; } -void free_expression(expression* expr) { +void free_expression(struct expression *expr) { if (!expr) return; @@ -137,14 +137,14 @@ void free_expression(expression* expr) { expr->var1 = expr->var2 = NULL; } -void free_arg_list(arg_list* list) { +void free_arg_list(struct arg_list *list) { free_expression(&list->elem); if (list->rest) free_arg_list(list->rest); my_free(list->rest); } -void free_rewrite_rule(rewrite_rule* rule) { +void free_rewrite_rule(struct rewrite_rule *rule) { my_free(rule->name); if (rule->args) free_arg_list(rule->args); @@ -152,7 +152,7 @@ void free_rewrite_rule(rewrite_rule* rule) { free_expression(&rule->rhs); } -void free_fuspel(fuspel* rules) { +void free_fuspel(struct fuspel *rules) { free_rewrite_rule(&rules->rule); if (rules->rest) free_fuspel(rules->rest); diff --git a/interpreter/syntax.h b/interpreter/syntax.h index 0333013..09ba3bc 100644 --- a/interpreter/syntax.h +++ b/interpreter/syntax.h @@ -5,7 +5,7 @@ /* TOKENS */ -typedef enum { +enum token_kind { TOKEN_SEMICOLON, /* ; */ TOKEN_EQUALS, /* = */ TOKEN_OPEN_SQ, /* [ */ @@ -18,69 +18,69 @@ typedef enum { TOKEN_IMPORT, /* import */ TOKEN_NAME, TOKEN_INT -} token_kind; +}; -typedef struct { - token_kind kind; - void* var; -} token; +struct token { + enum token_kind kind; + void *var; +}; -typedef struct token_list { - token elem; - struct token_list* rest; -} token_list; +struct token_list { + struct token elem; + struct token_list *rest; +}; -void free_token(token*); -void free_token_list(token_list*); +void free_token(struct token*); +void free_token_list(struct token_list*); /* ELEMENTS */ -typedef enum { +enum expr_kind { EXPR_INT, /* var1: pointer to int */ EXPR_NAME, /* var1: pointer to char* */ EXPR_CODE, /* var1: pointer to function; var2: pointer to unsigned char (nr. of arguments) */ - EXPR_LIST, /* var1, var2: pointers to expression OR (nil) */ - EXPR_TUPLE, /* var1, var2: pointers to expression */ - EXPR_APP /* var1, var2: pointers to expression */ -} expr_kind; - -typedef struct { - expr_kind kind; - void* var1; - void* var2; -} expression; - -typedef struct arg_list { - expression elem; - struct arg_list* rest; -} arg_list; - -typedef struct { - char* name; - arg_list* args; - expression rhs; -} rewrite_rule; - -typedef struct fuspel { - rewrite_rule rule; - struct fuspel* rest; -} fuspel; - -bool empty_args_list(arg_list*); -unsigned char len_arg_list(arg_list*); - -void cpy_expression(expression* dst, expression* src); -bool eq_expression(expression*, expression*); - -void concat_fuspel(fuspel* start, fuspel* end); -fuspel* push_fuspel(fuspel*); -fuspel* pop_fuspel(fuspel*); -fuspel* popn_fuspel(fuspel*, unsigned char); - -void free_expression(expression*); -void free_arg_list(arg_list*); -void free_rewrite_rule(rewrite_rule*); -void free_fuspel(fuspel*); + EXPR_LIST, /* var1, var2: pointers to struct expression OR (nil) */ + EXPR_TUPLE, /* var1, var2: pointers to struct expression */ + EXPR_APP /* var1, var2: pointers to struct expression */ +}; + +struct expression { + enum expr_kind kind; + void *var1; + void *var2; +}; + +struct arg_list { + struct expression elem; + struct arg_list *rest; +}; + +struct rewrite_rule { + char *name; + struct arg_list *args; + struct expression rhs; +}; + +struct fuspel { + struct rewrite_rule rule; + struct fuspel *rest; +}; + +bool empty_args_list(struct arg_list*); +unsigned char len_arg_list(struct arg_list*); + +void cpy_expression(struct expression *dst, struct expression *src); +bool eq_expression(struct expression*, struct expression*); + +void concat_fuspel(struct fuspel *start, struct fuspel *end); +struct fuspel *push_fuspel(struct fuspel*); +struct fuspel *pop_fuspel(struct fuspel*); +struct fuspel *popn_fuspel(struct fuspel*, unsigned char); + +void free_expression(struct expression*); +void free_arg_list(struct arg_list*); +void free_rewrite_rule(struct rewrite_rule*); +void free_fuspel(struct fuspel*); #endif |