diff options
author | Camil Staps | 2016-08-25 20:29:50 +0200 |
---|---|---|
committer | Camil Staps | 2016-08-25 20:29:50 +0200 |
commit | 821939c6a6eb5761708146783ebd562422c2a7f7 (patch) | |
tree | dd26b29c65431801f4ca45d6952b7806534f9c8c | |
parent | Remove unnecessary includes (diff) |
pedantic
-rw-r--r-- | interpreter/.gitignore (renamed from compiler/.gitignore) | 0 | ||||
-rw-r--r-- | interpreter/Makefile (renamed from compiler/Makefile) | 2 | ||||
-rw-r--r-- | interpreter/eval.c (renamed from compiler/eval.c) | 29 | ||||
-rw-r--r-- | interpreter/eval.h (renamed from compiler/eval.h) | 0 | ||||
-rw-r--r-- | interpreter/fuspelc.c (renamed from compiler/fuspelc.c) | 10 | ||||
-rw-r--r-- | interpreter/lex.c (renamed from compiler/lex.c) | 18 | ||||
-rw-r--r-- | interpreter/lex.h (renamed from compiler/lex.h) | 0 | ||||
-rw-r--r-- | interpreter/log.c (renamed from compiler/log.c) | 0 | ||||
-rw-r--r-- | interpreter/log.h (renamed from compiler/log.h) | 0 | ||||
-rw-r--r-- | interpreter/mem.c (renamed from compiler/mem.c) | 0 | ||||
-rw-r--r-- | interpreter/mem.h (renamed from compiler/mem.h) | 0 | ||||
-rw-r--r-- | interpreter/parse.c (renamed from compiler/parse.c) | 15 | ||||
-rw-r--r-- | interpreter/parse.h (renamed from compiler/parse.h) | 0 | ||||
-rw-r--r-- | interpreter/print.c (renamed from compiler/print.c) | 0 | ||||
-rw-r--r-- | interpreter/print.h (renamed from compiler/print.h) | 0 | ||||
-rw-r--r-- | interpreter/syntax.c (renamed from compiler/syntax.c) | 7 | ||||
-rw-r--r-- | interpreter/syntax.h (renamed from compiler/syntax.h) | 20 |
17 files changed, 60 insertions, 41 deletions
diff --git a/compiler/.gitignore b/interpreter/.gitignore index 9148247..9148247 100644 --- a/compiler/.gitignore +++ b/interpreter/.gitignore diff --git a/compiler/Makefile b/interpreter/Makefile index a8cd4a1..c8bfeb7 100644 --- a/compiler/Makefile +++ b/interpreter/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-Wall -s +CFLAGS=-Wall -Wextra -Werror -Wpedantic -s DEPS=lex.h syntax.h print.h parse.h log.h eval.h mem.h OBJ=fuspelc.o lex.o syntax.o print.o parse.o log.o eval.o mem.o diff --git a/compiler/eval.c b/interpreter/eval.c index c9059be..fb07e92 100644 --- a/compiler/eval.c +++ b/interpreter/eval.c @@ -1,13 +1,15 @@ #include "eval.h" +#include <stdio.h> #include <string.h> #include "mem.h" +#include "print.h" void free_rules_until(fuspel* new, fuspel* old) { while (new != old) { - free_rewrite_rule(&new->rule); fuspel* _new = new->rest; + free_rewrite_rule(&new->rule); my_free(new); new = _new; } @@ -68,14 +70,14 @@ void free_replacements(replacements* repls) { unsigned match_expr(fuspel* rules, expression* to_match, expression* expr, replacements** repls) { + unsigned matches; + if (to_match->kind != EXPR_NAME) { expr = eval_rnf(rules, expr); if (!expr) return 0; } - unsigned matches; - switch (to_match->kind) { case EXPR_NAME: *repls = push_replacement(to_match->var1, expr, *repls); @@ -88,7 +90,7 @@ unsigned match_expr(fuspel* rules, expression* to_match, expression* expr, my_free(expr); return matches; case EXPR_LIST: - if (!to_match->var1) { // empty list + if (!to_match->var1) { matches = eq_expression(to_match, expr); free_expression(expr); my_free(expr); @@ -115,13 +117,16 @@ unsigned match_expr(fuspel* rules, expression* to_match, expression* expr, unsigned match_rule(fuspel* rules, rewrite_rule* rule, expression* expr, replacements** repls) { + expression** expr_args; + unsigned char i; + switch (expr->kind) { case EXPR_NAME: return (!strcmp(expr->var1, rule->name) && empty_args_list(rule->args)) ? 1 : 0; case EXPR_APP: - ;expression** expr_args = flatten_app_args(expr); - unsigned char i = 0; + expr_args = flatten_app_args(expr); + i = 0; if (!strcmp(expr_args[0]->var1, rule->name)) { expression* _expr = expr_args[++i]; arg_list* args = rule->args; @@ -169,11 +174,11 @@ expression* eval_rnf(fuspel* rules, expression* expr) { case EXPR_APP: while (_rules) { if (match_rule(rules, &_rules->rule, expr, repls)) { + expression* old_result = result; cpy_expression(result, &_rules->rule.rhs); replace_all(*repls, result); free_replacements(*repls); my_free(repls); - expression* old_result = result; result = eval_rnf(rules, old_result); free_expression(old_result); my_free(old_result); @@ -195,13 +200,15 @@ expression* eval_rnf(fuspel* rules, expression* expr) { } expression* eval(fuspel* rules, expression* expr) { - expression* result = my_calloc(1, sizeof(expression)); - expression *e1, *e2; fuspel* _rules = rules; - + expression* result = my_calloc(1, sizeof(expression)); replacements** repls = my_calloc(1, sizeof(replacements*)); + printf("Evaluating: "); + print_expression(expr); + printf("\n"); + switch (expr->kind) { case EXPR_INT: cpy_expression(result, expr); @@ -211,9 +218,9 @@ expression* eval(fuspel* rules, expression* expr) { case EXPR_APP: while (_rules) { if (match_rule(rules, &_rules->rule, expr, repls)) { + expression* old_result = result; cpy_expression(result, &_rules->rule.rhs); replace_all(*repls, result); - expression* old_result = result; result = eval(rules, old_result); free_expression(old_result); my_free(old_result); diff --git a/compiler/eval.h b/interpreter/eval.h index 3acd1d0..3acd1d0 100644 --- a/compiler/eval.h +++ b/interpreter/eval.h diff --git a/compiler/fuspelc.c b/interpreter/fuspelc.c index 48ca8cb..5edb48c 100644 --- a/compiler/fuspelc.c +++ b/interpreter/fuspelc.c @@ -8,7 +8,11 @@ #include "print.h" int main(void) { - token_list* tokens = NULL; + token_list* tokens; + fuspel* pgm; + expression to_eval, *evaled; + + tokens = NULL; while (!feof(stdin)) { char program[79]; @@ -26,7 +30,7 @@ int main(void) { } } - fuspel* pgm = parse(tokens); + pgm = parse(tokens); free_token_list(tokens); free(tokens); @@ -39,8 +43,6 @@ int main(void) { print_fuspel(pgm); printf("\n\n\n"); - expression to_eval, *evaled; - to_eval.kind = EXPR_NAME; to_eval.var1 = my_calloc(1, 5); strcpy(to_eval.var1, "main"); diff --git a/compiler/lex.c b/interpreter/lex.c index 2459881..b238b5e 100644 --- a/compiler/lex.c +++ b/interpreter/lex.c @@ -12,7 +12,7 @@ inline unsigned is_int_char(char input) { return '0' <= input && input <= '9'; } -// The number of bytes that should be read to read an integer +/* The number of bytes that should be read to read an integer */ unsigned char lex_int_length(char* input) { unsigned char n = 0; while (is_int_char(*input++)) n++; @@ -25,7 +25,7 @@ inline unsigned is_name_char(char input) { input == '_'); } -// The number of bytes that should be read to read a name +/* The number of bytes that should be read to read a name */ unsigned char lex_name_length(char* input) { unsigned char n = 0; while (is_name_char(*input++)) n++; @@ -33,11 +33,12 @@ unsigned char lex_name_length(char* input) { } token_list* lex(token_list* list, char* input) { + token_list* first_list; + if (input[0] == 0) { return NULL; } - token_list* first_list; if (list) { first_list = list; while (list->rest) list = list->rest; @@ -48,10 +49,10 @@ token_list* lex(token_list* list, char* input) { } while (*input) { - list->elem.var = NULL; - unsigned proceed_to_next_token = 1; + list->elem.var = NULL; + switch (*input) { case ';': list->elem.kind = TOKEN_SEMICOLON; break; case ':': list->elem.kind = TOKEN_COLON; break; @@ -64,17 +65,18 @@ token_list* lex(token_list* list, char* input) { default: if (is_int_char(*input)) { - list->elem.kind = TOKEN_INT; + char* s; unsigned char len = lex_int_length(input); - char* s = my_calloc(1, len + 1); + s = my_calloc(1, len + 1); + list->elem.kind = TOKEN_INT; list->elem.var = my_calloc(1, sizeof(int)); strncpy(s, input, len); *((int*) list->elem.var) = atoi(s); my_free(s); input += len - 1; } else if (is_name_char(*input)) { - list->elem.kind = TOKEN_NAME; unsigned char len = lex_name_length(input); + list->elem.kind = TOKEN_NAME; list->elem.var = my_calloc(1, len + 1); strncpy(list->elem.var, input, len); input += len - 1; diff --git a/compiler/lex.h b/interpreter/lex.h index 268d0e4..268d0e4 100644 --- a/compiler/lex.h +++ b/interpreter/lex.h diff --git a/compiler/log.c b/interpreter/log.c index c369b4b..c369b4b 100644 --- a/compiler/log.c +++ b/interpreter/log.c diff --git a/compiler/log.h b/interpreter/log.h index 7d4e406..7d4e406 100644 --- a/compiler/log.h +++ b/interpreter/log.h diff --git a/compiler/mem.c b/interpreter/mem.c index 7affd71..7affd71 100644 --- a/compiler/mem.c +++ b/interpreter/mem.c diff --git a/compiler/mem.h b/interpreter/mem.h index 0399cef..0399cef 100644 --- a/compiler/mem.h +++ b/interpreter/mem.h diff --git a/compiler/parse.c b/interpreter/parse.c index b5ae273..a9fc345 100644 --- a/compiler/parse.c +++ b/interpreter/parse.c @@ -17,6 +17,7 @@ token_list* parse_name(char** name, token_list* list) { } token_list* parse_simple_expression(expression* expr, token_list* list) { + expression* _expr; switch (list->elem.kind) { case TOKEN_INT: expr->kind = EXPR_INT; @@ -34,7 +35,6 @@ token_list* parse_simple_expression(expression* expr, token_list* list) { if (!list) return NULL; - expression* _expr; switch (list->elem.kind) { case TOKEN_CLOSE_P: return list->rest; @@ -139,16 +139,19 @@ token_list* parse_expression_no_app(expression* expr, token_list* list) { free_expression(expr); } } else if (list->elem.kind == TOKEN_OPEN_SQ) { + expression *expr1, *expr2; + token_list* _list; + expr->kind = EXPR_LIST; if (list->rest->elem.kind == TOKEN_CLOSE_SQ) { return list->rest->rest; } - expression* expr1 = my_calloc(1, sizeof(expression)); - expression* expr2 = my_calloc(1, sizeof(expression)); + expr1 = my_calloc(1, sizeof(expression)); + expr2 = my_calloc(1, sizeof(expression)); - token_list* _list = parse_expression(expr1, list->rest); + _list = parse_expression(expr1, list->rest); if (!_list || _list->elem.kind != TOKEN_COLON) { free_expression(expr1); my_free(expr1); @@ -228,13 +231,15 @@ token_list* parse_rule(rewrite_rule* rule, token_list* list) { } fuspel* parse(token_list* list) { + fuspel* rules; + while (list && list->elem.kind == TOKEN_SEMICOLON) list = list->rest; if (!list) return NULL; - fuspel* rules = my_calloc(1, sizeof(fuspel)); + rules = my_calloc(1, sizeof(fuspel)); list = parse_rule(&rules->rule, list); if (!list) diff --git a/compiler/parse.h b/interpreter/parse.h index 39f74e0..39f74e0 100644 --- a/compiler/parse.h +++ b/interpreter/parse.h diff --git a/compiler/print.c b/interpreter/print.c index a71e71f..a71e71f 100644 --- a/compiler/print.c +++ b/interpreter/print.c diff --git a/compiler/print.h b/interpreter/print.h index 5909087..5909087 100644 --- a/compiler/print.h +++ b/interpreter/print.h diff --git a/compiler/syntax.c b/interpreter/syntax.c index 0f80461..4eb845a 100644 --- a/compiler/syntax.c +++ b/interpreter/syntax.c @@ -69,6 +69,9 @@ unsigned eq_expression(expression* a, expression* b) { } expression** flatten_app_args(expression* from) { + expression** result; + unsigned int i; + unsigned char len = 0; expression* _from = from; while (_from->kind == EXPR_APP) { @@ -77,8 +80,8 @@ expression** flatten_app_args(expression* from) { } len++; - expression** result = my_calloc(1, sizeof(expression*) * (len + 1)); - unsigned int i = 1; + result = my_calloc(1, sizeof(expression*) * (len + 1)); + i = 1; while (from->kind == EXPR_APP) { result[len - i] = from->var2; from = from->var1; diff --git a/compiler/syntax.h b/interpreter/syntax.h index f71786c..05f09c7 100644 --- a/compiler/syntax.h +++ b/interpreter/syntax.h @@ -1,17 +1,17 @@ #ifndef _H_SYNTAX #define _H_SYNTAX -// TOKENS +/* TOKENS */ typedef enum { - TOKEN_SEMICOLON, // ; - TOKEN_EQUALS, // = - TOKEN_OPEN_SQ, // [ - TOKEN_CLOSE_SQ, // ] - TOKEN_OPEN_P, // ( - TOKEN_CLOSE_P, // ) - TOKEN_COMMA, // , - TOKEN_COLON, // : + TOKEN_SEMICOLON, /* ; */ + TOKEN_EQUALS, /* = */ + TOKEN_OPEN_SQ, /* [ */ + TOKEN_CLOSE_SQ, /* ] */ + TOKEN_OPEN_P, /* ( */ + TOKEN_CLOSE_P, /* ) */ + TOKEN_COMMA, /* , */ + TOKEN_COLON, /* : */ TOKEN_NAME, TOKEN_INT } token_kind; @@ -29,7 +29,7 @@ typedef struct token_list { void free_token(token*); void free_token_list(token_list*); -// ELEMENTS +/* ELEMENTS */ typedef enum { EXPR_INT, |