-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add abstract syntax tree code and some tests for it.
- Loading branch information
Luís Möllmann
committed
May 11, 2017
1 parent
12a46c1
commit 7d55afc
Showing
7 changed files
with
127 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "astree.h" | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <stdlib.h> | ||
#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; | ||
} |