diff --git a/astree.c b/astree.c new file mode 100644 index 0000000..3c2e7e8 --- /dev/null +++ b/astree.c @@ -0,0 +1,55 @@ +#include "astree.h" +#include +#include + +struct astree *ast_create(int type, struct hm_item *symbol, + struct astree *c0, + struct astree *c1, + struct astree *c2, + struct astree *c3) +{ + struct astree *node = calloc(1, sizeof(struct astree)); + + if (!node) { + perror("astree: node alocation"); + return NULL; + } + + node->type = type; + node->symbol = symbol; + node->children[0] = c0; + node->children[1] = c1; + node->children[2] = c2; + node->children[3] = c3; + + return node; +} + +static const char *type_to_string[] = { + "symbol", + "+", + "-", + "*", + "/" +}; + +void ast_fprint(FILE *stream, int level, struct astree *tree) { + if (!tree) { + return; + } + + int i; + for (i = 0; i < level; i++) { + fprintf(stream, "| "); + } + + fprintf(stream, "%s", type_to_string[tree->type]); + if (tree->type == AST_SYM) { + fprintf(stream, ": %s", tree->symbol->key); + } + fprintf(stream, "\n"); + + for (i = 0; i < AST_MAXCHILDREN; i++) { + ast_fprint(stream, level + 1, tree->children[i]); + } +} diff --git a/astree.h b/astree.h new file mode 100644 index 0000000..55f4f3d --- /dev/null +++ b/astree.h @@ -0,0 +1,31 @@ +#ifndef AST_H +#define AST_H + +#include "hashmap.h" + +#define AST_MAXCHILDREN 4 + +#define AST_SYM 0 +#define AST_ADD 1 +#define AST_SUB 2 +#define AST_MUL 3 +#define AST_DIV 4 + +struct astree { + int type; + struct hm_item *symbol; + struct astree *children[AST_MAXCHILDREN]; +}; + +/* Create a tree node with a type, a pointer to the symbol table + * and children.*/ +struct astree *ast_create(int type, struct hm_item *symbol, + struct astree *c0, + struct astree *c1, + struct astree *c2, + struct astree *c3); + +/* Pretty print a tree to a stream. */ +void ast_fprint(FILE *stream, int level, struct astree *tree); + +#endif /* ifndef AST_H */ diff --git a/hashmap.c b/hashmap.c index 0c776ac..2d5c73a 100644 --- a/hashmap.c +++ b/hashmap.c @@ -184,6 +184,12 @@ int hm_put(struct hashmap *hm, const char *key, const void *value) { return -2; } +struct hm_item *hm_getref(struct hashmap *hm, const char *key) { + struct hm_item **item = hm_find(hm, key); + + return *item; +} + int hm_get(struct hashmap *hm, const char *key, void *value) { struct hm_item **item = hm_find(hm, key); diff --git a/hashmap.h b/hashmap.h index f0c1d20..91478cd 100644 --- a/hashmap.h +++ b/hashmap.h @@ -28,6 +28,9 @@ void hm_terminate(struct hashmap *hm); /* Insert (key, value) pair. */ int hm_put(struct hashmap *hm, const char *key, const void *value); +/* Retrieve a reference to an item */ +struct hm_item *hm_getref(struct hashmap *hm, const char *key); + /* Retrive (key, value) pair. */ int hm_get(struct hashmap *hm, const char *key, void *value); diff --git a/sample.txt b/sample.txt deleted file mode 100644 index bf421ca..0000000 --- a/sample.txt +++ /dev/null @@ -1,45 +0,0 @@ -// UFRGS - Compiladores - Marcelo Johann - 2017/1 - -a: short 0; -i: long 0; -v: long[10] 0 0 0 0 0 0 0 0 0 0; -mat: long[10]; -f: float 10.55; -c: byte 'x'; -d: byte 65; - - -long main() - { - a = 10; - v#2 = v[10-1]; - for (i = 1 to 10) - print i " "; - - print " Digite um numero: \n"; - read i; - while (i<10) - { - a = incn(a,1); - i = i + 1; - a = (i + a + 1); - }; - print "Incrementado algumas vezes fica " a "\n"; - when (a==5) then - print "Parou no meio\n"; - when (a==11) then - ; - when (a==11) then else - ; - ;;; - {;}; - {}; - }; - -long incn (long x, long n) - return x+n; - -short voidfunc() ; - - - diff --git a/tests/Makefile b/tests/Makefile index 2798479..2b83f08 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,11 +1,13 @@ -ALL_TARGETS := test_hashmap +ALL_TARGETS := test_hashmap test_astree .PHONY = all all: $(ALL_TARGETS) test_hashmap: test_hashmap.c ../hashmap.c gcc -I .. -o test_hashmap $^ - ./test_hashmap + +test_astree: test_astree.c ../hashmap.c ../astree.c + gcc -Wall -g -I .. -o test_astree $^ .PHONY = clean clean: diff --git a/tests/test_astree.c b/tests/test_astree.c new file mode 100644 index 0000000..bce96fa --- /dev/null +++ b/tests/test_astree.c @@ -0,0 +1,28 @@ +#include +#include "astree.h" +#include "hashmap.h" + +int main(int argc, char *argv[]) +{ + struct hashmap map; + hm_initialize(10, 0.8, 1, &map); + + /* put some garbage in the hashmap */ + int n1, n2; + hm_put(&map, "1", &n1); + hm_put(&map, "2", &n2); + struct hm_item *s1 = hm_getref(&map, "1"); + struct hm_item *s2 = hm_getref(&map, "2"); + + /* build 1 + 2 */ + struct astree *t1 = ast_create(AST_SYM, s1, NULL, NULL, NULL, NULL); + struct astree *t2 = ast_create(AST_SYM, s2, NULL, NULL, NULL, NULL); + struct astree *sum = ast_create(AST_ADD, NULL, t1, t2, NULL, NULL); + ast_fprint(stderr, 0, sum); + + hm_terminate(&map); + free(t1); + free(t2); + free(sum); + return 0; +}