Skip to content

Commit

Permalink
add abstract syntax tree code and some tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luís Möllmann committed May 11, 2017
1 parent 12a46c1 commit 7d55afc
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 47 deletions.
55 changes: 55 additions & 0 deletions astree.c
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]);
}
}
31 changes: 31 additions & 0 deletions astree.h
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 */
6 changes: 6 additions & 0 deletions hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
45 changes: 0 additions & 45 deletions sample.txt

This file was deleted.

6 changes: 4 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_astree.c
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;
}

0 comments on commit 7d55afc

Please sign in to comment.