block: don't put spaces around :
[ironout.git] / ast.c
blob9e4c990a210906aca154b22acf5c8f5f8385d267
1 #include <stdlib.h>
2 #include <string.h>
3 #include "ast.h"
6 void node_walk(struct node *node,
7 int (*callback) (struct node *, void *),
8 void *data)
10 int i;
11 if (callback(node, data))
12 for (i = 0; i < node->count; i++)
13 node_walk(node->children[i], callback, data);
16 struct node_search {
17 long offset;
18 struct node *result;
21 static int search_node(struct node *node, void *data)
23 struct node_search *search = data;
24 long offset = search->offset;
25 if (node->start <= offset && offset < node->end) {
26 search->result = node;
27 return 1;
29 return 0;
32 struct node *node_find(struct node *node, long offset)
34 struct node_search search;
35 search.offset = offset;
36 search.result = NULL;
37 node_walk(node, search_node, &search);
38 return search.result;
41 int node_cmp(struct node *n1, struct node *n2)
43 int i;
44 if (n1->type != n2->type || n1->count != n2->count)
45 return 1;
46 /* node->data should be compared based on node->type */
47 if (n1->type == AST_IDENTIFIER || n1->type == AST_TYPENAME)
48 if (strcmp(n1->data, n2->data))
49 return 1;
50 for (i = 0; i < n1->count; i++)
51 if (node_cmp(n1->children[i], n2->children[i]))
52 return 1;
53 return 0;
56 struct node *declarator_name(struct node *node)
58 struct node *cur = node;
59 while (cur->count) {
60 if (cur->type == AST_DECL)
61 cur = cur->children[cur->count - 1];
62 else
63 cur = cur->children[0];
65 if (cur->type == AST_IDENTIFIER)
66 return cur;
67 return NULL;